-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.odin
138 lines (117 loc) · 4.11 KB
/
array.odin
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
package sds
import "base:builtin"
import "base:runtime"
/*
Static array with dynamic length
Based on core:container/small_array.
Usage is similar to `[dynamic]T`
*/
Array :: struct($Num: i32, $Val: typeid) where Num >= 0 {
data: [Num]Val,
len: i32,
invalid_value: Val,
}
@(require_results)
array_has_index :: #force_inline proc "contextless" (a: $A/Array, #any_int index: int) -> bool {
return index >= 0 && index < int(a.len)
}
@(require_results)
array_slice :: #force_inline proc "contextless" (a: ^$A/Array($N, $T)) -> []T {
return a.data[:a.len]
}
@(require_results)
array_get :: #force_inline proc "contextless" (a: $A/Array($N, $T), #any_int index: int, loc := #caller_location) -> T {
runtime.bounds_check_error_loc(loc, index, int(a.len))
return a.data[index]
}
@(require_results)
array_get_safe :: proc "contextless" (a: $A/Array($N, $T), #any_int index: int) -> (T, bool) #optional_ok {
if index < 0 || index >= int(a.len) {
return {}, false
}
return a.data[index], true
}
@(require_results)
array_get_ptr :: #force_inline proc "contextless" (a: ^$A/Array($N, $T), #any_int index: int, loc := #caller_location) -> ^T {
runtime.bounds_check_error_loc(loc, index, int(a.len))
return &a.data[index]
}
@(require_results)
array_get_ptr_safe :: proc "contextless" (a: ^$A/Array($N, $T), #any_int index: int) -> (^T, bool) #optional_ok {
if index < 0 || index >= int(a.len) {
return &a.invalid_value, false
}
return &a.data[index], true
}
array_set :: #force_inline proc "contextless" (a: ^$A/Array($N, $T), #any_int index: int, value: T, loc := #caller_location) {
runtime.bounds_check_error_loc(loc, index, int(a.len))
a.data[index] = value
}
array_set_safe :: proc "contextless" (a: ^$A/Array($N, $T), #any_int index: int, value: T) -> bool {
if index < 0 || index >= int(a.len) {
return false
}
a.data[index] = value
return true
}
// Returns index of the pushed value
array_push :: proc "contextless" (a: ^$A/Array($N, $T), item: T, loc := #caller_location) -> int {
assert_contextless(a.len < i32(N), "Reached the array size limit", loc)
index := a.len
a.data[index] = item
a.len += 1
return int(index)
}
array_push_safe :: proc "contextless" (a: ^$A/Array($N, $T), item: T) -> (index: int, ok: bool) #optional_ok {
index = array_push_empty(a) or_return
a.data[index] = item
return index, true
}
// Warning: doesn't clear previous value!
@(require_results)
array_push_empty :: proc "contextless" (a: ^$A/Array($N, $T)) -> (index: int, ok: bool) #optional_ok {
if a.len >= N {
return 0, false
}
index = int(a.len)
a.len += 1
return index, true
}
array_push_elems :: proc "contextless" (a: ^$A/Array($N, $T), elems: ..T, loc := #caller_location) {
n := copy(a.data[a.len:], elems[:])
a.len += i32(n)
assert_contextless(n == len(elems), "Not enough space in the array", loc)
}
array_push_elems_safe :: proc "contextless" (a: ^$A/Array($N, $T), elems: ..T) -> bool {
n := copy(a.data[a.len:], elems[:])
a.len += n
return n == len(elems)
}
@(require_results)
array_pop_back :: proc "contextless" (a: ^$A/Array($N, $T), loc := #caller_location) -> T {
assert_contextless(a.len > 0, "Array is empty", loc)
item := a.data[a.len - 1]
a.len -= 1
return item
}
@(require_results)
array_pop_back_safe :: proc "contextless" (a: ^$A/Array($N, $T)) -> (item: T, ok: bool) #optional_ok {
if a.len <= 0 {
return {}, false
}
item = a.data[a.len - 1]
a.len -= 1
return item, true
}
array_remove :: proc "contextless" (a: ^$A/Array($N, $T), #any_int index: int, loc := #caller_location) {
runtime.bounds_check_error_loc(loc, index, int(a.len))
n := a.len - 1
if index != int(n) {
a.data[index] = a.data[n]
}
a.len -= 1
}
array_from_slice :: proc "contextless" (a: ^$A/Array($N, $T), data: []T) -> bool {
a.len = cast(i32)copy(a.data[:], data)
return int(a.len) == len(data)
}