]> OCCT Git - occt.git/commitdiff
Foundation Classes - Separate map's prime declaration #211
authordpasukhi <dpasukhi@opencascade.com>
Thu, 26 Dec 2024 22:33:34 +0000 (22:33 +0000)
committerdpasukhi <dpasukhi@opencascade.com>
Fri, 27 Dec 2024 10:40:54 +0000 (10:40 +0000)
Refactor TCollection to use NCollection_Primes and remove obsolete TCollection.cxx
New prime declaration now will be more clear to use.

src/NCollection/FILES
src/NCollection/NCollection_BaseMap.cxx
src/NCollection/NCollection_BaseMap.hxx
src/NCollection/NCollection_BasePointerVector.cxx
src/NCollection/NCollection_Primes.cxx [new file with mode: 0644]
src/NCollection/NCollection_Primes.hxx [new file with mode: 0644]
src/TColStd/TColStd_PackedMapOfInteger.cxx
src/TCollection/FILES
src/TCollection/TCollection.cxx [deleted file]
src/TCollection/TCollection.hxx

index d93ebc08fbe36aaa5db3fabcfbfa3c23d52a8c57..9ddad29b4b796193acdfd472bd9b49e1a9cdcf75 100755 (executable)
@@ -49,6 +49,8 @@ NCollection_Map.hxx
 NCollection_Mat3.hxx
 NCollection_Mat4.hxx
 NCollection_OccAllocator.hxx
+NCollection_Primes.cxx
+NCollection_Primes.hxx
 NCollection_Sequence.hxx
 NCollection_Shared.hxx
 NCollection_SparseArray.hxx
index 7f32f332b30ce79cd057588bd7577b9bca353a1a..8d87f82d6be79813968dc803825a71ec7d223b77 100644 (file)
@@ -16,7 +16,7 @@
 // Purpose:     Implementation of the BaseMap class
 
 #include <NCollection_BaseMap.hxx>
-#include <TCollection.hxx>
+#include <NCollection_Primes.hxx>
 
 //=======================================================================
 //function : BeginResize
@@ -46,7 +46,7 @@ Standard_Boolean  NCollection_BaseMap::BeginResize
       Standard::Allocate((N+1)*sizeof(NCollection_ListNode *));
   }
   else
-    data2 = NULL;
+    data2 = nullptr;
   return Standard_True;
 }
 
@@ -71,7 +71,6 @@ void  NCollection_BaseMap::EndResize
   myData2 = data2;
 }
 
-
 //=======================================================================
 //function : Destroy
 //purpose  : 
@@ -101,16 +100,15 @@ void  NCollection_BaseMap::Destroy (NCollection_DelMapNode fDel,
     {
       memset(myData2, 0, (aNbBuckets + 1) * sizeof(NCollection_ListNode*));
     }
+    mySize = 0;
   }
-
-  mySize = 0;
   if (doReleaseMemory)
   {
     if (myData1)
       Standard::Free(myData1);
     if (myData2)
       Standard::Free(myData2);
-    myData1 = myData2 = NULL;
+    myData1 = myData2 = nullptr;
   }
 }
 
@@ -175,6 +173,5 @@ void NCollection_BaseMap::Statistics(Standard_OStream& S) const
 Standard_Integer NCollection_BaseMap::NextPrimeForMap
   (const Standard_Integer N) const
 {
-  return TCollection::NextPrimeForMap ( N );
+  return NCollection_Primes::NextPrimeForMap ( N );
 }
-
index 07e530db08f362495ac023d1228dea20a38b885c..8ead95ded7887bb99a0fa618f5af3aaa6797a423 100644 (file)
@@ -49,16 +49,16 @@ public:
     //! Empty constructor
     Iterator (void) :
       myNbBuckets (0),
-      myBuckets   (NULL),
+      myBuckets   (nullptr),
       myBucket    (0),
-      myNode      (NULL) {}
+      myNode      (nullptr) {}
     
     //! Constructor
     Iterator (const NCollection_BaseMap& theMap) :
       myNbBuckets (theMap.myNbBuckets),
       myBuckets   (theMap.myData1),
       myBucket    (-1),
-      myNode      (NULL)
+      myNode      (nullptr)
     {
       if (!myBuckets) 
         myNbBuckets = -1;
@@ -78,7 +78,7 @@ public:
       myNbBuckets = theMap.myNbBuckets;
       myBuckets = theMap.myData1;
       myBucket = -1;
-      myNode = NULL;
+      myNode = nullptr;
       if (!myBuckets) 
         myNbBuckets = -1;
       PNext();
@@ -88,7 +88,7 @@ public:
     void Reset (void)
     {
       myBucket = -1;
-      myNode = NULL;
+      myNode = nullptr;
       PNext();
     }
     
@@ -101,7 +101,7 @@ public:
   protected:
     //! PMore
     Standard_Boolean PMore (void) const
-    { return (myNode != NULL); }
+    { return (myNode != nullptr); }
     
     //! PNext
     void PNext (void)
@@ -161,8 +161,8 @@ public:
                        const Standard_Boolean single,
                        const Handle(NCollection_BaseAllocator)& theAllocator) :
     myAllocator(theAllocator.IsNull() ? NCollection_BaseAllocator::CommonBaseAllocator() : theAllocator),
-    myData1(NULL),
-    myData2(NULL),
+    myData1(nullptr),
+    myData2(nullptr),
     myNbBuckets(NbBuckets),
     mySize(0),
     isDouble(!single)
index 55a343944835be820185230dba91fe4006d7c08a..dce82004231a3a515742a109b86997c811421c36 100644 (file)
@@ -13,7 +13,6 @@
 
 #include <NCollection_BasePointerVector.hxx>
 
-#include <TCollection.hxx>
 #include <cstring>
 
 //=======================================================================
diff --git a/src/NCollection/NCollection_Primes.cxx b/src/NCollection/NCollection_Primes.cxx
new file mode 100644 (file)
index 0000000..d161497
--- /dev/null
@@ -0,0 +1,47 @@
+// Copyright (c) 2024 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.
+
+#include <NCollection_Primes.hxx>
+
+#include <Standard_OutOfRange.hxx>
+
+#include <algorithm>
+#include <array>
+
+namespace
+{
+  // The array of prime numbers used as consecutive steps for
+  // size of array of buckets in the map.
+  // The prime numbers are used for array size with the hope that this will
+  // lead to less probability of having the same hash codes for
+  // different map items (note that all hash codes are modulo that size).
+  // The value of each next step is chosen to be ~2 times greater than previous.
+  // Though this could be thought as too much, actually the amount of
+  // memory overhead in that case is only ~15% as compared with total size of
+  // all auxiliary data structures (each map node takes ~24 bytes),
+  // and this proves to pay off in performance (see OCC13189).
+  constexpr std::array<int, 24> THE_PRIME_VECTOR = {101,      1009,      2003,      5003,      10007,      20011,
+                                                    37003,    57037,     65003,     100019,    209953,     472393,
+                                                    995329,   2359297,   4478977,   9437185,   17915905,   35831809,
+                                                    71663617, 150994945, 301989889, 573308929, 1019215873, 2038431745};
+}
+
+int NCollection_Primes::NextPrimeForMap(const int theN)
+{
+  auto aResult = std::lower_bound(THE_PRIME_VECTOR.begin(), THE_PRIME_VECTOR.end(), theN + 1);
+  if (aResult == THE_PRIME_VECTOR.end())
+  {
+    throw Standard_OutOfRange("NCollection_Primes::NextPrimeForMap() - requested too big size");
+  }
+  return *aResult;
+}
diff --git a/src/NCollection/NCollection_Primes.hxx b/src/NCollection/NCollection_Primes.hxx
new file mode 100644 (file)
index 0000000..c95f807
--- /dev/null
@@ -0,0 +1,37 @@
+// Copyright (c) 2024 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 _NCollection_Primes_HeaderFile
+#define _NCollection_Primes_HeaderFile
+
+#include <Standard_Macro.hxx>
+
+//! Namespace provides a collection of prime numbers.
+//!
+//! This namespace is used to store a collection of prime numbers that are used as
+//! consecutive steps for the size of an array of buckets in a map. The prime
+//! numbers are chosen to minimize the probability of having the same hash codes
+//! for different map items. The namespace also provides a method to find the next
+//! prime number greater than or equal to a given number.
+//!
+//! The following are Pierpont primes, prime numbers of the form 2^u * 3^v + 1:
+//! 101, 1009, 2003, 5003, 10007, 20011, 37003, 57037, 65003, 100019, 209953, 472393,
+//! 995329, 2359297, 4478977, 9437185, 17915905, 35831809, 71663617, 150994945,
+//! 301989889, 573308929, 1019215873, 2038431745
+namespace NCollection_Primes
+{
+  //! Returns the next prime number greater than or equal to theN.
+  Standard_EXPORT int NextPrimeForMap(const int theN);
+};
+
+#endif // _NCollection_Primes_HeaderFile
\ No newline at end of file
index 55fa98ad8ab1ee604636256f2738feb0c9593a50..5d9cb4264c3f400e9b76b2c01df5d2e85b10faa5 100644 (file)
@@ -16,7 +16,7 @@
 #include <TColStd_PackedMapOfInteger.hxx>
 
 #include <NCollection_Array1.hxx>
-#include <TCollection.hxx>
+#include <NCollection_Primes.hxx>
 
 namespace
 {
@@ -153,7 +153,7 @@ TColStd_PackedMapOfInteger& TColStd_PackedMapOfInteger::Assign
 
 void TColStd_PackedMapOfInteger::ReSize (const Standard_Integer theNbBuckets)
 {
-  Standard_Integer aNewBuck = TCollection::NextPrimeForMap (theNbBuckets);
+  Standard_Integer aNewBuck = NCollection_Primes::NextPrimeForMap (theNbBuckets);
   if (aNewBuck <= myNbBuckets)
   {
     if (!IsEmpty())
index e9d85856a8561c2bec600c14cad8913a3c0f18cd..e25280fb4f2f5eb69e43a7f94e63b8316b16000c 100755 (executable)
@@ -1,4 +1,3 @@
-TCollection.cxx
 TCollection.hxx
 TCollection_AsciiString.cxx
 TCollection_AsciiString.hxx
diff --git a/src/TCollection/TCollection.cxx b/src/TCollection/TCollection.cxx
deleted file mode 100644 (file)
index e41ec35..0000000
+++ /dev/null
@@ -1,74 +0,0 @@
-// Created on: 1993-01-14
-// Created by: Remi LEQUETTE
-// Copyright (c) 1993-1999 Matra Datavision
-// Copyright (c) 1999-2014 OPEN CASCADE SAS
-//
-// This file is part of Open CASCADE Technology software library.
-//
-// This library is free software; you can redistribute it and/or modify it under
-// the terms of the GNU Lesser General Public License version 2.1 as published
-// by the Free Software Foundation, with special exception defined in the file
-// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
-// distribution for complete text of the license and disclaimer of any warranty.
-//
-// Alternatively, this file may be used under the terms of Open CASCADE
-// commercial license or contractual agreement.
-
-#include <TCollection.hxx>
-
-#include <Standard_OutOfRange.hxx>
-
-// The array of prime numbers used as consecutive steps for
-// size of array of buckets in the map.
-// The prime numbers are used for array size with the hope that this will 
-// lead to less probability of having the same hash codes for
-// different map items (note that all hash codes are modulo that size).
-// The value of each next step is chosen to be ~2 times greater than previous.
-// Though this could be thought as too much, actually the amount of 
-// memory overhead in that case is only ~15% as compared with total size of
-// all auxiliary data structures (each map node takes ~24 bytes), 
-// and this proves to pay off in performance (see OCC13189).
-#define THE_NB_PRIMES 24
-static const Standard_Integer THE_TCollection_Primes[THE_NB_PRIMES] =
-{
-         101,
-        1009,
-        2003,
-        5003,
-       10007,
-       20011,
-       37003,
-       57037,
-       65003,
-      100019,
-      209953, // The following are Pierpont primes [List of prime numbers]
-      472393,
-      995329,
-     2359297,
-     4478977,
-     9437185,
-    17915905,
-    35831809,
-    71663617,
-   150994945,
-   301989889,
-   573308929,
-  1019215873,
-  2038431745
-};
-
-// =======================================================================
-// function : NextPrimeForMap
-// purpose  :
-// =======================================================================
-Standard_Integer TCollection::NextPrimeForMap(const Standard_Integer N)
-{
-  for (Standard_Integer aPrimeIter = 0; aPrimeIter < THE_NB_PRIMES; ++aPrimeIter)
-  {
-    if (THE_TCollection_Primes[aPrimeIter] > N)
-    {
-      return THE_TCollection_Primes[aPrimeIter];
-    }
-  }
-  throw Standard_OutOfRange ("TCollection::NextPrimeForMap() - requested too big size");
-}
index 9867b943b9c51847fdcdca2f9a09489c3e1371f1..4e0903ba2eb1bb5904a08ea2ce6ee791324dffd3 100644 (file)
 #ifndef _TCollection_HeaderFile
 #define _TCollection_HeaderFile
 
+#include <NCollection_Primes.hxx>
+
 #include <Standard.hxx>
 #include <Standard_DefineAlloc.hxx>
 #include <Standard_Handle.hxx>
 
 //! The package <TCollection> provides the services for the
 //! transient basic data structures.
-class TCollection 
+class Standard_DEPRECATED("Deprecated since OCCT 7.9, NCollection_Primes should be used instead of TCollection") TCollection
 {
 public:
 
@@ -35,7 +37,10 @@ public:
   //! around 1 000 000). This is not a limit of the number of
   //! items but a limit in the number  of buckets.  i.e.
   //! there will be more collisions  in  the map.
-  Standard_EXPORT static Standard_Integer NextPrimeForMap (const Standard_Integer I);
+  static Standard_Integer NextPrimeForMap (const Standard_Integer I)
+  {
+    return NCollection_Primes::NextPrimeForMap(I);
+  }
 
 };