-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathNeslib.Collections.pas
More file actions
4972 lines (4131 loc) · 131 KB
/
Neslib.Collections.pas
File metadata and controls
4972 lines (4131 loc) · 131 KB
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
unit Neslib.Collections;
{< Generic collections that are faster and more light-weight than Delphi's
built-in collections. Also adds read-only views for most collections, as well
as additional collections not found in the RTL. }
{$INCLUDE 'Neslib.inc'}
interface
uses
System.Types,
System.SyncObjs,
System.Generics.Defaults,
Neslib.System;
type
{ Various utilities that operate on generic dynamic arrays. Mostly used
internally by various generic collections. }
TArray = class // static
{$REGION 'Internal Declarations'}
private
class procedure QuickSort<T>(var AValues: TArray<T>;
const AComparer: IComparer<T>; L, R: Integer); static;
{$ENDREGION 'Internal Declarations'}
public
{ Moves items within an array.
Parameters:
AArray: the array
AFromIndex: the source index into AArray
AToIndex: the destination index into AArray
ACount: the number of elements to move.
You should use this utility instead of System.Move since it correctly
handles elements with [weak] references.
@bold(Note): no range checking is performed on the arguments. }
class procedure Move<T>(var AArray: TArray<T>; const AFromIndex, AToIndex,
ACount: Integer); overload; static;
{ Moves items from one array to another.
Parameters:
AFromArray: the source array
AFromIndex: the source index into AFromArray
AToArray: the destination array
AToIndex: the destination index into AToArray
ACount: the number of elements to move.
You should use this utility instead of System.Move since it correctly
handles elements with [weak] references.
@bold(Note): no range checking is performed on the arguments. }
class procedure Move<T>(const AFromArray: TArray<T>; const AFromIndex: Integer;
var AToArray: TArray<T>; const AToIndex, ACount: Integer); overload; static;
{ Finalizes an element in an array.
Parameters:
AArray: the array containing the element to finalize.
AIndex: the index of the element to finalize.
You should call this utility to mark an element in an array as "unused".
This prevents memory problems when the array contains elements that are
reference counted or contain [weak] references. In those cases, the
element will be set to all zero's. If the array contains "regular"
elements, then this method does nothing.
@bold(Note): no range checking is performed on the arguments. }
class procedure Finalize<T>(var AArray: TArray<T>;
const AIndex: Integer); overload; static; inline;
{ Finalizes a range ofelements in an array.
Parameters:
AArray: the array containing the elements to finalize.
AIndex: the index of the first element to finalize.
ACount: the number of elements to finalize.
You should call this utility to mark an element in an array as "unused".
This prevents memory problems when the array contains elements that are
reference counted or contain [weak] references. In those cases, the
element will be set to all zero's. If the array contains "regular"
elements, then this method does nothing.
@bold(Note): no range checking is performed on the arguments. }
class procedure Finalize<T>(var AArray: TArray<T>; const AIndex,
ACount: Integer); overload; static; inline;
{ Sorts an array using the default comparer.
Parameters:
AValues: the array to sort. }
class procedure Sort<T>(var AValues: TArray<T>); overload; static;
{ Sorts an array.
Parameters:
AValues: the array to sort.
AComparer: the comparer to use to determine sort order.
AIndex: the start index into the array
ACount: the number of elements to sort
Set AIndex to 0 and ACount to Length(AValues) to sort the entire array.
@bold(Note): no range checking is performed on the arguments. }
class procedure Sort<T>(var AValues: TArray<T>;
const AComparer: IComparer<T>; const AIndex, ACount: Integer); overload; static;
{ Searches for an item in a sorted array.
Parameters:
AValues: the array to search. This array must be sorted (you can use
Sort to sort it).
AItem: the item to search for.
AFoundIndex: when the array contains AItem, this value is set to the
index of that item. Otherwise, it is set to the index between the two
items where AItem would fit.
AComparer: the comparer to use to determine sort order.
AIndex: the start index into the array
ACount: the number of elements to search
Returns:
True when AItem is found, or False if not.
Set AIndex to 0 and ACount to Length(AValues) to search in the entire
array.
@bold(Note): no range checking is performed on the arguments. }
class function BinarySearch<T>(const AValues: TArray<T>; const AItem: T;
out AFoundIndex: Integer; const AComparer: IComparer<T>;
const AIndex, ACount: Integer): Boolean; overload; static;
end;
type
{ Abstract base generic enumerator class. Generic collections have a method
called GetEnumerator that returns an instance of a class derived from this
class. This allows for <tt>for..in</tt> enumeration of collections. }
TEnumerator<T> = class abstract
{$REGION 'Internal Declarations'}
protected
function GetCurrent: T; virtual; abstract;
{$ENDREGION 'Internal Declarations'}
public
{ Moves to the next element in the collection.
Returns:
True if there is a next element to enumerate. False otherwise. }
function MoveNext: Boolean; virtual; abstract;
{ The current value of the enumeration }
property Current: T read GetCurrent;
end;
type
{ A standard enumerator for enumerating elements in a dynamic array. Lists
and stacks use this class for their enumerators. }
TArrayEnumerator<T> = class(TEnumerator<T>)
{$REGION 'Internal Declarations'}
private
FItems: TArray<T>;
FHigh: Integer;
FIndex: Integer;
protected
function GetCurrent: T; override;
{$ENDREGION 'Internal Declarations'}
public
constructor Create(const AItems: TArray<T>; const ACount: Integer);
function MoveNext: Boolean; override;
end;
type
{ Interface for enumerable collections. }
IEnumerable<T> = interface
{ Copies the elements in the collection to a dynamic array }
function ToArray: TArray<T>;
{ Returns an enumerator to enumerate over the items in the collection. }
function GetEnumerator: TEnumerator<T>;
end;
type
{ Abstract base class for collections that are enumerable. Most collections
types in this unit are.
Implements IEnumerable<T>. }
TEnumerable<T> = class abstract(TNonRefCountedObject, IEnumerable<T>)
public
{ Copies the elements in the collection to a dynamic array }
function ToArray: TArray<T>; virtual; abstract;
{ Returns an enumerator to enumerate over the items in the collection. }
function GetEnumerator: TEnumerator<T>; virtual; abstract;
end;
type
{ A read-only view of a list. }
IReadOnlyList<T> = interface(IEnumerable<T>)
{$REGION 'Internal Declarations'}
function GetCount: Integer;
function GetItem(const AIndex: Integer): T;
function GetCapacity: Integer;
{$ENDREGION 'Internal Declarations'}
{ Checks whether the list contains a given item.
This method performs a O(n) linear search and uses the list's comparer to
check for equality. For a faster check, use BinarySearch.
Parameters:
AValue: The value to check.
Returns:
True if the list contains AValue. }
function Contains(const AValue: T): Boolean;
{ Returns the index of a given item or -1 if not found.
This method performs a O(n) linear search and uses the list's comparer to
check for equality. For a faster check, use BinarySearch.
Parameters:
AValue: The value to find. }
function IndexOf(const AValue: T): Integer;
{ Returns the last index of a given item or -1 if not found.
This method performs a O(n) backwards linear search and uses the list's
comparer to check for equality. For a faster check, use BinarySearch.
Parameters:
AValue: The value to find. }
function LastIndexOf(const AValue: T): Integer;
{ Returns the index of a given item or -1 if not found.
This method performs a O(n) linear search and uses the list's comparer to
check for equality. For a faster check, use BinarySearch.
Parameters:
AValue: The value to find.
ADirection: Whether to search forwards or backwards. }
function IndexOfItem(const AValue: T; const ADirection: TDirection): Integer;
{ Performs a binary search for a given item. This requires that the list
is sorted. This is an O(log n) operation that uses the default comparer to
check for equality.
Parameters:
AItem: The item to find.
AIndex: is set to the index of AItem if found. If not found, it is set
to the index of the first entry larger than AItem.
Returns:
Whether the list contains the item. }
function BinarySearch(const AItem: T; out AIndex: Integer): Boolean; overload;
{ Performs a binary search for a given item. This requires that the list
is sorted. This is an O(log n) operation that uses the given comparer to
check for equality.
Parameters:
AItem: The item to find.
AIndex: is set to the index of AItem if found. If not found, it is set
to the index of the first entry larger than AItem.
AComparer: the comparer to use to check for equality.
Returns:
Whether the list contains the item. }
function BinarySearch(const AItem: T; out AIndex: Integer;
const AComparer: IComparer<T>): Boolean; overload;
{ Returns the first item in the list. }
function First: T;
{ Returns the last item in the list. }
function Last: T;
{ The number of items in the list }
property Count: Integer read GetCount;
{ The items in the list }
property Items[const AIndex: Integer]: T read GetItem; default;
{ The number of reserved items in the list. Is >= Count to improve
performance by reducing memory reallocations. }
property Capacity: Integer read GetCapacity;
end;
type
{ Base class for TList<T> and TSortedList<T> }
TBaseList<T> = class abstract(TEnumerable<T>, IReadOnlyList<T>)
{$REGION 'Internal Declarations'}
private
FItems: TArray<T>;
FCount: Integer;
private
procedure SetCount(const Value: Integer);
procedure SetCapacity(const Value: Integer);
private
procedure Grow(const AMinCount: Integer);
procedure GrowCheck; overload; inline;
procedure GrowCheck(const AMinCount: Integer); overload; inline;
protected
{ IReadOnlyList<T> }
function GetCount: Integer;
function GetCapacity: Integer;
function GetItem(const AIndex: Integer): T; inline;
protected
procedure ItemDeleted(const AItem: T); virtual;
{$ENDREGION 'Internal Declarations'}
public
{ IEnumerable<T> }
{ Copies the elements in the list to a dynamic array }
function ToArray: TArray<T>; override; final;
{ Allow <tt>for..in</tt> enumeration of the list. }
function GetEnumerator: TEnumerator<T>; override;
public
{ Checks whether the list contains a given item.
This method performs a O(n) linear search and uses the list's comparer to
check for equality. For a faster check, use BinarySearch.
Parameters:
AValue: The value to check.
Returns:
True if the list contains AValue. }
function Contains(const AValue: T): Boolean;
{ Returns the index of a given item or -1 if not found.
This method performs a O(n) linear search and uses the list's comparer to
check for equality. For a faster check, use BinarySearch.
Parameters:
AValue: The value to find. }
function IndexOf(const AValue: T): Integer;
{ Returns the last index of a given item or -1 if not found.
This method performs a O(n) backwards linear search and uses the list's
comparer to check for equality. For a faster check, use BinarySearch.
Parameters:
AValue: The value to find. }
function LastIndexOf(const AValue: T): Integer;
{ Returns the index of a given item or -1 if not found.
This method performs a O(n) linear search and uses the list's comparer to
check for equality. For a faster check, use BinarySearch.
Parameters:
AValue: The value to find.
ADirection: Whether to search forwards or backwards. }
function IndexOfItem(const AValue: T; const ADirection: TDirection): Integer;
{ Performs a binary search for a given item. This requires that the list
is sorted. This is an O(log n) operation that uses the default comparer to
check for equality.
Parameters:
AItem: The item to find.
AIndex: is set to the index of AItem if found. If not found, it is set
to the index of the first entry larger than AItem.
Returns:
Whether the list contains the item. }
function BinarySearch(const AItem: T; out AIndex: Integer): Boolean; overload;
{ Performs a binary search for a given item. This requires that the list
is sorted. This is an O(log n) operation that uses the given comparer to
check for equality.
Parameters:
AItem: The item to find.
AIndex: is set to the index of AItem if found. If not found, it is set
to the index of the first entry larger than AItem.
AComparer: the comparer to use to check for equality.
Returns:
Whether the list contains the item. }
function BinarySearch(const AItem: T; out AIndex: Integer;
const AComparer: IComparer<T>): Boolean; overload;
{ Returns the first item in the list. }
function First: T;
{ Returns the last item in the list. }
function Last: T;
{ Clears the list }
procedure Clear; virtual;
{ Deletes an item from the list.
Parameters:
AIndex: the index of the item to delete }
procedure Delete(const AIndex: Integer);
{ Deletes a range of items from the list.
Parameters:
AIndex: the index of the first item to delete
ACount: the number of items to delete }
procedure DeleteRange(const AIndex, ACount: Integer);
{ Removes an item from the list.
Parameters:
AValue: the value of the item to remove. It this list does not contain
this item, nothing happens.
Returns:
The index of the removed item, or -1 of the list does not contain
AValue.
If the list contains multiple items with the same value, only the first
item is removed. }
function Remove(const AValue: T): Integer;
{ Removes an item from the list, starting from the beginning or end.
Parameters:
AValue: the value of the item to remove. It this list does not contain
this item, nothing happens.
ADirection: the direction to search for the item (from the beginning or
the end)
Returns:
The index of the removed item (given ADirection), or -1 of the list does
not contain AValue.
If the list contains multiple items with the same value, only the first
(or last) item is removed. }
function RemoveItem(const AValue: T; const ADirection: TDirection): Integer;
{ Trims excess memory used by the list. To improve performance and reduce
memory reallocations, the list usually contains space for more items than
are actually stored in this list. That is, Capacity >= Count. Call this
method free that excess memory. You can do this when you are done filling
the list to free memory. }
procedure TrimExcess;
{ The number of items in the list }
property Count: Integer read FCount write SetCount;
{ The items in the list }
property Items[const AIndex: Integer]: T read GetItem; default;
{ The number of reserved items in the list. Is >= Count to improve
performance by reducing memory reallocations. }
property Capacity: Integer read GetCapacity write SetCapacity;
end;
type
{ Generic list. Similar to Delphi's TList<T> }
TList<T> = class(TBaseList<T>)
{$REGION 'Internal Declarations'}
protected
function GetItem(const AIndex: Integer): T; inline;
procedure SetItem(const AIndex: Integer; const Value: T); virtual;
{$ENDREGION 'Internal Declarations'}
public
{ Creates an empty list }
constructor Create; overload;
{ Creates a list with the contents of another collection.
Parameters:
ACollection: the collection containing the items to add. Can be any
class that descends from TEnumerable<T>. }
constructor Create(const ACollection: TEnumerable<T>); overload;
{ Creates a list with an initial capacity.
Parameters:
AInitialCapacity: the initial capacity of the list. }
constructor Create(const AInitialCapacity: Integer); overload;
{ Adds an item to the end of the list.
Parameters:
AValue: the item to add.
Returns:
The index of the added item. }
function Add(const AValue: T): Integer;
{ Adds a range of items to the end of the list.
Parameters:
AValues: an array of items to add. }
procedure AddRange(const AValues: array of T); overload;
{ Adds the items of another collection to the end of the list.
Parameters:
ACollection: the collection containing the items to add. Can be any
class that descends from TEnumerable<T>. }
procedure AddRange(const ACollection: TEnumerable<T>); overload; inline;
{ Inserts an item into the list.
Parameters:
AIndex: the index in the list to insert the item. The item will be
inserted before AIndex. Set to 0 to insert at the beginning to the
list. Set to Count to add to the end of the list.
AValue: the item to insert. }
procedure Insert(const AIndex: Integer; const AValue: T);
{ Inserts a range of items into the list.
Parameters:
AIndex: the index in the list to insert the items. The items will be
inserted before AIndex. Set to 0 to insert at the beginning to the
list. Set to Count to add to the end of the list.
AValues: the items to insert. }
procedure InsertRange(const AIndex: Integer; const AValues: array of T); overload;
{ Inserts the items from another collection into the list.
Parameters:
AIndex: the index in the list to insert the items. The items will be
inserted before AIndex. Set to 0 to insert at the beginning to the
list. Set to Count to add to the end of the list.
ACollection: the collection containing the items to insert. Can be any
class that descends from TEnumerable<T>. }
procedure InsertRange(const AIndex: Integer; const ACollection: TEnumerable<T>); overload;
{ Swaps to elements in the list.
Parameters:
AIndex: the index of the first element to swap
AIndex: the index of the last element to swap }
procedure Exchange(const AIndex1, AIndex2: Integer);
{ Moves an element in the list to a different location.
Parameters:
ACurIndex: the index of the element to move.
ANewIndex: the new index for the element. }
procedure Move(const ACurIndex, ANewIndex: Integer);
{ Reverses the order of the elements in the list. }
procedure Reverse;
{ Sort the list using the default comparer for the element type }
procedure Sort; overload;
{ Sort the list using a custom comparer.
Parameters:
AComparer: the comparer to use to sort the list. }
procedure Sort(const AComparer: IComparer<T>); overload;
{ The items in the list }
property Items[const AIndex: Integer]: T read GetItem write SetItem; default;
end;
type
{ Generic sorted list. Adding and removing items will keep the list in a
sorted state. }
TSortedList<T> = class(TBaseList<T>)
{$REGION 'Internal Declarations'}
private
FComparer: IComparer<T>;
FDuplicates: TDuplicates;
protected
function GetItem(const AIndex: Integer): T; inline;
{$ENDREGION 'Internal Declarations'}
public
{ Creates an empty list, using the default comparer for sorting. }
constructor Create; overload;
{ Creates an empty list, using a given comparer for sorting.
Parameters:
AComparer: the comparer to use for sorting. }
constructor Create(const AComparer: IComparer<T>); overload;
{ Creates a list with the contents of another collection. It uses the
default comparer for sorting.
Parameters:
ACollection: the collection containing the items to add. Can be any
class that descends from TEnumerable<T>. }
constructor Create(const ACollection: TEnumerable<T>); overload;
{ Creates a list with the contents of another collection and a given
comparer for sorting.
Parameters:
AComparer: the comparer to use for sorting.
ACollection: the collection containing the items to add. Can be any
class that descends from TEnumerable<T>. }
constructor Create(const AComparer: IComparer<T>;
const ACollection: TEnumerable<T>); overload;
{ Adds an item to the list in sorted order.
Parameters:
AValue: the item to add.
Returns:
The index of the added item. }
function Add(const AValue: T): Integer;
{ Adds a range of items to the list in sorted order.
Parameters:
AValues: an array of items to add. }
procedure AddRange(const AValues: array of T); overload;
{ Adds the items of another collection to the list in sorted order.
Parameters:
ACollection: the collection containing the items to add. Can be any
class that descends from TEnumerable<T>. }
procedure AddRange(const ACollection: TEnumerable<T>); overload; inline;
{ How duplicates should be handled:
* dupIgnore: (default) duplicates are ignored and not added to the list.
* dupAccept: duplicates are added to the list.
* dupError: an exception will be raised when trying to add a duplicate to
the list. }
property Duplicates: TDuplicates read FDuplicates write FDuplicates;
{ The items in the list }
property Items[const AIndex: Integer]: T read GetItem; default;
end;
type
{ Generic list that owns the objects it contains.
Similar to Delphi's TObjectList<T>. }
TObjectList<T: class> = class(TList<T>)
{$REGION 'Internal Declarations'}
protected
procedure ItemDeleted(const AItem: T); override;
procedure SetItem(const AIndex: Integer; const Value: T); override;
public
destructor Destroy; override;
procedure Clear; override;
{$ENDREGION 'Internal Declarations'}
public
{ Extracts an item from this list, @bold(without) freeing it.
Parameters:
AValue: the item to extract
Returns:
AValue if the item was in the list, or Default(T) otherwise. }
function Extract(const AValue: T): T;
{ Extracts an item from this list, @bold(without) freeing it.
Parameters:
AValue: the item to extract
ADirection: the direction to search for the item
Returns:
AValue if the item was in the list, or Default(T) otherwise. }
function ExtractItem(const AValue: T; const ADirection: TDirection): T;
end;
type
{ A version of TSortedList<T> that owns the objects it contains. }
TSortedObjectList<T: class> = class(TSortedList<T>)
{$REGION 'Internal Declarations'}
protected
procedure ItemDeleted(const AItem: T); override;
public
destructor Destroy; override;
procedure Clear; override;
{$ENDREGION 'Internal Declarations'}
public
{ Extracts an item from this list, @bold(without) freeing it.
Parameters:
AValue: the item to extract
Returns:
AValue if the item was in the list, or Default(T) otherwise. }
function Extract(const AValue: T): T;
{ Extracts an item from this list, @bold(without) freeing it.
Parameters:
AValue: the item to extract
ADirection: the direction to search for the item
Returns:
AValue if the item was in the list, or Default(T) otherwise. }
function ExtractItem(const AValue: T; const ADirection: TDirection): T;
end;
type
{ Read-only view of a TStack<T> }
IReadOnlyStack<T> = interface(IEnumerable<T>)
{$REGION 'Internal Declarations'}
function GetCount: Integer;
function GetCapacity: Integer;
{$ENDREGION 'Internal Declarations'}
{ Peeks at the top of the stack.
Returns:
The value on the top of the stack, without popping it }
function Peek: T;
{ The number of items on the stack }
property Count: Integer read GetCount;
{ The number of reserved items in the stack. Is >= Count to improve
performance by reducing memory reallocations. }
property Capacity: Integer read GetCapacity;
end;
type
{ A generic stack (Last-In-First-Out), similar to Delphi's TStack<T> }
TStack<T> = class(TEnumerable<T>, IReadOnlyStack<T>)
{$REGION 'Internal Declarations'}
private
FItems: TArray<T>;
FCount: Integer;
private
procedure Grow;
procedure SetCapacity(const Value: Integer);
protected
{ IReadOnlyStack<T> }
function GetCount: Integer;
function GetCapacity: Integer;
protected
procedure ItemDeleted(const AItem: T); virtual;
{$ENDREGION 'Internal Declarations'}
public
{ TEnumerable<T> }
{ Copies the elements in the stack to a dynamic array }
function ToArray: TArray<T>; override;
{ Allow <tt>for..in</tt> enumeration of the stack. }
function GetEnumerator: TEnumerator<T>; override;
public
{ Clears the stack }
procedure Clear; virtual;
{ Pushes a value onto the (top of the) stack.
Parameters:
AValue: the value to push }
procedure Push(const AValue: T);
{ Peeks at the top of the stack.
Returns:
The value on the top of the stack, without popping it }
function Peek: T;
{ Pops a value from the (top of the) stack.
Returns:
The popped value. }
function Pop: T;
{ Tries to pop a value from the (top of the) stack.
Parameters:
AValue: is set to the popped value on success, or Default(T) on
failure.
Returns:
True if there was a value to pop, or False otherwise }
function TryPop(out AValue: T): Boolean; inline;
{ Trims excess memory used by the stack. To improve performance and reduce
memory reallocations, the stack usually contains space for more items than
are actually stored in this list. That is, Capacity >= Count. Call this
method free that excess memory. You can do this when you are done filling
the stack to free memory. }
procedure TrimExcess;
{ The number of items on the stack }
property Count: Integer read FCount;
{ The number of reserved items in the stack. Is >= Count to improve
performance by reducing memory reallocations. }
property Capacity: Integer read GetCapacity write SetCapacity;
end;
type
{ Generic stack of that owns its objects.
Similar to Delphi's TObjectStack. }
TObjectStack<T: class> = class(TStack<T>)
{$REGION 'Internal Declarations'}
protected
procedure ItemDeleted(const AItem: T); override;
public
destructor Destroy; override;
procedure Clear; override;
{$ENDREGION 'Internal Declarations'}
public
{ Pops a value from the (top of the) stack and frees it. This version
does not return any value, because that value could have been freed
already. Instead, you should use Peek to inspect the value before popping
it, or Extract to pop the value without freeing it. }
procedure Pop;
{ Pops a value from the (top of the) stack @bold(without) freeing it.
Returns:
The popped value. }
function Extract: T;
end;
type
{ A thread-safe stack }
TConcurrentStack<T> = class
{$REGION 'Internal Declarations'}
private
FStack: TStack<T>;
FLock: TCriticalSection;
{$ENDREGION 'Internal Declarations'}
public
constructor Create;
destructor Destroy; override;
{ Clears the stack }
procedure Clear; inline;
{ Pushes a value onto the (top of the) stack.
Parameters:
AValue: the value to push }
procedure Push(const AValue: T); inline;
{ Peeks at the top of the stack.
Returns:
The value on the top of the stack, without popping it }
function Peek: T; inline;
{ Pops a value from the (top of the) stack.
Returns:
The popped value. }
function Pop: T; inline;
{ Tries to pop a value from the (top of the) stack.
Parameters:
AValue: is set to the popped value on success, or Default(T) on
failure.
Returns:
True if there was a value to pop, or False otherwise }
function TryPop(out AValue: T): Boolean; inline;
{ Trims excess memory used by the stack. To improve performance and reduce
memory reallocations, the stack usually contains space for more items than
are actually stored in this list. That is, Capacity >= Count. Call this
method free that excess memory. You can do this when you are done filling
the stack to free memory. }
procedure TrimExcess; inline;
end;
type
{ Read-only view of a TQueue<T> }
IReadOnlyQueue<T> = interface(IEnumerable<T>)
{$REGION 'Internal Declarations'}
function GetCount: Integer;
function GetCapacity: Integer;
{$ENDREGION 'Internal Declarations'}
{ Peeks at the beginning of the queue.
Returns:
The value at the beginning of the queue, without dequeueing it }
function Peek: T;
{ The number of items in the queue }
property Count: Integer read GetCount;
{ The number of reserved items in the queue. Is >= Count to improve
performance by reducing memory reallocations. }
property Capacity: Integer read GetCapacity;
end;
type
{ A generic queue (First-In-First-Out), similar to Delphi's TQueue<T> }
TQueue<T> = class(TEnumerable<T>, IReadOnlyQueue<T>)
{$REGION 'Internal Declarations'}
public type
TEnumerator = class(TEnumerator<T>)
{$REGION 'Internal Declarations'}
private
FQueue: TQueue<T>;
FIndex: Integer;
FHigh: Integer;
protected
function GetCurrent: T; override;
{$ENDREGION 'Internal Declarations'}
public
constructor Create(const AQueue: TQueue<T>);
function MoveNext: Boolean; override;
end;
protected
FItems: TArray<T>;
FCount: Integer;
FHead: Integer;
FTail: Integer;
FMask: Integer;
private
procedure Grow;
procedure SetCapacity(const Value: Integer);
protected
{ IReadOnlyStack<T> }
function GetCount: Integer;
function GetCapacity: Integer;
protected
procedure ItemDeleted(const AItem: T); virtual;
{$ENDREGION 'Internal Declarations'}
public
{ TEnumerable<T> }
{ Copies the elements in the queue to a dynamic array }
function ToArray: TArray<T>; override; final;
{ Allow <tt>for..in</tt> enumeration of the queue.
Items are enumerated in enqueue order. }
function GetEnumerator: TEnumerator<T>; override;
public
{ Clears the queue }
procedure Clear; virtual;
{ Enqueues a value (adds it to the end of the queue)
Parameters:
AValue: the value to enqueue }
procedure Enqueue(const AValue: T);
{ Peeks at the beginning of the queue.
Returns:
The value at the beginning of the queue, without dequeueing it }
function Peek: T;
{ Dequeues a value (removes it from the beginning of the queue)
Returns:
The dequeued value }
function Dequeue: T;
{ Tries to dequeue a value (and remove it from the beginning of the queue)
Parameters:
AValue: is set to the dequeued value on success, or Default(T) on
failure.
Returns:
True if there was a value to dequeue, or False otherwise }
function TryDequeue(out AValue: T): Boolean; inline;
{ The number of items in the queue }
property Count: Integer read FCount;
{ The number of reserved items in the queue. Is >= Count to improve
performance by reducing memory reallocations. }
property Capacity: Integer read GetCapacity write SetCapacity;
end;
type
{ Generic queue that owns its object.
Similar to Delphi's TObjectQueue. }
TObjectQueue<T: class> = class(TQueue<T>)
{$REGION 'Internal Declarations'}
protected
procedure ItemDeleted(const AItem: T); override;
public
destructor Destroy; override;
procedure Clear; override;
{$ENDREGION 'Internal Declarations'}
public
{ Dequeues a value (removes it from the beginning of the queue) and frees
it. This version does not return any value, because that value could have
been freed already. Instead, you should use Peek to inspect the value
before dequeueing it, or Extract to dequeue the value without freeing it. }
procedure Dequeue;
{ Dequeues a value @bold(without) freeing it.
Returns:
The dequeued value }
function Extract: T;
end;
type
{ A thread-safe queue }
TConcurrentQueue<T> = class
{$REGION 'Internal Declarations'}
private
FQueue: TQueue<T>;
FLock: TCriticalSection;
{$ENDREGION 'Internal Declarations'}
public