Skip to content

Commit

Permalink
Foundation Classes - Update Map to store insert order
Browse files Browse the repository at this point in the history
Changes to keep the insert order to improve iteration procedure.
Making TCollection deprecated as a not clear definition.
  • Loading branch information
dpasukhi committed Nov 2, 2024
1 parent 2d9c5a8 commit 7172c16
Show file tree
Hide file tree
Showing 12 changed files with 301 additions and 227 deletions.
2 changes: 2 additions & 0 deletions src/NCollection/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ NCollection_Map.hxx
NCollection_Mat3.hxx
NCollection_Mat4.hxx
NCollection_OccAllocator.hxx
NCollection_Primes.hxx
NCollection_SeqNode.hxx
NCollection_Sequence.hxx
NCollection_Shared.hxx
NCollection_SparseArray.hxx
Expand Down
67 changes: 43 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,46 @@ 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;
myData1 = (NCollection_ListNode**)Standard::Reallocate(myData1, (theNbBuckets + 1) * sizeof(NCollection_ListNode*));
memset(myData1, 0, aNewBuckets * sizeof(NCollection_ListNode*));
if (isDouble)
{
myData2 = (NCollection_ListNode**)Standard::Reallocate(myData2, (theNbBuckets + 1) * sizeof(NCollection_ListNode*));
memset(myData2, 0, aNewBuckets * 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 +126,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 +197,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 );
}

26 changes: 14 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 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
20 changes: 1 addition & 19 deletions src/NCollection/NCollection_BaseSequence.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,7 @@
#include <Standard.hxx>
#include <NCollection_BaseAllocator.hxx>
#include <NCollection_DefineAlloc.hxx>

// **************************************** Class SeqNode ********************

class NCollection_SeqNode
{
public:
// define new operator for use with NCollection allocators
DEFINE_NCOLLECTION_ALLOC
public:
NCollection_SeqNode () : myNext (NULL), myPrevious (NULL) {}
NCollection_SeqNode * Next () const { return myNext; }
NCollection_SeqNode * Previous () const { return myPrevious; }
void SetNext (NCollection_SeqNode * theNext) { myNext = theNext; }
void SetPrevious (NCollection_SeqNode * thePrev) { myPrevious = thePrev; }

private:
NCollection_SeqNode* myNext;
NCollection_SeqNode* myPrevious;
};
#include <NCollection_SeqNode.hxx>

typedef void (* NCollection_DelSeqNode)
(NCollection_SeqNode*, Handle(NCollection_BaseAllocator)& theAl);
Expand Down
Loading

0 comments on commit 7172c16

Please sign in to comment.