-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuListObject.pas
More file actions
350 lines (275 loc) · 9.04 KB
/
uListObject.pas
File metadata and controls
350 lines (275 loc) · 9.04 KB
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
// Developed using Delphi for Windows and Mac platforms.
// Ths source is distributed under Apache 2.0
// Copyright (C) 2019-2020 Herbert M Sauro
// Author Contact Information:
// email: hsauro@gmail.com
unit uListObject;
interface
Uses System.SysUtils, uUtils, System.generics.Collections,
uMemoryManager, uStringObject;
type
TListItemType = (liInteger, liBoolean, liDouble, liString, liList);
TListItem = class;
TListObject = class (TRhodusObject)
private
public
list : TObjectList<TListItem>;
class function addLists (list1, list2 : TListObject) : TListObject;
class function multiply (value : integer; aList : TListObject) : TListObject;
class function listEquals (list1, list2 : TListObject) : boolean;
procedure append (iValue : integer); overload;
procedure append (bValue : boolean); overload;
procedure append (dValue : double); overload;
procedure append (sValue : TStringObject); overload;
function clone : TListObject;
function listToString : string;
function getsize : integer;
constructor Create (count : integer);
destructor Destroy; override;
end;
TListItem = class (TObject)
itemType : TListItemType;
iValue : integer;
bValue : boolean;
dValue : double;
sValue : TStringObject;
lValue : TListObject;
function elementToString : string;
class function listEquals (list1 : TListItem; list2 : TListItem) : boolean;
function getSize() : integer;
constructor Create (iValue : integer); overload;
constructor Create (bValue : boolean); overload;
constructor Create (dValue : double); overload;
constructor Create (sValue : TStringObject); overload;
constructor Create (lValue : TListObject); overload;
constructor Create(item : TListItem); overload;
destructor Destroy; override;
end;
implementation
Uses uVMExceptions;
constructor TListObject.Create (count : integer);
var i : integer;
begin
inherited Create;
list := TObjectList<TListItem>.Create;
for i := 0 to count - 1 do
list.add (TListItem.Create (0));
memoryList.addNode (self);
end;
destructor TListObject.Destroy;
begin
list.Free;
inherited;
end;
function TListObject.clone : TListObject;
var i : integer;
begin
result := TListObject.Create (self.list.count);
for i := 0 to self.list.Count - 1 do
case self.list[i].itemType of
liInteger : begin result.list[i].itemType := liInteger; result.list[i].iValue := self.list[i].iValue; end;
liBoolean : begin result.list[i].itemType := liBoolean; result.list[i].bValue := self.list[i].bValue; end;
liDouble : begin result.list[i].itemType := liDouble; result.list[i].dValue := self.list[i].dValue; end;
liString : begin
result.list[i].itemType := liString;
result.list[i].sValue := self.list[i].sValue.clone;
result.list[i].sValue.blockType := btOwned;
end;
liList : begin
result.list[i].itemType := liList;
result.list[i].lValue := self.list[i].lValue.clone;
result.list[i].lValue.blockType := btOwned;
end;
end;
end;
function TListObject.listToString : string;
var i : integer;
begin
result := '{';
if self.list.Count > 0 then
result := result + self.list[0].elementToString;
for i := 1 to self.list.Count - 1 do
begin
result := result + ',' + self.list[i].elementToString;
end;
result := result + '}';
end;
function TListObject.getsize : integer;
var i : integer;
begin
result := getRhodusObjectSize;
result := result + self.InstanceSize;
for i := 0 to self.list.Count - 1 do
result := result + self.list[i].getSize();
end;
class function TListObject.addLists (list1, list2 : TListObject) : TListObject;
var i : integer;
begin
if list1.blockType = btGarbage then
result := list1
else
result := list1.clone;
for i := 0 to list2.list.Count - 1 do
result.list.Add (TListItem.Create (list2.list[i]));
end;
// Make value copies of aList and combine them into one list
// eg 3*{1} = {1,1,1}
class function TListObject.multiply (value : integer; aList : TListObject) : TListObject;
var i, j : integer;
nContents : integer;
workingCopy : TListObject;
item : TListItem;
begin
if aList.isBound then
workingCopy := alist.clone
else
workingCopy := aList;
nContents := aList.list.Count;
result := TListObject.Create(0);
for i := 0 to value - 1 do
if nContents = 0 then
result.list.Add(TListItem.Create (0))
else
for j := 0 to nContents - 1 do
begin
item := TListItem.Create(workingCopy.list[j]);
result.list.Add(item);
end;
end;
class function TListObject.listEquals (list1, list2 : TListObject) : boolean;
var i : integer;
begin
if list1.list.count <> list2.list.count then
exit (False);
result := True;
for i := 0 to list1.list.count - 1 do
result := result and TListItem.listEquals (list1.list[i], list2.list[i]);
end;
procedure TListObject.append (iValue : integer);
begin
list.Add (TListItem.Create(iValue));
end;
procedure TListObject.append (bValue : boolean);
begin
list.Add (TListItem.Create(bValue));
end;
procedure TListObject.append (dValue : double);
begin
list.Add (TListItem.Create(dValue));
end;
procedure TListObject.append (sValue : TStringObject);
begin
list.Add (TListItem.Create(sValue));
end;
// ----------------------------------------------------------------------
constructor TListItem.Create(item : TListItem);
begin
inherited Create;
itemType := item.itemType;
case itemType of
liInteger: self.iValue := item.iValue;
liDouble: self.dValue := item.dValue;
liBoolean: self.bValue := item.bValue;
liString: begin self.sValue := item.sValue.clone; self.sValue.blockType := btOwned; end;
liList : begin self.lValue := item.lValue.clone; self.lValue.blockType := btOwned; end;
else
ERuntimeException.Create('Internal Error: unknown itemType in ListItem.Create');
end;
end;
constructor TListItem.Create (iValue : integer);
begin
itemType := liInteger;
self.iValue := iValue;
end;
constructor TListItem.Create (bValue : boolean);
begin
itemType := liBoolean;
self.bValue := bValue;
end;
constructor TListItem.Create (dValue : double);
begin
itemType := liDouble;
self.dValue := dValue;
end;
constructor TListItem.Create (sValue : TStringObject);
begin
itemType := liString;
self.sValue := sValue;
end;
constructor TListItem.Create (lValue : TListObject);
begin
itemType := liList;
self.lValue := lValue;
end;
destructor TListItem.Destroy;
begin
if itemType = liList then
begin
if lValue.isOwned then
lValue.blockType := btGarbage
else
lValue.Free;
end;
if itemType = liString then
begin
if sValue.isOwned then
sValue.blockType := btGarbage
else
sValue.Free;
end;
inherited;
end;
function TListItem.getSize() : integer;
begin
result := sizeof (TObject);
result := result + sizeof (self.itemType);
case itemType of
liInteger : result := result + sizeof (integer);
liBoolean : result := result + sizeof (boolean);
liDouble : result := result + sizeof (double);
liString : result := result + sValue.getSize();
liList : result := result + lValue.getsize();
end;
end;
function boolToString (value : boolean) : string;
begin
if value then
result := 'True'
else
result := 'False';
end;
function TListItem.elementToString : string;
begin
result := '';
case self.itemType of
liInteger : result := result + inttostr (self.iValue);
liBoolean : result := result + boolToString (self.bValue);
liDouble : result := result + floattostr (self.dValue);
liString : result := result + self.sValue.value;
liList : result := result + self.lValue.listToString;
else
raise ERuntimeException.Create('Unknown type in elementToString');
end;
end;
class function TListItem.listEquals (list1 : TListItem; list2 : TListItem) : boolean;
begin
result := False;
if (list1.itemType = liInteger) and (list2.itemType = liInteger) then
if list1.iValue = list2.iValue then
exit (True) else exit (False);
if (list1.itemType = liBoolean) and (list2.itemType = liBoolean) then
if list1.bValue = list2.bValue then
exit (True) else exit (False);
if (list1.itemType = liDouble) and (list2.itemType = liDouble) then
if list1.dValue = list2.dValue then
exit (True) else exit (False);
if (list1.itemType = liString) and (list2.itemType = liString) then
if list1.sValue.isEqualTo (list2.sValue) then
exit (True) else exit (False);
if (list1.itemType = liList) and (list2.itemType = liList) then
exit (TListObject.listEquals (List1.lValue, List2.lValue))
else
exit (False);
end;
// -----------------------------------------------------------------------
end.