forked from Sovos-Compliance/convey-public-libs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
uLinkedListDefaultImplementor.pas
624 lines (553 loc) · 17.2 KB
/
uLinkedListDefaultImplementor.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
unit uLinkedListDefaultImplementor;
interface
uses
iContainers, uAllocators, uLinkedList;
type
TLinkedList = class (TAbstractLinkedList, ILinkedList, IIterable, IDataHandler, INodeRemover)
private
FAllocator : TFixedBlockHeap;
FDataHandler: IDataHandler;
FEventSubscriber: IIterableEventSubscriber;
FFirstNode: PLinkedNode;
FLastNode: PLinkedNode;
function AllocNode: PLinkedNode;
procedure CleanNode(ANode: PLinkedNode);
procedure DeallocNode(ANode: PLinkedNode);
function GetDataHandler: IDataHandler;
procedure InternalRemoveNode(ANode: PLinkedNode);
function NewNode(APointer: Pointer): PLinkedNode; overload;
function NewAnsiStringNode(const AAnsiString: AnsiString): PLinkedNode;
function NewNode(AInteger: Integer): PLinkedNode; overload;
function NewShortStringNode(const AShortString: ShortString): PLinkedNode;
function NewNode(AInt64: Int64): PLinkedNode; overload;
function NewNode(AExtended: Extended): PLinkedNode; overload;
function NewNode(const ACurrency: Currency): PLinkedNode; overload;
function NewNode(ABoolean: Boolean): PLinkedNode; overload;
function NewNode(AAnsiChar: AnsiChar): PLinkedNode; overload;
function NewNode(AWideChar: WideChar): PLinkedNode; overload;
function NewWideStringNode(const AWideString: WideString): PLinkedNode;
function NewNode(AInterface: IUnknown): PLinkedNode; overload;
function NewNode(AObject: TObject): PLinkedNode; overload;
protected
function InitIterable(AtEnd: Boolean): Pointer; virtual;
function Next(AContext: Pointer): Pointer; virtual; // IIterable
function Prev(AContext : Pointer): Pointer; virtual;
function GetEmpty: Boolean; virtual; // ILinkedList
function InsertNodeInList(ANode: PLinkedNode): PLinkedNode;
procedure INodeRemover_RemoveNode(AContext : Pointer);
procedure INodeRemover.RemoveNode = INodeRemover_RemoveNode;
procedure Clear; virtual; // ILinkedList
function Insert(AInteger: Integer): PLinkedNode; overload; virtual;
function Insert(AObject: TObject): PLinkedNode; overload; virtual;
function InsertAnsiString(const AAnsiString: AnsiString): PLinkedNode; overload; virtual;
function Insert(APointer: Pointer): PLinkedNode; overload; virtual;
function InsertShortString(const AShortString: ShortString): PLinkedNode; overload; virtual;
function Insert(const AInt64: Int64): PLinkedNode; overload; virtual;
function Insert(const AExtended: Extended): PLinkedNode; overload; virtual;
function Insert(const ACurrency: Currency): PLinkedNode; overload; virtual;
function Insert(ABoolean: Boolean): PLinkedNode; overload; virtual;
function Insert(AAnsiChar: AnsiChar): PLinkedNode; overload; virtual;
function Insert(AWideChar: WideChar): PLinkedNode; overload; virtual;
function InsertWideString(const AWideString: WideString): PLinkedNode; overload; virtual;
function Insert(AInterface: IUnknown): PLinkedNode; overload; virtual;
function Remove(APointer: Pointer): Boolean; overload; virtual;
function Remove(AObject: TObject): Boolean; overload; virtual;
function Remove(AInteger: Integer): Boolean; overload; virtual;
function RemoveAnsiString(const AAnsiString: AnsiString): Boolean; overload; virtual;
function RemoveShortString(const AShortString: ShortString): Boolean; overload; virtual;
function Remove(const AInt64: Int64): Boolean; overload; virtual;
function Remove(const AExtended : Extended): Boolean; overload; virtual;
function Remove(const ACurrency : Currency): Boolean; overload; virtual;
function Remove(ABoolean: Boolean): Boolean; overload; virtual;
function Remove(AAnsiChar: AnsiChar): Boolean; overload; virtual;
function Remove(AWideChar: WideChar): Boolean; overload; virtual;
function RemoveWideString(const AWideString: WideString): Boolean; overload; virtual;
function Remove(AInterface: IUnknown): Boolean; overload; virtual;
procedure MoveToStartOfList(ANode : PLinkedNode); virtual;
procedure MoveToEndOfList(ANode : PLinkedNode); virtual;
function FirstNode: PLinkedNode; virtual;
function GetAutoFreeObjects: Boolean;
function LastNode: PLinkedNode; virtual;
procedure SetAutoFreeObjects(Value: Boolean);
procedure SetIterableEventSubscriber(ASubscriber : IIterableEventSubscriber);
function TailableIteratorSupported: Boolean;
property Empty: Boolean read GetEmpty; // ILinkedList
public
constructor Create; override;
destructor Destroy; override;
procedure AfterConstruction; override;
procedure RemoveNode(ANode: PLinkedNode); virtual;
property DataHandler: IDataHandler read GetDataHandler implements IDataHandler;
end;
implementation
uses
uContainerDataHandler, SysUtils;
resourcestring
SCallNewLinkedListToCreateANewLin = 'Call NewLinkedList to create a new LinkedList';
SAContextNilCallingTLinkedListNex = 'AContext = nil calling TLinkedList.Next';
SAContextNilCallingTLinkedListPre = 'AContext = nil calling TLinkedList.Prev';
const
NODEBLOCKS = 8;
constructor TLinkedList.Create;
begin
inherited;
FAllocator := TFixedBlockHeap.Create(sizeof (TLinkedNode), NODEBLOCKS);
FDataHandler := TContainerDataHandler.Create;
end;
destructor TLinkedList.Destroy;
begin
Clear;
FAllocator.Free;
inherited;
end;
procedure TLinkedList.AfterConstruction;
begin
inherited;
if FAllocator = nil
then raise Exception.Create (SCallNewLinkedListToCreateANewLin);
end;
function TLinkedList.AllocNode: PLinkedNode;
begin
if FEventSubscriber <> nil then
FEventSubscriber.BeforeAllocNode;
Result := FAllocator.Alloc;
if FEventSubscriber <> nil then
FEventSubscriber.AfterAllocNode;
Result^.Next := nil;
Result^.Prev := nil;
Result^.Data.AsPointer := nil;
end;
function TLinkedList.InitIterable(AtEnd: Boolean): Pointer;
begin
if not AtEnd
then Result := FFirstNode
else Result := FLastNode;
end;
procedure TLinkedList.Clear;
var
FNextNode : PLinkedNode;
begin
FNextNode := FFirstNode;
while FNextNode <> nil do
begin
FNextNode := FNextNode^.Next;
DeallocNode (FFirstNode);
FFirstNode := FNextNode;
end;
FLastNode := nil;
end;
function TLinkedList.GetEmpty: Boolean;
begin
Result := FFirstNode = nil;
end;
function TLinkedList.Insert(AInteger: Integer): PLinkedNode;
begin
Result := InsertNodeInList(NewNode(AInteger));
end;
function TLinkedList.InsertAnsiString(const AAnsiString: AnsiString):
PLinkedNode;
begin
Result := InsertNodeInList(NewAnsiStringNode(AAnsiString));
end;
function TLinkedList.InsertNodeInList(ANode: PLinkedNode): PLinkedNode;
begin
ANode.Prev := FLastNode;
if FLastNode <> nil then
FLastNode.Next := ANode;
FLastNode := ANode;
if FFirstNode = nil then
FFirstNode := ANode;
if FEventSubscriber <> nil then
FEventSubscriber.ItemInserted(ANode);
Result := ANode;
end;
function TLinkedList.Next(AContext: Pointer): Pointer;
begin
if AContext = nil then
raise EContainer.Create (SAContextNilCallingTLinkedListNex);
Result := PLinkedNode (AContext).Next;
end;
function TLinkedList.NewNode(APointer: Pointer): PLinkedNode;
begin
Result := AllocNode;
FDataHandler.SetPointer(Result, APointer);
end;
function TLinkedList.NewAnsiStringNode(const AAnsiString: AnsiString):
PLinkedNode;
begin
Result := AllocNode;
FDataHandler.SetAnsiString(Result, AAnsiString);
end;
procedure TLinkedList.CleanNode(ANode: PLinkedNode);
begin
FDataHandler.CleanData (ANode);
end;
procedure TLinkedList.DeallocNode(ANode: PLinkedNode);
begin
CleanNode(ANode);
if FEventSubscriber <> nil then
FEventSubscriber.BeforeDeallocNode;
Dealloc(ANode);
if FEventSubscriber <> nil then
FEventSubscriber.AfterDeallocNode;
end;
function TLinkedList.FirstNode: PLinkedNode;
begin
Result := FFirstNode;
end;
function TLinkedList.GetAutoFreeObjects: Boolean;
begin
Result := FDataHandler.AutoFreeObjects;
end;
function TLinkedList.GetDataHandler: IDataHandler;
begin
Result := FDataHandler;
end;
procedure TLinkedList.INodeRemover_RemoveNode(AContext : Pointer);
begin
RemoveNode(AContext);
end;
function TLinkedList.Insert(APointer: Pointer): PLinkedNode;
begin
Result := InsertNodeInList(NewNode(APointer));
end;
function TLinkedList.InsertShortString(const AShortString: ShortString):
PLinkedNode;
begin
Result := InsertNodeInList(NewShortStringNode(AShortString));
end;
function TLinkedList.Insert(const AInt64: Int64): PLinkedNode;
begin
Result := InsertNodeInList(NewNode(AInt64));
end;
function TLinkedList.Insert(const AExtended: Extended): PLinkedNode;
begin
Result := InsertNodeInList(NewNode(AExtended));
end;
function TLinkedList.Insert(const ACurrency: Currency): PLinkedNode;
begin
Result := InsertNodeInList(NewNode(ACurrency));
end;
function TLinkedList.Insert(ABoolean: Boolean): PLinkedNode;
begin
Result := InsertNodeInList(NewNode(ABoolean));
end;
function TLinkedList.Insert(AAnsiChar: AnsiChar): PLinkedNode;
begin
Result := InsertNodeInList(NewNode(AAnsiChar));
end;
function TLinkedList.Insert(AWideChar: WideChar): PLinkedNode;
begin
Result := InsertNodeInList(NewNode(AWideChar));
end;
function TLinkedList.InsertWideString(const AWideString: WideString):
PLinkedNode;
begin
Result := InsertNodeInList(NewWideStringNode(AWideString));
end;
function TLinkedList.Insert(AInterface: IUnknown): PLinkedNode;
begin
Result := InsertNodeInList(NewNode(AInterface));
end;
function TLinkedList.Insert(AObject: TObject): PLinkedNode;
begin
Result := InsertNodeInList(NewNode(AObject));
end;
procedure TLinkedList.InternalRemoveNode(ANode: PLinkedNode);
begin
if FEventSubscriber <> nil then
FEventSubscriber.ItemRemoved(ANode);
if FFirstNode = ANode then
FFirstNode := ANode.Next;
if FLastNode = ANode then
FLastNode := ANode.Prev;
if ANode.Prev <> nil then
ANode.Prev.Next := ANode.Next;
if ANode.Next <> nil then
ANode.Next.Prev := ANode.Prev;
end;
function TLinkedList.LastNode: PLinkedNode;
begin
Result := FLastNode;
end;
procedure TLinkedList.MoveToEndOfList(ANode : PLinkedNode);
begin
if FLastNode = nil then
raise Exception.Create ('Last node can''t be nil');
if ANode <> FLastNode then
begin
InternalRemoveNode (ANode);
ANode.Prev := FLastNode;
FLastNode.Next := ANode;
ANode.Next := nil;
FLastNode := ANode;
end;
end;
procedure TLinkedList.MoveToStartOfList(ANode : PLinkedNode);
begin
if FFirstNode = nil then
raise Exception.Create ('First node can''t be nil');
if ANode <> FFirstNode then
begin
InternalRemoveNode (ANode);
ANode.Next := FFirstNode;
ANode.Prev := nil;
FFirstNode.Prev := ANode;
FFirstNode := ANode;
end;
end;
function TLinkedList.NewNode(AInteger: Integer): PLinkedNode;
begin
Result := AllocNode;
FDataHandler.SetInteger(Result, AInteger);
end;
function TLinkedList.NewShortStringNode(const AShortString: ShortString):
PLinkedNode;
begin
Result := AllocNode;
FDataHandler.SetShortString(Result, AShortString);
end;
function TLinkedList.NewNode(AInt64: Int64): PLinkedNode;
begin
Result := AllocNode;
FDataHandler.SetInt64(Result, AInt64);
end;
function TLinkedList.NewNode(AExtended: Extended): PLinkedNode;
begin
Result := AllocNode;
FDataHandler.SetExtended(Result, AExtended);
end;
function TLinkedList.NewNode(const ACurrency: Currency): PLinkedNode;
begin
Result := AllocNode;
FDataHandler.SetCurrency(Result, ACurrency);
end;
function TLinkedList.NewNode(ABoolean: Boolean): PLinkedNode;
begin
Result := AllocNode;
FDataHandler.SetBoolean(Result, ABoolean);
end;
function TLinkedList.NewNode(AAnsiChar: AnsiChar): PLinkedNode;
begin
Result := AllocNode;
FDataHandler.SetAnsiChar(Result, AAnsiChar);
end;
function TLinkedList.NewNode(AWideChar: WideChar): PLinkedNode;
begin
Result := AllocNode;
FDataHandler.SetWideChar(Result, AWideChar);
end;
function TLinkedList.NewWideStringNode(const AWideString: WideString):
PLinkedNode;
begin
Result := AllocNode;
FDataHandler.SetWideString(Result, AWideString);
end;
function TLinkedList.NewNode(AInterface: IUnknown): PLinkedNode;
begin
Result := AllocNode;
FDataHandler.SetInterface(Result, AInterface);
end;
function TLinkedList.NewNode(AObject: TObject): PLinkedNode;
begin
Result := AllocNode;
FDataHandler.SetObject(Result, AObject);
end;
function TLinkedList.Prev(AContext : Pointer): Pointer;
begin
if AContext = nil then
raise EContainer.Create (SAContextNilCallingTLinkedListPre);
Result := PLinkedNode (AContext).Prev;
end;
function TLinkedList.Remove(AAnsiChar: AnsiChar): Boolean;
begin
Result := False;
with TIterator.CreateIterator(Self) do
while IterateForward do
if FDataHandler.Compare (Context, AAnsiChar) = 0
then
begin
RemoveNode (Context);
Result := True;
exit;
end;
end;
function TLinkedList.RemoveAnsiString(const AAnsiString: AnsiString): Boolean;
begin
Result := False;
with TIterator.CreateIterator(Self) do
while IterateForward do
if FDataHandler.CompareAnsiString (Context, AAnsiString) = 0
then
begin
RemoveNode (Context);
Result := True;
exit;
end;
end;
function TLinkedList.Remove(ABoolean: Boolean): Boolean;
begin
Result := False;
with TIterator.CreateIterator(Self) do
while IterateForward do
if FDataHandler.Compare (Context, ABoolean) = 0
then
begin
RemoveNode (Context);
Result := True;
exit;
end;
end;
function TLinkedList.Remove(const ACurrency : Currency): Boolean;
begin
Result := False;
with TIterator.CreateIterator(Self) do
while IterateForward do
if FDataHandler.Compare (Context, ACurrency) = 0
then
begin
RemoveNode (Context);
Result := True;
exit;
end;
end;
function TLinkedList.Remove(const AExtended : Extended): Boolean;
begin
Result := False;
with TIterator.CreateIterator(Self) do
while IterateForward do
if FDataHandler.Compare (Context, AExtended) = 0
then
begin
RemoveNode (Context);
Result := True;
exit;
end;
end;
function TLinkedList.Remove(AInteger: Integer): Boolean;
begin
Result := False;
with TIterator.CreateIterator(Self) do
while IterateForward do
if FDataHandler.Compare (Context, AInteger) = 0
then
begin
RemoveNode (Context);
Result := True;
exit;
end;
end;
function TLinkedList.Remove(const AInt64: Int64): Boolean;
begin
Result := False;
with TIterator.CreateIterator(Self) do
while IterateForward do
if FDataHandler.Compare (Context, AInt64) = 0
then
begin
RemoveNode (Context);
Result := True;
exit;
end;
end;
function TLinkedList.Remove(APointer: Pointer): Boolean;
begin
Result := False;
with TIterator.CreateIterator(Self) do
while IterateForward do
if FDataHandler.Compare (Context, APointer) = 0
then
begin
RemoveNode (Context);
Result := True;
exit;
end;
end;
function TLinkedList.RemoveShortString(const AShortString: ShortString): Boolean;
begin
Result := False;
with TIterator.CreateIterator(Self) do
while IterateForward do
if FDataHandler.CompareShortString (Context, AShortString) = 0
then
begin
RemoveNode (Context);
Result := True;
exit;
end;
end;
function TLinkedList.Remove(AInterface: IUnknown): Boolean;
begin
Result := False;
with TIterator.CreateIterator(Self) do
while IterateForward do
if FDataHandler.Compare (Context, AInterface) = 0
then
begin
RemoveNode (Context);
Result := True;
exit;
end;
end;
function TLinkedList.Remove(AObject: TObject): Boolean;
begin
Result := False;
with TIterator.CreateIterator(Self) do
while IterateForward do
if FDataHandler.Compare (Context, AObject) = 0
then
begin
RemoveNode (Context);
Result := True;
exit;
end;
end;
function TLinkedList.RemoveWideString(const AWideString: WideString): Boolean;
begin
Result := False;
with TIterator.CreateIterator(Self) do
while IterateForward do
if FDataHandler.CompareWideString (Context, AWideString) = 0
then
begin
RemoveNode (Context);
Result := True;
exit;
end;
end;
function TLinkedList.Remove(AWideChar: WideChar): Boolean;
begin
Result := False;
with TIterator.CreateIterator(Self) do
while IterateForward do
if FDataHandler.Compare (Context, AWideChar) = 0
then
begin
RemoveNode (Context);
Result := True;
exit;
end;
end;
procedure TLinkedList.RemoveNode(ANode: PLinkedNode);
begin
InternalRemoveNode (ANode);
DeallocNode (ANode);
end;
procedure TLinkedList.SetAutoFreeObjects(Value: Boolean);
begin
FDataHandler.AutoFreeObjects := Value;
end;
procedure TLinkedList.SetIterableEventSubscriber(ASubscriber :
IIterableEventSubscriber);
begin
FEventSubscriber := ASubscriber;
end;
function TLinkedList.TailableIteratorSupported: Boolean;
begin
Result := True;
end;
initialization
LinkedListFactory.PushImplementorClass(TLinkedList);
finalization
LinkedListFactory.RemoveImplementor(TLinkedList);
end.