From 2b2be3fb825f5823d4fa446d7e88facab937beab Mon Sep 17 00:00:00 2001 From: tiv Date: Thu, 28 Mar 2019 12:42:41 +0300 Subject: [PATCH] 0030550: Coding - Integer overflow in Standard_CString HashCodes 0030551: Foundation Classes - Integer overflow in NCollection_CellFilter HashCode MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed integers are not used in hash code functions now to prevent undefined behavior on left shift operations with signed integers. A possibility of negative values of hash codes is eliminated. INT_MAX → IntegerLast() in hash code functions. All found hash code functions behaves uniformly now: they return a value in the range [1, theUpperBound]. Relevant comments are added to such functions. --- src/BOPDS/BOPDS_Pair.hxx | 10 +- src/BOPDS/BOPDS_PairMapHasher.hxx | 13 +- src/BOPDS/BOPDS_PaveMapHasher.hxx | 11 +- src/BOPDS/BOPDS_PaveMapHasher.lxx | 11 +- src/BOPTools/BOPTools_Parallel.hxx | 9 +- src/BOPTools/BOPTools_Set.cxx | 11 +- src/BOPTools/BOPTools_Set.hxx | 7 +- src/BOPTools/BOPTools_SetMapHasher.hxx | 13 +- src/BOPTools/BOPTools_SetMapHasher.lxx | 12 +- src/BRepMesh/BRepMesh_Edge.hxx | 9 +- src/BRepMesh/BRepMesh_OrientedEdge.hxx | 19 +-- src/BRepMesh/BRepMesh_Triangle.hxx | 23 +-- src/BRepMesh/BRepMesh_Vertex.hxx | 20 +-- src/Font/Font_FontMgr.hxx | 14 +- src/Font/Font_SystemFont.hxx | 13 +- src/IMeshData/IMeshData_Types.hxx | 8 +- src/IntPolyh/IntPolyh_Couple.hxx | 11 +- src/IntPolyh/IntPolyh_CoupleMapHasher.hxx | 13 +- .../IntTools_CurveRangeSampleMapHasher.hxx | 11 +- .../IntTools_CurveRangeSampleMapHasher.lxx | 13 +- .../IntTools_SurfaceRangeSampleMapHasher.hxx | 11 +- .../IntTools_SurfaceRangeSampleMapHasher.lxx | 15 +- .../Interface_MapAsciiStringHasher.cxx | 10 +- .../Interface_MapAsciiStringHasher.hxx | 10 +- src/MAT2d/MAT2d_MapBiIntHasher.hxx | 13 +- src/MAT2d/MAT2d_MapBiIntHasher.lxx | 9 +- src/MeshVS/MeshVS_SymmetricPairHasher.hxx | 8 +- src/MeshVS/MeshVS_TwoColors.cxx | 11 +- src/MeshVS/MeshVS_TwoColors.hxx | 7 +- src/MeshVS/MeshVS_TwoNodes.hxx | 18 ++- src/MoniTool/MoniTool_ElemHasher.cxx | 10 +- src/MoniTool/MoniTool_ElemHasher.hxx | 14 +- src/MoniTool/MoniTool_MTHasher.hxx | 12 +- src/MoniTool/MoniTool_MTHasher.lxx | 11 +- src/NCollection/NCollection_AccAllocator.hxx | 10 +- src/NCollection/NCollection_CellFilter.hxx | 34 +++-- src/NCollection/NCollection_DefaultHasher.hxx | 24 ++-- src/OSD/OSD_Thread.cxx | 9 +- src/Poly/Poly_MakeLoops.hxx | 12 +- src/QANCollection/QANCollection_Test.cxx | 9 +- src/Quantity/Quantity_ColorHasher.hxx | 16 ++- src/Quantity/Quantity_ColorRGBAHasher.hxx | 9 +- src/RWStl/RWStl_Reader.cxx | 8 +- .../STEPConstruct_PointHasher.hxx | 11 +- .../STEPConstruct_PointHasher.lxx | 24 ++-- src/Standard/FILES | 1 + src/Standard/Standard_Address.hxx | 27 ++-- src/Standard/Standard_CString.cxx | 26 ++-- src/Standard/Standard_CString.hxx | 34 +++-- src/Standard/Standard_ErrorHandler.cxx | 2 +- src/Standard/Standard_ExtString.cxx | 17 ++- src/Standard/Standard_ExtString.hxx | 9 +- src/Standard/Standard_GUID.cxx | 8 +- src/Standard/Standard_GUID.hxx | 9 +- src/Standard/Standard_Handle.hxx | 66 ++------- src/Standard/Standard_Integer.hxx | 132 +++++++++++------- src/Standard/Standard_Real.cxx | 32 +++-- src/Standard/Standard_Real.hxx | 7 +- src/Standard/Standard_ShortReal.cxx | 27 ++-- src/Standard/Standard_ShortReal.hxx | 7 +- src/Standard/Standard_Size.hxx | 16 ++- src/Standard/Standard_Std.hxx | 106 ++++++++++++++ src/Standard/Standard_ThreadId.hxx | 22 +-- src/Standard/Standard_Transient.hxx | 11 +- src/Standard/Standard_Type.cxx | 8 +- .../StepToTopoDS_CartesianPointHasher.cxx | 11 +- .../StepToTopoDS_CartesianPointHasher.hxx | 11 +- .../StepToTopoDS_PointPairHasher.cxx | 11 +- .../StepToTopoDS_PointPairHasher.hxx | 8 +- src/TColStd/TColStd_PackedMapOfInteger.cxx | 10 +- src/TCollection/TCollection_AsciiString.hxx | 11 +- src/TCollection/TCollection_AsciiString.lxx | 28 ++-- .../TCollection_ExtendedString.hxx | 20 ++- src/TDF/TDF_LabelMapHasher.hxx | 10 +- src/TObj/TObj_Common.hxx | 10 +- src/TopLoc/TopLoc_Location.cxx | 24 ++-- src/TopLoc/TopLoc_Location.hxx | 21 +-- .../TopTools_OrientedShapeMapHasher.hxx | 11 +- .../TopTools_OrientedShapeMapHasher.lxx | 10 +- src/TopTools/TopTools_ShapeMapHasher.hxx | 11 +- src/TopTools/TopTools_ShapeMapHasher.lxx | 10 +- src/TopoDS/TopoDS_Shape.cxx | 17 ++- src/TopoDS/TopoDS_Shape.hxx | 19 ++- src/Transfer/Transfer_FindHasher.cxx | 11 +- src/Transfer/Transfer_FindHasher.hxx | 13 +- src/VrmlData/VrmlData_Node.cxx | 11 +- src/VrmlData/VrmlData_Node.hxx | 8 +- src/XCAFDoc/XCAFDoc_AssemblyItemId.hxx | 11 +- src/XCAFPrs/XCAFPrs_Style.hxx | 16 ++- 89 files changed, 882 insertions(+), 584 deletions(-) create mode 100644 src/Standard/Standard_Std.hxx diff --git a/src/BOPDS/BOPDS_Pair.hxx b/src/BOPDS/BOPDS_Pair.hxx index ca089412e5..747ecc834f 100644 --- a/src/BOPDS/BOPDS_Pair.hxx +++ b/src/BOPDS/BOPDS_Pair.hxx @@ -62,11 +62,13 @@ class BOPDS_Pair { return (myIndex1 == theOther.myIndex1 && myIndex2 == theOther.myIndex2) || (myIndex1 == theOther.myIndex2 && myIndex2 == theOther.myIndex1); } - // - //! Returns hash code - Standard_Integer HashCode (const Standard_Integer theUpper) const + + //! Computes a hash code for this pair, in the range [1, theUpperBound] + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + Standard_Integer HashCode (const Standard_Integer theUpperBound) const { - return ::HashCode(myIndex1 + myIndex2, theUpper); + return ::HashCode(myIndex1 + myIndex2, theUpperBound); } protected: diff --git a/src/BOPDS/BOPDS_PairMapHasher.hxx b/src/BOPDS/BOPDS_PairMapHasher.hxx index d0360f74b5..73b78bfb33 100644 --- a/src/BOPDS/BOPDS_PairMapHasher.hxx +++ b/src/BOPDS/BOPDS_PairMapHasher.hxx @@ -30,13 +30,16 @@ class BOPDS_PairMapHasher public: DEFINE_STANDARD_ALLOC - - static Standard_Integer HashCode(const BOPDS_Pair& thePair, - const Standard_Integer Upper) + + //! Computes a hash code for the given pair, in the range [1, theUpperBound] + //! @param thePair the pair which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (const BOPDS_Pair& thePair, const Standard_Integer theUpperBound) { - return thePair.HashCode(Upper); + return thePair.HashCode (theUpperBound); } - + static Standard_Boolean IsEqual(const BOPDS_Pair& thePair1, const BOPDS_Pair& thePair2) { diff --git a/src/BOPDS/BOPDS_PaveMapHasher.hxx b/src/BOPDS/BOPDS_PaveMapHasher.hxx index 0e7b9ef425..95ea61db20 100644 --- a/src/BOPDS/BOPDS_PaveMapHasher.hxx +++ b/src/BOPDS/BOPDS_PaveMapHasher.hxx @@ -31,10 +31,13 @@ public: DEFINE_STANDARD_ALLOC - - static Standard_Integer HashCode (const BOPDS_Pave& aPave, const Standard_Integer Upper); - - static Standard_Boolean IsEqual (const BOPDS_Pave& aPave1, const BOPDS_Pave& aPave2); + //! Computes a hash code for the given pave, in the range [1, theUpperBound] + //! @param thePave the pave which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (const BOPDS_Pave& thePave, Standard_Integer theUpperBound); + + static Standard_Boolean IsEqual (const BOPDS_Pave& aPave1, const BOPDS_Pave& aPave2); diff --git a/src/BOPDS/BOPDS_PaveMapHasher.lxx b/src/BOPDS/BOPDS_PaveMapHasher.lxx index 45dcf11e74..a6fb0b44a8 100644 --- a/src/BOPDS/BOPDS_PaveMapHasher.lxx +++ b/src/BOPDS/BOPDS_PaveMapHasher.lxx @@ -15,15 +15,14 @@ #include //======================================================================= -//function : HashCode -//purpose : +// function : HashCode +// purpose : //======================================================================= -inline - Standard_Integer BOPDS_PaveMapHasher::HashCode(const BOPDS_Pave& aPave, - const Standard_Integer Upper) +inline Standard_Integer BOPDS_PaveMapHasher::HashCode (const BOPDS_Pave& thePave, const Standard_Integer theUpperBound) { - return ::HashCode(aPave.Index(), Upper); + return ::HashCode (thePave.Index(), theUpperBound); } + //======================================================================= //function :IsEqual //purpose : diff --git a/src/BOPTools/BOPTools_Parallel.hxx b/src/BOPTools/BOPTools_Parallel.hxx index 58e121bdef..c35deaaef5 100644 --- a/src/BOPTools/BOPTools_Parallel.hxx +++ b/src/BOPTools/BOPTools_Parallel.hxx @@ -55,10 +55,13 @@ class BOPTools_Parallel //! Auxiliary thread ID hasher. struct Hasher { - static Standard_Integer HashCode(const Standard_ThreadId theKey, - const Standard_Integer Upper) + //! Computes a hash code for the given thread identifier, in the range [1, theUpperBound] + //! @param theThreadId the thread identifier which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (const Standard_ThreadId theThreadId, const Standard_Integer theUpperBound) { - return ::HashCode((Standard_Size)theKey, Upper); + return ::HashCode (theThreadId, theUpperBound); } static Standard_Boolean IsEqual(const Standard_ThreadId theKey1, diff --git a/src/BOPTools/BOPTools_Set.cxx b/src/BOPTools/BOPTools_Set.cxx index 680545b09d..aaf2beddea 100644 --- a/src/BOPTools/BOPTools_Set.cxx +++ b/src/BOPTools/BOPTools_Set.cxx @@ -107,15 +107,16 @@ const TopoDS_Shape& BOPTools_Set::Shape()const { return myShape; } + //======================================================================= -//function : HashCode -//purpose : +// function : HashCode +// purpose : //======================================================================= -Standard_Integer BOPTools_Set::HashCode - (const Standard_Integer theUpper)const +Standard_Integer BOPTools_Set::HashCode (const Standard_Integer theUpperBound) const { - return ::HashCode(mySum, theUpper); + return ::HashCode (mySum, theUpperBound); } + //======================================================================= //function : IsEqual //purpose : diff --git a/src/BOPTools/BOPTools_Set.hxx b/src/BOPTools/BOPTools_Set.hxx index 2d1a0dfcf6..2104bbcb29 100644 --- a/src/BOPTools/BOPTools_Set.hxx +++ b/src/BOPTools/BOPTools_Set.hxx @@ -54,10 +54,11 @@ BOPTools_Set& operator = (const BOPTools_Set& Other) Standard_EXPORT Standard_Integer NbShapes() const; Standard_EXPORT Standard_Boolean IsEqual (const BOPTools_Set& aOther) const; - - Standard_EXPORT Standard_Integer HashCode (const Standard_Integer Upper) const; - + //! Computes a hash code for this set, in the range [1, theUpperBound] + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + Standard_EXPORT Standard_Integer HashCode (Standard_Integer theUpperBound) const; protected: diff --git a/src/BOPTools/BOPTools_SetMapHasher.hxx b/src/BOPTools/BOPTools_SetMapHasher.hxx index e9b1ba8f9c..ae8cfdaf52 100644 --- a/src/BOPTools/BOPTools_SetMapHasher.hxx +++ b/src/BOPTools/BOPTools_SetMapHasher.hxx @@ -30,11 +30,14 @@ class BOPTools_SetMapHasher public: DEFINE_STANDARD_ALLOC - - - static Standard_Integer HashCode (const BOPTools_Set& aSet, const Standard_Integer Upper); - - static Standard_Boolean IsEqual (const BOPTools_Set& aSet1, const BOPTools_Set& aSet2); + + //! Computes a hash code for the given set, in the range [1, theUpperBound] + //! @param theSet the set which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (const BOPTools_Set& theSet, Standard_Integer theUpperBound); + + static Standard_Boolean IsEqual (const BOPTools_Set& aSet1, const BOPTools_Set& aSet2); diff --git a/src/BOPTools/BOPTools_SetMapHasher.lxx b/src/BOPTools/BOPTools_SetMapHasher.lxx index 93a79f4260..3cdd0c9b6f 100644 --- a/src/BOPTools/BOPTools_SetMapHasher.lxx +++ b/src/BOPTools/BOPTools_SetMapHasher.lxx @@ -13,15 +13,17 @@ // commercial license or contractual agreement. //#include + //======================================================================= -//function : HashCode -//purpose : +// function : HashCode +// purpose : //======================================================================= -inline Standard_Integer BOPTools_SetMapHasher::HashCode(const BOPTools_Set& theSS, - const Standard_Integer Upper) +inline Standard_Integer BOPTools_SetMapHasher::HashCode (const BOPTools_Set& theSet, + const Standard_Integer theUpperBound) { - return theSS.HashCode(Upper); + return theSet.HashCode (theUpperBound); } + //======================================================================= //function :IsEqual //purpose : diff --git a/src/BRepMesh/BRepMesh_Edge.hxx b/src/BRepMesh/BRepMesh_Edge.hxx index 70cc60365f..3ef8151311 100644 --- a/src/BRepMesh/BRepMesh_Edge.hxx +++ b/src/BRepMesh/BRepMesh_Edge.hxx @@ -86,10 +86,13 @@ private: BRepMesh_DegreeOfFreedom myMovability; }; -inline Standard_Integer HashCode(const BRepMesh_Edge& theEdge, - const Standard_Integer theUpper) +//! Computes a hash code for the given edge, in the range [1, theUpperBound] +//! @param theEdge the edge which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +inline Standard_Integer HashCode (const BRepMesh_Edge& theEdge, const Standard_Integer theUpperBound) { - return theEdge.HashCode(theUpper); + return theEdge.HashCode (theUpperBound); } #endif diff --git a/src/BRepMesh/BRepMesh_OrientedEdge.hxx b/src/BRepMesh/BRepMesh_OrientedEdge.hxx index 94b8fc3731..976c1ba4a2 100644 --- a/src/BRepMesh/BRepMesh_OrientedEdge.hxx +++ b/src/BRepMesh/BRepMesh_OrientedEdge.hxx @@ -54,12 +54,12 @@ public: return myLastNode; } - //! Returns hash code for this edge. - //! @param theUpper upper index in the container. - //! @return hash code. - inline Standard_Integer HashCode(const Standard_Integer theUpper) const + //! Computes a hash code for this oriented edge, in the range [1, theUpperBound] + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + inline Standard_Integer HashCode (const Standard_Integer theUpperBound) const { - return ::HashCode(myFirstNode + myLastNode, theUpper); + return ::HashCode (myFirstNode + myLastNode, theUpperBound); } //! Checks this and other edge for equality. @@ -82,10 +82,13 @@ private: Standard_Integer myLastNode; }; -inline Standard_Integer HashCode(const BRepMesh_OrientedEdge& theEdge, - const Standard_Integer theUpper) +//! Computes a hash code for the given oriented edge, in the range [1, theUpperBound] +//! @param theOrientedEdge the oriented edge which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +inline Standard_Integer HashCode (const BRepMesh_OrientedEdge& theOrientedEdge, const Standard_Integer theUpperBound) { - return theEdge.HashCode(theUpper); + return theOrientedEdge.HashCode (theUpperBound); } #endif diff --git a/src/BRepMesh/BRepMesh_Triangle.hxx b/src/BRepMesh/BRepMesh_Triangle.hxx index 3c70c1a157..6cb531a56b 100644 --- a/src/BRepMesh/BRepMesh_Triangle.hxx +++ b/src/BRepMesh/BRepMesh_Triangle.hxx @@ -91,15 +91,15 @@ public: { myMovability = theMovability; } - - //! Returns hash code for this triangle. - //! @param theUpper upper index in the container. - //! @return hash code. - inline Standard_Integer HashCode(const Standard_Integer theUpper) const + + //! Computes a hash code for this triangle, in the range [1, theUpperBound] + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + inline Standard_Integer HashCode (const Standard_Integer theUpperBound) const { - return ::HashCode(myEdges[0] + myEdges[1] + myEdges[2], theUpper); + return ::HashCode (myEdges[0] + myEdges[1] + myEdges[2], theUpperBound); } - + //! Checks for equality with another triangle. //! @param theOther triangle to be checked against this one. //! @return TRUE if equal, FALSE if not. @@ -143,10 +143,13 @@ public: BRepMesh_DegreeOfFreedom myMovability; }; -inline Standard_Integer HashCode(const BRepMesh_Triangle& theTriangle, - const Standard_Integer theUpper) +//! Computes a hash code for the given triangle, in the range [1, theUpperBound] +//! @param theTriangle the triangle which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +inline Standard_Integer HashCode (const BRepMesh_Triangle& theTriangle, const Standard_Integer theUpperBound) { - return theTriangle.HashCode(theUpper); + return theTriangle.HashCode (theUpperBound); } #endif diff --git a/src/BRepMesh/BRepMesh_Vertex.hxx b/src/BRepMesh/BRepMesh_Vertex.hxx index 4618e5f71f..73a31798ea 100644 --- a/src/BRepMesh/BRepMesh_Vertex.hxx +++ b/src/BRepMesh/BRepMesh_Vertex.hxx @@ -103,13 +103,13 @@ public: { myMovability = theMovability; } - - //! Returns hash code for this vertex. - //! @param theUpper upper index in the container. - //! @return hash code. - inline Standard_Integer HashCode(const Standard_Integer Upper) const + + //! Computes a hash code for this vertex, in the range [1, theUpperBound] + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + inline Standard_Integer HashCode(const Standard_Integer theUpperBound) const { - return ::HashCode(Floor(1e5 * myUV.X()) * Floor(1e5 * myUV.Y()), Upper); + return ::HashCode(Floor(1e5 * myUV.X()) * Floor(1e5 * myUV.Y()), theUpperBound); } //! Checks for equality with another vertex. @@ -139,9 +139,13 @@ private: BRepMesh_DegreeOfFreedom myMovability; }; -inline Standard_Integer HashCode(const BRepMesh_Vertex& me, const Standard_Integer Upper) +//! Computes a hash code for the given vertex, in the range [1, theUpperBound] +//! @param theVertex the vertex which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +inline Standard_Integer HashCode (const BRepMesh_Vertex& theVertex, const Standard_Integer theUpperBound) { - return me.HashCode(Upper); + return theVertex.HashCode (theUpperBound); } #endif diff --git a/src/Font/Font_FontMgr.hxx b/src/Font/Font_FontMgr.hxx index 37e2aaa75e..0017e518bb 100644 --- a/src/Font/Font_FontMgr.hxx +++ b/src/Font/Font_FontMgr.hxx @@ -136,13 +136,15 @@ private: Handle(Font_SystemFont) Find (const TCollection_AsciiString& theFontName) const; public: - - //! Hash value, for Map interface. - //! Based on Font Family, so that the whole family with different aspects can be found within the same bucket. - static Standard_Integer HashCode (const Handle(Font_SystemFont)& theFont, - const Standard_Integer theUpper) + //! Computes a hash code for the system font, in the range [1, theUpperBound]. Based on Font Family, so that the + //! whole family with different aspects can be found within the same bucket of some map + //! @param theHExtendedString the handle referred to extended string which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (const Handle (Font_SystemFont) & theSystemFont, + const Standard_Integer theUpperBound) { - return ::HashCode (theFont->FontKey(), theUpper); + return ::HashCode (theSystemFont->FontKey(), theUpperBound); } //! Matching two instances, for Map interface. diff --git a/src/Font/Font_SystemFont.hxx b/src/Font/Font_SystemFont.hxx index b9ae8a028b..a3e6e08680 100644 --- a/src/Font/Font_SystemFont.hxx +++ b/src/Font/Font_SystemFont.hxx @@ -89,13 +89,14 @@ public: Standard_EXPORT TCollection_AsciiString ToString() const; public: - - //! Hash value, for Map interface. - //! Based on Font Family, so that the whole family with different aspects can be found within the same bucket. - static Standard_Integer HashCode (const Handle(Font_SystemFont)& theFont, - const Standard_Integer theUpper) + //! Computes a hash code for the system font, in the range [1, theUpperBound]. Based on Font Family, so that the whole + //! family with different aspects can be found within the same bucket of some map + //! @param theSystemFont the system font which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (const Handle (Font_SystemFont) & theSystemFont, const Standard_Integer theUpperBound) { - return ::HashCode (theFont->FontKey(), theUpper); + return ::HashCode (theSystemFont->FontKey(), theUpperBound); } //! Matching two instances, for Map interface. diff --git a/src/IMeshData/IMeshData_Types.hxx b/src/IMeshData/IMeshData_Types.hxx index 1914c11f35..0b52effc0a 100644 --- a/src/IMeshData/IMeshData_Types.hxx +++ b/src/IMeshData/IMeshData_Types.hxx @@ -141,9 +141,13 @@ namespace IMeshData return (theFirst == theSecond); } - static Standard_Integer HashCode(const Type* thePtr, Standard_Integer theUpper) + //! Computes a hash code for the given pointer, in the range [1, theUpperBound] + //! @param thePointer the pointer which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (const Type* const thePointer, Standard_Integer theUpperBound) { - return ::HashCode(thePtr, theUpper); + return ::HashCode (thePointer, theUpperBound); } }; diff --git a/src/IntPolyh/IntPolyh_Couple.hxx b/src/IntPolyh/IntPolyh_Couple.hxx index 3c49c2c800..24770ef677 100644 --- a/src/IntPolyh/IntPolyh_Couple.hxx +++ b/src/IntPolyh/IntPolyh_Couple.hxx @@ -92,12 +92,15 @@ public: return (myIndex1 == theOther.myIndex1 && myIndex2 == theOther.myIndex2) || (myIndex1 == theOther.myIndex2 && myIndex2 == theOther.myIndex1); } - // - //! Returns hash code - Standard_Integer HashCode (const Standard_Integer theUpper) const + + //! Computes a hash code for this couple, in the range [1, theUpperBound] + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + Standard_Integer HashCode (const Standard_Integer theUpperBound) const { - return ::HashCode(myIndex1 + myIndex2, theUpper); + return ::HashCode (myIndex1 + myIndex2, theUpperBound); } + // Dump Standard_EXPORT void Dump (const Standard_Integer v) const; diff --git a/src/IntPolyh/IntPolyh_CoupleMapHasher.hxx b/src/IntPolyh/IntPolyh_CoupleMapHasher.hxx index 5086d77711..e0300624a3 100644 --- a/src/IntPolyh/IntPolyh_CoupleMapHasher.hxx +++ b/src/IntPolyh/IntPolyh_CoupleMapHasher.hxx @@ -24,13 +24,16 @@ class IntPolyh_Couple; class IntPolyh_CoupleMapHasher { public: - - static Standard_Integer HashCode(const IntPolyh_Couple& theCouple, - const Standard_Integer Upper) + + //! Computes a hash code for the given couple, in the range [1, theUpperBound] + //! @param theCouple the couple which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (const IntPolyh_Couple& theCouple, const Standard_Integer theUpperBound) { - return theCouple.HashCode(Upper); + return theCouple.HashCode (theUpperBound); } - + static Standard_Boolean IsEqual(const IntPolyh_Couple& theCouple1, const IntPolyh_Couple& theCouple2) { diff --git a/src/IntTools/IntTools_CurveRangeSampleMapHasher.hxx b/src/IntTools/IntTools_CurveRangeSampleMapHasher.hxx index 41f46e978b..1dc9d3f96c 100644 --- a/src/IntTools/IntTools_CurveRangeSampleMapHasher.hxx +++ b/src/IntTools/IntTools_CurveRangeSampleMapHasher.hxx @@ -32,11 +32,12 @@ public: DEFINE_STANDARD_ALLOC - - //! Returns a HasCode value for the Key in the - //! range 0..Upper. - static Standard_Integer HashCode (const IntTools_CurveRangeSample& K, const Standard_Integer Upper); - + //! Computes a hash code for the given key, in the range [1, theUpperBound] + //! @param theKey the key which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (const IntTools_CurveRangeSample& theKey, const Standard_Integer theUpperBound); + //! Returns True when the two keys are the same. Two //! same keys must have the same hashcode, the //! contrary is not necessary. diff --git a/src/IntTools/IntTools_CurveRangeSampleMapHasher.lxx b/src/IntTools/IntTools_CurveRangeSampleMapHasher.lxx index 44f188d598..dcd962ff94 100644 --- a/src/IntTools/IntTools_CurveRangeSampleMapHasher.lxx +++ b/src/IntTools/IntTools_CurveRangeSampleMapHasher.lxx @@ -13,11 +13,16 @@ // Alternatively, this file may be used under the terms of Open CASCADE // commercial license or contractual agreement. -inline Standard_Integer IntTools_CurveRangeSampleMapHasher::HashCode(const IntTools_CurveRangeSample& K, - const Standard_Integer Upper) { - return (K.GetDepth() % Upper); +//======================================================================= +// function : HashCode +// purpose : +//======================================================================= +inline Standard_Integer IntTools_CurveRangeSampleMapHasher::HashCode (const IntTools_CurveRangeSample& theKey, + const Standard_Integer theUpperBound) +{ + return ::HashCode(theKey.GetDepth(), theUpperBound); } - + inline Standard_Boolean IntTools_CurveRangeSampleMapHasher::IsEqual(const IntTools_CurveRangeSample& S1, const IntTools_CurveRangeSample& S2) { return S1.IsEqual(S2); diff --git a/src/IntTools/IntTools_SurfaceRangeSampleMapHasher.hxx b/src/IntTools/IntTools_SurfaceRangeSampleMapHasher.hxx index d801eb4e90..d8495ac4b9 100644 --- a/src/IntTools/IntTools_SurfaceRangeSampleMapHasher.hxx +++ b/src/IntTools/IntTools_SurfaceRangeSampleMapHasher.hxx @@ -32,11 +32,12 @@ public: DEFINE_STANDARD_ALLOC - - //! Returns a HasCode value for the Key in the - //! range 0..Upper. - static Standard_Integer HashCode (const IntTools_SurfaceRangeSample& K, const Standard_Integer Upper); - + //! Computes a hash code for the given key, in the range [1, theUpperBound] + //! @param theKey the key which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (const IntTools_SurfaceRangeSample& theKey, Standard_Integer theUpperBound); + //! Returns True when the two keys are the same. Two //! same keys must have the same hashcode, the //! contrary is not necessary. diff --git a/src/IntTools/IntTools_SurfaceRangeSampleMapHasher.lxx b/src/IntTools/IntTools_SurfaceRangeSampleMapHasher.lxx index 148a39e2dc..7e2b67b01d 100644 --- a/src/IntTools/IntTools_SurfaceRangeSampleMapHasher.lxx +++ b/src/IntTools/IntTools_SurfaceRangeSampleMapHasher.lxx @@ -13,12 +13,17 @@ // Alternatively, this file may be used under the terms of Open CASCADE // commercial license or contractual agreement. -inline Standard_Integer IntTools_SurfaceRangeSampleMapHasher::HashCode(const IntTools_SurfaceRangeSample& K, - const Standard_Integer Upper) { -// return (((K.GetDepthU() % Upper) ^ (K.GetDepthV() % Upper)) % Upper); - return ((K.GetIndexU() * K.GetIndexV()) % Upper); +//======================================================================= +// function : HashCode +// purpose : +//======================================================================= +inline Standard_Integer IntTools_SurfaceRangeSampleMapHasher::HashCode (const IntTools_SurfaceRangeSample& theKey, + const Standard_Integer theUpperBound) +{ + // return (((K.GetDepthU() % Upper) ^ (K.GetDepthV() % Upper)) % Upper); + return ::HashCode (theKey.GetIndexU() * theKey.GetIndexV(), theUpperBound); } - + inline Standard_Boolean IntTools_SurfaceRangeSampleMapHasher::IsEqual(const IntTools_SurfaceRangeSample& S1, const IntTools_SurfaceRangeSample& S2) { return S1.IsEqual(S2); diff --git a/src/Interface/Interface_MapAsciiStringHasher.cxx b/src/Interface/Interface_MapAsciiStringHasher.cxx index a9bbd3857c..e7a24f319a 100644 --- a/src/Interface/Interface_MapAsciiStringHasher.cxx +++ b/src/Interface/Interface_MapAsciiStringHasher.cxx @@ -16,13 +16,13 @@ #include //======================================================================= -//function : HashCode -//purpose : +// function : HashCode +// purpose : //======================================================================= -Standard_Integer Interface_MapAsciiStringHasher::HashCode(const TCollection_AsciiString& K, - const Standard_Integer Upper) +Standard_Integer Interface_MapAsciiStringHasher::HashCode (const TCollection_AsciiString& theAsciiString, + const Standard_Integer theUpperBound) { - return ::HashCode(K.ToCString(),Upper); + return ::HashCode (theAsciiString.ToCString(), theAsciiString.Length(), theUpperBound); } //======================================================================= diff --git a/src/Interface/Interface_MapAsciiStringHasher.hxx b/src/Interface/Interface_MapAsciiStringHasher.hxx index f9a9cce05b..295b8de013 100644 --- a/src/Interface/Interface_MapAsciiStringHasher.hxx +++ b/src/Interface/Interface_MapAsciiStringHasher.hxx @@ -32,9 +32,13 @@ public: DEFINE_STANDARD_ALLOC - - Standard_EXPORT static Standard_Integer HashCode (const TCollection_AsciiString& K, const Standard_Integer Upper); - + //! Computes a hash code for the given ASCII string, in the range [1, theUpperBound] + //! @param theAsciiString the ASCII string which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + Standard_EXPORT static Standard_Integer HashCode (const TCollection_AsciiString& theAsciiString, + Standard_Integer theUpperBound); + Standard_EXPORT static Standard_Boolean IsEqual (const TCollection_AsciiString& K1, const TCollection_AsciiString& K2); diff --git a/src/MAT2d/MAT2d_MapBiIntHasher.hxx b/src/MAT2d/MAT2d_MapBiIntHasher.hxx index 82be3f547b..5b786e1c90 100644 --- a/src/MAT2d/MAT2d_MapBiIntHasher.hxx +++ b/src/MAT2d/MAT2d_MapBiIntHasher.hxx @@ -32,11 +32,14 @@ class MAT2d_MapBiIntHasher public: DEFINE_STANDARD_ALLOC - - - static Standard_Integer HashCode (const MAT2d_BiInt& Key1, const Standard_Integer Upper); - - static Standard_Boolean IsEqual (const MAT2d_BiInt& Key1, const MAT2d_BiInt& Key2); + + //! Computes a hash code for the given key, in the range [1, theUpperBound] + //! @param theKey the key which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (const MAT2d_BiInt& theKey, const Standard_Integer theUpperBound); + + static Standard_Boolean IsEqual (const MAT2d_BiInt& Key1, const MAT2d_BiInt& Key2); diff --git a/src/MAT2d/MAT2d_MapBiIntHasher.lxx b/src/MAT2d/MAT2d_MapBiIntHasher.lxx index 30b0601073..1192c2a393 100644 --- a/src/MAT2d/MAT2d_MapBiIntHasher.lxx +++ b/src/MAT2d/MAT2d_MapBiIntHasher.lxx @@ -18,13 +18,12 @@ #include //======================================================================= -//function : HashCode -//purpose : +// function : HashCode +// purpose : //======================================================================= -inline Standard_Integer MAT2d_MapBiIntHasher::HashCode -(const MAT2d_BiInt& Key1, const Standard_Integer upper) +inline Standard_Integer MAT2d_MapBiIntHasher::HashCode (const MAT2d_BiInt& theKey, const Standard_Integer theUpperBound) { - return TColStd_MapIntegerHasher::HashCode(Key1.FirstIndex(),upper); + return TColStd_MapIntegerHasher::HashCode (theKey.FirstIndex(), theUpperBound); } //======================================================================= diff --git a/src/MeshVS/MeshVS_SymmetricPairHasher.hxx b/src/MeshVS/MeshVS_SymmetricPairHasher.hxx index e3abfbcc28..3b3435b916 100644 --- a/src/MeshVS/MeshVS_SymmetricPairHasher.hxx +++ b/src/MeshVS/MeshVS_SymmetricPairHasher.hxx @@ -21,9 +21,13 @@ typedef std::pair MeshVS_NodePair; //! Provides symmetric hash methods pair of integers. struct MeshVS_SymmetricPairHasher { - static Standard_Integer HashCode (const MeshVS_NodePair& thePair, const Standard_Integer theMaxCode) + //! Computes a hash code for the node pair, in the range [1, theUpperBound] + //! @param theNodePair the node pair which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (const MeshVS_NodePair& theNodePair, const Standard_Integer theUpperBound) { - return ((thePair.first + thePair.second) & 0x7FFFFFFF) % theMaxCode + 1; + return ::HashCode(theNodePair.first + theNodePair.second, theUpperBound); } static Standard_Boolean IsEqual (const MeshVS_NodePair& thePair1, const MeshVS_NodePair& thePair2) diff --git a/src/MeshVS/MeshVS_TwoColors.cxx b/src/MeshVS/MeshVS_TwoColors.cxx index 2be89c61f3..e845d9ece0 100644 --- a/src/MeshVS/MeshVS_TwoColors.cxx +++ b/src/MeshVS/MeshVS_TwoColors.cxx @@ -17,18 +17,17 @@ //================================================================ -// Function : HashCode -// Purpose : +// function : HashCode +// purpose : //================================================================ -Standard_Integer HashCode ( const MeshVS_TwoColors& theKey, - const Standard_Integer theUpper) +Standard_Integer HashCode (const MeshVS_TwoColors& theKey, const Standard_Integer theUpperBound) { #define MESHPRS_HASH_BYTE(val) { \ aHash += (val); \ aHash += (aHash << 10); \ aHash ^= (aHash >> 6); \ } - Standard_Integer aHash = 0; + unsigned int aHash = 0; MESHPRS_HASH_BYTE (theKey.r1) MESHPRS_HASH_BYTE (theKey.g1) MESHPRS_HASH_BYTE (theKey.b1) @@ -38,7 +37,7 @@ Standard_Integer HashCode ( const MeshVS_TwoColors& theKey, aHash += (aHash << 3); aHash ^= (aHash >> 11); aHash += (aHash << 15); - return (( aHash & 0x7fffffff ) % theUpper) + 1; + return HashCode(aHash, theUpperBound); #undef MESHPRS_HASH_BYTE } diff --git a/src/MeshVS/MeshVS_TwoColors.hxx b/src/MeshVS/MeshVS_TwoColors.hxx index b302ca26d3..8dfa4e067e 100644 --- a/src/MeshVS/MeshVS_TwoColors.hxx +++ b/src/MeshVS/MeshVS_TwoColors.hxx @@ -28,8 +28,11 @@ typedef struct { } MeshVS_TwoColors; -Standard_EXPORT Standard_Integer HashCode ( const MeshVS_TwoColors& theKey, - const Standard_Integer theUpper ); +//! Computes a hash code for the key, in the range [1, theUpperBound] +//! @param theKey the key which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +Standard_EXPORT Standard_Integer HashCode (const MeshVS_TwoColors& theKey, Standard_Integer theUpperBound); Standard_EXPORT Standard_Boolean IsEqual (const MeshVS_TwoColors& K1, const MeshVS_TwoColors& K2 ); diff --git a/src/MeshVS/MeshVS_TwoNodes.hxx b/src/MeshVS/MeshVS_TwoNodes.hxx index db2200ed9a..a9b06cf2fa 100644 --- a/src/MeshVS/MeshVS_TwoNodes.hxx +++ b/src/MeshVS/MeshVS_TwoNodes.hxx @@ -32,17 +32,15 @@ struct MeshVS_TwoNodes : First(aFirst), Second(aSecond) {} }; -//================================================================ -// Function : HashCode -// Purpose : -//================================================================ - -inline Standard_Integer HashCode( const MeshVS_TwoNodes& obj, - const Standard_Integer Upper ) +//! Computes a hash code for two nodes, in the range [1, theUpperBound] +//! @param theTwoNodes the object of structure containing two IDs which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +inline Standard_Integer HashCode (const MeshVS_TwoNodes& theTwoNodes, const Standard_Integer theUpperBound) { - // symmetrical with respect to obj.First and obj.Second - const Standard_Integer aKey = obj.First + obj.Second; - return HashCode (aKey, Upper); + // symmetrical with respect to theTwoNodes.First and theTwoNodes.Second + const Standard_Integer aKey = theTwoNodes.First + theTwoNodes.Second; + return HashCode (aKey, theUpperBound); } //================================================================ diff --git a/src/MoniTool/MoniTool_ElemHasher.cxx b/src/MoniTool/MoniTool_ElemHasher.cxx index 262a83d4ec..fa203e6d76 100644 --- a/src/MoniTool/MoniTool_ElemHasher.cxx +++ b/src/MoniTool/MoniTool_ElemHasher.cxx @@ -15,10 +15,14 @@ #include #include -Standard_Integer MoniTool_ElemHasher::HashCode - (const Handle(MoniTool_Element)& K, const Standard_Integer Upper) +//============================================================================ +// function : HashCode +// purpose : +//============================================================================ +Standard_Integer MoniTool_ElemHasher::HashCode (const Handle (MoniTool_Element) & theElement, + const Standard_Integer theUpperBound) { - return ((K->GetHashCode() - 1) % Upper) + 1; + return ::HashCode(theElement->GetHashCode() - 1, theUpperBound); } Standard_Boolean MoniTool_ElemHasher::IsEqual diff --git a/src/MoniTool/MoniTool_ElemHasher.hxx b/src/MoniTool/MoniTool_ElemHasher.hxx index 5be876d71a..32a3a5092a 100644 --- a/src/MoniTool/MoniTool_ElemHasher.hxx +++ b/src/MoniTool/MoniTool_ElemHasher.hxx @@ -38,12 +38,14 @@ public: DEFINE_STANDARD_ALLOC - - //! Returns a HashCode in the range <0,Upper> for a Element : - //! asks the Element its HashCode then transforms it to be in the - //! required range - Standard_EXPORT static Standard_Integer HashCode (const Handle(MoniTool_Element)& K, const Standard_Integer Upper); - + //! Returns hash code for the given element, in the range [1, theUpperBound]. + //! Asks theElement its HashCode, then transforms it to be in the required range. + //! @param theElement the element which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + Standard_EXPORT static Standard_Integer HashCode (const Handle (MoniTool_Element) & theElement, + Standard_Integer theUpperBound); + //! Returns True if two keys are the same. //! The test does not work on the Elements themselves but by //! calling their methods Equates diff --git a/src/MoniTool/MoniTool_MTHasher.hxx b/src/MoniTool/MoniTool_MTHasher.hxx index 2c6d178e5d..c6e00bd7b0 100644 --- a/src/MoniTool/MoniTool_MTHasher.hxx +++ b/src/MoniTool/MoniTool_MTHasher.hxx @@ -33,12 +33,12 @@ public: DEFINE_STANDARD_ALLOC - - //! Returns a HasCode value for the CString in the - //! range 0..Upper. - //! Default ::HashCode(Str,Upper) - static Standard_Integer HashCode (const Standard_CString Str, const Standard_Integer Upper); - + //! Returns hash code for the given string, in the range [1, theUpperBound] + //! @param theString the string which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (Standard_CString theString, Standard_Integer theUpperBound); + //! Returns True when the two CString are the same. Two //! same strings must have the same hashcode, the //! contrary is not necessary. diff --git a/src/MoniTool/MoniTool_MTHasher.lxx b/src/MoniTool/MoniTool_MTHasher.lxx index 97a0e53449..a39f1c6d9f 100644 --- a/src/MoniTool/MoniTool_MTHasher.lxx +++ b/src/MoniTool/MoniTool_MTHasher.lxx @@ -14,14 +14,13 @@ #include //======================================================================= -//function : HashCode -//purpose : +// function : HashCode +// purpose : //======================================================================= - -inline Standard_Integer MoniTool_MTHasher::HashCode(const Standard_CString Str, - const Standard_Integer Upper) +inline Standard_Integer MoniTool_MTHasher::HashCode (const Standard_CString theString, + const Standard_Integer theUpperBound) { - return ::HashCode(Str,Upper); + return ::HashCode (theString, theUpperBound); } //======================================================================= diff --git a/src/NCollection/NCollection_AccAllocator.hxx b/src/NCollection/NCollection_AccAllocator.hxx index 67e3178315..ba716b8d71 100644 --- a/src/NCollection/NCollection_AccAllocator.hxx +++ b/src/NCollection/NCollection_AccAllocator.hxx @@ -108,8 +108,14 @@ protected: class Hasher { public: - static Standard_Integer HashCode(const Key theKey, const Standard_Integer theUpper) - { return theKey.Value % theUpper + 1; } + //! Returns hash code for the given key, in the range [1, theUpperBound] + //! @param theKey the key which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (const Key theKey, const Standard_Integer theUpperBound) + { + return ::HashCode (theKey.Value, theUpperBound); + } static Standard_Boolean IsEqual(const Key theOne, const Key theTwo) { return theOne.Value == theTwo.Value; } diff --git a/src/NCollection/NCollection_CellFilter.hxx b/src/NCollection/NCollection_CellFilter.hxx index 598f718db4..dc447eabb9 100644 --- a/src/NCollection/NCollection_CellFilter.hxx +++ b/src/NCollection/NCollection_CellFilter.hxx @@ -317,26 +317,38 @@ protected: return Standard_True; } - //! Compute hash code - Standard_Integer HashCode (const Standard_Integer theUpper) const + //! Returns hash code for this cell, in the range [1, theUpperBound] + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + Standard_Integer HashCode (const Standard_Integer theUpperBound) const { // number of bits per each dimension in the hash code - Standard_Integer aDim = Standard_Integer(index.Size()); - const Standard_Size aShiftBits = (BITS(long)-1) / aDim; - long aCode=0; - for (int i=0; i < aDim; i++) - aCode = ( aCode << aShiftBits ) ^ index[i]; - return (unsigned)aCode % theUpper; + const std::size_t aDim = index.Size(); + const std::size_t aShiftBits = (BITS (long) - 1) / aDim; + unsigned int aCode = 0; + + for (std::size_t i = 0; i < aDim; ++i) + { + aCode = (aCode << aShiftBits) ^ index[i]; + } + + return ::HashCode(aCode, theUpperBound); } public: NCollection_LocalArray index; ListNode *Objects; }; + + //! Returns hash code for the given cell, in the range [1, theUpperBound] + //! @param theCell the cell object which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + friend Standard_Integer HashCode (const Cell& theCell, const Standard_Integer theUpperBound) + { + return theCell.HashCode (theUpperBound); + } - // definition of global functions is needed for map - friend Standard_Integer HashCode (const Cell &aCell, const Standard_Integer theUpper) - { return aCell.HashCode(theUpper); } friend Standard_Boolean IsEqual (const Cell &aCell1, const Cell &aCell2) { return aCell1.IsEqual(aCell2); } diff --git a/src/NCollection/NCollection_DefaultHasher.hxx b/src/NCollection/NCollection_DefaultHasher.hxx index fc2bb1ebe0..aff6a472fb 100644 --- a/src/NCollection/NCollection_DefaultHasher.hxx +++ b/src/NCollection/NCollection_DefaultHasher.hxx @@ -23,11 +23,15 @@ //purpose : Function is required to call the global function HashCode. //======================================================================= -template -inline Standard_Integer HashCode_Proxy (const TheKeyType& theKey, - const Standard_Integer Upper) +//! Returns hash code for the given key, in the range [1, theUpperBound] +//! @tparam TheKeyType the type of the given key +//! @param theKey the key which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +template +inline Standard_Integer HashCode_Proxy (const TheKeyType& theKey, const Standard_Integer theUpperBound) { - return HashCode (theKey, Upper); + return HashCode (theKey, theUpperBound); } //======================================================================= @@ -65,11 +69,15 @@ inline Standard_Boolean IsEqual_Proxy (const TheKeyType& theKey1, */ template class NCollection_DefaultHasher { public: - // - static Standard_Integer HashCode(const TheKeyType& theKey, - const Standard_Integer Upper) { - return HashCode_Proxy(theKey, Upper); + //! Returns hash code for the given key, in the range [1, theUpperBound] + //! @param theKey the key which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (const TheKeyType& theKey, const Standard_Integer theUpperBound) + { + return HashCode_Proxy (theKey, theUpperBound); } + // static Standard_Boolean IsEqual(const TheKeyType& theKey1, const TheKeyType& theKey2) { diff --git a/src/OSD/OSD_Thread.cxx b/src/OSD/OSD_Thread.cxx index 6cb971632e..0e609005e0 100644 --- a/src/OSD/OSD_Thread.cxx +++ b/src/OSD/OSD_Thread.cxx @@ -144,9 +144,10 @@ Standard_Boolean OSD_Thread::Run (const Standard_Address data, adata->func = myFunc; // then try to create a new thread - + DWORD aThreadId = DWORD(); myThread = CreateThread ( NULL, WNTStackSize, WNTthread_func, - adata, 0, &myThreadId ); + adata, 0, &aThreadId ); + myThreadId = aThreadId; if ( myThread ) SetThreadPriority (myThread, myPriority); else { @@ -162,7 +163,7 @@ Standard_Boolean OSD_Thread::Run (const Standard_Address data, } else { - myThreadId = myThread; + myThreadId = (Standard_ThreadId)myThread; } #endif return myThread != 0; @@ -328,6 +329,6 @@ Standard_ThreadId OSD_Thread::Current () #ifdef _WIN32 return GetCurrentThreadId(); #else - return pthread_self(); + return (Standard_ThreadId)pthread_self(); #endif } diff --git a/src/Poly/Poly_MakeLoops.hxx b/src/Poly/Poly_MakeLoops.hxx index 2058412889..79216540ea 100644 --- a/src/Poly/Poly_MakeLoops.hxx +++ b/src/Poly/Poly_MakeLoops.hxx @@ -250,13 +250,13 @@ private: TColStd_PackedMapOfInteger myHangIndices; }; -/** - * HashCode method is needed for maps - */ -inline Standard_Integer HashCode(const Poly_MakeLoops::Link& theKey, - int theLimit) +//! Computes a hash code for the given link, in the range [1, theUpperBound] +//! @param theLink the link which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +inline Standard_Integer HashCode (const Poly_MakeLoops::Link& theLink, const Standard_Integer theUpperBound) { - return HashCode(theKey.node1 + theKey.node2, theLimit); + return HashCode (theLink.node1 + theLink.node2, theUpperBound); } /** diff --git a/src/QANCollection/QANCollection_Test.cxx b/src/QANCollection/QANCollection_Test.cxx index 154c7c203e..918c9694cc 100644 --- a/src/QANCollection/QANCollection_Test.cxx +++ b/src/QANCollection/QANCollection_Test.cxx @@ -73,9 +73,14 @@ DEFINE_SEQUENCE(QANCollection_SequenceFunc,QANCollection_BaseColFunc,ItemType) DEFINE_HSEQUENCE(QANCollection_HSequenceFunc,QANCollection_SequenceFunc) // HashCode and IsEquel must be defined for key types of maps -Standard_Integer HashCode(const gp_Pnt thePnt, int theUpper) + +//! Computes a hash code for the point, in the range [1, theUpperBound] +//! @param thePoint the point which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +Standard_Integer HashCode (const gp_Pnt& thePoint, int theUpperBound) { - return HashCode(thePnt.X(),theUpper); + return HashCode (thePoint.X(), theUpperBound); } Standard_Boolean IsEqual(const gp_Pnt& theP1, const gp_Pnt& theP2) diff --git a/src/Quantity/Quantity_ColorHasher.hxx b/src/Quantity/Quantity_ColorHasher.hxx index 65fc4509f3..87010e6b80 100644 --- a/src/Quantity/Quantity_ColorHasher.hxx +++ b/src/Quantity/Quantity_ColorHasher.hxx @@ -20,22 +20,27 @@ //! Hasher of Quantity_Color. struct Quantity_ColorHasher { - //! Returns hash code for the given color. + //! Returns hash code for the given RGB color, in the range [1, theUpperBound] + //! @param theColor the RGB color object which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing range must be within + //! @return a computed hash code, in the range [1, theUpperBound] static Standard_Integer HashCode (const Quantity_Color& theColor, - const Standard_Integer theUpper) + const Standard_Integer theUpperBound) { Standard_Integer aRed = Standard_Integer (255 * theColor.Red()); Standard_Integer aGreen = Standard_Integer (255 * theColor.Green()); Standard_Integer aBlue = Standard_Integer (255 * theColor.Blue()); - Standard_Integer aHash = 0; + unsigned int aHash = 0; + updateHash (aHash, aRed); updateHash (aHash, aGreen); updateHash (aHash, aBlue); aHash += (aHash << 3); aHash ^= (aHash >> 11); aHash += (aHash << 15); - return ((aHash & 0x7fff) % theUpper) + 1; + + return IntegerHashCode(aHash, 0x7fff, theUpperBound); } //! Returns true if two colors are equal. @@ -46,8 +51,7 @@ struct Quantity_ColorHasher } protected: - static void updateHash (Standard_Integer& theHash, - const Standard_Integer theValue) + static void updateHash (unsigned int& theHash, const Standard_Integer theValue) { theHash += theValue; theHash += (theHash << 10); diff --git a/src/Quantity/Quantity_ColorRGBAHasher.hxx b/src/Quantity/Quantity_ColorRGBAHasher.hxx index 73f388fd02..c7c776c018 100644 --- a/src/Quantity/Quantity_ColorRGBAHasher.hxx +++ b/src/Quantity/Quantity_ColorRGBAHasher.hxx @@ -21,16 +21,19 @@ struct Quantity_ColorRGBAHasher { - //! Returns hash code for the given color. + //! Returns hash code for the given RGBA color, in the range [1, theUpperBound] + //! @param theColor the RGBA color object which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] static Standard_Integer HashCode (const Quantity_ColorRGBA& theColor, - const Standard_Integer theUpper) + const Standard_Integer theUpperBound) { const NCollection_Vec4& aColor = theColor; uint32_t aColor32 = (uint32_t(aColor.a() * 255.0f) << 24) + (uint32_t(aColor.b() * 255.0f) << 16) + (uint32_t(aColor.g() * 255.0f) << 8) + uint32_t(aColor.r() * 255.0f); - return ((aColor32 & 0x7fffffff) % theUpper) + 1; + return ::HashCode(aColor32, theUpperBound); } //! Returns true if two colors are equal. diff --git a/src/RWStl/RWStl_Reader.cxx b/src/RWStl/RWStl_Reader.cxx index 89f5cb91ec..a9c1290c19 100644 --- a/src/RWStl/RWStl_Reader.cxx +++ b/src/RWStl/RWStl_Reader.cxx @@ -75,9 +75,13 @@ namespace return (thePnt1 - thePnt2).SquareModulus() < Precision::SquareConfusion(); } - static Standard_Integer HashCode (const gp_XYZ& thePnt, Standard_Integer theUpper) + //! Computes a hash code for the point, in the range [1, theUpperBound] + //! @param thePoint the point which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (const gp_XYZ& thePoint, const Standard_Integer theUpperBound) { - return ::HashCode (thePnt.X() * M_LN10 + thePnt.Y() * M_PI + thePnt.Z() * M_E, theUpper); + return ::HashCode (thePoint.X() * M_LN10 + thePoint.Y() * M_PI + thePoint.Z() * M_E, theUpperBound); } private: diff --git a/src/STEPConstruct/STEPConstruct_PointHasher.hxx b/src/STEPConstruct/STEPConstruct_PointHasher.hxx index e710cd8943..9fa891b9fc 100644 --- a/src/STEPConstruct/STEPConstruct_PointHasher.hxx +++ b/src/STEPConstruct/STEPConstruct_PointHasher.hxx @@ -32,11 +32,12 @@ public: DEFINE_STANDARD_ALLOC - - //! Returns a HasCode value for the Key in the - //! range 0..Upper. - static Standard_Integer HashCode (const gp_Pnt& Point, const Standard_Integer Upper); - + //! Computes a hash code for the point, in the range [1, theUpperBound] + //! @param thePoint the point which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (const gp_Pnt& thePoint, const Standard_Integer theUpperBound); + //! Returns True when the two keys are the same. Two //! same keys must have the same hashcode, the //! contrary is not necessary. diff --git a/src/STEPConstruct/STEPConstruct_PointHasher.lxx b/src/STEPConstruct/STEPConstruct_PointHasher.lxx index edd101b522..1e2e907af8 100644 --- a/src/STEPConstruct/STEPConstruct_PointHasher.lxx +++ b/src/STEPConstruct/STEPConstruct_PointHasher.lxx @@ -12,22 +12,20 @@ // commercial license or contractual agreement. //======================================================================= -//function : HashCode -//purpose : +// function : HashCode +// purpose : //======================================================================= -inline Standard_Integer STEPConstruct_PointHasher::HashCode - (const gp_Pnt& point, const Standard_Integer Upper) +inline Standard_Integer STEPConstruct_PointHasher::HashCode (const gp_Pnt& thePoint, + const Standard_Integer theUpperBound) { - union - { - Standard_Real R[3]; + union + { + Standard_Real R[3]; Standard_Integer I[6]; - } U; + } U; - point.Coord(U.R[0],U.R[1],U.R[2]); - - return ::HashCode(U.I[0]/23+U.I[1]/19+U.I[2]/17+U.I[3]/13+U.I[4]/11+U.I[5]/7,Upper); - // return ::HashCode((U.I[0]>>4)+(U.I[1]>>3)+(U.I[2]>>2)+(U.I[3]>>1)+(U.I[4]>>4)+(U.I[5]>>3),Upper); + thePoint.Coord (U.R[0], U.R[1], U.R[2]); + return ::HashCode (U.I[0] / 23 + U.I[1] / 19 + U.I[2] / 17 + U.I[3] / 13 + U.I[4] / 11 + U.I[5] / 7, theUpperBound); + // return ::HashCode((U.I[0]>>4)+(U.I[1]>>3)+(U.I[2]>>2)+(U.I[3]>>1)+(U.I[4]>>4)+(U.I[5]>>3),theUpperBound); } - diff --git a/src/Standard/FILES b/src/Standard/FILES index 1d752bb89f..96f430fa5f 100755 --- a/src/Standard/FILES +++ b/src/Standard/FILES @@ -82,6 +82,7 @@ Standard_ShortReal.cxx Standard_ShortReal.hxx Standard_Size.hxx Standard_SStream.hxx +Standard_Std.hxx Standard_Stream.hxx Standard_Strtod.cxx Standard_ThreadId.hxx diff --git a/src/Standard/Standard_Address.hxx b/src/Standard/Standard_Address.hxx index 6328037139..2919d451e9 100644 --- a/src/Standard/Standard_Address.hxx +++ b/src/Standard/Standard_Address.hxx @@ -17,18 +17,23 @@ #include -//============================================================================ -//==== HashCode : Returns a HashCode CString -//============================================================================ -inline Standard_Integer HashCode (const Standard_Address Value, - const Standard_Integer Upper) +//! Returns a hash code of the given memory pointer +//! @param thePointer the memory pointer which hash code it to be computed +//! @param theUpperBound the upper bound of the range a resulting hash code must be within +//! @return a value of a computed hash code, in range [1, UpperBound] +inline Standard_Integer HashCode (const void* const thePointer, const Standard_Integer theUpperBound) { - union {Standard_Address L ; - Standard_Integer I[2] ;} U ; - U.I[0] = 0 ; - U.I[1] = 0 ; - U.L = Value; - return HashCode( ( ( U.I[0] ^ U.I[1] ) & 0x7fffffff ) , Upper ) ; + union + { + const void* L; + Standard_Integer I[2]; + } U; + + U.I[0] = 0; + U.I[1] = 0; + U.L = thePointer; + + return HashCode (U.I[0] ^ U.I[1], theUpperBound); } //============================================================================ diff --git a/src/Standard/Standard_CString.cxx b/src/Standard/Standard_CString.cxx index 9e05938100..9e70d38166 100755 --- a/src/Standard/Standard_CString.cxx +++ b/src/Standard/Standard_CString.cxx @@ -28,31 +28,33 @@ #include //============================================================================ -//==== HashCode of a CString +// function : HashCode +// purpose : //============================================================================ -Standard_Integer HashCode (const Standard_CString Value, - const Standard_Integer Upper ) +Standard_Integer HashCode (const Standard_CString theString, const Standard_Integer theUpperBound) { - Standard_Integer aLen = (Standard_Integer)strlen(Value); - return HashCode (HashCodes (Value, aLen), Upper); + const Standard_Integer aLength = static_cast (strlen (theString)); + + return HashCode (theString, aLength, theUpperBound); } //============================================================================ -//==== HashCode of a CString +// function : HashCodes +// purpose : //============================================================================ -Standard_Integer HashCodes (const Standard_CString Value, - const Standard_Integer Len) +Standard_Integer HashCodes (const Standard_CString theString, const Standard_Integer theLength) { // compute DJB2 hash of a string - Standard_Integer hash = 0; - const Standard_Character *c = Value; - for (Standard_Integer i = 0; i < Len; i++, c++) + unsigned int hash = 0; + const Standard_Character* c = theString; + + for (Standard_Integer i = 0; i < theLength; ++i, ++c) { /* hash = hash * 33 ^ c */ hash = ((hash << 5) + hash) ^ (*c); } - return hash; + return static_cast(hash); } //====================================================================== diff --git a/src/Standard/Standard_CString.hxx b/src/Standard/Standard_CString.hxx index 61db9f0995..c8aa5c4728 100644 --- a/src/Standard/Standard_CString.hxx +++ b/src/Standard/Standard_CString.hxx @@ -31,20 +31,30 @@ #include -//! Returns bounded hash code for a null-terminated string, in range [1, theUpper] -Standard_EXPORT Standard_Integer HashCode (const Standard_CString theStr, const Standard_Integer theUpper); - -//! Returns 32-bit hash code for the first theLen characters in the string theStr -Standard_EXPORT Standard_Integer HashCodes (const Standard_CString theStr, const Standard_Integer theLen); - -//! Returns bounded hash code for the first theLen characters in -//! the string theStr, in range [1, theUpper] -inline Standard_Integer HashCode (const Standard_CString theStr, - const Standard_Integer theLen, - const Standard_Integer theUpper) +//! Returns bounded hash code for the null-terminated string, in the range [1, theUpperBound] +//! @param theString the null-terminated string which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +Standard_EXPORT Standard_Integer HashCode (Standard_CString theString, Standard_Integer theUpperBound); + +//! Returns 32-bit hash code for the first theLen characters in the string theStr. +//! The result is unbound (may be not only positive, but also negative) +//! @param theString the string which hash code is to be computed +//! @param theLength the length of the given string +//! @return a computed hash code of the given string +Standard_EXPORT Standard_Integer HashCodes (Standard_CString theString, Standard_Integer theLength); + +//! Returns bounded hash code for the first theLength characters in the string theString, in the range [1, theUpperBound] +//! @param theString the string which hash code is to be computed +//! @param theLength the length of the initial substring of the given string which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code of the given string +inline Standard_Integer HashCode (const Standard_CString theString, + const Standard_Integer theLength, + const Standard_Integer theUpperBound) { // return (Abs( HashCodes( Value , Len ) ) % Upper ) + 1 ; - return HashCode (HashCodes (theStr, theLen), theUpper); + return HashCode (HashCodes (theString, theLength), theUpperBound); } //! Returns Standard_True if two strings are equal diff --git a/src/Standard/Standard_ErrorHandler.cxx b/src/Standard/Standard_ErrorHandler.cxx index c95b516f19..0b76c3ce0b 100644 --- a/src/Standard/Standard_ErrorHandler.cxx +++ b/src/Standard/Standard_ErrorHandler.cxx @@ -47,7 +47,7 @@ static Standard_Mutex theMutex; static inline Standard_ThreadId GetThreadID() { #ifndef _WIN32 - return pthread_self(); + return (Standard_ThreadId)pthread_self(); #else return GetCurrentThreadId(); #endif diff --git a/src/Standard/Standard_ExtString.cxx b/src/Standard/Standard_ExtString.cxx index e49b0371f6..94ca59fea3 100644 --- a/src/Standard/Standard_ExtString.cxx +++ b/src/Standard/Standard_ExtString.cxx @@ -13,19 +13,22 @@ // commercial license or contractual agreement. #include -#include -#include +#include -Standard_Integer HashCode (const Standard_ExtString Value, - const Standard_Integer Upper) +//============================================================================ +// function : HashCode +// purpose : +//============================================================================ +Standard_Integer HashCode (const Standard_ExtString theExtString, const Standard_Integer theUpperBound) { // compute SDBM hash of an ext string - Standard_Integer hash = 0; - for (const Standard_ExtCharacter *c = Value; *c; c++) + unsigned int hash = 0; + + for (const Standard_ExtCharacter* c = theExtString; *c; ++c) { /* hash = hash * 33 ^ c */ hash = (*c) + (hash << 6) + (hash << 16) - hash; } - return HashCode (hash, Upper); + return HashCode (hash, theUpperBound); } diff --git a/src/Standard/Standard_ExtString.hxx b/src/Standard/Standard_ExtString.hxx index c97c6c64c4..fb4e83484b 100644 --- a/src/Standard/Standard_ExtString.hxx +++ b/src/Standard/Standard_ExtString.hxx @@ -24,9 +24,10 @@ #include -//============================================================================ -//==== HashCode : Returns a HashCode ExtString -//============================================================================ -Standard_EXPORT Standard_Integer HashCode (const Standard_ExtString, const Standard_Integer); +//! Computes a hash code for the given wide character string, in the range [1, theUpperBound] +//! @param theExtString the wide character string which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +Standard_EXPORT Standard_Integer HashCode (Standard_ExtString theExtString, Standard_Integer theUpperBound); #endif diff --git a/src/Standard/Standard_GUID.cxx b/src/Standard/Standard_GUID.cxx index 7896d4196f..011a252312 100644 --- a/src/Standard/Standard_GUID.cxx +++ b/src/Standard/Standard_GUID.cxx @@ -388,9 +388,13 @@ void Standard_GUID::ShallowDump(Standard_OStream& aStream) const aStream << sguid; } -Standard_Integer Standard_GUID::HashCode(const Standard_GUID& aGuid,const Standard_Integer Upper) +//============================================================================ +// function : HashCode +// purpose : +//============================================================================ +Standard_Integer Standard_GUID::HashCode (const Standard_GUID& theGuid, const Standard_Integer theUpperBound) { - return aGuid.Hash(Upper); + return theGuid.Hash (theUpperBound); } Standard_Integer Standard_GUID::Hash(const Standard_Integer Upper) const diff --git a/src/Standard/Standard_GUID.hxx b/src/Standard/Standard_GUID.hxx index 3d390759f6..1e4dac4c05 100644 --- a/src/Standard/Standard_GUID.hxx +++ b/src/Standard/Standard_GUID.hxx @@ -112,9 +112,12 @@ void operator = (const Standard_UUID& uid) //! Hash function for GUID. Standard_EXPORT Standard_Integer Hash (const Standard_Integer Upper) const; - - //! H method used by collections. - Standard_EXPORT static Standard_Integer HashCode (const Standard_GUID& aguid, const Standard_Integer Upper); + + //! Computes a hash code for the given GUID of the Standard_Integer type, in the range [1, theUpperBound] + //! @param theGUID the GUID which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + Standard_EXPORT static Standard_Integer HashCode (const Standard_GUID& theGUID, Standard_Integer theUpperBound); //! Returns True when the two GUID are the same. Standard_EXPORT static Standard_Boolean IsEqual (const Standard_GUID& string1, const Standard_GUID& string2); diff --git a/src/Standard/Standard_Handle.hxx b/src/Standard/Standard_Handle.hxx index 9e7a3a6a5e..d411ae52ac 100644 --- a/src/Standard/Standard_Handle.hxx +++ b/src/Standard/Standard_Handle.hxx @@ -15,67 +15,15 @@ #define _Standard_Handle_HeaderFile #include +#include #include #include -#include - class Standard_Transient; //! Namespace opencascade is intended for low-level template classes and functions namespace opencascade { - //! Namespace opencascade::std includes templates from C++11 std namespace used by - //! OCCT classes. These definitions are imported from std namespace, plus (on older - //! compilers) from std::tr1, or implemented by custom code where neither std - //! not std::tr1 provide necessary definitions. - namespace std - { - // import all available standard stuff from std namespace - using namespace ::std; - -// for old MSVC compiler, some standard templates are defined in std::tr1 namespace, -// and some missing ones are implemented here -#if(defined(_MSC_VER) && (_MSC_VER < 1600)) - using namespace ::std::tr1; - - // C++11 template class enable_if - template - struct enable_if - {// type is undefined for assumed !_Test - }; - - template - struct enable_if - {// type is _Type for _Test - typedef _Type type; - }; - - template - struct conditional - { - typedef TypeTrue type; - }; - - template - struct conditional - { - typedef TypeFalse type; - }; - -#endif - - } // namespace opencascade::std - - //! Trait yielding true if class T1 is base of T2 but not the same - template - struct is_base_but_not_same : opencascade::std::is_base_of {}; - - //! Explicit specialization of is_base_of trait to workaround the - //! requirement of type to be complete when T1 and T2 are the same. - template - struct is_base_but_not_same ::value>::type> : opencascade::std::false_type {}; - //! Intrusive smart pointer for use with Standard_Transient class and its descendants. //! //! This class is similar to boost::intrusive_ptr<>. The reference counter @@ -462,11 +410,15 @@ namespace opencascade { //! Define Handle() macro #define Handle(Class) opencascade::handle -//! Global method HashCode(), for use in hash maps -template -inline Standard_Integer HashCode (const Handle(T)& theHandle, const Standard_Integer theUpper) +//! Computes a hash code for the standard handle, in the range [1, theUpperBound] +//! @param TheTransientType the type of the object the handle is referred to +//! @param theHandle the standard handle which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +template +Standard_Integer HashCode (const Handle (TheTransientType) & theHandle, const Standard_Integer theUpperBound) { - return ::HashCode (const_cast(static_cast(theHandle.get())), theUpper); + return ::HashCode (theHandle.get(), theUpperBound); } //! For compatibility with previous versions of OCCT, define Handle_Class alias for opencascade::handle. diff --git a/src/Standard/Standard_Integer.hxx b/src/Standard/Standard_Integer.hxx index a8ee8f8932..29a792b354 100755 --- a/src/Standard/Standard_Integer.hxx +++ b/src/Standard/Standard_Integer.hxx @@ -15,6 +15,7 @@ #ifndef _Standard_Integer_HeaderFile #define _Standard_Integer_HeaderFile +#include #include #include @@ -30,54 +31,6 @@ inline Standard_Integer Abs (const Standard_Integer Value) return Value >= 0 ? Value : -Value; } -// ------------------------------------------------------------------ -// Hascode : Computes a hascoding value for a given Integer -// ------------------------------------------------------------------ -inline Standard_Integer HashCode (const Standard_Integer theMe, - const Standard_Integer theUpper) -{ - //return (Abs (theMe) % theUpper) + 1; - return ((theMe & 0x7fffffff ) % theUpper) + 1; -} - -// ------------------------------------------------------------------ -// IsEqual : Returns Standard_True if two integers are equal -// ------------------------------------------------------------------ -inline Standard_Boolean IsEqual (const Standard_Integer theOne, - const Standard_Integer theTwo) -{ - return theOne == theTwo; -} - -// ------------------------------------------------------------------ -// Hascode : Computes a hascoding value for a given long long integer -// ------------------------------------------------------------------ -inline Standard_Integer HashCode(const long long int theMe, - const Standard_Integer theUpper) -{ - return ((theMe & 0x7fffffffffffffff) % theUpper) + 1; -} - -#if (defined(_LP64) || defined(__LP64__) || defined(_WIN64)) || defined(__APPLE__) -// ------------------------------------------------------------------ -// Hascode : Computes a hascoding value for a given unsigned integer -// ------------------------------------------------------------------ -inline Standard_Integer HashCode (const Standard_Utf32Char theMe, - const Standard_Integer theUpper) -{ - return ((theMe & 0x7fffffff ) % theUpper) + 1; -} - -// ------------------------------------------------------------------ -// IsEqual : Returns Standard_True if two integers are equal -// ------------------------------------------------------------------ -inline Standard_Boolean IsEqual (const Standard_Utf32Char theOne, - const Standard_Utf32Char theTwo) -{ - return theOne == theTwo; -} -#endif - // ------------------------------------------------------------------ // IsEven : Returns Standard_True if an integer is even // ------------------------------------------------------------------ @@ -140,4 +93,87 @@ inline Standard_Integer IntegerLast() inline Standard_Integer IntegerSize() { return BITS(Standard_Integer); } + +//! Computes a hash code for the given value of some integer type, in range [1, theUpperBound] +//! @tparam TheInteger the type of the integer which hash code is to be computed +//! @param theValue the value of the TheInteger type which hash code is to be computed +//! @param theMask the mask for the last bits of the value that are used in the computation of a hash code +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in range [1, theUpperBound] +template +typename opencascade::std::enable_if::value, Standard_Integer>::type +IntegerHashCode (const TheInteger theValue, + const typename opencascade::disable_deduction::type theMask, + const Standard_Integer theUpperBound) +{ + return static_cast ((theValue & theMask) % theUpperBound + 1); +} + +//! Computes a hash code for the given value of the Standard_Integer type, in range [1, theUpperBound] +//! @param theValue the value of the Standard_Integer type which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in range [1, theUpperBound] +inline Standard_Integer HashCode (const Standard_Integer theValue, + const Standard_Integer theUpperBound) +{ + // return (Abs (theMe) % theUpper) + 1; + return IntegerHashCode(theValue, IntegerLast(), theUpperBound); +} + +// ------------------------------------------------------------------ +// IsEqual : Returns Standard_True if two integers are equal +// ------------------------------------------------------------------ +inline Standard_Boolean IsEqual (const Standard_Integer theOne, + const Standard_Integer theTwo) +{ + return theOne == theTwo; +} + +//! Computes a hash value for the given unsigned integer, in range [1, theUpperBound] +//! @tparam TheUnsignedInteger the type of the given value (it is "unsigned int", +//! and must not be the same as Standard_Size, because the overload of the HashCode function +//! for Standard_Size type is already presented in Standard_Size.hxx) +//! @param theValue the unsigned integer which hash code is to be computed +//! @return a hash value computed for the given unsigned integer, in range [1, theUpperBound] +template +typename opencascade::std::enable_if::value + && opencascade::std::is_same::value, + Standard_Integer>::type +HashCode (const TheUnsignedInteger theValue, const Standard_Integer theUpperBound) +{ + return HashCode (static_cast (theValue), theUpperBound); +} + +//! Computes a hash code for the given value of the "long long int" type, in range [1, theUpperBound] +//! @param theValue the value of the "long long int" type which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in range [1, theUpperBound] +inline Standard_Integer HashCode (const long long int theValue, const Standard_Integer theUpperBound) +{ + return IntegerHashCode(theValue, 0x7fffffffffffffff, theUpperBound); +} + +#if (defined(_LP64) || defined(__LP64__) || defined(_WIN64)) || defined(__APPLE__) + +//! Computes a hash code for the given value of the Standard_Utf32Char type, in range [1, theUpperBound] +//! @param theValue the value of the Standard_Utf32Char type which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in range [1, theUpperBound] +inline Standard_Integer HashCode (const Standard_Utf32Char theValue, + const Standard_Integer theUpperBound) +{ + return IntegerHashCode(theValue, IntegerLast(), theUpperBound); +} + +// ------------------------------------------------------------------ +// IsEqual : Returns Standard_True if two integers are equal +// ------------------------------------------------------------------ +inline Standard_Boolean IsEqual (const Standard_Utf32Char theOne, + const Standard_Utf32Char theTwo) +{ + return theOne == theTwo; +} + +#endif + #endif diff --git a/src/Standard/Standard_Real.cxx b/src/Standard/Standard_Real.cxx index 106b7ac4cf..1f2cbac123 100644 --- a/src/Standard/Standard_Real.cxx +++ b/src/Standard/Standard_Real.cxx @@ -22,23 +22,27 @@ static const Standard_Real ACosLimit = 1. + Epsilon(1.); -// ------------------------------------------------------------------ -// Hascode : Computes a hascoding value for a given real -// ------------------------------------------------------------------ -Standard_Integer HashCode(const Standard_Real me, const Standard_Integer Upper) +//============================================================================ +// function : HashCode +// purpose : +//============================================================================ +Standard_Integer HashCode (const Standard_Real theReal, const Standard_Integer theUpperBound) { - if (Upper < 1){ - throw Standard_RangeError("Try to apply HashCode method with negative or null argument."); + if (theUpperBound < 1) + { + throw Standard_RangeError ("Try to apply HashCode method with negative or null argument."); } - union - { - Standard_Real R; + union + { + Standard_Real R; Standard_Integer I[2]; - } U; -// U.R = Abs(me); // Treat me = -0.0 ADN 27/11/97 - U.R = me ; - return HashCode( ( U.I[0] ^ U.I[1] ) , Upper ) ; - } + } U; + + // U.R = Abs(me); // Treat me = -0.0 ADN 27/11/97 + U.R = theReal; + + return HashCode (U.I[0] ^ U.I[1], theUpperBound); +} //------------------------------------------------------------------- // ACos : Returns the value of the arc cosine of a real diff --git a/src/Standard/Standard_Real.hxx b/src/Standard/Standard_Real.hxx index cc3d2e73d8..aca73cb2ef 100644 --- a/src/Standard/Standard_Real.hxx +++ b/src/Standard/Standard_Real.hxx @@ -30,7 +30,12 @@ // ================================== // Methods implemeted in Standard_Real.cxx // ================================== -Standard_EXPORT Standard_Integer HashCode (const Standard_Real, const Standard_Integer); + +//! Computes a hash code for the given real, in the range [1, theUpperBound] +//! @param theReal the real value which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +Standard_EXPORT Standard_Integer HashCode (Standard_Real theReal, Standard_Integer theUpperBound); Standard_EXPORT Standard_Real ACos (const Standard_Real ); Standard_EXPORT Standard_Real ACosApprox (const Standard_Real ); diff --git a/src/Standard/Standard_ShortReal.cxx b/src/Standard/Standard_ShortReal.cxx index 63d765d887..2de2d0f24f 100644 --- a/src/Standard/Standard_ShortReal.cxx +++ b/src/Standard/Standard_ShortReal.cxx @@ -18,19 +18,22 @@ #include #include -// ------------------------------------------------------------------ -// Hascode : Computes a hascoding value for a given ShortReal -// ------------------------------------------------------------------ -Standard_Integer HashCode(const Standard_ShortReal me, const Standard_Integer Upper) +//============================================================================ +// function : HashCode +// purpose : +//============================================================================ +Standard_Integer HashCode (const Standard_ShortReal theShortReal, const Standard_Integer theUpperBound) { - if (Upper < 1){ - throw Standard_RangeError("Try to apply HashCode method with negative or null argument."); + if (theUpperBound < 1) + { + throw Standard_RangeError ("Try to apply HashCode method with negative or null argument."); } - union - { + union + { Standard_ShortReal R; - Standard_Integer I; - } U; - U.R = me; - return HashCode( U.I , Upper) ; + Standard_Integer I; + } U; + U.R = theShortReal; + + return HashCode (U.I, theUpperBound); } diff --git a/src/Standard/Standard_ShortReal.hxx b/src/Standard/Standard_ShortReal.hxx index 1f2a294503..fea03833b1 100644 --- a/src/Standard/Standard_ShortReal.hxx +++ b/src/Standard/Standard_ShortReal.hxx @@ -138,7 +138,12 @@ inline Standard_ShortReal Min (const Standard_ShortReal Val1, // ================================== // Methods implemeted in Standard_ShortReal.cxx // ================================== -Standard_EXPORT Standard_Integer HashCode (const Standard_ShortReal, const Standard_Integer); + +//! Computes a hash code for the given short real, in the range [1, theUpperBound] +//! @param theShortReal the short real value which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +Standard_EXPORT Standard_Integer HashCode (Standard_ShortReal theShortReal, Standard_Integer theUpperBound); //------------------------------------------------------------------- // IsEqual : Returns Standard_True if two ShortReals are equal diff --git a/src/Standard/Standard_Size.hxx b/src/Standard/Standard_Size.hxx index dada6eb175..2c23819876 100644 --- a/src/Standard/Standard_Size.hxx +++ b/src/Standard/Standard_Size.hxx @@ -16,21 +16,23 @@ #ifndef _Standard_Size_HeaderFile #define _Standard_Size_HeaderFile +#include + // msv 26.05.2009: add HashCode and IsEqual functions -// ------------------------------------------------------------------ -// Hascode : Computes a hashcoding value for a given value -// ------------------------------------------------------------------ -inline Standard_Integer HashCode(const Standard_Size Val, - const Standard_Integer Upper) +//! Computes a hash code for the given value of the Standard_Size type, in the range [1, theUpperBound] +//! @param theValue the value of the Standard_Size type which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +inline Standard_Integer HashCode (const Standard_Size theValue, const Standard_Integer theUpperBound) { - Standard_Size aKey = ~Val + (Val << 18); + Standard_Size aKey = ~theValue + (theValue << 18); aKey ^= (aKey >> 31); aKey *= 21; aKey ^= (aKey >> 11); aKey += (aKey << 6); aKey ^= (aKey >> 22); - return (Standard_Integer(aKey & 0x7fffffff) % Upper) + 1; + return IntegerHashCode(aKey, IntegerLast(), theUpperBound); } // ------------------------------------------------------------------ diff --git a/src/Standard/Standard_Std.hxx b/src/Standard/Standard_Std.hxx new file mode 100644 index 0000000000..db925b0c20 --- /dev/null +++ b/src/Standard/Standard_Std.hxx @@ -0,0 +1,106 @@ +// Created on: 2019-03-27 +// Created by: Timur Izmaylov +// Copyright (c) 2019 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 _Standard_Std_HeaderFile +#define _Standard_Std_HeaderFile + + +#include + + +//! Namespace opencascade is intended for low-level template classes and functions +namespace opencascade +{ + + //! Namespace opencascade::std includes templates from C++11 std namespace used by + //! OCCT classes. These definitions are imported from std namespace, plus (on older + //! compilers) from std::tr1, or implemented by custom code where neither std + //! not std::tr1 provide necessary definitions. + namespace std + { + // import all available standard stuff from std namespace + using namespace ::std; + +// for old MSVC compiler, some standard templates are defined in std::tr1 namespace, +// and some missing ones are implemented here +#if (defined(_MSC_VER) && (_MSC_VER < 1600)) + using namespace ::std::tr1; + + // C++11 template class enable_if + template + struct enable_if + { // type is undefined for assumed !_Test + }; + + template + struct enable_if + { // type is _Type for _Test + typedef _Type type; + }; + + template + struct conditional + { + typedef TypeTrue type; + }; + + template + struct conditional + { + typedef TypeFalse type; + }; + +#endif + + } // namespace std + + //! Trait yielding true if class T1 is base of T2 but not the same + template + struct is_base_but_not_same : opencascade::std::is_base_of + { + }; + + //! Explicit specialization of is_base_of trait to workaround the + //! requirement of type to be complete when T1 and T2 are the same. + template + struct is_base_but_not_same::value>::type> + : opencascade::std::false_type + { + }; + + //! The type trait that checks if the passed type is integer (it must be integral and not boolean) + //! @tparam TheInteger the checked type + template + struct is_integer : std::integral_constant::value + && !opencascade::std::is_same::value> + { + }; + + //! The auxiliary template that is used for template argument deduction in function templates. A function argument + //! which type is a template type parameter and it is not needed to be deducted must be declared using this class + //! template based on the type of some other template type parameter of a function template + //! @tparam TheType the type that is used as a function argument type to prevent its deduction + template + struct disable_deduction + { + typedef TheType type; + }; + +} // namespace opencascade + +#endif diff --git a/src/Standard/Standard_ThreadId.hxx b/src/Standard/Standard_ThreadId.hxx index 5ebc667fad..b4d06260ee 100644 --- a/src/Standard/Standard_ThreadId.hxx +++ b/src/Standard/Standard_ThreadId.hxx @@ -16,25 +16,9 @@ #ifndef Standard_ThreadId_HeaderFile #define Standard_ThreadId_HeaderFile -// Platform-dependent definition of the thread identifier type +#include -#ifdef _WIN32 - -#include -typedef DWORD Standard_ThreadId; - -inline Standard_Integer HashCode(const Standard_ThreadId Value, - const Standard_Integer Upper) -{ - // Size of int == size of unsigned long == 4 for WIN32 and WIN64 - return HashCode((Standard_Integer)Value, Upper); -} - -#else - -#include -typedef pthread_t Standard_ThreadId; - -#endif +// Platform-independent definition of the thread identifier type +typedef Standard_Size Standard_ThreadId; #endif diff --git a/src/Standard/Standard_Transient.hxx b/src/Standard/Standard_Transient.hxx index 40f9524470..576553f0a9 100644 --- a/src/Standard/Standard_Transient.hxx +++ b/src/Standard/Standard_Transient.hxx @@ -105,10 +105,15 @@ private: mutable volatile Standard_Integer count; }; -//! Global method HashCode(), for use in hash maps -inline Standard_Integer HashCode (const Standard_Transient* theObject, const Standard_Integer theUpper) + +//! Computes a hash code for the given transient object, in the range [1, theUpperBound] +//! @param theTransientObject the transient object which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +inline Standard_Integer HashCode (const Standard_Transient* const theTransientObject, + const Standard_Integer theUpperBound) { - return ::HashCode ((Standard_Address*)theObject, theUpper); + return ::HashCode (static_cast (theTransientObject), theUpperBound); } //! Definition of Handle_Standard_Transient as typedef for compatibility diff --git a/src/Standard/Standard_Type.cxx b/src/Standard/Standard_Type.cxx index c07d1693a4..a07b48735d 100644 --- a/src/Standard/Standard_Type.cxx +++ b/src/Standard/Standard_Type.cxx @@ -74,9 +74,13 @@ namespace { // Value-based hasher for plain C string (char*) struct CStringHasher { - static Standard_Integer HashCode (const Standard_CString& theKey, const Standard_Integer Upper) + //! Computes a hash code of the given Standard_CString, in the range [1, theUpperBound] + //! @param theKey the key which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (const Standard_CString& theKey, const Standard_Integer theUpperBound) { - return ::HashCode (theKey, Upper); + return ::HashCode (theKey, theUpperBound); } static bool IsEqual (const Standard_CString& theKey1, const Standard_CString& theKey2) { diff --git a/src/StepToTopoDS/StepToTopoDS_CartesianPointHasher.cxx b/src/StepToTopoDS/StepToTopoDS_CartesianPointHasher.cxx index f3d87c96d1..6deba8805d 100644 --- a/src/StepToTopoDS/StepToTopoDS_CartesianPointHasher.cxx +++ b/src/StepToTopoDS/StepToTopoDS_CartesianPointHasher.cxx @@ -19,13 +19,14 @@ #include //======================================================================= -//function : HashCode -//purpose : +// function : HashCode +// purpose : //======================================================================= -Standard_Integer StepToTopoDS_CartesianPointHasher::HashCode - (const Handle(StepGeom_CartesianPoint)& K, const Standard_Integer Upper) +Standard_Integer StepToTopoDS_CartesianPointHasher::HashCode (const Handle (StepGeom_CartesianPoint) + & theCartesianPoint, + const Standard_Integer theUpperBound) { - return ::HashCode(K,Upper); + return ::HashCode (theCartesianPoint, theUpperBound); } //======================================================================= diff --git a/src/StepToTopoDS/StepToTopoDS_CartesianPointHasher.hxx b/src/StepToTopoDS/StepToTopoDS_CartesianPointHasher.hxx index e58ea99639..accd92f990 100644 --- a/src/StepToTopoDS/StepToTopoDS_CartesianPointHasher.hxx +++ b/src/StepToTopoDS/StepToTopoDS_CartesianPointHasher.hxx @@ -33,10 +33,13 @@ public: DEFINE_STANDARD_ALLOC - - //! Returns a HasCode value for the CartesianPoint - Standard_EXPORT static Standard_Integer HashCode (const Handle(StepGeom_CartesianPoint)& K, const Standard_Integer Upper); - + //! Computes a hash code for the cartesian point, in the range [1, theUpperBound] + //! @param theCartesianPoint the cartesian point which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + Standard_EXPORT static Standard_Integer HashCode (const Handle (StepGeom_CartesianPoint) & theCartesianPoint, + Standard_Integer theUpperBound); + //! Returns True when the two CartesianPoint are the same Standard_EXPORT static Standard_Boolean IsEqual (const Handle(StepGeom_CartesianPoint)& K1, const Handle(StepGeom_CartesianPoint)& K2); diff --git a/src/StepToTopoDS/StepToTopoDS_PointPairHasher.cxx b/src/StepToTopoDS/StepToTopoDS_PointPairHasher.cxx index ae20c0a121..22324ec390 100644 --- a/src/StepToTopoDS/StepToTopoDS_PointPairHasher.cxx +++ b/src/StepToTopoDS/StepToTopoDS_PointPairHasher.cxx @@ -20,13 +20,14 @@ #include //======================================================================= -//function : HashCode -//purpose : +// function : HashCode +// purpose : //======================================================================= -Standard_Integer StepToTopoDS_PointPairHasher::HashCode - (const StepToTopoDS_PointPair& P, const Standard_Integer Upper) +Standard_Integer StepToTopoDS_PointPairHasher::HashCode (const StepToTopoDS_PointPair& thePointPair, + const Standard_Integer theUpperBound) { - return (::HashCode(P.myP1,Upper) + ::HashCode(P.myP2,Upper)) % Upper; + return ::HashCode (::HashCode (thePointPair.myP1, theUpperBound) + ::HashCode (thePointPair.myP2, theUpperBound), + theUpperBound); } //======================================================================= diff --git a/src/StepToTopoDS/StepToTopoDS_PointPairHasher.hxx b/src/StepToTopoDS/StepToTopoDS_PointPairHasher.hxx index 448b764241..542e540280 100644 --- a/src/StepToTopoDS/StepToTopoDS_PointPairHasher.hxx +++ b/src/StepToTopoDS/StepToTopoDS_PointPairHasher.hxx @@ -33,9 +33,11 @@ public: DEFINE_STANDARD_ALLOC - - //! Returns a HasCode value for the PointPair - Standard_EXPORT static Standard_Integer HashCode (const StepToTopoDS_PointPair& K, const Standard_Integer Upper); + //! Computes a hash code for the point pair, in the range [1, theUpperBound] + //! @param thePointPair the point pair which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + Standard_EXPORT static Standard_Integer HashCode (const StepToTopoDS_PointPair& thePointPair, Standard_Integer theUpperBound); //! Returns True when the two PointPair are the same Standard_EXPORT static Standard_Boolean IsEqual (const StepToTopoDS_PointPair& K1, const StepToTopoDS_PointPair& K2); diff --git a/src/TColStd/TColStd_PackedMapOfInteger.cxx b/src/TColStd/TColStd_PackedMapOfInteger.cxx index a1e23d2501..6ce5124f72 100644 --- a/src/TColStd/TColStd_PackedMapOfInteger.cxx +++ b/src/TColStd/TColStd_PackedMapOfInteger.cxx @@ -79,12 +79,12 @@ public: */ Standard_Integer FindNext (unsigned int& theMask) const; - /** - * Support of Map interface. - */ - inline Standard_Integer HashCode (const Standard_Integer theUpper) const + //! Computes a hash code for this map in the range [1, theUpperBound] + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + inline Standard_Integer HashCode (const Standard_Integer theUpperBound) const { - return ::HashCode (Standard_Integer(myMask >> 5), theUpper); + return ::HashCode (myMask >> 5, theUpperBound); } /** diff --git a/src/TCollection/TCollection_AsciiString.hxx b/src/TCollection/TCollection_AsciiString.hxx index 1c3722f055..4f7df1a6fe 100644 --- a/src/TCollection/TCollection_AsciiString.hxx +++ b/src/TCollection/TCollection_AsciiString.hxx @@ -679,10 +679,13 @@ friend Standard_EXPORT Standard_IStream& operator >> (Standard_IStream& astream, //! aString.Value(2) returns 'e' Standard_EXPORT Standard_Character Value (const Standard_Integer where) const; - //! Hash function for AsciiString - //! (returns the same Integer value that the hash function for ExtendedString) - static Standard_Integer HashCode (const TCollection_AsciiString& astring, const Standard_Integer Upper); - + //! Computes a hash code for the given ASCII string, in the range [1, theUpperBound]. + //! Returns the same integer value as the hash function for TCollection_ExtendedString + //! @param theAsciiString the ASCII string which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (const TCollection_AsciiString& theAsciiString, Standard_Integer theUpperBound); + //! Returns True when the two strings are the same. //! (Just for HashCode for AsciiString) static Standard_Boolean IsEqual (const TCollection_AsciiString& string1, const TCollection_AsciiString& string2); diff --git a/src/TCollection/TCollection_AsciiString.lxx b/src/TCollection/TCollection_AsciiString.lxx index 0464a37682..6cc80dc268 100644 --- a/src/TCollection/TCollection_AsciiString.lxx +++ b/src/TCollection/TCollection_AsciiString.lxx @@ -15,14 +15,15 @@ #include #include -//definition global methods for using in NCollection -//------------------------------------------------------------------------ -// HashCode -//------------------------------------------------------------------------ -inline Standard_Integer HashCode(const TCollection_AsciiString& astring, - const Standard_Integer Upper) +// definition global methods for using in NCollection + +//! Computes a hash code for the given ASCII string, in the range [1, theUpperBound] +//! @param theAsciiString the ASCII string which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +inline Standard_Integer HashCode (const TCollection_AsciiString& theAsciiString, const Standard_Integer theUpperBound) { - return TCollection_AsciiString::HashCode(astring,Upper); + return TCollection_AsciiString::HashCode (theAsciiString, theUpperBound); } @@ -78,13 +79,14 @@ inline TCollection_AsciiString TCollection_AsciiString::Cat(const Standard_Real return TCollection_AsciiString( *this , TCollection_AsciiString(other) ) ; } -//------------------------------------------------------------------------ -// HashCode -//------------------------------------------------------------------------ -inline Standard_Integer TCollection_AsciiString::HashCode(const TCollection_AsciiString& astring, - const Standard_Integer Upper) +//============================================================================ +// function : HashCode +// purpose : +//============================================================================ +inline Standard_Integer TCollection_AsciiString::HashCode (const TCollection_AsciiString& theAsciiString, + const Standard_Integer theUpperBound) { - return ::HashCode(astring.ToCString(),Upper); + return ::HashCode (theAsciiString.ToCString(), theAsciiString.Length(), theUpperBound); } diff --git a/src/TCollection/TCollection_ExtendedString.hxx b/src/TCollection/TCollection_ExtendedString.hxx index 27b9ad77f2..3b429d2e3e 100644 --- a/src/TCollection/TCollection_ExtendedString.hxx +++ b/src/TCollection/TCollection_ExtendedString.hxx @@ -335,13 +335,16 @@ friend Standard_EXPORT Standard_OStream& operator << (Standard_OStream& astream, //! the bounds of this extended string. Standard_EXPORT Standard_ExtCharacter Value (const Standard_Integer where) const; - //! Returns a hashed value for the extended string within the range 1..theUpper. + //! Returns a hashed value for the extended string within the range 1 .. theUpper. //! Note: if string is ASCII, the computed value is the same as the value computed with the HashCode function on a //! TCollection_AsciiString string composed with equivalent ASCII characters. + //! @param theExtendedString the extended string which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] static Standard_Integer HashCode (const TCollection_ExtendedString& theString, - const Standard_Integer theUpper) + const Standard_Integer theUpperBound) { - return ::HashCode (theString.ToExtString(), theUpper); + return ::HashCode (theString.ToExtString(), theUpperBound); } //! Returns true if the characters in this extended @@ -376,11 +379,14 @@ private: }; -//! Compute hash code for extended string -inline Standard_Integer HashCode (const TCollection_ExtendedString& theString, - const Standard_Integer theUpper) +//! Computes a hash code for the given extended string, in the range [1, theUpperBound] +//! @param theExtendedString the extended string which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +inline Standard_Integer HashCode (const TCollection_ExtendedString& theExtendedString, + const Standard_Integer theUpperBound) { - return TCollection_ExtendedString::HashCode (theString, theUpper); + return TCollection_ExtendedString::HashCode (theExtendedString, theUpperBound); } #endif // _TCollection_ExtendedString_HeaderFile diff --git a/src/TDF/TDF_LabelMapHasher.hxx b/src/TDF/TDF_LabelMapHasher.hxx index c7840d2498..9084d15c2f 100644 --- a/src/TDF/TDF_LabelMapHasher.hxx +++ b/src/TDF/TDF_LabelMapHasher.hxx @@ -22,11 +22,13 @@ class TDF_LabelMapHasher { public: - - //! Returns a HasCode value for the Key in the range 0..Upper. - static Standard_Integer HashCode(const TDF_Label& aLab, const Standard_Integer Upper) + //! Computes a hash code for the given label, in the range [1, theUpperBound] + //! @param theLabel the label which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode(const TDF_Label& theLabel, const Standard_Integer theUpperBound) { - return ::HashCode((Standard_Address)aLab.myLabelNode, Upper); + return ::HashCode(theLabel.myLabelNode, theUpperBound); } //! Returns True when the two keys are the same. Two diff --git a/src/TObj/TObj_Common.hxx b/src/TObj/TObj_Common.hxx index 29418a04d8..85fdd77e36 100644 --- a/src/TObj/TObj_Common.hxx +++ b/src/TObj/TObj_Common.hxx @@ -31,10 +31,14 @@ //! Methods inline implimentation for HExtendedString -inline Standard_Integer HashCode (const Handle(TCollection_HExtendedString)& theStr, - const Standard_Integer theBnd) +//! Computes a hash code for the given handle referred to extended string, in the range [1, theUpperBound] +//! @param theHExtendedString the handle referred to extended string which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +inline Standard_Integer HashCode (const Handle (TCollection_HExtendedString) & theHExtendedString, + const Standard_Integer theUpperBound) { - return TCollection_ExtendedString::HashCode(theStr->String(), theBnd); + return TCollection_ExtendedString::HashCode (theHExtendedString->String(), theUpperBound); } inline Standard_Boolean IsEqual (const Handle(TCollection_HExtendedString)& theStr1, diff --git a/src/TopLoc/TopLoc_Location.cxx b/src/TopLoc/TopLoc_Location.cxx index 8111de5570..7dbce0588b 100644 --- a/src/TopLoc/TopLoc_Location.cxx +++ b/src/TopLoc/TopLoc_Location.cxx @@ -169,11 +169,10 @@ TopLoc_Location TopLoc_Location::Powered (const Standard_Integer pwr) const } //======================================================================= -//function : HashCode -//purpose : +// function : HashCode +// purpose : //======================================================================= - -Standard_Integer TopLoc_Location::HashCode(const Standard_Integer upper) const +Standard_Integer TopLoc_Location::HashCode (const Standard_Integer theUpperBound) const { // the HashCode computed for a Location is the bitwise exclusive or // of values computed for each element of the list @@ -186,18 +185,19 @@ Standard_Integer TopLoc_Location::HashCode(const Standard_Integer upper) const // this value is biwise rotated by depth // the use of depth avoids getting the same result for two permutated lists. - Standard_Integer depth = 0; - unsigned int h = 0; + Standard_Integer depth = 0; + unsigned int h = 0; TopLoc_SListOfItemLocation items = myItems; - while (items.More()) { + while (items.More()) + { depth += 3; - unsigned int i = ::HashCode (items.Value().myDatum, upper); - unsigned int j = ( (i + items.Value().myPower) <>(32-depth) | j<> (32 - depth) | j << depth; h ^= j; - items.Next(); + items.Next (); } - return h % upper; + return ::HashCode (h, theUpperBound); } //======================================================================= diff --git a/src/TopLoc/TopLoc_Location.hxx b/src/TopLoc/TopLoc_Location.hxx index 88d361421c..0eec93b46b 100644 --- a/src/TopLoc/TopLoc_Location.hxx +++ b/src/TopLoc/TopLoc_Location.hxx @@ -115,11 +115,12 @@ Standard_NODISCARD TopLoc_Location operator/ (const TopLoc_Location& Other) cons //! returns Identity. can be lower than zero //! (usual meaning for powers). Standard_EXPORT Standard_NODISCARD TopLoc_Location Powered (const Standard_Integer pwr) const; - - //! Returns a hashed value for this local coordinate system. - //! This value is used, with map tables, to store and - //! retrieve the object easily, and is in the range [ 1..Upper ]. - Standard_EXPORT Standard_Integer HashCode (const Standard_Integer Upper) const; + + //! Returns a hashed value for this local coordinate system. This value is used, with map tables, to store and + //! retrieve the object easily, and is in the range [1, theUpperBound]. + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + Standard_EXPORT Standard_Integer HashCode (Standard_Integer theUpperBound) const; //! Returns true if this location and the location Other //! have the same elementary data, i.e. contain the same @@ -166,9 +167,13 @@ private: #include - -inline Standard_Integer HashCode(const TopLoc_Location& me,const Standard_Integer Upper) { - return me.HashCode(Upper); +//! Computes a hash code for the given location, in the range [1, theUpperBound] +//! @param theLocation the location which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +inline Standard_Integer HashCode (const TopLoc_Location& theLocation, const Standard_Integer theUpperBound) +{ + return theLocation.HashCode (theUpperBound); } inline void ShallowDump(const TopLoc_Location& me,Standard_OStream& S) { diff --git a/src/TopTools/TopTools_OrientedShapeMapHasher.hxx b/src/TopTools/TopTools_OrientedShapeMapHasher.hxx index 1d2c5ce6ee..d4e105e8ae 100644 --- a/src/TopTools/TopTools_OrientedShapeMapHasher.hxx +++ b/src/TopTools/TopTools_OrientedShapeMapHasher.hxx @@ -33,11 +33,12 @@ public: DEFINE_STANDARD_ALLOC - - //! Returns a HasCode value for the Key in the - //! range 0..Upper. - static Standard_Integer HashCode (const TopoDS_Shape& S, const Standard_Integer Upper); - + //! Computes a hash code for the given shape, in the range [1, theUpperBound] + //! @param theShape the shape which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (const TopoDS_Shape& theShape, const Standard_Integer theUpperBound); + //! Returns True when the two keys are equal. Two same //! keys must have the same hashcode, the contrary is //! not necessary. diff --git a/src/TopTools/TopTools_OrientedShapeMapHasher.lxx b/src/TopTools/TopTools_OrientedShapeMapHasher.lxx index 5ef9e1e1f3..6fb47100bd 100644 --- a/src/TopTools/TopTools_OrientedShapeMapHasher.lxx +++ b/src/TopTools/TopTools_OrientedShapeMapHasher.lxx @@ -17,13 +17,13 @@ #include //======================================================================= -//function : HashCode -//purpose : +// function : HashCode +// purpose : //======================================================================= -inline Standard_Integer TopTools_OrientedShapeMapHasher::HashCode - (const TopoDS_Shape& S, const Standard_Integer Upper) +inline Standard_Integer TopTools_OrientedShapeMapHasher::HashCode (const TopoDS_Shape& theShape, + const Standard_Integer theUpperBound) { - return S.HashCode(Upper); + return theShape.HashCode (theUpperBound); } //======================================================================= diff --git a/src/TopTools/TopTools_ShapeMapHasher.hxx b/src/TopTools/TopTools_ShapeMapHasher.hxx index 32c1bc5117..8827ff9e61 100644 --- a/src/TopTools/TopTools_ShapeMapHasher.hxx +++ b/src/TopTools/TopTools_ShapeMapHasher.hxx @@ -33,11 +33,12 @@ public: DEFINE_STANDARD_ALLOC - - //! Returns a HasCode value for the Key in the - //! range 0..Upper. - static Standard_Integer HashCode (const TopoDS_Shape& S, const Standard_Integer Upper); - + //! Computes a hash code for the given shape, in the range [1, theUpperBound] + //! @param theShape the shape which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (const TopoDS_Shape& theShape, Standard_Integer theUpperBound); + //! Returns True when the two keys are the same. Two //! same keys must have the same hashcode, the //! contrary is not necessary. diff --git a/src/TopTools/TopTools_ShapeMapHasher.lxx b/src/TopTools/TopTools_ShapeMapHasher.lxx index d441b29ef0..a3789d085b 100644 --- a/src/TopTools/TopTools_ShapeMapHasher.lxx +++ b/src/TopTools/TopTools_ShapeMapHasher.lxx @@ -17,13 +17,13 @@ #include //======================================================================= -//function : HashCode -//purpose : +// function : HashCode +// purpose : //======================================================================= -inline Standard_Integer TopTools_ShapeMapHasher::HashCode - (const TopoDS_Shape& S, const Standard_Integer Upper) +inline Standard_Integer TopTools_ShapeMapHasher::HashCode (const TopoDS_Shape& theShape, + const Standard_Integer theUpperBound) { - return S.HashCode(Upper); + return theShape.HashCode (theUpperBound); } //======================================================================= diff --git a/src/TopoDS/TopoDS_Shape.cxx b/src/TopoDS/TopoDS_Shape.cxx index 9f91bdd15e..513703947c 100644 --- a/src/TopoDS/TopoDS_Shape.cxx +++ b/src/TopoDS/TopoDS_Shape.cxx @@ -23,14 +23,13 @@ #include //======================================================================= -//function : HashCode -//purpose : +// function : HashCode +// purpose : //======================================================================= -Standard_Integer TopoDS_Shape::HashCode(const Standard_Integer Upper) const +Standard_Integer TopoDS_Shape::HashCode (const Standard_Integer theUpperBound) const { - //PKV - const Standard_Integer aI = (Standard_Integer) ptrdiff_t(myTShape.get()); - const Standard_Integer aHS = ::HashCode(aI,Upper); - const Standard_Integer aHL = myLocation.HashCode(Upper); - return (aHS^aHL)%Upper; -} + // PKV + const Standard_Integer aHS = ::HashCode (myTShape.get(), theUpperBound); + const Standard_Integer aHL = myLocation.HashCode (theUpperBound); + return ::HashCode (aHS ^ aHL, theUpperBound); +} diff --git a/src/TopoDS/TopoDS_Shape.hxx b/src/TopoDS/TopoDS_Shape.hxx index 39134655c8..52fadaa111 100644 --- a/src/TopoDS/TopoDS_Shape.hxx +++ b/src/TopoDS/TopoDS_Shape.hxx @@ -232,11 +232,11 @@ public: Standard_Boolean IsNotEqual (const TopoDS_Shape& theOther) const { return !IsEqual (theOther); } Standard_Boolean operator != (const TopoDS_Shape& theOther) const { return IsNotEqual (theOther); } - //! Returns a hashed value denoting . This value - //! is in the range 1... It is computed from - //! the TShape and the Location. The Orientation is - //! not used. - Standard_EXPORT Standard_Integer HashCode (const Standard_Integer Upper) const; + //! Returns a hashed value denoting . This value is in the range [1, theUpperBound]. It is computed from the + //! TShape and the Location. The Orientation is not used. + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + Standard_EXPORT Standard_Integer HashCode (Standard_Integer theUpperBound) const; //! Replace by a new Shape with the same //! Orientation and Location and a new TShape with the @@ -263,8 +263,13 @@ private: }; -inline Standard_Integer HashCode(const TopoDS_Shape& me,const Standard_Integer Upper) { - return me.HashCode(Upper); +//! Computes a hash code for the given shape, in the range [1, theUpperBound] +//! @param theShape the shape which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +inline Standard_Integer HashCode (const TopoDS_Shape& theShape, const Standard_Integer theUpperBound) +{ + return theShape.HashCode (theUpperBound); } #endif // _TopoDS_Shape_HeaderFile diff --git a/src/Transfer/Transfer_FindHasher.cxx b/src/Transfer/Transfer_FindHasher.cxx index f81a5feb15..1bcf614d3c 100644 --- a/src/Transfer/Transfer_FindHasher.cxx +++ b/src/Transfer/Transfer_FindHasher.cxx @@ -15,10 +15,15 @@ #include #include -Standard_Integer Transfer_FindHasher::HashCode - (const Handle(Transfer_Finder)& K, const Standard_Integer Upper) + +//============================================================================ +// function : HashCode +// purpose : +//============================================================================ +Standard_Integer Transfer_FindHasher::HashCode (const Handle (Transfer_Finder) & theFinder, + const Standard_Integer theUpperBound) { - return ((K->GetHashCode() - 1) % Upper) + 1; + return ::HashCode (theFinder->GetHashCode() - 1, theUpperBound); } Standard_Boolean Transfer_FindHasher::IsEqual diff --git a/src/Transfer/Transfer_FindHasher.hxx b/src/Transfer/Transfer_FindHasher.hxx index 6a9c450995..01e936a5df 100644 --- a/src/Transfer/Transfer_FindHasher.hxx +++ b/src/Transfer/Transfer_FindHasher.hxx @@ -39,11 +39,14 @@ public: DEFINE_STANDARD_ALLOC - //! Returns a HashCode in the range <0,Upper> for a Finder : - //! asks the Finder its HashCode then transforms it to be in the - //! required range - Standard_EXPORT static Standard_Integer HashCode (const Handle(Transfer_Finder)& K, const Standard_Integer Upper); - + //! Returns hash code for the given finder, in the range [1, theUpperBound]. + //! Asks the finder its hash code, then transforms it to be in the required range + //! @param theFinder the finder which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + Standard_EXPORT static Standard_Integer HashCode (const Handle (Transfer_Finder) & theFinder, + Standard_Integer theUpperBound); + //! Returns True if two keys are the same. //! The test does not work on the Finders themselves but by //! calling their methods Equates diff --git a/src/VrmlData/VrmlData_Node.cxx b/src/VrmlData/VrmlData_Node.cxx index 68cd03bfc0..57f0c984e3 100644 --- a/src/VrmlData/VrmlData_Node.cxx +++ b/src/VrmlData/VrmlData_Node.cxx @@ -51,15 +51,12 @@ Standard_Boolean IsEqual (const Handle(VrmlData_Node)& theOne, } //======================================================================= -//function : HashCode -//purpose : Global method +// function : HashCode +// purpose : Global method //======================================================================= - -Standard_Integer HashCode(const Handle(VrmlData_Node)& theNode, - const Standard_Integer theUpper) +Standard_Integer HashCode (const Handle (VrmlData_Node) & theNode, const Standard_Integer theUpperBound) { - return (theNode->Name() == 0L ? 0 - : HashCode((Standard_CString)theNode->Name(), theUpper)); + return (theNode->Name () == NULL ? 1 : HashCode (theNode->Name (), theUpperBound)); } //======================================================================= diff --git a/src/VrmlData/VrmlData_Node.hxx b/src/VrmlData/VrmlData_Node.hxx index aeda75b40e..d289d96fd5 100644 --- a/src/VrmlData/VrmlData_Node.hxx +++ b/src/VrmlData/VrmlData_Node.hxx @@ -201,8 +201,12 @@ class VrmlData_Node : public Standard_Transient // Definition of HANDLE object using Standard_DefineHandle.hxx DEFINE_STANDARD_HANDLE (VrmlData_Node, Standard_Transient) -Standard_EXPORT Standard_Integer HashCode(const Handle(VrmlData_Node)& theNode, - const Standard_Integer theUpper); +//! Computes a hash code for the given VRML node, in the range [1, theUpperBound] +//! @param theNode the VRML node which hash code is to be computed +//! @param theUpperBound the upper bound of the range a computing hash code must be within +//! @return a computed hash code, in the range [1, theUpperBound] +Standard_EXPORT Standard_Integer HashCode (const Handle (VrmlData_Node) & theNode, Standard_Integer theUpperBound); + Standard_EXPORT Standard_Boolean IsEqual (const Handle(VrmlData_Node)& theOne, const Handle(VrmlData_Node)& theTwo); diff --git a/src/XCAFDoc/XCAFDoc_AssemblyItemId.hxx b/src/XCAFDoc/XCAFDoc_AssemblyItemId.hxx index 941c53a761..67a4024830 100644 --- a/src/XCAFDoc/XCAFDoc_AssemblyItemId.hxx +++ b/src/XCAFDoc/XCAFDoc_AssemblyItemId.hxx @@ -79,10 +79,15 @@ public: struct Hasher { - static int HashCode(const XCAFDoc_AssemblyItemId& theItem, - const int upper) + + //! Computes a hash code for the given value of the XCAFDoc_AssemblyItemId, in range [1, theUpperBound] + //! @param theAssemblyItemId the value of the XCAFDoc_AssemblyItemId type which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in range [1, theUpperBound] + static Standard_Integer HashCode (const XCAFDoc_AssemblyItemId& theAssemblyItemId, + const Standard_Integer theUpperBound) { - return ::HashCode(theItem.ToString(), upper); + return ::HashCode (theAssemblyItemId.ToString(), theUpperBound); } static int IsEqual(const XCAFDoc_AssemblyItemId& theItem1, diff --git a/src/XCAFPrs/XCAFPrs_Style.hxx b/src/XCAFPrs/XCAFPrs_Style.hxx index 47a6d3118a..214a0bdcfa 100644 --- a/src/XCAFPrs/XCAFPrs_Style.hxx +++ b/src/XCAFPrs/XCAFPrs_Style.hxx @@ -92,25 +92,27 @@ public: return IsEqual (theOther); } - //! Returns a HasCode value. - static Standard_Integer HashCode (const XCAFPrs_Style& theStyle, - const Standard_Integer theUpper) + //! Computes a hash code for the given set of styling settings, in the range [1, theUpperBound] + //! @param theStyle the set of styling settings which hash code is to be computed + //! @param theUpperBound the upper bound of the range a computing hash code must be within + //! @return a computed hash code, in the range [1, theUpperBound] + static Standard_Integer HashCode (const XCAFPrs_Style& theStyle, const Standard_Integer theUpperBound) { if (!theStyle.myIsVisible) { return 1; } - int aHashCode = 0; + Standard_Integer aHashCode = 0; if (theStyle.myHasColorSurf) { - aHashCode = aHashCode ^ Quantity_ColorRGBAHasher::HashCode (theStyle.myColorSurf, theUpper); + aHashCode = aHashCode ^ Quantity_ColorRGBAHasher::HashCode (theStyle.myColorSurf, theUpperBound); } if (theStyle.myHasColorCurv) { - aHashCode = aHashCode ^ Quantity_ColorHasher::HashCode (theStyle.myColorCurv, theUpper); + aHashCode = aHashCode ^ Quantity_ColorHasher::HashCode (theStyle.myColorCurv, theUpperBound); } - return ((aHashCode & 0x7fffffff) % theUpper) + 1; + return ::HashCode (aHashCode, theUpperBound); } //! Returns True when the two keys are the same. -- 2.20.1