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