b311480e |
1 | // Created on: 2002-04-15 |
2 | // Created by: Alexander Kartomin (akm) |
973c2be1 |
3 | // Copyright (c) 2002-2014 OPEN CASCADE SAS |
b311480e |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
b311480e |
6 | // |
973c2be1 |
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. |
b311480e |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
7fd59977 |
15 | |
16 | #ifndef NCollection_Array1_HeaderFile |
17 | #define NCollection_Array1_HeaderFile |
18 | |
19 | #ifndef No_Exception |
20 | #include <Standard_DimensionMismatch.hxx> |
21 | #include <Standard_OutOfMemory.hxx> |
22 | #include <Standard_OutOfRange.hxx> |
23 | #endif |
24 | |
25 | #include <NCollection_BaseCollection.hxx> |
26 | |
7fd59977 |
27 | // *********************************************** Template for Array1 class |
28 | |
29 | /** |
30 | * Purpose: The class Array1 represents unidimensional arrays |
31 | * of fixed size known at run time. |
32 | * The range of the index is user defined. |
33 | * An array1 can be constructed with a "C array". |
34 | * This functionality is useful to call methods expecting |
35 | * an Array1. It allows to carry the bounds inside the arrays. |
36 | * |
37 | * Examples: Item tab[100]; // An example with a C array |
38 | * Array1OfItem ttab (tab[0],1,100); |
39 | * |
40 | * Array1OfItem tttab (ttab(10),10,20); // a slice of ttab |
41 | * |
42 | * If you want to reindex an array from 1 to Length do : |
43 | * |
44 | * Array1 tab1(tab(tab.Lower()),1,tab.Length()); |
45 | * |
46 | * Warning: Programs client of such a class must be independant |
47 | * of the range of the first element. Then, a C++ for |
48 | * loop must be written like this |
49 | * |
50 | * for (i = A.Lower(); i <= A.Upper(); i++) |
51 | * |
52 | * Changes: In comparison to TCollection the flag isAllocated was |
53 | * renamed into myDeletable (alike in the Array2). For naming |
54 | * compatibility the method IsAllocated remained in class along |
55 | * with IsDeletable. |
56 | */ |
57 | template <class TheItemType> class NCollection_Array1 |
58 | : public NCollection_BaseCollection<TheItemType> |
59 | { |
60 | |
61 | public: |
62 | //! Implementation of the Iterator interface. |
63 | class Iterator : public NCollection_BaseCollection<TheItemType>::Iterator |
64 | { |
65 | public: |
66 | //! Empty constructor - for later Init |
67 | Iterator (void) : |
68 | myCurrent (0), |
69 | myArray (NULL) {} |
70 | //! Constructor with initialisation |
71 | Iterator (const NCollection_Array1& theArray) : |
72 | myCurrent (theArray.Lower()), |
73 | myArray ((NCollection_Array1 *) &theArray) {} |
74 | //! Initialisation |
75 | void Init (const NCollection_Array1& theArray) |
76 | { |
77 | myCurrent = theArray.Lower(); |
78 | myArray = (NCollection_Array1 *) &theArray; |
79 | } |
80 | //! Check end |
81 | virtual Standard_Boolean More (void) const |
82 | { return (myCurrent<=myArray->Upper()); } |
83 | //! Make step |
84 | virtual void Next (void) |
85 | { myCurrent++; } |
86 | //! Constant value access |
87 | virtual const TheItemType& Value (void) const |
88 | { return myArray->Value(myCurrent); } |
89 | //! Variable value access |
90 | virtual TheItemType& ChangeValue (void) const |
91 | { return myArray->ChangeValue(myCurrent); } |
7fd59977 |
92 | private: |
93 | Standard_Integer myCurrent; //!< Index of the current item |
94 | NCollection_Array1* myArray; //!< Pointer to the array being iterated |
95 | }; // End of the nested class Iterator |
96 | |
97 | public: |
98 | // ---------- PUBLIC METHODS ------------ |
99 | |
100 | //! Constructor |
101 | NCollection_Array1(const Standard_Integer theLower, |
102 | const Standard_Integer theUpper) : |
103 | NCollection_BaseCollection<TheItemType> (), |
104 | myLowerBound (theLower), |
105 | myUpperBound (theUpper), |
106 | myDeletable (Standard_True) |
107 | { |
108 | #if !defined No_Exception && !defined No_Standard_RangeError |
109 | if (theUpper < theLower) |
110 | Standard_RangeError::Raise ("NCollection_Array1::Create"); |
111 | #endif |
112 | TheItemType* pBegin = new TheItemType[Length()]; |
113 | #if !defined No_Exception && !defined No_Standard_OutOfMemory |
114 | if (!pBegin) |
115 | Standard_OutOfMemory::Raise ("NCollection_Array1 : Allocation failed"); |
116 | #endif |
117 | |
118 | myData = pBegin - theLower; |
119 | } |
120 | |
121 | //! Copy constructor |
122 | NCollection_Array1 (const NCollection_Array1& theOther) : |
123 | NCollection_BaseCollection<TheItemType> (), |
124 | myLowerBound (theOther.Lower()), |
125 | myUpperBound (theOther.Upper()), |
126 | myDeletable (Standard_True) |
127 | { |
128 | TheItemType* pBegin = new TheItemType[Length()]; |
129 | #if !defined No_Exception && !defined No_Standard_OutOfMemory |
130 | if (!pBegin) |
131 | Standard_OutOfMemory::Raise ("NCollection_Array1 : Allocation failed"); |
132 | #endif |
133 | myData = pBegin - myLowerBound; |
134 | |
135 | *this = theOther; |
136 | } |
137 | |
138 | //! C array-based constructor |
139 | NCollection_Array1 (const TheItemType& theBegin, |
140 | const Standard_Integer theLower, |
141 | const Standard_Integer theUpper) : |
142 | NCollection_BaseCollection<TheItemType> (), |
143 | myLowerBound (theLower), |
144 | myUpperBound (theUpper), |
145 | myDeletable (Standard_False) |
146 | { |
147 | #if !defined No_Exception && !defined No_Standard_RangeError |
148 | if (theUpper < theLower) |
149 | Standard_RangeError::Raise ("NCollection_Array1::Array1"); |
150 | #endif |
151 | myData = (TheItemType *) &theBegin - theLower; |
152 | } |
153 | |
154 | //! Initialise the items with theValue |
155 | void Init (const TheItemType& theValue) |
156 | { |
157 | TheItemType *pCur = &myData[myLowerBound], *pEnd=&myData[myUpperBound]; |
158 | for(; pCur <= pEnd; pCur++) |
159 | *pCur = (TheItemType&) theValue; |
160 | } |
161 | |
162 | //! Size query |
163 | virtual Standard_Integer Size (void) const |
164 | { return Length(); } |
165 | //! Length query (the same) |
166 | Standard_Integer Length (void) const |
167 | { return (myUpperBound-myLowerBound+1); } |
168 | |
169 | //! Lower bound |
170 | Standard_Integer Lower (void) const |
171 | { return myLowerBound; } |
172 | //! Upper bound |
173 | Standard_Integer Upper (void) const |
174 | { return myUpperBound; } |
175 | |
176 | //! myDeletable flag |
177 | Standard_Boolean IsDeletable (void) const |
178 | { return myDeletable; } |
179 | |
180 | //! IsAllocated flag - for naming compatibility |
181 | Standard_Boolean IsAllocated (void) const |
182 | { return myDeletable; } |
183 | |
184 | //! Assign (any collection to this array) |
185 | // Copies items from the other collection into the allocated |
186 | // storage. Raises an exception when sizes differ. |
187 | virtual void Assign (const NCollection_BaseCollection<TheItemType>& theOther) |
188 | { |
189 | if (&theOther == this) |
190 | return; |
191 | #if !defined No_Exception && !defined No_Standard_DimensionMismatch |
192 | if (Length() != theOther.Size()) |
193 | Standard_DimensionMismatch::Raise ("NCollection_Array1::Assign"); |
194 | #endif |
195 | TYPENAME NCollection_BaseCollection<TheItemType>::Iterator& anIter2 = |
196 | theOther.CreateIterator(); |
197 | TheItemType * const pEndItem = &myData[myUpperBound]; |
198 | for (TheItemType * pItem = &myData[myLowerBound]; |
199 | pItem <= pEndItem; anIter2.Next()) |
200 | * pItem ++ = anIter2.Value(); |
201 | } |
202 | |
203 | //! operator= (array to array) |
204 | NCollection_Array1& operator= (const NCollection_Array1& theOther) |
205 | { |
206 | if (&theOther == this) |
207 | return *this; |
208 | #if !defined No_Exception && !defined No_Standard_DimensionMismatch |
209 | if (Length() != theOther.Length()) |
210 | Standard_DimensionMismatch::Raise ("NCollection_Array1::operator="); |
211 | #endif |
212 | TheItemType * pMyItem = &myData[myLowerBound]; |
213 | TheItemType * const pEndItem = &(theOther.myData)[theOther.myUpperBound]; |
214 | TheItemType * pItem = &(theOther.myData)[theOther.myLowerBound]; |
215 | while (pItem <= pEndItem) * pMyItem ++ = * pItem ++; |
216 | return *this; |
217 | } |
218 | |
a174a3c5 |
219 | //! @return first element |
220 | const TheItemType& First() const |
221 | { |
222 | return myData[myLowerBound]; |
223 | } |
224 | |
225 | //! @return first element |
226 | TheItemType& ChangeFirst() |
227 | { |
228 | return myData[myLowerBound]; |
229 | } |
230 | |
231 | //! @return last element |
232 | const TheItemType& Last() const |
233 | { |
234 | return myData[myUpperBound]; |
235 | } |
236 | |
237 | //! @return last element |
238 | TheItemType& ChangeLast() |
239 | { |
240 | return myData[myUpperBound]; |
241 | } |
242 | |
7fd59977 |
243 | //! Constant value access |
244 | const TheItemType& Value (const Standard_Integer theIndex) const |
245 | { |
246 | #if !defined No_Exception && !defined No_Standard_OutOfRange |
247 | if (theIndex < myLowerBound || theIndex > myUpperBound) |
248 | Standard_OutOfRange::Raise ("NCollection_Array1::Value"); |
249 | #endif |
250 | return myData[theIndex]; |
251 | } |
252 | |
253 | //! operator() - alias to Value |
254 | const TheItemType& operator() (const Standard_Integer theIndex) const |
255 | { return Value (theIndex); } |
256 | |
257 | //! Variable value access |
258 | TheItemType& ChangeValue (const Standard_Integer theIndex) |
259 | { |
260 | #if !defined No_Exception && !defined No_Standard_OutOfRange |
261 | if (theIndex < myLowerBound || theIndex > myUpperBound) |
262 | Standard_OutOfRange::Raise ("NCollection_Array1::ChangeValue"); |
263 | #endif |
264 | return myData[theIndex]; |
265 | } |
266 | |
267 | //! operator() - alias to ChangeValue |
268 | TheItemType& operator() (const Standard_Integer theIndex) |
269 | { return ChangeValue (theIndex); } |
270 | |
271 | //! Set value |
272 | void SetValue (const Standard_Integer theIndex, |
273 | const TheItemType& theItem) |
274 | { |
275 | #if !defined No_Exception && !defined No_Standard_OutOfRange |
276 | if (theIndex < myLowerBound || theIndex > myUpperBound) |
277 | Standard_OutOfRange::Raise ("NCollection_Array1::SetValue"); |
278 | #endif |
279 | myData[theIndex] = theItem; |
280 | } |
281 | |
282 | //! Destructor - releases the memory |
283 | ~NCollection_Array1 (void) |
284 | { if (myDeletable) delete [] &(myData[myLowerBound]); } |
285 | |
286 | private: |
287 | // ----------- PRIVATE METHODS ----------- |
288 | |
289 | // ******** Creates Iterator for use on BaseCollection |
290 | virtual |
291 | TYPENAME NCollection_BaseCollection<TheItemType>::Iterator& |
292 | CreateIterator(void) const |
293 | { return *(new (this->IterAllocator()) Iterator(*this)); } |
294 | |
295 | protected: |
296 | // ---------- PROTECTED FIELDS ----------- |
297 | Standard_Integer myLowerBound; |
298 | Standard_Integer myUpperBound; |
299 | Standard_Boolean myDeletable; //!< Flag showing who allocated the array |
300 | TheItemType* myData; //!< Pointer to '0'th array item |
301 | }; |
302 | |
7fd59977 |
303 | #endif |