Skip to content

Commit 7172c16

Browse files
committed
Foundation Classes - Update Map to store insert order
Changes to keep the insert order to improve iteration procedure. Making TCollection deprecated as a not clear definition.
1 parent 2d9c5a8 commit 7172c16

12 files changed

+301
-227
lines changed

src/NCollection/FILES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ NCollection_Map.hxx
4949
NCollection_Mat3.hxx
5050
NCollection_Mat4.hxx
5151
NCollection_OccAllocator.hxx
52+
NCollection_Primes.hxx
53+
NCollection_SeqNode.hxx
5254
NCollection_Sequence.hxx
5355
NCollection_Shared.hxx
5456
NCollection_SparseArray.hxx

src/NCollection/NCollection_BaseMap.cxx

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// Purpose: Implementation of the BaseMap class
1717

1818
#include <NCollection_BaseMap.hxx>
19-
#include <TCollection.hxx>
19+
#include <NCollection_Primes.hxx>
2020

2121
//=======================================================================
2222
//function : BeginResize
@@ -30,7 +30,7 @@ Standard_Boolean NCollection_BaseMap::BeginResize
3030
NCollection_ListNode**& data2) const
3131
{
3232
// get next size for the buckets array
33-
N = NextPrimeForMap(NbBuckets);
33+
N = NCollection_Primes::NextPrimeForMap(NbBuckets);
3434
if (N <= myNbBuckets)
3535
{
3636
if (!myData1)
@@ -46,7 +46,7 @@ Standard_Boolean NCollection_BaseMap::BeginResize
4646
Standard::Allocate((N+1)*sizeof(NCollection_ListNode *));
4747
}
4848
else
49-
data2 = NULL;
49+
data2 = nullptr;
5050
return Standard_True;
5151
}
5252

@@ -71,14 +71,46 @@ void NCollection_BaseMap::EndResize
7171
myData2 = data2;
7272
}
7373

74-
7574
//=======================================================================
76-
//function : Destroy
77-
//purpose :
75+
//function : Reallocate
76+
//purpose :
7877
//=======================================================================
78+
Standard_Boolean NCollection_BaseMap::Reallocate(const Standard_Integer theNbBuckets)
79+
{
80+
// get next size for the buckets array
81+
Standard_Integer aNewBuckets = NCollection_Primes::NextPrimeForMap(theNbBuckets);
82+
if (aNewBuckets <= myNbBuckets)
83+
{
84+
if (!myData1)
85+
{
86+
aNewBuckets = myNbBuckets;
87+
}
88+
else
89+
{
90+
return Standard_False;
91+
}
92+
}
93+
myNbBuckets = aNewBuckets;
94+
myData1 = (NCollection_ListNode**)Standard::Reallocate(myData1, (theNbBuckets + 1) * sizeof(NCollection_ListNode*));
95+
memset(myData1, 0, aNewBuckets * sizeof(NCollection_ListNode*));
96+
if (isDouble)
97+
{
98+
myData2 = (NCollection_ListNode**)Standard::Reallocate(myData2, (theNbBuckets + 1) * sizeof(NCollection_ListNode*));
99+
memset(myData2, 0, aNewBuckets * sizeof(NCollection_ListNode*));
100+
}
101+
else
102+
{
103+
myData2 = nullptr;
104+
}
105+
return Standard_True;
106+
}
107+
108+
//=======================================================================
109+
//function : Destroy
110+
//purpose :
111+
//=======================================================================
79112

80-
void NCollection_BaseMap::Destroy (NCollection_DelMapNode fDel,
81-
Standard_Boolean doReleaseMemory)
113+
void NCollection_BaseMap::Destroy(NCollection_DelMapNode fDel, Standard_Boolean doReleaseMemory)
82114
{
83115
if (!IsEmpty())
84116
{
@@ -94,23 +126,22 @@ void NCollection_BaseMap::Destroy (NCollection_DelMapNode fDel,
94126
fDel (aCur, myAllocator);
95127
aCur = aNext;
96128
}
97-
myData1[anInd] = nullptr;
129+
myData1[anInd] = nullptr;
98130
}
99131
}
100132
if (myData2)
101133
{
102134
memset(myData2, 0, (aNbBuckets + 1) * sizeof(NCollection_ListNode*));
103135
}
136+
mySize = 0;
104137
}
105-
106-
mySize = 0;
107138
if (doReleaseMemory)
108139
{
109140
if (myData1)
110141
Standard::Free(myData1);
111142
if (myData2)
112143
Standard::Free(myData2);
113-
myData1 = myData2 = NULL;
144+
myData1 = myData2 = nullptr;
114145
}
115146
}
116147

@@ -166,15 +197,3 @@ void NCollection_BaseMap::Statistics(Standard_OStream& S) const
166197

167198
delete [] sizes;
168199
}
169-
170-
//=======================================================================
171-
//function : NextPrimeForMap
172-
//purpose :
173-
//=======================================================================
174-
175-
Standard_Integer NCollection_BaseMap::NextPrimeForMap
176-
(const Standard_Integer N) const
177-
{
178-
return TCollection::NextPrimeForMap ( N );
179-
}
180-

src/NCollection/NCollection_BaseMap.hxx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ public:
4949
//! Empty constructor
5050
Iterator (void) :
5151
myNbBuckets (0),
52-
myBuckets (NULL),
52+
myBuckets (nullptr),
5353
myBucket (0),
54-
myNode (NULL) {}
54+
myNode (nullptr) {}
5555

5656
//! Constructor
5757
Iterator (const NCollection_BaseMap& theMap) :
5858
myNbBuckets (theMap.myNbBuckets),
5959
myBuckets (theMap.myData1),
6060
myBucket (-1),
61-
myNode (NULL)
61+
myNode (nullptr)
6262
{
6363
if (!myBuckets)
6464
myNbBuckets = -1;
@@ -78,7 +78,7 @@ public:
7878
myNbBuckets = theMap.myNbBuckets;
7979
myBuckets = theMap.myData1;
8080
myBucket = -1;
81-
myNode = NULL;
81+
myNode = nullptr;
8282
if (!myBuckets)
8383
myNbBuckets = -1;
8484
PNext();
@@ -88,7 +88,7 @@ public:
8888
void Reset (void)
8989
{
9090
myBucket = -1;
91-
myNode = NULL;
91+
myNode = nullptr;
9292
PNext();
9393
}
9494

@@ -101,7 +101,7 @@ public:
101101
protected:
102102
//! PMore
103103
Standard_Boolean PMore (void) const
104-
{ return (myNode != NULL); }
104+
{ return (myNode != nullptr); }
105105

106106
//! PNext
107107
void PNext (void)
@@ -161,8 +161,8 @@ public:
161161
const Standard_Boolean single,
162162
const Handle(NCollection_BaseAllocator)& theAllocator) :
163163
myAllocator(theAllocator.IsNull() ? NCollection_BaseAllocator::CommonBaseAllocator() : theAllocator),
164-
myData1(NULL),
165-
myData2(NULL),
164+
myData1(nullptr),
165+
myData2(nullptr),
166166
myNbBuckets(NbBuckets),
167167
mySize(0),
168168
isDouble(!single)
@@ -200,6 +200,12 @@ public:
200200
NCollection_ListNode** data1,
201201
NCollection_ListNode** data2);
202202

203+
//! Reallocate the existed data containers.
204+
//! Filling operation must to be done outside.
205+
//! Reallocated memory will be cleared (all elements will be set to nullptr).
206+
Standard_EXPORT Standard_Boolean Reallocate
207+
(const Standard_Integer theNbBuckets);
208+
203209
//! Resizable
204210
Standard_Boolean Resizable() const
205211
{ return IsEmpty() || (mySize > myNbBuckets); }
@@ -214,10 +220,6 @@ public:
214220
Standard_EXPORT void Destroy(NCollection_DelMapNode fDel,
215221
Standard_Boolean doReleaseMemory = Standard_True);
216222

217-
//! NextPrimeForMap
218-
Standard_EXPORT Standard_Integer NextPrimeForMap
219-
(const Standard_Integer N) const;
220-
221223
//! Exchange content of two maps without data copying
222224
void exchangeMapsData (NCollection_BaseMap& theOther)
223225
{

src/NCollection/NCollection_BasePointerVector.cxx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
#include <NCollection_BasePointerVector.hxx>
1515

16-
#include <TCollection.hxx>
1716
#include <cstring>
1817

1918
//=======================================================================

src/NCollection/NCollection_BaseSequence.hxx

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,7 @@
1919
#include <Standard.hxx>
2020
#include <NCollection_BaseAllocator.hxx>
2121
#include <NCollection_DefineAlloc.hxx>
22-
23-
// **************************************** Class SeqNode ********************
24-
25-
class NCollection_SeqNode
26-
{
27-
public:
28-
// define new operator for use with NCollection allocators
29-
DEFINE_NCOLLECTION_ALLOC
30-
public:
31-
NCollection_SeqNode () : myNext (NULL), myPrevious (NULL) {}
32-
NCollection_SeqNode * Next () const { return myNext; }
33-
NCollection_SeqNode * Previous () const { return myPrevious; }
34-
void SetNext (NCollection_SeqNode * theNext) { myNext = theNext; }
35-
void SetPrevious (NCollection_SeqNode * thePrev) { myPrevious = thePrev; }
36-
37-
private:
38-
NCollection_SeqNode* myNext;
39-
NCollection_SeqNode* myPrevious;
40-
};
22+
#include <NCollection_SeqNode.hxx>
4123

4224
typedef void (* NCollection_DelSeqNode)
4325
(NCollection_SeqNode*, Handle(NCollection_BaseAllocator)& theAl);

0 commit comments

Comments
 (0)