Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/netgen/libsrc/general/hashtabl.hpp
3206 views
1
#ifndef FILE_HASHTABL
2
#define FILE_HASHTABL
3
4
/**************************************************************************/
5
/* File: hashtabl.hh */
6
/* Author: Joachim Schoeberl */
7
/* Date: 01. Jun. 95 */
8
/**************************************************************************/
9
10
/**
11
Abstract data type HASHTABLE.
12
Hash is done by one INDEX
13
*/
14
class BASE_INDEX_HASHTABLE
15
{
16
protected:
17
/// keys are stored in this table
18
TABLE<INDEX> hash;
19
20
public:
21
///
22
BASE_INDEX_HASHTABLE (int size)
23
: hash (size) { };
24
25
protected:
26
///
27
int HashValue (const INDEX & ind) const
28
{
29
return ind % hash.Size() + 1;
30
}
31
32
///
33
int Position (int bnr, const INDEX & ind) const;
34
};
35
36
///
37
template <class T>
38
class INDEX_HASHTABLE : private BASE_INDEX_HASHTABLE
39
{
40
///
41
TABLE<T> cont;
42
43
public:
44
///
45
inline INDEX_HASHTABLE (int size);
46
///
47
inline void Set (const INDEX & hash, const T & acont);
48
///
49
inline const T & Get (const INDEX & ahash) const;
50
///
51
inline bool Used (const INDEX & ahash) const;
52
///
53
inline int GetNBags () const;
54
///
55
inline int GetBagSize (int bnr) const;
56
///
57
inline void GetData (int bnr, int colnr, INDEX & ahash, T & acont) const;
58
59
///
60
inline void PrintMemInfo (ostream & ost) const;
61
};
62
63
64
65
66
67
68
69
70
71
72
73
///
74
class BASE_INDEX_2_HASHTABLE
75
{
76
protected:
77
///
78
TABLE<INDEX_2> hash;
79
80
public:
81
///
82
BASE_INDEX_2_HASHTABLE (int size)
83
: hash (size) { };
84
85
///
86
void PrintStat (ostream & ost) const;
87
void BaseSetSize(int s) {hash.SetSize(s);}
88
protected:
89
///
90
int HashValue (const INDEX_2 & ind) const
91
{
92
return (ind.I1() + ind.I2()) % hash.Size() + 1;
93
}
94
///
95
int Position (int bnr, const INDEX_2 & ind) const
96
{
97
int i;
98
for (i = 1; i <= hash.EntrySize (bnr); i++)
99
if (hash.Get(bnr, i) == ind)
100
return i;
101
return 0;
102
}
103
};
104
105
106
///
107
template <class T>
108
class INDEX_2_HASHTABLE : public BASE_INDEX_2_HASHTABLE
109
{
110
///
111
TABLE<T> cont;
112
113
public:
114
///
115
INDEX_2_HASHTABLE (int size)
116
: BASE_INDEX_2_HASHTABLE (size), cont(size)
117
{ ; }
118
119
///
120
void SetSize(int s)
121
{
122
cont.SetSize(s);
123
BaseSetSize(s);
124
}
125
126
///
127
void Set (const INDEX_2 & ahash, const T & acont)
128
{
129
int bnr = HashValue (ahash);
130
int pos = Position (bnr, ahash);
131
if (pos)
132
cont.Set (bnr, pos, acont);
133
else
134
{
135
hash.Add1 (bnr, ahash);
136
cont.Add1 (bnr, acont);
137
}
138
}
139
140
///
141
const T & Get (const INDEX_2 & ahash) const
142
{
143
int bnr = HashValue (ahash);
144
int pos = Position (bnr, ahash);
145
return cont.Get (bnr, pos);
146
}
147
148
///
149
bool Used (const INDEX_2 & ahash) const
150
{
151
return (Position (HashValue (ahash), ahash)) ? 1 : 0;
152
}
153
///
154
int GetNBags () const
155
{
156
return cont.Size();
157
}
158
159
///
160
int GetBagSize (int bnr) const
161
{
162
return cont.EntrySize (bnr);
163
}
164
165
///
166
void GetData (int bnr, int colnr,
167
INDEX_2 & ahash, T & acont) const
168
{
169
ahash = hash.Get(bnr, colnr);
170
acont = cont.Get(bnr, colnr);
171
}
172
173
///
174
void SetData (int bnr, int colnr,
175
const INDEX_2 & ahash, const T & acont)
176
{
177
hash.Set(bnr, colnr, ahash);
178
cont.Set(bnr, colnr, acont);
179
}
180
181
///
182
void PrintMemInfo (ostream & ost) const
183
{
184
ost << "Hash: " << endl;
185
hash.PrintMemInfo (ost);
186
ost << "Cont: " << endl;
187
cont.PrintMemInfo (ost);
188
}
189
190
191
void DeleteData ()
192
{
193
int n = hash.Size();
194
hash.SetSize (n);
195
cont.SetSize (n);
196
}
197
198
199
class Iterator
200
{
201
const INDEX_2_HASHTABLE & ht;
202
int bagnr, pos;
203
public:
204
Iterator (const INDEX_2_HASHTABLE & aht,
205
int abagnr, int apos)
206
: ht(aht), bagnr(abagnr), pos(apos)
207
{ ; }
208
209
int BagNr() const { return bagnr; }
210
int Pos() const { return pos; }
211
212
void operator++ (int)
213
{
214
// cout << "begin Operator ++: bagnr = " << bagnr << " - pos = " << pos << endl;
215
pos++;
216
while (bagnr < ht.GetNBags() &&
217
pos == ht.GetBagSize(bagnr+1))
218
{
219
pos = 0;
220
bagnr++;
221
}
222
// cout << "end Operator ++: bagnr = " << bagnr << " - pos = " << pos << endl;
223
}
224
225
bool operator != (int i) const
226
{
227
return bagnr != i;
228
}
229
230
};
231
232
Iterator Begin () const
233
{
234
Iterator it(*this, 0, -1);
235
it++;
236
return it;
237
}
238
239
int End() const
240
{
241
return GetNBags();
242
}
243
244
void GetData (const Iterator & it,
245
INDEX_2 & ahash, T & acont) const
246
{
247
ahash = hash[it.BagNr()][it.Pos()];
248
acont = cont[it.BagNr()][it.Pos()];
249
}
250
251
const INDEX_2 & GetHash (const Iterator & it) const
252
{ return hash[it.BagNr()][it.Pos()]; }
253
254
const T & GetData (const Iterator & it) const
255
{ return cont[it.BagNr()][it.Pos()]; }
256
};
257
258
259
260
template <typename T>
261
inline ostream & operator<< (ostream & ost, const INDEX_2_HASHTABLE<T> & ht)
262
{
263
for (typename INDEX_2_HASHTABLE<T>::Iterator it = ht.Begin();
264
it != ht.End(); it++)
265
{
266
ost << ht.GetHash(it) << ": " << ht.GetData(it) << endl;
267
}
268
269
return ost;
270
}
271
272
273
274
275
276
277
278
///
279
class BASE_INDEX_3_HASHTABLE
280
{
281
protected:
282
///
283
TABLE<INDEX_3> hash;
284
285
public:
286
///
287
BASE_INDEX_3_HASHTABLE (int size)
288
: hash (size) { };
289
290
protected:
291
///
292
int HashValue (const INDEX_3 & ind) const
293
{
294
return (ind.I1() + ind.I2() + ind.I3()) % hash.Size() + 1;
295
}
296
297
///
298
int Position (int bnr, const INDEX_3 & ind) const
299
{
300
const INDEX_3 * pi = &hash.Get(bnr, 1);
301
int n = hash.EntrySize(bnr);
302
for (int i = 1; i <= n; ++i, ++pi)
303
{
304
if (*pi == ind)
305
return i;
306
}
307
308
return 0;
309
}
310
311
312
};
313
314
315
///
316
template <class T>
317
class INDEX_3_HASHTABLE : private BASE_INDEX_3_HASHTABLE
318
{
319
///
320
TABLE<T> cont;
321
322
public:
323
///
324
inline INDEX_3_HASHTABLE (int size);
325
///
326
inline void Set (const INDEX_3 & ahash, const T & acont);
327
///
328
inline const T & Get (const INDEX_3 & ahash) const;
329
///
330
inline bool Used (const INDEX_3 & ahash) const;
331
///
332
inline int GetNBags () const;
333
///
334
inline int GetBagSize (int bnr) const;
335
///
336
inline void SetData (int bnr, int colnr, const INDEX_3 & ahash, const T & acont);
337
///
338
inline void GetData (int bnr, int colnr, INDEX_3 & ahash, T & acont) const;
339
/// returns position, if not existing, will create (create == return 1)
340
inline int PositionCreate (const INDEX_3 & ahash, int & bnr, int & colnr);
341
///
342
inline void SetSize (int size);
343
344
///
345
inline void PrepareSet (const INDEX_3 & ahash);
346
///
347
inline void AllocateElements ();
348
349
///
350
inline void PrintMemInfo (ostream & ost) const;
351
///
352
inline void DeleteData ();
353
354
355
356
357
358
359
360
361
362
class Iterator
363
{
364
const INDEX_3_HASHTABLE & ht;
365
int bagnr, pos;
366
public:
367
Iterator (const INDEX_3_HASHTABLE & aht,
368
int abagnr, int apos)
369
: ht(aht), bagnr(abagnr), pos(apos)
370
{ ; }
371
372
int BagNr() const { return bagnr; }
373
int Pos() const { return pos; }
374
375
void operator++ (int)
376
{
377
// cout << "begin Operator ++: bagnr = " << bagnr << " - pos = " << pos << endl;
378
pos++;
379
while (bagnr < ht.GetNBags() &&
380
pos == ht.GetBagSize(bagnr+1))
381
{
382
pos = 0;
383
bagnr++;
384
}
385
// cout << "end Operator ++: bagnr = " << bagnr << " - pos = " << pos << endl;
386
}
387
388
bool operator != (int i) const
389
{
390
return bagnr != i;
391
}
392
393
};
394
395
Iterator Begin () const
396
{
397
Iterator it(*this, 0, -1);
398
it++;
399
return it;
400
}
401
402
int End() const
403
{
404
return GetNBags();
405
}
406
407
void GetData (const Iterator & it,
408
INDEX_3 & ahash, T & acont) const
409
{
410
ahash = hash[it.BagNr()][it.Pos()];
411
acont = cont[it.BagNr()][it.Pos()];
412
}
413
414
const INDEX_3 & GetHash (const Iterator & it) const
415
{ return hash[it.BagNr()][it.Pos()]; }
416
417
const T & GetData (const Iterator & it) const
418
{ return cont[it.BagNr()][it.Pos()]; }
419
420
421
422
};
423
424
425
426
427
428
429
template <typename T>
430
inline ostream & operator<< (ostream & ost, const INDEX_3_HASHTABLE<T> & ht)
431
{
432
for (typename INDEX_3_HASHTABLE<T>::Iterator it = ht.Begin();
433
it != ht.End(); it++)
434
{
435
ost << ht.GetHash(it) << ": " << ht.GetData(it) << endl;
436
}
437
438
return ost;
439
}
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/// Closed Hashing HT
465
466
class BASE_INDEX_CLOSED_HASHTABLE
467
{
468
protected:
469
///
470
MoveableArray<INDEX> hash;
471
///
472
int invalid;
473
public:
474
///
475
BASE_INDEX_CLOSED_HASHTABLE (int size);
476
477
int Size() const { return hash.Size(); }
478
int UsedPos (int pos) const { return ! (hash.Get(pos) == invalid); }
479
int UsedElements () const;
480
481
///
482
int HashValue (const INDEX & ind) const
483
{
484
return ind % hash.Size() + 1;
485
}
486
487
488
int Position (const INDEX & ind) const
489
{
490
int i = HashValue(ind);
491
while (1)
492
{
493
if (hash.Get(i) == ind) return i;
494
if (hash.Get(i) == invalid) return 0;
495
i++;
496
if (i > hash.Size()) i = 1;
497
}
498
}
499
500
// returns 1, if new postion is created
501
int PositionCreate (const INDEX & ind, int & apos)
502
{
503
int i = HashValue (ind);
504
if (hash.Get(i) == ind)
505
{
506
apos = i;
507
return 0;
508
}
509
if (hash.Get(i) == invalid)
510
{
511
hash.Elem(i) = ind;
512
apos = i;
513
return 1;
514
}
515
return PositionCreate2 (ind, apos);
516
}
517
518
protected:
519
int Position2 (const INDEX & ind) const;
520
int PositionCreate2 (const INDEX & ind, int & apos);
521
void BaseSetSize (int asize);
522
};
523
524
525
template <class T>
526
class INDEX_CLOSED_HASHTABLE : public BASE_INDEX_CLOSED_HASHTABLE
527
{
528
///
529
MoveableArray<T> cont;
530
531
public:
532
///
533
INDEX_CLOSED_HASHTABLE (int size)
534
: BASE_INDEX_CLOSED_HASHTABLE(size), cont(size)
535
{
536
cont.SetName ("ind-hashtable, contents");
537
}
538
539
540
void Set (const INDEX & ahash, const T & acont)
541
{
542
int pos;
543
PositionCreate (ahash, pos);
544
hash.Elem(pos) = ahash;
545
cont.Elem(pos) = acont;
546
}
547
548
const T & Get (const INDEX & ahash) const
549
{
550
int pos = Position (ahash);
551
return cont.Get(pos);
552
}
553
554
///
555
bool Used (const INDEX & ahash) const
556
{
557
int pos = Position (ahash);
558
return (pos != 0);
559
}
560
561
562
///
563
inline void SetData (int pos, const INDEX & ahash, const T & acont)
564
{
565
hash.Elem(pos) = ahash;
566
cont.Elem(pos) = acont;
567
}
568
569
///
570
void GetData (int pos, INDEX & ahash, T & acont) const
571
{
572
ahash = hash.Get(pos);
573
acont = cont.Get(pos);
574
}
575
576
///
577
inline void SetData (int pos, const T & acont)
578
{
579
cont.Elem(pos) = acont;
580
}
581
582
///
583
void GetData (int pos, T & acont) const
584
{
585
acont = cont.Get(pos);
586
}
587
588
///
589
const T & GetData (int pos) { return cont.Get(pos); }
590
///
591
inline void SetSize (int size)
592
{
593
BaseSetSize(size);
594
cont.SetSize(size);
595
}
596
597
///
598
inline void DeleteData ()
599
{ SetSize (cont.Size()); }
600
601
void SetName (const char * aname)
602
{
603
cont.SetName(aname);
604
hash.SetName(aname);
605
}
606
};
607
608
609
610
611
612
613
614
/// Closed Hashing HT
615
616
class BASE_INDEX_2_CLOSED_HASHTABLE
617
{
618
protected:
619
///
620
MoveableArray<INDEX_2> hash;
621
///
622
int invalid;
623
public:
624
///
625
BASE_INDEX_2_CLOSED_HASHTABLE (int size);
626
627
int Size() const { return hash.Size(); }
628
int UsedPos (int pos) const { return ! (hash.Get(pos).I1() == invalid); }
629
int UsedElements () const;
630
631
///
632
int HashValue (const INDEX_2 & ind) const
633
{
634
return (ind.I1() + 71 * ind.I2()) % hash.Size() + 1;
635
}
636
637
638
int Position (const INDEX_2 & ind) const
639
{
640
int i = HashValue(ind);
641
while (1)
642
{
643
if (hash.Get(i) == ind) return i;
644
if (hash.Get(i).I1() == invalid) return 0;
645
i++;
646
if (i > hash.Size()) i = 1;
647
}
648
}
649
650
// returns 1, if new postion is created
651
int PositionCreate (const INDEX_2 & ind, int & apos)
652
{
653
int i = HashValue (ind);
654
if (hash.Get(i) == ind)
655
{
656
apos = i;
657
return 0;
658
}
659
if (hash.Get(i).I1() == invalid)
660
{
661
hash.Elem(i) = ind;
662
apos = i;
663
return 1;
664
}
665
return PositionCreate2 (ind, apos);
666
}
667
668
protected:
669
///
670
671
int Position2 (const INDEX_2 & ind) const;
672
int PositionCreate2 (const INDEX_2 & ind, int & apos);
673
void BaseSetSize (int asize);
674
};
675
676
677
template <class T>
678
class INDEX_2_CLOSED_HASHTABLE : public BASE_INDEX_2_CLOSED_HASHTABLE
679
{
680
///
681
MoveableArray<T> cont;
682
683
public:
684
///
685
inline INDEX_2_CLOSED_HASHTABLE (int size);
686
///
687
inline void Set (const INDEX_2 & ahash, const T & acont);
688
///
689
inline const T & Get (const INDEX_2 & ahash) const;
690
///
691
inline bool Used (const INDEX_2 & ahash) const;
692
///
693
inline void SetData (int pos, const INDEX_2 & ahash, const T & acont);
694
///
695
inline void GetData (int pos, INDEX_2 & ahash, T & acont) const;
696
///
697
inline void SetData (int pos, const T & acont);
698
///
699
inline void GetData (int pos, T & acont) const;
700
///
701
const T & GetData (int pos) { return cont.Get(pos); }
702
///
703
inline void SetSize (int size);
704
///
705
inline void PrintMemInfo (ostream & ost) const;
706
///
707
inline void DeleteData ()
708
{ SetSize (cont.Size()); }
709
710
void SetName (const char * aname)
711
{
712
cont.SetName(aname);
713
hash.SetName(aname);
714
}
715
};
716
717
class BASE_INDEX_3_CLOSED_HASHTABLE
718
{
719
protected:
720
MoveableArray<INDEX_3> hash;
721
int invalid;
722
723
protected:
724
// public: //SZ
725
BASE_INDEX_3_CLOSED_HASHTABLE (int size)
726
: hash(size)
727
{
728
hash.SetName ("i3-hashtable, hash");
729
invalid = -1;
730
for (int i = 0; i < size; i++)
731
hash[i].I1() = invalid;
732
}
733
734
public:
735
int Size() const
736
{
737
return hash.Size();
738
}
739
740
bool UsedPos (int pos) const
741
{
742
return ! (hash[pos].I1() == invalid);
743
}
744
745
int UsedElements () const
746
{
747
int n = hash.Size();
748
int cnt = 0;
749
for (int i = 0; i < n; i++)
750
if (hash[i].I1() != invalid)
751
cnt++;
752
return cnt;
753
}
754
755
int HashValue (const INDEX_3 & ind) const
756
{
757
return (ind.I1() + 15 * ind.I2() + 41 * ind.I3()) % hash.Size();
758
}
759
760
int Position (const INDEX_3 & ind) const
761
{
762
int i = HashValue(ind);
763
while (1)
764
{
765
if (hash[i] == ind) return i;
766
if (hash[i].I1() == invalid) return -1;
767
i = (i+1) % hash.Size();
768
}
769
}
770
771
int Costs (const INDEX_3 & ind) const
772
{
773
int i = HashValue(ind);
774
int c = 1;
775
while (1)
776
{
777
if (hash[i] == ind) return c;
778
if (hash[i].I1() == invalid) return c;
779
i = (i+1) % hash.Size();
780
c++;
781
}
782
}
783
784
785
786
// returns true, if new postion is created
787
bool PositionCreate (const INDEX_3 & ind, int & apos)
788
{
789
int i = HashValue (ind);
790
if (hash[i] == ind)
791
{
792
apos = i;
793
return false;
794
}
795
if (hash[i].I1() == invalid)
796
{
797
hash[i] = ind;
798
apos = i;
799
return true;
800
}
801
return PositionCreate2 (ind, apos);
802
}
803
804
protected:
805
bool PositionCreate2 (const INDEX_3 & ind, int & apos);
806
void BaseSetSize (int asize);
807
};
808
809
810
811
template <class T>
812
class INDEX_3_CLOSED_HASHTABLE : public BASE_INDEX_3_CLOSED_HASHTABLE
813
{
814
MoveableArray<T,0> cont;
815
816
public:
817
INDEX_3_CLOSED_HASHTABLE (int size)
818
: BASE_INDEX_3_CLOSED_HASHTABLE(size), cont(size)
819
{
820
cont.SetName ("i3-hashtable, contents");
821
}
822
823
void Set (const INDEX_3 & ahash, const T & acont)
824
{
825
int pos;
826
PositionCreate (ahash, pos);
827
hash[pos] = ahash;
828
cont[pos] = acont;
829
}
830
831
const T & Get (const INDEX_3 & ahash) const
832
{
833
return cont[Position (ahash)];
834
}
835
836
bool Used (const INDEX_3 & ahash) const
837
{
838
return (Position (ahash) != -1);
839
}
840
841
void SetData (int pos, const INDEX_3 & ahash, const T & acont)
842
{
843
hash[pos] = ahash;
844
cont[pos] = acont;
845
}
846
847
void GetData (int pos, INDEX_3 & ahash, T & acont) const
848
{
849
ahash = hash[pos];
850
acont = cont[pos];
851
}
852
853
void SetData (int pos, const T & acont)
854
{
855
cont[pos] = acont;
856
}
857
858
void GetData (int pos, T & acont) const
859
{
860
acont = cont[pos];
861
}
862
863
const T & GetData (int pos) const
864
{
865
return cont[pos];
866
}
867
868
void SetSize (int size)
869
{
870
BaseSetSize(size);
871
cont.SetSize(size);
872
}
873
874
void PrintMemInfo (ostream & ost) const
875
{
876
cout << "Hashtable: " << Size()
877
<< " entries of size " << sizeof(INDEX_3) << " + " << sizeof(T)
878
<< " = " << Size() * (sizeof(INDEX_3) + sizeof(T)) << " bytes" << endl;
879
880
}
881
882
void DeleteData ()
883
{
884
SetSize (cont.Size());
885
}
886
887
void SetName (const char * aname)
888
{
889
cont.SetName(aname);
890
hash.SetName(aname);
891
}
892
};
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
template<class T>
911
inline INDEX_3_HASHTABLE<T> :: INDEX_3_HASHTABLE (int size)
912
: BASE_INDEX_3_HASHTABLE (size), cont(size)
913
{
914
;
915
}
916
917
template<class T>
918
inline int INDEX_3_HASHTABLE<T> :: PositionCreate (const INDEX_3 & ahash, int & bnr, int & colnr)
919
{
920
bnr = HashValue (ahash);
921
colnr = Position (bnr, ahash);
922
if (!colnr)
923
{
924
hash.Add (bnr, ahash);
925
cont.AddEmpty (bnr);
926
colnr = cont.EntrySize (bnr);
927
return 1;
928
}
929
return 0;
930
}
931
932
933
template<class T>
934
inline void INDEX_3_HASHTABLE<T> :: Set (const INDEX_3 & ahash, const T & acont)
935
{
936
int bnr = HashValue (ahash);
937
int pos = Position (bnr, ahash);
938
if (pos)
939
cont.Set (bnr, pos, acont);
940
else
941
{
942
hash.Add1 (bnr, ahash);
943
cont.Add1 (bnr, acont);
944
}
945
}
946
947
template<class T>
948
inline const T & INDEX_3_HASHTABLE<T> :: Get (const INDEX_3 & ahash) const
949
{
950
int bnr = HashValue (ahash);
951
int pos = Position (bnr, ahash);
952
return cont.Get (bnr, pos);
953
}
954
955
template<class T>
956
inline bool INDEX_3_HASHTABLE<T> :: Used (const INDEX_3 & ahash) const
957
{
958
return (Position (HashValue (ahash), ahash)) ? 1 : 0;
959
}
960
961
template<class T>
962
inline int INDEX_3_HASHTABLE<T> :: GetNBags () const
963
{
964
return cont.Size();
965
}
966
967
template<class T>
968
inline int INDEX_3_HASHTABLE<T> :: GetBagSize (int bnr) const
969
{
970
return cont.EntrySize (bnr);
971
}
972
973
template<class T>
974
inline void INDEX_3_HASHTABLE<T> :: GetData (int bnr, int colnr, INDEX_3 & ahash, T & acont) const
975
{
976
ahash = hash.Get(bnr, colnr);
977
acont = cont.Get(bnr, colnr);
978
}
979
980
template<class T>
981
inline void INDEX_3_HASHTABLE<T> :: SetData (int bnr, int colnr, const INDEX_3 & ahash, const T & acont)
982
{
983
hash.Set(bnr, colnr, ahash);
984
cont.Set(bnr, colnr, acont);
985
}
986
987
template<class T>
988
inline void INDEX_3_HASHTABLE<T> :: SetSize (int size)
989
{
990
hash.SetSize (size);
991
cont.SetSize (size);
992
}
993
994
template<class T>
995
inline void INDEX_3_HASHTABLE<T> :: DeleteData ()
996
{
997
int n = hash.Size();
998
hash.SetSize (n);
999
cont.SetSize (n);
1000
}
1001
1002
template<class T>
1003
inline void INDEX_3_HASHTABLE<T> :: PrepareSet (const INDEX_3 & ahash)
1004
{
1005
int bnr = HashValue (ahash);
1006
hash.IncSizePrepare (bnr-1);
1007
cont.IncSizePrepare (bnr-1);
1008
}
1009
1010
1011
template<class T>
1012
inline void INDEX_3_HASHTABLE<T> :: AllocateElements ()
1013
{
1014
hash.AllocateElementsOneBlock();
1015
cont.AllocateElementsOneBlock();
1016
}
1017
1018
1019
1020
template<class T>
1021
inline void INDEX_3_HASHTABLE<T> :: PrintMemInfo (ostream & ost) const
1022
{
1023
ost << "Hash: " << endl;
1024
hash.PrintMemInfo (ost);
1025
ost << "Cont: " << endl;
1026
cont.PrintMemInfo (ost);
1027
}
1028
1029
1030
1031
1032
1033
template<class T>
1034
inline INDEX_HASHTABLE<T> :: INDEX_HASHTABLE (int size)
1035
: BASE_INDEX_HASHTABLE (size), cont(size)
1036
{
1037
;
1038
}
1039
1040
template<class T>
1041
inline void INDEX_HASHTABLE<T> :: Set (const INDEX & ahash, const T & acont)
1042
{
1043
int bnr = HashValue (ahash);
1044
int pos = Position (bnr, ahash);
1045
if (pos)
1046
cont.Set (bnr, pos, acont);
1047
else
1048
{
1049
hash.Add (bnr, ahash);
1050
cont.Add (bnr, acont);
1051
}
1052
}
1053
1054
template<class T>
1055
inline const T & INDEX_HASHTABLE<T> :: Get (const INDEX & ahash) const
1056
{
1057
int bnr = HashValue (ahash);
1058
int pos = Position (bnr, ahash);
1059
return cont.Get (bnr, pos);
1060
}
1061
1062
template<class T>
1063
inline bool INDEX_HASHTABLE<T> :: Used (const INDEX & ahash) const
1064
{
1065
return (Position (HashValue (ahash), ahash)) ? 1 : 0;
1066
}
1067
1068
template<class T>
1069
inline int INDEX_HASHTABLE<T> :: GetNBags () const
1070
{
1071
return hash.Size();
1072
}
1073
1074
template<class T>
1075
inline int INDEX_HASHTABLE<T> :: GetBagSize (int bnr) const
1076
{
1077
return hash.EntrySize(bnr);
1078
}
1079
1080
template<class T>
1081
inline void INDEX_HASHTABLE<T> :: GetData (int bnr, int colnr, INDEX & ahash, T & acont) const
1082
{
1083
ahash = hash.Get(bnr, colnr);
1084
acont = cont.Get(bnr, colnr);
1085
}
1086
1087
template<class T>
1088
inline void INDEX_HASHTABLE<T> :: PrintMemInfo (ostream & ost) const
1089
{
1090
ost << "Hash: " << endl;
1091
hash.PrintMemInfo (ost);
1092
ost << "Cont: " << endl;
1093
cont.PrintMemInfo (ost);
1094
}
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
/* *********** Closed Hashing ************************* */
1111
1112
1113
1114
template<class T>
1115
inline INDEX_2_CLOSED_HASHTABLE<T> ::
1116
INDEX_2_CLOSED_HASHTABLE (int size)
1117
: BASE_INDEX_2_CLOSED_HASHTABLE(size), cont(size)
1118
{
1119
cont.SetName ("i2-hashtable, contents");
1120
}
1121
1122
template<class T>
1123
inline void INDEX_2_CLOSED_HASHTABLE<T> ::
1124
Set (const INDEX_2 & ahash, const T & acont)
1125
{
1126
int pos;
1127
PositionCreate (ahash, pos);
1128
hash.Elem(pos) = ahash;
1129
cont.Elem(pos) = acont;
1130
}
1131
1132
template<class T>
1133
inline const T & INDEX_2_CLOSED_HASHTABLE<T> ::
1134
Get (const INDEX_2 & ahash) const
1135
{
1136
int pos = Position (ahash);
1137
return cont.Get(pos);
1138
}
1139
1140
template<class T>
1141
inline bool INDEX_2_CLOSED_HASHTABLE<T> ::
1142
Used (const INDEX_2 & ahash) const
1143
{
1144
int pos = Position (ahash);
1145
return (pos != 0);
1146
}
1147
1148
template<class T>
1149
inline void INDEX_2_CLOSED_HASHTABLE<T> ::
1150
SetData (int pos, const INDEX_2 & ahash, const T & acont)
1151
{
1152
hash.Elem(pos) = ahash;
1153
cont.Elem(pos) = acont;
1154
}
1155
1156
template<class T>
1157
inline void INDEX_2_CLOSED_HASHTABLE<T> ::
1158
GetData (int pos, INDEX_2 & ahash, T & acont) const
1159
{
1160
ahash = hash.Get(pos);
1161
acont = cont.Get(pos);
1162
}
1163
1164
template<class T>
1165
inline void INDEX_2_CLOSED_HASHTABLE<T> ::
1166
SetData (int pos, const T & acont)
1167
{
1168
cont.Elem(pos) = acont;
1169
}
1170
1171
template<class T>
1172
inline void INDEX_2_CLOSED_HASHTABLE<T> ::
1173
GetData (int pos, T & acont) const
1174
{
1175
acont = cont.Get(pos);
1176
}
1177
1178
1179
template<class T>
1180
inline void INDEX_2_CLOSED_HASHTABLE<T> ::
1181
SetSize (int size)
1182
{
1183
BaseSetSize(size);
1184
cont.SetSize(size);
1185
}
1186
1187
1188
1189
template<class T>
1190
inline void INDEX_2_CLOSED_HASHTABLE<T> ::
1191
PrintMemInfo (ostream & ost) const
1192
{
1193
cout << "Hashtable: " << Size()
1194
<< " entries of size " << sizeof(INDEX_2) << " + " << sizeof(T)
1195
<< " = " << Size() * (sizeof(INDEX_2) + sizeof(T)) << " bytes."
1196
<< " Used els: " << UsedElements()
1197
<< endl;
1198
}
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
/*
1216
template<class T>
1217
inline INDEX_3_CLOSED_HASHTABLE<T> ::
1218
INDEX_3_CLOSED_HASHTABLE (int size)
1219
: BASE_INDEX_3_CLOSED_HASHTABLE(size), cont(size)
1220
{
1221
cont.SetName ("i3-hashtable, contents");
1222
}
1223
1224
template<class T>
1225
inline void INDEX_3_CLOSED_HASHTABLE<T> ::
1226
Set (const INDEX_3 & ahash, const T & acont)
1227
{
1228
int pos;
1229
PositionCreate (ahash, pos);
1230
hash.Elem(pos) = ahash;
1231
cont.Elem(pos) = acont;
1232
}
1233
1234
template<class T>
1235
inline const T & INDEX_3_CLOSED_HASHTABLE<T> ::
1236
Get (const INDEX_3 & ahash) const
1237
{
1238
int pos = Position (ahash);
1239
return cont[pos];
1240
}
1241
1242
template<class T>
1243
inline bool INDEX_3_CLOSED_HASHTABLE<T> ::
1244
Used (const INDEX_3 & ahash) const
1245
{
1246
int pos = Position (ahash);
1247
return (pos != 0);
1248
}
1249
1250
template<class T>
1251
inline void INDEX_3_CLOSED_HASHTABLE<T> ::
1252
SetData (int pos, const INDEX_3 & ahash, const T & acont)
1253
{
1254
hash.Elem(pos) = ahash;
1255
cont.Elem(pos) = acont;
1256
}
1257
1258
template<class T>
1259
inline void INDEX_3_CLOSED_HASHTABLE<T> ::
1260
GetData (int pos, INDEX_3 & ahash, T & acont) const
1261
{
1262
ahash = hash.Get(pos);
1263
acont = cont.Get(pos);
1264
}
1265
1266
template<class T>
1267
inline void INDEX_3_CLOSED_HASHTABLE<T> ::
1268
SetData (int pos, const T & acont)
1269
{
1270
cont.Elem(pos) = acont;
1271
}
1272
1273
template<class T>
1274
inline void INDEX_3_CLOSED_HASHTABLE<T> ::
1275
GetData (int pos, T & acont) const
1276
{
1277
acont = cont.Get(pos);
1278
}
1279
1280
template<class T>
1281
inline const T & INDEX_3_CLOSED_HASHTABLE<T> ::
1282
GetData (int pos) const
1283
{
1284
return cont.Get(pos);
1285
}
1286
1287
1288
template<class T>
1289
inline void INDEX_3_CLOSED_HASHTABLE<T> ::
1290
SetSize (int size)
1291
{
1292
BaseSetSize(size);
1293
cont.SetSize(size);
1294
}
1295
1296
template<class T>
1297
inline void INDEX_3_CLOSED_HASHTABLE<T> ::
1298
PrintMemInfo (ostream & ost) const
1299
{
1300
cout << "Hashtable: " << Size()
1301
<< " entries of size " << sizeof(INDEX_3) << " + " << sizeof(T)
1302
<< " = " << Size() * (sizeof(INDEX_3) + sizeof(T)) << " bytes" << endl;
1303
}
1304
*/
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
#endif
1323
1324