From 4954e4970c0fd6ac7eabce387b5130f97be1eacb Mon Sep 17 00:00:00 2001 From: kgv Date: Fri, 17 Feb 2017 12:35:13 +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] --- src/NCollection/NCollection_Array1.hxx | 111 +++++++++++++++++++- src/Poly/FILES | 1 - src/Poly/Poly_Triangulation.cxx | 134 ++++++------------------- src/Poly/Poly_Triangulation.hxx | 20 ++-- src/Poly/Poly_Triangulation.lxx | 46 --------- 5 files changed, 148 insertions(+), 164 deletions(-) delete mode 100644 src/Poly/Poly_Triangulation.lxx 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]); } diff --git a/src/Poly/FILES b/src/Poly/FILES index 641d172ec0..5452e22df2 100755 --- a/src/Poly/FILES +++ b/src/Poly/FILES @@ -32,4 +32,3 @@ Poly_Triangle.hxx Poly_Triangle.lxx Poly_Triangulation.cxx Poly_Triangulation.hxx -Poly_Triangulation.lxx diff --git a/src/Poly/Poly_Triangulation.cxx b/src/Poly/Poly_Triangulation.cxx index 1e0466ae75..5e23ceba23 100644 --- a/src/Poly/Poly_Triangulation.cxx +++ b/src/Poly/Poly_Triangulation.cxx @@ -28,16 +28,14 @@ IMPLEMENT_STANDARD_RTTIEXT (Poly_Triangulation, MMgt_TShared) //function : Poly_Triangulation //purpose : //======================================================================= -Poly_Triangulation::Poly_Triangulation(const Standard_Integer NbNodes, - const Standard_Integer NbTriangles, - const Standard_Boolean UVNodes) : - myDeflection(0), - myNbNodes(NbNodes), - myNbTriangles(NbTriangles), - myNodes(1, NbNodes), - myTriangles(1, NbTriangles) +Poly_Triangulation::Poly_Triangulation(const Standard_Integer theNbNodes, + const Standard_Integer theNbTriangles, + const Standard_Boolean theHasUVNodes) +: myDeflection(0), + myNodes (1, theNbNodes), + myTriangles (1, theNbTriangles) { - if (UVNodes) myUVNodes = new TColgp_HArray1OfPnt2d(1, myNbNodes); + if (theHasUVNodes) myUVNodes = new TColgp_HArray1OfPnt2d(1, theNbNodes); } //======================================================================= @@ -45,16 +43,14 @@ Poly_Triangulation::Poly_Triangulation(const Standard_Integer NbNodes, //purpose : //======================================================================= -Poly_Triangulation::Poly_Triangulation(const TColgp_Array1OfPnt& Nodes, - const Poly_Array1OfTriangle& Triangles) : - myDeflection(0), - myNbNodes(Nodes.Length()), - myNbTriangles(Triangles.Length()), - myNodes(1, Nodes.Length()), - myTriangles(1, Triangles.Length()) +Poly_Triangulation::Poly_Triangulation(const TColgp_Array1OfPnt& theNodes, + const Poly_Array1OfTriangle& theTriangles) +: myDeflection(0), + myNodes (1, theNodes.Length()), + myTriangles (1, theTriangles.Length()) { - myNodes = Nodes; - myTriangles = Triangles; + myNodes = theNodes; + myTriangles = theTriangles; } //======================================================================= @@ -62,19 +58,17 @@ Poly_Triangulation::Poly_Triangulation(const TColgp_Array1OfPnt& Nodes, //purpose : //======================================================================= -Poly_Triangulation::Poly_Triangulation(const TColgp_Array1OfPnt& Nodes, - const TColgp_Array1OfPnt2d& UVNodes, - const Poly_Array1OfTriangle& Triangles) : - myDeflection(0), - myNbNodes(Nodes.Length()), - myNbTriangles(Triangles.Length()), - myNodes(1, Nodes.Length()), - myTriangles(1, Triangles.Length()) +Poly_Triangulation::Poly_Triangulation(const TColgp_Array1OfPnt& theNodes, + const TColgp_Array1OfPnt2d& theUVNodes, + const Poly_Array1OfTriangle& theTriangles) +: myDeflection(0), + myNodes (1, theNodes.Length()), + myTriangles (1, theTriangles.Length()) { - myNodes = Nodes; - myTriangles = Triangles; - myUVNodes = new TColgp_HArray1OfPnt2d(1, myNbNodes); - myUVNodes->ChangeArray1() = UVNodes; + myNodes = theNodes; + myTriangles = theTriangles; + myUVNodes = new TColgp_HArray1OfPnt2d (1, theNodes.Length()); + myUVNodes->ChangeArray1() = theUVNodes; } //======================================================================= @@ -121,17 +115,7 @@ Poly_Triangulation::Poly_Triangulation (const Handle(Poly_Triangulation)& theTri //purpose : //======================================================================= -Standard_Real Poly_Triangulation::Deflection() const -{ - return myDeflection; -} - -//======================================================================= -//function : Deflection -//purpose : -//======================================================================= - -void Poly_Triangulation::Deflection (const Standard_Real theDeflection) +void Poly_Triangulation::Deflection(const Standard_Real theDeflection) { myDeflection = theDeflection; } @@ -146,26 +130,6 @@ void Poly_Triangulation::RemoveUVNodes() myUVNodes.Nullify(); } -//======================================================================= -//function : Nodes -//purpose : -//======================================================================= - -const TColgp_Array1OfPnt& Poly_Triangulation::Nodes() const -{ - return myNodes; -} - -//======================================================================= -//function : ChangeNodes -//purpose : -//======================================================================= - -TColgp_Array1OfPnt& Poly_Triangulation::ChangeNodes() -{ - return myNodes; -} - //======================================================================= //function : Node //purpose : @@ -194,26 +158,6 @@ gp_Pnt& Poly_Triangulation::ChangeNode (const Standard_Integer theIndex) return myNodes.ChangeValue (theIndex); } -//======================================================================= -//function : UVNodes -//purpose : -//======================================================================= - -const TColgp_Array1OfPnt2d& Poly_Triangulation::UVNodes() const -{ - return myUVNodes->Array1(); -} - -//======================================================================= -//function : ChangeUVNodes -//purpose : -//======================================================================= - -TColgp_Array1OfPnt2d& Poly_Triangulation::ChangeUVNodes() -{ - return myUVNodes->ChangeArray1(); -} - //======================================================================= //function : UVNode //purpose : @@ -242,26 +186,6 @@ gp_Pnt2d& Poly_Triangulation::ChangeUVNode (const Standard_Integer theIndex) return myUVNodes->ChangeValue (theIndex); } -//======================================================================= -//function : Triangles -//purpose : -//======================================================================= - -const Poly_Array1OfTriangle& Poly_Triangulation::Triangles() const -{ - return myTriangles; -} - -//======================================================================= -//function : ChangeTriangles -//purpose : -//======================================================================= - -Poly_Array1OfTriangle& Poly_Triangulation::ChangeTriangles() -{ - return myTriangles; -} - //======================================================================= //function : Triangle //purpose : @@ -298,7 +222,7 @@ Poly_Triangle& Poly_Triangulation::ChangeTriangle (const Standard_Integer theInd void Poly_Triangulation::SetNormals (const Handle(TShort_HArray1OfShortReal)& theNormals) { - if(theNormals.IsNull() || theNormals->Length() != 3*myNbNodes) { + if(theNormals.IsNull() || theNormals->Length() != 3 * NbNodes()) { throw Standard_DomainError("Poly_Triangulation::SetNormals : wrong length"); } @@ -313,7 +237,7 @@ void Poly_Triangulation::SetNormals (const Handle(TShort_HArray1OfShortReal)& th const TShort_Array1OfShortReal& Poly_Triangulation::Normals() const { - if(myNormals.IsNull() || myNormals->Length() != 3*myNbNodes) { + if(myNormals.IsNull() || myNormals->Length() != 3 * NbNodes()) { throw Standard_NullObject("Poly_Triangulation::Normals : " "wrong length or null array"); } @@ -329,7 +253,7 @@ const TShort_Array1OfShortReal& Poly_Triangulation::Normals() const TShort_Array1OfShortReal& Poly_Triangulation::ChangeNormals() { - if(myNormals.IsNull() || myNormals->Length() != 3*myNbNodes) { + if(myNormals.IsNull() || myNormals->Length() != 3 * NbNodes()) { throw Standard_NullObject("Poly_Triangulation::ChangeNormals : " "wrong length or null array"); } @@ -344,7 +268,7 @@ TShort_Array1OfShortReal& Poly_Triangulation::ChangeNormals() Standard_Boolean Poly_Triangulation::HasNormals() const { - if(myNormals.IsNull() || myNormals->Length() != 3*myNbNodes) { + if(myNormals.IsNull() || myNormals->Length() != 3 * NbNodes()) { return Standard_False; } return Standard_True; diff --git a/src/Poly/Poly_Triangulation.hxx b/src/Poly/Poly_Triangulation.hxx index b6051309de..bb0c2fb367 100644 --- a/src/Poly/Poly_Triangulation.hxx +++ b/src/Poly/Poly_Triangulation.hxx @@ -97,7 +97,7 @@ public: Standard_EXPORT Poly_Triangulation (const Handle(Poly_Triangulation)& theTriangulation); //! Returns the deflection of this triangulation. - Standard_EXPORT Standard_Real Deflection() const; + Standard_Real Deflection() const { return myDeflection; } //! Sets the deflection of this triangulation to theDeflection. //! See more on deflection in Polygon2D @@ -107,23 +107,23 @@ public: Standard_EXPORT void RemoveUVNodes(); //! Returns the number of nodes for this triangulation. - Standard_Integer NbNodes() const { return myNbNodes; } + Standard_Integer NbNodes() const { return myNodes.Length(); } //! Returns the number of triangles for this triangulation. - Standard_Integer NbTriangles() const { return myNbTriangles; } + Standard_Integer NbTriangles() const { return myTriangles.Length(); } //! Returns Standard_True if 2D nodes are associated with 3D nodes for this triangulation. Standard_Boolean HasUVNodes() const { return !myUVNodes.IsNull(); } //! Returns the table of 3D nodes (3D points) for this triangulation. - Standard_EXPORT const TColgp_Array1OfPnt& Nodes() const; + const TColgp_Array1OfPnt& Nodes() const { return myNodes; } //! Returns the table of 3D nodes (3D points) for this triangulation. //! The returned array is //! shared. Therefore if the table is selected by reference, you //! can, by simply modifying it, directly modify the data //! structure of this triangulation. - Standard_EXPORT TColgp_Array1OfPnt& ChangeNodes(); + TColgp_Array1OfPnt& ChangeNodes() { return myNodes; } //! Returns node at the given index. //! Raises Standard_OutOfRange exception if theIndex is less than 1 or greater than NbNodes. @@ -138,7 +138,7 @@ public: //! The function HasUVNodes checks if 2D nodes //! are associated with the 3D nodes of this triangulation. //! Const reference on the 2d nodes values. - Standard_EXPORT const TColgp_Array1OfPnt2d& UVNodes() const; + const TColgp_Array1OfPnt2d& UVNodes() const { return myUVNodes->Array1(); } //! Returns the table of 2D nodes (2D points) associated with //! each 3D node of this triangulation. @@ -146,7 +146,7 @@ public: //! Therefore if the table is selected by reference, //! you can, by simply modifying it, directly modify the data //! structure of this triangulation. - Standard_EXPORT TColgp_Array1OfPnt2d& ChangeUVNodes(); + TColgp_Array1OfPnt2d& ChangeUVNodes() { return myUVNodes->ChangeArray1(); } //! Returns UVNode at the given index. //! Raises Standard_OutOfRange exception if theIndex is less than 1 or greater than NbNodes. @@ -157,14 +157,14 @@ public: Standard_EXPORT gp_Pnt2d& ChangeUVNode (const Standard_Integer theIndex); //! Returns the table of triangles for this triangulation. - Standard_EXPORT const Poly_Array1OfTriangle& Triangles() const; + const Poly_Array1OfTriangle& Triangles() const { return myTriangles; } //! Returns the table of triangles for this triangulation. //! Function ChangeUVNodes shares the returned array. //! Therefore if the table is selected by reference, //! you can, by simply modifying it, directly modify the data //! structure of this triangulation. - Standard_EXPORT Poly_Array1OfTriangle& ChangeTriangles(); + Poly_Array1OfTriangle& ChangeTriangles() { return myTriangles; } //! Returns triangle at the given index. //! Raises Standard_OutOfRange exception if theIndex is less than 1 or greater than NbTriangles. @@ -199,8 +199,6 @@ public: protected: Standard_Real myDeflection; - Standard_Integer myNbNodes; - Standard_Integer myNbTriangles; TColgp_Array1OfPnt myNodes; Handle(TColgp_HArray1OfPnt2d) myUVNodes; Poly_Array1OfTriangle myTriangles; diff --git a/src/Poly/Poly_Triangulation.lxx b/src/Poly/Poly_Triangulation.lxx deleted file mode 100644 index 63809e9660..0000000000 --- a/src/Poly/Poly_Triangulation.lxx +++ /dev/null @@ -1,46 +0,0 @@ -// Created on: 1995-03-06 -// Created by: Laurent PAINNOT -// Copyright (c) 1995-1999 Matra Datavision -// Copyright (c) 1999-2014 OPEN CASCADE SAS -// -// This file is part of Open CASCADE Technology software library. -// -// This library is free software; you can redistribute it and/or modify it under -// the terms of the GNU Lesser General Public License version 2.1 as published -// by the Free Software Foundation, with special exception defined in the file -// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT -// distribution for complete text of the license and disclaimer of any warranty. -// -// Alternatively, this file may be used under the terms of Open CASCADE -// commercial license or contractual agreement. - -//======================================================================= -//function : NbNodes -//purpose : -//======================================================================= - -inline Standard_Integer Poly_Triangulation::NbNodes() const -{ - return myNbNodes; -} - -//======================================================================= -//function : NbTriangles -//purpose : -//======================================================================= - -inline Standard_Integer Poly_Triangulation::NbTriangles() const -{ - return myNbTriangles; -} - -//======================================================================= -//function : HasUVNodes -//purpose : -//======================================================================= - -inline Standard_Boolean Poly_Triangulation::HasUVNodes() const -{ - return !myUVNodes.IsNull(); -} -