0024428: Implementation of LGPL license
[occt.git] / src / IntPolyh / IntPolyh_Array.hxx
1 // Created on: 2014-11-13
2 // Created by: Peter KURNEV
3 // Copyright (c) 2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and / or modify it
8 // under the terms of the GNU Lesser General Public version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef IntPolyh_Array_HeaderFile
17 #define IntPolyh_Array_HeaderFile
18
19 #include <NCollection_Vector.hxx>
20 #include <stdio.h>
21
22 /**
23 * Class IntPolyh_Array (dynamic array of objects)
24 *
25 *   1. The Array is dynamic array of objects.
26 *
27 *   2. The Array uses NCollection_Vector to store objects
28 *
29 *   3. The Array can be created:
30 *      3.1.  with initial length Nb=0. 
31 *            In this case Array should be initiated by invoke 
32 *            the method Init(Nb).
33 *      3.2.  with initial length Nb>0.
34 *            In this case Array is initiated automatically.
35
36 *      The memory is allocated to store myNbAllocated oblects.
37 *
38 *   4. The number of items that are stored in the Array (myNbItems)
39 *      can be increased by calling the method:  IncrementNbItems().
40 *      The objects are stored in already allocated memory if it is 
41 *      possible.
42 *      Otherwise the new chunk of memory is allocated to store the 
43 *      objects.
44 *      The size of chunk <aIncrement> can be defined during the creation
45 *      of the Array.
46 *      
47 *   5. The start index of the Array is 0, The end index of the Array 
48 *      can be obtained by the method  NbItems();
49
50 *   6. The contents of the element with index "i" can be queried or 
51 *      modified by the methods:  Value(i), ChangeValue(i), operator[](i)
52 */
53
54 //=======================================================================
55 // class : IntPolyh_Array
56 //
57 //=======================================================================
58 template <class Type> class IntPolyh_Array {
59  public:
60   typedef NCollection_Vector <Type> IntPolyh_VectorOfType;
61   
62   /**
63    * Constructor.
64    * @param aIncrement
65    *   size of memory (in terms of Items) to expand the array
66    */
67   IntPolyh_Array(const Standard_Integer aIncrement=256) {
68     myNbAllocated=0;
69     myNbItems=0;
70     myIncrement=aIncrement;
71   }
72   
73   /**
74    * Constructor.
75    * @param aN
76    *   size of memory (in terms of Items) to allocate
77    * @param aIncrement
78    *   size of memory (in terms of Items) to expand the array
79    */
80   IntPolyh_Array(const Standard_Integer aN,
81                  const Standard_Integer aIncrement=256) {
82     myNbItems=0;
83     myIncrement=aIncrement;
84     Init(aN);
85   }
86   
87   /**
88    * Assignment operator
89    * @param
90    *   aOther - the array to copy from 
91    * @return
92    *   the array
93    */
94   IntPolyh_Array& operator =(const IntPolyh_Array& aOther) {
95     return Copy(aOther);
96   }
97
98   /**
99    * Copy 
100    * @param
101    *   aOther - the array to copy from
102    * @return
103    *   the array 
104    */
105   IntPolyh_Array& Copy(const IntPolyh_Array& aOther) {
106     myVectorOfType.Clear();
107     Init(aOther.myNbAllocated);
108     myVectorOfType=aOther.myVectorOfType;
109     myNbItems=aOther.myNbItems; 
110     //
111     return *this;
112   }
113
114   /**
115    * Init - allocate memory for <aN> items  
116    * @param
117    *   aN - the number of items to allocate the memory
118    */
119   void Init(const Standard_Integer aN) {
120     Type aSL;
121     //
122     myVectorOfType.SetValue(aN, aSL);
123     myNbAllocated=aN;
124   }
125
126   /**
127    * IncrementNbItems - increment the number of stored items 
128    */
129   void IncrementNbItems() {
130     myNbItems++; 
131     if (myNbItems>=myNbAllocated) {
132       Standard_Integer aN;
133       //
134       aN=myNbAllocated+myIncrement;
135       Init(aN);
136     }
137   } 
138
139   /**
140    * GetN - returns the number of 'allocated' items  
141    * @return
142    *   the number of 'allocated' items 
143    */
144   Standard_Integer GetN() const { 
145     return myNbAllocated; 
146   }
147
148   /**
149    * NbItems - returns the number of stored items  
150    * @return
151    *   the number of stored items 
152    */
153   Standard_Integer NbItems() const { 
154     return myNbItems; 
155   }
156   
157
158   /**
159    * set the number of stored items  
160    * @param aNb
161    *   the number of stored items 
162    */
163   void SetNbItems(const Standard_Integer aNb){ 
164     myNbItems=aNb; 
165   } 
166
167   /**
168    * query the const value
169    * @param aIndex
170    *   index 
171    * @return
172    *   the const item
173    */
174   const Type& Value(const Standard_Integer aIndex) const {
175     return myVectorOfType.Value(aIndex);
176   }
177
178   /**
179    * query the const value
180    * @param aIndex
181    *   index 
182    * @return
183    *   the const item
184    */
185   const Type& operator [](const Standard_Integer aIndex) const {
186     return Value(aIndex);
187   }
188
189    /**
190    * query the value
191    * @param aIndex
192    *   index 
193    * @return
194    *   the item
195    */
196   Type& ChangeValue(const Standard_Integer aIndex)  {
197     return myVectorOfType.ChangeValue(aIndex);
198   }
199   
200   /**
201    * query the value
202    * @param aIndex
203    *   index 
204    * @return
205    *   the item
206    */
207   Type& operator [](const Standard_Integer aIndex)  {
208     return ChangeValue(aIndex);
209   }
210
211   /**
212    * dump the contents
213    */
214   void Dump() const   { 
215     printf("\n ArrayOfSectionLines 0-> %d",myNbItems-1);
216     for(Standard_Integer i=0;i<myNbItems;i++) { 
217       (*this)[i].Dump();
218     }
219     printf("\n");
220   }
221
222  protected:
223   Standard_Integer myNbAllocated;
224   Standard_Integer myNbItems;
225   Standard_Integer myIncrement;
226   IntPolyh_VectorOfType myVectorOfType;
227 };
228
229 #endif