-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmall_map.odin
More file actions
68 lines (55 loc) · 1.05 KB
/
Copy pathsmall_map.odin
File metadata and controls
68 lines (55 loc) · 1.05 KB
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
package small_map
import "base:intrinsics"
Small_Map :: struct($K, $V: typeid, $N: int) {
entries: #soa[N]struct {
key: K,
value: V,
},
len: int,
}
small_map_insert :: proc(sm: ^Small_Map($K, $V, $N), key: K, value: V) -> (ok: bool) {
for &e in sm.entries[:sm.len] {
if e.key == key {
e.value = value
return true
}
}
if sm.len >= N {
return
}
sm.entries[sm.len] = {
key = key,
value = value,
}
sm.len += 1
return true
}
small_map_remove :: proc(sm: ^Small_Map($K, $V, $N), key: K) -> (ok: bool) {
key := key
for &e, i in sm.entries[:sm.len] {
if e.key == key {
sm.len -= 1
sm.entries[i] = sm.entries[sm.len]
return true
}
}
return false
}
small_map_get :: proc(sm: ^$SM/Small_Map($K, $V, $N), key: K) -> (value: ^V) {
key := key
for &e in sm.entries[:sm.len] {
if e.key == key {
return &e.value
}
}
return nil
}
small_map_contains :: proc(sm: ^$SM/Small_Map($K, $V, $N), key: K) -> bool {
key := key
for e in sm.entries[:sm.len] {
if e.key == key {
return true
}
}
return false
}