-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashset.go
229 lines (190 loc) · 3.8 KB
/
hashset.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
package cm
type HashValue uintptr
// HashSetBin implements a linked list
type HashSetBin[U comparable] struct {
elt U
hash HashValue
next *HashSetBin[U]
}
// HashSet implements a hash set
type HashSet[T, U comparable] struct {
// number of bins in the table, not just table size
entries uint
isEqual func(ptr T, elt U) bool
size uint
table []*HashSetBin[U]
pooledBins *HashSetBin[U]
}
// NewHashSet is a HashSet constructor
func NewHashSet[T, U comparable](isEqual func(ptr T, elt U) bool) *HashSet[T, U] {
size := nextPrime(0)
return &HashSet[T, U]{
isEqual: isEqual,
size: size,
table: make([]*HashSetBin[U], size),
}
}
func (set *HashSet[T, U]) resize() {
newSize := nextPrime(set.size + 1)
newTable := make([]*HashSetBin[U], newSize)
var i uint
for i = 0; i < set.size; i++ {
bin := set.table[i]
for bin != nil {
next := bin.next
idx := uint(bin.hash) % newSize
bin.next = newTable[idx]
newTable[idx] = bin
bin = next
}
}
set.table = newTable
set.size = newSize
}
// Count returns the number of entries
func (set *HashSet[T, U]) Count() uint {
return set.entries
}
// Insert returns the U the T is in, or inserts a new U and returns it.
func (set *HashSet[T, U]) Insert(hash HashValue, ptr T, transform func(obj T) U) U {
idx := uint(hash) % set.size
// Find the bin with the matching element.
bin := set.table[idx]
for bin != nil && !set.isEqual(ptr, bin.elt) {
bin = bin.next
}
if bin != nil {
return bin.elt
}
// Create it if necessary.
bin = set.getUnusedBin()
bin.hash = hash
bin.elt = transform(ptr)
bin.next = set.table[idx]
set.table[idx] = bin
set.entries++
if set.entries >= set.size {
set.resize()
}
return bin.elt
}
// Remove removes the T from the HashSet, returning the U it was in.
func (set *HashSet[T, U]) Remove(hash HashValue, ptr T) U {
idx := uint(hash) % set.size
bin := set.table[idx]
prevPtr := &set.table[idx]
// Find the bin
for bin != nil && !set.isEqual(ptr, bin.elt) {
prevPtr = &bin.next
bin = bin.next
}
// Remove the bin if it exists
if bin != nil {
// Update the previous linked list pointer
*prevPtr = bin.next
set.entries--
elt := bin.elt
set.recycle(bin)
return elt
}
var zero U
return zero
}
// Find returns the U the T is in, or nil.
func (set *HashSet[T, U]) Find(hash HashValue, ptr T) U {
idx := uint(hash) % set.size
bin := set.table[idx]
for bin != nil && !set.isEqual(ptr, bin.elt) {
bin = bin.next
}
if bin != nil {
return bin.elt
}
var zero U
return zero
}
// Each calls f for every U in the HashSet.
func (set *HashSet[T, U]) Each(f func(U)) {
for _, bin := range set.table {
for bin != nil {
next := bin.next
f(bin.elt)
bin = next
}
}
}
// Filter removes elements if f returns false
func (set *HashSet[T, U]) Filter(f func(U) bool) {
var i uint
for i = 0; i < set.size; i++ {
prevPtr := &set.table[i]
bin := set.table[i]
for bin != nil {
next := bin.next
if f(bin.elt) {
prevPtr = &bin.next
} else {
*prevPtr = next
set.entries--
set.recycle(bin)
}
bin = next
}
}
}
func (set *HashSet[T, U]) recycle(bin *HashSetBin[U]) {
bin.next = set.pooledBins
set.pooledBins = bin
var zero U
bin.elt = zero
}
func (set *HashSet[T, U]) getUnusedBin() *HashSetBin[U] {
bin := set.pooledBins
if bin != nil {
set.pooledBins = bin.next
return bin
}
for i := 0; i < pooledBufferSize; i++ {
set.recycle(&HashSetBin[U]{})
}
return &HashSetBin[U]{}
}
var primes = [30]uint{
5,
13,
23,
47,
97,
193,
389,
769,
1543,
3079,
6151,
12289,
24593,
49157,
98317,
196613,
393241,
786433,
1572869,
3145739,
6291469,
12582917,
25165843,
50331653,
100663319,
201326611,
402653189,
805306457,
1610612741,
0,
}
func nextPrime(n uint) uint {
var i uint = 0
for n > primes[i] {
i++
}
return primes[i]
}