mirror of
https://git.dev.opencascade.org/repos/occt.git
synced 2025-04-03 17:56:21 +03:00
Foundation Classes - Separate map's prime declaration #211
Refactor TCollection to use NCollection_Primes and remove obsolete TCollection.cxx New prime declaration now will be more clear to use.
This commit is contained in:
parent
ca688cfac3
commit
1b4bb62150
@ -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
|
||||
|
@ -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 );
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
@ -13,7 +13,6 @@
|
||||
|
||||
#include <NCollection_BasePointerVector.hxx>
|
||||
|
||||
#include <TCollection.hxx>
|
||||
#include <cstring>
|
||||
|
||||
//=======================================================================
|
||||
|
47
src/NCollection/NCollection_Primes.cxx
Normal file
47
src/NCollection/NCollection_Primes.cxx
Normal file
@ -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;
|
||||
}
|
37
src/NCollection/NCollection_Primes.hxx
Normal file
37
src/NCollection/NCollection_Primes.hxx
Normal file
@ -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
|
@ -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())
|
||||
|
@ -1,4 +1,3 @@
|
||||
TCollection.cxx
|
||||
TCollection.hxx
|
||||
TCollection_AsciiString.cxx
|
||||
TCollection_AsciiString.hxx
|
||||
|
@ -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");
|
||||
}
|
@ -17,13 +17,15 @@
|
||||
#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);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user