Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Foundation Classes - Update Map to store insert order #140

Draft
wants to merge 7 commits into
base: IR
Choose a base branch
from
1 change: 1 addition & 0 deletions src/NCollection/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ NCollection_Map.hxx
NCollection_Mat3.hxx
NCollection_Mat4.hxx
NCollection_OccAllocator.hxx
NCollection_Primes.hxx
NCollection_Sequence.hxx
NCollection_Shared.hxx
NCollection_SparseArray.hxx
Expand Down
68 changes: 44 additions & 24 deletions src/NCollection/NCollection_BaseMap.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// Purpose: Implementation of the BaseMap class

#include <NCollection_BaseMap.hxx>
#include <TCollection.hxx>
#include <NCollection_Primes.hxx>

//=======================================================================
//function : BeginResize
Expand All @@ -30,7 +30,7 @@ Standard_Boolean NCollection_BaseMap::BeginResize
NCollection_ListNode**& data2) const
{
// get next size for the buckets array
N = NextPrimeForMap(NbBuckets);
N = NCollection_Primes::NextPrimeForMap(NbBuckets);
if (N <= myNbBuckets)
{
if (!myData1)
Expand All @@ -46,7 +46,7 @@ Standard_Boolean NCollection_BaseMap::BeginResize
Standard::Allocate((N+1)*sizeof(NCollection_ListNode *));
}
else
data2 = NULL;
data2 = nullptr;
return Standard_True;
}

Expand All @@ -71,14 +71,47 @@ void NCollection_BaseMap::EndResize
myData2 = data2;
}


//=======================================================================
//function : Destroy
//purpose :
//function : Reallocate
//purpose :
//=======================================================================
Standard_Boolean NCollection_BaseMap::Reallocate(const Standard_Integer theNbBuckets)
{
// get next size for the buckets array
Standard_Integer aNewBuckets = NCollection_Primes::NextPrimeForMap(theNbBuckets);
if (aNewBuckets <= myNbBuckets)
{
if (!myData1)
{
aNewBuckets = myNbBuckets;
}
else
{
return Standard_False;
}
}
myNbBuckets = aNewBuckets;
const size_t aSize = myNbBuckets + 1;
myData1 = (NCollection_ListNode**)Standard::Reallocate(myData1, aSize * sizeof(NCollection_ListNode*));
memset(myData1, 0, aSize * sizeof(NCollection_ListNode*));
if (isDouble)
{
myData2 = (NCollection_ListNode**)Standard::Reallocate(myData2, aSize * sizeof(NCollection_ListNode*));
memset(myData2, 0, aSize * sizeof(NCollection_ListNode*));
}
else
{
myData2 = nullptr;
}
return Standard_True;
}

//=======================================================================
//function : Destroy
//purpose :
//=======================================================================

void NCollection_BaseMap::Destroy (NCollection_DelMapNode fDel,
Standard_Boolean doReleaseMemory)
void NCollection_BaseMap::Destroy(NCollection_DelMapNode fDel, Standard_Boolean doReleaseMemory)
{
if (!IsEmpty())
{
Expand All @@ -94,23 +127,22 @@ void NCollection_BaseMap::Destroy (NCollection_DelMapNode fDel,
fDel (aCur, myAllocator);
aCur = aNext;
}
myData1[anInd] = nullptr;
myData1[anInd] = nullptr;
}
}
if (myData2)
{
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;
}
}

Expand Down Expand Up @@ -166,15 +198,3 @@ void NCollection_BaseMap::Statistics(Standard_OStream& S) const

delete [] sizes;
}

//=======================================================================
//function : NextPrimeForMap
//purpose :
//=======================================================================

Standard_Integer NCollection_BaseMap::NextPrimeForMap
(const Standard_Integer N) const
{
return TCollection::NextPrimeForMap ( N );
}

28 changes: 16 additions & 12 deletions src/NCollection/NCollection_BaseMap.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -78,7 +78,7 @@ public:
myNbBuckets = theMap.myNbBuckets;
myBuckets = theMap.myData1;
myBucket = -1;
myNode = NULL;
myNode = nullptr;
if (!myBuckets)
myNbBuckets = -1;
PNext();
Expand All @@ -88,7 +88,7 @@ public:
void Reset (void)
{
myBucket = -1;
myNode = NULL;
myNode = nullptr;
PNext();
}

Expand All @@ -101,7 +101,7 @@ public:
protected:
//! PMore
Standard_Boolean PMore (void) const
{ return (myNode != NULL); }
{ return (myNode != nullptr); }

//! PNext
void PNext (void)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -200,6 +200,12 @@ public:
NCollection_ListNode** data1,
NCollection_ListNode** data2);

//! Reallocate the existed data containers.
//! Filling operation must to be done outside.
//! Reallocated memory will be cleared (all elements will be set to nullptr).
Standard_EXPORT Standard_Boolean Reallocate
(const Standard_Integer theNbBuckets);

//! Resizable
Standard_Boolean Resizable() const
{ return IsEmpty() || (mySize > myNbBuckets); }
Expand All @@ -214,10 +220,6 @@ public:
Standard_EXPORT void Destroy(NCollection_DelMapNode fDel,
Standard_Boolean doReleaseMemory = Standard_True);

//! NextPrimeForMap
Standard_EXPORT Standard_Integer NextPrimeForMap
(const Standard_Integer N) const;

//! Exchange content of two maps without data copying
void exchangeMapsData (NCollection_BaseMap& theOther)
{
Expand All @@ -243,6 +245,8 @@ public:
NCollection_ListNode ** myData1;
NCollection_ListNode ** myData2;

void resetSize() { mySize = 0; }

private:
// ---------- PRIVATE FIELDS ------------
Standard_Integer myNbBuckets;
Expand Down
1 change: 0 additions & 1 deletion src/NCollection/NCollection_BasePointerVector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

#include <NCollection_BasePointerVector.hxx>

#include <TCollection.hxx>
#include <cstring>

//=======================================================================
Expand Down
Loading
Loading