From db3bc35477865cb18fc83bba255b5d91d87bb2d0 Mon Sep 17 00:00:00 2001 From: kgv Date: Sat, 17 Mar 2018 14:49:58 +0300 Subject: [PATCH] 0028470: Foundation Classes, NCollection_Array1 - add Resize() method for re-allocating array with new limits NCollection_Array1 now provides method Resize() for re-allocating array to new bounds. Added Move Constructor and Move Assignment operator. Added empty constructor defining array of zero size. #Poly_Triangulation, dropped duplicating fields myNbNodes and myNbTriangles. #Removed unused file Poly_Triangulation.lxx. Use std::move within NCollection_Array1::operator=() [fix for 0028470] # Conflicts: # src/Poly/Poly_Triangulation.cxx # src/Poly/Poly_Triangulation.hxx --- src/NCollection/NCollection_Array1.hxx | 111 ++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-) diff --git a/src/NCollection/NCollection_Array1.hxx b/src/NCollection/NCollection_Array1.hxx index 580fa69bd3..c476322fe0 100644 --- a/src/NCollection/NCollection_Array1.hxx +++ b/src/NCollection/NCollection_Array1.hxx @@ -154,6 +154,16 @@ public: public: // ---------- PUBLIC METHODS ------------ + //! Empty constructor; should be used with caution. + NCollection_Array1() + : myLowerBound (1), + myUpperBound (0), + myDeletable (Standard_False), + myData (NULL) + { + // + } + //! Constructor NCollection_Array1(const Standard_Integer theLower, const Standard_Integer theUpper) : @@ -181,6 +191,18 @@ public: *this = theOther; } + //! Move constructor + NCollection_Array1 (const NCollection_Array1&& theOther) + : myLowerBound (theOther.myLowerBound), + myUpperBound (theOther.myUpperBound), + myDeletable (theOther.myDeletable), + myData (theOther.myData) + { + theOther.myUpperBound = theOther.myLowerBound - 1; + theOther.myDeletable = false; + theOther.myData = NULL; + } + //! C array-based constructor NCollection_Array1 (const TheItemType& theBegin, const Standard_Integer theLower, @@ -208,6 +230,9 @@ public: Standard_Integer Length (void) const { return (myUpperBound-myLowerBound+1); } + //! Return TRUE if array has zero length. + Standard_Boolean IsEmpty() const { return myUpperBound < myLowerBound; } + //! Lower bound Standard_Integer Lower (void) const { return myLowerBound; } @@ -228,12 +253,41 @@ public: { if (&theOther == this) return *this; + Standard_DimensionMismatch_Raise_if (Length() != theOther.Length(), "NCollection_Array1::operator="); + if (myData == NULL) + { + return *this; + } + TheItemType * pMyItem = &myData[myLowerBound]; TheItemType * const pEndItem = &(theOther.myData)[theOther.myUpperBound]; TheItemType * pItem = &(theOther.myData)[theOther.myLowerBound]; while (pItem <= pEndItem) * pMyItem ++ = * pItem ++; - return *this; + return *this; + } + + //! Move assignment + NCollection_Array1& Move (NCollection_Array1&& theOther) + { + if (&theOther == this) + { + return *this; + } + + if (myDeletable) + { + delete[] &myData[myLowerBound]; + } + myLowerBound = theOther.myLowerBound; + myUpperBound = theOther.myUpperBound; + myDeletable = theOther.myDeletable; + myData = theOther.myData; + + theOther.myUpperBound = theOther.myLowerBound - 1; + theOther.myDeletable = Standard_False; + theOther.myData = NULL; + return *this; } //! Assignment operator @@ -242,6 +296,12 @@ public: return Assign (theOther); } + //! Move assignment operator. + NCollection_Array1& operator= (NCollection_Array1&& theOther) + { + return Move (std::move (theOther)); + } + //! @return first element const TheItemType& First() const { @@ -296,6 +356,55 @@ public: myData[theIndex] = theItem; } + //! Resizes the array to specified bounds. + //! No re-allocation will be done if length of array does not change, + //! but existing values will not be discarded if theToCopyData set to FALSE. + //! @param theLower new lower bound of array + //! @param theUpper new upper bound of array + //! @param theToCopyData flag to copy existing data into new array + void Resize (const Standard_Integer theLower, + const Standard_Integer theUpper, + const Standard_Boolean theToCopyData) + { + Standard_RangeError_Raise_if (theUpper < theLower, "NCollection_Array1::Resize"); + const Standard_Integer anOldLen = Length(); + const Standard_Integer aNewLen = theUpper - theLower + 1; + const Standard_Integer aLowerOld = myLowerBound; + + TheItemType* aBeginOld = &myData[aLowerOld]; + myLowerBound = theLower; + myUpperBound = theUpper; + if (aNewLen == anOldLen) + { + myData = aBeginOld - theLower; + return; + } + + if (!theToCopyData && myDeletable) + { + delete[] aBeginOld; + } + TheItemType* aBeginNew = new TheItemType[aNewLen]; + Standard_OutOfMemory_Raise_if (aBeginNew == NULL, "NCollection_Array1 : Allocation failed"); + myData = aBeginNew - theLower; + if (!theToCopyData) + { + myDeletable = Standard_True; + return; + } + + const Standard_Integer aLenCopy = Min (anOldLen, aNewLen); + for (Standard_Integer anIter = 0; anIter < aLenCopy; ++anIter) + { + aBeginNew[anIter] = aBeginOld[anIter]; + } + if (myDeletable) + { + delete[] aBeginOld; + } + myDeletable = Standard_True; + } + //! Destructor - releases the memory ~NCollection_Array1 (void) { if (myDeletable) delete [] &(myData[myLowerBound]); } -- 2.39.5