forked from harrisonpartch/spasim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContainers.Mod
1412 lines (1217 loc) · 29.8 KB
/
Containers.Mod
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
MODULE Containers; (** AUTHOR "SAGE"; PURPOSE "Generic Containers for different base
and complex types"; *)
(*
2013/03/14 - SAGE: Dictionary Remove method implemented.
2013/02/24 - SAGE: Renamed List to Vector (C++ STL notation), because it's more
clear name for structure based on dynamic array and turned back to traditional
list growths strategy (increasing array size twice). Added Dictionary class
based on simple implementation (simple collision resolving technique and ) of
hash table, suitable for making on top of it Map and Set structures in C++ STL
notation. Moved Heap class from BinaryHeaps module.
2013/02/10 - SAGE: Much more effective (for about 40% less comparisons needed in some
algorithms) implementation of binary search algorithm. Binary search algorithm
integer overflow bug of middle position calculation fixed. Special list growth
strategy for preventing waste of memory.
*)
IMPORT
Strings, UTF8Strings,
Commands;
CONST
VECTOR_SORTED* = 0;
VECTOR_NO_DUPLICATES* = 1;
BASE_DWORD = 32;
TYPE
LongintArray = POINTER TO ARRAY OF LONGINT;
SetArray = POINTER TO ARRAY OF SET;
LongintItem* = POINTER TO RECORD
value*: LONGINT;
END;
StringItem* = POINTER TO RECORD
value*: Strings.String;
END;
AnyArray* = POINTER TO ARRAY OF ANY;
(** CompareMethod defines a Method that compares two Objects.
The Method then returns:
-1 if the first Object is "smaller" then the second Object
0 if both Objects are "equal"
1 if the first Object is "greater" then the second Object. *)
CompareMethod* = PROCEDURE {DELEGATE} (first, second: ANY): LONGINT;
(** EqualityCompareMethod defines a Method that compares two Objects.
The Methods then returns:
TRUE if both Objects are "equal". *)
EqualityCompareMethod* = PROCEDURE {DELEGATE} (first, second: ANY): BOOLEAN;
(** HashMethod defines a Method that returns hash code for Object.
The Methods then returns:
hash code. *)
HashMethod* = PROCEDURE {DELEGATE} (item: ANY): LONGINT;
(** Base Vector container *)
Vector* = OBJECT
VAR
array: AnyArray;
nCount, nReadLock: LONGINT;
compare: CompareMethod;
bSorted, bNoDuplicates: BOOLEAN;
PROCEDURE &Init*(compare: CompareMethod; options: SET);
BEGIN
SELF.compare := compare;
nReadLock := 0;
nCount := 0;
bSorted := {VECTOR_SORTED} * options # {};
bNoDuplicates := {VECTOR_NO_DUPLICATES} * options # {};
NEW(array, 4)
END Init;
(** Lock prevents modifications to the list. All calls to Lock
must be followed by a call to Unlock. Lock can be nested. *)
PROCEDURE Lock;
BEGIN {EXCLUSIVE}
INC(nReadLock); ASSERT(nReadLock > 0)
END Lock;
(** Unlock removes one modification lock. All calls to Unlock
must be preceeded by a call to Lock. *)
PROCEDURE Unlock;
BEGIN {EXCLUSIVE}
DEC(nReadLock); ASSERT(nReadLock >= 0)
END Unlock;
PROCEDURE Grow;
VAR
old: AnyArray;
nLen, i: LONGINT;
BEGIN
old := array;
nLen := LEN(old);
(*
(*
Special list growth strategy for
preventing waste of memory.
Same strategy used in Qt containers.
*)
IF nLen < 20 THEN
INC(nLen, 4)
ELSIF nLen < 4084 THEN
nLen := nLen * 2 + 12
ELSE
INC(nLen, 2048)
END;
*)
(*nLen := nLen + nLen DIV 2;*)
nLen := nLen*2;
NEW(array, nLen);
FOR i := 0 TO LEN(old) - 1 DO
array[i] := old[i]
END
END Grow;
PROCEDURE FindSequentially(x: ANY): LONGINT;
VAR
i: LONGINT;
BEGIN
i := 0;
WHILE i < nCount DO
IF compare(x, array[i]) = 0 THEN
RETURN i
END;
INC(i)
END;
RETURN -1
END FindSequentially;
(*
PROCEDURE FindPosition(x: ANY): LONGINT;
VAR
lower, middle, upper: LONGINT;
value: LONGINT;
BEGIN
IF count = 0 THEN RETURN 0 END;
IF compare(list[0], x) > 0 THEN RETURN 0 END;
IF compare(list[count-1], x) < 0 THEN RETURN count END;
lower := 0;
upper := count - 1;
WHILE (upper - lower) > 1 DO
middle := (upper + lower) DIV 2;
value := compare(list[middle], x);
IF value = 0 THEN RETURN middle END;
IF value < 0 THEN
lower := middle
ELSE
upper := middle
END;
END;
IF compare(list[lower], x) = 0 THEN
RETURN lower
ELSE
RETURN upper
END;
END FindPosition;
*)
PROCEDURE FindPosition(x: ANY; VAR bFound: BOOLEAN): LONGINT;
VAR
lower, middle, upper: LONGINT;
value: LONGINT;
BEGIN
bFound := FALSE;
IF nCount = 0 THEN RETURN 0 END;
lower := 0;
upper := nCount - 1;
WHILE lower <= upper DO
middle := lower + (upper - lower) DIV 2;
value := compare(array[middle], x);
IF value = 0 THEN
bFound := TRUE;
RETURN middle
ELSIF value < 0 THEN
lower := middle + 1
ELSE
upper := middle - 1
END;
END;
IF lower <= upper THEN
RETURN upper
ELSE
RETURN lower
END;
END FindPosition;
(** return the index of an object. In a multi-process situation, the process calling the IndexOf method should
call Lock before IndexOf and Unlock after the last use of an index based on IndexOf.
If the object is not found, -1 is returned *)
PROCEDURE IndexOf*(x: ANY): LONGINT;
VAR
pos: LONGINT;
bFound: BOOLEAN;
BEGIN {EXCLUSIVE}
IF bSorted THEN
pos := FindPosition(x, bFound);
IF bFound THEN
RETURN pos
ELSE
RETURN -1
END
ELSE
RETURN FindSequentially(x)
END
END IndexOf;
(** return the number of objects in the list. If count is used for indexing elements (e.g. FOR - Loop) in a multi-process
situation, the process calling the GetCount method should call Lock before GetCount and Unlock after the
last use of an index based on GetCount *)
PROCEDURE GetCount*():LONGINT;
BEGIN
RETURN nCount
END GetCount;
(** return an object based on an index. In a multi-process situation, GetItem is only safe in a locked region Lock / Unlock *)
PROCEDURE GetItem*(pos: LONGINT): ANY;
BEGIN
ASSERT((pos >= 0) & (pos < nCount), 101);
RETURN array[pos]
END GetItem;
(** Add an object to the list. Add may block if number of
calls to Lock is bigger than the number of calls to Unlock *)
PROCEDURE Add*(x: ANY);
BEGIN {EXCLUSIVE}
AWAIT(nReadLock = 0);
IF bSorted THEN AddUnlocked(x) ELSE AppendUnlocked(x) END
END Add;
PROCEDURE Insert*(pos: LONGINT; x: ANY);
BEGIN {EXCLUSIVE}
AWAIT(nReadLock = 0);
ASSERT((pos >= 0) & (pos < nCount), 101);
IF bSorted THEN AddUnlocked(x) ELSE InsertUnlocked(pos, x) END
END Insert;
(** Does the actual Adding without locking (should already
have been done by the caller) *)
PROCEDURE AddUnlocked(x: ANY);
VAR
i, pos: LONGINT;
bFound: BOOLEAN;
BEGIN
pos := FindPosition(x, bFound);
IF bNoDuplicates THEN
ASSERT(~bFound)
END;
IF nCount = LEN(array) THEN Grow END;
i := nCount - 1;
WHILE i >= pos DO
array[i + 1] := array[i];
DEC(i)
END;
array[pos] := x;
INC(nCount)
END AddUnlocked;
PROCEDURE AppendUnlocked(x: ANY);
BEGIN
IF bNoDuplicates THEN
ASSERT(FindSequentially(x) = -1)
END;
IF nCount = LEN(array) THEN Grow END;
array[nCount] := x;
INC(nCount)
END AppendUnlocked;
PROCEDURE InsertUnlocked(pos: LONGINT; x: ANY);
VAR
i: LONGINT;
BEGIN
IF bNoDuplicates THEN
ASSERT(FindSequentially(x) = -1)
END;
IF nCount = LEN(array) THEN Grow END;
i := nCount - 1;
WHILE i >= pos DO
array[i + 1] := array[i];
DEC(i)
END;
array[pos] := x;
INC(nCount)
END InsertUnlocked;
(** Remove an object from the list. Remove may block if number of calls to Lock is bigger than the number of calls to Unlock *)
PROCEDURE Remove*(pos: LONGINT);
BEGIN {EXCLUSIVE}
AWAIT(nReadLock = 0);
ASSERT((pos >= 0) & (pos < nCount), 101);
RemoveUnlocked(pos)
END Remove;
(* Does the actual Removing without locking (should already have been done by the caller) *)
PROCEDURE RemoveUnlocked(pos: LONGINT);
BEGIN
WHILE pos < nCount - 1 DO
array[pos] := array[pos + 1];
INC(pos)
END;
DEC(nCount);
array[nCount] := NIL
END RemoveUnlocked;
(** atomic replace x by y. That means that x is removed and y is added to the SortedList *)
(*PROCEDURE Replace*(x, y: ANY);
BEGIN {EXCLUSIVE}
AWAIT(readLock = 0);
RemoveUnlocked(x);
AddUnlocked(y);
END Replace;*)
END Vector;
(** Custom Vector container for making on top of it of the vectors for any data types *)
CustomVector* = OBJECT
VAR
vector*: Vector;
PROCEDURE Lock*;
BEGIN
vector.Lock
END Lock;
PROCEDURE Unlock*;
BEGIN
vector.Unlock
END Unlock;
PROCEDURE Remove*(i: LONGINT);
BEGIN
vector.Remove(i)
END Remove;
PROCEDURE GetCount*(): LONGINT;
BEGIN
RETURN vector.GetCount()
END GetCount;
PROCEDURE IsEmpty*(): BOOLEAN;
BEGIN
RETURN vector.GetCount() = 0
END IsEmpty;
END CustomVector;
(** Vector of LONGINTs *)
LongintVector* = OBJECT(CustomVector)
PROCEDURE &New*(options: SET);
BEGIN
NEW(vector, Compare, options)
END New;
PROCEDURE Compare(first, second: ANY): LONGINT;
VAR
nFirst, nSecond: LONGINT;
BEGIN
nFirst := first(LongintItem).value;
nSecond := second(LongintItem).value;
IF nFirst < nSecond THEN
RETURN -1
ELSIF nFirst > nSecond THEN
RETURN 1
ELSE
RETURN 0
END
END Compare;
PROCEDURE Add*(x: LONGINT);
VAR
item: LongintItem;
BEGIN
NEW(item);
item.value := x;
vector.Add(item)
END Add;
PROCEDURE Insert*(pos: LONGINT; x: LONGINT);
VAR
item: LongintItem;
BEGIN
NEW(item);
item.value := x;
vector.Insert(pos, item)
END Insert;
PROCEDURE IndexOf*(x: LONGINT): LONGINT;
VAR
item: LongintItem;
BEGIN
NEW(item);
item.value := x;
RETURN vector.IndexOf(item)
END IndexOf;
PROCEDURE GetItem*(i: LONGINT): LONGINT;
BEGIN
RETURN vector.GetItem(i)(LongintItem).value
END GetItem;
END LongintVector;
(** Vector of LONGINT vectors *)
LongintVectorVector* = OBJECT(CustomVector)
PROCEDURE &New*(options: SET);
BEGIN
NEW(vector, Compare, options)
END New;
PROCEDURE Compare(first, second: ANY): LONGINT;
BEGIN
RETURN CompareLongintVectors(first(LongintVector), second(LongintVector))
END Compare;
PROCEDURE Add*(x: LongintVector);
BEGIN
vector.Add(x)
END Add;
PROCEDURE Insert*(pos: LONGINT; x: LongintVector);
BEGIN
vector.Insert(pos, x)
END Insert;
PROCEDURE IndexOf*(x: LongintVector): LONGINT;
BEGIN
RETURN vector.IndexOf(x)
END IndexOf;
PROCEDURE GetItem*(i: LONGINT): LongintVector;
BEGIN
RETURN vector.GetItem(i)(LongintVector)
END GetItem;
END LongintVectorVector;
(** Vector of strings *)
StringVector* = OBJECT(CustomVector)
PROCEDURE &New*(options: SET);
BEGIN
NEW(vector, Compare, options)
END New;
PROCEDURE Compare(first, second: ANY): LONGINT;
BEGIN
RETURN UTF8Strings.Compare(first(StringItem).value^,
second(StringItem).value^)
END Compare;
PROCEDURE Add*(CONST x: ARRAY OF CHAR);
VAR
item: StringItem;
BEGIN
NEW(item);
item.value := Strings.NewString(x);
vector.Add(item)
END Add;
PROCEDURE Insert*(pos: LONGINT; CONST x: ARRAY OF CHAR);
VAR
item: StringItem;
BEGIN
NEW(item);
item.value := Strings.NewString(x);
vector.Insert(pos, item)
END Insert;
PROCEDURE IndexOf*(CONST x: ARRAY OF CHAR): LONGINT;
VAR
item: StringItem;
BEGIN
NEW(item);
item.value := Strings.NewString(x);
RETURN vector.IndexOf(item)
END IndexOf;
PROCEDURE GetItem*(i: LONGINT): Strings.String;
BEGIN
RETURN vector.GetItem(i)(StringItem).value
END GetItem;
END StringVector;
(** Base Dictionary container *)
Dictionary* = OBJECT
VAR
array: AnyArray;
hashes: LongintArray;
deleted: SetArray;
iterator*: DictionaryIterator;
nCount, iPrime-, nCollisions-: LONGINT;
equal: EqualityCompareMethod;
hash: HashMethod;
PROCEDURE &Init*(equal: EqualityCompareMethod; hash: HashMethod);
BEGIN
SELF.equal := equal;
SELF.hash := hash;
nCount := 0;
iPrime := 0;
nCollisions := 0;
InitArrays;
NEW(iterator, SELF);
END Init;
PROCEDURE InitArrays;
VAR
i, prime: LONGINT;
BEGIN
prime := PRIMES[iPrime];
NEW(array, prime);
FOR i := 0 TO LEN(array) - 1 DO
array[i] := NIL
END;
NEW(hashes, prime);
NEW(deleted, prime DIV BASE_DWORD + 1);
FOR i := 0 TO LEN(deleted) - 1 DO
deleted[i] := {}
END;
END InitArrays;
PROCEDURE Grow;
VAR
oldArray: AnyArray;
oldHashes: LongintArray;
oldDeleted: SetArray;
i: LONGINT;
PROCEDURE Deleted(i: LONGINT): BOOLEAN;
VAR
iDiv, iMod: LONGINT;
BEGIN
iDiv := i DIV BASE_DWORD;
iMod := i MOD BASE_DWORD;
RETURN oldDeleted[iDiv] * {iMod} # {}
END Deleted;
BEGIN
oldArray := array;
oldHashes := hashes;
oldDeleted := deleted;
INC(iPrime);
InitArrays;
iterator.Init(SELF);
FOR i := 0 TO LEN(oldArray) - 1 DO
IF (oldArray[i] # NIL) & ~Deleted(i) THEN
ASSERT(~hashSearch(oldArray[i], TRUE, TRUE, oldHashes[i]))
END
END
END Grow;
PROCEDURE Delete(i: LONGINT);
VAR
iDiv, iMod: LONGINT;
BEGIN
iDiv := i DIV BASE_DWORD;
iMod := i MOD BASE_DWORD;
deleted[iDiv] := deleted[iDiv] + {iMod}
END Delete;
PROCEDURE Deleted(i: LONGINT): BOOLEAN;
VAR
iDiv, iMod: LONGINT;
BEGIN
iDiv := i DIV BASE_DWORD;
iMod := i MOD BASE_DWORD;
RETURN deleted[iDiv] * {iMod} # {}
END Deleted;
PROCEDURE GetCount(): LONGINT;
BEGIN
RETURN nCount
END GetCount;
PROCEDURE Add*(item: ANY);
VAR
nHashCode: LONGINT;
BEGIN
ASSERT(~hashSearch(item, TRUE, FALSE, nHashCode));
INC(nCount);
(* fill factor 5/8 = 0.625 (near to 0.63) *)
IF (nCount*8) DIV 5 >= PRIMES[iPrime] THEN
Grow
END;
END Add;
PROCEDURE Contains*(item: ANY): BOOLEAN;
VAR
nHashCode: LONGINT;
BEGIN
RETURN hashSearch(item, FALSE, FALSE, nHashCode);
END Contains;
PROCEDURE Get*(item: ANY): ANY;
VAR
nHashCode: LONGINT;
itemResult: ANY;
BEGIN
itemResult := NIL;
IF hashSearch(item, FALSE, FALSE, nHashCode) THEN
itemResult := array[nHashCode]
END;
RETURN itemResult;
END Get;
PROCEDURE Remove*(item: ANY);
VAR
nHashCode: LONGINT;
BEGIN
IF hashSearch(item, FALSE, FALSE, nHashCode) THEN
Delete(nHashCode);
DEC(nCount);
END;
END Remove;
PROCEDURE hashSearch(item: ANY; bAddAllowed: BOOLEAN; bGrowOperation: BOOLEAN; VAR nHashCode: LONGINT): BOOLEAN;
VAR
d, nHash, h, prime: LONGINT;
bFound, bExit, bOverflow: BOOLEAN;
BEGIN
IF bGrowOperation THEN
nHash := nHashCode;
ELSE
nHash := ABS(hash(item));
END;
LOOP
d := 1;
bExit := FALSE;
bFound := FALSE;
bOverflow := FALSE;
prime := PRIMES[iPrime];
h := nHash MOD prime;
WHILE ~(bFound OR bExit) DO
IF (array[h] = NIL) OR (~bGrowOperation & (array[h] # NIL) & Deleted(h) & bAddAllowed) THEN (* new entry *)
bExit := TRUE;
IF bAddAllowed THEN
array[h] := item;
hashes[h] := nHash;
nHashCode := h
END
ELSIF ~bGrowOperation & ~Deleted(h) & equal(array[h], item) THEN (* match *)
bFound := TRUE;
nHashCode := h;
ELSE (* collision *)
INC(nCollisions);
h := h + d; d := d + 2;
IF h >= prime THEN h := h - prime END;
IF d = prime THEN (* Table owerflow! *)
bExit := TRUE;
bOverflow := bAddAllowed;
END
END
END;
IF bOverflow THEN
Grow
ELSE
EXIT
END
END;
RETURN bFound
END hashSearch;
END Dictionary;
(** Iterator for sequental access to the Dictionary *)
DictionaryIterator* = OBJECT
VAR
iCurrentPos: LONGINT;
dictionary: Dictionary;
PROCEDURE &New(dictionary: Dictionary);
BEGIN
Init(dictionary);
END New;
PROCEDURE Init(dictionary: Dictionary);
BEGIN
SELF.dictionary := dictionary;
iCurrentPos := -1;
END Init;
PROCEDURE Reset*;
BEGIN
iCurrentPos := -1;
END Reset;
PROCEDURE HasNext*(): BOOLEAN;
VAR
i: LONGINT;
BEGIN
i := iCurrentPos;
REPEAT
INC(i);
UNTIL (i >= LEN(dictionary.array)) OR ((dictionary.array[i] # NIL) & ~dictionary.Deleted(i));
RETURN i < LEN(dictionary.array)
END HasNext;
PROCEDURE GetNext*(): ANY;
VAR
item: ANY;
BEGIN
REPEAT
INC(iCurrentPos);
UNTIL (iCurrentPos >= LEN(dictionary.array)) OR ((dictionary.array[iCurrentPos] # NIL) & ~dictionary.Deleted(iCurrentPos));
item := NIL;
IF iCurrentPos < LEN(dictionary.array) THEN
item := dictionary.array[iCurrentPos];
END;
RETURN item
END GetNext;
END DictionaryIterator;
(** Custom Set container for making on top of it of
the sets (sets consists of only pure keys without any other data)
and maps (maps consists of pairs key:value) for any data types *)
CustomSet* = OBJECT
VAR
dictionary*: Dictionary;
PROCEDURE GetCount*(): LONGINT;
BEGIN
RETURN dictionary.GetCount()
END GetCount;
PROCEDURE IsEmpty*(): BOOLEAN;
BEGIN
RETURN dictionary.GetCount() = 0
END IsEmpty;
PROCEDURE Reset*;
BEGIN
dictionary.iterator.Reset
END Reset;
PROCEDURE HasNext*(): BOOLEAN;
BEGIN
RETURN dictionary.iterator.HasNext()
END HasNext;
END CustomSet;
(** Set of LONGINTs *)
LongintSet* = OBJECT(CustomSet)
PROCEDURE LongintItemsEqual(first, second: ANY): BOOLEAN;
BEGIN
RETURN first(LongintItem).value = second(LongintItem).value
END LongintItemsEqual;
PROCEDURE LongintItemHash(item: ANY): LONGINT;
BEGIN
RETURN item(LongintItem).value
END LongintItemHash;
PROCEDURE &Init*;
BEGIN
NEW(dictionary, LongintItemsEqual, LongintItemHash);
END Init;
PROCEDURE Add*(x: LONGINT);
VAR
item: LongintItem;
BEGIN
NEW(item);
item.value := x;
dictionary.Add(item)
END Add;
PROCEDURE Remove*(x: LONGINT);
VAR
item: LongintItem;
BEGIN
NEW(item);
item.value := x;
dictionary.Remove(item)
END Remove;
PROCEDURE Contains*(x: LONGINT): BOOLEAN;
VAR
item: LongintItem;
BEGIN
NEW(item);
item.value := x;
RETURN dictionary.Contains(item)
END Contains;
PROCEDURE GetNext*(): LONGINT;
BEGIN
RETURN dictionary.iterator.GetNext()(LongintItem).value
END GetNext;
PROCEDURE ToString*(): Strings.String;
VAR
a: ARRAY 32 OF CHAR;
s: Strings.String;
BEGIN
Reset;
s := Strings.NewString("{");
WHILE HasNext() DO
Strings.IntToStr(GetNext(), a);
s := Strings.ConcatToNew(s^, a);
IF HasNext() THEN
s := Strings.ConcatToNew(s^, ", ")
END
END;
s := Strings.ConcatToNew(s^, "}");
RETURN s
END ToString;
END LongintSet;
(** Set of Strings *)
StringSet* = OBJECT(CustomSet)
PROCEDURE StringsEqual(first, second: ANY): BOOLEAN;
BEGIN
RETURN first(StringItem).value^ = second(StringItem).value^
END StringsEqual;
PROCEDURE StringHash(item: ANY): LONGINT;
BEGIN
RETURN HashString(item(StringItem).value)
END StringHash;
PROCEDURE &Init*;
BEGIN
NEW(dictionary, StringsEqual, StringHash);
END Init;
PROCEDURE Add*(CONST x: ARRAY OF CHAR);
VAR
item: StringItem;
BEGIN
NEW(item);
item.value := Strings.NewString(x);
dictionary.Add(item)
END Add;
PROCEDURE Remove*(CONST x: ARRAY OF CHAR);
VAR
item: StringItem;
BEGIN
NEW(item);
item.value := Strings.NewString(x);
dictionary.Remove(item)
END Remove;
PROCEDURE Contains*(CONST x: ARRAY OF CHAR): BOOLEAN;
VAR
item: StringItem;
BEGIN
NEW(item);
item.value := Strings.NewString(x);
RETURN dictionary.Contains(item)
END Contains;
PROCEDURE GetNext*(): Strings.String;
BEGIN
RETURN dictionary.iterator.GetNext()(StringItem).value
END GetNext;
END StringSet;
(** Set of LONGINT sets *)
LongintSetSet* = OBJECT(CustomSet)
PROCEDURE LongintSetsEqual(first, second: ANY): BOOLEAN;
BEGIN
RETURN EqualityCompareLongintSets(first(LongintSet), second(LongintSet))
END LongintSetsEqual;
PROCEDURE LongintSetHash(item: ANY): LONGINT;
BEGIN
RETURN HashLongintSet(item(LongintSet))
END LongintSetHash;
PROCEDURE &Init*;
BEGIN
NEW(dictionary, LongintSetsEqual, LongintSetHash)
END Init;
PROCEDURE Add*(x: LongintSet);
BEGIN
dictionary.Add(x)
END Add;
PROCEDURE Remove*(x: LongintSet);
BEGIN
dictionary.Remove(x)
END Remove;
PROCEDURE Contains*(x: LongintSet): BOOLEAN;
BEGIN
RETURN dictionary.Contains(x)
END Contains;
PROCEDURE GetNext*(): LongintSet;
BEGIN
RETURN dictionary.iterator.GetNext()(LongintSet)
END GetNext;
END LongintSetSet;
(** Vector of LONGINT sets *)
LongintSetVector* = OBJECT(CustomVector)
PROCEDURE &New*(options: SET);
BEGIN
NEW(vector, Compare, options)
END New;
PROCEDURE Compare(first, second: ANY): LONGINT;
BEGIN
RETURN CompareLongintSets(first(LongintSet), second(LongintSet))
END Compare;
PROCEDURE Add*(x: LongintSet);
BEGIN
vector.Add(x)
END Add;
PROCEDURE Insert*(pos: LONGINT; x: LongintSet);
BEGIN
vector.Insert(pos, x)
END Insert;
PROCEDURE IndexOf*(x: LongintSet): LONGINT;
BEGIN
RETURN vector.IndexOf(x)
END IndexOf;
PROCEDURE GetItem*(i: LONGINT): LongintSet;
BEGIN
RETURN vector.GetItem(i)(LongintSet)
END GetItem;
END LongintSetVector;
(** Binary heap. *)
Heap* = OBJECT
VAR
array: AnyArray;
nCount, nReadLock: LONGINT;
compare: CompareMethod;
PROCEDURE &Init*(compare: CompareMethod);
BEGIN
SELF.compare := compare;
nReadLock := 0;
nCount := 0;
NEW(array, 8)
END Init;
(** Lock prevents modifications to the list. All calls to Lock
must be followed by a call to Unlock. Lock can be nested. *)
PROCEDURE Lock*;
BEGIN {EXCLUSIVE}
INC(nReadLock); ASSERT(nReadLock > 0)
END Lock;
(** Unlock removes one modification lock. All calls to Unlock
must be preceeded by a call to Lock. *)
PROCEDURE Unlock*;
BEGIN {EXCLUSIVE}
DEC(nReadLock); ASSERT(nReadLock >= 0)
END Unlock;
PROCEDURE Grow;