-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunordered.go
218 lines (180 loc) · 5.42 KB
/
unordered.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
package hashmaps
type linkedList[K comparable, V any] struct {
head *listNode[K, V]
}
type listNode[K comparable, V any] struct {
next *listNode[K, V]
key K
value V
}
// Unordered is a hash map implementation, where the elements are organized into buckets
// depending on their hash values. Collisions are chained in a single linked list.
// An inserted value keeps its memory address, means a element in a bucket will not copied
// or swapped. That supports holding points instead of copy by value. see: `Insert` and `lookup`.
type Unordered[K comparable, V any] struct {
buckets []linkedList[K, V]
hasher HashFn[K]
// length stores the current inserted elements
length uintptr
// capMinus1 is used for a bitwise AND on the hash value,
// because the size of the underlying array is a power of two value
capMinus1 uintptr
}
// NewUnordered creates a ready to use `unordered` hash map with default settings.
func NewUnordered[K comparable, V any]() *Unordered[K, V] {
return NewUnorderedWithHasher[K, V](GetHasher[K]())
}
// NewUnorderedWithHasher same as `NewUnordered` but with a given hash function.
func NewUnorderedWithHasher[K comparable, V any](hasher HashFn[K]) *Unordered[K, V] {
return &Unordered[K, V]{
capMinus1: 3,
buckets: make([]linkedList[K, V], 4),
hasher: hasher,
}
}
// Get returns the value stored for this key, or false if not found.
func (m *Unordered[K, V]) Get(key K) (V, bool) {
var (
idx = m.hasher(key) & m.capMinus1
v V
)
for current := m.buckets[idx].head; current != nil; current = current.next {
if current.key == key {
return current.value, true
}
}
return v, false
}
// Lookup returns a pointer to the stored value for this key or nil if not found.
// The pointer is valid until the key is part of the hash map.
// Note, use `Get` for small values.
func (m *Unordered[K, V]) Lookup(key K) *V {
idx := m.hasher(key) & m.capMinus1
for current := m.buckets[idx].head; current != nil; current = current.next {
if current.key == key {
return &(current.value)
}
}
return nil
}
// Insert returns a pointer to a zero allocated value. These pointer is valid until
// the key is part of the hash map. Note, use `Put` for small values.
func (m *Unordered[K, V]) Insert(key K) (*V, bool) {
if m.length >= uintptr(cap(m.buckets)) {
m.grow()
}
idx := m.hasher(key) & m.capMinus1
// check head
if m.buckets[idx].head == nil {
newNode := &listNode[K, V]{key: key}
m.buckets[idx].head = newNode
m.length++
return &newNode.value, true
}
// search
for current := m.buckets[idx].head; ; current = current.next {
if current.key == key {
return ¤t.value, false
}
// reached end of list, so insert
if current.next == nil {
newNode := &listNode[K, V]{key: key}
current.next = newNode
m.length++
return &newNode.value, true
}
}
}
func (m *Unordered[K, V]) rehash(n uintptr) {
m.capMinus1 = n - 1
newBuckets := make([]linkedList[K, V], n)
for i := range m.buckets {
for current := m.buckets[i].head; current != nil; {
newElem := current
current = current.next
newElem.next = nil // unlink from old
// push newElem to front of the list
newIdx := m.hasher(newElem.key) & m.capMinus1
head := newBuckets[newIdx].head
headRef := &newBuckets[newIdx].head
*headRef = newElem
newElem.next = head
}
}
m.buckets = newBuckets
}
// Clear removes all key-value pairs from the map.
func (m *Unordered[K, V]) Clear() {
for i := range m.buckets {
m.buckets[i].head = nil
}
m.length = 0
}
// Size returns the number of items in the map.
func (m *Unordered[K, V]) Size() int {
return int(m.length)
}
// Load return the current load of the hash map.
func (m *Unordered[K, V]) Load() float32 {
return float32(m.length) / float32(cap(m.buckets))
}
func (m *Unordered[K, V]) grow() {
newSize := uintptr(cap(m.buckets) * 2)
m.rehash(newSize)
}
// 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 *Unordered[K, V]) Reserve(n uintptr) {
newCap := uintptr(NextPowerOf2(uint64(n)))
if uintptr(cap(m.buckets)) < newCap {
m.rehash(newCap)
}
}
// Put maps the given key to the given value. If the key already exists its
// value will be overwritten with the new value.
// Returns true, if the element is a new item in the hash map.
// go:inline
func (m *Unordered[K, V]) Put(key K, val V) bool {
v, isNew := m.Insert(key)
*v = val
return isNew
}
// Remove removes the specified key-value pair from the map.
// Returns true, if the element was in the hash map.
func (m *Unordered[K, V]) Remove(key K) bool {
var (
idx = m.hasher(key) & m.capMinus1
current = m.buckets[idx].head
prev *listNode[K, V]
)
// check head
if current != nil && current.key == key {
m.buckets[idx].head = current.next
m.length--
return true
}
// search for the key
for current != nil && current.key != key {
prev = current
current = current.next
}
if current == nil {
return false // not found
}
// unlink
prev.next = current.next
m.length--
return true
}
// Each calls 'fn' on every key-value pair in the hash map in no particular order.
// If 'fn' returns true, the iteration stops.
func (m *Unordered[K, V]) Each(fn func(key K, val V) bool) {
for i := range m.buckets {
for current := m.buckets[i].head; current != nil; current = current.next {
if stop := fn(current.key, current.value); stop {
// stop iteration
return
}
}
}
}