2 #ifndef __DALI_ARRAY2D_H__
3 #define __DALI_ARRAY2D_H__
6 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 #include <dali/public-api/common/dali-vector.h>
29 * Helper wrapper for two dimensional array using std::vector
32 * Array2d< int > intArray( 3, 3 );
33 * intArray[ 0 ][ 0 ] = 10;
34 * intArray.Resize( 4, 4 );
37 template< typename T >
43 * Default constructor. Creates a 0x0 array
46 : mArray( 0, std::vector< T >( 0 ) )
50 * Constructs an array with given dimensions
51 * @param [in] rows for array
52 * @param [in] columns for array
54 Array2d( unsigned int rows, unsigned int columns )
55 : mArray( rows, std::vector< T >( columns ) )
62 { } // Nothing to do, vector cleans up itself
66 * @param array to copy from
68 Array2d( const Array2d& array )
72 mArray = array.mArray;
78 * @param array to copy from
79 * @return reference to self for chaining
81 Array2d& operator=( const Array2d& array )
85 mArray = array.mArray;
91 * @return the number of rows in the array
93 unsigned int GetRows()
99 * @return the number of columns in the array
101 unsigned int GetColumns()
103 if( mArray.size() > 0 )
105 // all columns are equal length
106 return mArray[ 0 ].size();
112 * @param [in] index of the row
113 * @return reference to the row vector for given index
115 std::vector< T >& operator[]( unsigned int index )
117 return mArray[ index ];
121 * @param [in] index of the row
122 * @return const reference to the row vector for given index
124 const std::vector< T >& operator[]( unsigned int index ) const
126 return mArray[ index ];
130 * Insert a new row to given index
131 * @param [in] rowIndex of the new row
133 void InsertRow( unsigned int rowIndex )
135 // insert default initialized row of elements
136 mArray.insert( mArray.begin() + rowIndex, std::vector< T >( GetColumns() ) );
140 * Delete a row from given index
141 * Removed elements are deleted
142 * @param [in] rowIndex of the row to delete
144 void DeleteRow( unsigned int rowIndex )
147 mArray.erase( mArray.begin() + rowIndex );
151 * Delete a row from given index
152 * @param [in] rowIndex of the row to delete
153 * @param [out] removed elements
155 void DeleteRow( unsigned int rowIndex, std::vector< T >& removed )
157 // copy the row elements
158 removed.insert( removed.end(), mArray[ rowIndex ].begin(), mArray[ rowIndex ].end() );
160 mArray.erase( mArray.begin() + rowIndex );
164 * Insert a new column to given index
165 * @param [in] columnIndex of the new column
167 void InsertColumn( unsigned int columnIndex )
169 // go through all rows
170 const unsigned int rows = GetRows();
171 for( unsigned int i = 0; i < rows; ++i )
173 // insert default initialized element
174 mArray[ i ].insert( mArray[ i ].begin() + columnIndex, T() );
179 * Delete a column from given index.
180 * Removed elements are deleted
181 * @param [in] columnIndex of the column to delete
183 void DeleteColumn( unsigned int columnIndex )
185 // go through all rows
186 const unsigned int rows = GetRows();
187 for( unsigned int i = 0; i < rows; ++i )
190 mArray[ i ].erase( mArray[ i ].begin() + columnIndex );
195 * Delete a column from given index
196 * @param [in] columnIndex of the column to delete
197 * @param [out] removed elements
199 void DeleteColumn( unsigned int columnIndex, std::vector< T >& removed )
201 // go through all rows
202 const unsigned int rows = GetRows();
203 for( unsigned int i = 0; i < rows; ++i )
205 // copy the column element of this row
206 removed.push_back( mArray[ i ][ columnIndex ] );
208 mArray[ i ].erase( mArray[ i ].begin() + columnIndex );
213 * Resizes the array to given dimensions
214 * If new size is smaller in either dimension, items that wont fit will be deleted
215 * @param [in] rows for array
216 * @param [in] columns for array
218 void Resize( unsigned int rows, unsigned int columns )
220 // resize rows first, may increase or decrease size
221 mArray.resize( rows );
222 for( unsigned int i = 0; i < rows; ++i )
224 // resize each column, may increase or decrease size
225 mArray[ i ].resize( columns );
230 * Resizes the array to given dimensions
231 * If new size is smaller, items that wont fit will be returned to caller
232 * @param [in] rows for array
233 * @param [in] columns for array
234 * @param [out] removed elements in case array is smaller in either dimension
236 void Resize( unsigned int rows, unsigned int columns, std::vector< T >& removed )
238 // remember old counts
239 const unsigned int oldRows = GetRows();
240 const unsigned int oldColumns = GetColumns();
241 // are rows being removed ?
244 // gather the elements of removed rows
245 for( unsigned int i = rows; i < oldRows; ++i )
247 // copy the row elements, the whole row is gone
248 removed.insert( removed.end(), mArray[ i ].begin(), mArray[ i ].end() );
251 // resize the rows, may increase or decrease size
252 mArray.resize( rows );
253 // process columns, need to do all rows as also columns for new row need resizing
254 for( unsigned int i = 0; i < rows; ++i )
256 // if this is an old row and columns are being removed
257 if( ( i < oldRows )&&( columns < oldColumns ) )
259 // copy the columns, the end of row from columns is gone
260 removed.insert( removed.end(), mArray[ i ].begin() + columns, mArray[ i ].end() );
262 // resize the column, may increase of decrease size
263 mArray[ i ].resize( columns );
269 std::vector< std::vector< T > > mArray;
275 #endif // __DALI_ARRAY2D_H__