0024530: TKMesh - remove unused package IntPoly
[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//
973c2be1 8// This library is free software; you can redistribute it and / or modify it
9// under the terms of the GNU Lesser General Public version 2.1 as published
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
17#ifdef HAVE_CONFIG_H
18# include <config.h>
19#endif
20#include <TCollection_BasicMap.ixx>
21#include <TCollection.hxx>
22#include <TCollection_BasicMapIterator.hxx>
23#include <TCollection_MapNode.hxx>
24#include <Standard_Stream.hxx>
25
26//=======================================================================
27//function : TCollection_BasicMap
28//purpose :
29//=======================================================================
30
31TCollection_BasicMap::TCollection_BasicMap(const Standard_Integer NbBuckets,
32 const Standard_Boolean single) :
33 myData1(NULL),
34 myData2(NULL),
35 isDouble(!single),
36 mySaturated(Standard_False),
37 myNbBuckets(NbBuckets),
38 mySize(0)
39{
40}
41
42
43//=======================================================================
44//function : BeginResize
45//purpose :
46//=======================================================================
47
48Standard_Boolean TCollection_BasicMap::BeginResize
49 (const Standard_Integer NbBuckets,
50 Standard_Integer& N,
51 Standard_Address& data1,
52 Standard_Address& data2) const
53{
54 if (mySaturated) return Standard_False;
55 N = TCollection::NextPrimeForMap(NbBuckets);
56 if (N <= myNbBuckets) {
57 if (IsEmpty())
58 N = myNbBuckets;
59 else
60 return Standard_False;
61 }
62 data1 = Standard::Allocate((N+1)*sizeof(TCollection_MapNodePtr));
63 memset(data1, 0, (N+1)*sizeof(TCollection_MapNodePtr));
64 if (isDouble) {
65 data2 = Standard::Allocate((N+1)*sizeof(TCollection_MapNodePtr));
66 memset(data2, 0, (N+1)*sizeof(TCollection_MapNodePtr));
67 }
68 else
69 data2 = NULL;
70 return Standard_True;
71}
72
73
74//=======================================================================
75//function : EndResize
76//purpose :
77//=======================================================================
78
79void TCollection_BasicMap::EndResize(const Standard_Integer NbBuckets,
80 const Standard_Integer N,
81 const Standard_Address data1,
82 const Standard_Address data2)
83{
84 Standard::Free(myData1);
85 Standard::Free(myData2);
86 myNbBuckets = N;
87 mySaturated = myNbBuckets <= NbBuckets;
88 myData1 = data1;
89 myData2 = data2;
90}
91
92
93//=======================================================================
94//function : Destroy
95//purpose :
96//=======================================================================
97
98void TCollection_BasicMap::Destroy()
99{
100 mySize = 0;
101 mySaturated = Standard_False;
102 Standard::Free(myData1);
103 Standard::Free(myData2);
104 myData1 = myData2 = NULL;
105}
106
107
108//=======================================================================
109//function : Statistics
110//purpose :
111//=======================================================================
112
113void TCollection_BasicMap::Statistics(Standard_OStream& S) const
114{
115 S <<"\nMap Statistics\n---------------\n\n";
116 S <<"This Map has "<<myNbBuckets<<" Buckets and "<<mySize<<" Keys\n\n";
117 if (mySaturated) S<<"The maximum number of Buckets is reached\n";
118
119 if (mySize == 0) return;
120
121 // compute statistics on 1
122 Standard_Integer * sizes = new Standard_Integer [mySize+1];
123 Standard_Integer i,l,nb;
124 TCollection_MapNode* p;
125 TCollection_MapNode** data;
126
127 S << "\nStatistics for the first Key\n";
128 for (i = 0; i <= mySize; i++) sizes[i] = 0;
129 data = (TCollection_MapNode**) myData1;
130 nb = 0;
131 for (i = 0; i <= myNbBuckets; i++) {
132 l = 0;
133 p = data[i];
134 if (p) nb++;
135 while (p) {
136 l++;
137 p = p->Next();
138 }
139 sizes[l]++;
140 }
141
142 // display results
143 l = 0;
144 for (i = 0; i<= mySize; i++) {
145 if (sizes[i] > 0) {
146 l += sizes[i] * i;
147 S << setw(5) << sizes[i] <<" buckets of size "<<i<<"\n";
148 }
149 }
150
151 Standard_Real mean = ((Standard_Real) l) / ((Standard_Real) nb);
152 S<<"\n\nMean of length : "<<mean<<"\n";
153
154 delete [] sizes;
155}