-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathmap.go
209 lines (181 loc) · 4.45 KB
/
map.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
// Package hashmap provides an implementation of a hashmap. The map uses linear
// probing and automatically resizes. The map can also be efficiently copied,
// and will perform copies lazily, using copy-on-write. However, the
// copy-on-write will copy the entire map after the first write. One can imagine
// a more efficient implementation that would split the map into chunks and use
// copy-on-write selectively for each chunk.
package hashmap
import (
g "github.com/zyedidia/generic"
)
type entry[K, V any] struct {
key K
filled bool
value V
}
// A Map is a hashmap that supports copying via copy-on-write.
type Map[K, V any] struct {
entries []entry[K, V]
capacity uint64
length uint64
readonly bool
ops ops[K]
}
type ops[T any] struct {
equals func(a, b T) bool
hash func(t T) uint64
}
func pow2ceil(num uint64) uint64 {
power := uint64(1)
for power < num {
power *= 2
}
return power
}
// New constructs a new map with the given capacity.
func New[K, V any](capacity uint64, equals g.EqualsFn[K], hash g.HashFn[K]) *Map[K, V] {
if capacity == 0 {
capacity = 1
}
capacity = pow2ceil(capacity)
return &Map[K, V]{
entries: make([]entry[K, V], capacity),
capacity: capacity,
ops: ops[K]{
equals: equals,
hash: hash,
},
}
}
// Get returns the value stored for this key, or false if there is no such
// value.
func (m *Map[K, V]) Get(key K) (V, bool) {
hash := m.ops.hash(key)
idx := hash & (m.capacity - 1)
for m.entries[idx].filled {
if m.ops.equals(m.entries[idx].key, key) {
return m.entries[idx].value, true
}
idx++
if idx >= m.capacity {
idx = 0
}
}
var v V
return v, false
}
func (m *Map[K, V]) resize(newcap uint64) {
newm := Map[K, V]{
capacity: newcap,
length: m.length,
entries: make([]entry[K, V], newcap),
ops: m.ops,
}
for _, ent := range m.entries {
if ent.filled {
newm.Put(ent.key, ent.value)
}
}
m.capacity = newm.capacity
m.entries = newm.entries
}
// 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 *Map[K, V]) Put(key K, val V) {
if m.length >= m.capacity/2 {
m.resize(m.capacity * 2)
} else if m.readonly {
entries := make([]entry[K, V], len(m.entries), cap(m.entries))
copy(entries, m.entries)
m.entries = entries
m.readonly = false
}
hash := m.ops.hash(key)
idx := hash & (m.capacity - 1)
for m.entries[idx].filled {
if m.ops.equals(m.entries[idx].key, key) {
m.entries[idx].value = val
return
}
idx++
if idx >= m.capacity {
idx = 0
}
}
m.entries[idx].key = key
m.entries[idx].value = val
m.entries[idx].filled = true
m.length++
}
func (m *Map[K, V]) remove(idx uint64) {
var k K
var v V
m.entries[idx].filled = false
m.entries[idx].key = k
m.entries[idx].value = v
m.length--
}
// Remove removes the specified key-value pair from the map.
func (m *Map[K, V]) Remove(key K) {
hash := m.ops.hash(key)
idx := hash & (m.capacity - 1)
for m.entries[idx].filled && !m.ops.equals(m.entries[idx].key, key) {
idx = (idx + 1) & (m.capacity - 1)
}
if !m.entries[idx].filled {
return
}
if m.readonly {
entries := make([]entry[K, V], len(m.entries), cap(m.entries))
copy(entries, m.entries)
m.entries = entries
m.readonly = false
}
m.remove(idx)
idx = (idx + 1) & (m.capacity - 1)
for m.entries[idx].filled {
krehash := m.entries[idx].key
vrehash := m.entries[idx].value
m.remove(idx)
m.Put(krehash, vrehash)
idx = (idx + 1) & (m.capacity - 1)
}
// halves the array if it is 12.5% full or less
if m.length > 0 && m.length <= m.capacity/8 {
m.resize(m.capacity / 2)
}
}
// Clear removes all key-value pairs from the map.
func (m *Map[K, V]) Clear() {
for idx, entry := range m.entries {
if entry.filled {
m.remove(uint64(idx))
}
}
}
// Size returns the number of items in the map.
func (m *Map[K, V]) Size() int {
return int(m.length)
}
// Copy returns a copy of this map. The copy will not allocate any memory until
// the first write, so any number of read-only copies can be made without any
// additional allocations.
func (m *Map[K, V]) Copy() *Map[K, V] {
m.readonly = true
return &Map[K, V]{
entries: m.entries,
capacity: m.capacity,
length: m.length,
readonly: true,
ops: m.ops,
}
}
// Each calls 'fn' on every key-value pair in the hashmap in no particular
// order.
func (m *Map[K, V]) Each(fn func(key K, val V)) {
for _, ent := range m.entries {
if ent.filled {
fn(ent.key, ent.value)
}
}
}