-
Notifications
You must be signed in to change notification settings - Fork 0
/
flat.go
247 lines (204 loc) · 5.22 KB
/
flat.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
package hashmaps
import "fmt"
type fBucket[K comparable, V any] struct {
key K
value V
}
// Flat is a open addressing hash map implementation which uses linear probing
// as conflict resolution.
type Flat[K comparable, V any] struct {
buckets []fBucket[K, V]
empty K
hasher HashFn[K]
capMinus1 uintptr
length uintptr
}
// go:inline
func newfBucketArray[K comparable, V any](capacity uintptr, empty K) []fBucket[K, V] {
var (
buckets = make([]fBucket[K, V], capacity)
zero K
)
if zero != empty {
// need to "zero" the keys
for i := range buckets {
buckets[i].key = empty
}
}
return buckets
}
// NewFlat creates a new ready to use flat hash map.
//
// Note:
// This map has zero memory overhead per bucket and uses therefore
// the golang default variable initialization representation as tracking.
// This means in details a Get, Put or Remove call fails, if the key is:
// - 0 (int, uint, uint64, ...)
// - 0.0 (float32, float64)
// - "" (string)
func NewFlat[K comparable, V any]() *Flat[K, V] {
var empty K // uses default zero representation
return NewFlatWithHasher[K, V](empty, GetHasher[K]())
}
// NewFlatWithHasher constructs a new map with the given hasher.
// Furthermore the representation for a empty bucket can be set.
func NewFlatWithHasher[K comparable, V any](empty K, hasher HashFn[K]) *Flat[K, V] {
return &Flat[K, V]{
buckets: newfBucketArray[K, V](4, empty),
capMinus1: 3,
hasher: hasher,
empty: empty,
}
}
// Get returns the value stored for this key, or false if not found.
func (m *Flat[K, V]) Get(key K) (V, bool) {
if key == m.empty {
panic(fmt.Sprintf("key %v is same as empty %v", key, m.empty))
}
var (
hash = m.hasher(key)
idx = hash & m.capMinus1
v V
)
for m.buckets[idx].key != m.empty {
if m.buckets[idx].key == key {
return m.buckets[idx].value, true
}
// next index
idx = (idx + 1) & m.capMinus1
}
return v, false
}
func (m *Flat[K, V]) resize(n uintptr) {
newm := Flat[K, V]{
capMinus1: n - 1,
length: m.length,
empty: m.empty,
hasher: m.hasher,
buckets: newfBucketArray[K, V](n, m.empty),
}
for i := range m.buckets {
if m.buckets[i].key != m.empty {
newm.emplace(m.buckets[i].key, m.buckets[i].value)
}
}
m.capMinus1 = newm.capMinus1
m.buckets = newm.buckets
}
// emplace does not check if the key is already in.
func (m *Flat[K, V]) emplace(key K, val V) {
var (
hash = m.hasher(key)
idx = hash & m.capMinus1
)
for {
if m.buckets[idx].key == m.empty {
break
}
// next index
idx = (idx + 1) & m.capMinus1
}
// we have a position for emplacing
m.buckets[idx].key = key
m.buckets[idx].value = val
}
// Put maps the given key to the given value. If the key already exists its
// value will be overwritten with the new value.
func (m *Flat[K, V]) Put(key K, val V) bool {
if key == m.empty {
panic(fmt.Sprintf("key %v is same as empty %v", key, m.empty))
}
if m.length >= uintptr(cap(m.buckets))/2 {
m.resize(uintptr(cap(m.buckets)) * 2)
}
var (
hash = m.hasher(key)
idx = hash & m.capMinus1
)
for m.buckets[idx].key != m.empty {
if m.buckets[idx].key == key {
m.buckets[idx].value = val
return false
}
// next index
idx = (idx + 1) & m.capMinus1
}
m.buckets[idx].key = key
m.buckets[idx].value = val
m.length++
return true
}
// Remove removes the specified key-value pair from the map.
func (m *Flat[K, V]) Remove(key K) bool {
if key == m.empty {
panic(fmt.Sprintf("key %v is same as empty %v", key, m.empty))
}
var (
hash = m.hasher(key)
idx = hash & m.capMinus1
)
for m.buckets[idx].key != m.empty && !(m.buckets[idx].key == key) {
idx = (idx + 1) & m.capMinus1
}
if m.buckets[idx].key == m.empty {
return false
}
m.buckets[idx].key = m.empty
m.length--
for {
idx = (idx + 1) & m.capMinus1
if m.buckets[idx].key == m.empty {
break
}
k := m.buckets[idx].key
v := m.buckets[idx].value
m.buckets[idx].key = m.empty
m.emplace(k, v)
}
return true
}
// Reserve sets the number of buckets to the most appropriate to contain at least n elements.
// If n is lower than that, the function may have no effect.
func (m *Flat[K, V]) Reserve(n uintptr) {
newCap := uintptr(NextPowerOf2(uint64(2 * n)))
if uintptr(cap(m.buckets)) < newCap {
m.resize(newCap)
}
}
// Clear removes all key-value pairs from the map.
func (m *Flat[K, V]) Clear() {
for i := range m.buckets {
m.buckets[i].key = m.empty
}
m.length = 0
}
// Size returns the number of items in the map.
func (m *Flat[K, V]) Size() int {
return int(m.length)
}
// Load return the current load of the hash map.
func (m *Flat[K, V]) Load() float32 {
return float32(m.length) / float32(cap(m.buckets))
}
func (m *Flat[K, V]) Copy() *Flat[K, V] {
newM := &Flat[K, V]{
buckets: make([]fBucket[K, V], uintptr(cap(m.buckets))),
capMinus1: m.capMinus1,
length: m.length,
hasher: m.hasher,
empty: m.empty,
}
copy(newM.buckets, m.buckets)
return newM
}
// Each calls 'fn' on every key-value pair in the hashmap in no particular order.
func (m *Flat[K, V]) Each(fn func(key K, val V) bool) {
for i := range m.buckets {
if m.buckets[i].key != m.empty {
if stop := fn(m.buckets[i].key, m.buckets[i].value); stop {
// stop iteration
return
}
}
}
}