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
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 2011 by Jonas Maebe
member of the Free Pascal development team.
This file implements the helper routines for dyn. Arrays in FPC
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program 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.
**********************************************************************
}
{ copying helpers }
procedure fpc_copy_shallow_array(src, dst: JLObject; srcstart: jint = -1; srccopylen: jint = -1);
var
srclen, dstlen: jint;
begin
if assigned(src) then
srclen:=JLRArray.getLength(src)
else
srclen:=0;
if assigned(dst) then
dstlen:=JLRArray.getLength(dst)
else
dstlen:=0;
if srcstart=-1 then
srcstart:=0
else if srcstart>=srclen then
exit;
if srccopylen=-1 then
srccopylen:=srclen
else if srcstart+srccopylen>srclen then
srccopylen:=srclen-srcstart;
{ causes exception in JLSystem.arraycopy }
if (srccopylen=0) or
(dstlen=0) then
exit;
JLSystem.arraycopy(src,srcstart,dst,0,min(srccopylen,dstlen));
end;
procedure fpc_copy_jrecord_array(src, dst: TJRecordArray; srcstart: jint = -1; srccopylen: jint = -1);
var
i: longint;
srclen, dstlen: jint;
begin
srclen:=length(src);
dstlen:=length(dst);
if srcstart=-1 then
srcstart:=0
else if srcstart>=srclen then
exit;
if srccopylen=-1 then
srccopylen:=srclen
else if srcstart+srccopylen>srclen then
srccopylen:=srclen-srcstart;
{ no arraycopy, have to clone each element }
for i:=0 to min(srccopylen,dstlen)-1 do
src[srcstart+i].fpcDeepCopy(dst[i]);
end;
procedure fpc_copy_jenumset_array(src, dst: TJEnumSetArray; srcstart: jint = -1; srccopylen: jint = -1);
var
i: longint;
srclen, dstlen: jint;
begin
srclen:=length(src);
dstlen:=length(dst);
if srcstart=-1 then
srcstart:=0
else if srcstart>=srclen then
exit;
if srccopylen=-1 then
srccopylen:=srclen
else if srcstart+srccopylen>srclen then
srccopylen:=srclen-srcstart;
{ no arraycopy, have to clone each element }
for i:=0 to min(srccopylen,dstlen)-1 do
begin
dst[i].clear;
dst[i].addAll(src[srcstart+i]);
end;
end;
procedure fpc_copy_jbitset_array(src, dst: TJBitSetArray; srcstart: jint = -1; srccopylen: jint = -1);
var
i: longint;
srclen, dstlen: jint;
begin
srclen:=length(src);
dstlen:=length(dst);
if srcstart=-1 then
srcstart:=0
else if srcstart>=srclen then
exit;
if srccopylen=-1 then
srccopylen:=srclen
else if srcstart+srccopylen>srclen then
srccopylen:=srclen-srcstart;
{ no arraycopy, have to clone each element }
for i:=0 to min(srccopylen,dstlen)-1 do
begin
dst[i].clear;
dst[i].addAll(src[srcstart+i]);
end;
end;
procedure fpc_copy_jprocvar_array(src, dst: TJProcVarArray; srcstart: jint = -1; srccopylen: jint = -1);
var
i: longint;
srclen, dstlen: jint;
begin
srclen:=length(src);
dstlen:=length(dst);
if srcstart=-1 then
srcstart:=0
else if srcstart>=srclen then
exit;
if srccopylen=-1 then
srccopylen:=srclen
else if srcstart+srccopylen>srclen then
srccopylen:=srclen-srcstart;
{ no arraycopy, have to clone each element }
for i:=0 to min(srccopylen,dstlen)-1 do
src[srcstart+i].fpcDeepCopy(dst[i]);
end;
procedure fpc_copy_jshortstring_array(src, dst: TShortstringArray; srcstart: jint = -1; srccopylen: jint = -1);
var
i: longint;
srclen, dstlen: jint;
begin
srclen:=length(src);
dstlen:=length(dst);
if srcstart=-1 then
srcstart:=0
else if srcstart>=srclen then
exit;
if srccopylen=-1 then
srccopylen:=srclen
else if srcstart+srccopylen>srclen then
srccopylen:=srclen-srcstart;
{ no arraycopy, have to clone each element }
for i:=0 to min(srccopylen,dstlen)-1 do
pshortstring(src[srcstart+i])^:=pshortstring(dst[i])^;
end;
{ 1-dimensional setlength routines }
function fpc_setlength_dynarr_generic(aorg, anew: JLObject; deepcopy: boolean; docopy: boolean = true): JLObject;
var
orglen, newlen: jint;
begin
orglen:=0;
newlen:=0;
if not deepcopy then
begin
if assigned(aorg) then
orglen:=JLRArray.getLength(aorg)
else
orglen:=0;
if assigned(anew) then
newlen:=JLRArray.getLength(anew)
else
newlen:=0;
end;
if deepcopy or
(orglen<>newlen) then
begin
if docopy then
fpc_copy_shallow_array(aorg,anew);
result:=anew
end
else
result:=aorg;
end;
function fpc_setlength_dynarr_jrecord(aorg, anew: TJRecordArray; deepcopy: boolean): TJRecordArray;
begin
if deepcopy or
(length(aorg)<>length(anew)) then
begin
fpc_copy_jrecord_array(aorg,anew);
result:=anew
end
else
result:=aorg;
end;
function fpc_setlength_dynarr_jenumset(aorg, anew: TJEnumSetArray; deepcopy: boolean): TJEnumSetArray;
begin
if deepcopy or
(length(aorg)<>length(anew)) then
begin
fpc_copy_jenumset_array(aorg,anew);
result:=anew
end
else
result:=aorg;
end;
function fpc_setlength_dynarr_jbitset(aorg, anew: TJBitSetArray; deepcopy: boolean): TJBitSetArray;
begin
if deepcopy or
(length(aorg)<>length(anew)) then
begin
fpc_copy_jbitset_array(aorg,anew);
result:=anew
end
else
result:=aorg;
end;
function fpc_setlength_dynarr_jprocvar(aorg, anew: TJProcVarArray; deepcopy: boolean): TJProcVarArray;
begin
if deepcopy or
(length(aorg)<>length(anew)) then
begin
fpc_copy_jprocvar_array(aorg,anew);
result:=anew
end
else
result:=aorg;
end;
function fpc_setlength_dynarr_jshortstring(aorg, anew: TShortstringArray; deepcopy: boolean): TShortstringArray;
begin
if deepcopy or
(length(aorg)<>length(anew)) then
begin
fpc_copy_jshortstring_array(aorg,anew);
result:=anew
end
else
result:=aorg;
end;
{ multi-dimensional setlength routine }
function fpc_setlength_dynarr_multidim(aorg, anew: TJObjectArray; deepcopy: boolean; ndim: longint; eletype: jchar): TJObjectArray;
var
partdone,
i: longint;
begin
{ resize the current dimension; no need to copy the subarrays of the old
array, as the subarrays will be (re-)initialised immediately below }
{ the srcstart/srccopylen always refers to the first dimension (since copy()
performs a shallow copy of a dynamic array }
result:=TJObjectArray(fpc_setlength_dynarr_generic(JLObject(aorg),JLObject(anew),deepcopy,false));
{ if aorg was empty, there's nothing else to do since result will now
contain anew, of which all other dimensions are already initialised
correctly since there are no aorg elements to copy }
if not assigned(aorg) and
not deepcopy then
exit;
partdone:=min(high(result),high(aorg));
{ ndim must be >=2 when this routine is called, since it has to return
an array of java.lang.Object! (arrays are also objects, but primitive
types are not) }
if ndim=2 then
begin
{ final dimension -> copy the primitive arrays }
case eletype of
FPCJDynArrTypeRecord:
begin
for i:=low(result) to partdone do
result[i]:=JLObject(fpc_setlength_dynarr_jrecord(TJRecordArray(aorg[i]),TJRecordArray(anew[i]),deepcopy));
for i:=succ(partdone) to high(result) do
result[i]:=JLObject(fpc_setlength_dynarr_jrecord(nil,TJRecordArray(anew[i]),deepcopy));
end;
FPCJDynArrTypeEnumSet:
begin
for i:=low(result) to partdone do
result[i]:=JLObject(fpc_setlength_dynarr_jenumset(TJEnumSetArray(aorg[i]),TJEnumSetArray(anew[i]),deepcopy));
for i:=succ(partdone) to high(result) do
result[i]:=JLObject(fpc_setlength_dynarr_jenumset(nil,TJEnumSetArray(anew[i]),deepcopy));
end;
FPCJDynArrTypeBitSet:
begin
for i:=low(result) to partdone do
result[i]:=JLObject(fpc_setlength_dynarr_jbitset(TJBitSetArray(aorg[i]),TJBitSetArray(anew[i]),deepcopy));
for i:=succ(partdone) to high(result) do
result[i]:=JLObject(fpc_setlength_dynarr_jbitset(nil,TJBitSetArray(anew[i]),deepcopy));
end;
FPCJDynArrTypeProcVar:
begin
for i:=low(result) to partdone do
result[i]:=JLObject(fpc_setlength_dynarr_jprocvar(TJProcVarArray(aorg[i]),TJProcVarArray(anew[i]),deepcopy));
for i:=succ(partdone) to high(result) do
result[i]:=JLObject(fpc_setlength_dynarr_jprocvar(nil,TJProcVarArray(anew[i]),deepcopy));
end;
FPCJDynArrTypeShortstring:
begin
for i:=low(result) to partdone do
result[i]:=JLObject(fpc_setlength_dynarr_jshortstring(TShortstringArray(aorg[i]),TShortstringArray(anew[i]),deepcopy));
for i:=succ(partdone) to high(result) do
result[i]:=JLObject(fpc_setlength_dynarr_jshortstring(nil,TShortstringArray(anew[i]),deepcopy));
end;
else
begin
for i:=low(result) to partdone do
result[i]:=fpc_setlength_dynarr_generic(aorg[i],anew[i],deepcopy);
for i:=succ(partdone) to high(result) do
result[i]:=fpc_setlength_dynarr_generic(nil,anew[i],deepcopy);
end;
end;
end
else
begin
{ recursively handle the next dimension }
for i:=low(result) to partdone do
result[i]:=JLObject(fpc_setlength_dynarr_multidim(TJObjectArray(aorg[i]),TJObjectArray(anew[i]),deepcopy,pred(ndim),eletype));
for i:=succ(partdone) to high(result) do
result[i]:=JLObject(fpc_setlength_dynarr_multidim(nil,TJObjectArray(anew[i]),deepcopy,pred(ndim),eletype));
end;
end;
function fpc_dynarray_copy(src: JLObject; start, len: longint; ndim: longint; eletype: jchar): JLObject;
var
i: longint;
srclen: longint;
begin
if not assigned(src) then
begin
result:=nil;
exit;
end;
srclen:=JLRArray.getLength(src);
if (start=-1) and
(len=-1) then
begin
len:=srclen;
start:=0;
end
else if (start+len>srclen) then
len:=srclen-start+1;
result:=JLRArray.newInstance(src.getClass.getComponentType,len);
if ndim=1 then
begin
case eletype of
FPCJDynArrTypeRecord:
begin
for i:=0 to len-1 do
TJObjectArray(result)[i]:=FpcBaseRecordType(TJObjectArray(src)[i]).clone;
end;
FPCJDynArrTypeEnumSet:
begin
for i:=0 to len-1 do
TJObjectArray(result)[i]:=JUEnumSet(TJObjectArray(src)[i]).clone;
end;
FPCJDynArrTypeBitSet:
begin
for i:=0 to len-1 do
TJObjectArray(result)[i]:=FpcBitSet(TJObjectArray(src)[i]).clone;
end;
FPCJDynArrTypeProcvar:
begin
for i:=0 to len-1 do
TJObjectArray(result)[i]:=FpcBaseProcVarType(TJObjectArray(src)[i]).clone;
end;
FPCJDynArrTypeShortstring:
begin
for i:=0 to len-1 do
TJObjectArray(result)[i]:=ShortStringClass(TJObjectArray(src)[i]).clone;
end;
else
fpc_copy_shallow_array(src,result,start,len);
end
end
else
begin
for i:=0 to len-1 do
TJObjectArray(result)[i]:=fpc_dynarray_copy(TJObjectArray(src)[start+i],-1,-1,ndim-1,eletype);
end;
end;
|