-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOBJECTS.PAS
520 lines (473 loc) · 10.4 KB
/
OBJECTS.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
{$IFDEF debug}
{$A+,B-,D+,F+,G+,I+,L+,N+,P+,Q+,R+,S+,V+,X+,Y+}
{$ELSE}
{$A+,B-,D-,F+,G+,I+,L-,N+,P+,Q-,R-,S-,V+,X+,Y-}
{$ENDIF}
unit Objects;
interface
uses
Consts,
Utils;
type
PByte = ^Byte;
PBaseObject = ^TBaseObject;
TComparer = function(item1, item2: pointer): integer;
TPredicate = function(item: pointer; var value): boolean;
TBaseObject = object
private
public
constructor Create;
procedure Init; virtual;
destructor Done; virtual;
end;
PObject = ^TObject;
TObject = object(TBaseObject)
private
public
TypeName: string25;
Id: string25;
constructor Create(newId: string);
procedure Init; virtual;
function ToString: string; virtual;
end;
PList = ^TList;
TList = object(TObject)
private
_buffer: pointer;
_count: integer;
_capacity: integer;
_sorted: boolean;
_comparer: TComparer;
function GetSortedIndex(item: pointer): integer; virtual;
public
constructor CreateCapacity(newId: string; initialCapacity: integer);
constructor Create(newId: string);
constructor CreateSorted(newId: string; comparer: TComparer);
destructor Done; virtual;
procedure Init; virtual;
procedure Sort(comparer: TComparer); virtual;
function ToString: string; virtual;
function GetItem(index: integer): pointer;
function IndexOf(item: pointer): integer;
function Add(item: pointer): integer;
function Count: integer; virtual;
function Capacity: integer; virtual;
function Sorted: boolean; virtual;
function Where(predicate: TPredicate; var value): PList;
procedure SetItem(index: integer; item: pointer);
procedure SetCapacity(cap: integer);
procedure Insert(index: integer; item: pointer);
procedure Delete(index: integer);
procedure Clear; virtual;
end;
PObjectList = ^TObjectList;
TObjectList = object(TList)
private
public
DisposeObjects: boolean;
destructor Done; virtual;
procedure Init; virtual;
function ToString: string; virtual;
function GetItem(index: integer): PObject;
function IndexOf(item: PObject): integer;
function IndexOfId(idToFind: string25): integer;
function Add(item: PObject): integer;
procedure Insert(index: integer; item: PObject);
procedure Delete(index: integer);
procedure Clear; virtual;
end;
PAllocationError = ^TAllocationError;
TAllocationError = procedure(var error: TError);
PPersistent = ^TPersistent;
TPersistent = object(TObject)
public
procedure Assign(source: TPersistent); virtual;
procedure AssignTo(dest: TPersistent); virtual;
end;
var
OnAllocationError: PAllocationError;
implementation
uses
Crt;
type
PPointer = ^Pointer;
constructor TBaseObject.Create;
begin
Init;
end;
procedure TBaseObject.Init;
begin
end;
destructor TBaseObject.Done;
begin
end;
constructor TObject.Create(newId: string);
begin
Id := NewId;
Init;
end;
procedure TObject.Init;
begin
TBaseObject.Init;
TypeName := 'TObject';
end;
function TObject.ToString: string;
begin
ToString := TypeName;
end;
constructor TList.CreateCapacity(newId: string; initialCapacity: integer);
begin
Id := newId;
_capacity := 0;
SetCapacity(initialCapacity);
_sorted := false;
_comparer := nil;
Init;
end;
constructor TList.Create(newId: string);
begin
Id := newId;
_capacity := 0;
SetCapacity(DefaultListCapacity);
_sorted := false;
_comparer := nil;
Init;
end;
constructor TList.CreateSorted(newId: string; comparer: TComparer);
begin
Id := newId;
_capacity := 0;
_sorted := true;
_comparer := comparer;
SetCapacity(DefaultListCapacity);
Init;
end;
procedure TList.Init;
begin
TObject.Init;
TypeName := 'TList';
_count := 0;
end;
destructor TList.Done;
begin
if (_buffer <> nil) then FreeMem(_buffer, _capacity * SizeOf(Pointer));
TObject.Done;
end;
function TList.Sorted: boolean;
begin
Sorted := _sorted;
end;
function TList.ToString: string;
begin
ToString := 'TList';
end;
procedure TList.SetItem(index: integer; item: pointer);
var
itemLoc: pointer;
begin
itemLoc := Pointer(LongInt(_buffer) + 4 * index);
Move(itemLoc^, item, 4);
end;
function TList.GetItem(index: integer): pointer;
var
itemLoc: pointer;
result: pointer;
begin
if (index < 0) or (index > _count) then
begin
GetItem := nil;
Exit;
end;
itemLoc := Pointer(LongInt(_buffer) + 4 * index);
Move(itemLoc^, result, 4);
GetItem := result;
end;
function TList.IndexOf(item: pointer): integer;
var
index: integer;
current: pointer;
itemLoc: PPointer;
begin
IndexOf := -1;
if (_count = 0) then
Exit;
index := 0;
repeat
if (index > _count) then
break;
itemLoc := pointer(LongInt(_buffer) + 4 * index);
current := itemLoc^;
if (item = current) then
begin
IndexOf := index;
break;
end;
Inc(index);
until false;
end;
procedure TList.Sort(comparer: TComparer);
procedure QuickSort(lowIndex, highIndex: integer);
var
low, high: integer;
pivot, temp, lowItem, highItem: pointer;
begin
low := lowIndex;
high := highIndex;
lowItem := GetItem(lowIndex);
highItem := GetItem(highIndex);
pivot := GetItem((lowIndex + highIndex) div 2);
repeat
while comparer(lowItem, pivot) < 0 do
Inc(low) ;
while comparer(highItem, pivot) > 0 do
Dec(high);
if low <= high then
begin
temp := GetItem(low);
SetItem(low, GetItem(high));
SetItem(high, temp);
Inc(low) ;
Dec(high) ;
end;
until low > high;
if high > lowIndex then
QuickSort(lowIndex, high);
if low < high then
QuickSort(low, highIndex);
end;
begin
if (_count = 0) then exit;
QuickSort(0, _count - 1);
end;
function TList.GetSortedIndex(item: pointer): integer;
var
compared: integer;
upper, lower, middle: integer;
item2: pointer;
begin
GetSortedIndex := 0;
if (_count = 0) then exit;
if (_count = 1) then
begin
if (TComparer(_comparer)(item, GetItem(0)) < 0) then
GetSortedIndex := 0
else
GetSortedIndex := 1;
end;
upper := _count;
lower := 0;
repeat
middle := (upper + lower) div 2;
item2 := GetItem(middle);
compared := TComparer(_comparer)(item, item2);
if (compared < 0) then
lower := middle
else if (compared > 0) then
upper := middle
else
begin
middle := upper;
break;
end;
until (upper = lower);
GetSortedIndex := middle;
end;
function TList.Add(item: pointer): integer;
var
newCap: word;
itemLoc: PPointer;
index: integer;
begin
if (_count + 1 > _capacity) then
begin
newCap := _capacity * 2;
SetCapacity(newCap);
end;
index := _count;
if (_sorted) then
begin
index := GetSortedIndex(item);
end;
if (index = _count) then
begin
itemLoc := pointer(LongInt(_buffer) + 4 * index);
Inc(_count);
itemLoc^ := item;
end
else
Insert(index, item);
end;
procedure TList.Insert(index: integer; item: pointer);
var
newCap: integer;
itemLoc: PPointer;
locIndex: integer;
begin
if (_count + 1 > _capacity) then
begin
newCap := _capacity * 2;
SetCapacity(newCap);
end;
for locIndex := _count - 1 downto index do
begin
itemLoc := pointer(LongInt(_buffer) + locIndex * 4);
Move(itemLoc^, pointer(LongInt(itemLoc) + 4)^, 4);
end;
Inc(_count);
itemLoc^ := item;
end;
procedure TList.Delete(index: integer);
var
newCap: LongInt;
itemLoc: pointer;
locIndex: LongInt;
begin
for locIndex := index to _count - 1 do
begin
itemLoc := pointer(LongInt(_buffer) + locIndex * 4);
Move(pointer(LongInt(itemLoc) + 4)^, itemLoc^, 4);
end;
Dec(_count);
end;
procedure TList.Clear;
begin
FreeMem(_buffer, _capacity * SizeOf(pointer));
_count := 0;
_buffer := nil;
_capacity := 0;
SetCapacity(DefaultListCapacity);
end;
function TList.Where(predicate: TPredicate; var value): PList;
var
result: PList;
index: integer;
item: pointer;
begin
result := New(PList, Create(''));
Where := result;
if (_count = 0) then exit;
for index := 0 to _count - 1 do
begin
item := GetItem(index);
if predicate(item, value) then
result^.Add(item);
end;
end;
procedure TList.SetCapacity(cap: integer);
var
newBuffer: pointer;
max: word;
dataSize: word;
error: TError;
begin
if (_capacity = cap) then exit;
dataSize := cap * SizeOf(Pointer);
GetMem(newBuffer, dataSize);
if (newBuffer = nil) then
begin
if (OnAllocationError <> nil) then
TAllocationError(OnAllocationError^)(error);
exit;
end;
FillChar(newBuffer^, dataSize, 0);
if (_capacity > 0) and (_buffer <> nil) then
begin
if (cap > _capacity) then
Move(_buffer^, newBuffer^, _capacity)
else
Move(_buffer^, NewBuffer^, cap);
FreeMem(_buffer, _capacity * SizeOf(pointer));
end;
_buffer := newBuffer;
_capacity := cap;
if _count > _capacity then
_count := _capacity;
end;
function TList.Count: integer;
begin
Count := _count;
end;
function TList.Capacity: integer;
begin
Capacity := _capacity;
end;
procedure TObjectList.Init;
begin
TList.Init;
TypeName := 'TObjectList';
DisposeObjects := true;
end;
destructor TObjectList.Done;
var
index: integer;
item: PObject;
begin;
if DisposeObjects then
begin
for index := 0 to _count - 1 do
begin;
item := GetItem(index);
Dispose(item, Done);
end;
end;
TList.Done;
end;
function TObjectList.ToString: string;
begin
ToString := 'TObjectList';
end;
function TObjectList.GetItem(index: integer): PObject;
begin
GetItem := TList.GetItem(index);
end;
function TObjectList.IndexOf(item: PObject): integer;
begin
IndexOf := TList.IndexOf(item);
end;
function TObjectList.IndexOfId(idToFind: string25): integer;
var
index: integer;
currentId: string25;
begin
IndexOfId := -1;
for index := 0 to _count - 1 do
begin
currentId := GetItem(index)^.Id;
if (currentId = idToFind) then
begin
IndexOfId := index;
break;
end;
end;
end;
function TObjectList.Add(item: PObject): integer;
begin
Add := TList.Add(item);
end;
procedure TObjectList.Insert(index: integer; item: PObject);
begin
TList.Insert(index, item);
end;
procedure TObjectList.Delete(index: integer);
var
obj: PObject;
begin
obj := GetItem(index);
Dispose(obj);
end;
procedure TObjectList.Clear;
var
index: word;
item: PObject;
begin
TList.Clear;
end;
procedure TPersistent.Assign(source: TPersistent);
begin
end;
procedure TPersistent.AssignTo(dest: TPersistent);
begin
end;
begin
OnAllocationError := nil;
end.