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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
|
package slice
import "base:intrinsics"
import "base:builtin"
import "core:math/bits"
import "base:runtime"
_ :: intrinsics
_ :: builtin
_ :: bits
_ :: runtime
/*
Turn a pointer and a length into a slice.
*/
@(require_results)
from_ptr :: proc "contextless" (ptr: ^$T, count: int) -> []T {
return ([^]T)(ptr)[:count]
}
/*
Turn a pointer and a length into a byte slice.
*/
@(require_results)
bytes_from_ptr :: proc "contextless" (ptr: rawptr, byte_count: int) -> []byte {
return ([^]byte)(ptr)[:byte_count]
}
/*
Turn a slice into a byte slice.
See `slice.reinterpret` to go the other way.
*/
@(require_results)
to_bytes :: proc "contextless" (s: []$T) -> []byte {
return ([^]byte)(raw_data(s))[:len(s) * size_of(T)]
}
/*
Turn a slice of one type, into a slice of another type.
Only converts the type and length of the slice itself.
The length is rounded down to the nearest whole number of items.
```
large_items := []i64{1, 2, 3, 4}
small_items := slice.reinterpret([]i32, large_items)
assert(len(small_items) == 8)
```
```
small_items := []byte{1, 0, 0, 0, 0, 0, 0, 0,
2, 0, 0, 0}
large_items := slice.reinterpret([]i64, small_items)
assert(len(large_items) == 1) // only enough bytes to make 1 x i64; two would need at least 8 bytes.
```
*/
@(require_results)
reinterpret :: proc "contextless" ($T: typeid/[]$U, s: []$V) -> []U {
when size_of(U) == 0 || size_of(V) == 0 {
return nil
} else {
bytes := to_bytes(s)
n := len(bytes) / size_of(U)
return ([^]U)(raw_data(bytes))[:n]
}
}
swap :: proc(array: $T/[]$E, a, b: int) {
when size_of(E) > 8 {
ptr_swap_non_overlapping(&array[a], &array[b], size_of(E))
} else {
array[a], array[b] = array[b], array[a]
}
}
swap_between :: proc(a, b: $T/[]$E) {
n := builtin.min(len(a), len(b))
if n >= 0 {
ptr_swap_overlapping(&a[0], &b[0], size_of(E)*n)
}
}
reverse :: proc(array: $T/[]$E) {
n := len(array)/2
for i in 0..<n {
swap(array, i, len(array)-i-1)
}
}
@(require_results)
contains :: proc(array: $T/[]$E, value: E) -> bool where intrinsics.type_is_comparable(E) {
_, found := linear_search(array, value)
return found
}
@(require_results)
linear_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool)
where intrinsics.type_is_comparable(T) #no_bounds_check {
for x, i in array {
if x == key {
return i, true
}
}
return -1, false
}
@(require_results)
linear_search_proc :: proc(array: $A/[]$T, f: proc(T) -> bool) -> (index: int, found: bool) #no_bounds_check {
for x, i in array {
if f(x) {
return i, true
}
}
return -1, false
}
/*
Binary search searches the given slice for the given element.
If the slice is not sorted, the returned index is unspecified and meaningless.
If the value is found then the returned int is the index of the matching element.
If there are multiple matches, then any one of the matches could be returned.
If the value is not found then the returned int is the index where a matching
element could be inserted while maintaining sorted order.
# Examples
Looks up a series of four elements. The first is found, with a
uniquely determined position; the second and third are not
found; the fourth could match any position in `[1, 4]`.
```
index: int
found: bool
s := []i32{0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55}
index, found = slice.binary_search(s, 13)
assert(index == 9 && found == true)
index, found = slice.binary_search(s, 4)
assert(index == 7 && found == false)
index, found = slice.binary_search(s, 100)
assert(index == 13 && found == false)
index, found = slice.binary_search(s, 1)
assert(index >= 1 && index <= 4 && found == true)
```
For slices of more complex types see: binary_search_by
*/
@(require_results)
binary_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool)
where intrinsics.type_is_ordered(T) #no_bounds_check
{
return binary_search_by(array, key, cmp_proc(T))
}
@(require_results)
binary_search_by :: proc(array: $A/[]$T, key: T, f: proc(T, T) -> Ordering) -> (index: int, found: bool) #no_bounds_check {
n := len(array)
left, right := 0, n
for left < right {
mid := int(uint(left+right) >> 1)
if f(array[mid], key) == .Less {
left = mid+1
} else {
// .Equal or .Greater
right = mid
}
}
// left == right
// f(array[left-1], key) == .Less (if left > 0)
return left, left < n && f(array[left], key) == .Equal
}
@(require_results)
equal :: proc(a, b: $T/[]$E) -> bool where intrinsics.type_is_comparable(E) {
if len(a) != len(b) {
return false
}
when intrinsics.type_is_simple_compare(E) {
return runtime.memory_compare(raw_data(a), raw_data(b), len(a)*size_of(E)) == 0
} else {
for i in 0..<len(a) {
if a[i] != b[i] {
return false
}
}
return true
}
}
@(require_results)
simple_equal :: proc(a, b: $T/[]$E) -> bool where intrinsics.type_is_simple_compare(E) {
if len(a) != len(b) {
return false
}
return runtime.memory_compare(raw_data(a), raw_data(b), len(a)*size_of(E)) == 0
}
/*
return the prefix length common between slices `a` and `b`.
slice.prefix_length([]u8{1, 2, 3, 4}, []u8{1}) -> 1
slice.prefix_length([]u8{1, 2, 3, 4}, []u8{1, 2, 3}) -> 3
slice.prefix_length([]u8{1, 2, 3, 4}, []u8{2, 3, 4}) -> 0
*/
@(require_results)
prefix_length :: proc(a, b: $T/[]$E) -> (n: int) where intrinsics.type_is_comparable(E) {
_len := builtin.min(len(a), len(b))
#no_bounds_check for n < _len && a[n] == b[n] {
n += 1
}
return
}
@(require_results)
has_prefix :: proc(array: $T/[]$E, needle: E) -> bool where intrinsics.type_is_comparable(E) {
n := len(needle)
if len(array) >= n {
return equal(array[:n], needle)
}
return false
}
@(require_results)
has_suffix :: proc(array: $T/[]$E, needle: E) -> bool where intrinsics.type_is_comparable(E) {
array := array
m, n := len(array), len(needle)
if m >= n {
return equal(array[m-n:], needle)
}
return false
}
zero :: proc(array: $T/[]$E) #no_bounds_check {
if len(array) > 0 {
intrinsics.mem_zero(raw_data(array), size_of(E)*len(array))
}
}
fill :: proc(array: $T/[]$E, value: E) #no_bounds_check {
if len(array) <= 0 {
return
}
array[0] = value
for i := 1; i < len(array); i *= 2 {
copy(array[i:], array[:i])
}
}
rotate_left :: proc(array: $T/[]$E, mid: int) {
n := len(array)
m := mid %% n
k := n - m
// FIXME: (ap29600) this cast is a temporary fix for the compiler not matching
// [^T] with $P/^$T
p := cast(^E)raw_data(array)
ptr_rotate(m, ptr_add(p, m), k)
}
rotate_right :: proc(array: $T/[]$E, k: int) {
rotate_left(array, -k)
}
swap_with_slice :: proc(a, b: $T/[]$E, loc := #caller_location) {
assert(len(a) == len(b), "miss matching slice lengths", loc)
ptr_swap_non_overlapping(raw_data(a), raw_data(b), len(a)*size_of(E))
}
@(require_results)
concatenate :: proc(a: []$T/[]$E, allocator := context.allocator) -> (res: T, err: runtime.Allocator_Error) #optional_allocator_error {
if len(a) == 0 {
return
}
n := 0
for s in a {
n += len(s)
}
res = make(T, n, allocator) or_return
i := 0
for s in a {
i += copy(res[i:], s)
}
return
}
// copies a slice into a new slice
@(require_results)
clone :: proc(a: $T/[]$E, allocator := context.allocator, loc := #caller_location) -> ([]E, runtime.Allocator_Error) #optional_allocator_error {
d, err := make([]E, len(a), allocator, loc)
copy(d[:], a)
return d, err
}
// copies slice into a new dynamic array
clone_to_dynamic :: proc(a: $T/[]$E, allocator := context.allocator, loc := #caller_location) -> ([dynamic]E, runtime.Allocator_Error) #optional_allocator_error {
d, err := make([dynamic]E, len(a), allocator, loc)
copy(d[:], a)
return d, err
}
to_dynamic :: clone_to_dynamic
// Converts slice into a dynamic array without cloning or allocating memory
@(require_results)
into_dynamic :: proc(a: $T/[]$E) -> [dynamic]E {
s := transmute(runtime.Raw_Slice)a
d := runtime.Raw_Dynamic_Array{
data = s.data,
len = 0,
cap = s.len,
allocator = runtime.nil_allocator(),
}
return transmute([dynamic]E)d
}
@(require_results)
length :: proc(a: $T/[]$E) -> int {
return len(a)
}
@(require_results)
is_empty :: proc(a: $T/[]$E) -> bool {
return len(a) == 0
}
@(require_results)
split_at :: proc(array: $T/[]$E, index: int) -> (a, b: T) {
return array[:index], array[index:]
}
@(require_results)
split_first :: proc(array: $T/[]$E) -> (first: E, rest: T) {
return array[0], array[1:]
}
@(require_results)
split_last :: proc(array: $T/[]$E) -> (rest: T, last: E) {
n := len(array)-1
return array[:n], array[n]
}
@(require_results)
first :: proc(array: $T/[]$E) -> E {
return array[0]
}
@(require_results)
last :: proc(array: $T/[]$E) -> E {
return array[len(array)-1]
}
@(require_results)
first_ptr :: proc(array: $T/[]$E) -> ^E {
if len(array) != 0 {
return &array[0]
}
return nil
}
@(require_results)
last_ptr :: proc(array: $T/[]$E) -> ^E {
if len(array) != 0 {
return &array[len(array)-1]
}
return nil
}
@(require_results)
get :: proc(array: $T/[]$E, index: int) -> (value: E, ok: bool) {
if uint(index) < len(array) {
value = array[index]
ok = true
}
return
}
@(require_results)
get_ptr :: proc(array: $T/[]$E, index: int) -> (value: ^E, ok: bool) {
if uint(index) < len(array) {
value = &array[index]
ok = true
}
return
}
@(require_results)
as_ptr :: proc(array: $T/[]$E) -> [^]E {
return raw_data(array)
}
@(require_results)
mapper :: proc(s: $S/[]$U, f: proc(U) -> $V, allocator := context.allocator) -> (r: []V, err: runtime.Allocator_Error) #optional_allocator_error {
r = make([]V, len(s), allocator) or_return
for v, i in s {
r[i] = f(v)
}
return
}
@(require_results)
reduce :: proc(s: $S/[]$U, initializer: $V, f: proc(V, U) -> V) -> V {
r := initializer
for v in s {
r = f(r, v)
}
return r
}
@(require_results)
reduce_reverse :: proc(s: $S/[]$U, initializer: $V, f: proc(V, U) -> V) -> V {
r := initializer
for i := len(s)-1; i >= 0; i -= 1 {
#no_bounds_check r = f(r, s[i])
}
return r
}
@(require_results)
filter :: proc(s: $S/[]$U, f: proc(U) -> bool, allocator := context.allocator) -> (res: S, err: runtime.Allocator_Error) #optional_allocator_error {
r := make([dynamic]U, 0, 0, allocator) or_return
for v in s {
if f(v) {
append(&r, v)
}
}
return r[:], nil
}
@(require_results)
filter_reverse :: proc(s: $S/[]$U, f: proc(U) -> bool, allocator := context.allocator) -> (res: S, err: runtime.Allocator_Error) #optional_allocator_error {
r := make([dynamic]U, 0, 0, allocator) or_return
for i := len(s)-1; i >= 0; i -= 1 {
#no_bounds_check v := s[i]
if f(v) {
append(&r, v)
}
}
return r[:], nil
}
@(require_results)
scanner :: proc (s: $S/[]$U, initializer: $V, f: proc(V, U) -> V, allocator := context.allocator) -> (res: []V, err: runtime.Allocator_Error) #optional_allocator_error {
if len(s) == 0 { return }
res = make([]V, len(s), allocator) or_return
p := as_ptr(s)
q := as_ptr(res)
r := initializer
for l := len(s); l > 0; l -= 1 {
r = f(r, p[0])
q[0] = r
p = p[1:]
q = q[1:]
}
return
}
@(require_results)
repeat :: proc(s: $S/[]$U, count: int, allocator := context.allocator) -> (b: S, err: runtime.Allocator_Error) #optional_allocator_error {
if count < 0 {
panic("slice: negative repeat count")
} else if count > 0 && (len(s)*count)/count != len(s) {
panic("slice: repeat count will cause an overflow")
}
b = make(S, len(s)*count, allocator) or_return
i := copy(b, s)
for i < len(b) { // 2^N trick to reduce the need to copy
copy(b[i:], b[:i])
i *= 2
}
return
}
// 'unique' replaces consecutive runs of equal elements with a single copy.
// The procedures modifies the slice in-place and returns the modified slice.
@(require_results)
unique :: proc(s: $S/[]$T) -> S where intrinsics.type_is_comparable(T) #no_bounds_check {
if len(s) < 2 {
return s
}
i := 1
for j in 1..<len(s) {
if s[j] != s[j-1] && i != j {
s[i] = s[j]
i += 1
}
}
return s[:i]
}
// 'unique_proc' replaces consecutive runs of equal elements with a single copy using a comparison procedure
// The procedures modifies the slice in-place and returns the modified slice.
@(require_results)
unique_proc :: proc(s: $S/[]$T, eq: proc(T, T) -> bool) -> S #no_bounds_check {
if len(s) < 2 {
return s
}
i := 1
for j in 1..<len(s) {
if !eq(s[j], s[j-1]) && i != j {
s[i] = s[j]
i += 1
}
}
return s[:i]
}
@(require_results)
min :: proc(s: $S/[]$T) -> (res: T, ok: bool) where intrinsics.type_is_ordered(T) #optional_ok {
if len(s) != 0 {
res = s[0]
ok = true
for v in s[1:] {
res = builtin.min(res, v)
}
}
return
}
@(require_results)
max :: proc(s: $S/[]$T) -> (res: T, ok: bool) where intrinsics.type_is_ordered(T) #optional_ok {
if len(s) != 0 {
res = s[0]
ok = true
for v in s[1:] {
res = builtin.max(res, v)
}
}
return
}
@(require_results)
min_max :: proc(s: $S/[]$T) -> (min, max: T, ok: bool) where intrinsics.type_is_ordered(T) {
if len(s) != 0 {
min, max = s[0], s[0]
ok = true
for v in s[1:] {
min = builtin.min(min, v)
max = builtin.max(max, v)
}
}
return
}
// Find the index of the (first) minimum element in a slice.
@(require_results)
min_index :: proc(s: $S/[]$T) -> (min_index: int, ok: bool) where intrinsics.type_is_ordered(T) #optional_ok {
if len(s) == 0 {
return -1, false
}
min_index = 0
min_value := s[0]
for v, i in s[1:] {
if v < min_value {
min_value = v
min_index = i+1
}
}
return min_index, true
}
// Find the index of the (first) maximum element in a slice.
@(require_results)
max_index :: proc(s: $S/[]$T) -> (max_index: int, ok: bool) where intrinsics.type_is_ordered(T) #optional_ok {
if len(s) == 0 {
return -1, false
}
max_index = 0
max_value := s[0]
for v, i in s[1:] {
if v > max_value {
max_value = v
max_index = i+1
}
}
return max_index, true
}
@(require_results)
any_of :: proc(s: $S/[]$T, value: T) -> bool where intrinsics.type_is_comparable(T) {
for v in s {
if v == value {
return true
}
}
return false
}
@(require_results)
none_of :: proc(s: $S/[]$T, value: T) -> bool where intrinsics.type_is_comparable(T) {
for v in s {
if v == value {
return false
}
}
return true
}
@(require_results)
all_of :: proc(s: $S/[]$T, value: T) -> bool where intrinsics.type_is_comparable(T) {
if len(s) == 0 {
return false
}
for v in s {
if v != value {
return false
}
}
return true
}
@(require_results)
any_of_proc :: proc(s: $S/[]$T, f: proc(T) -> bool) -> bool {
for v in s {
if f(v) {
return true
}
}
return false
}
@(require_results)
none_of_proc :: proc(s: $S/[]$T, f: proc(T) -> bool) -> bool {
for v in s {
if f(v) {
return false
}
}
return true
}
@(require_results)
all_of_proc :: proc(s: $S/[]$T, f: proc(T) -> bool) -> bool {
if len(s) == 0 {
return false
}
for v in s {
if !f(v) {
return false
}
}
return true
}
@(require_results)
count :: proc(s: $S/[]$T, value: T) -> (n: int) where intrinsics.type_is_comparable(T) {
for v in s {
if v == value {
n += 1
}
}
return
}
@(require_results)
count_proc :: proc(s: $S/[]$T, f: proc(T) -> bool) -> (n: int) {
for v in s {
if f(v) {
n += 1
}
}
return
}
@(require_results)
dot_product :: proc(a, b: $S/[]$T) -> (r: T, ok: bool)
where intrinsics.type_is_numeric(T) {
if len(a) != len(b) {
return
}
#no_bounds_check for _, i in a {
r += a[i] * b[i]
}
return r, true
}
// Convert a pointer to an enumerated array to a slice of the element type
@(require_results)
enumerated_array :: proc(ptr: ^$T) -> []intrinsics.type_elem_type(T)
where intrinsics.type_is_enumerated_array(T) {
return ([^]intrinsics.type_elem_type(T))(ptr)[:len(T)]
}
|