-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.odin
181 lines (148 loc) · 5.57 KB
/
pool.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
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
package sds
import "base:intrinsics"
import "base:runtime"
/*
Pool (sparse buffer with a free list)
Slots of removed items are kept in a free list and reused later.
All operations are O(1).
Index 0 is for "invalid value".
Iterate with something like this:
```odin
for i in 1..=my_pool.max_index {
ptr, handle := sds.pool_index_get_ptr_safe(&my_pool, i) or_continue
// Alternatively use sds.pool_index_get_safe when iterating by value
// ...
}
```
Note: the reason why this takes a Handle parameter instead of the Index and Gen directly is
to better support distinct handle types, even in return values etc.
*/
Pool :: struct($Num: int, $Val: typeid, $Handle: typeid) where Num > 0{
max_index: i32,
first_free: i32,
// Indexes:
// zero = never assigned, invalid
// max(Handle_Index) = slot is currently used
// other (1..<N) = used in a free list of unused slots.
gen_indexes: #soa[Num]Handle,
data: [Num]Val,
}
pool_clear :: proc "contextless" (p: ^$T/Pool($N, $V, $H)) {
p.first_free = 0
p.max_index = 0
intrinsics.mem_zero(&p.gen_indexes, size_of(p.gen_indexes))
}
// Warning: doesn't clear previous value!
@(require_results)
pool_push_empty :: proc "contextless" (p: ^$T/Pool($N, $V, $H), loc := #caller_location) -> (handle: H, ok: bool) #optional_ok {
index := p.first_free
// Eclude zero index!
if index > 0 && int(index) < N {
// get slot from the free list
p.first_free = auto_cast p.gen_indexes[index].index
} else {
// push to the end
if p.max_index < 0 || int(p.max_index) >= N - 1 {
return {}, false
}
p.max_index += 1
index = p.max_index
}
p.gen_indexes[index].index = max(intrinsics.type_field_type(H, "index"))
return {index = auto_cast index, gen = p.gen_indexes[index].gen}, true
}
pool_push :: proc "contextless" (p: ^$T/Pool($N, $V, $H), value: V, loc := #caller_location) -> (handle: H, ok: bool) #optional_ok {
handle = pool_push_empty(p, loc) or_return
p.data[handle.index] = value
return handle, true
}
pool_remove :: proc "contextless" (p: ^$T/Pool($N, $V, $H), handle: H, loc := #caller_location) -> (V, bool) #optional_ok {
if handle.index <= 0 || int(handle.index) >= N {
return {}, false
}
when size_of(handle.gen) > 0 {
if p.gen_indexes[handle.index].gen != handle.gen {
return {}, false
}
p.gen_indexes[handle.index].gen += 1
}
removed_value := p.data[handle.index]
p.gen_indexes[handle.index].index = auto_cast p.first_free
p.first_free = auto_cast handle.index
return removed_value, true
}
@(require_results)
pool_index_is_valid :: #force_inline proc "contextless" (p: $T/Pool($N, $V, $H), #any_int index: int) -> bool {
return index > 0 || index < N
}
@(require_results)
pool_has_index :: proc "contextless" (p: $T/Pool($N, $V, $H), #any_int index: int) -> bool {
if index <= 0 || index >= N {
return false
}
return p.gen_indexes[index].index == max(intrinsics.type_field_type(H, "index"))
}
@(require_results)
pool_has_handle :: proc "contextless" (p: $T/Pool($N, $V, $H), handle: H) -> bool {
if !pool_has_index(p, handle.index) {
return false
}
// generation check
when size_of(handle.gen) > 0 {
if p.gen_indexes[handle.index].gen != handle.gen {
return false
}
}
return true
}
@(require_results)
pool_get :: #force_inline proc "contextless" (p: $T/Pool($N, $V, $H), handle: H, loc := #caller_location) -> V {
assert_contextless(pool_has_handle(p, handle), "Pool doesn't contain the handle", loc)
return p.data[handle.index]
}
@(require_results)
pool_get_safe :: proc "contextless" (p: $T/Pool($N, $V, $H), handle: H) -> (V, bool) #optional_ok {
if !pool_has_handle(p, handle) {
return {}, false
}
return p.data[handle.index], true
}
@(require_results)
pool_get_ptr :: #force_inline proc "contextless" (p: ^$T/Pool($N, $V, $H), handle: H, loc := #caller_location) -> ^V {
assert_contextless(pool_has_handle(p^, handle), "Pool doesn't contain the handle", loc)
return &p.data[handle.index]
}
@(require_results)
pool_get_ptr_safe :: proc "contextless" (p: ^$T/Pool($N, $V, $H), handle: H) -> (^V, bool) #optional_ok {
if !pool_has_handle(p^, handle) {
return &p.data[0], false
}
return &p.data[handle.index], true
}
// For iteration
@(require_results)
pool_index_get_safe :: proc "contextless" (p: $T/Pool($N, $V, $H), #any_int index: int) -> (V, H, bool) {
if !pool_has_index(p, index) {
return {}, {}, false
}
return p.data[index], {auto_cast index, p.gen_indexes[index].gen}, true
}
// For iteration
@(require_results)
pool_index_get_ptr_safe :: proc "contextless" (p: ^$T/Pool($N, $V, $H), #any_int index: int) -> (^V, H, bool) {
if !pool_has_index(p^, index) {
return &p.data[0], {}, false
}
return &p.data[index], {auto_cast index, p.gen_indexes[index].gen}, true
}
pool_set :: #force_inline proc "contextless" (p: ^$T/Pool($N, $V, $H), handle: H, value: V, loc := #caller_location) {
assert_contextless(pool_has_handle(p^, handle), "Pool doesn't contain the handle", loc)
p.data[handle.index] = value
}
pool_set_safe :: proc "contextless" (p: ^$T/Pool($N, $V, $H), handle: H, value: V) -> bool {
if !pool_has_handle(p^, handle) {
return false
}
p.data[handle.index] = value
return true
}