1 // Created on: 2002-04-15
2 // Created by: Alexander Kartomin (akm)
3 // Copyright (c) 2002-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License 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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef NCollection_Array1_HeaderFile
17 #define NCollection_Array1_HeaderFile
19 #include <Standard_DimensionMismatch.hxx>
20 #include <Standard_OutOfMemory.hxx>
21 #include <Standard_OutOfRange.hxx>
23 #include <NCollection_DefineAlloc.hxx>
24 #include <NCollection_StlIterator.hxx>
26 // *********************************************** Template for Array1 class
29 * Purpose: The class Array1 represents unidimensional arrays
30 * of fixed size known at run time.
31 * The range of the index is user defined.
32 * An array1 can be constructed with a "C array".
33 * This functionality is useful to call methods expecting
34 * an Array1. It allows to carry the bounds inside the arrays.
36 * Examples: Item tab[100]; // An example with a C array
37 * Array1OfItem ttab (tab[0],1,100);
39 * Array1OfItem tttab (ttab(10),10,20); // a slice of ttab
41 * If you want to reindex an array from 1 to Length do :
43 * Array1 tab1(tab(tab.Lower()),1,tab.Length());
45 * Warning: Programs client of such a class must be independant
46 * of the range of the first element. Then, a C++ for
47 * loop must be written like this
49 * for (i = A.Lower(); i <= A.Upper(); i++)
51 * Changes: In comparison to TCollection the flag isAllocated was
52 * renamed into myDeletable (alike in the Array2). For naming
53 * compatibility the method IsAllocated remained in class along
56 template <class TheItemType>
57 class NCollection_Array1
60 //! STL-compliant typedef for value type
61 typedef TheItemType value_type;
64 //! Implementation of the Iterator interface.
69 //! Empty constructor - for later Init
77 //! Constructor with initialization
78 Iterator (const NCollection_Array1& theArray, Standard_Boolean theToEnd = Standard_False) :
79 myPtrEnd (const_cast<TheItemType*> (&theArray.Last() + 1))
81 myPtrCur = theToEnd ? myPtrEnd : const_cast<TheItemType*> (&theArray.First());
85 void Init (const NCollection_Array1& theArray)
87 myPtrCur = const_cast<TheItemType*> (&theArray.First());
88 myPtrEnd = const_cast<TheItemType*> (&theArray.Last() + 1);
92 Iterator& operator= (const Iterator& theOther)
94 myPtrCur = theOther.myPtrCur;
95 myPtrEnd = theOther.myPtrEnd;
100 Standard_Boolean More (void) const
101 { return myPtrCur < myPtrEnd; }
103 //! Increment operator
107 //! Decrement operator
112 void Offset (ptrdiff_t theOffset)
113 { myPtrCur += theOffset; }
115 //! Difference operator.
116 ptrdiff_t Differ (const Iterator& theOther) const
117 { return myPtrCur - theOther.myPtrCur; }
119 //! Constant value access
120 const TheItemType& Value (void) const
121 { return *myPtrCur; }
123 //! Variable value access
124 TheItemType& ChangeValue (void) const
125 { return *myPtrCur; }
127 //! Performs comparison of two iterators
128 Standard_Boolean IsEqual (const Iterator& theOther) const
129 { return myPtrCur == theOther.myPtrCur; }
132 TheItemType* myPtrCur; //!< Pointer to the current element in the array
133 TheItemType* myPtrEnd; //!< Pointer to the past-the-end element in the array
134 }; // End of the nested class Iterator
136 //! Shorthand for a regular iterator type.
137 typedef NCollection_StlIterator<std::random_access_iterator_tag, Iterator, TheItemType, false> iterator;
139 //! Shorthand for a constant iterator type.
140 typedef NCollection_StlIterator<std::random_access_iterator_tag, Iterator, TheItemType, true> const_iterator;
142 //! Returns an iterator pointing to the first element in the array.
143 iterator begin() const { return Iterator (*this, false); }
145 //! Returns an iterator referring to the past-the-end element in the array.
146 iterator end() const { return Iterator (*this, true); }
148 //! Returns a const iterator pointing to the first element in the array.
149 const_iterator cbegin() const { return Iterator (*this, false); }
151 //! Returns a const iterator referring to the past-the-end element in the array.
152 const_iterator cend() const { return Iterator (*this, true); }
155 // ---------- PUBLIC METHODS ------------
157 //! Empty constructor; should be used with caution.
158 //! @sa methods Resize() and Move().
162 myDeletable (Standard_False),
169 NCollection_Array1(const Standard_Integer theLower,
170 const Standard_Integer theUpper) :
171 myLowerBound (theLower),
172 myUpperBound (theUpper),
173 myDeletable (Standard_True)
175 Standard_RangeError_Raise_if (theUpper < theLower, "NCollection_Array1::Create");
176 TheItemType* pBegin = new TheItemType[Length()];
177 Standard_OutOfMemory_Raise_if (!pBegin, "NCollection_Array1 : Allocation failed");
179 myData = pBegin - theLower;
183 NCollection_Array1 (const NCollection_Array1& theOther) :
184 myLowerBound (theOther.Lower()),
185 myUpperBound (theOther.Upper()),
186 myDeletable (Standard_True)
188 TheItemType* pBegin = new TheItemType[Length()];
189 Standard_OutOfMemory_Raise_if (!pBegin, "NCollection_Array1 : Allocation failed");
190 myData = pBegin - myLowerBound;
195 #ifndef OCCT_NO_RVALUE_REFERENCE
197 NCollection_Array1 (NCollection_Array1&& theOther)
198 : myLowerBound (theOther.myLowerBound),
199 myUpperBound (theOther.myUpperBound),
200 myDeletable (theOther.myDeletable),
201 myData (theOther.myData)
203 theOther.myDeletable = false;
207 //! C array-based constructor.
209 //! Makes this array to use the buffer pointed by theBegin
210 //! instead of allocating it dynamically.
211 //! Argument theBegin should be a reference to the first element
212 //! of the pre-allocated buffer (usually local C array buffer),
213 //! with size at least theUpper - theLower + 1 items.
215 //! Warning: returning array object created using this constructor
216 //! from function by value will result in undefined behavior
217 //! if compiler performs return value optimization (this is likely
218 //! to be true for all modern compilers in release mode).
219 //! The same happens if array is copied using Move() function
220 //! or move constructor and target object's lifespan is longer
221 //! than that of the buffer.
222 NCollection_Array1 (const TheItemType& theBegin,
223 const Standard_Integer theLower,
224 const Standard_Integer theUpper) :
225 myLowerBound (theLower),
226 myUpperBound (theUpper),
227 myDeletable (Standard_False)
229 Standard_RangeError_Raise_if (theUpper < theLower, "NCollection_Array1::Create");
230 #if (defined(__GNUC__) && __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
231 // gcc emits -Warray-bounds warning when NCollection_Array1 is initialized
232 // from local array with lower index 1 (so that (&theBegin - 1) points out of array bounds).
233 // NCollection_Array1 initializes myData with a shift to avoid this shift within per-element access.
234 // It is undesired changing this logic, and -Warray-bounds is not useful here.
235 #pragma GCC diagnostic push
236 #pragma GCC diagnostic ignored "-Warray-bounds"
238 myData = (TheItemType *) &theBegin - theLower;
239 #if (defined(__GNUC__) && __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
240 #pragma GCC diagnostic pop
244 //! Initialise the items with theValue
245 void Init (const TheItemType& theValue)
247 TheItemType *pCur = &myData[myLowerBound], *pEnd=&myData[myUpperBound];
248 for(; pCur <= pEnd; pCur++)
249 *pCur = (TheItemType&) theValue;
253 Standard_Integer Size (void) const
255 //! Length query (the same)
256 Standard_Integer Length (void) const
257 { return (myUpperBound-myLowerBound+1); }
259 //! Return TRUE if array has zero length.
260 Standard_Boolean IsEmpty() const { return myUpperBound < myLowerBound; }
263 Standard_Integer Lower (void) const
264 { return myLowerBound; }
266 Standard_Integer Upper (void) const
267 { return myUpperBound; }
270 Standard_Boolean IsDeletable (void) const
271 { return myDeletable; }
273 //! IsAllocated flag - for naming compatibility
274 Standard_Boolean IsAllocated (void) const
275 { return myDeletable; }
277 //! Copies data of theOther array to this.
278 //! This array should be pre-allocated and have the same length as theOther;
279 //! otherwise exception Standard_DimensionMismatch is thrown.
280 NCollection_Array1& Assign (const NCollection_Array1& theOther)
282 if (&theOther == this)
285 Standard_DimensionMismatch_Raise_if (Length() != theOther.Length(), "NCollection_Array1::operator=");
291 TheItemType * pMyItem = &myData[myLowerBound];
292 TheItemType * const pEndItem = &(theOther.myData)[theOther.myUpperBound];
293 TheItemType * pItem = &(theOther.myData)[theOther.myLowerBound];
294 while (pItem <= pEndItem) * pMyItem ++ = * pItem ++;
299 //! This array will borrow all the data from theOther.
300 //! The moved object will keep pointer to the memory buffer and
301 //! range, but it will not free the buffer on destruction.
302 NCollection_Array1& Move (NCollection_Array1& theOther)
304 if (&theOther == this)
311 delete[] &myData[myLowerBound];
314 myLowerBound = theOther.myLowerBound;
315 myUpperBound = theOther.myUpperBound;
316 myDeletable = theOther.myDeletable;
317 myData = theOther.myData;
319 theOther.myDeletable = Standard_False;
324 //! Assignment operator; @sa Assign()
325 NCollection_Array1& operator= (const NCollection_Array1& theOther)
327 return Assign (theOther);
330 #ifndef OCCT_NO_RVALUE_REFERENCE
331 //! Move assignment operator; @sa Move()
332 NCollection_Array1& operator= (NCollection_Array1&& theOther)
334 return Move (theOther);
338 //! @return first element
339 const TheItemType& First() const
341 return myData[myLowerBound];
344 //! @return first element
345 TheItemType& ChangeFirst()
347 return myData[myLowerBound];
350 //! @return last element
351 const TheItemType& Last() const
353 return myData[myUpperBound];
356 //! @return last element
357 TheItemType& ChangeLast()
359 return myData[myUpperBound];
362 //! Constant value access
363 const TheItemType& Value (const Standard_Integer theIndex) const
365 Standard_OutOfRange_Raise_if (theIndex < myLowerBound || theIndex > myUpperBound, "NCollection_Array1::Value");
366 return myData[theIndex];
369 //! operator() - alias to Value
370 const TheItemType& operator() (const Standard_Integer theIndex) const
371 { return Value (theIndex); }
373 //! operator[] - alias to Value
374 const TheItemType& operator[] (Standard_Integer theIndex) const { return Value (theIndex); }
376 //! Variable value access
377 TheItemType& ChangeValue (const Standard_Integer theIndex)
379 Standard_OutOfRange_Raise_if (theIndex < myLowerBound || theIndex > myUpperBound, "NCollection_Array1::ChangeValue");
380 return myData[theIndex];
383 //! operator() - alias to ChangeValue
384 TheItemType& operator() (const Standard_Integer theIndex)
385 { return ChangeValue (theIndex); }
387 //! operator[] - alias to ChangeValue
388 TheItemType& operator[] (Standard_Integer theIndex) { return ChangeValue (theIndex); }
391 void SetValue (const Standard_Integer theIndex,
392 const TheItemType& theItem)
394 Standard_OutOfRange_Raise_if (theIndex < myLowerBound || theIndex > myUpperBound, "NCollection_Array1::SetValue");
395 myData[theIndex] = theItem;
398 //! Resizes the array to specified bounds.
399 //! No re-allocation will be done if length of array does not change,
400 //! but existing values will not be discarded if theToCopyData set to FALSE.
401 //! @param theLower new lower bound of array
402 //! @param theUpper new upper bound of array
403 //! @param theToCopyData flag to copy existing data into new array
404 void Resize (const Standard_Integer theLower,
405 const Standard_Integer theUpper,
406 const Standard_Boolean theToCopyData)
408 Standard_RangeError_Raise_if (theUpper < theLower, "NCollection_Array1::Resize");
409 const Standard_Integer anOldLen = Length();
410 const Standard_Integer aNewLen = theUpper - theLower + 1;
411 const Standard_Integer aLowerOld = myLowerBound;
413 TheItemType* aBeginOld = &myData[aLowerOld];
414 myLowerBound = theLower;
415 myUpperBound = theUpper;
416 if (aNewLen == anOldLen)
418 myData = aBeginOld - theLower;
422 if (!theToCopyData && myDeletable)
426 TheItemType* aBeginNew = new TheItemType[aNewLen];
427 Standard_OutOfMemory_Raise_if (aBeginNew == NULL, "NCollection_Array1 : Allocation failed");
428 myData = aBeginNew - theLower;
431 myDeletable = Standard_True;
435 const Standard_Integer aLenCopy = Min (anOldLen, aNewLen);
436 for (Standard_Integer anIter = 0; anIter < aLenCopy; ++anIter)
438 aBeginNew[anIter] = aBeginOld[anIter];
444 myDeletable = Standard_True;
447 //! Destructor - releases the memory
448 ~NCollection_Array1 (void)
451 delete [] &(myData[myLowerBound]);
455 // ---------- PROTECTED FIELDS -----------
456 Standard_Integer myLowerBound;
457 Standard_Integer myUpperBound;
458 Standard_Boolean myDeletable; //!< Flag showing who allocated the array
459 TheItemType* myData; //!< Pointer to '0'th array item