-
-
Notifications
You must be signed in to change notification settings - Fork 419
/
Copy patharray.pony
571 lines (481 loc) · 13.5 KB
/
array.pony
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
class Array[A] is Seq[A]
"""
Contiguous, resizable memory to store elements of type A.
"""
var _size: USize
var _alloc: USize
var _ptr: Pointer[A]
new create(len: USize = 0) =>
"""
Create an array with zero elements, but space for len elements.
"""
_size = 0
if len > 0 then
_alloc = len.next_pow2().max(len).max(8)
_ptr = Pointer[A]._alloc(_alloc)
else
_alloc = 0
_ptr = Pointer[A]
end
new init(from: A^, len: USize) =>
"""
Create an array of len elements, all initialised to the given value.
"""
_size = len
if len > 0 then
_alloc = len.next_pow2().max(len).max(8)
_ptr = Pointer[A]._alloc(_alloc)
var i: USize = 0
while i < len do
_ptr._update(i, from)
i = i + 1
end
else
_alloc = 0
_ptr = Pointer[A]
end
new from_cpointer(ptr: Pointer[A], len: USize, alloc: USize = 0) =>
"""
Create an array from a C-style pointer and length. The contents are not
copied.
"""
_size = len
if alloc > len then
_alloc = alloc
else
_alloc = len
end
_ptr = ptr
fun _copy_to(ptr: Pointer[this->A!], copy_len: USize,
from_offset: USize = 0, to_offset: USize = 0) =>
"""
Copy copy_len elements from this to that at specified offsets.
"""
_ptr._offset(from_offset)._copy_to(ptr._offset(to_offset), copy_len)
fun cpointer(offset: USize = 0): Pointer[A] tag =>
"""
Return the underlying C-style pointer.
"""
_ptr._offset(offset)
fun size(): USize =>
"""
The number of elements in the array.
"""
_size
fun space(): USize =>
"""
The available space in the array.
"""
_alloc
fun ref reserve(len: USize) =>
"""
Reserve space for len elements, including whatever elements are already in
the array. Array space grows geometrically.
"""
if _alloc < len then
_alloc = len.next_pow2().max(len).max(8)
_ptr = _ptr._realloc(_alloc)
end
fun ref compact() =>
"""
Try to remove unused space, making it available for garbage collection. The
request may be ignored.
"""
if _size <= (512 / _ptr._element_size()) then
if _size.next_pow2() != _alloc.next_pow2() then
_alloc = _size.next_pow2()
let old_ptr = _ptr = Pointer[A]._alloc(_alloc)
_ptr._consume_from(consume old_ptr, _size)
end
elseif _size < _alloc then
_alloc = _size
let old_ptr = _ptr = Pointer[A]._alloc(_alloc)
_ptr._consume_from(consume old_ptr, _size)
end
fun ref undefined[B: (A & Real[B] val & Number) = A](len: USize) =>
"""
Resize to len elements, populating previously empty elements with random
memory. This is only allowed for an array of numbers.
"""
reserve(len)
_size = len
fun apply(i: USize): this->A ? =>
"""
Get the i-th element, raising an error if the index is out of bounds.
"""
if i < _size then
_ptr._apply(i)
else
error
end
fun ref update(i: USize, value: A): A^ ? =>
"""
Change the i-th element, raising an error if the index is out of bounds.
"""
if i < _size then
_ptr._update(i, consume value)
else
error
end
fun ref insert(i: USize, value: A) ? =>
"""
Insert an element into the array. Elements after this are moved up by one
index, extending the array.
An out of bounds index raises an error.
"""
if i <= _size then
reserve(_size + 1)
_ptr._offset(i)._insert(1, _size - i)
_ptr._update(i, consume value)
_size = _size + 1
else
error
end
fun ref delete(i: USize): A^ ? =>
"""
Delete an element from the array. Elements after this are moved down by one
index, compacting the array.
An out of bounds index raises an error.
The deleted element is returned.
"""
if i < _size then
_size = _size - 1
_ptr._offset(i)._delete(1, _size - i)
else
error
end
fun ref truncate(len: USize) =>
"""
Truncate an array to the given length, discarding excess elements. If the
array is already smaller than len, do nothing.
"""
_size = _size.min(len)
fun ref trim_in_place(from: USize = 0, to: USize = -1) =>
"""
Trim the array to a portion of itself, covering `from` until `to`.
Unlike slice, the operation does not allocate a new array nor copy elements.
"""
let last = _size.min(to)
let offset = last.min(from)
_size = last - offset
_alloc = _alloc - offset
_ptr = if _size > 0 then _ptr._offset(offset) else _ptr.create() end
fun val trim(from: USize = 0, to: USize = -1): Array[A] val =>
"""
Return a shared portion of this array, covering `from` until `to`.
Both the original and the new array are immutable, as they share memory.
The operation does not allocate a new array pointer nor copy elements.
"""
let last = _size.min(to)
let offset = last.min(from)
recover
let size' = last - offset
let alloc = _alloc - offset
if size' > 0 then
from_cpointer(_ptr._offset(offset)._unsafe(), size', alloc)
else
create()
end
end
fun copy_to(dst: Array[this->A!], src_idx: USize, dst_idx: USize,
len: USize)
=>
"""
Copy len elements from this(src_idx) to dst(dst_idx).
"""
dst.reserve(dst_idx + len)
_ptr._offset(src_idx)._copy_to(dst._ptr._offset(dst_idx), len)
if dst._size < (dst_idx + len) then
dst._size = dst_idx + len
end
fun ref remove(i: USize, n: USize) =>
"""
Remove n elements from the array, beginning at index i.
"""
if i < _size then
let count = n.min(_size - i)
_size = _size - count
_ptr._offset(i)._delete(count, _size - i)
end
fun ref clear() =>
"""
Remove all elements from the array.
"""
_size = 0
fun ref push(value: A) =>
"""
Add an element to the end of the array.
"""
reserve(_size + 1)
_ptr._update(_size, consume value)
_size = _size + 1
fun ref pop(): A^ ? =>
"""
Remove an element from the end of the array.
The removed element is returned.
"""
delete(_size - 1)
fun ref unshift(value: A) =>
"""
Add an element to the beginning of the array.
"""
try
insert(0, consume value)
end
fun ref shift(): A^ ? =>
"""
Remove an element from the beginning of the array.
The removed element is returned.
"""
delete(0)
fun ref append(seq: (ReadSeq[A] & ReadElement[A^]), offset: USize = 0,
len: USize = -1)
=>
"""
Append the elements from a sequence, starting from the given offset.
"""
if offset >= seq.size() then
return
end
let copy_len = len.min(seq.size() - offset)
reserve(_size + copy_len)
var n = USize(0)
try
while n < copy_len do
_ptr._update(_size + n, seq(offset + n))
n = n + 1
end
end
_size = _size + n
fun ref concat(iter: Iterator[A^], offset: USize = 0, len: USize = -1) =>
"""
Add len iterated elements to the end of the array, starting from the given
offset.
"""
var n = USize(0)
try
while n < offset do
if iter.has_next() then
iter.next()
else
return
end
n = n + 1
end
end
n = 0
// If a concrete len is specified, we take the caller at their word
// and reserve that much space, even though we can't verify that the
// iterator actually has that many elements available. Reserving ahead
// of time lets us take a fast path of direct pointer access.
if len != -1 then
reserve(_size + len)
try
while n < len do
if iter.has_next() then
_ptr._update(_size + n, iter.next())
else
break
end
n = n + 1
end
end
_size = _size + n
else
try
while n < len do
if iter.has_next() then
push(iter.next())
else
break
end
n = n + 1
end
end
end
fun find(value: A!, offset: USize = 0, nth: USize = 0,
predicate: {(box->A!, box->A!): Bool} val =
{(l: box->A!, r: box->A!): Bool => l is r }): USize ?
=>
"""
Find the `nth` appearance of `value` from the beginning of the array,
starting at `offset` and examining higher indices, and using the supplied
`predicate` for comparisons. Returns the index of the value, or raise an
error if the value isn't present.
By default, the search starts at the first element of the array, returns the
first instance of `value` found, and uses object identity for comparison.
"""
var i = offset
var n = USize(0)
while i < _size do
if predicate(_ptr._apply(i), value) then
if n == nth then
return i
end
n = n + 1
end
i = i + 1
end
error
fun contains(value: A!, predicate: {(box->A!, box->A!): Bool} val =
{(l: box->A!, r: box->A!): Bool => l is r }): Bool =>
"""
Returns true if the array contains `value`, false otherwise.
"""
var i = USize(0)
while i < _size do
if predicate(_ptr._apply(i), value) then
return true
end
i = i + 1
end
false
fun rfind(value: A!, offset: USize = -1, nth: USize = 0,
predicate: {(box->A!, box->A!): Bool} val =
{(l: box->A!, r: box->A!): Bool => l is r }): USize ?
=>
"""
Find the `nth` appearance of `value` from the end of the array, starting at
`offset` and examining lower indices, and using the supplied `predicate` for
comparisons. Returns the index of the value, or raise an error if the value
isn't present.
By default, the search starts at the last element of the array, returns the
first instance of `value` found, and uses object identity for comparison.
"""
if _size > 0 then
var i = if offset >= _size then _size - 1 else offset end
var n = USize(0)
repeat
if predicate(_ptr._apply(i), value) then
if n == nth then
return i
end
n = n + 1
end
until (i = i - 1) == 0 end
end
error
fun clone(): Array[this->A!]^ =>
"""
Clone the array.
The new array contains references to the same elements that the old array
contains, the elements themselves are not cloned.
"""
let out = Array[this->A!](_size)
_ptr._copy_to(out._ptr, _size)
out._size = _size
out
fun slice(from: USize = 0, to: USize = -1, step: USize = 1)
: Array[this->A!]^
=>
"""
Create a new array that is a clone of a portion of this array. The range is
exclusive and saturated.
The new array contains references to the same elements that the old array
contains, the elements themselves are not cloned.
"""
let out = Array[this->A!]
let last = _size.min(to)
let len = last - from
if (last > from) and (step > 0) then
out.reserve((len + (step - 1)) / step)
if step == 1 then
copy_to(out, from, 0, len)
else
try
var i = from
while i < last do
out.push(this(i))
i = i + step
end
end
end
end
out
fun permute(indices: Iterator[USize]): Array[this->A!]^ ? =>
"""
Create a new array with the elements permuted.
Permute to an arbitrary order that may include duplicates. An out of bounds
index raises an error.
The new array contains references to the same elements that the old array
contains, the elements themselves are not copied.
"""
let out = Array[this->A!]
for i in indices do
out.push(this(i))
end
out
fun reverse(): Array[this->A!]^ =>
"""
Create a new array with the elements in reverse order.
The new array contains references to the same elements that the old array
contains, the elements themselves are not copied.
"""
clone().>reverse_in_place()
fun ref reverse_in_place() =>
"""
Reverse the array in place.
"""
if _size > 1 then
var i: USize = 0
var j = _size - 1
while i < j do
let x = _ptr._apply(i)
_ptr._update(i, _ptr._apply(j))
_ptr._update(j, x)
i = i + 1
j = j - 1
end
end
fun keys(): ArrayKeys[A, this->Array[A]]^ =>
"""
Return an iterator over the indices in the array.
"""
ArrayKeys[A, this->Array[A]](this)
fun values(): ArrayValues[A, this->Array[A]]^ =>
"""
Return an iterator over the values in the array.
"""
ArrayValues[A, this->Array[A]](this)
fun pairs(): ArrayPairs[A, this->Array[A]]^ =>
"""
Return an iterator over the (index, value) pairs in the array.
"""
ArrayPairs[A, this->Array[A]](this)
class ArrayKeys[A, B: Array[A] #read] is Iterator[USize]
let _array: B
var _i: USize
new create(array: B) =>
_array = array
_i = 0
fun has_next(): Bool =>
_i < _array.size()
fun ref next(): USize =>
if _i < _array.size() then
_i = _i + 1
else
_i
end
class ArrayValues[A, B: Array[A] #read] is Iterator[B->A]
let _array: B
var _i: USize
new create(array: B) =>
_array = array
_i = 0
fun has_next(): Bool =>
_i < _array.size()
fun ref next(): B->A ? =>
_array(_i = _i + 1)
fun ref rewind(): ArrayValues[A, B] =>
_i = 0
this
class ArrayPairs[A, B: Array[A] #read] is Iterator[(USize, B->A)]
let _array: B
var _i: USize
new create(array: B) =>
_array = array
_i = 0
fun has_next(): Bool =>
_i < _array.size()
fun ref next(): (USize, B->A) ? =>
(_i, _array(_i = _i + 1))