Updated all header files to new format
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / table-view / array-2d.h
1 #pragma once
2 #ifndef DALI_ARRAY2D_H
3 #define DALI_ARRAY2D_H
4
5 /*
6  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 // EXTERNAL INCLUDES
23 #include <dali/public-api/common/dali-vector.h>
24
25 namespace Dali
26 {
27 /**
28  * Helper wrapper for two dimensional array using std::vector
29  * Usage:
30  * <code>
31  *   Array2d< int > intArray( 3, 3 );
32  *   intArray[ 0 ][ 0 ] = 10;
33  *   intArray.Resize( 4, 4 );
34  * </code>
35  */
36 template<typename T>
37 class Array2d
38 {
39 public:
40   /**
41    * Default constructor. Creates a 0x0 array
42    */
43   Array2d()
44   : mArray(0, std::vector<T>(0))
45   {
46   }
47
48   /**
49    * Constructs an array with given dimensions
50    * @param [in] rows for array
51    * @param [in] columns for array
52    */
53   Array2d(unsigned int rows, unsigned int columns)
54   : mArray(rows, std::vector<T>(columns))
55   {
56   }
57
58   /**
59    * Destructor
60    */
61   ~Array2d()
62   {
63   } // Nothing to do, vector cleans up itself
64
65   /**
66    * Copy constructor
67    * @param array to copy from
68    */
69   Array2d(const Array2d& array)
70   {
71     if(this != &array)
72     {
73       mArray = array.mArray;
74     }
75   }
76
77   /**
78    * Assignment operator
79    * @param array to copy from
80    * @return reference to self for chaining
81    */
82   Array2d& operator=(const Array2d& array)
83   {
84     if(this != &array)
85     {
86       mArray = array.mArray;
87     }
88     return *this;
89   }
90
91   /**
92    * @return the number of rows in the array
93    */
94   unsigned int GetRows()
95   {
96     return mArray.size();
97   }
98
99   /**
100    * @return the number of columns in the array
101    */
102   unsigned int GetColumns()
103   {
104     if(mArray.size() > 0)
105     {
106       // all columns are equal length
107       return mArray[0].size();
108     }
109     return 0;
110   }
111
112   /**
113    * @param [in] index of the row
114    * @return reference to the row vector for given index
115    */
116   std::vector<T>& operator[](unsigned int index)
117   {
118     return mArray[index];
119   }
120
121   /**
122    * @param [in] index of the row
123    * @return const reference to the row vector for given index
124    */
125   const std::vector<T>& operator[](unsigned int index) const
126   {
127     return mArray[index];
128   }
129
130   /**
131    * Insert a new row to given index
132    * @param [in] rowIndex of the new row
133    */
134   void InsertRow(unsigned int rowIndex)
135   {
136     // insert default initialized row of elements
137     mArray.insert(mArray.begin() + rowIndex, std::vector<T>(GetColumns()));
138   }
139
140   /**
141    * Delete a row from given index
142    * Removed elements are deleted
143    * @param [in] rowIndex of the row to delete
144    */
145   void DeleteRow(unsigned int rowIndex)
146   {
147     // erase the row
148     mArray.erase(mArray.begin() + rowIndex);
149   }
150
151   /**
152    * Delete a row from given index
153    * @param [in] rowIndex of the row to delete
154    * @param [out] removed elements
155    */
156   void DeleteRow(unsigned int rowIndex, std::vector<T>& removed)
157   {
158     // copy the row elements
159     removed.insert(removed.end(), mArray[rowIndex].begin(), mArray[rowIndex].end());
160     // erase the row
161     mArray.erase(mArray.begin() + rowIndex);
162   }
163
164   /**
165    * Insert a new column to given index
166    * @param [in] columnIndex of the new column
167    */
168   void InsertColumn(unsigned int columnIndex)
169   {
170     // go through all rows
171     const unsigned int rows = GetRows();
172     for(unsigned int i = 0; i < rows; ++i)
173     {
174       // insert default initialized element
175       mArray[i].insert(mArray[i].begin() + columnIndex, T());
176     }
177   }
178
179   /**
180    * Delete a column from given index.
181    * Removed elements are deleted
182    * @param [in] columnIndex of the column to delete
183    */
184   void DeleteColumn(unsigned int columnIndex)
185   {
186     // go through all rows
187     const unsigned int rows = GetRows();
188     for(unsigned int i = 0; i < rows; ++i)
189     {
190       // erase the column
191       mArray[i].erase(mArray[i].begin() + columnIndex);
192     }
193   }
194
195   /**
196    * Delete a column from given index
197    * @param [in] columnIndex of the column to delete
198    * @param [out] removed elements
199    */
200   void DeleteColumn(unsigned int columnIndex, std::vector<T>& removed)
201   {
202     // go through all rows
203     const unsigned int rows = GetRows();
204     for(unsigned int i = 0; i < rows; ++i)
205     {
206       // copy the column element of this row
207       removed.push_back(mArray[i][columnIndex]);
208       // erase the column
209       mArray[i].erase(mArray[i].begin() + columnIndex);
210     }
211   }
212
213   /**
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
218    */
219   void Resize(unsigned int rows, unsigned int columns)
220   {
221     // resize rows first, may increase or decrease size
222     mArray.resize(rows);
223     for(unsigned int i = 0; i < rows; ++i)
224     {
225       // resize each column, may increase or decrease size
226       mArray[i].resize(columns);
227     }
228   }
229
230   /**
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
236    */
237   void Resize(unsigned int rows, unsigned int columns, std::vector<T>& removed)
238   {
239     // remember old counts
240     const unsigned int oldRows    = GetRows();
241     const unsigned int oldColumns = GetColumns();
242     // are rows being removed ?
243     if(rows < oldRows)
244     {
245       // gather the elements of removed rows
246       for(unsigned int i = rows; i < oldRows; ++i)
247       {
248         // copy the row elements, the whole row is gone
249         removed.insert(removed.end(), mArray[i].begin(), mArray[i].end());
250       }
251     }
252     // resize the rows, may increase or decrease size
253     mArray.resize(rows);
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)
256     {
257       // if this is an old row and columns are being removed
258       if((i < oldRows) && (columns < oldColumns))
259       {
260         // copy the columns, the end of row from columns is gone
261         removed.insert(removed.end(), mArray[i].begin() + columns, mArray[i].end());
262       }
263       // resize the column, may increase of decrease size
264       mArray[i].resize(columns);
265     }
266   }
267
268 private:
269   std::vector<std::vector<T> > mArray;
270 };
271
272 } // namespace Dali
273
274 #endif // DALI_ARRAY2D_H