-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslabmap.go
230 lines (203 loc) · 4.59 KB
/
slabmap.go
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
package slabmap
type SlabMap[T any] struct {
entries []*entry[T]
nextVacantIdx int
len int
nonOptimized int
}
type entryTag uint8
const invalidIndex = int(^uint(0) >> 1)
const (
_ entryTag = iota
entryTagOccupied
entryTagVacantHead
entryTagVacantTail
)
type entry[T any] struct {
tag entryTag
value T
vacantBodyLen int
nextVacantIdx int
}
func (e *entry[T]) isOccupied() bool {
return e.tag == entryTagOccupied
}
func occupied[T any](value T) *entry[T] {
return &entry[T]{
tag: entryTagOccupied,
value: value,
}
}
func vacantHead[T any](vacantBodyLen int) *entry[T] {
return &entry[T]{
tag: entryTagVacantHead,
vacantBodyLen: vacantBodyLen,
}
}
func vacantTail[T any](nextVacantIdx int) *entry[T] {
return &entry[T]{
tag: entryTagVacantTail,
nextVacantIdx: nextVacantIdx,
}
}
func NewSlabMap[T any]() *SlabMap[T] {
return NewSlabMapWithCapacity[T](0)
}
func NewSlabMapWithCapacity[T any](capacity int) *SlabMap[T] {
return &SlabMap[T]{
entries: make([]*entry[T], 0, capacity),
nextVacantIdx: invalidIndex,
len: 0,
nonOptimized: 0,
}
}
func (m *SlabMap[T]) Capacity() int {
return cap(m.entries)
}
func (m *SlabMap[T]) Reserve(additional int) {
entries := make([]*entry[T], 0, len(m.entries)+m.entriesAdditional(additional))
for _, entry := range m.entries {
entries = append(entries, entry)
}
m.entries = entries
}
func (m *SlabMap[T]) Len() int {
return m.len
}
func (m *SlabMap[T]) Get(key int) (value T, exists bool) {
if key < 0 || key >= len(m.entries) {
return value, false
}
entry := m.entries[key]
if entry.isOccupied() {
return entry.value, true
}
return value, false
}
func (m *SlabMap[T]) Contains(key int) bool {
_, exists := m.Get(key)
return exists
}
func (m *SlabMap[T]) Insert(value T) int {
return m.InsertWithKey(func(int) T { return value })
}
func (m *SlabMap[T]) InsertWithKey(f func(int) T) int {
var idx int
if m.nextVacantIdx < len(m.entries) {
idx = m.nextVacantIdx
current := m.entries[idx]
switch current.tag {
case entryTagVacantHead:
if current.vacantBodyLen > 0 {
m.entries[idx+1] = vacantHead[T](current.vacantBodyLen - 1)
}
m.nextVacantIdx = idx + 1
case entryTagVacantTail:
m.nextVacantIdx = current.nextVacantIdx
default:
// unreachable
}
m.entries[idx] = occupied(f(idx))
m.nonOptimized = saturatingSub(m.nonOptimized, 1, 0)
} else {
idx = len(m.entries)
m.entries = append(m.entries, occupied(f(idx)))
}
m.len++
return idx
}
func (m *SlabMap[T]) Remove(key int) (value T, removed bool) {
if key < 0 || key >= len(m.entries) {
return value, false
}
isLast := (key+1 == len(m.entries))
current := m.entries[key]
if !current.isOccupied() {
return value, false
}
m.len--
if isLast {
m.entries = m.entries[0 : len(m.entries)-1]
} else {
m.entries[key] = vacantTail[T](m.nextVacantIdx)
m.nextVacantIdx = key
m.nonOptimized++
}
if m.Len() == 0 {
m.Clear()
}
return current.value, true
}
func (m *SlabMap[T]) Clear() {
for i := 0; i < len(m.entries); i++ {
m.entries[i] = nil
}
m.len = 0
m.nextVacantIdx = invalidIndex
m.nonOptimized = 0
}
func (m *SlabMap[T]) Retain(f func(int, T) bool) {
idxVacantStart := 0
m.nextVacantIdx = invalidIndex
for idx := 0; idx < len(m.entries); {
current := m.entries[idx]
switch current.tag {
case entryTagVacantTail:
idx++
case entryTagVacantHead:
idx += current.vacantBodyLen + 2
case entryTagOccupied:
if f(idx, current.value) {
m.mergeVacant(idxVacantStart, idx)
idx++
idxVacantStart = idx
} else {
current.tag = entryTagVacantTail
current.nextVacantIdx = invalidIndex
idx++
}
default:
// unreachable
}
}
m.entries = m.entries[0:idxVacantStart]
m.nonOptimized = 0
}
func (m *SlabMap[T]) Optimize() {
if !m.isOptimized() {
m.Retain(func(int, T) bool { return true })
}
}
func (m *SlabMap[T]) Range(f func(int, T) bool) {
for idx := 0; idx < len(m.entries); idx++ {
current := m.entries[idx]
if !current.isOccupied() {
continue
}
if !f(idx, current.value) {
break
}
}
}
func (m *SlabMap[T]) entriesAdditional(additional int) int {
return saturatingSub(additional, len(m.entries)-m.len, 0)
}
func (m *SlabMap[T]) isOptimized() bool {
return m.nonOptimized == 0
}
func (m *SlabMap[T]) mergeVacant(start, end int) {
if start < end {
if start < end-1 {
m.entries[start] = vacantHead[T](end - start - 2)
}
m.entries[end-1] = vacantTail[T](m.nextVacantIdx)
m.nextVacantIdx = start
}
}
func saturatingSub(val int, sub int, low int) int {
res := val - sub
if res > low {
return res
}
return low
}