-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect.pas
604 lines (512 loc) · 17.2 KB
/
collect.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
UNIT Collect;
{ Collection classes for Delphi
compatible with TPW<=7.0/Delphi1 OWL TCollection
except for firstthat, foreach not implemented... (now can be just as fast with fcnt
Alin Flaider, 1996 aflaidar@datalog.ro.
1997 EDJ. made thread safe. Use ifdef threaded to enable thread safety for threaded applications
2004 EDJ. Made more memory efficient. 24-28 byte overhead reduced to 12 byte.
Compiles under D5..XE6.
Can use QStrings under D5..D7 for optimized string compare
}
{ Xistext }
INTERFACE
//{$ifdef lowdebug}{$D+}{$O-}{$else}{$O+}{$D-}{$endif}
uses Classes, Sysutils, nobjects;
const
coIndexError = -1; { Index out of range }
coOverflow = -2; { Overflow }
coUnderflow = -3; { Underflow }
coWaitTimeout= -4; { timeout while waiting for thread }
CONST { Tdcollection Bits }
// CB_Sorted = 1; no longer used, available
CB_OwnItems = 2;
CB_HaveObjs = 4;
CB_Duplicates=8; {used by sorted collection
if true, rejects item whose key already exists}
{override this method to specify relation bewtween two keys
1 if Key1 comes after Key2, -1 if Key1 comes before Key2,
0 if Key1 is equivalent to Key2}
(* not used yet CB_FixedChildType=16; { used to know if a stream list needs to store the streamid
for each child or just once for the whole list }*)
{ 16,32,64,128 unused }
TYPE TCollection = CLASS;
CollException = class(Exception);
TInsertProc = PROCEDURE( List : TCollection; Item : Pointer );
TCollectionInfo = PACKED RECORD { 64 bits; 8 bytes }
Bits : BYTE; { Collection Bits defined above }
Delta : WORD; { Number of items by which the collection grows when full }
Reserved : BYTE;
Limit : INTEGER; { Current Allocated size of array }
END;
PPointerList = ^TPointerList;
TPointerList = array[0..Maxint div 16 - 1] of Pointer;
TCollection = CLASS( TdObject )
public
Count : INTEGER; { Current Number of Items, 8 bytes }
It : PPointerList; { array of pointers }
public
constructor Create; OVERRIDE;
constructor init( aLimit, aDelta : INTEGER );
destructor Destroy; override; {before deallocating object it disposes all items and the storage array}
function At( Index : INTEGER ) : Pointer; { return item at index position }
procedure AtPut( Index : INTEGER; Item : Pointer); { replace item at index position}
procedure AtInsert( Index : INTEGER; Item : Pointer); {inserts Item at specified position }
procedure AtFree(Index: INTEGER); {deletes and disposes Item at specified position}
procedure FreeAll; { deletes and disposes all items }
procedure FreeItem (Item : Pointer); virtual;
procedure AtDelete(Index : INTEGER);
procedure Delete( Item : Pointer); {deletes Item}
procedure DeleteAll; {deletes all Items without disposing them }
procedure Pack; {packs collection by removing nil Items}
procedure Clear(Item: Pointer); {formerly Free, renamed to Clear to avoid bypassing inherited TObject.Free;
deletes and disposes Item }
function IndexOf( Item : Pointer) : Integer; virtual;{finds position of Item using a linear search}
property Items[Index: INTEGER]: pointer read At write AtPut; default; {direct access to items through position}
FUNCTION FirstPtr : POINTER; { returns a pointer to first item, for walking list }
FUNCTION FCnt( VAR FirstP ) : INTEGER; { returns a pointer to first item, for walking list }
PROCEDURE Insert (Item : Pointer);
PROTECTED
InsertProc : TInsertProc;
PROCEDURE SetLimit( aLimit : INTEGER );
PRIVATE
FInfo : TCollectionInfo; { Collection information Record }
PROCEDURE SetSorted( iSorted : BOOLEAN );
FUNCTION GetSorted : BOOLEAN;
PROCEDURE SetOwnItems( iOwnItems : BOOLEAN );
FUNCTION GetOwnItems : BOOLEAN;
PROCEDURE SetDuplicates( iDuplicates: BOOLEAN );
FUNCTION GetDuplicates : BOOLEAN;
PROCEDURE SetHaveObjs( iHaveObjs : BOOLEAN );
FUNCTION GetHaveObjs : BOOLEAN;
PROCEDURE Error( Code,Info : Integer); DYNAMIC;
FUNCTION Grow : BOOLEAN;
PUBLIC
PROPERTY Sorted : BOOLEAN READ GetSorted WRITE SetSorted;
PROPERTY OwnItems : BOOLEAN READ GetOwnItems WRITE SetOwnItems;
PROPERTY Duplicates : BOOLEAN READ GetDuplicates WRITE SetDuplicates;
PROPERTY HaveObjs : BOOLEAN READ GetHaveObjs WRITE SetHaveObjs;
PROPERTY Bits : BYTE READ FInfo.Bits WRITE FInfo.Bits;
PROPERTY Delta : WORD READ FInfo.Delta;
PROPERTY Limit : INTEGER Read FInfo.Limit;
END;
TSortedCollection = class(TCollection)
constructor Create; OVERRIDE;
constructor init( aLimit, aDelta : INTEGER );
{ Duplicates: boolean; now in Bits }
function IndexOf (Item : Pointer): Integer; override;
{finds item required position and performs insertion }
// procedure Insert (Item : Pointer); override;
{finds index of item by performing an optimised search}
function Search (key : Pointer; Var Index : integer) : Boolean;
function KeyOf (Item : Pointer): Pointer; virtual; {finds index of item by calling Search}
function Compare (Key1,Key2 : Pointer): Integer; virtual; {returns key of Item}
END;
TStrCollection = CLASS( TSortedCollection )
function Compare(Key1, Key2: Pointer): Integer; override;
procedure FreeItem(Item: Pointer); override;
END;
IMPLEMENTATION {---------------------------------------------------------------}
PROCEDURE InsertToEnd( List : TCollection; Item : Pointer );
BEGIN
WITH List DO
AtInsert( Count, Item );
END;
PROCEDURE InsertSorted( List : TCollection; Item : Pointer);
VAR I : INTEGER;
BEGIN
{$ifdef threaded}IF ThreadLock THEN{$endif} WITH TSortedCollection( List ) DO
BEGIN
IF NOT Search(KeyOf(Item), I) OR ( FInfo.Bits AND CB_Duplicates > 0 ) THEN
AtInsert(I, Item);
{$ifdef threaded}ThreadRelease;{$endif}
END
END;
constructor TCollection.Create;
begin
inherited Create;
Count:=0;
It := nil;
{$ifdef QStrings}
Q_ZeroMem( @Info, SizeOf( Info ));
{$else}
FillChar( FInfo, SizeOf( FInfo ), 0 );
{$endif}
FInfo.Delta:=10;
FInfo.Bits := cb_OwnItems OR cb_HaveObjs;
SetLimit( 1 );
InsertProc := @InsertToEnd;
end;
constructor TCollection.Init( ALimit, ADelta: INTEGER );
begin
inherited Create;
Count:=0;
It := nil;
{$ifdef QStrings}
Q_ZeroMem( @Info, SizeOf( Info ));
{$else}
FillChar( FInfo, SizeOf( FInfo ), 0 );
{$endif}
FInfo.Delta := aDelta;
FInfo.Bits := cb_OwnItems OR cb_HaveObjs;
SetLimit( ALimit );
InsertProc := @InsertToEnd;
end;
destructor TCollection.Destroy;
begin
FreeAll;
SetLimit(0);
inherited Destroy;
end;
function TCollection.At(Index: INTEGER ): Pointer;
begin
Result := NIL;
If ( Index < Count ) THEN
BEGIN
{$ifdef threaded}IF ThreadLock THEN{$endif}
BEGIN
Result := It^[Index];
{$ifdef threaded}ThreadRelease;{$endif}
END
END
end;
procedure TCollection.AtPut(Index: INTEGER; Item: Pointer);
begin
if (Index >= Count) then
Error(coIndexError,0)
else
{$ifdef threaded}IF ThreadLock THEN{$endif}
BEGIN
It^[Index] := Item;
{$ifdef threaded}ThreadRelease;{$endif}
END;
end;
procedure TCollection.AtDelete(Index: INTEGER);
{ removes an index from the collection }
begin
if (Index >= Count) then
Error(coIndexError,0)
else
begin
{$ifdef threaded}IF ThreadLock THEN BEGIN{$endif}
if Index < pred(Count) then
{$ifdef QStrings}
Q_MoveMem( @It^[succ(Index)], @It^[Index], (count-index)*sizeof(pointer));
{$else}
Move( It^[succ(Index)], It^[Index], (count-index)*sizeof(pointer));
{$endif}
Dec(Count);
{$ifdef threaded}ThreadRelease;END{$endif}
end
end;
FUNCTION TCollection.Grow : BOOLEAN;
BEGIN
WITH FInfo DO
BEGIN
Result := Count < Limit; { return true if we don't need to grow }
IF NOT Result THEN
BEGIN
Result := Delta > 0; { return false if we need to grow but can't }
IF Result THEn
SetLimit( Limit + Delta ); { return true if we grew successfully }
END;
END;
END;
procedure TCollection.AtInsert( Index: INTEGER; Item: pointer);
begin
if ( Index > Count ) then
Index := Count;
IF NOT Grow THEN
BEGIN
Error(coOverFlow,0);
exit;
end;
{$ifdef threaded}IF ThreadLock THEN{$endif}
BEGIN
If Index <> Count then {move compensates for overlaps}
{$ifdef QStrings}
Q_MoveMem( @It^[Index], @It^[Index+1], (count - index)*sizeof(pointer));
{$else}
Move( It^[Index], It^[Index+1], (count - index)*sizeof(pointer));
{$endif}
It^[Index] := Item;
Inc(Count);
{$ifdef threaded}ThreadRelease;{$endif}
END
end;
procedure TCollection.Delete( Item: pointer);
VAR Ix : INTEGER;
begin
Ix := IndexOf( Item );
IF Ix > -1 THEN
AtDelete( Ix );
end;
procedure TCollection.DeleteAll;
begin
Count:=0
end;
FUNCTION TCollection.GetSorted : BOOLEAN;
BEGIN
// Result := Info.Bits AND CB_Sorted > 0;
Result := ({$ifndef FPC}@{$endif}InsertProc = @InsertSorted)
END;
PROCEDURE TCollection.SetSorted( iSorted : BOOLEAN );
{ VAR IsSorted : BOOLEAN;}
BEGIN
(* IsSorted := Info.Bits AND CB_Sorted > 0;
IF IsSorted XOR iSorted THEN { if new setting is different }
BEGIN
IF iSorted THEN
BEGIN
Info.Bits := Info.Bits OR CB_Sorted;{ turn on }
InsertProc := InsertSorted
END
ELSE
BEGIN
Info.Bits := Info.Bits XOR CB_Sorted; { turn off }
InsertProc := InsertToEnd
END
END;*)
IF ISorted THEN
InsertProc := @InsertSorted
ELSE
InsertProc := @InsertToEnd;
END;
FUNCTION TCollection.GetOwnItems : BOOLEAN;
BEGIN
Result := FInfo.Bits AND CB_OwnItems > 0;
END;
PROCEDURE TCollection.SetOwnItems( iOwnItems : BOOLEAN );
VAR DoOwnItems : BOOLEAN;
BEGIN
DoOwnItems := FInfo.Bits AND CB_OwnItems > 0;
IF DoOwnItems XOR iOwnItems THEN { if new setting is different }
BEGIN
IF iOwnItems THEN
FInfo.Bits := FInfo.Bits OR CB_OwnItems { turn on }
ELSE
FInfo.Bits := FInfo.Bits XOR CB_OwnItems; { turn off }
END;
END;
FUNCTION TCollection.GetHaveObjs : BOOLEAN;
BEGIN
Result := FInfo.Bits AND CB_HaveObjs > 0;
END;
PROCEDURE TCollection.SetHaveObjs( iHaveObjs : BOOLEAN );
VAR DoHaveObjs : BOOLEAN;
BEGIN
DoHaveObjs:= FInfo.Bits AND CB_HaveObjs > 0;
IF DoHaveObjs XOR iHaveObjs THEN { if new setting is different }
BEGIN
IF iHaveObjs THEN
FInfo.Bits := FInfo.Bits OR CB_HaveObjs { turn on }
ELSE
FInfo.Bits := FInfo.Bits XOR CB_HaveObjs ; { turn off }
END;
END;
FUNCTION TCollection.GetDuplicates : BOOLEAN;
BEGIN
Result := FInfo.Bits AND CB_Duplicates > 0;
END;
PROCEDURE TCollection.SetDuplicates( iDuplicates : BOOLEAN );
VAR HasDuplicates : BOOLEAN;
BEGIN
HasDuplicates := FInfo.Bits AND CB_Duplicates > 0;
IF HasDuplicates XOR iDuplicates THEN { if new setting is different }
BEGIN
IF iDuplicates THEN
FInfo.Bits := FInfo.Bits OR CB_Duplicates { turn on }
ELSE
FInfo.Bits := FInfo.Bits XOR CB_Duplicates; { turn off }
END;
END;
procedure TCollection.Error(Code, Info: Integer);
begin
case Code of
coIndexError: raise CollException.Create(ClassName+' error; bad index: '+IntToStr(Info));
coOverflow: raise CollException.Create(ClassName+' overflow - can''t grow!');
coUnderflow: raise CollException.Create(ClassName+' underflow - can''t shrink!');
end
end;
procedure TCollection.Clear(Item: Pointer);
begin
Delete(Item);
FreeItem(Item);
end;
procedure TCollection.FreeAll;
var Item : ^TObject;
I : INTEGER;
begin
{$ifdef threaded}IF ThreadLock THEN BEGIN{$endif}
FOR I := FCnt( Item ) DOWNTO 0 DO
BEGIN
FreeItem( Item^ );
INC( Item );
END;
Count := 0;
{$ifdef threaded}ThreadRelease; END{$endif}
end;
procedure TCollection.FreeItem(Item: Pointer);
begin
if ASSIGNED( Item ) AND OwnItems THEN
TObject(Item).Free;
end;
function TCollection.IndexOf(Item: Pointer): integer;
var i : integer;
It1 : ^TObject;
begin
Result := -1;
{$ifdef threaded}IF ThreadLock THEN{$endif}
BEGIN
for i := 0 TO FCnt( It1 ) DO
BEGIN
if Item = Pointer(It1^) then
begin
Result := i;
break
end;
INC( It1 );
END;
{$ifdef threaded}ThreadRelease;{$endif}
END
end;
procedure TCollection.Pack;
var i: integer;
begin
{$ifdef threaded}IF ThreadLock THEN{$endif}
BEGIN
for i := pred(count) downto 0 do if It^[i] = nil then AtDelete(i);
{$ifdef threaded}ThreadRelease;{$endif}
END
end;
procedure TCollection.SetLimit( ALimit: INTEGER );
begin
if (ALimit < Count) then
Error( coUnderFlow , 0)
ELSE
WITH FInfo DO IF ( ALimit <> Limit ) THEN
BEGIN
{$ifdef threaded}IF ThreadLock THEN BEGIN{$endif}
{ reallocate the memory, compiler platform specific }
ReallocMem( It, ALimit* SizeOf(Pointer));
Limit := ALimit;
{$ifdef threaded}ThreadRelease; END{$endif}
END;
end;
FUNCTION TCollection.FirstPtr : POINTER;
{ returns a pointer to first item, for walking list.
This has been replaced by the more versatile FCnt }
BEGIN
Result := NIL;
IF Count > 0 THEN
Result := @It^
END;
FUNCTION TCollection.FCnt( VAR FirstP { untyped pointer} ) : INTEGER;
{ Used for walking list in a FOR loop ....
FOR I := TheList.FCnt( APtr ) DOWNTO 0 DO
BEGIN
APtr^.Blah; do something with the item.
INC( APtr );
END; }
BEGIN
Result := Count - 1;
IF Result >= 0 THEN
POINTER( FirstP ) := @It^; { return pointer to the first item in list }
END;
PROCEDURE TCollection.Insert (Item : Pointer);
begin
insertproc( self, item );
end;
{-------------------------------------}
constructor TSortedCollection.Create;
BEGIN
INHERITED Create;
InsertPRoc := @InsertSorted;
END;
constructor TSortedCollection.init( aLimit, aDelta : INTEGER );
BEGIN
INHERITED Init( aLimit, aDelta );
InsertProc := @InsertSorted;
END;
function TSortedCollection.Compare( Key1,Key2 : Pointer): Integer;
begin
Result := 0;
end;
function TSortedCollection.IndexOf(Item: Pointer): Integer;
var i: Integer;
begin
Result := -1;
if Search(KeyOf(Item), i) then { use keyof on item to short circuit sorted search }
begin
if ( FInfo.Bits AND CB_Duplicates > 0 ) then
while (i < Count) and (Item <> It^[I]) do
Inc(i);
if i < Count then
Result := i;
end
else
Result := INHERITED IndexOf( Item ); { if not found by key, brute force search for pointer }
end;
(*
procedure TSortedCollection.Insert(Item: Pointer);
var i : integer;
begin
{$ifdef threaded}IF ThreadLock THEN{$endif}
BEGIN
if not Search(KeyOf(Item), I) or ( Info.Bits AND CB_Duplicates > 0 ) then
AtInsert(I, Item);
{$ifdef threaded}ThreadRelease;{$endif}
END
end;
*)
function TSortedCollection.KeyOf(Item: Pointer): Pointer;
begin
Result := Item;
end;
function TSortedCollection.Search(key : Pointer; Var Index : integer) : Boolean;
var L, H, I, C: Integer;
Dups : BOOLEAN;
begin
Result := False;
{$ifdef threaded}IF ThreadLock THEN BEGIN {$endif}
Dups := FInfo.Bits AND CB_Duplicates > 1;
L := 0;
H := Count - 1;
while L <= H do
begin
I := (L + H) shr 1; { div 2 }
C := Compare(KeyOf(It^[I]), Key);
if C < 0 then
L := I + 1
else
begin
H := I - 1;
Result := C = 0;
IF Result AND NOT Dups THEN
L := I;
end;
end;
Index := L;
{ !!! if duplicates, should we arrange to return the first of the duplicates ?}
{$ifdef threaded}ThreadRelease; END;{$endif}
end;
procedure TCollection.AtFree(Index: INTEGER);
var Item: Pointer;
begin
Item := At(Index);
AtDelete(Index);
FreeItem(Item);
end;
{-------------------------------------}
function TStrCollection.Compare(Key1, Key2: Pointer): Integer;
begin
{$ifdef QStrings}
Result := Q_PCompStr(Key1, Key2);
{$else}
Result := StrComp( PChar( Key1 ), PChar( Key2 ));
{$endif}
end;
procedure TStrCollection.FreeItem(Item: Pointer);
begin
StrDispose(PChar(Item));
end;
END.