2 #ifndef __DALI_ARRAY2D_H__
3 #define __DALI_ARRAY2D_H__
6 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
8 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
22 #include <dali/public-api/common/dali-vector.h>
28 * Helper wrapper for two dimensional array using std::vector
31 * Array2d< int > intArray( 3, 3 );
32 * intArray[ 0 ][ 0 ] = 10;
33 * intArray.Resize( 4, 4 );
36 template< typename T >
42 * Default constructor. Creates a 0x0 array
45 : mArray( 0, std::vector< T >( 0 ) )
49 * Constructs an array with given dimensions
50 * @param [in] rows for array
51 * @param [in] columns for array
53 Array2d( unsigned int rows, unsigned int columns )
54 : mArray( rows, std::vector< T >( columns ) )
61 { } // Nothing to do, vector cleans up itself
65 * @param array to copy from
67 Array2d( const Array2d& array )
71 mArray = array.mArray;
77 * @param array to copy from
78 * @return reference to self for chaining
80 Array2d& operator=( const Array2d& array )
84 mArray = array.mArray;
90 * @return the number of rows in the array
92 unsigned int GetRows()
98 * @return the number of columns in the array
100 unsigned int GetColumns()
102 if( mArray.size() > 0 )
104 // all columns are equal length
105 return mArray[ 0 ].size();
111 * @param [in] index of the row
112 * @return reference to the row vector for given index
114 std::vector< T >& operator[]( unsigned int index )
116 return mArray[ index ];
120 * @param [in] index of the row
121 * @return const reference to the row vector for given index
123 const std::vector< T >& operator[]( unsigned int index ) const
125 return mArray[ index ];
129 * Insert a new row to given index
130 * @param [in] rowIndex of the new row
132 void InsertRow( unsigned int rowIndex )
134 // insert default initialized row of elements
135 mArray.insert( mArray.begin() + rowIndex, std::vector< T >( GetColumns() ) );
139 * Delete a row from given index
140 * Removed elements are deleted
141 * @param [in] rowIndex of the row to delete
143 void DeleteRow( unsigned int rowIndex )
146 mArray.erase( mArray.begin() + rowIndex );
150 * Delete a row from given index
151 * @param [in] rowIndex of the row to delete
152 * @param [out] removed elements
154 void DeleteRow( unsigned int rowIndex, std::vector< T >& removed )
156 // copy the row elements
157 removed.insert( removed.end(), mArray[ rowIndex ].begin(), mArray[ rowIndex ].end() );
159 mArray.erase( mArray.begin() + rowIndex );
163 * Insert a new column to given index
164 * @param [in] columnIndex of the new column
166 void InsertColumn( unsigned int columnIndex )
168 // go through all rows
169 const unsigned int rows = GetRows();
170 for( unsigned int i = 0; i < rows; ++i )
172 // insert default initialized element
173 mArray[ i ].insert( mArray[ i ].begin() + columnIndex, T() );
178 * Delete a column from given index.
179 * Removed elements are deleted
180 * @param [in] columnIndex of the column to delete
182 void DeleteColumn( unsigned int columnIndex )
184 // go through all rows
185 const unsigned int rows = GetRows();
186 for( unsigned int i = 0; i < rows; ++i )
189 mArray[ i ].erase( mArray[ i ].begin() + columnIndex );
194 * Delete a column from given index
195 * @param [in] columnIndex of the column to delete
196 * @param [out] removed elements
198 void DeleteColumn( unsigned int columnIndex, std::vector< T >& removed )
200 // go through all rows
201 const unsigned int rows = GetRows();
202 for( unsigned int i = 0; i < rows; ++i )
204 // copy the column element of this row
205 removed.push_back( mArray[ i ][ columnIndex ] );
207 mArray[ i ].erase( mArray[ i ].begin() + columnIndex );
212 * Resizes the array to given dimensions
213 * If new size is smaller in either dimension, items that wont fit will be deleted
214 * @param [in] rows for array
215 * @param [in] columns for array
217 void Resize( unsigned int rows, unsigned int columns )
219 // resize rows first, may increase or decrease size
220 mArray.resize( rows );
221 for( unsigned int i = 0; i < rows; ++i )
223 // resize each column, may increase or decrease size
224 mArray[ i ].resize( columns );
229 * Resizes the array to given dimensions
230 * If new size is smaller, items that wont fit will be returned to caller
231 * @param [in] rows for array
232 * @param [in] columns for array
233 * @param [out] removed elements in case array is smaller in either dimension
235 void Resize( unsigned int rows, unsigned int columns, std::vector< T >& removed )
237 // remember old counts
238 const unsigned int oldRows = GetRows();
239 const unsigned int oldColumns = GetColumns();
240 // are rows being removed ?
243 // gather the elements of removed rows
244 for( unsigned int i = rows; i < oldRows; ++i )
246 // copy the row elements, the whole row is gone
247 removed.insert( removed.end(), mArray[ i ].begin(), mArray[ i ].end() );
250 // resize the rows, may increase or decrease size
251 mArray.resize( rows );
252 // process columns, need to do all rows as also columns for new row need resizing
253 for( unsigned int i = 0; i < rows; ++i )
255 // if this is an old row and columns are being removed
256 if( ( i < oldRows )&&( columns < oldColumns ) )
258 // copy the columns, the end of row from columns is gone
259 removed.insert( removed.end(), mArray[ i ].begin() + columns, mArray[ i ].end() );
261 // resize the column, may increase of decrease size
262 mArray[ i ].resize( columns );
268 std::vector< std::vector< T > > mArray;
274 #endif // __DALI_ARRAY2D_H__