-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadthash_impl.i
2545 lines (2276 loc) · 69.3 KB
/
adthash_impl.i
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{@discard
This file is a part of the PascalAdt library, which provides
commonly used algorithms and data structures for the FPC and Delphi
compilers.
Copyright (C) 2004, 2005 by Lukasz Czajka
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA }
{@discard
adthash_impl.i::prefix=&_mcp_prefix&::item_type=&ItemType&
}
&include adthash.defs
&include adthash_impl.mcp
{ turn range checking off }
{$R-}
{ **************************************************************************** }
{ Notes on the implementation of THashTable: }
{ THashTable is implemented as a closed hash table. FBuckets is an
array of buckets, containing elements of type THashNode. If
FBuckets[i].Next = @FBuckets[i] then it means that
bucket number i is empty. Items that hash to the same bucket number
are placed in a singly linked list; FBuckets[i] is the head
of the list, next node can be reached through Next field of
THashNode, the last node in the list has Next field set to nil. All
nodes except for the first one are dynamically allocated. Items that
have the same keys (according to comparison functor, not only that
they hash to the same value) are stored in consequtive
nodes. FCapacity is the number of buckets allocated. FSize is the
number of items stored in the table. FTableSize is
log2(FCapacity). When the ratio FSize/FCapacity exceeds
MaxFillRatio then the table is Rehash'ed to be two times
bigger. AutoShrink can be set to true to make the table shrink
automatically when it contains less than
(MinFillRatio*FCapacity)/100 items. FCanShrink is used to keep track
whether it is resonable to shrink the table automatically (only if
AutoShrink is true). It is considered unreasonable to shrink the
table automatically if it has just been Rehash'ed by the user,
i.e. the user called Rehash and there were no subsequent automatic
calls to this method. In such a situation the user probably intends
to insert a large number of items and set the capacity large enough
to insert all those items without intervening automatic
Rehash'es. It would be pointless to ruin his endeavours by shrinking
the table automatically. A position within a hash table is
represented by a (bucket,node) pair, where bucket is the index of
the bucket and node is the node in this bucket just before the one
that contains the item at the position. I.e. node^.Next points to
the item. The first position in a bucket is represented by
(bucket,nil) pair, where bucket is the index of that bucket. The
one-beyond-last position is represented by (FCapacity,nil) pair. Any
other pairs are not valid positions. }
{ Calling CheckMaxFillRatio, CheckMinFillRatio. }
{ Caution must be taken when calling one of these methods. It should
be remembered that any of them may potentially call Rehash and
invalidate all pointers into the table. }
{ Graphical representation of THashTable: }
{
Bucket
index
+-----+ +-----+ +-----+
0 | 2n |--->| n |--->| 0 |
+-----+ +-----+ +-----+
1 | 1 |--+ +-----+ +-----+ +-----+
+-----+ +-->| 1 |--->| n+1 |--->| n+1 |
2 | nil | +-----+ +-----+ +-----+
+-----+
...
| |
+-----+ +-----+
n-3 |2n-3 |--->| n-3 |
+-----+ +-----+
n-2 | nil |
+-----+
n-1 | n-1 |
+-----+
}
{ This sample hash table stores items represented by integers and uses
a simple hashing function that returns the number itself to be used
as an index into the Fbuckets array. It is then mod'ed by n (the
capacity) and an appropriate node in the list is
located. RepeatedItems is true for this table. }
{ **************************************************************************** }
{ ------------------------ non-member routines ------------------------------- }
{ ------------------------------- THashTable --------------------------------- }
constructor THashTable.Create;
begin
inherited;
InitFields;
end;
constructor THashTable.CreateCopy(const ht : THashTable;
const itemCopier : IUnaryFunctor);
var
i : IndexType;
dest, src : PHashNode;
begin
inherited CreateCopy(ht);
FSize := 0;
FMaxFillRatio := ht.FMaxFillRatio;
FMinFillRatio := ht.FMinFillRatio;
if itemCopier <> nil then
begin
FCanShrink := ht.FCanShrink;
FFirstUsedBucket := ht.FFirstUsedBucket;
FTableSize := ht.FTableSize;
FCapacity := ht.FCapacity;
SetLength(FBuckets, FCapacity);
i := 0;
try
for i := 0 to FCapacity - 1 do
begin
dest := @FBuckets[i];
_mcp_set_zero(dest^.Item);
src := @ht.FBuckets[i];
if src^.Next <> src then
begin
dest^.Next := nil;
dest^.Item := itemCopier.Perform(src^.Item);
Inc(FSize);
src := src^.Next;
while src <> nil do
begin
NewNode(dest^.Next);
dest := dest^.Next;
dest^.Next := nil;
dest^.Item := itemCopier.Perform(src^.Item);
Inc(FSize);
src := src^.Next;
end;
end else
begin
dest^.Next := dest;
end;
end; { end for }
i := FCapacity;
except
Inc(i);
while i < FCapacity do
begin
with FBuckets[i] do
begin
_mcp_set_zero(Item);
Next := @FBuckets[i];
end;
Inc(i);
end;
raise;
end;
end else { not itemCopier <> nil }
begin
InitBuckets;
end;
end;
destructor THashTable.Destroy;
begin
if FBuckets <> nil then
begin
ClearBuckets;
SetLength(FBuckets, 0);
end;
inherited;
end;
function THashTable.GetBucketIndex(value : UnsignedType) : IndexType;
{$ifdef INLINE_DIRECTIVE_REPEAT }
inline;
{$endif }
begin
Result := value and (FCapacity - 1);
end;
procedure THashTable.CheckMinFillRatio;
{$ifdef INLINE_DIRECTIVE_REPEAT }
inline;
{$endif }
begin
if AutoShrink and FCanShrink and
((FSize shl htRatioFactor) shr FTableSize < FMinFillRatio) and
(FTableSize - 1 >= htMinTableSize) then
begin
Rehash(-1);
FCanShrink := true;
end;
end;
procedure THashTable.CheckMaxFillRatio;
begin
if (FSize shl htRatioFactor) shr FTableSize > FMaxFillRatio then
begin
Rehash(1);
FCanShrink := true;
end;
end;
procedure THashTable.InitFields;
begin
FMaxFillRatio := (htDefaultMaxFillRatio shl htRatioFactor) div 100;
FMinFillRatio := (htDefaultMaxFillRatio shl htRatioFactor) div 100;
InitBuckets;
end;
procedure THashTable.InitBuckets;
var
i : IndexType;
begin
FFirstUsedBucket := -1;
FCanShrink := false;
FSize := 0;
FTableSize := htInitialTableSize;
FCapacity := CalculateCapacity(FTableSize);
SetLength(FBuckets, FCapacity);
for i := 0 to FCapacity - 1 do
begin
_mcp_set_zero(FBuckets[i].Item);
FBuckets[i].Next := @FBuckets[i];
end;
end;
procedure THashTable.AdvanceToNearestItem(var bucket : IndexType;
var node : PHashNode);
begin
if (node <> nil) and (node^.Next = nil) then
begin
node := nil;
Inc(bucket);
end;
if node = nil then
begin
while (bucket < FCapacity) and
(FBuckets[bucket].Next = @FBuckets[bucket]) do
begin
Inc(bucket);
end;
end;
end;
procedure THashTable.RetreatToNearestItem(var bucket : IndexType;
var node : PHashNode);
var
prev : PHashNode;
begin
Assert(bucket <= FCapacity, msgInvalidIterator);
if bucket = FCapacity then
Dec(bucket);
Assert(bucket >= 0, msgRetreatingStartIterator);
while FBuckets[bucket].Next = @FBuckets[bucket] do
begin
Dec(bucket);
Assert(bucket >= 0, msgRetreatingStartIterator);
end;
prev := nil;
node := @FBuckets[bucket];
while node^.Next <> nil do
begin
prev := node;
node := node^.Next;
end;
node := prev;
end;
function THashTable.EqualItemsAhead(var bucket : IndexType;
var node : PHashNode;
aitem : ItemType) : SizeType;
begin
{ assert that the chain is not empty }
Assert(FBuckets[bucket].Next <> @FBuckets[bucket]);
Result := 1;
if node = nil then
begin
node := @FBuckets[bucket];
end else
begin
Assert(node^.Next <> nil, msgInternalError);
node := node^.Next;
end;
Assert(_mcp_equal(node^.Item, aitem), msgInternalError);
while (node^.Next <> nil) and
(_mcp_equal(node^.Next^.Item, aitem)) do
begin
Inc(Result);
node := node^.Next;
end;
if node^.Next = nil then
begin
node := nil;
Inc(bucket);
end;
end;
function THashTable.FindNode(aitem : ItemType; var bucket : IndexType;
var node : PHashNode) : Boolean;
begin
bucket := GetBucketIndex(Hasher.Hash(aitem));
if FBuckets[bucket].Next <> @FBuckets[bucket] then
begin
if _mcp_equal(FBuckets[bucket].Item, aitem) then
begin
node := nil;
Result := true;
end else
begin
node := @FBuckets[bucket];
while (node^.Next <> nil) and
(not _mcp_equal(node^.Next^.Item, aitem)) do
begin
node := node^.Next;
end;
if node^.Next <> nil then
begin
Result := true;
end else
Result := false;
end;
end else begin
node := nil;
Result := false;
end;
end;
procedure THashTable.InsertNode(var bucket : IndexType; var node : PHashNode;
aitem : ItemType);
var
temp : PHashNode;
begin
if node <> nil then
begin
temp := node^.Next;
NewNode(node^.Next);
with node^.Next^ do
begin
Item := aitem;
Next := temp;
end;
end else
begin
if FBuckets[bucket].Next = @FBuckets[bucket] then
begin
FBuckets[bucket].Next := nil;
FBuckets[bucket].Item := aitem
end else
begin
with FBuckets[bucket] do
begin
temp := Next;
NewNode(Next);
Next^.Next := temp;
Next^.Item := aitem;
end;
node := @FBuckets[bucket];
end;
end;
Inc(FSize);
FFirstUsedBucket := -1;
end;
function THashTable.ExtractNode(bucket : IndexType; node : PHashNode) : ItemType;
var
nnode : PHashNode;
begin
Assert(FBuckets[bucket].Next <> @FBuckets[bucket]);
if (node = nil) then
begin
node := @FBuckets[bucket];
Result := node^.Item;
nnode := node^.Next;
if nnode <> nil then
begin
node^.Item := nnode^.Item;
node^.Next := nnode^.Next;
DisposeNode(nnode);
end else begin
node^.Next := node;
node^.Item := DefaultItem;
end;
end else
begin
Assert(node^.Next <> nil, msgDeletingInvalidIterator);
nnode := node^.Next;
Result := nnode^.Item;
node^.Next := nnode^.Next;
DisposeNode(nnode);
end;
Dec(FSize);
FFirstUsedBucket := -1;
end;
procedure THashTable.ClearBuckets;
var
i : IndexType;
node, nnode : PHashNode;
begin
for i := 0 to FCapacity - 1 do
begin
if FBuckets[i].Next <> @FBuckets[i] then
begin
node := @FBuckets[i];
DisposeItem(node^.Item);
node := node^.Next;
while node <> nil do
begin
DisposeItem(node^.Item);
nnode := node^.Next;
DisposeNode(node);
node := nnode;
end;
{ after ClearBuckets the buckets are usually freed, so the
values of Next and Item do not matter; if it is necessary
have these field valid, then it is the responsibility of
the caler to validate them; thisd is not done here because
of the (small) performance penalty (as it is not needed
most of the time, anyway) }
end;
end;
FFirstUsedBucket := -1;
end;
procedure THashTable.NewNode(var node : PHashNode);
begin
New(node);
end;
procedure THashTable.DisposeNode(node : PHashNode);
begin
Dispose(node);
end;
function THashTable.GetCapacity : SizeType;
begin
Result := FCapacity;
end;
function THashTable.CalculateCapacity(ex : SizeType) : SizeType;
begin
Result := 1 shl ex;
end;
function THashTable.GetMaxFillRatio : SizeType;
begin
Result := (FMaxFillRatio*100) shr htRatioFactor;
end;
procedure THashTable.SetMaxFillRatio(fr : SizeType);
begin
FMaxFillRatio := (fr shl htRatioFactor) div 100;
end;
function THashTable.GetMinFillRatio : SizeType;
begin
Result := (FMinFillRatio*100) shr htRatioFactor;
end;
procedure THashTable.SetMinFillRatio(fr : SizeType);
begin
FMinFillRatio := (fr shl htRatioFactor) div 100;
end;
{$ifdef TEST_PASCAL_ADT }
procedure THashTable.LogStatus(mname : String);
var
maxItemsInBucket, m2ItemsInBucket : SizeType;
BucketsEmpty, BucketsUsed, itemsInBucket : SizeType;
avgItemsInBucket, varItemsInBucket, avgAll, varAll : Double;
i : IndexType;
node : PHashNode;
begin
inherited;
m2ItemsInBucket := 0;
BucketsEmpty := 0;
maxItemsInBucket := 0;
for i := 0 to FCapacity - 1 do
begin
node := @FBuckets[i];
if node^.Next = node then
begin
Inc(BucketsEmpty);
end else
begin
itemsInBucket := 0;
while node <> nil do
begin
Inc(itemsInBucket);
node := node^.Next;
end;
Inc(m2ItemsInBucket, itemsInBucket * itemsInBucket);
if itemsInBucket > maxItemsInBucket then
maxItemsInBucket := itemsInBucket;
end;
end;
BucketsUsed := FCapacity - BucketsEmpty;
avgItemsInBucket := FSize / BucketsUsed;
varItemsInBucket :=
m2ItemsInBucket / BucketsUsed - Sqr(avgItemsInBucket);
avgAll := FSize / FCapacity;
varAll := m2ItemsInBucket / FCapacity - Sqr(avgAll);
WriteLog('Total buckets: ' + IntToStr(FCapacity));
WriteLog('Empty buckets: ' + IntToStr(BucketsEmpty));
WriteLog('Used buckets: ' + IntToStr(BucketsUsed));
WriteLog('Max items in bucket: ' + IntToStr(maxItemsInBucket));
WriteLog('Average items in bucket (all): ' + FloatToStr(avgAll));
WriteLog('Variance of items in bucket (all): ' + FloatToStr(varAll));
WriteLog('Deviation of items in bucket (all): ' +
FloatToStr(Sqrt(varAll)));
WriteLog('Average items in bucket (used): ' +
FloatToStr(avgItemsInBucket));
WriteLog('Variance of items in bucket (used): ' +
FloatToStr(varItemsInBucket));
WriteLog('Deviation of items in bucket (used): ' +
FloatToStr(Sqrt(varItemsInBucket)));
WriteLog('Average steps searching for an existing item: ' +
FloatToStr(((m2ItemsInBucket / FSize) + 1) / 2));
WriteLog;
end;
{$endif TEST_PASCAL_ADT }
function THashTable.CopySelf(const ItemCopier : IUnaryFunctor) : TContainerAdt;
begin
Result := THashTable.CreateCopy(self, itemCopier);
end;
procedure THashTable.Swap(cont : TContainerAdt);
var
table : THashTable;
begin
if cont is THashTable then
begin
BasicSwap(cont);
table := THashTable(cont);
ExchangePtr(FBuckets, table.FBuckets); // CHECK
ExchangeData(FCapacity, table.FCapacity, SizeOf(SizeType));
ExchangeData(FSize, table.FSize, SizeOf(SizeType));
ExchangeData(FTableSize, table.FTableSize, SizeOf(SizeType));
ExchangeData(FMinFillRatio, table.FMinFillRatio, SizeOf(SizeType));
ExchangeData(FMaxFillRatio, table.FMaxFillRatio, SizeOf(SizeType));
ExchangeData(FCanShrink, table.FCanShrink, SizeOf(Boolean));
ExchangeData(FFirstUsedBucket, table.FFirstUsedBucket, SizeOf(IndexType));
end else
inherited;
end;
function THashTable.Start : TSetIterator;
begin
Result := THashTableIterator.Create(0, nil, self);
end;
function THashTable.Finish : TSetIterator;
begin
Result := THashTableIterator.Create(FCapacity, nil, self);
end;
&if (&_mcp_accepts_nil)
function THashTable.FindOrInsert(aitem : ItemType) : ItemType;
var
bucket : IndexType;
node : PHashNode;
begin
if RepeatedItems then
begin
Insert(aitem);
Result := nil;
end else
begin
if FindNode(aitem, bucket, node) then
begin
if node <> nil then
Result := node^.Next^.Item
else
Result := FBuckets[bucket].Item;
end else begin
InsertNode(bucket, node, aitem);
Result := nil;
CheckMaxFillRatio;
end;
end;
end;
function THashTable.Find(aitem : ItemType) : ItemType;
var
bucket : IndexType;
node : PHashNode;
begin
if FindNode(aitem, bucket, node) then
begin
if node = nil then
Result := FBuckets[bucket].Item
else
Result := node^.Next^.Item
end else
Result := nil;
end;
&endif &# end &_mcp_accepts_nil
function THashTable.Has(aitem : ItemType) : Boolean;
var
bucket : IndexType;
node : PHashNode;
begin
Result := FindNode(aitem, bucket, node);
end;
function THashTable.Count(aitem : ItemType) : SizeType;
var
node : PHashNode;
bucket : IndexType;
begin
if FindNode(aitem, bucket, node) then
begin
Result := EqualItemsAhead(bucket, node, aitem);
end else
Result := 0;
end;
function THashTable.Insert(pos : TSetIterator; aitem : ItemType) : Boolean;
begin
Result := Insert(aitem);
end;
function THashTable.Insert(aitem : ItemType) : Boolean;
var
bucket : IndexType;
node : PHashNode;
begin
if FindNode(aitem, bucket, node) then
begin
if RepeatedItems then
begin
InsertNode(bucket, node, aitem);
Result := true;
end else
Result := false;
end else
begin
InsertNode(bucket, node, aitem);
Result := true;
end;
CheckMaxFillRatio;
end;
procedure THashTable.Delete(pos : TSetIterator);
var
aitem : ItemType;
begin
Assert(pos is THashTableIterator, msgInvalidIterator);
aitem := ExtractNode(THashTableIterator(pos).FBucket,
THashTableIterator(pos).FNode);
DisposeItem(aitem);
end;
function THashTable.Delete(aitem : ItemType) : SizeType;
var
bucket : IndexType;
node, nnode, fnode : PHashNode;
begin
CheckMinFillRatio;
Result := 0;
if FindNode(aitem, bucket, node) then
begin
if node = nil then
begin
DisposeItem(FBuckets[bucket].Item);
Inc(Result);
node := FBuckets[bucket].Next;
while (node <> nil) and
(_mcp_equal(node^.Item, aitem)) do
begin
nnode := node^.Next;
DisposeItem(node^.Item);
DisposeNode(node);
node := nnode;
Inc(Result);
end;
if node = nil then
begin
with FBuckets[bucket] do
begin
Item := DefaultItem;
Next := @FBuckets[bucket];
end;
end else
begin
with FBuckets[bucket] do
begin
Item := node^.Item;
Next := node^.Next;
end;
DisposeNode(node);
end;
end else
begin
fnode := node;
node := node^.Next;
repeat
nnode := node^.Next;
DisposeItem(node^.Item);
DisposeNode(node);
Inc(Result);
node := nnode;
until (node = nil) or (not _mcp_equal(node^.Item, aitem));
fnode^.Next := node;
end;
FFirstUsedBucket := -1;
end;
Dec(FSize, Result);
end;
function THashTable.LowerBound(aitem : ItemType) : TSetIterator;
var
bucket : IndexType;
node : PHashNode;
begin
FindNode(aitem, bucket, node);
Result := THashTableIterator.Create(bucket, node, self);
end;
function THashTable.UpperBound(aitem : ItemType) : TSetIterator;
var
bucket : IndexType;
node : PHashNode;
begin
if FindNode(aitem, bucket, node) then
begin
EqualItemsAhead(bucket, node, aitem);
end;
Result := THashTableIterator.Create(bucket, node, self);
end;
function THashTable.EqualRange(aitem : ItemType) : TSetIteratorRange;
var
bucket1, bucket2 : IndexType;
node1, node2 : PHashNode;
begin
if FindNode(aitem, bucket1, node1) then
begin
bucket2 := bucket1;
node2 := node1;
EqualItemsAhead(bucket2, node2, aitem);
end else
begin
bucket2 := bucket1;
node2 := node1;
end;
Result := TSetIteratorRange.Create(
THashTableIterator.Create(bucket1, node1, self),
THashTableIterator.Create(bucket2, node2, self)
);
end;
procedure THashTable.Rehash(ex : SizeType);
var
buckets : array of THashNode;
oldCap, NewCapacity, NewTableSize : SizeType;
nnode, node, lnode, temp : PHashNode;
i, lbucket : IndexType;
lastItem : ItemType;
lastItemValid : Boolean;
blist : PHashNode; { list of buckets that can be reused }
procedure GetNewNode(var node : PHashNode);
begin
if blist <> nil then
begin
node := blist;
blist := blist^.Next;
end else
NewNode(node); { may raise }
end;
procedure ReInsert(aitem : ItemType);
begin
if (lastItemValid) and (_mcp_equal(aitem, lastItem)) then
begin
temp := lnode^.Next;
GetNewNode(lnode^.Next); { may raise }
lnode := lnode^.Next;
with lnode^ do
begin
Item := aitem;
Next := temp;
end;
end else
begin
lbucket := GetBucketIndex(Hasher.Hash(aitem));
lnode := @FBuckets[lbucket];
if lnode^.Next = lnode then
begin
lnode^.Item := aitem;
lnode^.Next := nil;
end else
begin
GetNewNode(temp); { may raise }
temp^.Item := lnode^.Item;
temp^.Next := lnode^.Next;
lnode^.Next := temp;
lnode^.Item := aitem;
end;
end;
lastItem := aitem;
lastItemValid := true;
end; { end ReInsert }
procedure ReInsertItems;
var
i : IndexType;
begin
lnode := nil;
blist := nil;
lastItem := DefaultItem;
lastItemValid := false;
for i := 0 to oldCap - 1 do
begin
if buckets[i].Next <> @buckets[i] then
begin
node := @buckets[i];
{ may raise here (and only here) if there are no more
nodes left in blist and there is not enough memory to
allocate a new node; this is possible only if ex < 0 }
ReInsert(node^.Item);
node := node^.Next;
while node <> nil do
begin
nnode := node^.Next;
{ add node to the list of unused bucket nodes }
node^.Next := blist;
blist := node;
{ cannot raise here because there is now at least one
node in blist }
ReInsert(node^.Item);
node := nnode;
end;
{ in case of an exception the fields at the current
position to defaults (to avoid doing it when an
exception occurs) }
with buckets[i] do
begin
{ this should not be &<_mcp_set_zero>'ed; &<_mcp_set_zero>
should (and must!) be used only for newly allocated
items }
Item := DefaultItem;
Next := @buckets[i];
end;
end;
end; { end for }
end; { end ReInsertItems }
procedure DeallocateOldBuckets;
begin
while blist <> nil do
begin
nnode := blist^.Next;
DisposeNode(blist);
blist := nnode;
end;
SetLength(buckets, 0);
end;
begin
Assert(FTableSize + ex >= htMinTableSize, msgContainerTooSmall);
{$ifdef TEST_PASCAL_ADT }
{ LogStatus('Rehash'); }
{$endif }
oldCap := FCapacity;
NewTableSize := FTableSize + ex;
NewCapacity := CalculateCapacity(NewTableSize);
SetLength(buckets, NewCapacity); { may raise }
ExchangePtr(buckets, FBuckets);
FCapacity := NewCapacity;
FTableSize := NewTableSize;
FFirstUsedBucket := -1;
for i := 0 to FCapacity - 1 do
begin
with FBuckets[i] do
begin
_mcp_set_zero(Item);
Next := @FBuckets[i];
end;
end;
try
ReInsertItems;
except
{ no nodes were deleted up to now, so we may be sure that no
memory will be attempted to be allocated while calling
ReInsert; note that additional memory for nodes may be needed
only we we make the table smaller, so if there is an exception
raised while trying to allocate a node then we may be sure
that the old table needs less memory for nodes than the new
one; hence, while re-inserting back to the old table we won't
need any more additional memory; therefore, we just exchange
the table and use the procedure ReInsertItems to insert items
back to the old table }
ExchangePtr(FBuckets, buckets);
FCapacity := oldCap;
oldCap := NewCapacity;
FTableSize := FTableSize - ex;
ReInsertItems;
DeallocateOldBuckets;
raise;
end;
DeallocateOldBuckets;
FCanShrink := false;
{ it is desirable not to shrink the table if it were rehashed by a
user to make place for data that is going to be inserted, so we
set FCanShrink to false; if Rehash is called internally then
FCanShrink is reset to true after the call. }
end; { end Rehash }
procedure THashTable.Clear;
begin
ClearBuckets;
SetLength(FBuckets, 0);
InitBuckets;
GrabageCollector.FreeObjects;
end;
function THashTable.Empty : Boolean;
begin
Result := FSize = 0;
end;
function THashTable.Size : SizeType;
begin
Result := FSize;
end;
function THashTable.MinCapacity : SizeType;
begin
Result := CalculateCapacity(htMinTableSize);
end;
{ -------------------------- THashTableIterator ----------------------------- }
constructor THashTableIterator.Create(abucket : IndexType; anode : PHashNode;