This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcontainer.arraylist.pas
More file actions
583 lines (494 loc) · 17.5 KB
/
container.arraylist.pas
File metadata and controls
583 lines (494 loc) · 17.5 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
(******************************************************************************)
(* libPasC-Algorithms *)
(* delphi and object pascal library of common data structures and algorithms *)
(* https://github.com/fragglet/c-algorithms *)
(* *)
(* Copyright (c) 2020 - 2021 Ivan Semenkov *)
(* https://github.com/isemenkov/libpasc-algorithms ivan@semenkov.pro *)
(* Ukraine *)
(******************************************************************************)
(* *)
(* Permission is hereby granted, free of charge, to any person obtaining a *)
(* copy of this software and associated documentation files (the "Software"), *)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)
(* and/or sell copies of the Software, and to permit persons to whom the *)
(* Software is furnished to do so, subject to the following conditions: *)
(* *)
(* The above copyright notice and this permission notice shall be included in *)
(* all copies or substantial portions of the Software. *)
(* *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)
(* DEALINGS IN THE SOFTWARE. *)
(* *)
(******************************************************************************)
unit container.arraylist;
{$IFDEF FPC}
{$mode objfpc}{$H+}
{$ENDIF}
{$IFOPT D+}
{$DEFINE DEBUG}
{$ENDIF}
interface
uses
SysUtils{$IFDEF USE_OPTIONAL}, utils.optional{$ENDIF}, utils.enumerate
{$IFNDEF FPC}, utils.functor{$ENDIF};
type
{$IFNDEF USE_OPTIONAL}
{ ArrayList index is not exists. }
EIndexOutOfRangeException = class(Exception);
{$ENDIF}
{ Automatically resizing array.
ArrayLists are generic arrays of T which automatically increase in size. }
{$IFDEF FPC}generic{$ENDIF} TArrayList<T; BinaryCompareFunctor
{$IFNDEF FPC}: constructor, utils.functor.TBinaryFunctor<T,
Integer>{$ENDIF}> = class
public
type
{$IFDEF USE_OPTIONAL}
TOptionalValue = {$IFDEF FPC}specialize{$ENDIF} TOptional<T>;
TOptionalIndex = {$IFDEF FPC}specialize{$ENDIF} TOptional<LongInt>;
{$ENDIF}
protected
type
{ Internal container storage data type. }
PData = ^TData;
TData = record
Value : T;
end;
TDynArray = array of PData;
PDynArray = ^TDynArray;
public
type
{ TArrayList iterator. }
TIterator = class; { Fix for FreePascal compiler. }
TIterator = class ({$IFDEF FPC}specialize{$ENDIF}
TBidirectionalIterator<T, TIterator>)
protected
{ Create new iterator for arraylist item entry. }
{%H-}constructor Create (Arr : PDynArray; Len : Cardinal; Pos :
Integer);
public
{ Return true if iterator has correct value }
function HasValue : Boolean; override;
{ Retrieve the previous entry in a list. }
function Prev : TIterator; override;
{ Retrieve the next entry in a list. }
function Next : TIterator; override;
{ Return True if we can move to next element. }
function MoveNext : Boolean; override;
{ Return enumerator for in operator. }
function GetEnumerator : TIterator; override;
protected
{ Get item value. }
function GetValue : {$IFNDEF USE_OPTIONAL}T{$ELSE}TOptionalValue
{$ENDIF}; override;
{ Set new item value. }
procedure SetValue (AValue : {$IFNDEF USE_OPTIONAL}T{$ELSE}
TOptionalValue{$ENDIF});
{ Get item index. }
function GetItemIndex : {$IFNDEF USE_OPTIONAL}LongInt{$ELSE}
TOptionalIndex{$ENDIF};
{ Return current item iterator and move it to next. }
function GetCurrent : {$IFNDEF USE_OPTIONAL}T{$ELSE}TOptionalValue
{$ENDIF}; override;
public
{ Read/Write arraylist item value. If value not exists raise
EIndexOutOfRangeException. }
property Value : {$IFNDEF USE_OPTIONAL}T{$ELSE}TOptionalValue{$ENDIF}
read GetValue write SetValue;
{ Get current item index. If value not exists raise
EIndexOutOfRangeException. }
property Index : {$IFNDEF USE_OPTIONAL}LongInt{$ELSE}TOptionalIndex
{$ENDIF} read GetItemIndex;
property Current : {$IFNDEF USE_OPTIONAL}T{$ELSE}TOptionalValue{$ENDIF}
read GetCurrent;
protected
FArray : PDynArray;
FLength : LongInt;
FPosition : LongInt;
end;
public
constructor Create (ALength : Cardinal = 0);
destructor Destroy; override;
{ Append a value to the end of an ArrayList.
Return true if the request was successful, false if it was not possible to
allocate more memory for the new entry. }
function Append (AValue : T) : Boolean;
{$IFNDEF DEBUG}inline;{$ENDIF}
{ Prepend a value to the beginning of an ArrayList.
Return true if the request was successful, false if it was not possible to
allocate more memory for the new entry. }
function Prepend (AValue : T) : Boolean;
{$IFNDEF DEBUG}inline;{$ENDIF}
{ Remove the entry at the specified location in an ArrayList.
Return true if the request was successful, false if index is not exists. }
function Remove (AIndex: Cardinal) : Boolean;
{$IFNDEF DEBUG}inline;{$ENDIF}
{ Remove a range of entries at the specified location in an ArrayList.
Return true if the request was successful, false if not. }
function RemoveRange (AIndex : LongInt; ALength : LongInt) : Boolean;
{ Insert a value at the specified index in an ArrayList.
The index where the new value can be inserted is limited by the size of
the ArrayList.
Returns false if unsuccessful, else true if successful (due to an invalid
index or if it was impossible to allocate more memory). }
function Insert (AIndex : LongInt; AData : T) : Boolean;
{ Find the index of a particular value in an ArrayList.
Return the index of the value if found, or -1 if not found. }
function IndexOf (AData : T) : Integer;
{ Retrive the first entry in a arraylist. }
function FirstEntry : TIterator;
{$IFNDEF DEBUG}inline;{$ENDIF}
{ Retrive the last entry in a arraylist. }
function LastEntry : TIterator;
{$IFNDEF DEBUG}inline;{$ENDIF}
{ Remove all entries from an ArrayList. }
procedure Clear;
{$IFNDEF DEBUG}inline;{$ENDIF}
{ Sort the values in an ArrayList. }
procedure Sort;
{ Return true if container is empty. }
function IsEmpty : Boolean;
{$IFNDEF DEBUG}inline;{$ENDIF}
{ Return enumerator for in operator. }
function GetEnumerator : TIterator;
{$IFNDEF DEBUG}inline;{$ENDIF}
protected
{ Reallocate the array to the new size }
function Enlarge : Boolean;
{ Sort the values }
procedure SortInternal (var AData : array of PData; ALength : Cardinal);
{ Get value by index. }
function GetValue (AIndex : LongInt) : {$IFNDEF USE_OPTIONAL}T{$ELSE}
TOptionalValue{$ENDIF};
{$IFNDEF DEBUG}inline;{$ENDIF}
{ Set new value by index. }
procedure SetValue (AIndex : LongInt; AData : {$IFNDEF USE_OPTIONAL}T
{$ELSE}TOptionalValue{$ENDIF});
{$IFNDEF DEBUG}inline;{$ENDIF}
protected
var
FData : array of PData;
FLength : LongInt;
FAlloced : LongInt;
FCompareFunctor : BinaryCompareFunctor;
public
{ Read/Write value in an ArrayList. If index not exists raise
EIndexOutOfRangeException. }
property Value [AIndex : LongInt] : {$IFNDEF USE_OPTIONAL}T{$ELSE}
TOptionalValue{$ENDIF} read GetValue write SetValue;
{ Get ArrayList length. }
property Length : LongInt read FLength;
end;
implementation
{ TArrayList.TIterator }
constructor TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.TIterator.Create (Arr : PDynArray; Len : Cardinal; Pos : Integer);
begin
FArray := Arr;
FLength := Len;
FPosition := Pos;
end;
function TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.TIterator.HasValue : Boolean;
begin
if (FPosition >= FLength) or (FPosition < 0) then
begin
Exit(False);
end;
Result := True;
end;
function TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.TIterator.Prev : TIterator;
begin
Result := TIterator.Create(FArray, FLength, FPosition - 1);
end;
function TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.TIterator.Next : TIterator;
begin
Result := TIterator.Create(FArray, FLength, FPosition + 1);
end;
function TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.TIterator.MoveNext : Boolean;
begin
Result := FPosition < FLength;
end;
function TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.TIterator.GetEnumerator : TIterator;
begin
Result := TIterator.Create(FArray, FLength, FPosition);
end;
function TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.TIterator.GetValue : {$IFNDEF USE_OPTIONAL}T{$ELSE}
TOptionalValue{$ENDIF};
begin
if FPosition > FLength then
begin
{$IFNDEF USE_OPTIONAL}
raise EIndexOutOfRangeException.Create('Index out of range.');
{$ELSE}
Exit(TOptionalValue.Create);
{$ENDIF}
end;
Result := {$IFDEF USE_OPTIONAL}TOptionalValue.Create({$ENDIF}
FArray^[FPosition]^.Value{$IFDEF USE_OPTIONAL}){$ENDIF};
end;
procedure TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.TIterator.SetValue (AValue : {$IFNDEF USE_OPTIONAL}T{$ELSE}
TOptionalValue{$ENDIF});
begin
if FPosition > FLength then
begin
{$IFNDEF USE_OPTIONAL}
raise EIndexOutOfRangeException.Create('Index out of range.');
{$ELSE}
Exit;
{$ENDIF}
end;
FArray^[FPosition]^.Value := AValue{$IFDEF USE_OPTIONAL}.Unwrap{$ENDIF};
end;
function TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.TIterator.GetItemIndex : {$IFNDEF USE_OPTIONAL}LongInt
{$ELSE}TOptionalIndex{$ENDIF};
begin
if FPosition > FLength then
begin
{$IFNDEF USE_OPTIONAL}
raise EIndexOutOfRangeException.Create('Index out of range.');
{$ELSE}
Exit(TOptionalIndex.Create);
{$ENDIF}
end;
Result := {$IFNDEF USE_OPTIONAL}FPosition{$ELSE}
TOptionalIndex.Create(FPosition){$ENDIF};
end;
function TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.TIterator.GetCurrent : {$IFNDEF USE_OPTIONAL}T{$ELSE}
TOptionalValue{$ENDIF};
begin
Result := GetValue;
Inc(FPosition);
end;
{ TArrayList }
constructor TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.Create(ALength : Cardinal);
begin
if ALength = 0 then
begin
ALength := 16;
end;
SetLength(FData, ALength);
FAlloced := ALength;
FLength := 0;
FCompareFunctor := BinaryCompareFunctor.Create;
end;
destructor TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.Destroy;
begin
SetLength(FData, 0);
inherited Destroy;
end;
function TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.GetValue (AIndex : LongInt) : {$IFNDEF USE_OPTIONAL}T
{$ELSE}TOptionalValue{$ENDIF};
begin
if (AIndex >= FLength) or (AIndex < 0) then
begin
{$IFNDEF USE_OPTIONAL}
raise EIndexOutOfRangeException.Create('Index out of range.');
{$ELSE}
Exit(TOptionalValue.Create);
{$ENDIF}
end;
Result := {$IFDEF USE_OPTIONAL}TOptionalValue.Create({$ENDIF}FData[AIndex]^
.Value{$IFDEF USE_OPTIONAL}){$ENDIF};
end;
procedure TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.SetValue (AIndex : LongInt; AData : {$IFNDEF USE_OPTIONAL}
T{$ELSE}TOptionalValue{$ENDIF});
begin
if AIndex > FLength then
begin
{$IFNDEF USE_OPTIONAL}
raise EIndexOutOfRangeException.Create('Index out of range.');
{$ELSE}
Exit;
{$ENDIF}
end;
New(FData[AIndex]);
FData[AIndex]^.Value := AData{$IFDEF USE_OPTIONAL}.Unwrap{$ENDIF};
end;
function TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.Enlarge : Boolean;
var
NewSize : Cardinal;
begin
{ Double the allocated size }
NewSize := FAlloced * 2;
{ Reallocate the array to the new size }
SetLength(FData, NewSize);
FAlloced := NewSize;
Result := True;
end;
procedure TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.SortInternal (var AData : array of PData; ALength : Cardinal);
var
pivot, tmp : PData;
list1_length, list2_length : Cardinal;
i : Cardinal;
begin
{ If less than two items, it is always sorted. }
if ALength <= 1 then
begin
Exit;
end;
{ Take the last item as the pivot. }
pivot := AData[ALength - 1];
{ Divide the list into two lists:
List 1 contains data less than the pivot.
List 2 contains data more than the pivot.
As the lists are build up, they are stored sequentially after each other,
ie. AData[ALength - 1] is the last item in list 1, AData[ALength] is the
first item in list 2. }
list1_length := 0;
for i := 0 to ALength - 1 do
begin
if FCompareFunctor.Call(AData[i]^.Value, pivot^.Value) < 0 then
begin
{ This should be in list 1. Therefore it is in the wrong position. Swap
the data immediately following the last item in list 1 with this data. }
tmp := AData[i];
AData[i] := AData[list1_length];
AData[list1_length] := tmp;
Inc(list1_length);
end else
begin
{ This should be in list 2. This is already in the right position. }
end;
end;
{ The length of list 2 can be calculated. }
list2_length := ALength - list1_length - 1;
{ AData[0..list1_length - 1] now contains all items which are before the
pivot.
AData[list1_length..ALength - 2] contains all items after or equal to the
pivot.
Move the pivot into place, by swapping it with the item immediately
following the end of list 1. }
AData[ALength - 1] := AData[list1_length];
AData[list1_length] := pivot;
{ Recursively sort the sublists. }
SortInternal(AData, list1_length);
SortInternal(AData[list1_length + 1], list2_length);
end;
function TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.Insert (AIndex : LongInt; AData : T) : Boolean;
begin
{ Sanity check the index }
if (AIndex > FLength) or (AIndex < 0) then
begin
Result := False;
Exit;
end;
{ Increase the size if necessary }
if FLength + 1 > FAlloced then
begin
if not Enlarge then
begin
Result := False;
Exit;
end;
end;
{ Move the contents of the array forward from the index onwards }
System.Move(FData[AIndex], FData[AIndex + 1], (FLength - AIndex) *
Sizeof(PData));
{ Insert the new entry at the index }
New(FData[AIndex]);
FData[AIndex]^.Value := AData;
Inc(FLength);
Result := True;
end;
function TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.Append (AValue : T) : Boolean;
begin
Result := Insert(FLength, AValue);
end;
function TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.Prepend (AValue : T) : Boolean;
begin
Result := Insert(0, AValue);
end;
function TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.RemoveRange (AIndex : LongInt; ALength : LongInt) : Boolean;
begin
{ Check this is a valid range }
if (AIndex > FLength) or (AIndex + ALength > FLength) or (AIndex < 0) then
begin
Result := False;
Exit;
end;
{ Move back the entries following the range to be removed }
Move(FData[AIndex + ALength], FData[AIndex],
(FLength - (AIndex + ALength)) * SizeOf(PData));
Dec(FLength, ALength);
Result := True;
end;
function TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.Remove (AIndex : Cardinal) : Boolean;
begin
Result := RemoveRange(AIndex, 1);
end;
function TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.IndexOf (AData : T) : Integer;
var
i : Cardinal;
begin
if FLength <= 0 then
Exit(-1);
for i := 0 to FLength - 1 do
begin
if FCompareFunctor.Call(FData[i]^.Value, AData) = 0 then
begin
Result := i;
Exit;
end;
end;
Result := -1;
end;
function TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.FirstEntry : TIterator;
begin
Result := TIterator.Create(@FData, FLength, 0);
end;
function TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.LastEntry : TIterator;
begin
Result := TIterator.Create(@FData, FLength, FLength - 1);
end;
procedure TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.Clear;
begin
FLength := 0;
end;
procedure TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.Sort;
begin
{ Perform the recursive sort. }
SortInternal(FData, FLength);
end;
function TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.GetEnumerator : TIterator;
begin
Result := TIterator.Create(@FData, FLength, 0);
end;
function TArrayList{$IFNDEF FPC}<T, BinaryCompareFunctor>{$ENDIF}
.IsEmpty : Boolean;
begin
Result := (Length = 0);
end;
end.