Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/netgen/libsrc/general/hashtabl.cpp
3206 views
1
/**************************************************************************/
2
/* File: hashtabl.cpp */
3
/* Author: Joachim Schoeberl */
4
/* Date: 01. Jun. 95 */
5
/**************************************************************************/
6
7
/*
8
Abstract data type HASHTABLE
9
*/
10
11
#include <algorithm>
12
#include <mystdlib.h>
13
#include <myadt.hpp>
14
15
namespace netgen
16
{
17
//using namespace netgen;
18
19
void INDEX_4 :: Sort ()
20
{
21
if (i[0] > i[1]) Swap (i[0], i[1]);
22
if (i[2] > i[3]) Swap (i[2], i[3]);
23
if (i[0] > i[2]) Swap (i[0], i[2]);
24
if (i[1] > i[3]) Swap (i[1], i[3]);
25
if (i[1] > i[2]) Swap (i[1], i[2]);
26
}
27
28
29
30
void INDEX_4Q :: Sort ()
31
{
32
if (min2 (i[1], i[2]) < min2 (i[0], i[3]))
33
{ Swap (i[0], i[1]); Swap (i[2], i[3]);}
34
if (i[3] < i[0])
35
{ Swap (i[0], i[3]); Swap (i[1], i[2]);}
36
if (i[3] < i[1])
37
{ Swap (i[1], i[3]); }
38
}
39
40
41
ostream & operator<<(ostream & s, const INDEX_2 & i2)
42
{
43
return s << i2.I1() << ", " << i2.I2();
44
}
45
46
ostream & operator<<(ostream & s, const INDEX_3 & i3)
47
{
48
return s << i3.I1() << ", " << i3.I2() << ", " << i3.I3();
49
}
50
51
ostream & operator<<(ostream & s, const INDEX_4 & i4)
52
{
53
return s << i4.I1() << ", " << i4.I2() << ", " << i4.I3() << ", " << i4.I4();
54
}
55
56
ostream & operator<<(ostream & s, const INDEX_4Q & i4)
57
{
58
return s << i4.I1() << ", " << i4.I2() << ", " << i4.I3() << ", " << i4.I4();
59
}
60
61
62
int BASE_INDEX_HASHTABLE :: Position (int bnr, const INDEX & ind) const
63
{
64
int i;
65
for (i = 1; i <= hash.EntrySize (bnr); i++)
66
if (hash.Get(bnr, i) == ind)
67
return i;
68
return 0;
69
}
70
71
72
73
/*
74
int BASE_INDEX_2_HASHTABLE :: Position (int bnr, const INDEX_2 & ind) const
75
{
76
int i;
77
for (i = 1; i <= hash.EntrySize (bnr); i++)
78
if (hash.Get(bnr, i) == ind)
79
return i;
80
return 0;
81
}
82
*/
83
84
void BASE_INDEX_2_HASHTABLE :: PrintStat (ostream & ost) const
85
{
86
int n = hash.Size();
87
int i;
88
int sumn = 0, sumnn = 0;
89
90
for (i = 1; i <= n; i++)
91
{
92
sumn += hash.EntrySize(i);
93
sumnn += sqr (hash.EntrySize(i));
94
}
95
96
ost << "Hashtable: " << endl
97
<< "size : " << n << endl
98
<< "elements per row : " << (double(sumn) / double(n)) << endl
99
<< "av. acces time : "
100
<< (sumn ? (double (sumnn) / double(sumn)) : 0) << endl;
101
}
102
103
104
/*
105
int BASE_INDEX_3_HASHTABLE :: Position (int bnr, const INDEX_3 & ind) const
106
{
107
int i;
108
const INDEX_3 * pi = &hash.Get(bnr, 1);
109
int n = hash.EntrySize(bnr);
110
for (i = 1; i <= n; ++i, ++pi)
111
{
112
if (*pi == ind)
113
return i;
114
}
115
116
return 0;
117
}
118
*/
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
BASE_INDEX_CLOSED_HASHTABLE ::
140
BASE_INDEX_CLOSED_HASHTABLE (int size)
141
: hash(size)
142
{
143
hash.SetName ("index-hashtable, hash");
144
145
invalid = -1;
146
for (int i = 1; i <= size; i++)
147
hash.Elem(i) = invalid;
148
}
149
150
void BASE_INDEX_CLOSED_HASHTABLE ::
151
BaseSetSize (int size)
152
{
153
hash.SetSize(size);
154
for (int i = 1; i <= size; i++)
155
hash.Elem(i) = invalid;
156
}
157
158
int BASE_INDEX_CLOSED_HASHTABLE ::
159
Position2 (const INDEX & ind) const
160
{
161
int i = HashValue(ind);
162
while (1)
163
{
164
i++;
165
if (i > hash.Size()) i = 1;
166
if (hash.Get(i) == ind) return i;
167
if (hash.Get(i) == invalid) return 0;
168
}
169
}
170
171
int BASE_INDEX_CLOSED_HASHTABLE ::
172
PositionCreate2 (const INDEX & ind, int & apos)
173
{
174
int i = HashValue(ind);
175
int startpos = i;
176
while (1)
177
{
178
i++;
179
if (i > hash.Size()) i = 1;
180
if (hash.Get(i) == ind)
181
{
182
apos = i;
183
return 0;
184
}
185
if (hash.Get(i) == invalid)
186
{
187
hash.Elem(i) = ind;
188
apos = i;
189
return 1;
190
}
191
if (i == startpos)
192
throw NgException ("Try to set new element in full closed hashtable");
193
}
194
}
195
196
int BASE_INDEX_CLOSED_HASHTABLE :: UsedElements () const
197
{
198
int n = hash.Size();
199
int cnt = 0;
200
for (int i = 1; i <= n; i++)
201
if (hash.Get(i) != invalid)
202
cnt++;
203
return cnt;
204
}
205
206
207
208
209
210
211
212
213
214
215
216
BASE_INDEX_2_CLOSED_HASHTABLE ::
217
BASE_INDEX_2_CLOSED_HASHTABLE (int size)
218
: hash(size)
219
{
220
hash.SetName ("i2-hashtable, hash");
221
222
invalid = -1;
223
for (int i = 1; i <= size; i++)
224
hash.Elem(i).I1() = invalid;
225
}
226
227
void BASE_INDEX_2_CLOSED_HASHTABLE ::
228
BaseSetSize (int size)
229
{
230
hash.SetSize(size);
231
for (int i = 1; i <= size; i++)
232
hash.Elem(i).I1() = invalid;
233
}
234
235
236
int BASE_INDEX_2_CLOSED_HASHTABLE ::
237
Position2 (const INDEX_2 & ind) const
238
{
239
int i = HashValue(ind);
240
while (1)
241
{
242
i++;
243
if (i > hash.Size()) i = 1;
244
if (hash.Get(i) == ind) return i;
245
if (hash.Get(i).I1() == invalid) return 0;
246
}
247
}
248
249
int BASE_INDEX_2_CLOSED_HASHTABLE ::
250
PositionCreate2 (const INDEX_2 & ind, int & apos)
251
{
252
int i = HashValue(ind);
253
int startpos = i;
254
while (1)
255
{
256
i++;
257
if (i > hash.Size()) i = 1;
258
if (hash.Get(i) == ind)
259
{
260
apos = i;
261
return 0;
262
}
263
if (hash.Get(i).I1() == invalid)
264
{
265
hash.Elem(i) = ind;
266
apos = i;
267
return 1;
268
}
269
if (i == startpos)
270
throw NgException ("Try to set new element in full closed hashtable");
271
}
272
}
273
274
int BASE_INDEX_2_CLOSED_HASHTABLE :: UsedElements () const
275
{
276
int n = hash.Size();
277
int cnt = 0;
278
for (int i = 1; i <= n; i++)
279
if (hash.Get(i).I1() != invalid)
280
cnt++;
281
return cnt;
282
}
283
284
285
286
287
288
289
290
291
void BASE_INDEX_3_CLOSED_HASHTABLE ::
292
BaseSetSize (int size)
293
{
294
hash.SetSize(size);
295
for (int i = 0; i < size; i++)
296
hash[i].I1() = invalid;
297
}
298
299
bool BASE_INDEX_3_CLOSED_HASHTABLE ::
300
PositionCreate2 (const INDEX_3 & ind, int & apos)
301
{
302
int i = HashValue(ind);
303
int startpos = i;
304
while (1)
305
{
306
/*
307
i++;
308
if (i >= hash.Size()) i = 0;
309
*/
310
i = (i+1) % hash.Size();
311
if (hash[i] == ind)
312
{
313
apos = i;
314
return false;
315
}
316
if (hash[i].I1() == invalid)
317
{
318
hash[i] = ind;
319
apos = i;
320
return true;
321
}
322
if (i == startpos)
323
throw NgException ("Try to set new element in full closed hashtable");
324
}
325
}
326
}
327
328
329