-
Notifications
You must be signed in to change notification settings - Fork 4
/
arr_set.go
283 lines (271 loc) · 8.6 KB
/
arr_set.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
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
// The MIT License (MIT)
//
// Copyright (c) 2022 West Damron
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package amt
import (
"hash/maphash"
"math/bits"
"unsafe"
)
// ArrSet contains a set of byte arrays. Methods on a set value will panic if
// the set is not initialized. A set value is safe to copy.
type ArrSet[K ArrKey] struct {
*root
}
// NewArrSet returns an initialized set. The set value is safe to copy.
func NewArrSet[K ArrKey]() ArrSet[K] {
return ArrSet[K]{newRoot()}
}
// Nil returns true if s is not initialized.
func (s ArrSet[K]) Nil() bool { return s.root == nil }
// Len returns the number of keys in s. If s is not initialized, Len returns 0.
func (s ArrSet[K]) Len() uint { return s.root.Len() }
// Dep returns the average (mean) depth of all keys in s.
// If s is not initialized, Dep returns 0.
func (s ArrSet[K]) Dep() float64 { return s.root.Dep() }
// Has returns true if s contains key.
func (s ArrSet[K]) Has(key K) bool {
kb := key.KeyBytes()
var hw maphash.Hash
hw.SetSeed(s.seed)
hw.Write(kb[:])
hd, l, d := hw.Sum64(), &s.link, uint8(0)
radix := uint8(hd & 0xF)
bit, idx := uint32(1)<<radix, uint8(bits.OnesCount32(l.pmap&^(^uint32(0)<<radix)))
for l.pmap&bit != 0 { // item present
item := (*link)(unsafe.Pointer(uintptr(l.ptr) + uintptr(idx)*linkSize))
if l.tmap&bit == 0 { // traverse branch
l = item
d++
if d%(64/4) != 0 { // hash bits available
hd >>= 4
} else { // rehash
hw.Write(kb[:])
hd = hw.Sum64()
}
radix = uint8(hd & 0xF)
bit, idx = 1<<radix, uint8(bits.OnesCount32(l.pmap&^(^uint32(0)<<radix)))
continue
}
return key == (*arrkv[K, struct{}])(item.ptr).k // key match/mismatch
}
return false // item missing
}
// Add adds key to s.
func (s ArrSet[K]) Add(key K) {
kb := key.KeyBytes()
var hw maphash.Hash
hw.SetSeed(s.seed)
hw.Write(kb[:])
hd, l, d := hw.Sum64(), &s.link, uint8(0)
radix := uint8(hd & 0xF)
bit, idx := uint32(1)<<radix, uint8(bits.OnesCount32(l.pmap&^(^uint32(0)<<radix)))
for l.pmap&bit != 0 { // item present
item := (*link)(unsafe.Pointer(uintptr(l.ptr) + uintptr(idx)*linkSize))
if l.tmap&bit == 0 { // traverse branch
l = item
d++
if d%(64/4) != 0 { // hash bits available
hd >>= 4
} else { // rehash
hw.Write(kb[:])
hd = hw.Sum64()
}
radix = uint8(hd & 0xF)
bit, idx = 1<<radix, uint8(bits.OnesCount32(l.pmap&^(^uint32(0)<<radix)))
continue
}
ckv := (*arrkv[K, struct{}])(item.ptr)
ckey := ckv.k
if ckey == key { // exists
return
}
// rehash conflicting key
ckb := ckey.KeyBytes()
var chw maphash.Hash
chw.SetSeed(s.seed)
for cd := uint8(0); cd <= d; cd += (64 / 4) {
chw.Write(ckb[:])
}
chd := chw.Sum64() >> (4 * (d % (64 / 4)))
// replace with new branch until non-colliding
l.tmap &^= bit
s.dep -= uint64(d) // conflicting key depth
for {
d++
if d%(64/4) != 0 { // hash bits available
hd >>= 4
chd >>= 4
} else { // rehash keys
hw.Write(kb[:])
chw.Write(ckb[:])
hd, chd = hw.Sum64(), chw.Sum64()
}
kbit, cbit := uint32(1)<<uint8(hd&0xF), uint32(1)<<uint8(chd&0xF)
item.pmap = kbit | cbit
if kbit != cbit { // non-colliding
item.tmap = item.pmap
item.ptr = newLinkArray(2)
kv := &arrkv[K, struct{}]{k: key}
if pair := (*[2]link)(item.ptr); kbit < cbit {
pair[0].ptr, pair[1].ptr = unsafe.Pointer(kv), unsafe.Pointer(ckv)
} else {
pair[0].ptr, pair[1].ptr = unsafe.Pointer(ckv), unsafe.Pointer(kv)
}
s.len++
s.dep += uint64(d) * 2
return // key added
}
// handle collision at new level
item.ptr = newLinkArray(1)
item = (*link)(item.ptr)
}
}
count := uint8(bits.OnesCount32(l.pmap))
if (count != 0 && count%4 != 0) || d == 0 { // array slot available
for after := int(count) - 1; after >= int(idx); after-- {
*(*link)(unsafe.Pointer(uintptr(l.ptr) + uintptr(after+1)*linkSize)) =
*(*link)(unsafe.Pointer(uintptr(l.ptr) + uintptr(after)*linkSize))
}
*(*link)(unsafe.Pointer(uintptr(l.ptr) + uintptr(idx)*linkSize)) = link{
ptr: unsafe.Pointer(&arrkv[K, struct{}]{k: key}),
}
} else { // array full or empty
src := l.ptr
l.ptr = newLinkArray(count + 1)
for before := uint8(0); before < idx; before++ {
*(*link)(unsafe.Pointer(uintptr(l.ptr) + uintptr(before)*linkSize)) =
*(*link)(unsafe.Pointer(uintptr(src) + uintptr(before)*linkSize))
}
*(*link)(unsafe.Pointer(uintptr(l.ptr) + uintptr(idx)*linkSize)) = link{
ptr: unsafe.Pointer(&arrkv[K, struct{}]{k: key}),
}
for after := idx; after < count; after++ {
*(*link)(unsafe.Pointer(uintptr(l.ptr) + uintptr(after+1)*linkSize)) =
*(*link)(unsafe.Pointer(uintptr(src) + uintptr(after)*linkSize))
}
}
l.pmap |= bit
l.tmap |= bit
s.len++
s.dep += uint64(d)
}
// Del deletes key from s.
func (s ArrSet[K]) Del(key K) {
path := s.path[:0]
kb := key.KeyBytes()
var hw maphash.Hash
hw.SetSeed(s.seed)
hw.Write(kb[:])
hd, l, d := hw.Sum64(), &s.link, uint8(0)
radix := uint8(hd & 0xF)
bit, idx := uint32(1)<<radix, uint8(bits.OnesCount32(l.pmap&^(^uint32(0)<<radix)))
for l.pmap&bit != 0 { // item present
path = append(path, pathLink{radix, l})
item := (*link)(unsafe.Pointer(uintptr(l.ptr) + uintptr(idx)*linkSize))
if l.tmap&bit == 0 { // traverse branch
l = item
d++
if d%(64/4) != 0 { // hash bits available
hd >>= 4
} else { // rehash
hw.Write(kb[:])
hd = hw.Sum64()
}
radix = uint8(hd & 0xF)
bit, idx = 1<<radix, uint8(bits.OnesCount32(l.pmap&^(^uint32(0)<<radix)))
continue
}
if (*arrkv[K, struct{}])(item.ptr).k != key { // key missing
return
}
l.pmap &^= bit
l.tmap &^= bit
s.len--
s.dep -= uint64(d)
path[d].link = nil
count := uint8(bits.OnesCount32(l.pmap))
// unlink empty branches up to the root
for count == 0 && d != 0 {
l.ptr = nil
d--
l, radix = path[d].link, path[d].radix
path[d].link = nil
bit, idx = 1<<radix, uint8(bits.OnesCount32(l.pmap&^(^uint32(0)<<radix)))
l.pmap &^= bit
l.tmap &^= bit
count = uint8(bits.OnesCount32(l.pmap))
}
// shift items back
src := l.ptr
if count%4 == 0 && d != 0 { // copy all items when reallocating
l.ptr = newLinkArray(count)
for before := uint8(0); before < idx; before++ {
*(*link)(unsafe.Pointer(uintptr(l.ptr) + uintptr(before)*linkSize)) =
*(*link)(unsafe.Pointer(uintptr(src) + uintptr(before)*linkSize))
}
}
for after := idx; after < count; after++ {
*(*link)(unsafe.Pointer(uintptr(l.ptr) + uintptr(after)*linkSize)) =
*(*link)(unsafe.Pointer(uintptr(src) + uintptr(after+1)*linkSize))
}
// replace single-valued branches with key-values up to the root
for count == 1 && l.pmap == l.tmap && d != 0 {
*l = *(*link)(l.ptr)
s.dep--
d--
l, radix = path[d].link, path[d].radix
path[d].link = nil
l.tmap |= 1 << radix
count = uint8(bits.OnesCount32(l.pmap))
}
// clear the path to prevent leaks
for d != 0 {
d--
path[d].link = nil
}
return // item removed
}
}
// All ranges over keys in s, applying the do callback to each key until
// the callback returns false or all keys have been visited. The iteration order
// is not randomized for each call.
func (s ArrSet[K]) All(do func(K) bool) {
arrSetScan(&s.link, do)
}
func arrSetScan[K ArrKey](l *link, do func(K) bool) bool {
pmap, tmap := l.pmap, l.tmap
count := uint8(bits.OnesCount32(pmap))
for i := uint8(0); i < count; i++ {
bit := uint32(1) << uint8(bits.TrailingZeros32(pmap))
item := (*link)(unsafe.Pointer(uintptr(l.ptr) + uintptr(i)*linkSize))
if tmap&bit != 0 {
kv := (*arrkv[K, struct{}])(item.ptr)
if !do(kv.k) {
return false
}
} else if !arrSetScan(item, do) {
return false
}
pmap &^= bit
}
return true
}