-
Notifications
You must be signed in to change notification settings - Fork 283
/
gcontnrs.pas
8888 lines (7379 loc) · 214 KB
/
gcontnrs.pas
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
{
Copyright (C) 2014 Yann Mérignac
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.
As a special exception, the copyright holders of this library give
you permission to link this library with independent modules to
produce an executable, regardless of the license terms of these
independent modules,and to copy and distribute the resulting
executable under terms of your choice, provided that you also meet,
for each linked independent module, the terms and conditions of the
license of that module. An independent module is a module which is
not derived from or based on this library. If you modify this
library, you may extend this exception to your version of the
library, but you are not obligated to do so. If you do not wish to
do so, delete this exception statement from your version.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see
<http://www.gnu.org/licenses/>.
}
unit GContnrs;
{$mode objfpc}{$H+}
interface
uses Classes, SysUtils;
const
MIN_BUCKET_COUNT = 4;
MAX_BUCKET_COUNT = 1 shl 30;
DEFAULT_HASHMAP_LOAD_FACTOR = 1.0;
type
EContainerError = class(Exception);
{ TContainer }
TContainer = class
protected
procedure RaiseContainerEmpty;
procedure RaiseCursorDenotesWrongContainer;
procedure RaiseCursorIsNil;
procedure RaiseError(const Msg: String);
procedure RaiseIndexOutOfRange;
procedure RaiseItemAlreadyInSet;
procedure RaiseItemNotInSet;
procedure RaiseKeyAlreadyInMap;
procedure RaiseKeyNotInMap;
procedure RaiseMethodNotRedefined;
procedure Unused(P: Pointer); inline;
end;
{ TGenEnumerator }
generic TGenEnumerator<_TItem_, _TPosition_> = class
public type
TGetCurrent = function(const Pos: _TPosition_) : _TItem_ of object;
TMoveNext = function(var Pos:_TPosition_) : Boolean of object;
private
fGetter : TGetCurrent;
fMover : TMoveNext;
fPos : _TPosition_;
function GetCurrent : _TItem_;
public
constructor Create(const Pos : _TPosition_; Mover: TMoveNext;
Getter: TGetCurrent);
function MoveNext: Boolean;
property Current: _TItem_ read GetCurrent;
end;
{ TAbstractVector }
TAbstractVector = class(TContainer)
protected
fCapacity : Integer;
fSize : Integer;
procedure CheckIndex(Index: Integer); inline;
procedure CheckIndexForAdd(Index: Integer); inline;
procedure InsertSpaceFast(Position, Count: Integer); virtual; abstract;
function ItemToString(Index: Integer) : String; virtual; abstract;
procedure SetCapacity(ACapacity : Integer); virtual; abstract;
public
{** Removes all the items from the container. }
procedure Clear;
{** Deletes Count items begining at Position. }
procedure Delete(Position: Integer; Count: Integer = 1);
{** Deletes the first Count items. }
procedure DeleteFirst(Count: Integer = 1);
{** Deletes the last Count items. }
procedure DeleteLast(Count: Integer = 1);
{** Deletes all items in the range [PosFrom..PosTo]. }
procedure DeleteRange(PosFrom, PosTo: Integer);
{** Inserts Count undefined items at Position. }
procedure InsertSpace(Position: Integer; Count: Integer = 1);
{** Returns true if the container is empty. }
function IsEmpty: Boolean; inline;
{** Copies Count items from Src to Dst. }
procedure Move(Src, Dst, Count: Integer); virtual; abstract;
{** If necessary, increases the capacity of the container to ensure that it
can hold at least MinCapacity items. }
procedure Reserve(MinCapacity: Integer);
{** Resizes the container to contain NewSize items. }
procedure Resize(NewSize: Integer);
{** Reorders the items in reverse order. }
procedure Reverse;
{** Reorders the items in the range [PosFrom..PosTo] in reverse order. }
procedure ReverseRange(PosFrom, PosTo: Integer);
{** Rearrange items randomly. }
procedure Shuffle;
{** Rearrange items in the range [PosFrom..PosTo] randomly. }
procedure Shuffle(PosFrom, PosTo: Integer);
{** Swaps the values of the items designated by I and J. }
procedure Swap(I, J: Integer);
{** Swaps the values of the items designated by I and J (no bounds check). }
procedure SwapFast(I, J: Integer); virtual; abstract;
{** Return a string representation for the container. }
function ToString : String; override;
{** Capacity of the container. }
property Capacity : Integer read fCapacity;
{** Number of items. }
property Size: Integer read fSize;
end;
{ TGenVector }
generic TGenVector<_TItem_> = class(TAbstractVector)
public type
PItem = ^_TItem_;
TCompareItems = function (const A, B: _TItem_) : Integer of object;
TItemToString = function (const Item: _TItem_) : String of object;
TProcessItem = procedure(var Item: _TItem_) of object;
TEnumerator = specialize TGenEnumerator<_TItem_, Integer>;
strict private
fItems : array of _TItem_;
fOnCompareItems: TCompareItems;
fOnItemToString: TItemToString;
function EnumeratorGet(const Pos: Integer) : _TItem_;
function EnumeratorNext(var Pos: Integer) : Boolean;
procedure Fill(Index, Count: Integer; const Value: _TItem_);
function GetItemFast(Position: Integer) : _TItem_; inline;
function GetItemPtrFast(Position: Integer): PItem;
procedure InsertionSort(PosFrom, PosTo: Integer; Comparator: TCompareItems);
procedure Quicksort(Left, Right: Integer; Comparator: TCompareItems);
class procedure RealMove(Src, Dst: TGenVector;
SrcFirst, DstFirst, Count: Integer);
procedure SetOnCompareItems(AValue: TCompareItems);
procedure SetOnItemToString(AValue: TItemToString);
protected
procedure InsertSpaceFast(Position, Count: Integer); override;
function ItemToString(Index: Integer) : String; override;
procedure SetCapacity(ACapacity : Integer); override;
public
{** Inserts Count times Item at the end of the container. }
procedure Append(const Item: _TItem_);
{** Inserts all the items of Src at the end of the container. }
procedure AppendAll(Src: TGenVector);
{** Inserts all the items of Src in the range [PosFrom..PosTo] at the end of
the container. }
procedure AppendRange(Src: TGenVector; PosFrom, PosTo: Integer);
{** Searches for Item using the binary search algorithm. Returns the index of
Item if its found. Otherwise, returns ( - InsertionPoint - 1 ).
InsertionPoint is the point at which the key would be inserted into the
container. }
function BinarySearch(const Item: _TItem_) : Integer;
function BinarySearch(const Item: _TItem_;
Comparator: TCompareItems) : Integer;
{** Searches for Item in range [PosFrom..PosTo] using the binary search
algorithm. Returns the index of Item if its found. Otherwise, returns
( - InsertionPoint - 1 ). InsertionPoint is the point at which the key
would be inserted into the range. }
function BinarySearch(const Item: _TItem_;
PosFrom, PosTo: Integer) : Integer;
function BinarySearch(const Item: _TItem_;
PosFrom, PosTo: Integer; Comparator: TCompareItems) : Integer;
{** Returns true if the container contains Item. }
function Contains(const Item: _TItem_) : Boolean;
function Contains(const Item: _TItem_; Comparator: TCompareItems) : Boolean;
{** Creates an empty vector and sets his capacity to InitialCapacity. }
constructor Create(InitialCapacity: Integer = 16);
function DefaultCompareItems(const A, B: _TItem_) : Integer; virtual;
function DefaultItemToString(const Item: _TItem_) : String; virtual;
{** Destroys the container. }
destructor Destroy; override;
{** If Obj = Self then returns true, else if Obj is not a TGenVector returns
false, else returns true if Self and Obj contain the sames items. }
function Equals(Obj: TObject) : Boolean; override;
function Equals(Obj: TObject; Comparator: TCompareItems) : Boolean;
{** Returns the index of the first item equal to Item or -1. }
function FindIndex(const Item: _TItem_) : Integer;
function FindIndex(const Item: _TItem_;
Comparator: TCompareItems) : Integer;
{** Returns a cursor on the first item equal to Item or NilCursor. The search
starts at the element From. }
function FindIndex(const Item: _TItem_; PosFrom: Integer) : Integer;
function FindIndex(const Item: _TItem_; PosFrom: Integer;
Comparator: TCompareItems) : Integer;
{** Returns the first Item. }
function FirstItem : _TItem_; inline;
function GetEnumerator : TEnumerator;
{** Returns item at position Position. }
function GetItem(Position: Integer) : _TItem_; inline;
{** Returns a pointer designating item at position Position. }
function GetItemPtr(Position: Integer): PItem;
{** Inserts Count times Item before Before. }
procedure Insert(Before: Integer; const Item: _TItem_;
Count: Integer = 1);
{** Inserts all the items of Src before Before. }
procedure InsertAll(Before: Integer; Src: TGenVector);
{** Inserts before Before all the items of Src in the range
[PosFrom..PosTo]. }
procedure InsertRange(Before: Integer; Src: TGenVector;
PosFrom, PosTo: Integer);
{** Returns true if the items are sorted. }
function IsSorted : Boolean;
function IsSorted(Comparator: TCompareItems): Boolean;
{** Invokes Process on each items. }
procedure Iterate(Process: TProcessItem);
{** Invokes Process on each items in range [PosFrom..PosTo]. }
procedure Iterate(Process: TProcessItem; const PosFrom, PosTo: Integer);
{** Returns the last Item. }
function LastItem: _TItem_; inline;
{** Returns index of the greatest item. }
function MaxPos : Integer;
function MaxPos(Comparator: TCompareItems) : Integer;
{** Returns index of the greatest item in the range [PosFrom..PosTo]. }
function MaxPos(PosFrom, PosTo: Integer) : Integer;
function MaxPos(PosFrom, PosTo: Integer;
Comparator: TCompareItems) : Integer;
{** Removes items from Src and inserts them into Self. Afterwards, Self
contains the union of the items that were initially in Src and Self. Src
is left empty. If Self and Src are initially sorted, then Self is
sorted. }
procedure Merge(Src: TGenVector);
procedure Merge(Src: TGenVector; Comparator: TCompareItems);
{** Returns index of the lowest item. }
function MinPos : Integer;
function MinPos(Comparator: TCompareItems) : Integer;
{** Returns index of the lowest item in the range [PosFrom..PosTo]. }
function MinPos(PosFrom, PosTo: Integer) : Integer;
function MinPos(PosFrom, PosTo: Integer;
Comparator: TCompareItems) : Integer;
{** Copies Count items from Src to Dst. }
procedure Move(Src, Dst, Count: Integer); override;
{** Inserts Count times Item at the begining of the container. }
procedure Prepend(const Item: _TItem_; Count: Integer = 1);
{** Inserts all the items of Src at the begining of the container. }
procedure PrependAll(Src: TGenVector);
{** Inserts all the items of Src in the range [PosFrom..PosTo] at the
begining of the container. }
procedure PrependRange(Src: TGenVector; PosFrom, PosTo: Integer);
procedure ReadFirstItem(out Value : _TItem_); inline;
procedure ReadItem(Position: Integer; out Value: _TItem_);
procedure ReadItemFast(Position: Integer; out Value: _TItem_); inline;
procedure ReadLastItem(out Value : _TItem_); inline;
{** Replaces items in range [Index..Index + Count - 1] by Value. }
procedure Replace(Index, Count: Integer; const Value: _TItem_);
{** Returns the index of the first item equal to Item or -1. }
function ReverseFindIndex(const Item: _TItem_) : Integer;
function ReverseFindIndex(const Item: _TItem_;
Comparator: TCompareItems) : Integer;
{** Returns a cursor on the first item equal to Item or NilCursor. The search
starts at the element From. }
function ReverseFindIndex(const Item: _TItem_; PosFrom: Integer) : Integer;
function ReverseFindIndex(const Item: _TItem_;
PosFrom: Integer; Comparator: TCompareItems) : Integer;
{** Assigns the value Value to the item at Position. }
procedure SetItem(Position: Integer; const Value: _TItem_); inline;
procedure SetItemFast(Position: Integer; const Value: _TItem_); inline;
{** Sorts the items. }
procedure Sort;
procedure Sort(Comparator: TCompareItems);
{** Sorts the items in the range [PosFrom..PosTo]. }
procedure Sort(PosFrom, PosTo: Integer);
procedure Sort(PosFrom, PosTo: Integer; Comparator: TCompareItems);
{** Swaps the values of the items designated by I and J (no bounds check). }
procedure SwapFast(I, J: Integer); override;
{** Provides access to the items in the container. }
property Items[Index: Integer] : _TItem_ read GetItemFast
write SetItemFast; default;
{** Provides access to pointers on the items in the container. }
property ItemsPtr[Index: Integer] : PItem read GetItemPtrFast;
property OnCompareItems : TCompareItems read fOnCompareItems
write SetOnCompareItems;
property OnItemToString : TItemToString read fOnItemToString
write SetOnItemToString;
end;
{ TGenDeque }
generic TGenDeque<_TItem_> = class(TAbstractVector)
public type
PItem = ^_TItem_;
TCompareItems = function (const A, B: _TItem_) : Integer of object;
TItemToString = function (const Item: _TItem_) : String of object;
TProcessItem = procedure(var Item: _TItem_) of object;
TEnumerator = specialize TGenEnumerator<_TItem_, Integer>;
strict private
fItems : array of _TItem_;
fOnCompareItems: TCompareItems;
fOnItemToString: TItemToString;
fStart : Integer;
procedure DecRank(var Rank: Integer); inline;
function Equals(Deque: TGenDeque; Comparator: TCompareItems): Boolean;
function EnumeratorGet(const Pos: Integer) : _TItem_;
function EnumeratorNext(var Pos: Integer) : Boolean;
procedure Fill(Index, Count: Integer; const Value: _TItem_);
function GetItemPtrFast(Position: Integer): PItem;
procedure IncRank(var Rank: Integer); inline;
procedure IncreaseCapacity(ACapacity : Integer);
function IndexToRank(Index: Integer) : Integer; inline;
procedure InsertionSort(PosFrom, PosTo: Integer; Comparator: TCompareItems);
procedure Quicksort(Left, Right: Integer; Comparator: TCompareItems);
function RankToIndex(Rank: Integer) : Integer; inline;
class procedure RealMoveIndex(Src, Dst: TGenDeque;
SrcFirst, DstFirst, Count: Integer);
procedure RealMoveRank(Src, Dst, Count: Integer);
procedure ReduceCapacity(ACapacity : Integer);
procedure SetOnCompareItems(AValue: TCompareItems);
procedure SetOnItemToString(AValue: TItemToString);
protected
procedure InsertSpaceFast(Position, Count: Integer); override;
function ItemToString(Index: Integer) : String; override;
procedure SetCapacity(ACapacity : Integer); override;
public
{** Inserts Count times Item at the end of the container. }
procedure Append(const Item: _TItem_; Count: Integer = 1);
{** Inserts all the items of Src at the end of the container. }
procedure AppendAll(Src: TGenDeque);
{** Inserts all the items of Src in the range [PosFrom..PosTo] at the end of
the container. }
procedure AppendRange(Src: TGenDeque; PosFrom, PosTo: Integer);
{** Searches for Item using the binary search algorithm. Returns the index of
Item if its found. Otherwise, returns ( - InsertionPoint - 1 ).
InsertionPoint is the point at which the key would be inserted into the
container. }
function BinarySearch(const Item: _TItem_) : Integer;
function BinarySearch(const Item: _TItem_; Comparator: TCompareItems) : Integer;
{** Searches for Item in range [PosFrom..PosTo] using the binary search
algorithm. Returns the index of Item if its found. Otherwise, returns
( - InsertionPoint - 1 ). InsertionPoint is the point at which the key
would be inserted into the range. }
function BinarySearch(const Item: _TItem_; PosFrom, PosTo: Integer) : Integer;
function BinarySearch(const Item: _TItem_;
PosFrom, PosTo: Integer; Comparator: TCompareItems) : Integer;
{** Returns true if the container contains Item. }
function Contains(const Item: _TItem_) : Boolean;
function Contains(const Item: _TItem_; Comparator: TCompareItems) : Boolean;
{** Creates an empty deque and sets his capacity to InitialCapacity. }
constructor Create(InitialCapacity: Integer = 16);
function DefaultCompareItems(const A, B: _TItem_) : Integer; virtual;
function DefaultItemToString(const Item: _TItem_) : String; virtual;
{** Destroys the container. }
destructor Destroy; override;
{** If Obj = Self then returns @true, else if Obj is not a TGenDeque returns
false, else returns @true if Self and Obj contain the sames items. }
function Equals(Obj: TObject) : Boolean; override;
function Equals(Obj: TObject; Comparator: TCompareItems) : Boolean;
{** Returns the index of the first item equal to Item or -1. }
function FindIndex(const Item: _TItem_) : Integer;
function FindIndex(const Item: _TItem_; Comparator: TCompareItems) : Integer;
{** Returns a cursor on the first item equal to Item or NilCursor. The search
starts at the element From. }
function FindIndex(const Item: _TItem_; PosFrom: Integer) : Integer;
function FindIndex(const Item: _TItem_; PosFrom: Integer; Comparator: TCompareItems) : Integer;
{** Returns the first Item. }
function FirstItem : _TItem_; inline;
function GetEnumerator : TEnumerator;
function GetItemFast(Position: Integer) : _TItem_; inline;
{** Returns item at position Position. }
function GetItem(Position: Integer) : _TItem_; inline;
{** Returns a pointer designating item at position Position. }
function GetItemPtr(Position: Integer): PItem;
{** Inserts Count times Item before Before. }
procedure Insert(Before: Integer; const Item: _TItem_;
Count: Integer = 1);
{** Inserts all the items of Src before Before. }
procedure InsertAll(Before: Integer; Src: TGenDeque);
{** Inserts before Before all the items of Src in the range
[PosFrom..PosTo]. }
procedure InsertRange(Before: Integer; Src: TGenDeque;
PosFrom, PosTo: Integer);
{** Returns true if the items are sorted. }
function IsSorted: Boolean;
function IsSorted(Comparator: TCompareItems): Boolean;
{** Invokes Process on each items. }
procedure Iterate(Process: TProcessItem);
{** Invokes Process on each items in range [PosFrom..PosTo]. }
procedure Iterate(Process: TProcessItem; const PosFrom, PosTo: Integer);
{** Returns the last Item. }
function LastItem: _TItem_; inline;
{** Returns index of the greatest item. }
function MaxPos : Integer;
function MaxPos(Comparator: TCompareItems) : Integer;
{** Returns index of the greatest item in the range [PosFrom..PosTo]. }
function MaxPos(PosFrom, PosTo: Integer) : Integer;
function MaxPos(PosFrom, PosTo: Integer; Comparator: TCompareItems) : Integer;
{** Removes items from Src and inserts them into Self. Afterwards, Self
contains the union of the items that were initially in Src and Self. Src
is left empty. If Self and Src are initially sorted, then Self is
sorted. }
procedure Merge(Src: TGenDeque);
procedure Merge(Src: TGenDeque; Comparator: TCompareItems);
{** Returns index of the lowest item. }
function MinPos : Integer;
function MinPos(Comparator: TCompareItems) : Integer;
{** Returns index of the lowest item in the range [PosFrom..PosTo]. }
function MinPos(PosFrom, PosTo: Integer) : Integer;
function MinPos(PosFrom, PosTo: Integer; Comparator: TCompareItems) : Integer;
{** Copies Count items from Src to Dst. }
procedure Move(Src, Dst, Count: Integer); override;
{** Inserts Count times Item at the begining of the container. }
procedure Prepend(const Item: _TItem_; Count: Integer = 1);
{** Inserts all the items of Src at the begining of the container. }
procedure PrependAll(Src: TGenDeque);
{** Inserts all the items of Src in the range [PosFrom..PosTo] at the
begining of the container. }
procedure PrependRange(Src: TGenDeque; PosFrom, PosTo: Integer);
procedure ReadFirstItem(out Value : _TItem_); inline;
procedure ReadItem(Position: Integer; out Value: _TItem_);
procedure ReadItemFast(Position: Integer; out Value: _TItem_); inline;
procedure ReadLastItem(out Value : _TItem_); inline;
{** Replaces items in range [Index..Index + Count - 1] by Value. }
procedure Replace(Index, Count: Integer; const Value: _TItem_);
{** Returns the index of the first item equal to Item or -1. }
function ReverseFindIndex(const Item: _TItem_) : Integer;
function ReverseFindIndex(const Item: _TItem_; Comparator: TCompareItems) : Integer;
{** Returns a cursor on the first item equal to Item or NilCursor. The search
starts at the element From. }
function ReverseFindIndex(const Item: _TItem_; PosFrom: Integer) : Integer;
function ReverseFindIndex(const Item: _TItem_; PosFrom: Integer;
Comparator: TCompareItems) : Integer;
{** Assigns the value Value to the item at Position. }
procedure SetItem(Position: Integer; const Value: _TItem_); inline;
procedure SetItemFast(Position: Integer; const Value: _TItem_); inline;
{** Sorts the items. }
procedure Sort;
procedure Sort(Comparator: TCompareItems);
{** Sorts the items in the range [PosFrom..PosTo]. }
procedure Sort(PosFrom, PosTo: Integer);
procedure Sort(PosFrom, PosTo: Integer; Comparator: TCompareItems);
procedure SwapFast(I, J: Integer); override;
{** Provides access to the items in the container. }
property Items[Index: Integer] : _TItem_ read GetItemFast
write SetItemFast; default;
{** Provides access to pointers on the items in the container. }
property ItemsPtr[Index: Integer] : PItem read GetItemPtrFast;
property OnCompareItems : TCompareItems read fOnCompareItems
write SetOnCompareItems;
property OnItemToString : TItemToString read fOnItemToString
write SetOnItemToString;
end;
TAbstractList = class;
{ TListCursor }
TListCursor = object
strict private
fList : TAbstractList;
fNode : Pointer;
public
{** Check if the cursors designate the same item. }
function Equals(const Cursor: TListCursor) : Boolean; inline;
{** Check if the cursors designate an item. }
function HasItem: Boolean; inline;
constructor Init(AList : TAbstractList; ANode: Pointer);
{** Returns true if the cursor designates the first element. }
function IsFirst: Boolean; inline;
{** Returns true if the cursor designates the last element. }
function IsLast: Boolean; inline;
{** Equivalent to not HasItem. }
function IsNil: Boolean; inline;
{** If cursor is nil then do nothing, else if cursor is last then cursor
becomes nil cursor, otherwise move cursor to the next item. }
procedure MoveNext; inline;
{** If cursor is nil then do nothing, else if cursor is first then cursor
becomes nil cursor, otherwise move cursor to the previous item. }
procedure MovePrevious; inline;
{** The designated List. }
property List : TAbstractList read fList;
{** The designated node. }
property Node : Pointer read fNode write fNode;
end;
{ TAbstractList }
TAbstractList = class(TContainer)
protected
procedure CheckValid(const Cursor: TListCursor);
procedure CheckNotNil(const Cursor: TListCursor);
function CursorIsFirst(const Cursor: TListCursor) : Boolean; virtual; abstract;
function CursorIsLast(const Cursor: TListCursor) : Boolean; virtual; abstract;
procedure CursorMoveNext(var Cursor: TListCursor); virtual; abstract;
procedure CursorMovePrev(var Cursor: TListCursor); virtual; abstract;
end;
{ TGenList }
generic TGenList<_TItem_> = class(TAbstractList)
public type
PItem = ^_TItem_;
TCompareItems = function (const A, B: _TItem_) : Integer of object;
TItemToString = function (const Item: _TItem_) : String of object;
TProcessItem = procedure(var Item: _TItem_) of object;
TEnumerator = specialize TGenEnumerator<_TItem_, TListCursor>;
strict private type
PNode = ^TNode;
TNode = record
Item : _TItem_;
Next, Previous : PNode;
end;
strict private
fHead : PNode;
fOnCompareItems: TCompareItems;
fOnItemToString: TItemToString;
fTail : PNode;
fSize : Integer;
fNilCursor : TListCursor;
procedure DeleteNodesBackward(From: PNode; Count: Integer);
procedure DeleteNodesBetween(NodeFrom, NodeTo: PNode);
procedure DeleteNodesForward(From: PNode; Count: Integer);
function EnumeratorGet(const Pos: TListCursor) : _TItem_;
function EnumeratorNext(var Pos: TListCursor) : Boolean;
function Equals(List: TGenList; Comparator: TCompareItems) : Boolean;
function GetItemFast(const Position: TListCursor) : _TItem_; inline;
function GetItemPtrFast(const Position: TListCursor) : PItem; inline;
procedure InsertItem(const Item: _TItem_; Pos: PNode; Count: Integer);
procedure Partition(Pivot, Back: PNode; Comparator: TCompareItems);
procedure RealSort(Front, Back: PNode; Comparator: TCompareItems);
procedure SetOnCompareItems(AValue: TCompareItems);
procedure SetOnItemToString(AValue: TItemToString);
procedure SpliceNodes(Before, PosFrom, PosTo: PNode);
protected
function CursorIsFirst(const Cursor: TListCursor) : Boolean; override;
function CursorIsLast(const Cursor: TListCursor) : Boolean; override;
procedure CursorMoveNext(var Cursor: TListCursor); override;
procedure CursorMovePrev(var Cursor: TListCursor); override;
public
{** Inserts Count times Item at the end of the container. }
procedure Append(const Item: _TItem_; Count: Integer = 1);
{** Inserts all the items of Src at the end of the container. }
procedure AppendAll(Src: TGenList);
{** Inserts all the items of Src in the range [PosFrom..PosTo] at the end of
the container. }
procedure AppendRange(Src: TGenList; const PosFrom, PosTo: TListCursor);
{** Removes all the items from the container. }
procedure Clear;
{** Returns true if the container contains Item. }
function Contains(const Item: _TItem_) : Boolean;
function Contains(const Item: _TItem_; Comparator: TCompareItems) : Boolean;
{** Creates an empty list. }
constructor Create;
function DefaultCompareItems(const A, B: _TItem_) : Integer; virtual;
function DefaultItemToString(const Item: _TItem_) : String; virtual;
{** Deletes Count items begining at Position and then sets Position to
NilCursor. }
procedure Delete(var Position: TListCursor; Count: Integer = 1);
{** Deletes the first Count items. }
procedure DeleteFirst(Count: Integer = 1);
{** Deletes the last Count items. }
procedure DeleteLast(Count: Integer = 1);
{** Deletes all items in the range [PosFrom..PosTo]. }
procedure DeleteRange(const PosFrom, PosTo: TListCursor);
{** Destroys the container. }
destructor Destroy; override;
{** If Obj = Self then returns true, else if Obj is not a TGenList returns false,
else returns true if Self and Obj contain the sames items. }
function Equals(Obj: TObject) : Boolean; override;
function Equals(Obj: TObject; Comparator: TCompareItems) : Boolean;
{** Returns a cursor on the first item equal to Item or NilCursor. }
function Find(const Item: _TItem_) : TListCursor;
function Find(const Item: _TItem_; Comparator: TCompareItems) : TListCursor;
{** Returns a cursor on the first item equal to Item or NilCursor.The search
starts at the first element if Position is NilCursor, and at the element
designated by Position otherwise. }
function Find(const Item: _TItem_; const Position: TListCursor) : TListCursor;
function Find(const Item: _TItem_; const Position: TListCursor; Comparator: TCompareItems): TListCursor;
{** Returns a cursor that designates the first element of the container or
NilCursor if the container is empty. }
function First: TListCursor;
{** Returns the first Item. }
function FirstItem : _TItem_; inline;
{** If Index is not in the range [0..Size - 1], then returns NilCursor.
Otherwise, returns a cursor designating the item at position Index. }
function GetCursor(Index: Integer): TListCursor;
function GetEnumerator : TEnumerator;
{** Returns the item designated by Position. }
function GetItem(const Position: TListCursor) : _TItem_; inline;
{** Returns a pointer designating the item designated by Position. }
function GetItemPtr(const Position: TListCursor) : PItem; inline;
{** Inserts Count times Item before Before. }
procedure Insert(const Before: TListCursor; const Item: _TItem_;
Count: Integer = 1);
{** Inserts Count times Item before Before. Position designates the first
newly-inserted element. }
procedure Insert(const Before: TListCursor; const Item: _TItem_;
out Position: TListCursor; Count: Integer);
{** Inserts all the items of Src before Before. }
procedure InsertAll(const Before: TListCursor; Src: TGenList);
{** Inserts before Before all the items of Src in the range
[PosFrom..PosTo]. }
procedure InsertRange(const Before : TListCursor; Src: TGenList;
const PosFrom, PosTo: TListCursor);
{** Returns true if the list is empty. }
function IsEmpty: Boolean; inline;
{** Returns @true if the items are sorted. }
function IsSorted : Boolean;
function IsSorted(Comparator: TCompareItems) : Boolean;
procedure Iterate(Process: TProcessItem);
procedure Iterate(Process: TProcessItem; const PosFrom, PosTo: TListCursor);
{** Returns a cursor that designates the last element of the container or
NilCursor if the container is empty. }
function Last: TListCursor;
{** Returns the last Item. }
function LastItem: _TItem_; inline;
{** Removes items from Src and inserts them into Self. Afterwards, Self
contains the union of the items that were initially in Src and Self. Src
is left empty. If Self and Src are initially sorted, then Self is
sorted. }
procedure Merge(Src: TGenList);
procedure Merge(Src: TGenList; Comparator: TCompareItems);
{** Inserts Count times Item at the begining of the container. }
procedure Prepend(const Item: _TItem_; Count: Integer = 1);
{** Inserts all the items of Src at the begining of the container. }
procedure PrependAll(Src: TGenList);
{** Inserts all the items of Src in the range [PosFrom..PosTo] at the
begining of the container. }
procedure PrependRange(Src: TGenList; const PosFrom, PosTo: TListCursor);
procedure ReadFirstItem(out Value : _TItem_); inline;
procedure ReadItem(const Position: TListCursor; out Value: _TItem_);
procedure ReadItemFast(const Position: TListCursor; out Value: _TItem_); inline;
procedure ReadLastItem(out Value : _TItem_); inline;
{** Replaces items in range [Position..Position + Count - 1] by Value. }
procedure Replace(const Position: TListCursor; Count: Integer;
const Value: _TItem_);
{** Reorders the items in reverse order. }
procedure Reverse;
{** Returns a cursor on the first item equal to Item or NilCursor. }
function ReverseFind(const Item: _TItem_) : TListCursor;
function ReverseFind(const Item: _TItem_; Comparator: TCompareItems): TListCursor;
{** Returns a cursor on the first item equal to Item or NilCursor.The search
starts at the last element if Position is NilCursor, and at the element
designated by Position otherwise. }
function ReverseFind(const Item: _TItem_; const Position: TListCursor) : TListCursor;
function ReverseFind(const Item: _TItem_; const Position: TListCursor;
Comparator: TCompareItems) : TListCursor;
{** Reorders the items in the range [PosFrom..PosTo] in reverse order. }
procedure ReverseRange(const PosFrom, PosTo: TListCursor);
{** Assigns the value Value to the item designated by Position. }
procedure SetItem(const Position: TListCursor; const Value: _TItem_);
procedure SetItemFast(const Position: TListCursor; const Value: _TItem_); inline;
{** Sorts the items. }
procedure Sort;
procedure Sort(Comparator: TCompareItems);
{** Sorts the items in the range [PosFrom..PosTo]. }
procedure Sort(const PosFrom, PosTo: TListCursor);
procedure Sort(const PosFrom, PosTo: TListCursor; Comparator: TCompareItems);
{** Removes all items of Src and moves them to Self before Before. }
procedure Splice(const Before: TListCursor; Src: TGenList);
{** Removes from Src the item designated by Position and moves it to Self
before Before. }
procedure Splice(const Before: TListCursor; Src: TGenList;
const Position: TListCursor);
{** Removes all items of Src in the range [SrcFrom..SrcTo] and moves them to
Self before Before. }
procedure Splice(const Before: TListCursor; Src: TGenList;
const SrcFrom, SrcTo: TListCursor);
{** Swaps the values of the items designated by I and J. }
procedure Swap(const I, J: TListCursor);
{** Swaps the nodes designated by I and J. }
procedure SwapLinks(const I, J: TListCursor);
{** Return a string representation for the container. }
function ToString : String; override;
{** Provides access to the items in the container. }
property Items[const Index: TListCursor] : _TItem_
read GetItemFast write SetItemFast; default;
{** Provides access to pointers on the items in the container. }
property ItemsPtr[const Index: TListCursor] : PItem read GetItemPtrFast;
{** A nil cursor. }
property NilCursor: TListCursor read fNilCursor;
property OnCompareItems : TCompareItems read fOnCompareItems
write SetOnCompareItems;
property OnItemToString : TItemToString read fOnItemToString
write SetOnItemToString;
{** Number of elements in the list. }
property Size: Integer read fSize;
end;
{ TGenPriorityQueue }
generic TGenPriorityQueue<_TItem_> = class(TContainer)
public type
TCompareItems = function (const A, B: _TItem_) : Integer of object;
strict private
fCapacity : Integer;
fItems : array of _TItem_;
fOnCompareItems: TCompareItems;
fSize : Integer;
procedure SetOnCompareItems(AValue: TCompareItems);
procedure MoveDown(Index: Integer; const Item: _TItem_);
procedure MoveUp(Index: Integer; const Item: _TItem_);
public
{** Empty the queue of all items. }
procedure Clear;
{** Creates an empty priority queue. }
constructor Create(InitialCapacity : Integer = 16);
function DefaultCompareItems(const A, B: _TItem_) : Integer; virtual;
{** Returns true if the priority queue is empty. }
function IsEmpty: Boolean; inline;
{** Frees unused memory. }
procedure Pack;
{** Removes the item from the top of the stack. }
procedure Pop;
{** Adds Item to the top of the stack. }
procedure Push(const Item: _TItem_);
procedure ReadTop(out Value: _TItem_);
{** If necessary, increases the capacity of the container to ensure that it
can hold at least MinCapacity items. }
procedure Reserve(MinCapacity : Integer);
{** Returns the item at the top of the stack. }
function Top : _TItem_;
{** Capacity of the container. }
property Capacity : Integer read fCapacity;
property OnCompareItems : TCompareItems read fOnCompareItems write SetOnCompareItems;
{** Number of elements. }
property Size: Integer read fSize;
end;
{ TGenQueue }
generic TGenQueue<_TItem_, _TContainer_> = class(TContainer)
private
fData : _TContainer_;
function GetSize: Integer; inline;
public
{** Add the item to the back of the queue. }
procedure Append(const Item: _TItem_);
{** Empty the queue of all items. }
procedure Clear;
{** Creates an empty queue. }
constructor Create;
{** Destroys the container. }
destructor Destroy; override;
{** Returns a copy of the item at the front of the queue. }
function Front : _TItem_;
{** Returns true if the queue is empty. }
function IsEmpty: Boolean; inline;
{** Removes the item from the front of the queue. }
procedure Pop;
procedure ReadFront(out Value: _TItem_);
{** Number of items. }
property Size : Integer read GetSize;
end;
{ TGenStack }
generic TGenStack<_TItem_, _TContainer_> = class(TContainer)
private
fData : _TContainer_;
function GetSize: Integer; inline;
public
{** Removes all the items from the stack. }
procedure Clear;
{** Creates an empty stack. }
constructor Create;
{** Destroys the stack. }