-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathArray.mpl
353 lines (299 loc) · 8.19 KB
/
Array.mpl
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
# Copyright (C) Matway Burkow
#
# This repository and all its contents belong to Matway Burkow (referred here and below as "the owner").
# The content is for demonstration purposes only.
# It is forbidden to use the content or any part of it for any purpose without explicit permission from the owner.
# By contributing to the repository, contributors acknowledge that ownership of their work transfers to the owner.
"Span.toSpan" use
"Span.toSpan2" use
"algorithm.each" use
"algorithm.filter" use
"algorithm.findOrdinal" use
"algorithm.schemaNameBeginsWith" use
"algorithm.toIter" use
"algorithm.unhead" use
"control.&&" use
"control.Natx" use
"control.Ref" use
"control.assert" use
"control.automatic?" use
"control.compose" use
"control.drop" use
"control.pfunc" use
"control.times" use
"control.when" use
"control.while" use
"memory.memcpy" use
"memory.mplFree" use
"memory.mplMalloc" use
"memory.mplRealloc" use
makeArrayObject: [
Item: MEMORY_DEBUG_OBJECT:;;
{
SCHEMA_NAME: "Array<" @Item schemaName & ">" & virtual;
Item: @Item Ref virtual;
append: [
append: ["Invalid source schema" raiseStaticError];
append: [toIter TRUE] [
iter: toIter;
@iter "size" has [
oldSize: arraySize new;
oldSize @iter.size + enlarge
@iter.size [
@iter.next drop oldSize i + at set
] times
] [
[
@iter.next [append TRUE] [drop FALSE] if
] loop
] if
] pfunc;
append: [toSpan TRUE] [
span: toSpan;
oldSize: arraySize new;
oldSize span.size + enlarge
@Item automatic? [
span.size [
i @span.at oldSize i + at set
] times
] [
[span.data @Item same] "Inconsistent schemas" assert
span.size 0 = ~ [
@Item storageSize span.size Natx cast * span.data storageAddress oldSize at storageAddress memcpy drop
] when
] if
] pfunc;
append: [@Item same] [
item:;
addReserve
arraySize 1 + !arraySize
newItem: arraySize 1 - at;
@newItem manuallyInitVariable
@item @newItem set
] pfunc;
append
];
assign: [
assign: ["Invalid source schema" raiseStaticError];
assign: [toIter TRUE] [
iter: toIter;
@iter "size" has [
@iter.size resize
@iter.size [
@iter.next drop i at set
] times
] [
clear
[
@iter.next [append TRUE] [drop FALSE] if
] loop
] if
] pfunc;
assign: [toSpan TRUE] [
span: toSpan;
span.size resize
@Item automatic? [
span.size [
i @span.at i at set
] times
] [
[span.data @Item same] "Inconsistent schemas" assert
span.size 0 = ~ [
@Item storageSize span.size Natx cast * span.data storageAddress @arrayData storageAddress memcpy drop
] when
] if
] pfunc;
assign: [self same] [@self set] pfunc;
assign
];
at: [
index:;
index 0i32 same ~ [0 .ONLY_I32_ALLOWED] when
[index 0 < ~ [index arraySize <] &&] "Index is out of range!" assert
@arrayData storageAddress @Item storageSize index Natx cast * + @Item addressToReference
];
clear: [
0 shrink
];
data: [
@arrayData
];
enlarge: [
newSize: new dynamic;
[newSize arraySize < ~] "Enlarged size is less than old size!" assert
arrayReserve newSize < [
newReserve: getNextReserve;
newReserve newSize < [newSize new !newReserve] when
newReserve setReserve
] when
i: arraySize new;
newSize new !arraySize
@Item automatic? [
[i arraySize <] [
i at manuallyInitVariable
i 1 + !i
] while
] when
];
erase: [
index:;
index 0i32 same ~ [0 .ONLY_I32_ALLOWED] when
[index 0 < ~ [index arraySize <] &&] "Index is out of range!" assert
index size 1 - < [
last index at set
] when
popBack
];
eraseIf: [
eraseIfBody:;
key: self @eraseIfBody findOrdinal;
key -1 = ~ [
@self key 1 + unhead @eraseIfBody [~] compose filter [
key at set
key 1 + !key
] each
key shrink
] when
];
iter: [
span
];
last: [
arraySize 1 - at
];
popBack: [
[arraySize 0 >] "Pop from empty array!" assert
arraySize 1 - shrink
];
release: [
clear
addr: @arrayData storageAddress;
size: @Item storageSize arrayReserve Natx cast *;
MEMORY_DEBUG_OBJECT [TRUE !memoryDebugEnabled] when
addr 0nx = ~ [size addr mplFree] when
MEMORY_DEBUG_OBJECT [FALSE !memoryDebugEnabled] when
@Item !arrayData
0 !arraySize
0 !arrayReserve
];
reserve: [
arrayReserve new
];
resize: [
newSize:;
newSize arraySize = [
] [
newSize arraySize < [
newSize shrink
] [
newSize enlarge
] if
] if
];
setReserve: [
newReserve:;
[newReserve arrayReserve < ~] "New reserve is less than old reserve!" assert
newReserve arrayReserve = ~ [
MEMORY_DEBUG_OBJECT [TRUE !memoryDebugEnabled] when
arrayReserve 0 = [
@Item storageSize newReserve Natx cast * mplMalloc
] [
@Item storageSize newReserve Natx cast * @Item storageSize arrayReserve Natx cast * @arrayData storageAddress mplRealloc
] if
MEMORY_DEBUG_OBJECT [FALSE !memoryDebugEnabled] when
@Item addressToReference !arrayData
newReserve new !arrayReserve
] when
];
shrink: [
newSize:;
[newSize arraySize > ~] "Shrinked size is bigger than the old size!" assert
@Item automatic? [
i: arraySize new dynamic;
[i newSize >] [
i 1 - !i
i at manuallyDestroyVariable
] while
] when
newSize new !arraySize
];
size: [
arraySize new
];
slice: [
span.slice
];
span: [
@arrayData arraySize toSpan2
];
private MEMORY_DEBUG_OBJECT: MEMORY_DEBUG_OBJECT new virtual;
private arrayData: @Item;
private arraySize: 0;
private arrayReserve: 0;
private ASSIGN: [
other:;
other.size resize
i: 0 dynamic;
[i arraySize <] [
i other.at i at set
i 1 + !i
] while
];
private DIE: [
clear
addr: @arrayData storageAddress;
size: @Item storageSize arrayReserve Natx cast *;
MEMORY_DEBUG_OBJECT [TRUE !memoryDebugEnabled] when
addr 0nx = ~ [size addr mplFree] when
MEMORY_DEBUG_OBJECT [FALSE !memoryDebugEnabled] when
];
private INIT: [
@Item !arrayData
0 !arraySize
0 !arrayReserve
];
private addReserve: [
arraySize arrayReserve = [
getNextReserve setReserve
] when
];
private getNextReserve: [
arrayReserve arrayReserve 4 / + 4 +
];
}
];
Array: [FALSE makeArrayObject];
MemoryDebugArray: [TRUE makeArrayObject];
getHeapUsedSize: ["Array<" schemaNameBeginsWith] [
arg:;
result: arg.data storageSize arg.arrayReserve Natx cast *;
i: 0 dynamic;
[
i arg.size < [
result i arg.at getHeapUsedSize + !result
i 1 + !i
TRUE
] &&
] loop
result
] pfunc;
toArray: [toIter TRUE] [
iter: toIter;
first: firstValid: @iter.next;;
array: @first newVarOfTheSameType Array;
firstValid [
@iter "size" has [@iter.size 1 + @array.setReserve] when
@first @array.append
@iter @array.append
] when
@array
] pfunc;
toArray: [toSpan TRUE] [
span: toSpan;
array: @span.data newVarOfTheSameType Array;
@span @array.assign
@array
] pfunc;
toArray: ["Array<" schemaNameBeginsWith] [
new
] pfunc;