-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadtcont_impl.i
598 lines (492 loc) · 14.6 KB
/
adtcont_impl.i
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
{@discard
This file is a part of the PascalAdt library, which provides commonly
used algorithms and data structures for the FPC and Delphi compilers.
Copyright (C) 2004, 2005 by Lukasz Czajka
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA }
{@discard
adtcont_impl.inc::prefix=&_mcp_prefix&::item_type=&ItemType&
}
&include adtcont.defs
&include adtcont_impl.mcp
type
TSortedSetPriorityQueueInterface = class (TPriorityQueueAdt)
private
FSet : TSortedSetAdt;
owns : Boolean;
public
{ aowns indicates whether aset is owned by the object or not }
constructor Create(aset : TSortedSetAdt; aowns : Boolean);
destructor Destroy; override;
function CopySelf(const ItemCopier :
IUnaryFunctor) : TContainerAdt; override;
procedure Insert(aitem : ItemType); override;
function First : ItemType; override;
function ExtractFirst : ItemType; override;
procedure Merge(aqueue : TPriorityQueueAdt); override;
{ clears the container - removes all items; @complexity O(n). }
procedure Clear; override;
{ returns true if container is empty; equivalent to Size = 0,
but may be faster; @complexity guaranteed worst-case O(1). }
function Empty : Boolean; override;
{ returns the number of items; @complexity guaranteed average,
amortized or worst-case O(1) and never more than worst-case
O(n). }
function Size : SizeType; override;
end;
{ ----------------------- TDefinedOrderContainerAdt --------------------------- }
function TDefinedOrderContainerAdt.IsDefinedOrder : Boolean;
begin
Result := true;
end;
{ -------------------------- TPriorityQueueAdt -------------------------------- }
constructor TPriorityQueueAdt.Create;
begin
inherited Create;
FComparer := &_mcp_comparer(&ItemType);
end;
constructor TPriorityQueueAdt.CreateCopy(const cont : TPriorityQueueAdt);
begin
inherited CreateCopy(TContainerAdt(cont));
Fcomparer := cont.ItemComparer;
end;
procedure TPriorityQueueAdt.DeleteFirst;
var
aitem : ItemType;
begin
aitem := ExtractFirst;
DisposeItem(aitem);
end;
function TPriorityQueueAdt.InsertItem(aitem : ItemType) : Boolean;
begin
Insert(aitem);
Result := true;
end;
function TPriorityQueueAdt.ExtractItem : ItemType;
begin
Assert(CanExtract);
Result := ExtractFirst;
end;
function TPriorityQueueAdt.CanExtract : Boolean;
begin
Result := not Empty;
end;
function TPriorityQueueAdt.IsDefinedOrder : Boolean;
begin
Result := true;
end;
{ -------------------------- TBasicTreeAdt -------------------------------- }
function TBasicTreeAdt.InsertItem(aitem : ItemType) : Boolean;
begin
InsertAsRoot(aitem);
Result := true;
end;
function TBasicTreeAdt.ExtractItem : ItemType;
var
iter : TPostOrderIterator;
begin
Assert(CanExtract);
iter := PostOrderIterator;
Result := iter.Item;
DeleteSubTree(iter.TreeIterator);
iter.Destroy;
end;
function TBasicTreeAdt.CanExtract : Boolean;
begin
Result := not Empty;
end;
{ -------------------------- TQueueAdt -------------------------------- }
function TQueueAdt.InsertItem(aitem : ItemType) : Boolean;
begin
PushBack(aitem);
Result := true;
end;
function TQueueAdt.ExtractItem : ItemType;
begin
Assert(CanExtract);
Result := Front;
PopFront;
end;
function TQueueAdt.CanExtract : Boolean;
begin
Result := not Empty;
end;
function TQueueAdt.IsDefinedOrder : Boolean;
begin
Result := false;
end;
{ ------------------------------- TSetAdt ------------------------------------- }
class function TSetAdt.NeedsHasher : Boolean;
begin
Result := false;
end;
procedure TSetAdt.SetRepeatedItems(b : Boolean);
begin
Assert(b or not RepeatedItems or Empty, msgChangingRepeatedItemsInNonEmptyContainer);
FRepeatedItems := b;
end;
procedure TSetAdt.SetComparer(comp : IBinaryComparer);
begin
Assert(Empty);
Fcomparer := comp;
end;
constructor TSetAdt.Create;
begin
inherited Create;
FComparer := &_mcp_comparer(&ItemType);
FRepeatedItems := false;
end;
constructor TSetAdt.CreateCopy(const cont : TSetAdt);
begin
inherited CreateCopy(TContainerAdt(cont));
FComparer := cont.FComparer;
FRepeatedItems := cont.FRepeatedItems;
end;
procedure TSetAdt.BasicSwap(cont : TContainerAdt);
begin
inherited;
if cont is TSetAdt then
begin
ExchangePtr(FComparer, TSetAdt(cont).FComparer);
ExchangeData(FRepeatedItems, TSetAdt(cont).FRepeatedItems,
SizeOf(Boolean));
end;
end;
function TSetAdt.Extract(aitem : ItemType) : SizeType;
var
owns : Boolean;
begin
owns := OwnsItems;
try
OwnsItems := false;
Result := Delete(aitem);
finally
OwnsItems := owns;
end;
end;
function TSetAdt.InsertItem(aitem : ItemType) : Boolean;
begin
Result := Insert(aitem);
end;
function TSetAdt.ExtractItem : ItemType;
var
iter : TSetIterator;
begin
Assert(CanExtract);
iter := Start;
Assert(not iter.IsFinish);
Result := iter.Item;
iter.Delete;
iter.Destroy;
end;
function TSetAdt.CanExtract : Boolean;
begin
Result := not Empty;
end;
{ ------------------------- TSortedSetAdt ------------------------------ }
function TSortedSetAdt.First : ItemType;
var
iter : TBidirectionalIterator;
begin
iter := Start;
Result := iter.Item;
iter.Destroy;
end;
function TSortedSetAdt.ExtractFirst : ItemType;
var
iter : TBidirectionalIterator;
begin
iter := Start;
Result := iter.Extract;
iter.Destroy;
end;
procedure TSortedSetAdt.DeleteFirst;
var
aitem : ItemType;
begin
aitem := ExtractFirst;
DisposeItem(aitem);
end;
function TSortedSetAdt.PriorityQueueInterface : TPriorityQueueAdt;
begin
Result := TSortedSetPriorityQueueInterface.Create(self, false);
end;
{ -------------------------- THashSetAdt ------------------------------- }
class function THashSetAdt.NeedsHasher : Boolean;
begin
Result := true;
end;
constructor THashSetAdt.Create;
begin
inherited Create;
FHasher := &_mcp_hasher(&ItemType);
FAutoShrink := false;
end;
constructor THashSetAdt.CreateCopy(const cont : THashSetAdt);
begin
inherited CreateCopy(TSetAdt(cont));
FHasher := cont.FHasher;
end;
procedure THashSetAdt.BasicSwap(cont : TContainerAdt);
begin
inherited;
if cont is THashSetAdt then
begin
ExchangePtr(FHasher, THashSetAdt(cont).FHasher);
ExchangeData(FAutoShrink, THashSetAdt(cont).FAutoShrink,
SizeOf(Boolean));
end;
end;
procedure THashSetAdt.SetCapacity(cap : SizeType);
begin
if cap > GetCapacity then
begin
if GetCapacity <> 0 then
Rehash(FloorLog2(cap) - FloorLog2(GetCapacity))
else
Rehash(FLoorLog2(cap));
end;
end;
{$ifdef TEST_PASCAL_ADT }
procedure THashSetAdt.LogStatus(mname : String);
begin
inherited;
WriteLog('Max fill ratio: ' + IntToStr(MaxFillRatio) + '%');
WriteLog('Current fill ratio: ' + IntToStr((Size * 100) div Capacity) + '%');
end;
{$endif TEST_PASCAL_ADT }
{ ---------------------------- TListAdt ---------------------------------- }
{$ifdef DEBUG_PASCAL_ADT }
constructor TListAdt.Create;
begin
inherited Create;
FSizeCanRecalc := true;
end;
{$endif DEBUG_PASCAL_ADT }
procedure TListAdt.Move(SourceStart, SourceFinish, Dest : TForwardIterator);
begin
adtalgs.Move(SourceStart, SourceFinish, Dest)
end;
procedure TListAdt.Move(Source, Dest : TForwardIterator);
begin
adtalgs.Move(Source, Next(Source), Dest);
end;
{ ---------------------------- TDoubleListAdt ---------------------------------- }
function TDoubleListAdt.BidirectionalStart : TBidirectionalIterator;
begin
Result := TBidirectionalIterator(ForwardStart);
end;
function TDoubleListAdt.BidirectionalFinish : TBidirectionalIterator;
begin
Result := TBidirectionalIterator(ForwardFinish);
end;
{ ------------------------- TRandomAccessContainerAdt -------------------------- }
function TRandomAccessContainerAdt.ForwardStart : TForwardIterator;
begin
Result := RandomAccessStart;
end;
function TRandomAccessContainerAdt.ForwardFinish : TForwardIterator;
begin
Result := RandomAccessFinish;
end;
procedure TRandomAccessContainerAdt.Insert(iter : TForwardIterator;
aitem : ItemType);
begin
Assert(iter is TRandomAccessIterator, msgInvalidIterator);
Insert(TRandomAccessIterator(iter).Index, aitem);
end;
procedure TRandomAccessContainerAdt.Delete(iter : TForwardIterator);
begin
Assert(iter is TRandomAccessIterator, msgInvalidIterator);
Delete(TRandomAccessIterator(iter).Index);
end;
function TRandomAccessContainerAdt.Delete(start,
finish : TForwardIterator) : SizeType;
var
ind1 : IndexType;
begin
Assert(start is TRandomAccessIterator, msgInvalidIterator);
Assert(finish is TRandomAccessIterator, msgInvalidIterator);
ind1 := TRandomAccessIterator(start).Index;
Result := Delete(ind1, TRandomAccessIterator(finish).Index - ind1);
end;
function TRandomAccessContainerAdt.Extract(iter : TForwardIterator) : ItemType;
begin
Assert(iter is TRandomAccessIterator, msgInvalidIterator);
Result := Extract(TRandomAccessIterator(iter).Index);
end;
function TRandomAccessContainerAdt.LowIndex : IndexType;
begin
Result := 0;
end;
function TRandomAccessContainerAdt.HighIndex : IndexType;
begin
Result := Size - 1;
end;
{ ---------------------- TRandomAccessContainerIterator ------------------------ }
constructor TRandomAccessContainerIterator.
Create(ind : IndexType; const Cont : TRandomAccessContainerAdt);
begin
inherited Create(cont);
FIndex := ind;
FCont := cont;
end;
{$ifdef OVERLOAD_DIRECTIVE }
procedure TRandomAccessContainerIterator.Advance;
{$else }
procedure TRandomAccessContainerIterator.AdvanceOnePosition;
{$endif OVERLOAD_DIRECTIVE }
begin
Inc(FIndex);
end;
procedure TRandomAccessContainerIterator.Advance(i : IndexType);
begin
Inc(FIndex, i);
end;
procedure TRandomAccessContainerIterator.Retreat;
begin
Dec(FIndex);
end;
function TRandomAccessContainerIterator.Distance(const Pos : TRandomAccessIterator)
: IndexType;
begin
Assert(pos is TRandomAccessContainerIterator, msgInvalidIterator);
Assert(pos.Owner = Owner, msgWrongOwner);
Result := TRandomAccessContainerIterator(pos).FIndex - FIndex;
end;
function TRandomAccessContainerIterator.Equal(const Pos : TIterator) : Boolean;
begin
Assert(pos is TRandomAccessContainerIterator, msgInvalidIterator);
Result := (TRandomAccessContainerIterator(pos).FIndex = FIndex)
and (TRandomAccessContainerIterator(pos).FCont = FCont);
end;
function TRandomAccessContainerIterator.Less(const Pos : TRandomAccessIterator)
: Boolean;
begin
Assert(pos is TRandomAccessContainerIterator, msgInvalidIterator);
Assert(pos.Owner = Owner, msgWrongOwner);
Result := FIndex < TRandomAccessContainerIterator(pos).FIndex;
end;
function TRandomAccessContainerIterator.GetItem : ItemType;
begin
Result := FCont.GetItem(FIndex);
end;
procedure TRandomAccessContainerIterator.SetItem(Aitem : ItemType);
begin
FCont.SetItem(FIndex, aitem);
end;
function TRandomAccessContainerIterator.GetItemAt(i : IndexType) : ItemType;
begin
Result := FCont.GetItem(FIndex + i);
end;
procedure TRandomAccessContainerIterator.SetItemAt(i : IndexType;
aitem : ItemType);
begin
FCont.SetItem(FIndex + i, aitem);
end;
procedure TRandomAccessContainerIterator.ExchangeItem(iter : TIterator);
begin
if iter.Owner = FCont then
begin
Assert((iter is TRandomAccessContainerIterator), msgInvalidIterator);
{ ExchangeItemsAt works with indicies relative to self.Index }
ExchangeItemsAt(0, TRandomAccessContainerIterator(iter).FIndex - FIndex);
end else
DoExchangeItem(iter);
end;
procedure TRandomAccessContainerIterator.Insert(Aitem : ItemType);
begin
FCont.Insert(FIndex, aitem);
end;
function TRandomAccessContainerIterator.Extract : ItemType;
begin
Result := FCont.Extract(FIndex);
end;
function TRandomAccessContainerIterator.Delete(n : SizeType) : SizeType;
begin
Result := FCont.Delete(FIndex, n);
end;
function TRandomAccessContainerIterator.Index : IndexType;
begin
Result := FIndex;
end;
function TRandomAccessContainerIterator.Owner : TContainerAdt;
begin
Result := FCont;
end;
function TRandomAccessContainerIterator.IsStart : Boolean;
begin
Result := (FIndex = FCont.LowIndex);
end;
function TRandomAccessContainerIterator.IsFinish : Boolean;
begin
Result := (FIndex = FCont.HighIndex + 1);
end;
{ -------------------- TSortedSetPriorityQueueInterface --------------------- }
constructor TSortedSetPriorityQueueInterface.Create(aset : TSortedSetAdt;
aowns : Boolean);
begin
FSet := aset;
owns := aowns;
end;
destructor TSortedSetPriorityQueueInterface.Destroy;
begin
if owns then
FSet.Free;
inherited;
end;
function TSortedSetPriorityQueueInterface.
CopySelf(const ItemCopier : IUnaryFunctor) : TContainerAdt;
begin
Result := TSortedSetPriorityQueueInterface.
Create(TSortedSetAdt(FSet.CopySelf(ItemCopier)), true);
end;
procedure TSortedSetPriorityQueueInterface.Insert(aitem : ItemType);
begin
FSet.Insert(aitem);
end;
function TSortedSetPriorityQueueInterface.First : ItemType;
begin
Result := FSet.First;
end;
function TSortedSetPriorityQueueInterface.ExtractFirst : ItemType;
begin
Result := FSet.ExtractFirst;
end;
procedure TSortedSetPriorityQueueInterface.Merge(aqueue : TPriorityQueueAdt);
begin
Assert(aqueue is TSortedSetPriorityQueueInterface);
while not aqueue.Empty do
begin
Insert(aqueue.ExtractFirst);
end;
end;
procedure TSortedSetPriorityQueueInterface.Clear;
begin
FSet.Clear;
end;
function TSortedSetPriorityQueueInterface.Empty : Boolean;
begin
Result := FSet.Empty;
end;
function TSortedSetPriorityQueueInterface.Size : SizeType;
begin
Result := FSet.Size;
end;
{$ifdef OVERLOAD_DIRECTIVE }
function CopyOf(const iter : TRandomAccessContainerIterator) :
TRandomAccessContainerIterator;
begin
Result := TRandomAccessContainerIterator(iter.CopySelf);
end;
{$endif OVERLOAD_DIRECTIVE }