mirror of
https://git.dev.opencascade.org/repos/occt.git
synced 2025-08-29 14:00:49 +03:00
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
This commit is contained in:
@@ -154,6 +154,16 @@ public:
|
|||||||
public:
|
public:
|
||||||
// ---------- PUBLIC METHODS ------------
|
// ---------- PUBLIC METHODS ------------
|
||||||
|
|
||||||
|
//! Empty constructor; should be used with caution.
|
||||||
|
NCollection_Array1()
|
||||||
|
: myLowerBound (1),
|
||||||
|
myUpperBound (0),
|
||||||
|
myDeletable (Standard_False),
|
||||||
|
myData (NULL)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
//! Constructor
|
//! Constructor
|
||||||
NCollection_Array1(const Standard_Integer theLower,
|
NCollection_Array1(const Standard_Integer theLower,
|
||||||
const Standard_Integer theUpper) :
|
const Standard_Integer theUpper) :
|
||||||
@@ -181,6 +191,18 @@ public:
|
|||||||
*this = theOther;
|
*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
|
//! C array-based constructor
|
||||||
NCollection_Array1 (const TheItemType& theBegin,
|
NCollection_Array1 (const TheItemType& theBegin,
|
||||||
const Standard_Integer theLower,
|
const Standard_Integer theLower,
|
||||||
@@ -208,6 +230,9 @@ public:
|
|||||||
Standard_Integer Length (void) const
|
Standard_Integer Length (void) const
|
||||||
{ return (myUpperBound-myLowerBound+1); }
|
{ return (myUpperBound-myLowerBound+1); }
|
||||||
|
|
||||||
|
//! Return TRUE if array has zero length.
|
||||||
|
Standard_Boolean IsEmpty() const { return myUpperBound < myLowerBound; }
|
||||||
|
|
||||||
//! Lower bound
|
//! Lower bound
|
||||||
Standard_Integer Lower (void) const
|
Standard_Integer Lower (void) const
|
||||||
{ return myLowerBound; }
|
{ return myLowerBound; }
|
||||||
@@ -228,7 +253,13 @@ public:
|
|||||||
{
|
{
|
||||||
if (&theOther == this)
|
if (&theOther == this)
|
||||||
return *this;
|
return *this;
|
||||||
|
|
||||||
Standard_DimensionMismatch_Raise_if (Length() != theOther.Length(), "NCollection_Array1::operator=");
|
Standard_DimensionMismatch_Raise_if (Length() != theOther.Length(), "NCollection_Array1::operator=");
|
||||||
|
if (myData == NULL)
|
||||||
|
{
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
TheItemType * pMyItem = &myData[myLowerBound];
|
TheItemType * pMyItem = &myData[myLowerBound];
|
||||||
TheItemType * const pEndItem = &(theOther.myData)[theOther.myUpperBound];
|
TheItemType * const pEndItem = &(theOther.myData)[theOther.myUpperBound];
|
||||||
TheItemType * pItem = &(theOther.myData)[theOther.myLowerBound];
|
TheItemType * pItem = &(theOther.myData)[theOther.myLowerBound];
|
||||||
@@ -236,12 +267,41 @@ public:
|
|||||||
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
|
//! Assignment operator
|
||||||
NCollection_Array1& operator= (const NCollection_Array1& theOther)
|
NCollection_Array1& operator= (const NCollection_Array1& theOther)
|
||||||
{
|
{
|
||||||
return Assign (theOther);
|
return Assign (theOther);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! Move assignment operator.
|
||||||
|
NCollection_Array1& operator= (NCollection_Array1&& theOther)
|
||||||
|
{
|
||||||
|
return Move (std::move (theOther));
|
||||||
|
}
|
||||||
|
|
||||||
//! @return first element
|
//! @return first element
|
||||||
const TheItemType& First() const
|
const TheItemType& First() const
|
||||||
{
|
{
|
||||||
@@ -296,6 +356,55 @@ public:
|
|||||||
myData[theIndex] = theItem;
|
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
|
//! Destructor - releases the memory
|
||||||
~NCollection_Array1 (void)
|
~NCollection_Array1 (void)
|
||||||
{ if (myDeletable) delete [] &(myData[myLowerBound]); }
|
{ if (myDeletable) delete [] &(myData[myLowerBound]); }
|
||||||
|
Reference in New Issue
Block a user