-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxLogic.FastList.pas
More file actions
2159 lines (1822 loc) · 49.4 KB
/
Copy pathMaxLogic.FastList.pas
File metadata and controls
2159 lines (1822 loc) · 49.4 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 maxLogic.FastList;
{ *******************************************************
Digital Omerta 3D Engine
Unit Author: Pawel Piotrowski
Copyright Pawel Piotrowski
STATUS:
WORKING
Version: 2.15
History
2019-12-24: GetRangeIndices implemented for TSimpleList and TSortedList
2019-10-15: managed types fully supported with TSimpleList and TSortedList
2015-08-05: hotfix when using managed types with TSimpleList or TSortedList
2015-07-23: compatible with delphi xe2
2015-07-14: some optimizations, some generic support
2007-12-22 Making it more flexible
******************************************************* }
interface
uses
Classes, types, SysUtils, SyncObjs, Contnrs, generics.collections, generics.defaults;
type
{$IFDEF CLASSIMPORTER}
Pointer = integer;
pInteger = integer;
{$ENDIF}
{ **
This is just a small record, to help making real small lists, no larger then 255 elements long
** }
TSmallList = packed record
List: array of Pointer;
Capacity,
Count: byte;
end;
TListAsRecord<t >= record
private
fCount: integer;
function getCapacity: integer;
procedure SetCapacity(const Value: integer);
function GetItem(Index: integer): t;
procedure Setitem(Index: integer; const Value: t);
procedure setCount(const Value: integer);
public
// direct access
Items: TArray<t>;
property Capacity: integer read getCapacity write SetCapacity;
property Count: integer read fCount write setCount;
property Item[Index: integer]: t read GetItem write Setitem;
default;
procedure add(const Value: t);
procedure Clear;
class
function Create: TListAsRecord<t>;
static;
end;
{ *******************************************************
Class: TIDSortedList
Author: pawelp
Date: 2004-03-30 13:48
DESCRIPTION:
This list uses QuickSort Algo to sort its items by the given ID, which can be either
an Integer or an Single.
NOTE:
It is not recomendet to mix integer and Single values... it is posible... byt the result are not tested
******************************************************* }
TSortedList<TKey, TValue> = class
private
type
TIDSortedListItem = packed record
Data: TValue;
IID: TKey;
end;
private
fComparer: IComparer<TKey>;
fHasManagedType: boolean;
fCount: integer;
FGrowthFactor: Single;
FDupIgnore: boolean;
procedure SetCapacity(const Value: integer);
function getCapacity: integer;
function GetFirst: TValue;
function GetLast: TValue;
function GetItem(Index: integer): TValue;
procedure Setitem(Index: integer; const Value: TValue);
procedure setCount(const Value: integer);
procedure SetGrowthFactor(const Value: Single);
procedure SetDupIgnore(const Value: boolean);
function DoFind(const Id: TKey;
var l, H: integer;
out Index: integer): boolean;
inline;
public
// you have direct access to the underlying itens, but, do not change the iid, otherwise you will need to resort the array
Items: array of TIDSortedListItem;
constructor Create(aComparer: IComparer<TKey>);
overload;
constructor Create;
overload;
destructor Destroy;
override;
// I know... should be name/value, but that is like that to keep backwards copatibility with some legacy code
// returns false if IgnoreDuplicates is true and this item already exists
function add(const Value: TValue; const Id: TKey): boolean;
procedure AddOrSet(const Value: TValue; const Id: TKey);
// if you know exactly where it goes, then you can use this method
procedure Insert(aIndex: integer; const Value: TValue; const Id: TKey);
// note: if duplicates are allowed, then this will return an undefined item from the items with the same id. please refer to GetRangeIndices if you need to get the minIndex and maxIndex of the items with the same id
function find(const Id: TKey; out Index: integer): boolean;
virtual;
// please note: it will return just the first item that matches
// aOffset tells us from which item to start, in other words, how many items to skip
function IndexOf(const Value: TValue; aOffset: integer = 0;
aComparer: IComparer<TValue> = nil): integer;
{ Allows us to Retrieve the min and max index of items with the same id
Note: aLeftIndex and aRightIndex will be the same if there is 1 or less items with the same id }
function FindMinMaxIndex(const aId: TKey; out aLowIndex, aHighIndex: integer): boolean;
{ will search of items that are between those values : aLeftKey <= aItem.ID <= aRightKey
will return false if aRightKey< aLeftKey
will return false if none of the values are between aLeftKey and aRightKey
NOTE: on result= false the indices are undefined }
function FindValuesBetween(const aLeftKey, aRightKey: TKey;
out aLowIndex, aHighIndex: integer): boolean;
procedure Delete(Index: integer);
virtual;
procedure Clear;
virtual;
procedure TrimExcess;
virtual;
// re-sort the underlying array
// useful if you used the insert method to append values
procedure sort;
// please note, this will work only if TKey and TValue are neither strings,pointers, classes or interfaces
// that is here mostly for backwards compatibility
procedure SaveToStream(Stream: TStream);
function SaveToMemorySize: integer;
class function GetMinRequiredmemorySizeForSaving: integer;
class procedure SaveEmptyListToMemory(var pb: pByte);
procedure SaveToMemory(var bytes: pByte);
procedure LoadFromStream(Stream: TStream);
procedure LoadFromMemory(var pb: pByte);
property First: TValue read GetFirst;
property Last: TValue read GetLast;
property Capacity: integer read getCapacity write SetCapacity;
// Please note: the new items are of undefined value, they are not filled in with anything - except if they are managed types, in that case they are set to their default values
property Count: integer read fCount write setCount;
property Item[Index: integer]: TValue read GetItem write Setitem;
default;
property GrowthFactor: Single read FGrowthFactor write SetGrowthFactor;
// ATTENTION: Existing values are not checked to be unique. this applies only to adding values
property dupIgnore: boolean read FDupIgnore write SetDupIgnore;
// ATTENTION: call sort() to re-sort the underlying array
property Comparer: IComparer<TKey>read fComparer write fComparer;
end;
TIdIntegerSortedList = TSortedList<integer, Pointer>;
TIdFloatSortedList = TSortedList<Single, Pointer>;
TIDSortedListItem2 = packed record
Data1: Pointer;
Data2: Pointer;
end;
TIDSortedList2 = class(TSortedList<integer, TIDSortedListItem2>)
private
function GetFirst_Data1: Pointer;
function GetFirst_Data2: Pointer;
function GetLast_Data1: Pointer;
function GetLast_Data2: Pointer;
function GetItem_Data1(Index: integer): Pointer;
function GetItem_Data2(Index: integer): Pointer;
procedure SetItem_Data1(Index: integer; const Value: Pointer);
procedure SetItem_Data2(Index: integer; const Value: Pointer);
public
property First_Data1: Pointer read GetFirst_Data1;
property First_Data2: Pointer read GetFirst_Data2;
property Last_Data1: Pointer read GetLast_Data1;
property Last_Data2: Pointer read GetLast_Data2;
property Item_Data1[Index: integer]: Pointer read GetItem_Data1 write SetItem_Data1;
property Item_Data2[Index: integer]: Pointer read GetItem_Data2 write SetItem_Data2;
procedure add(p1, p2: Pointer; Id: integer);
reintroduce;
function IndexOf_data1(p: Pointer): integer;
function IndexOf_data2(p: Pointer): integer;
end;
TIndexedIDSortedListItem = packed record
Data: Pointer;
pIndex: pInteger;
case boolean of
True:
(IID: integer; );
False:
(fid: Single; );
end;
{ *******************************************************
Class: TIndexedIDSortedList
Author: pawelp
Date: 2004-03-30 13:50
DESCRIPTION:
This is a mix between the FastList and the IDSortedList.
Like the IDSortedList the IndexedIDSortedList sorts its items by an given ID,
which can be an Integer or an Single value (please do not mix single and integers!!!).
And Stores the Index of an Item in pIndex which is an pointer to an use given Integer Value
******************************************************* }
TIndexedIDSortedList = class
private
FCapacity: integer;
fCount: integer;
procedure SetCapacity(const Value: integer);
function GetFirst: Pointer;
function GetLast: Pointer;
function GetItem(Index: integer): Pointer;
protected
public
Items: array of TIndexedIDSortedListItem;
property First: Pointer read GetFirst;
property Last: Pointer read GetLast;
property Capacity: integer read FCapacity write SetCapacity;
property Count: integer read fCount;
property Item[Index: integer]: Pointer read GetItem;
default;
constructor Create;
destructor Destroy;
override;
procedure add(p: Pointer; Id: integer; pIndex: pInteger);
overload;
procedure add(p: Pointer; Id: Single; pIndex: pInteger);
overload;
function find(Id: integer; out Index: integer): boolean;
overload;
function find(Id: Single; out Index: integer): boolean;
overload;
function IndexOf(p: Pointer): integer;
procedure Delete(i: integer);
procedure Clear;
end;
{ *******************************************************
Date: 2004-03-30 13:53
DESCRIPTION:
THis is an Array based List, like TList, but it does not change its Capacity during deleting or
Clear Proc. So we have an much smaller memory allocation Rate.
******************************************************* }
TSimpleList<t> = class
private
fCount: integer;
FGrowthFactor: Single;
fComparer: IComparer<t>;
FSorted: boolean;
fHasManagedType: boolean;
FDupIgnore: boolean;
procedure SetCapacity(const Value: integer);
function GetItem(Index: integer): t;
procedure Setitem(Index: integer; const Value: t);
procedure SetGrowthFactor(const Value: Single);
procedure setCount(const Value: integer);
procedure SetSorted(const Value: boolean);
function getCapacity: integer;
procedure SetDupIgnore(const Value: boolean);
public
// you have direct access to the array, but please be aware not to mess with it
Items: array of t;
function Slice(acount: integer): TArray<t>;
property Count: integer read fCount write setCount;
property Capacity: integer read getCapacity write SetCapacity;
property Item[Index: integer]: t read GetItem write Setitem;
default;
// ATTENTION: Existing values are not checked to be unique. this applies only to adding values
// ATTENTION: use only in combination with sorted!.
// if dupIgnore = true this will set sorted to true as well
property dupIgnore: boolean read FDupIgnore write SetDupIgnore;
// the multiplier when growing the list. default is 2.0
property GrowthFactor: Single read FGrowthFactor write SetGrowthFactor;
// ATTENTION: seting this to false will set dupIgnore to false as well
property Sorted: boolean read FSorted write SetSorted;
constructor Create(aComparer: IComparer<t>);
overload;
constructor Create;
overload;
destructor Destroy;
override;
// if DupIgnore is true, then it will check first, if the value does exist.
// returns true, if the value was added, false otherwise
function add(const Value: t): boolean;
// does not check for duplicates, ignores sorted. Be careful, you might end up with a miss sorted list
procedure Insert(Index: integer; const Value: t);
// if you want to add only a part of the array, then set aStartIndex and aCount
// NOTE: doesn't check for duplicate values inside the avalues array
procedure addSortedArray(const avalues: array of t; aStartIndex: integer; acount: integer);
procedure addList(const avalues: TSimpleList<t>);
// note: if sorted = true, the values will be sorted and addSortedArray will be called
// otherwise the values are just appended at the end
// NOTE: doesn't check for duplicate values inside the avalues array
procedure append(const Values: TArray<t>; aStartIndex: integer; acount: integer);
procedure Clear;
procedure TrimExcess;
function IndexOf(const Value: t): integer;
// requires sorted to be true!
function find(const Value: t; out Index: integer): boolean;
virtual;
function Contains(const aValue: t): boolean;
// requires sorted to be true!
// returns false if the range is not contained in the list
function GetRangeIndices(const LeftValue, RightValue: t; out LeftIndex, RightIndex: integer): boolean;
procedure Delete(i: integer);
overload;
procedure Delete(aStartIndex, aNumOfItemsToDelete: integer);
overload;
procedure Exchange(i1, i2: integer);
function toarray: TArray<t>;
procedure SaveToStream(Stream: TStream);
procedure LoadFromStream(Stream: TStream);
end;
pLLItem = ^TTLLItem;
TTLLItem = record
prev,
Next: pLLItem;
Data: Pointer;
end;
{ *******************************************************
Class: TLinkedList
Author: pawelp
Date: 2004-03-30 13:52
DESCRIPTION:
An typical Linked List
******************************************************* }
TLinkedList = class
private
fCount: integer;
fFirst, fLast: pLLItem;
function GetLLItems(Index: integer): pLLItem;
function GetItem(Index: integer): Pointer;
procedure Setitem(Index: integer; const Value: Pointer);
function GetNextLLItem(LLItem: pLLItem): pLLItem;
function GetPrevLLItem(LLItem: pLLItem): pLLItem;
function GetEmpty: boolean;
protected
public
property LLItems[Index: integer]: pLLItem read GetLLItems;
property First: pLLItem read fFirst;
property Last: pLLItem read fLast;
property Empty: boolean read GetEmpty; // tells if the list is empty or not
property Next[LLItem: pLLItem]: pLLItem read GetNextLLItem;
property prev[LLItem: pLLItem]: pLLItem read GetPrevLLItem;
property Count: integer read fCount;
property Items[Index: integer]: Pointer read GetItem write Setitem;
constructor Create;
destructor Destroy;
override;
procedure add(Obj: Pointer);
procedure AddAfter(Item: pLLItem; Obj: Pointer);
procedure AddBefore(Item: pLLItem; Obj: Pointer);
procedure Delete(Index: integer);
overload;
procedure Delete(p: pLLItem);
overload;
procedure DeleteFirst;
procedure DeleteLast;
procedure Exchange(i1, i2: integer);
procedure Clear;
function IndexOf(p: Pointer; var LLItem: pLLItem): integer;
end;
TFastListItem = record
Obj: Pointer;
pIndex: pInteger;
end;
{ *******************************************************
Class : TFastList
Author: pawelp
Date: 2004-03-30 13:44
DESCRIPTION:
This is a fast List, where each Item have an Pointer to some user defined data
and an Pointer where it updates its actual position.
This is very good for often searchable Lists
During deletion the last Item in the list is moved into the place
of the deleted item, this way we have only a small memory movment, and the indexes of most ites are not affected
******************************************************* }
TFastList = class
private
fItems: array of TFastListItem;
FCapacity, fCount, fInitSize: integer;
function GetItem(Index: integer): Pointer;
protected
public
property Item[Index: integer]: Pointer read GetItem;
default;
property Count: integer read fCount;
constructor Create(InitSize: integer);
destructor Destroy;
override;
procedure Clear;
procedure ClearHard;
procedure Delete(Index: integer);
function IndexOf(p: Pointer): integer;
procedure add(p: Pointer; pIndex: pInteger);
end;
TThreadSaveObjectList = class(TObject)
private
fSec: TCriticalSection;
fList: TObjectList;
function GetCount: integer;
protected
public
constructor Create;
destructor Destroy;
override;
procedure add(aObject: TObject);
procedure AddAsFirst(aObject: TObject);
function GetFirstAndRemFromList(out aObject: TObject): boolean;
function GetLastAndRemFromList(out aObject: TObject): boolean;
procedure Clear;
property Count: integer read GetCount;
end;
// windows replacement
procedure ZeroMemory(p: Pointer; Size: integer);
implementation
uses
System.RTLConsts, Rtti;
{ TFastList }
procedure ZeroMemory(p: Pointer; Size: integer);
begin
FillChar(p^, Size, 0);
end;
procedure TFastList.add(p: Pointer; pIndex: pInteger);
begin
if fCount >= FCapacity then
begin
Inc(FCapacity, self.fInitSize);
SetLength(fItems, FCapacity);
end;
fItems[fCount].Obj := p;
fItems[fCount].pIndex := pIndex;
pIndex^ := fCount;
Inc(fCount);
end;
procedure TFastList.Clear;
var
li: integer;
procedure c(st: integer);
var
X: integer;
begin
try
for X := st to fCount - 1 do
begin
li := X;
if fItems[X].pIndex <> nil then
fItems[X].pIndex^ := -1;
end;
except
c(li + 1);
end;
end;
begin
c(0);
fCount := 0;
end;
procedure TFastList.ClearHard;
begin
fCount := 0;
end;
constructor TFastList.Create(InitSize: integer);
begin
inherited Create;
FCapacity := InitSize;
SetLength(fItems, FCapacity);
fCount := 0;
fInitSize := InitSize;
end;
procedure TFastList.Delete(Index: integer);
begin
fItems[Index].pIndex^ := -1;
if (Index <> fCount - 1) and (fCount <> 1) then
begin
fItems[Index] := fItems[fCount - 1];
fItems[Index].pIndex^ := Index;
end;
Dec(fCount);
end;
destructor TFastList.Destroy;
begin
Clear;
fItems := nil;
inherited;
end;
function TFastList.GetItem(Index: integer): Pointer;
begin
Result := fItems[Index].Obj;
end;
function TFastList.IndexOf(p: Pointer): integer;
var
X: integer;
begin
for X := 0 to fCount - 1 do
if p = fItems[X].Obj then
begin
Result := X;
exit;
end;
Result := -1;
end;
{ TTLinkedList }
procedure TLinkedList.add(Obj: Pointer);
begin
AddAfter(Last, Obj);
end;
procedure TLinkedList.AddAfter(Item: pLLItem; Obj: Pointer);
var
NewItem: pLLItem;
begin
new(NewItem);
NewItem.Next := nil;
NewItem.prev := nil;
NewItem^.Data := Obj;
Inc(fCount);
if (Item = nil) and (fLast <> nil) then
Item := fLast;
if Item <> nil then
begin
NewItem.Next := Item.Next;
if assigned(Item.Next) then
Item.Next.prev := NewItem;
Item.Next := NewItem;
NewItem.prev := Item;
if Item = fLast then
fLast := NewItem;
end
else
begin
fLast := NewItem;
fFirst := NewItem;
end;
end;
procedure TLinkedList.AddBefore(Item: pLLItem; Obj: Pointer);
var
NewItem: pLLItem;
begin
new(NewItem);
NewItem.Next := nil;
NewItem.prev := nil;
NewItem^.Data := Obj;
Inc(fCount);
if Item = nil then
Item := fFirst;
if Item = nil then
begin
fFirst := NewItem;
fLast := NewItem;
end
else
begin
NewItem.prev := Item.prev;
if assigned(Item.prev) then
Item.prev.Next := NewItem;
Item.prev := NewItem;
NewItem.Next := Item;
if Item = fFirst then
fFirst := NewItem;
end;
end;
procedure TLinkedList.Clear;
var
p0, p1: pLLItem;
begin
p0 := First;
while p0 <> nil do
begin
p1 := p0;
p0 := p0.Next;
dispose(p1);
end;
fFirst := nil;
fLast := nil;
fCount := 0;
end;
constructor TLinkedList.Create;
begin
inherited;
fFirst := nil;
fLast := nil;
fCount := 0;
end;
procedure TLinkedList.Delete(p: pLLItem);
begin
if p = nil then
exit;
if (p <> fLast) and (p <> fFirst) then
begin
if p.prev <> nil then
p.prev.Next := p.Next;
if p.Next <> nil then
p.Next.prev := p.prev;
end
else
begin
if (p = fFirst) and (p = fLast) then
begin
fFirst := nil;
fLast := nil;
end
else if p = fFirst then
begin
fFirst := p.Next;
if fFirst <> nil then
fFirst.prev := nil;
end
else if p = fLast then
begin
fLast := p.prev;
if fLast <> nil then
fLast.Next := nil;
end;
end;
dispose(p);
Dec(fCount);
end;
procedure TLinkedList.DeleteLast;
begin
if fLast <> nil then
self.Delete(self.fLast);
end;
procedure TLinkedList.DeleteFirst;
begin
if fFirst <> nil then
Delete(fFirst);
end;
procedure TLinkedList.Delete(Index: integer);
var
p: pLLItem;
begin
p := LLItems[Index];
if p <> nil then
Delete(p);
end;
destructor TLinkedList.Destroy;
begin
Clear;
inherited;
end;
procedure TLinkedList.Exchange(i1, i2: integer);
var
p0, p1, p2: pLLItem;
begin
p1 := LLItems[i1];
p2 := LLItems[i2];
if p1.prev <> nil then
p1.prev.Next := p2;
if p1.Next <> nil then
p1.Next.prev := p2;
if p2.prev <> nil then
p2.prev.Next := p1;
if p2.Next <> nil then
p2.Next.prev := p1;
p0 := p1.Next;
p1.Next := p2.Next;
p2.Next := p0;
p0 := p1.prev;
p1.prev := p2.prev;
p2.prev := p0;
p0 := p2.Next;
p2.Next := p1.Next;
p1.Next := p0;
p0 := p2.prev;
p2.prev := p1.prev;
p1.prev := p0;
end;
function TLinkedList.GetEmpty: boolean;
begin
Result := fFirst = nil;
end;
function TLinkedList.GetItem(Index: integer): Pointer;
var
p: pLLItem;
begin
p := LLItems[Index];
if p <> nil then
Result := p^.Data
else
Result := nil;
end;
function TLinkedList.GetLLItems(Index: integer): pLLItem;
var
i: integer;
begin
if (fCount - Index) > Index then
begin
Result := fFirst;
i := 0;
while (Result <> nil) and (i <> Index) do
begin
Result := Result.Next;
Inc(i);
end;
end
else
begin
Result := fLast;
i := fCount - 1;
while (Result <> nil) and (i <> Index) do
begin
Result := Result.prev;
Dec(i);
end;
end;
end;
function TLinkedList.GetNextLLItem(LLItem: pLLItem): pLLItem;
begin
Result := LLItem^.Next;
end;
function TLinkedList.GetPrevLLItem(LLItem: pLLItem): pLLItem;
begin
Result := LLItem.prev;
end;
function TLinkedList.IndexOf(p: Pointer; var LLItem: pLLItem): integer;
var
pi: pLLItem;
begin
pi := First;
Result := -1;
while pi <> nil do
begin
Inc(Result);
if pi = p then
begin
if LLItem <> nil then
LLItem := pi;
exit;
end
else
pi := pi.Next;
end;
Result := -1;
end;
procedure TLinkedList.Setitem(Index: integer; const Value: Pointer);
var
p: pLLItem;
begin
p := LLItems[Index];
if p <> nil then
p^.Data := Value;
end;
{ TSimpleList<T> }
function TSimpleList<t>.add(const Value: t): boolean;
var
c, i: integer;
begin
if not FSorted then
begin
Result := True;
if Count + 1 >= Capacity then
Capacity := Round((Capacity + 1) * GrowthFactor);
Items[fCount] := Value;
Inc(fCount);
end
else
begin
if self.Count = 0 then
begin
Count := 1;
Items[0] := Value;
exit(True);
end
else
begin
{ a small speedup:
we know, that we mostly add the next bigger item,
in that case, let us first check the last item, that way we can skip a lot of comparisions }
i := Count - 1;
c := fComparer.Compare(Items[i], Value);
if FDupIgnore and (c = 0) then
exit(False)
else if c < 0 then
i := Count
else if find(Value, i) and FDupIgnore then
exit(False);
end;
Result := True;
Insert(i, Value);
end;
end;
procedure TSimpleList<t>.addList(const avalues: TSimpleList<t>);
begin
if avalues.Count = 0 then
exit;
if avalues.Sorted then
self.addSortedArray(avalues.Items, 0, avalues.Count)
else
self.append(avalues.Items, 0, avalues.Count);
end;
procedure TSimpleList<t>.addSortedArray(const avalues: array of t; aStartIndex: integer; acount: integer);
var
X, i1, i2: integer;
elementsToMove: integer;
begin
if acount = -1 then
acount := length(avalues) - aStartIndex;
if fHasManagedType then
begin
// managed types must be properly assigned, not via move memory
for X := aStartIndex to (aStartIndex + acount) - 1 do
add(avalues[X]);
exit;
end;
if acount = 1 then
begin
add(avalues[0]);
exit;
end;
while acount <> 0 do
begin
// can we append all at the end?
if (self.Count = 0)
or (fComparer.Compare(Items[Count - 1], avalues[aStartIndex]) < 0) then
begin
i1 := Count;
Count := Count + acount;
move(avalues[aStartIndex], Items[i1], acount * sizeOf(avalues[0]));
acount := 0;
end
else
{ // can we append the whole Array somewhere In the middle ? }if (not find(avalues[aStartIndex], i1))
and
(not find(avalues[(aStartIndex + acount) - 1], i2))
and
(i1 = i2) then
begin
elementsToMove := (Count - i1); // how many elements do we need to move to the end?
Count := Count + acount; // inc the count and capacity
// move the existing items to the end, so we have space for inserting the whole array
move(Items[i1], Items[i1 + acount], elementsToMove * sizeOf(avalues[0]));
move(avalues[aStartIndex], Items[i1], acount * sizeOf(avalues[0]));
acount := 0;
end
else
begin
/// add the first item in the array, maybe we can append the whole array later, without this element
add(avalues[aStartIndex]);
Inc(aStartIndex);
Dec(acount);
end;
end;
end;
procedure TSimpleList<t>.append(const Values: TArray<t>; aStartIndex: integer; acount: integer);
var
X, i: integer;
lValues: TArray<t>;
begin
if acount = -1 then
acount := length(Values) - aStartIndex;
if acount = 0 then
exit;
if self.Sorted and (not fHasManagedType) then
begin
lValues := copy(Values, aStartIndex, acount);
TArray.sort<t>(lValues, fComparer, 0, length(lValues));
self.addSortedArray(lValues, 0, length(lValues));