0024750: Replace instantiations of TCollection generic classes by NCollection templat...
[occt.git] / src / TCollection / TCollection_BasicMap.cxx
CommitLineData
b311480e 1// Created on: 1993-02-26
2// Created by: Remi LEQUETTE
3// Copyright (c) 1993-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
7fd59977 17#include <TCollection_BasicMap.ixx>
18#include <TCollection.hxx>
19#include <TCollection_BasicMapIterator.hxx>
20#include <TCollection_MapNode.hxx>
21#include <Standard_Stream.hxx>
22
23//=======================================================================
24//function : TCollection_BasicMap
25//purpose :
26//=======================================================================
27
28TCollection_BasicMap::TCollection_BasicMap(const Standard_Integer NbBuckets,
29 const Standard_Boolean single) :
30 myData1(NULL),
31 myData2(NULL),
32 isDouble(!single),
33 mySaturated(Standard_False),
34 myNbBuckets(NbBuckets),
35 mySize(0)
36{
37}
38
39
40//=======================================================================
41//function : BeginResize
42//purpose :
43//=======================================================================
44
45Standard_Boolean TCollection_BasicMap::BeginResize
46 (const Standard_Integer NbBuckets,
47 Standard_Integer& N,
48 Standard_Address& data1,
49 Standard_Address& data2) const
50{
51 if (mySaturated) return Standard_False;
52 N = TCollection::NextPrimeForMap(NbBuckets);
53 if (N <= myNbBuckets) {
54 if (IsEmpty())
55 N = myNbBuckets;
56 else
57 return Standard_False;
58 }
59 data1 = Standard::Allocate((N+1)*sizeof(TCollection_MapNodePtr));
60 memset(data1, 0, (N+1)*sizeof(TCollection_MapNodePtr));
61 if (isDouble) {
62 data2 = Standard::Allocate((N+1)*sizeof(TCollection_MapNodePtr));
63 memset(data2, 0, (N+1)*sizeof(TCollection_MapNodePtr));
64 }
65 else
66 data2 = NULL;
67 return Standard_True;
68}
69
70
71//=======================================================================
72//function : EndResize
73//purpose :
74//=======================================================================
75
76void TCollection_BasicMap::EndResize(const Standard_Integer NbBuckets,
77 const Standard_Integer N,
78 const Standard_Address data1,
79 const Standard_Address data2)
80{
81 Standard::Free(myData1);
82 Standard::Free(myData2);
83 myNbBuckets = N;
84 mySaturated = myNbBuckets <= NbBuckets;
85 myData1 = data1;
86 myData2 = data2;
87}
88
89
90//=======================================================================
91//function : Destroy
92//purpose :
93//=======================================================================
94
95void TCollection_BasicMap::Destroy()
96{
97 mySize = 0;
98 mySaturated = Standard_False;
99 Standard::Free(myData1);
100 Standard::Free(myData2);
101 myData1 = myData2 = NULL;
102}
103
104
105//=======================================================================
106//function : Statistics
107//purpose :
108//=======================================================================
109
110void TCollection_BasicMap::Statistics(Standard_OStream& S) const
111{
112 S <<"\nMap Statistics\n---------------\n\n";
113 S <<"This Map has "<<myNbBuckets<<" Buckets and "<<mySize<<" Keys\n\n";
114 if (mySaturated) S<<"The maximum number of Buckets is reached\n";
115
116 if (mySize == 0) return;
117
118 // compute statistics on 1
119 Standard_Integer * sizes = new Standard_Integer [mySize+1];
120 Standard_Integer i,l,nb;
121 TCollection_MapNode* p;
122 TCollection_MapNode** data;
123
124 S << "\nStatistics for the first Key\n";
125 for (i = 0; i <= mySize; i++) sizes[i] = 0;
126 data = (TCollection_MapNode**) myData1;
127 nb = 0;
128 for (i = 0; i <= myNbBuckets; i++) {
129 l = 0;
130 p = data[i];
131 if (p) nb++;
132 while (p) {
133 l++;
134 p = p->Next();
135 }
136 sizes[l]++;
137 }
138
139 // display results
140 l = 0;
141 for (i = 0; i<= mySize; i++) {
142 if (sizes[i] > 0) {
143 l += sizes[i] * i;
144 S << setw(5) << sizes[i] <<" buckets of size "<<i<<"\n";
145 }
146 }
147
148 Standard_Real mean = ((Standard_Real) l) / ((Standard_Real) nb);
149 S<<"\n\nMean of length : "<<mean<<"\n";
150
151 delete [] sizes;
152}