Split dali-toolkit into Base & Optional
[platform/core/uifw/dali-toolkit.git] / base / 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) 2014 Samsung Electronics Co., Ltd.
7 //
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
11 //
12 //     http://floralicense.org/license/
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 // INTERNAL INCLUDES
22 #include <dali/public-api/common/dali-vector.h>
23
24 namespace Dali
25 {
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   /**
42    * Default constructor. Creates a 0x0 array
43    */
44   Array2d()
45   : mArray( 0, std::vector< T >( 0 ) )
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    * Destructor
59    */
60   ~Array2d()
61   { } // Nothing to do, vector cleans up itself
62
63   /**
64    * Copy constructor
65    * @param array to copy from
66    */
67   Array2d( const Array2d& array )
68   {
69     if( this != &array )
70     {
71       mArray = array.mArray;
72     }
73   }
74
75   /**
76    * Assignment operator
77    * @param array to copy from
78    * @return reference to self for chaining
79    */
80   Array2d& operator=( const Array2d& array )
81   {
82     if( this != &array )
83     {
84       mArray = array.mArray;
85     }
86   return *this;
87   }
88
89   /**
90    * @return the number of rows in the array
91    */
92   unsigned int GetRows()
93   {
94     return mArray.size();
95   }
96
97   /**
98    * @return the number of columns in the array
99    */
100   unsigned int GetColumns()
101   {
102     if( mArray.size() > 0 )
103     {
104       // all columns are equal length
105       return mArray[ 0 ].size();
106     }
107     return 0;
108   }
109
110   /**
111    * @param [in] index of the row
112    * @return reference to the row vector for given index
113    */
114   std::vector< T >& operator[]( unsigned int index )
115   {
116     return mArray[ index ];
117   }
118
119   /**
120    * @param [in] index of the row
121    * @return const reference to the row vector for given index
122    */
123   const std::vector< T >& operator[]( unsigned int index ) const
124   {
125     return mArray[ index ];
126   }
127
128   /**
129    * Insert a new row to given index
130    * @param [in] rowIndex of the new row
131    */
132   void InsertRow( unsigned int rowIndex )
133   {
134     // insert default initialized row of elements
135     mArray.insert( mArray.begin() + rowIndex, std::vector< T >( GetColumns() ) );
136   }
137
138   /**
139    * Delete a row from given index
140    * Removed elements are deleted
141    * @param [in] rowIndex of the row to delete
142    */
143   void DeleteRow( unsigned int rowIndex )
144   {
145     // erase the row
146     mArray.erase( mArray.begin() + rowIndex );
147   }
148
149   /**
150    * Delete a row from given index
151    * @param [in] rowIndex of the row to delete
152    * @param [out] removed elements
153    */
154   void DeleteRow( unsigned int rowIndex, std::vector< T >& removed )
155   {
156     // copy the row elements
157     removed.insert( removed.end(), mArray[ rowIndex ].begin(), mArray[ rowIndex ].end() );
158     // erase the row
159     mArray.erase( mArray.begin() + rowIndex );
160   }
161
162   /**
163    * Insert a new column to given index
164    * @param [in] columnIndex of the new column
165    */
166   void InsertColumn( unsigned int columnIndex )
167   {
168     // go through all rows
169     const unsigned int rows = GetRows();
170     for( unsigned int i = 0; i < rows; ++i )
171     {
172       // insert default initialized element
173       mArray[ i ].insert( mArray[ i ].begin() + columnIndex, T() );
174     }
175   }
176
177   /**
178    * Delete a column from given index.
179    * Removed elements are deleted
180    * @param [in] columnIndex of the column to delete
181    */
182   void DeleteColumn( unsigned int columnIndex )
183   {
184     // go through all rows
185     const unsigned int rows = GetRows();
186     for( unsigned int i = 0; i < rows; ++i )
187     {
188       // erase the column
189       mArray[ i ].erase( mArray[ i ].begin() + columnIndex );
190     }
191   }
192
193   /**
194    * Delete a column from given index
195    * @param [in] columnIndex of the column to delete
196    * @param [out] removed elements
197    */
198   void DeleteColumn( unsigned int columnIndex, std::vector< T >& removed )
199   {
200     // go through all rows
201     const unsigned int rows = GetRows();
202     for( unsigned int i = 0; i < rows; ++i )
203     {
204       // copy the column element of this row
205       removed.push_back( mArray[ i ][ columnIndex ] );
206       // erase the column
207       mArray[ i ].erase( mArray[ i ].begin() + columnIndex );
208     }
209   }
210
211   /**
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
216    */
217   void Resize( unsigned int rows, unsigned int columns )
218   {
219     // resize rows first, may increase or decrease size
220     mArray.resize( rows );
221     for( unsigned int i = 0; i < rows; ++i )
222     {
223       // resize each column, may increase or decrease size
224       mArray[ i ].resize( columns );
225     }
226   }
227
228   /**
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
234    */
235   void Resize( unsigned int rows, unsigned int columns, std::vector< T >& removed )
236   {
237     // remember old counts
238     const unsigned int oldRows = GetRows();
239     const unsigned int oldColumns = GetColumns();
240     // are rows being removed ?
241     if( rows < oldRows )
242     {
243       // gather the elements of removed rows
244       for( unsigned int i = rows; i < oldRows; ++i )
245       {
246         // copy the row elements, the whole row is gone
247         removed.insert( removed.end(), mArray[ i ].begin(), mArray[ i ].end() );
248       }
249     }
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 )
254     {
255       // if this is an old row and columns are being removed
256       if( ( i < oldRows )&&( columns < oldColumns ) )
257       {
258         // copy the columns, the end of row from columns is gone
259         removed.insert( removed.end(), mArray[ i ].begin() + columns, mArray[ i ].end() );
260       }
261       // resize the column, may increase of decrease size
262       mArray[ i ].resize( columns );
263     }
264   }
265
266 private:
267
268   std::vector< std::vector< T > > mArray;
269
270 };
271
272 } // namespace Dali
273
274 #endif // __DALI_ARRAY2D_H__