6 * Copyright (c) 2021 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>
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 );
41 * Default constructor. Creates a 0x0 array
44 : 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))
63 } // Nothing to do, vector cleans up itself
67 * @param array to copy from
69 Array2d(const Array2d& array)
73 mArray = array.mArray;
79 * @param array to copy from
80 * @return reference to self for chaining
82 Array2d& operator=(const Array2d& array)
86 mArray = array.mArray;
92 * @return the number of rows in the array
94 unsigned int GetRows()
100 * @return the number of columns in the array
102 unsigned int GetColumns()
104 if(mArray.size() > 0)
106 // all columns are equal length
107 return mArray[0].size();
113 * @param [in] index of the row
114 * @return reference to the row vector for given index
116 std::vector<T>& operator[](unsigned int index)
118 return mArray[index];
122 * @param [in] index of the row
123 * @return const reference to the row vector for given index
125 const std::vector<T>& operator[](unsigned int index) const
127 return mArray[index];
131 * Insert a new row to given index
132 * @param [in] rowIndex of the new row
134 void InsertRow(unsigned int rowIndex)
136 // insert default initialized row of elements
137 mArray.insert(mArray.begin() + rowIndex, std::vector<T>(GetColumns()));
141 * Delete a row from given index
142 * Removed elements are deleted
143 * @param [in] rowIndex of the row to delete
145 void DeleteRow(unsigned int rowIndex)
148 mArray.erase(mArray.begin() + rowIndex);
152 * Delete a row from given index
153 * @param [in] rowIndex of the row to delete
154 * @param [out] removed elements
156 void DeleteRow(unsigned int rowIndex, std::vector<T>& removed)
158 // copy the row elements
159 removed.insert(removed.end(), mArray[rowIndex].begin(), mArray[rowIndex].end());
161 mArray.erase(mArray.begin() + rowIndex);
165 * Insert a new column to given index
166 * @param [in] columnIndex of the new column
168 void InsertColumn(unsigned int columnIndex)
170 // go through all rows
171 const unsigned int rows = GetRows();
172 for(unsigned int i = 0; i < rows; ++i)
174 // insert default initialized element
175 mArray[i].insert(mArray[i].begin() + columnIndex, T());
180 * Delete a column from given index.
181 * Removed elements are deleted
182 * @param [in] columnIndex of the column to delete
184 void DeleteColumn(unsigned int columnIndex)
186 // go through all rows
187 const unsigned int rows = GetRows();
188 for(unsigned int i = 0; i < rows; ++i)
191 mArray[i].erase(mArray[i].begin() + columnIndex);
196 * Delete a column from given index
197 * @param [in] columnIndex of the column to delete
198 * @param [out] removed elements
200 void DeleteColumn(unsigned int columnIndex, std::vector<T>& removed)
202 // go through all rows
203 const unsigned int rows = GetRows();
204 for(unsigned int i = 0; i < rows; ++i)
206 // copy the column element of this row
207 removed.push_back(mArray[i][columnIndex]);
209 mArray[i].erase(mArray[i].begin() + columnIndex);
214 * Resizes the array to given dimensions
215 * If new size is smaller in either dimension, items that wont fit will be deleted
216 * @param [in] rows for array
217 * @param [in] columns for array
219 void Resize(unsigned int rows, unsigned int columns)
221 // resize rows first, may increase or decrease size
223 for(unsigned int i = 0; i < rows; ++i)
225 // resize each column, may increase or decrease size
226 mArray[i].resize(columns);
231 * Resizes the array to given dimensions
232 * If new size is smaller, items that wont fit will be returned to caller
233 * @param [in] rows for array
234 * @param [in] columns for array
235 * @param [out] removed elements in case array is smaller in either dimension
237 void Resize(unsigned int rows, unsigned int columns, std::vector<T>& removed)
239 // remember old counts
240 const unsigned int oldRows = GetRows();
241 const unsigned int oldColumns = GetColumns();
242 // are rows being removed ?
245 // gather the elements of removed rows
246 for(unsigned int i = rows; i < oldRows; ++i)
248 // copy the row elements, the whole row is gone
249 removed.insert(removed.end(), mArray[i].begin(), mArray[i].end());
252 // resize the rows, may increase or decrease size
254 // process columns, need to do all rows as also columns for new row need resizing
255 for(unsigned int i = 0; i < rows; ++i)
257 // if this is an old row and columns are being removed
258 if((i < oldRows) && (columns < oldColumns))
260 // copy the columns, the end of row from columns is gone
261 removed.insert(removed.end(), mArray[i].begin() + columns, mArray[i].end());
263 // resize the column, may increase of decrease size
264 mArray[i].resize(columns);
269 std::vector<std::vector<T> > mArray;
274 #endif // DALI_ARRAY2D_H