-
Notifications
You must be signed in to change notification settings - Fork 1
/
adtarray_impl.i
513 lines (433 loc) · 12.8 KB
/
adtarray_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
{@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
array_impl.i::prefix=&_mcp_prefix&::item_type=&ItemType&
}
&include adtarray.defs
&include adtarray_impl.mcp
{ --------------------------- TArray ----------------------------- }
constructor TArray.Create(afirstIndex : IndexType);
begin
inherited Create;
firstIndex := afirstIndex;
ArrayAllocate(FItems, arrInitialCapacity, 0);
end;
constructor TArray.Create;
begin
inherited Create;
ArrayAllocate(FItems, arrInitialCapacity, 0);
end;
constructor TArray.CreateCopy(const cont : TArray;
const itemCopier : IUnaryFunctor);
var
i : IndexType;
begin
inherited CreateCopy(cont);
firstIndex := cont.Firstindex;
if itemCopier <> nil then
begin
ArrayAllocate(FItems, cont.FItems^.Size + arrInitialCapacity, 0);
for i := 0 to cont.FItems^.Size - 1 do
begin
{ may raise (the statement below) }
FItems^.Items[i] := itemCopier.Perform(cont.FItems^.Items[i]);
Inc(FItems^.Size); { increasing gradually (not at once after
the loop) in case of an exception }
end;
end else
ArrayAllocate(FItems, arrInitialCapacity, 0);
end;
destructor TArray.Destroy;
begin
if FItems <> nil then
Clear;
ArrayDeallocate(FItems);
inherited;
end;
function TArray.GetCapacity : SizeType;
begin
Result := FItems^.Capacity;
end;
procedure TArray.SetCapacity(cap : SizeType);
begin
if cap > FItems^.Size then
ArrayReallocate(FItems, cap);
end;
function TArray.RandomAccessStart : TRandomAccessIterator;
begin
Result := Start;
end;
function TArray.RandomAccessFinish : TRandomAccessIterator;
begin
Result := Finish;
end;
function TArray.Start : TArrayIterator;
begin
Result := TArrayIterator.Create(firstIndex, self);
end;
function TArray.Finish : TArrayIterator;
begin
Result := TArrayIterator.Create(firstIndex + FItems^.Size, self);
end;
function TArray.CopySelf(const ItemCopier : IUnaryFunctor) : TContainerAdt;
begin
Result := TArray.CreateCopy(self, itemCopier);
end;
procedure TArray.Swap(cont : TContainerAdt);
begin
if cont is TArray then
begin
BasicSwap(cont);
ExchangePtr(FItems, TArray(cont).FItems);
ExchangeData(firstIndex, TArray(cont).firstIndex, SizeOf(IndexType));
end else
inherited;
end;
function TArray.GetItem(index : IndexType) : ItemType;
begin
Result := FItems^.Items[index - firstIndex];
end;
procedure TArray.SetItem(index : IndexType; elem : ItemType);
begin
DisposeItem(FItems^.Items[index - firstIndex]);
FItems^.Items[index - firstIndex] := elem;
end;
procedure TArray.Insert(index : IndexType; aitem : ItemType);
begin
Assert((index >= LowIndex) and (index <= HighIndex + 1), msgInvalidIndex);
ArrayReserveItems(FItems, index - firstIndex, 1);
FItems^.Items[index - firstIndex] := aitem;
end;
procedure TArray.Delete(index : IndexType);
var
aitem : ItemType;
begin
Dec(index, firstIndex);
aitem := FItems^.Items[index];
ArrayRemoveItems(FItems, index, 1); { may raise }
DisposeItem(aitem);
end;
function TArray.Delete(astart : IndexType; n : SizeType) : SizeType;
var
fin, i : IndexType;
begin
fin := astart - firstIndex + n;
if fin > FItems^.Size then
fin := FItems^.Size;
&if (&_mcp_type_needs_destruction(&ItemType))
if OwnsItems then
begin
for i := astart - firstIndex to fin - 1 do
begin
DisposeItem(FItems^.Items[i]);
end;
end;
&endif
Result := fin - astart + firstIndex;
ArrayRemoveItems(FItems, astart - firstIndex, Result);
end;
function TArray.Extract(index : IndexType) : ItemType;
begin
Dec(index, firstIndex);
Result := FItems^.Items[index];
ArrayRemoveItems(FItems, index, 1); { may raise }
end;
procedure TArray.PushBack(aitem : ItemType);
begin
ArrayPushBack(FItems, aitem);
end;
procedure TArray.PushFront(aitem : ItemType);
begin
ArrayPushFront(FItems, aitem, false);
end;
procedure TArray.PopBack;
var
aitem : ItemType;
begin
Assert(FItems^.Size <> 0, msgReadEmpty);
aitem := ArrayPopBack(FItems);
DisposeItem(aitem);
end;
procedure TArray.PopFront;
var
aitem : ItemType;
begin
Assert(FItems^.Size <> 0, msgReadEmpty);
aitem := ArrayPopFront(FItems, true);
DisposeItem(aitem);
end;
function TArray.Back : ItemType;
begin
Assert(FItems^.Size <> 0, msgReadEmpty);
Result := FItems^.Items[FItems^.Size - 1];
end;
function TArray.Front : ItemType;
begin
Assert(FItems^.Size <> 0, msgReadEmpty);
Result := FItems^.Items[0];
end;
procedure TArray.Clear;
begin
&if (&_mcp_type_needs_destruction(&ItemType))
if OwnsItems then
begin
ArrayApplyFunctor(FItems,
AdaptObject(_mcp_address_of_DisposeItem));
end;
&endif
ArrayClear(FItems, arrInitialCapacity, 0);
GrabageCollector.FreeObjects;
end;
function TArray.Empty : Boolean;
begin
Result := FItems^.Size = 0;
end;
function TArray.Size : SizeType;
begin
Result := FItems^.Size;
end;
function TArray.LowIndex : IndexType;
begin
Result := firstIndex;
end;
function TArray.HighIndex : IndexType;
begin
Result := firstIndex + FItems^.Size - 1;
end;
function TArray.SetLowIndex(ind : IndexType) : IndexType;
begin
Result := firstIndex;
firstIndex := ind;
end;
{ -------------------------- TArrayIterator ----------------------------- }
function TArrayIterator.CopySelf : TIterator;
begin
Result := TArrayIterator.Create(FIndex, FCont);
end;
procedure TArrayIterator.ExchangeItemsAt(i, j : IndexType);
var
i1, i2 : IndexType;
begin
with TArray(FCont) do
begin
i1 := FIndex - TArray(FCont).firstIndex + i;
i2 := FIndex - TArray(FCont).firstIndex + j;
Assert((i1 >= 0) and (i1 < FItems^.Size), msgInvalidIndex);
Assert((i2 >= 0) and (i2 < FItems^.Size), msgInvalidIndex);
adtutils.ExchangeItem(FItems^.Items[i1], FItems^.Items[i2]);
end;
end;
{ ====================================================================== }
{ --------------------- TPascalArray --------------------------- }
constructor TPascalArray.Create(pascalArray : TPascalArrayType);
begin
inherited Create;
FPascalArray := pascalArray;
end;
constructor TPascalArray.CreateCopy(const cont : TPascalArray;
const itemCopier : IUnaryFunctor);
var
i : IndexType;
begin
inherited CreateCopy(cont);
if itemCopier <> nil then
begin
SetLength(FPascalArray, Length(cont.FPascalArray));
try
for i := 0 to Length(FPascalArray) - 1 do
FPascalArray[i] := itemCopier.Perform(cont.FPascalArray[i]);
i := Length(FPascalArray);
{ may raise above }
except
SetLength(FPascalArray, i);
raise;
end;
end else
SetLength(FPascalArray, 0);
end;
destructor TPascalArray.Destroy;
begin
Clear;
inherited;
end;
function TPascalArray.GetCapacity : SizeType;
begin
Result := Length(FPascalArray);
end;
procedure TPascalArray.SetCapacity(cap : SizeType);
begin
{ ignore }
end;
function TPascalArray.RandomAccessStart : TRandomAccessIterator;
begin
Result := Start;
end;
function TPascalArray.RandomAccessFinish : TRandomAccessIterator;
begin
Result := Finish;
end;
function TPascalArray.Start : TPascalArrayIterator;
begin
Result := TPascalArrayIterator.Create(0, self);
end;
function TPascalArray.Finish : TPascalArrayIterator;
begin
Result := TPascalArrayIterator.Create(Length(FPascalArray), self);
end;
function TPascalArray.CopySelf(const ItemCopier : IUnaryFunctor) : TContainerAdt;
begin
Result := TPascalArray.CreateCopy(self, itemcopier);
end;
procedure TPascalArray.Swap(cont : TContainerAdt);
begin
if cont is TPascalArray then
begin
BasicSwap(cont);
ExchangePtr(FPascalArray, TPascalArray(cont).FPascalArray);
end else
inherited;
end;
function TPascalArray.GetItem(index : IndexType) : ItemType;
begin
Assert((index >= 0) and (index < Length(FPascalArray)), msgInvalidIndex);
Result := FPascalArray[index];
end;
procedure TPascalArray.SetItem(index : IndexType; elem : ItemType);
begin
Assert((index >= 0) and (index < Length(FPascalArray)), msgInvalidIndex);
DisposeItem(FPascalArray[index]);
FPascalArray[index] := elem;
end;
procedure TPascalArray.Insert(index : IndexType; aitem : ItemType);
begin
Assert((index >= 0) and (index <= Length(FPascalArray)), msgInvalidIndex);
SetLength(FPascalArray, Length(FPascalArray) + 1);
SafeMove(FPascalArray[index], FPascalArray[index + 1],
(Length(FPascalArray) - 1 - index));
FPascalArray[index] := aitem;
end;
procedure TPascalArray.Delete(index : IndexType);
var
aitem : ItemType;
begin
Assert((index >= 0) and (index < Length(FPascalArray)), msgInvalidIndex);
aitem := Extract(index);
DisposeItem(aitem);
end;
function TPascalArray.Delete(astart : IndexType; n : SizeType) : SizeType;
var
i : IndexType;
begin
Assert((astart >= 0) and (astart < Length(FPascalArray)), msgInvalidIndex);
if astart + n > Length(FPascalArray) then
n := Length(FPascalArray) - astart;
&if (&_mcp_type_needs_destruction(&ItemType))
if OwnsItems then
begin
for i := astart to astart + n - 1 do
DisposeItem(FPascalArray[i]);
end;
&endif
if astart + n <> Length(FPascalArray) then
begin
SafeMove(FPascalArray[astart + n], FPascalArray[astart],
(Length(FPascalArray) - astart - n));
end;
SetLength(FPascalArray, Length(FPascalArray) - n);
Result := n;
end;
function TPascalArray.Extract(index : IndexType) : ItemType;
begin
Assert((index >= 0) and (index < Length(FPascalArray)), msgInvalidIndex);
Result := FPascalArray[index];
SafeMove(FPascalArray[index + 1], FPascalArray[index],
(Length(FPascalArray) - index - 1));
SetLength(FPascalArray, Length(FPascalArray) - 1);
end;
procedure TPascalArray.PushBack(aitem : ItemType);
begin
SetLength(FPascalArray, Length(FPascalArray) + 1);
FPascalArray[Length(FPascalArray) - 1] := aitem;
end;
procedure TPascalArray.PushFront(aitem : ItemType);
begin
SetLength(FPascalArray, Length(FPascalArray) + 1);
SafeMove(FPascalArray[0], FPascalArray[1],
(Length(FPascalArray) - 1));
FPascalArray[0] := aitem;
end;
procedure TPascalArray.PopBack;
begin
Assert(Length(FPascalArray) <> 0, msgReadEmpty);
DisposeItem(FPascalArray[Length(FPascalArray) - 1]);
SetLength(FPascalArray, Length(FPascalArray) - 1);
end;
procedure TPascalArray.PopFront;
begin
Assert(Length(FPascalArray) <> 0, msgReadEmpty);
DisposeItem(FPascalArray[0]);
SafeMove(FPascalArray[1], FPascalArray[0],
(Length(FPascalArray) - 1));
SetLength(FPascalArray, Length(FPascalArray) - 1);
end;
function TPascalArray.Back : ItemType;
begin
Assert(Length(FPascalArray) <> 0, msgReadEmpty);
Result := FPascalArray[Length(FPascalArray) - 1];
end;
function TPascalArray.Front : ItemType;
begin
Assert(Length(FPascalArray) <> 0, msgReadEmpty);
Result := FPascalArray[0];
end;
procedure TPascalArray.Clear;
var
i : IndexType;
begin
&if (&_mcp_type_needs_destruction(&ItemType))
if OwnsItems then
begin
for i := 0 to Length(FPascalArray) - 1 do
DisposeItem(FPascalArray[i]);
SetLength(FPascalArray, 0);
end;
&endif
GrabageCollector.FreeObjects;
end;
function TPascalArray.Empty : Boolean;
begin
Result := Length(FPascalArray) = 0;
end;
function TPascalArray.Size : SizeType;
begin
Result := Length(FPascalArray);
end;
{ -------------------------- TPascalArrayIterator --------------------------- }
function TPascalArrayIterator.CopySelf : TIterator;
begin
Result := TPascalArrayIterator.Create(FIndex, FCont);
end;
procedure TPascalArrayIterator.ExchangeItemsAt(i, j : IndexType);
begin
Assert((i >= 0) and (i < Length(TPascalArray(FCont).FPascalArray)),
msgInvalidIndex);
Assert((j >= 0) and (j < Length(TPascalArray(FCont).FPascalArray)),
msgInvalidIndex);
adtutils.ExchangeItem(TPascalArray(FCont).FPascalArray[i],
TPascalArray(FCont).FPascalArray[j]);
end;