]> OCCT Git - occt-copy.git/commitdiff
0028470: Foundation Classes, NCollection_Array1 - add Resize() method for re-allocati...
authorkgv <kgv@opencascade.com>
Sat, 17 Mar 2018 11:49:58 +0000 (14:49 +0300)
committerkgv <kgv@opencascade.com>
Sat, 17 Mar 2018 12:30:10 +0000 (15:30 +0300)
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

index 580fa69bd307f21fe1f2a23c51db8bb2f471b38a..c476322fe06fd884c2a97e737cf3d3b4a642dcf8 100644 (file)
@@ -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]); }