-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhashmap.go
More file actions
353 lines (298 loc) · 7.48 KB
/
hashmap.go
File metadata and controls
353 lines (298 loc) · 7.48 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package ecs
import (
"github.com/unitoftime/ecs/internal/intmap"
)
type intkey interface {
// comparable
~int | ~uint | ~int64 | ~uint64 | ~int32 | ~uint32 | ~int16 | ~uint16 | ~int8 | ~uint8 | ~uintptr
}
// This is useful for testing different map implementations in my workload
type internalMap[K intkey, V any] struct {
inner *intmap.Map[K, V]
}
func newMap[K intkey, V any](size int) *internalMap[K, V] {
return &internalMap[K, V]{
intmap.New[K, V](0),
}
}
func (m *internalMap[K, V]) Len() int {
return m.inner.Len()
}
func (m *internalMap[K, V]) Get(k K) (V, bool) {
return m.inner.Get(k)
}
func (m *internalMap[K, V]) Put(k K, val V) {
m.inner.Put(k, val)
}
func (m *internalMap[K, V]) Delete(k K) {
m.inner.Del(k)
}
func (m *internalMap[K, V]) Has(k K) bool {
_, has := m.inner.Get(k)
return has
}
//--------------------------------------------------------------------------------
// TODO: Move to generational Ids
// This is useful for testing different map implementations in my workload
type locMap struct {
// inner *LocMapImpl
inner *intmap.Map[Id, entLoc]
}
func newLocMap(size int) locMap {
return locMap{
// NewLocMapImpl(size),
intmap.New[Id, entLoc](0),
}
}
func (m *locMap) Len() int {
return m.inner.Len()
}
func (m *locMap) Get(k Id) (entLoc, bool) {
return m.inner.Get(k)
}
func (m *locMap) Put(k Id, val entLoc) {
m.inner.Put(k, val)
}
func (m *locMap) Delete(k Id) {
m.inner.Del(k)
}
func (m *locMap) Has(k Id) bool {
_, has := m.inner.Get(k)
return has
}
// --------------------------------------------------------------------------------
// const fillFactor64 = 0.5
// // Hashing Reference: https://gist.github.com/badboy/6267743
// func phiMix64(x int) int {
// // Note: With this, we are only just a bit faster than swissmap
// h := x * (-1_640_531_527) // This is just the int32 version of the 0x9E3779B9
// return h ^ (h >> 16)
// // // TODO: track collision counts and compare before enabling this
// // // Theory: Because ecs.Id is just incremented by 1 each time, it might be effective to just always take the next slot
// // return x + x
// }
// type locPair struct {
// K Id
// V entLoc
// }
// // LocMapImpl is a hashmap where the keys are some any integer type.
// type LocMapImpl struct {
// data []locPair
// size int
// zeroVal entLoc // value of 'zero' key
// hasZeroKey bool // do we have 'zero' key in the map?
// }
// // New creates a new map with keys being any integer subtype.
// // The map can store up to the given capacity before reallocation and rehashing occurs.
// func NewLocMapImpl(capacity int) *LocMapImpl {
// return &LocMapImpl{
// data: make([]locPair, arraySize(capacity, fillFactor64)),
// }
// }
// // Get returns the value if the key is found.
// func (m *LocMapImpl) Get(key Id) (entLoc, bool) {
// if key == InvalidEntity {
// if m.hasZeroKey {
// return m.zeroVal, true
// }
// var zero entLoc
// return zero, false
// }
// idx := m.startIndex(key)
// p := m.data[idx]
// if p.K == InvalidEntity { // end of chain already
// var zero entLoc
// return zero, false
// }
// if p.K == key { // we check zero prior to this call
// return p.V, true
// }
// // hash collision, seek next hash match, bailing on first empty
// for {
// idx = m.nextIndex(idx)
// p = m.data[idx]
// if p.K == InvalidEntity {
// var zero entLoc
// return zero, false
// }
// if p.K == key {
// return p.V, true
// }
// }
// }
// // Put adds or updates key with value val.
// func (m *LocMapImpl) Put(key Id, val entLoc) {
// if key == InvalidEntity {
// if !m.hasZeroKey {
// m.size++
// }
// m.zeroVal = val
// m.hasZeroKey = true
// return
// }
// idx := m.startIndex(key)
// p := &m.data[idx]
// if p.K == InvalidEntity { // end of chain already
// p.K = key
// p.V = val
// if m.size >= m.sizeThreshold() {
// m.rehash()
// } else {
// m.size++
// }
// return
// } else if p.K == key { // overwrite existing value
// p.V = val
// return
// }
// // hash collision, seek next empty or key match
// for {
// idx = m.nextIndex(idx)
// p = &m.data[idx]
// if p.K == InvalidEntity {
// p.K = key
// p.V = val
// if m.size >= m.sizeThreshold() {
// m.rehash()
// } else {
// m.size++
// }
// return
// } else if p.K == key {
// p.V = val
// return
// }
// }
// }
// // Clear removes all items from the map, but keeps the internal buffers for reuse.
// func (m *LocMapImpl) Clear() {
// var zero entLoc
// m.hasZeroKey = false
// m.zeroVal = zero
// // compiles down to runtime.memclr()
// for i := range m.data {
// m.data[i] = locPair{}
// }
// m.size = 0
// }
// func (m *LocMapImpl) rehash() {
// oldData := m.data
// m.data = make([]locPair, 2*len(m.data))
// // reset size
// if m.hasZeroKey {
// m.size = 1
// } else {
// m.size = 0
// }
// forEach64(oldData, m.Put)
// // for _, p := range oldData {
// // if p.K != InvalidEntity {
// // m.Put(p.K, p.V)
// // }
// // }
// }
// // Len returns the number of elements in the map.
// func (m *LocMapImpl) Len() int {
// return m.size
// }
// func (m *LocMapImpl) sizeThreshold() int {
// return int(math.Floor(float64(len(m.data)) * fillFactor64))
// }
// func (m *LocMapImpl) startIndex(key Id) int {
// return phiMix64(int(key)) & (len(m.data) - 1)
// }
// func (m *LocMapImpl) nextIndex(idx int) int {
// return (idx + 1) & (len(m.data) - 1)
// }
// func forEach64(pairs []locPair, f func(k Id, v entLoc)) {
// for _, p := range pairs {
// if p.K != InvalidEntity {
// f(p.K, p.V)
// }
// }
// }
// // Del deletes a key and its value, returning true iff the key was found
// func (m *LocMapImpl) Del(key Id) bool {
// if key == InvalidEntity {
// if m.hasZeroKey {
// m.hasZeroKey = false
// m.size--
// return true
// }
// return false
// }
// idx := m.startIndex(key)
// p := m.data[idx]
// if p.K == key {
// // any keys that were pushed back needs to be shifted nack into the empty slot
// // to avoid breaking the chain
// m.shiftKeys(idx)
// m.size--
// return true
// } else if p.K == InvalidEntity { // end of chain already
// return false
// }
// for {
// idx = m.nextIndex(idx)
// p = m.data[idx]
// if p.K == key {
// // any keys that were pushed back needs to be shifted nack into the empty slot
// // to avoid breaking the chain
// m.shiftKeys(idx)
// m.size--
// return true
// } else if p.K == InvalidEntity {
// return false
// }
// }
// }
// func (m *LocMapImpl) shiftKeys(idx int) int {
// // Shift entries with the same hash.
// // We need to do this on deletion to ensure we don't have zeroes in the hash chain
// for {
// var p locPair
// lastIdx := idx
// idx = m.nextIndex(idx)
// for {
// p = m.data[idx]
// if p.K == InvalidEntity {
// m.data[lastIdx] = locPair{}
// return lastIdx
// }
// slot := m.startIndex(p.K)
// if lastIdx <= idx {
// if lastIdx >= slot || slot > idx {
// break
// }
// } else {
// if lastIdx >= slot && slot > idx {
// break
// }
// }
// idx = m.nextIndex(idx)
// }
// m.data[lastIdx] = p
// }
// }
// func nextPowerOf2(x uint32) uint32 {
// if x == math.MaxUint32 {
// return x
// }
// if x == 0 {
// return 1
// }
// x--
// x |= x >> 1
// x |= x >> 2
// x |= x >> 4
// x |= x >> 8
// x |= x >> 16
// return x + 1
// }
// func arraySize(exp int, fill float64) int {
// s := nextPowerOf2(uint32(math.Ceil(float64(exp) / fill)))
// if s < 2 {
// s = 2
// }
// return int(s)
// }