1 // Created on: 2002-04-18
2 // Created by: Alexander KARTOMIN (akm)
3 // Copyright (c) 2002-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 // Purpose: Implementation of the BaseMap class
18 #include <NCollection_BaseMap.hxx>
19 #include <TCollection.hxx>
21 //=======================================================================
22 //function : BeginResize
24 //=======================================================================
26 Standard_Boolean NCollection_BaseMap::BeginResize
27 (const Standard_Integer NbBuckets,
29 NCollection_ListNode**& data1,
30 NCollection_ListNode**& data2) const
32 if (mySaturated) return Standard_False;
33 N = NextPrimeForMap(NbBuckets);
34 if (N <= myNbBuckets) {
38 return Standard_False;
40 data1 = (NCollection_ListNode **)
41 myAllocator->Allocate((N+1)*sizeof(NCollection_ListNode *));
42 memset(data1, 0, (N+1)*sizeof(NCollection_ListNode *));
45 data2 = (NCollection_ListNode **)
46 myAllocator->Allocate((N+1)*sizeof(NCollection_ListNode *));
47 memset(data2, 0, (N+1)*sizeof(NCollection_ListNode *));
54 //=======================================================================
55 //function : EndResize
57 //=======================================================================
59 void NCollection_BaseMap::EndResize
60 (const Standard_Integer NbBuckets,
61 const Standard_Integer N,
62 NCollection_ListNode** data1,
63 NCollection_ListNode** data2)
66 myAllocator->Free(myData1);
68 myAllocator->Free(myData2);
70 mySaturated = (myNbBuckets <= NbBuckets);
76 //=======================================================================
79 //=======================================================================
81 void NCollection_BaseMap::Destroy (NCollection_DelMapNode fDel,
82 Standard_Boolean doReleaseMemory)
87 NCollection_ListNode** data = (NCollection_ListNode**) myData1;
88 NCollection_ListNode *p,*q;
89 for (i = 0; i <= NbBuckets(); i++)
96 q = (NCollection_ListNode*)p->Next();
97 fDel (p, myAllocator);
108 mySaturated = Standard_False;
110 myAllocator->Free(myData1);
111 if (isDouble && myData2)
112 myAllocator->Free(myData2);
113 myData1 = myData2 = NULL;
118 //=======================================================================
119 //function : Statistics
121 //=======================================================================
123 void NCollection_BaseMap::Statistics(Standard_OStream& S) const
125 S <<"\nMap Statistics\n---------------\n\n";
126 S <<"This Map has "<<myNbBuckets<<" Buckets and "<<mySize<<" Keys\n\n";
127 if (mySaturated) S<<"The maximum number of Buckets is reached\n";
129 if (mySize == 0) return;
131 // compute statistics on 1
132 Standard_Integer * sizes = new Standard_Integer [mySize+1];
133 Standard_Integer i,l,nb;
134 NCollection_ListNode* p;
135 NCollection_ListNode** data;
137 S << "\nStatistics for the first Key\n";
138 for (i = 0; i <= mySize; i++) sizes[i] = 0;
139 data = (NCollection_ListNode **) myData1;
141 for (i = 0; i <= myNbBuckets; i++)
156 for (i = 0; i<= mySize; i++)
161 S << setw(5) << sizes[i] <<" buckets of size "<<i<<"\n";
165 Standard_Real mean = ((Standard_Real) l) / ((Standard_Real) nb);
166 S<<"\n\nMean of length : "<<mean<<"\n";
171 //=======================================================================
172 //function : NextPrimeForMap
174 //=======================================================================
176 Standard_Integer NCollection_BaseMap::NextPrimeForMap
177 (const Standard_Integer N) const
179 return TCollection::NextPrimeForMap ( N );