From: kgv Date: Tue, 12 Mar 2019 03:05:21 +0000 (+0300) Subject: 0030558: Coding - replace math_SingleTab with NCollection_LocalArray X-Git-Tag: V7_4_0_beta~204 X-Git-Url: http://git.dev.opencascade.org/gitweb/?p=occt.git;a=commitdiff_plain;h=18434846a3baf0ad7fee10c88ff246aa47a22205 0030558: Coding - replace math_SingleTab with NCollection_LocalArray math_Vector::Multiplied() - fixed modification of original array. math_SVD::Solve(), math_SVD::PseudoInverse(), GeomFill_LocationGuide::InitX() - removed incorrect constness. math_Vector, math_IntegerVector - math_SingleTab has been replaced by NCollection_LocalArray+NCollection_Array1. Added accessors returning const value. --- diff --git a/src/FEmTool/FEmTool_ProfileMatrix.cxx b/src/FEmTool/FEmTool_ProfileMatrix.cxx index 345f83c0c3..b61ab7665a 100644 --- a/src/FEmTool/FEmTool_ProfileMatrix.cxx +++ b/src/FEmTool/FEmTool_ProfileMatrix.cxx @@ -155,7 +155,7 @@ FEmTool_ProfileMatrix::FEmTool_ProfileMatrix(const TColStd_Array1OfInteger& Firs Standard_Real * x = &X(X.Lower()); x--; - Standard_Real * b = &B(B.Lower()); + const Standard_Real * b = &B(B.Lower()); b--; const Standard_Real * SMA = &SMatrix->Value(1); SMA --; @@ -207,7 +207,7 @@ FEmTool_ProfileMatrix::FEmTool_ProfileMatrix(const TColStd_Array1OfInteger& Firs Standard_Integer i, j, jj, DiagAddr, CurrAddr; Standard_Real * m = &MX(MX.Lower()); m--; - Standard_Real * x = &X(X.Lower()); + const Standard_Real * x = &X(X.Lower()); x--; const Standard_Real * PM = &ProfileMatrix->Value(1); PM--; diff --git a/src/GeomFill/GeomFill_LocationGuide.cxx b/src/GeomFill/GeomFill_LocationGuide.cxx index d18a957d76..54ca0ab807 100644 --- a/src/GeomFill/GeomFill_LocationGuide.cxx +++ b/src/GeomFill/GeomFill_LocationGuide.cxx @@ -1381,7 +1381,7 @@ void GeomFill_LocationGuide::Resolution (const Standard_Integer , //Function : InitX //Purpose : recherche par interpolation d'une valeur initiale //================================================================== -void GeomFill_LocationGuide::InitX(const Standard_Real Param) const +void GeomFill_LocationGuide::InitX(const Standard_Real Param) { Standard_Integer Ideb = 1, Ifin = myPoles2d->RowLength(), Idemi; diff --git a/src/GeomFill/GeomFill_LocationGuide.hxx b/src/GeomFill/GeomFill_LocationGuide.hxx index d0d2d06318..5fe11c4ccb 100644 --- a/src/GeomFill/GeomFill_LocationGuide.hxx +++ b/src/GeomFill/GeomFill_LocationGuide.hxx @@ -186,7 +186,7 @@ private: Standard_EXPORT void SetRotation (const Standard_Real PrecAngle, Standard_Real& LastAngle); - Standard_EXPORT void InitX (const Standard_Real Param) const; + Standard_EXPORT void InitX (const Standard_Real Param); Handle(GeomFill_TrihedronWithGuide) myLaw; Handle(GeomFill_SectionLaw) mySec; diff --git a/src/math/FILES b/src/math/FILES index c4043e2bf8..4a3bb324a6 100755 --- a/src/math/FILES +++ b/src/math/FILES @@ -111,7 +111,6 @@ math_PSOParticlesPool.cxx math_PSOParticlesPool.hxx math_Recipes.cxx math_Recipes.hxx -math_SingleTab.hxx math_SingularMatrix.hxx math_Status.hxx math_SVD.cxx diff --git a/src/math/math_IntegerVector.cxx b/src/math/math_IntegerVector.cxx index ae3b192965..60541a3e95 100644 --- a/src/math/math_IntegerVector.cxx +++ b/src/math/math_IntegerVector.cxx @@ -12,40 +12,31 @@ // Alternatively, this file may be used under the terms of Open CASCADE // commercial license or contractual agreement. -#define No_Standard_RangeError -#define No_Standard_OutOfRange -#define No_Standard_DimensionError - #include #include #include -math_IntegerVector::math_IntegerVector(const Standard_Integer theFirst, const Standard_Integer theLast) : - FirstIndex(theFirst), - LastIndex(theLast), - Array(theFirst, theLast) +math_IntegerVector::math_IntegerVector(const Standard_Integer theFirst, const Standard_Integer theLast) +: myLocArray (theLast - theFirst + 1), + Array (myLocArray[0], theFirst, theLast) { - Standard_RangeError_Raise_if(theFirst > theLast, " "); + // } math_IntegerVector::math_IntegerVector(const Standard_Integer theFirst, const Standard_Integer theLast, - const Standard_Integer theInitialValue) : - FirstIndex(theFirst), - LastIndex(theLast), - Array(theFirst, theLast) + const Standard_Integer theInitialValue) +: myLocArray (theLast - theFirst + 1), + Array (myLocArray[0], theFirst, theLast) { - Standard_RangeError_Raise_if(theFirst > theLast, " "); Array.Init(theInitialValue); } -math_IntegerVector::math_IntegerVector(const Standard_Address theTab, +math_IntegerVector::math_IntegerVector(const Standard_Integer* theTab, const Standard_Integer theFirst, - const Standard_Integer theLast) : - FirstIndex(theFirst), - LastIndex(theLast), - Array(theTab, theFirst, theLast) + const Standard_Integer theLast) +: Array (*theTab, theFirst, theLast) { Standard_RangeError_Raise_if(theFirst > theLast, " "); } @@ -55,24 +46,22 @@ void math_IntegerVector::Init(const Standard_Integer theInitialValue) Array.Init(theInitialValue); } -math_IntegerVector::math_IntegerVector(const math_IntegerVector& theOther) : - FirstIndex(theOther.FirstIndex), - LastIndex(theOther.LastIndex), - Array(theOther.Array) +math_IntegerVector::math_IntegerVector (const math_IntegerVector& theOther) +: myLocArray (theOther.Length()), + Array (myLocArray[0], theOther.Lower(), theOther.Upper()) { + memcpy (&myLocArray[0], &theOther.Array.First(), sizeof(Standard_Integer) * theOther.Length()); } void math_IntegerVector::SetFirst(const Standard_Integer theFirst) { - Array.SetLower(theFirst); - LastIndex = LastIndex - FirstIndex + theFirst; - FirstIndex = theFirst; + Array.Resize (theFirst, Array.Upper() - Array.Lower() + theFirst, Standard_False); } Standard_Real math_IntegerVector::Norm() const { Standard_Real Result = 0; - for(Standard_Integer Index = FirstIndex; Index <= LastIndex; Index++) + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Result = Result + Array(Index) * Array(Index); } @@ -82,7 +71,7 @@ Standard_Real math_IntegerVector::Norm() const Standard_Real math_IntegerVector::Norm2() const { Standard_Real Result = 0; - for(Standard_Integer Index = FirstIndex; Index <= LastIndex; Index++) + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Result = Result + Array(Index) * Array(Index); } @@ -93,7 +82,7 @@ Standard_Integer math_IntegerVector::Max() const { Standard_Integer I=0; Standard_Real X = RealFirst(); - for(Standard_Integer Index = FirstIndex; Index <= LastIndex; Index++) + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { if(Array(Index) > X) { @@ -108,7 +97,7 @@ Standard_Integer math_IntegerVector::Min() const { Standard_Integer I=0; Standard_Real X = RealLast(); - for(Standard_Integer Index = FirstIndex; Index <= LastIndex; Index++) + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { if(Array(Index) < X) { @@ -124,9 +113,9 @@ void math_IntegerVector::Invert() Standard_Integer J; Standard_Integer Temp; - for(Standard_Integer Index = FirstIndex; Index <= FirstIndex + Length() / 2 ; Index++) + for(Standard_Integer Index = Lower(); Index <= Lower() + Length() / 2 ; Index++) { - J = LastIndex + FirstIndex - Index; + J = Upper() + Lower() - Index; Temp = Array(Index); Array(Index) = Array(J); Array(J) = Temp; @@ -144,7 +133,7 @@ void math_IntegerVector::Set(const Standard_Integer theI1, const Standard_Integer theI2, const math_IntegerVector &theV) { - Standard_DimensionError_Raise_if((theI1 < FirstIndex) || (theI2 > LastIndex) || + Standard_DimensionError_Raise_if((theI1 < Lower()) || (theI2 > Upper()) || (theI1 > theI2) || (theI2 - theI1 + 1 != theV.Length()), " "); Standard_Integer I = theV.Lower(); @@ -157,7 +146,7 @@ void math_IntegerVector::Set(const Standard_Integer theI1, void math_IntegerVector::Multiply(const Standard_Integer theRight) { - for(Standard_Integer Index = FirstIndex; Index <= LastIndex; Index++) + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Array(Index) = Array(Index) * theRight; } @@ -167,8 +156,8 @@ void math_IntegerVector::Add(const math_IntegerVector& theRight) { Standard_DimensionError_Raise_if(Length() != theRight.Length(), " "); - Standard_Integer I = theRight.FirstIndex; - for(Standard_Integer Index = FirstIndex; Index <= LastIndex; Index++) + Standard_Integer I = theRight.Lower(); + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Array(Index) = Array(Index) + theRight.Array(I); I++; @@ -178,8 +167,8 @@ void math_IntegerVector::Add(const math_IntegerVector& theRight) void math_IntegerVector::Subtract(const math_IntegerVector& theRight) { Standard_DimensionError_Raise_if(Length() != theRight.Length(), " "); - Standard_Integer I = theRight.FirstIndex; - for(Standard_Integer Index = FirstIndex; Index <= LastIndex; Index++) + Standard_Integer I = theRight.Lower(); + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Array(Index) = Array(Index) - theRight.Array(I); I++; @@ -189,8 +178,8 @@ void math_IntegerVector::Subtract(const math_IntegerVector& theRight) math_IntegerVector math_IntegerVector::Slice(const Standard_Integer theI1, const Standard_Integer theI2) const { - Standard_DimensionError_Raise_if((theI1 < FirstIndex) || (theI1 > LastIndex) || - (theI2 < FirstIndex) || (theI2 > LastIndex), " "); + Standard_DimensionError_Raise_if((theI1 < Lower()) || (theI1 > Upper()) || + (theI2 < Lower()) || (theI2 > Upper()), " "); if(theI2 >= theI1) { @@ -218,7 +207,7 @@ Standard_Integer math_IntegerVector::Multiplied (const math_IntegerVector& theRi Standard_DimensionError_Raise_if(Length() != theRight.Length(), " "); - Standard_Integer I = theRight.FirstIndex; + Standard_Integer I = theRight.Lower(); for(Standard_Integer Index = 0; Index < Length(); Index++) { Result = Result + Array(Index) * theRight.Array(I); @@ -229,9 +218,9 @@ Standard_Integer math_IntegerVector::Multiplied (const math_IntegerVector& theRi math_IntegerVector math_IntegerVector::Multiplied (const Standard_Integer theRight)const { - math_IntegerVector Result(FirstIndex, LastIndex); + math_IntegerVector Result(Lower(), Upper()); - for(Standard_Integer Index = FirstIndex; Index <= LastIndex; Index++) + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Result.Array(Index) = Array(Index) * theRight; } @@ -240,9 +229,9 @@ math_IntegerVector math_IntegerVector::Multiplied (const Standard_Integer theRig math_IntegerVector math_IntegerVector::TMultiplied (const Standard_Integer theRight) const { - math_IntegerVector Result(FirstIndex, LastIndex); + math_IntegerVector Result(Lower(), Upper()); - for(Standard_Integer Index = FirstIndex; Index <= LastIndex; Index++) + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Result.Array(Index) = Array(Index) * theRight; } @@ -253,10 +242,10 @@ math_IntegerVector math_IntegerVector::Added (const math_IntegerVector& theRight { Standard_DimensionError_Raise_if(Length() != theRight.Length(), " "); - math_IntegerVector Result(FirstIndex, LastIndex); + math_IntegerVector Result(Lower(), Upper()); - Standard_Integer I = theRight.FirstIndex; - for(Standard_Integer Index = FirstIndex; Index <= LastIndex; Index++) + Standard_Integer I = theRight.Lower(); + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Result.Array(Index) = Array(Index) + theRight.Array(I); I++; @@ -266,9 +255,9 @@ math_IntegerVector math_IntegerVector::Added (const math_IntegerVector& theRight math_IntegerVector math_IntegerVector::Opposite() { - math_IntegerVector Result(FirstIndex, LastIndex); + math_IntegerVector Result(Lower(), Upper()); - for(Standard_Integer Index = FirstIndex; Index <= LastIndex; Index++) + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Result.Array(Index) = - Array(Index); } @@ -279,10 +268,10 @@ math_IntegerVector math_IntegerVector::Subtracted (const math_IntegerVector& the { Standard_DimensionError_Raise_if(Length() != theRight.Length(), " "); - math_IntegerVector Result(FirstIndex, LastIndex); + math_IntegerVector Result(Lower(), Upper()); - Standard_Integer I = theRight.FirstIndex; - for(Standard_Integer Index = FirstIndex; Index <= LastIndex; Index++) + Standard_Integer I = theRight.Lower(); + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Result.Array(Index) = Array(Index) - theRight.Array(I); I++; @@ -295,9 +284,9 @@ void math_IntegerVector::Add (const math_IntegerVector& theLeft, const math_Inte Standard_DimensionError_Raise_if((Length() != theRight.Length()) || (theRight.Length() != theLeft.Length()), " "); - Standard_Integer I = theLeft.FirstIndex; - Standard_Integer J = theRight.FirstIndex; - for(Standard_Integer Index = FirstIndex; Index <= LastIndex; Index++) + Standard_Integer I = theLeft.Lower(); + Standard_Integer J = theRight.Lower(); + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Array(Index) = theLeft.Array(I) + theRight.Array(J); I++; @@ -311,9 +300,9 @@ void math_IntegerVector::Subtract (const math_IntegerVector& theLeft, Standard_DimensionError_Raise_if((Length() != theRight.Length()) || (theRight.Length() != theLeft.Length()), " "); - Standard_Integer I = theLeft.FirstIndex; - Standard_Integer J = theRight.FirstIndex; - for(Standard_Integer Index = FirstIndex; Index <= LastIndex; Index++) + Standard_Integer I = theLeft.Lower(); + Standard_Integer J = theRight.Lower(); + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Array(Index) = theLeft.Array(I) - theRight.Array(J); I++; @@ -324,7 +313,7 @@ void math_IntegerVector::Subtract (const math_IntegerVector& theLeft, void math_IntegerVector::Multiply(const Standard_Integer theLeft, const math_IntegerVector& theRight) { Standard_DimensionError_Raise_if((Length() != theRight.Length()), " "); - for(Standard_Integer I = FirstIndex; I <= LastIndex; I++) + for(Standard_Integer I = Lower(); I <= Upper(); I++) { Array(I) = theLeft * theRight.Array(I); } @@ -333,17 +322,15 @@ void math_IntegerVector::Multiply(const Standard_Integer theLeft, const math_Int math_IntegerVector& math_IntegerVector::Initialized(const math_IntegerVector& theOther) { Standard_DimensionError_Raise_if(Length() != theOther.Length(), " "); - - (theOther.Array).Copy(Array); + memmove (&Array.ChangeFirst(), &theOther.Array.First(), sizeof(Standard_Integer) * Array.Length()); return *this; } void math_IntegerVector::Dump(Standard_OStream& theO) const { theO << "math_IntegerVector of Range = " << Length() << "\n"; - for(Standard_Integer Index = FirstIndex; Index <= LastIndex; Index++) + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { theO << "math_IntegerVector(" << Index << ") = " << Array(Index) << "\n"; } } - diff --git a/src/math/math_IntegerVector.hxx b/src/math/math_IntegerVector.hxx index 3b52ed2520..1fc47dea98 100644 --- a/src/math/math_IntegerVector.hxx +++ b/src/math/math_IntegerVector.hxx @@ -15,7 +15,8 @@ #ifndef _math_IntegerVector_HeaderFile #define _math_IntegerVector_HeaderFile -#include +#include +#include // resolve name collisions with X11 headers #ifdef Opposite @@ -70,7 +71,7 @@ public: //! constructs an IntegerVector in the range [Lower..Upper] //! which share the "c array" theTab. - Standard_EXPORT math_IntegerVector(const Standard_Address theTab, const Standard_Integer theFirst, const Standard_Integer theLast); + Standard_EXPORT math_IntegerVector(const Standard_Integer* theTab, const Standard_Integer theFirst, const Standard_Integer theLast); //! constructs a copy for initialization. //! An exception is raised if the lengths of the IntegerVectors @@ -80,19 +81,19 @@ public: //! returns the length of an IntegerVector inline Standard_Integer Length() const { - return LastIndex - FirstIndex +1; + return Array.Length(); } //! returns the value of the Lower index of an IntegerVector. inline Standard_Integer Lower() const { - return FirstIndex; + return Array.Lower(); } //! returns the value of the Upper index of an IntegerVector. inline Standard_Integer Upper() const { - return LastIndex; + return Array.Upper(); } //! returns the value of the norm of an IntegerVector. @@ -176,14 +177,24 @@ public: //! An exception is raised if the IntegerVectors have not the same length. Standard_EXPORT void Subtract(const math_IntegerVector& theLeft, const math_IntegerVector& theRight); + //! accesses the value of index theNum of an IntegerVector. + const Standard_Integer& Value (const Standard_Integer theNum) const + { + return Array(theNum); + } + //! accesses (in read or write mode) the value of index theNum of an IntegerVector. - inline Standard_Integer& Value(const Standard_Integer theNum) const + inline Standard_Integer& Value (const Standard_Integer theNum) { - Standard_RangeError_Raise_if(theNum < FirstIndex || theNum > LastIndex, " "); return Array(theNum); } - Standard_Integer& operator()(const Standard_Integer theNum) const + const Standard_Integer& operator()(const Standard_Integer theNum) const + { + return Value(theNum); + } + + Standard_Integer& operator()(const Standard_Integer theNum) { return Value(theNum); } @@ -252,9 +263,9 @@ protected: private: - Standard_Integer FirstIndex; - Standard_Integer LastIndex; - math_SingleTab Array; + NCollection_LocalArray myLocArray; + NCollection_Array1 Array; + }; #endif diff --git a/src/math/math_Matrix.cxx b/src/math/math_Matrix.cxx index fc9383afa0..5fa865e747 100644 --- a/src/math/math_Matrix.cxx +++ b/src/math/math_Matrix.cxx @@ -297,7 +297,7 @@ void math_Matrix::SetRow (const Standard_Integer Row, Standard_DimensionError_Raise_if (ColNumber() != V.Length(), "math_Matrix::SetRow() - input vector has wrong dimensions"); - Standard_Integer I = V.LowerIndex; + Standard_Integer I = V.Lower(); for(Standard_Integer Index = LowerColIndex; Index <= UpperColIndex; Index++) { Array(Row, Index) = V.Array(I); I++; @@ -314,7 +314,7 @@ void math_Matrix::SetCol (const Standard_Integer Col, Standard_DimensionError_Raise_if (RowNumber() != V.Length(), "math_Matrix::SetCol() - input vector has wrong dimensions"); - Standard_Integer I = V.LowerIndex; + Standard_Integer I = V.Lower(); for(Standard_Integer Index = LowerRowIndex; Index <= UpperRowIndex; Index++) { Array(Index, Col) = V.Array(I); I++; @@ -635,7 +635,7 @@ math_Vector math_Matrix::Multiplied(const math_Vector& Right)const for(Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++) { Result.Array(I) = 0.0; - Standard_Integer II = Right.LowerIndex; + Standard_Integer II = Right.Lower(); for(Standard_Integer J = LowerColIndex; J <= UpperColIndex; J++) { Result.Array(I) = Result.Array(I) + Array(I, J) * Right.Array(II); II++; diff --git a/src/math/math_SVD.cxx b/src/math/math_SVD.cxx index b0a4ed20b7..88c8d7d899 100644 --- a/src/math/math_SVD.cxx +++ b/src/math/math_SVD.cxx @@ -40,7 +40,7 @@ math_SVD::math_SVD (const math_Matrix& A) : void math_SVD::Solve (const math_Vector& B, math_Vector& X, - const Standard_Real Eps) const + const Standard_Real Eps) { StdFail_NotDone_Raise_if(!Done, " "); Standard_DimensionError_Raise_if((RowA != B.Length()) || @@ -58,7 +58,7 @@ void math_SVD::Solve (const math_Vector& B, } void math_SVD::PseudoInverse (math_Matrix& Result, - const Standard_Real Eps) const + const Standard_Real Eps) { Standard_Integer i, j ; diff --git a/src/math/math_SVD.hxx b/src/math/math_SVD.hxx index 5eb809e933..6a729d2a77 100644 --- a/src/math/math_SVD.hxx +++ b/src/math/math_SVD.hxx @@ -61,14 +61,14 @@ public: //! equal to the rowrange of A. //! Exception DimensionError is raised if the range of X is not //! equal to the colrange of A. - Standard_EXPORT void Solve (const math_Vector& B, math_Vector& X, const Standard_Real Eps = 1.0e-6) const; + Standard_EXPORT void Solve (const math_Vector& B, math_Vector& X, const Standard_Real Eps = 1.0e-6); //! Computes the inverse Inv of matrix A such as A * Inverse = Identity. //! Exceptions //! StdFail_NotDone if the algorithm fails (and IsDone returns false). //! Standard_DimensionError if the ranges of Inv are //! compatible with the ranges of A. - Standard_EXPORT void PseudoInverse (math_Matrix& Inv, const Standard_Real Eps = 1.0e-6) const; + Standard_EXPORT void PseudoInverse (math_Matrix& Inv, const Standard_Real Eps = 1.0e-6); //! Prints information on the current state of the object. //! Is used to redefine the operator <<. diff --git a/src/math/math_SingleTab.hxx b/src/math/math_SingleTab.hxx deleted file mode 100644 index 596646c8aa..0000000000 --- a/src/math/math_SingleTab.hxx +++ /dev/null @@ -1,116 +0,0 @@ -// Copyright (c) 1997-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. - -#ifndef _math_SingleTab_HeaderFile -#define _math_SingleTab_HeaderFile - -#include -#include - -#include - -static const Standard_Integer aLengthOfBuf = 512; - -template class math_SingleTab -{ -public: - - DEFINE_STANDARD_ALLOC - - math_SingleTab(const Standard_Integer LowerIndex, const Standard_Integer UpperIndex) : - Addr(Buf), - isAllocated(UpperIndex - LowerIndex + 1 > aLengthOfBuf), - First(LowerIndex), Last(UpperIndex) - { - T* TheAddr = !isAllocated? Buf : - (T*) Standard::Allocate((Last-First+1) * sizeof(T)); - Addr = (Standard_Address) (TheAddr - First); - } - - math_SingleTab(const Standard_Address Tab, const Standard_Integer LowerIndex, const Standard_Integer UpperIndex) : - Addr((void*)((const T*)Tab - LowerIndex)), - isAllocated(Standard_False), - First(LowerIndex), Last(UpperIndex) - { - } - - void Init(const T InitValue) - { - for(Standard_Integer i = First; i<= Last; i++) - { - ((T*)Addr)[i] = InitValue; - } - } - - math_SingleTab(const math_SingleTab& Other) : - isAllocated(Other.Last - Other.First + 1 > aLengthOfBuf), - First(Other.First), - Last(Other.Last) - { - T* TheAddr = !isAllocated? Buf : (T*) Standard::Allocate((Last-First+1) * sizeof(T)); - Addr = (Standard_Address) (TheAddr - First); - T* TheOtherAddr = (T*) Other.Addr; - memmove((void*) TheAddr, (const void*) (TheOtherAddr + First), (size_t)(Last - First + 1) * sizeof(T)); - } - - inline void Copy(math_SingleTab& Other) const - { - memmove((void*) (((T*)Other.Addr) + Other.First), - (const void*) (((T*)Addr) + First), - (size_t)(Last - First + 1) * sizeof(T)); - } - - void SetLower(const Standard_Integer LowerIndex) - { - T* TheAddr = (T*) Addr; - Addr = (Standard_Address) (TheAddr + First - LowerIndex); - Last = Last - First + LowerIndex; - First = LowerIndex; - } - - inline T& Value(const Standard_Integer Index) const - { - return ((T*)Addr)[Index]; - } - - T& operator()(const Standard_Integer Index) const - { - return Value(Index); - } - - void Free() - { - if(isAllocated) - { - Standard_Address it = (Standard_Address)&((T*)Addr)[First]; - Standard::Free(it); - Addr = 0; - } - } - - ~math_SingleTab() - { - Free(); - } - -private: - - Standard_Address Addr; - T Buf[aLengthOfBuf]; - Standard_Boolean isAllocated; - Standard_Integer First; - Standard_Integer Last; -}; - -#endif diff --git a/src/math/math_Vector.cxx b/src/math/math_Vector.cxx index f942a3e4a4..b57bbe39d5 100644 --- a/src/math/math_Vector.cxx +++ b/src/math/math_Vector.cxx @@ -22,48 +22,41 @@ #include #include -math_Vector::math_Vector(const Standard_Integer theLower, const Standard_Integer theUpper) : - LowerIndex(theLower), - UpperIndex(theUpper), - Array(theLower,theUpper) +math_Vector::math_Vector(const Standard_Integer theLower, const Standard_Integer theUpper) +: myLocArray (theUpper - theLower + 1), + Array (myLocArray[0], theLower, theUpper) { - Standard_RangeError_Raise_if (theLower > theUpper, "math_Vector() - invalid dimensions"); + // } math_Vector::math_Vector(const Standard_Integer theLower, const Standard_Integer theUpper, - const Standard_Real theInitialValue): - LowerIndex(theLower), - UpperIndex(theUpper), - Array(theLower,theUpper) + const Standard_Real theInitialValue) +: myLocArray (theUpper - theLower + 1), + Array (myLocArray[0], theLower, theUpper) { - Standard_RangeError_Raise_if (theLower > theUpper, "math_Vector() - invalid dimensions"); Array.Init(theInitialValue); } -math_Vector::math_Vector(const Standard_Address theTab, +math_Vector::math_Vector(const Standard_Real* theTab, const Standard_Integer theLower, - const Standard_Integer theUpper) : - LowerIndex(theLower), - UpperIndex(theUpper), - Array(theTab, theLower,theUpper) + const Standard_Integer theUpper) +: Array (*theTab, theLower, theUpper) { Standard_RangeError_Raise_if ((theLower > theUpper), "math_Vector() - invalid dimensions"); } -math_Vector::math_Vector(const gp_XY& theOther): - LowerIndex(1), - UpperIndex(2), - Array(1,2) +math_Vector::math_Vector (const gp_XY& theOther) +: myLocArray (2), + Array (myLocArray[0], 1,2) { Array(1) = theOther.X(); Array(2) = theOther.Y(); } -math_Vector::math_Vector(const gp_XYZ& theOther): - LowerIndex(1), - UpperIndex(3), - Array(1, 3) +math_Vector::math_Vector (const gp_XYZ& theOther) +: myLocArray (3), + Array (myLocArray[0], 1, 3) { Array(1) = theOther.X(); Array(2) = theOther.Y(); @@ -75,25 +68,23 @@ void math_Vector::Init(const Standard_Real theInitialValue) Array.Init(theInitialValue); } -math_Vector::math_Vector(const math_Vector& theOther) : - LowerIndex(theOther.LowerIndex), - UpperIndex(theOther.UpperIndex), - Array(theOther.Array) +math_Vector::math_Vector (const math_Vector& theOther) +: myLocArray (theOther.Length()), + Array (myLocArray[0], theOther.Lower(), theOther.Upper()) { + memcpy (&myLocArray[0], &theOther.Array.First(), sizeof(Standard_Real) * theOther.Length()); } void math_Vector::SetLower(const Standard_Integer theLower) { - Array.SetLower(theLower); - UpperIndex = UpperIndex - LowerIndex + theLower; - LowerIndex = theLower; + Array.Resize (theLower, Array.Upper() - Array.Lower() + theLower, Standard_False); } Standard_Real math_Vector::Norm() const { Standard_Real Result = 0; - for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Result = Result + Array(Index) * Array(Index); } @@ -104,7 +95,7 @@ Standard_Real math_Vector::Norm2() const { Standard_Real Result = 0; - for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Result = Result + Array(Index) * Array(Index); } @@ -116,7 +107,7 @@ Standard_Integer math_Vector::Max() const Standard_Integer I=0; Standard_Real X = RealFirst(); - for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { if(Array(Index) > X) { @@ -132,7 +123,7 @@ Standard_Integer math_Vector::Min() const Standard_Integer I=0; Standard_Real X = RealLast(); - for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { if(Array(Index) < X) { @@ -147,7 +138,7 @@ void math_Vector::Set(const Standard_Integer theI1, const Standard_Integer theI2, const math_Vector &theV) { - Standard_RangeError_Raise_if ((theI1 < LowerIndex) || (theI2 > UpperIndex) + Standard_RangeError_Raise_if ((theI1 < Lower()) || (theI2 > Upper()) || (theI1 > theI2) || (theI2 - theI1 + 1 != theV.Length()), "math_Vector::Set() - invalid indices"); @@ -164,7 +155,7 @@ void math_Vector::Normalize() Standard_Real Result = Norm(); Standard_NullValue_Raise_if ((Result <= RealEpsilon()), "math_Vector::Normalize() - vector has zero norm"); - for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Array(Index) = Array(Index) / Result; } @@ -180,14 +171,12 @@ math_Vector math_Vector::Normalized() const void math_Vector::Invert() { - Standard_Integer J; - Standard_Real Temp; - for(Standard_Integer Index = LowerIndex; Index <= (LowerIndex + Length()) >> 1 ; Index++) + for(Standard_Integer Index = Lower(); Index <= (Lower() + Length()) >> 1 ; Index++) { - J = UpperIndex + LowerIndex - Index; - Temp = Array(Index); + Standard_Integer J = Upper() + Lower() - Index; + Standard_Real aTemp = Array(Index); Array(Index) = Array(J); - Array(J) = Temp; + Array(J) = aTemp; } } @@ -200,9 +189,8 @@ math_Vector math_Vector::Inverse() const math_Vector math_Vector::Multiplied(const Standard_Real theRight) const { - math_Vector Result (LowerIndex, UpperIndex); - - for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) + math_Vector Result (Lower(), Upper()); + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Result.Array(Index) = Array(Index) * theRight; } @@ -211,9 +199,8 @@ math_Vector math_Vector::Multiplied(const Standard_Real theRight) const math_Vector math_Vector::TMultiplied(const Standard_Real theRight) const { - math_Vector Result (LowerIndex, UpperIndex); - - for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) + math_Vector Result (Lower(), Upper()); + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Result.Array(Index) = Array(Index) * theRight; } @@ -222,7 +209,7 @@ math_Vector math_Vector::TMultiplied(const Standard_Real theRight) const void math_Vector::Multiply(const Standard_Real theRight) { - for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Array(Index) = Array(Index) * theRight; } @@ -233,7 +220,7 @@ void math_Vector::Divide(const Standard_Real theRight) Standard_DivideByZero_Raise_if (Abs(theRight) <= RealEpsilon(), "math_Vector::Divide() - devisor is zero"); - for(Standard_Integer Index =LowerIndex; Index <=UpperIndex; Index++) + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Array(Index) = Array(Index) / theRight; } @@ -252,8 +239,8 @@ void math_Vector::Add(const math_Vector& theRight) Standard_DimensionError_Raise_if (Length() != theRight.Length(), "math_Vector::Add() - input vector has wrong dimensions"); - Standard_Integer I = theRight.LowerIndex; - for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) + Standard_Integer I = theRight.Lower(); + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Array(Index) = Array(Index) + theRight.Array(I); I++; @@ -265,10 +252,10 @@ math_Vector math_Vector::Added(const math_Vector& theRight) const Standard_DimensionError_Raise_if (Length() != theRight.Length(), "math_Vector::Added() - input vector has wrong dimensions"); - math_Vector Result(LowerIndex, UpperIndex); + math_Vector Result (Lower(), Upper()); - Standard_Integer I = theRight.LowerIndex; - for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) + Standard_Integer I = theRight.Lower(); + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Result.Array(Index) = Array(Index) + theRight.Array(I); I++; @@ -281,8 +268,8 @@ void math_Vector::Subtract(const math_Vector& theRight) Standard_DimensionError_Raise_if (Length() != theRight.Length(), "math_Vector::Subtract() - input vector has wrong dimensions"); - Standard_Integer I = theRight.LowerIndex; - for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) + Standard_Integer I = theRight.Lower(); + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Array(Index) = Array(Index) - theRight.Array(I); I++; @@ -294,10 +281,10 @@ math_Vector math_Vector::Subtracted (const math_Vector& theRight) const Standard_DimensionError_Raise_if (Length() != theRight.Length(), "math_Vector::Subtracted() - input vector has wrong dimensions"); - math_Vector Result(LowerIndex, UpperIndex); + math_Vector Result(Lower(), Upper()); - Standard_Integer I = theRight.LowerIndex; - for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) + Standard_Integer I = theRight.Lower(); + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Result.Array(Index) = Array(Index) - theRight.Array(I); I++; @@ -307,7 +294,7 @@ math_Vector math_Vector::Subtracted (const math_Vector& theRight) const math_Vector math_Vector::Slice(const Standard_Integer theI1, const Standard_Integer theI2) const { - Standard_RangeError_Raise_if ((theI1 < LowerIndex) || (theI1 > UpperIndex) || (theI2 < LowerIndex) || (theI2 > UpperIndex), + Standard_RangeError_Raise_if ((theI1 < Lower()) || (theI1 > Upper()) || (theI2 < Lower()) || (theI2 > Upper()), "math_Vector::Slice() - invalid indices"); if(theI2 >= theI1) @@ -335,9 +322,9 @@ void math_Vector::Add (const math_Vector& theLeft, const math_Vector& theRight) Standard_DimensionError_Raise_if ((Length() != theRight.Length()) || (theRight.Length() != theLeft.Length()), "math_Vector::Add() - input vectors have wrong dimensions"); - Standard_Integer I = theLeft.LowerIndex; - Standard_Integer J = theRight.LowerIndex; - for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) + Standard_Integer I = theLeft.Lower(); + Standard_Integer J = theRight.Lower(); + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Array(Index) = theLeft.Array(I) + theRight.Array(J); I++; @@ -350,9 +337,9 @@ void math_Vector::Subtract (const math_Vector& theLeft, const math_Vector& theRi Standard_DimensionError_Raise_if ((Length() != theRight.Length()) || (theRight.Length() != theLeft.Length()), "math_Vector::Subtract() - input vectors have wrong dimensions"); - Standard_Integer I = theLeft.LowerIndex; - Standard_Integer J = theRight.LowerIndex; - for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) + Standard_Integer I = theLeft.Lower(); + Standard_Integer J = theRight.Lower(); + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Array(Index) = theLeft.Array(I) - theRight.Array(J); I++; @@ -366,11 +353,11 @@ void math_Vector::Multiply(const math_Matrix& theLeft, const math_Vector& theRig || (theLeft.ColNumber() != theRight.Length()), "math_Vector::Multiply() - input matrix and/or vector have wrong dimensions"); - Standard_Integer Index = LowerIndex; + Standard_Integer Index = Lower(); for(Standard_Integer I = theLeft.LowerRowIndex; I <= theLeft.UpperRowIndex; I++) { Array(Index) = 0.0; - Standard_Integer K = theRight.LowerIndex; + Standard_Integer K = theRight.Lower(); for(Standard_Integer J = theLeft.LowerColIndex; J <= theLeft.UpperColIndex; J++) { Array(Index) = Array(Index) + theLeft.Array(I, J) * theRight.Array(K); @@ -386,11 +373,11 @@ void math_Vector::Multiply(const math_Vector& theLeft, const math_Matrix& theRig || (theLeft.Length() != theRight.RowNumber()), "math_Vector::Multiply() - input matrix and/or vector have wrong dimensions"); - Standard_Integer Index = LowerIndex; + Standard_Integer Index = Lower(); for(Standard_Integer J = theRight.LowerColIndex; J <= theRight.UpperColIndex; J++) { Array(Index) = 0.0; - Standard_Integer K = theLeft.LowerIndex; + Standard_Integer K = theLeft.Lower(); for(Standard_Integer I = theRight.LowerRowIndex; I <= theRight.UpperRowIndex; I++) { Array(Index) = Array(Index) + theLeft.Array(K) * theRight.Array(I, J); @@ -406,11 +393,11 @@ void math_Vector::TMultiply(const math_Matrix& theTLeft, const math_Vector& the || (theTLeft.RowNumber() != theRight.Length()), "math_Vector::TMultiply() - input matrix and/or vector have wrong dimensions"); - Standard_Integer Index = LowerIndex; + Standard_Integer Index = Lower(); for(Standard_Integer I = theTLeft.LowerColIndex; I <= theTLeft.UpperColIndex; I++) { Array(Index) = 0.0; - Standard_Integer K = theRight.LowerIndex; + Standard_Integer K = theRight.Lower(); for(Standard_Integer J = theTLeft.LowerRowIndex; J <= theTLeft.UpperRowIndex; J++) { Array(Index) = Array(Index) + theTLeft.Array(J, I) * theRight.Array(K); @@ -426,11 +413,11 @@ void math_Vector::TMultiply(const math_Vector& theLeft, const math_Matrix& theT || (theLeft.Length() != theTRight.ColNumber()), "math_Vector::TMultiply() - input matrix and/or vector have wrong dimensions"); - Standard_Integer Index = LowerIndex; + Standard_Integer Index = Lower(); for(Standard_Integer J = theTRight.LowerRowIndex; J <= theTRight.UpperRowIndex; J++) { Array(Index) = 0.0; - Standard_Integer K = theLeft.LowerIndex; + Standard_Integer K = theLeft.Lower(); for(Standard_Integer I = theTRight.LowerColIndex; I <= theTRight.UpperColIndex; I++) { @@ -448,8 +435,8 @@ Standard_Real math_Vector::Multiplied(const math_Vector& theRight) const Standard_DimensionError_Raise_if (Length() != theRight.Length(), "math_Vector::Multiplied() - input vector has wrong dimensions"); - Standard_Integer I = theRight.LowerIndex; - for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) + Standard_Integer I = theRight.Lower(); + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Result = Result + Array(Index) * theRight.Array(I); I++; @@ -459,9 +446,8 @@ Standard_Real math_Vector::Multiplied(const math_Vector& theRight) const math_Vector math_Vector::Opposite() { - math_Vector Result(LowerIndex, UpperIndex); - - for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) + math_Vector Result(Lower(), Upper()); + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { Result.Array(Index) = - Array(Index); } @@ -476,13 +462,13 @@ math_Vector math_Vector::Multiplied(const math_Matrix& theRight)const math_Vector Result(theRight.LowerColIndex, theRight.UpperColIndex); for(Standard_Integer J2 = theRight.LowerColIndex; J2 <= theRight.UpperColIndex; J2++) { - Array(J2) = 0.0; - Standard_Integer theI2 = theRight.LowerRowIndex; - for(Standard_Integer I = LowerIndex; I <= UpperIndex; I++) - { - Result.Array(J2) = Result.Array(J2) + Array(I) * theRight.Array(theI2, J2); - theI2++; - } + Result.Array(J2) = 0.0; + Standard_Integer theI2 = theRight.LowerRowIndex; + for(Standard_Integer I = Lower(); I <= Upper(); I++) + { + Result.Array(J2) = Result.Array(J2) + Array(I) * theRight.Array(theI2, J2); + theI2++; + } } return Result; } @@ -491,7 +477,7 @@ void math_Vector::Multiply(const Standard_Real theLeft, const math_Vector& theRi { Standard_DimensionError_Raise_if ((Length() != theRight.Length()), "math_Vector::Multiply() - input vector has wrong dimensions"); - for(Standard_Integer I = LowerIndex; I <= UpperIndex; I++) + for(Standard_Integer I = Lower(); I <= Upper(); I++) { Array(I) = theLeft * theRight.Array(I); } @@ -501,15 +487,14 @@ math_Vector& math_Vector::Initialized(const math_Vector& theOther) { Standard_DimensionError_Raise_if (Length() != theOther.Length(), "math_Vector::Initialized() - input vector has wrong dimensions"); - - (theOther.Array).Copy(Array); + memmove (&Array.ChangeFirst(), &theOther.Array.First(), sizeof(Standard_Real) * Array.Length()); return *this; } void math_Vector::Dump(Standard_OStream& theO) const { theO << "math_Vector of Length = " << Length() << "\n"; - for(Standard_Integer Index = LowerIndex; Index <= UpperIndex; Index++) + for(Standard_Integer Index = Lower(); Index <= Upper(); Index++) { theO << "math_Vector(" << Index << ") = " << Array(Index) << "\n"; } diff --git a/src/math/math_Vector.hxx b/src/math/math_Vector.hxx index e5625f7def..0c49d5166c 100644 --- a/src/math/math_Vector.hxx +++ b/src/math/math_Vector.hxx @@ -15,7 +15,8 @@ #ifndef _math_Vector_HeaderFile #define _math_Vector_HeaderFile -#include +#include +#include #include #include @@ -70,7 +71,7 @@ public: //! Constructs a vector in the range [theLower..theUpper] //! with the "c array" theTab. - Standard_EXPORT math_Vector(const Standard_Address theTab, const Standard_Integer theLower, const Standard_Integer theUpper); + Standard_EXPORT math_Vector(const Standard_Real* theTab, const Standard_Integer theLower, const Standard_Integer theUpper); //! Constructor for converting gp_XY to math_Vector Standard_EXPORT math_Vector(const gp_XY& Other); @@ -88,19 +89,19 @@ public: //! Returns the length of a vector inline Standard_Integer Length() const { - return UpperIndex - LowerIndex +1; + return Array.Length(); } //! Returns the value of the theLower index of a vector. inline Standard_Integer Lower() const { - return LowerIndex; + return Array.Lower(); } //! Returns the value of the theUpper index of a vector. inline Standard_Integer Upper() const { - return UpperIndex; + return Array.Upper(); } //! Returns the value or the square of the norm of this vector. @@ -241,14 +242,24 @@ public: //! Subtract whenever possible. Standard_EXPORT void Subtract(const math_Vector& theLeft,const math_Vector& theRight); + //! accesses the value of index "theNum" of a vector. + const Standard_Real& Value (const Standard_Integer theNum) const + { + return Array(theNum); + } + //! accesses (in read or write mode) the value of index "theNum" of a vector. - inline Standard_Real& Value(const Standard_Integer theNum) const + inline Standard_Real& Value (const Standard_Integer theNum) { - Standard_RangeError_Raise_if(theNum < LowerIndex || theNum > UpperIndex, " "); return Array(theNum); } - Standard_Real& operator()(const Standard_Integer theNum) const + const Standard_Real& operator()(const Standard_Integer theNum) const + { + return Value(theNum); + } + + Standard_Real& operator()(const Standard_Integer theNum) { return Value(theNum); } @@ -327,9 +338,9 @@ protected: private: - Standard_Integer LowerIndex; - Standard_Integer UpperIndex; - math_SingleTab Array; + NCollection_LocalArray myLocArray; + NCollection_Array1 Array; + }; #endif