@@ -15,8 +15,8 @@ type hashmap struct {
15
15
buckets unsafe.Pointer // pointer to array of buckets
16
16
seed uintptr
17
17
count uintptr
18
- valueSize uint32
19
- keySize uint8 // maybe this can store the key type as well? E.g. keysize == 5 means string?
18
+ keySize uintptr // maybe this can store the key type as well? E.g. keysize == 5 means string?
19
+ valueSize uintptr
20
20
bucketBits uint8
21
21
keyEqual func (x , y unsafe.Pointer , n uintptr ) bool
22
22
keyHash func (key unsafe.Pointer , size , seed uintptr ) uint32
@@ -60,14 +60,14 @@ func hashmapTopHash(hash uint32) uint8 {
60
60
}
61
61
62
62
// Create a new hashmap with the given keySize and valueSize.
63
- func hashmapMake (keySize uint8 , valueSize uint32 , sizeHint uintptr , alg uint8 ) * hashmap {
63
+ func hashmapMake (keySize , valueSize uintptr , sizeHint uintptr , alg uint8 ) * hashmap {
64
64
numBuckets := sizeHint / 8
65
65
bucketBits := uint8 (0 )
66
66
for numBuckets != 0 {
67
67
numBuckets /= 2
68
68
bucketBits ++
69
69
}
70
- bucketBufSize := unsafe .Sizeof (hashmapBucket {}) + uintptr ( keySize ) * 8 + uintptr ( valueSize ) * 8
70
+ bucketBufSize := unsafe .Sizeof (hashmapBucket {}) + keySize * 8 + valueSize * 8
71
71
buckets := alloc (bucketBufSize * (1 << bucketBits ), nil )
72
72
73
73
keyHash := hashmapKeyHashAlg (hashmapAlgorithm (alg ))
@@ -155,14 +155,14 @@ func hashmapSet(m *hashmap, key unsafe.Pointer, value unsafe.Pointer, hash uint3
155
155
if hashmapShouldGrow (m ) {
156
156
hashmapGrow (m )
157
157
// seed changed when we grew; rehash key with new seed
158
- hash = m .keyHash (key , uintptr ( m .keySize ) , m .seed )
158
+ hash = m .keyHash (key , m .keySize , m .seed )
159
159
}
160
160
161
161
tophash := hashmapTopHash (hash )
162
162
163
163
numBuckets := uintptr (1 ) << m .bucketBits
164
164
bucketNumber := (uintptr (hash ) & (numBuckets - 1 ))
165
- bucketSize := unsafe .Sizeof (hashmapBucket {}) + uintptr ( m .keySize ) * 8 + uintptr ( m .valueSize ) * 8
165
+ bucketSize := unsafe .Sizeof (hashmapBucket {}) + m .keySize * 8 + m .valueSize * 8
166
166
bucketAddr := uintptr (m .buckets ) + bucketSize * bucketNumber
167
167
bucket := (* hashmapBucket )(unsafe .Pointer (bucketAddr ))
168
168
var lastBucket * hashmapBucket
@@ -173,9 +173,9 @@ func hashmapSet(m *hashmap, key unsafe.Pointer, value unsafe.Pointer, hash uint3
173
173
var emptySlotTophash * byte
174
174
for bucket != nil {
175
175
for i := uintptr (0 ); i < 8 ; i ++ {
176
- slotKeyOffset := unsafe .Sizeof (hashmapBucket {}) + uintptr ( m .keySize ) * uintptr (i )
176
+ slotKeyOffset := unsafe .Sizeof (hashmapBucket {}) + m .keySize * uintptr (i )
177
177
slotKey := unsafe .Pointer (uintptr (unsafe .Pointer (bucket )) + slotKeyOffset )
178
- slotValueOffset := unsafe .Sizeof (hashmapBucket {}) + uintptr ( m .keySize ) * 8 + uintptr ( m .valueSize ) * uintptr (i )
178
+ slotValueOffset := unsafe .Sizeof (hashmapBucket {}) + m .keySize * 8 + m .valueSize * uintptr (i )
179
179
slotValue := unsafe .Pointer (uintptr (unsafe .Pointer (bucket )) + slotValueOffset )
180
180
if bucket .tophash [i ] == 0 && emptySlotKey == nil {
181
181
// Found an empty slot, store it for if we couldn't find an
@@ -186,9 +186,9 @@ func hashmapSet(m *hashmap, key unsafe.Pointer, value unsafe.Pointer, hash uint3
186
186
}
187
187
if bucket .tophash [i ] == tophash {
188
188
// Could be an existing key that's the same.
189
- if m .keyEqual (key , slotKey , uintptr ( m .keySize ) ) {
189
+ if m .keyEqual (key , slotKey , m .keySize ) {
190
190
// found same key, replace it
191
- memcpy (slotValue , value , uintptr ( m .valueSize ) )
191
+ memcpy (slotValue , value , m .valueSize )
192
192
return
193
193
}
194
194
}
@@ -203,24 +203,24 @@ func hashmapSet(m *hashmap, key unsafe.Pointer, value unsafe.Pointer, hash uint3
203
203
return
204
204
}
205
205
m .count ++
206
- memcpy (emptySlotKey , key , uintptr ( m .keySize ) )
207
- memcpy (emptySlotValue , value , uintptr ( m .valueSize ) )
206
+ memcpy (emptySlotKey , key , m .keySize )
207
+ memcpy (emptySlotValue , value , m .valueSize )
208
208
* emptySlotTophash = tophash
209
209
}
210
210
211
211
// hashmapInsertIntoNewBucket creates a new bucket, inserts the given key and
212
212
// value into the bucket, and returns a pointer to this bucket.
213
213
func hashmapInsertIntoNewBucket (m * hashmap , key , value unsafe.Pointer , tophash uint8 ) * hashmapBucket {
214
- bucketBufSize := unsafe .Sizeof (hashmapBucket {}) + uintptr ( m .keySize ) * 8 + uintptr ( m .valueSize ) * 8
214
+ bucketBufSize := unsafe .Sizeof (hashmapBucket {}) + m .keySize * 8 + m .valueSize * 8
215
215
bucketBuf := alloc (bucketBufSize , nil )
216
216
// Insert into the first slot, which is empty as it has just been allocated.
217
217
slotKeyOffset := unsafe .Sizeof (hashmapBucket {})
218
218
slotKey := unsafe .Pointer (uintptr (bucketBuf ) + slotKeyOffset )
219
- slotValueOffset := unsafe .Sizeof (hashmapBucket {}) + uintptr ( m .keySize ) * 8
219
+ slotValueOffset := unsafe .Sizeof (hashmapBucket {}) + m .keySize * 8
220
220
slotValue := unsafe .Pointer (uintptr (bucketBuf ) + slotValueOffset )
221
221
m .count ++
222
- memcpy (slotKey , key , uintptr ( m .keySize ) )
223
- memcpy (slotValue , value , uintptr ( m .valueSize ) )
222
+ memcpy (slotKey , key , m .keySize )
223
+ memcpy (slotValue , value , m .valueSize )
224
224
bucket := (* hashmapBucket )(bucketBuf )
225
225
bucket .tophash [0 ] = tophash
226
226
return bucket
@@ -235,14 +235,14 @@ func hashmapGrow(m *hashmap) {
235
235
// allocate our new buckets twice as big
236
236
n .bucketBits = m .bucketBits + 1
237
237
numBuckets := uintptr (1 ) << n .bucketBits
238
- bucketBufSize := unsafe .Sizeof (hashmapBucket {}) + uintptr ( m .keySize ) * 8 + uintptr ( m .valueSize ) * 8
238
+ bucketBufSize := unsafe .Sizeof (hashmapBucket {}) + m .keySize * 8 + m .valueSize * 8
239
239
n .buckets = alloc (bucketBufSize * numBuckets , nil )
240
240
241
241
// use a hashmap iterator to go through the old map
242
242
var it hashmapIterator
243
243
244
- var key = alloc (uintptr ( m .keySize ) , nil )
245
- var value = alloc (uintptr ( m .valueSize ) , nil )
244
+ var key = alloc (m .keySize , nil )
245
+ var value = alloc (m .valueSize , nil )
246
246
247
247
for hashmapNext (m , & it , key , value ) {
248
248
h := n .keyHash (key , uintptr (n .keySize ), n .seed )
@@ -265,7 +265,7 @@ func hashmapGet(m *hashmap, key, value unsafe.Pointer, valueSize uintptr, hash u
265
265
}
266
266
numBuckets := uintptr (1 ) << m .bucketBits
267
267
bucketNumber := (uintptr (hash ) & (numBuckets - 1 ))
268
- bucketSize := unsafe .Sizeof (hashmapBucket {}) + uintptr ( m .keySize ) * 8 + uintptr ( m .valueSize ) * 8
268
+ bucketSize := unsafe .Sizeof (hashmapBucket {}) + m .keySize * 8 + m .valueSize * 8
269
269
bucketAddr := uintptr (m .buckets ) + bucketSize * bucketNumber
270
270
bucket := (* hashmapBucket )(unsafe .Pointer (bucketAddr ))
271
271
@@ -278,15 +278,15 @@ func hashmapGet(m *hashmap, key, value unsafe.Pointer, valueSize uintptr, hash u
278
278
// Try to find the key.
279
279
for bucket != nil {
280
280
for i := uintptr (0 ); i < 8 ; i ++ {
281
- slotKeyOffset := unsafe .Sizeof (hashmapBucket {}) + uintptr ( m .keySize ) * uintptr (i )
281
+ slotKeyOffset := unsafe .Sizeof (hashmapBucket {}) + m .keySize * uintptr (i )
282
282
slotKey := unsafe .Pointer (uintptr (unsafe .Pointer (bucket )) + slotKeyOffset )
283
- slotValueOffset := unsafe .Sizeof (hashmapBucket {}) + uintptr ( m .keySize ) * 8 + uintptr ( m .valueSize ) * uintptr (i )
283
+ slotValueOffset := unsafe .Sizeof (hashmapBucket {}) + m .keySize * 8 + m .valueSize * uintptr (i )
284
284
slotValue := unsafe .Pointer (uintptr (unsafe .Pointer (bucket )) + slotValueOffset )
285
285
if bucket .tophash [i ] == tophash {
286
286
// This could be the key we're looking for.
287
- if m .keyEqual (key , slotKey , uintptr ( m .keySize ) ) {
287
+ if m .keyEqual (key , slotKey , m .keySize ) {
288
288
// Found the key, copy it.
289
- memcpy (value , slotValue , uintptr ( m .valueSize ) )
289
+ memcpy (value , slotValue , m .valueSize )
290
290
return true
291
291
}
292
292
}
@@ -295,7 +295,7 @@ func hashmapGet(m *hashmap, key, value unsafe.Pointer, valueSize uintptr, hash u
295
295
}
296
296
297
297
// Did not find the key.
298
- memzero (value , uintptr ( m .valueSize ) )
298
+ memzero (value , m .valueSize )
299
299
return false
300
300
}
301
301
@@ -312,7 +312,7 @@ func hashmapDelete(m *hashmap, key unsafe.Pointer, hash uint32) {
312
312
}
313
313
numBuckets := uintptr (1 ) << m .bucketBits
314
314
bucketNumber := (uintptr (hash ) & (numBuckets - 1 ))
315
- bucketSize := unsafe .Sizeof (hashmapBucket {}) + uintptr ( m .keySize ) * 8 + uintptr ( m .valueSize ) * 8
315
+ bucketSize := unsafe .Sizeof (hashmapBucket {}) + m .keySize * 8 + m .valueSize * 8
316
316
bucketAddr := uintptr (m .buckets ) + bucketSize * bucketNumber
317
317
bucket := (* hashmapBucket )(unsafe .Pointer (bucketAddr ))
318
318
@@ -325,11 +325,11 @@ func hashmapDelete(m *hashmap, key unsafe.Pointer, hash uint32) {
325
325
// Try to find the key.
326
326
for bucket != nil {
327
327
for i := uintptr (0 ); i < 8 ; i ++ {
328
- slotKeyOffset := unsafe .Sizeof (hashmapBucket {}) + uintptr ( m .keySize ) * uintptr (i )
328
+ slotKeyOffset := unsafe .Sizeof (hashmapBucket {}) + m .keySize * uintptr (i )
329
329
slotKey := unsafe .Pointer (uintptr (unsafe .Pointer (bucket )) + slotKeyOffset )
330
330
if bucket .tophash [i ] == tophash {
331
331
// This could be the key we're looking for.
332
- if m .keyEqual (key , slotKey , uintptr ( m .keySize ) ) {
332
+ if m .keyEqual (key , slotKey , m .keySize ) {
333
333
// Found the key, delete it.
334
334
bucket .tophash [i ] = 0
335
335
m .count --
@@ -367,7 +367,7 @@ func hashmapNext(m *hashmap, it *hashmapIterator, key, value unsafe.Pointer) boo
367
367
// went through all buckets
368
368
return false
369
369
}
370
- bucketSize := unsafe .Sizeof (hashmapBucket {}) + uintptr ( m .keySize ) * 8 + uintptr ( m .valueSize ) * 8
370
+ bucketSize := unsafe .Sizeof (hashmapBucket {}) + m .keySize * 8 + m .valueSize * 8
371
371
bucketAddr := uintptr (it .buckets ) + bucketSize * it .bucketNumber
372
372
it .bucket = (* hashmapBucket )(unsafe .Pointer (bucketAddr ))
373
373
it .bucketNumber ++ // next bucket
@@ -379,24 +379,24 @@ func hashmapNext(m *hashmap, it *hashmapIterator, key, value unsafe.Pointer) boo
379
379
}
380
380
381
381
bucketAddr := uintptr (unsafe .Pointer (it .bucket ))
382
- slotKeyOffset := unsafe .Sizeof (hashmapBucket {}) + uintptr ( m .keySize ) * uintptr (it .bucketIndex )
382
+ slotKeyOffset := unsafe .Sizeof (hashmapBucket {}) + m .keySize * uintptr (it .bucketIndex )
383
383
slotKey := unsafe .Pointer (bucketAddr + slotKeyOffset )
384
- memcpy (key , slotKey , uintptr ( m .keySize ) )
384
+ memcpy (key , slotKey , m .keySize )
385
385
386
386
if it .buckets == m .buckets {
387
387
// Our view of the buckets is the same as the parent map.
388
388
// Just copy the value we have
389
- slotValueOffset := unsafe .Sizeof (hashmapBucket {}) + uintptr ( m .keySize ) * 8 + uintptr ( m .valueSize ) * uintptr (it .bucketIndex )
389
+ slotValueOffset := unsafe .Sizeof (hashmapBucket {}) + m .keySize * 8 + m .valueSize * uintptr (it .bucketIndex )
390
390
slotValue := unsafe .Pointer (bucketAddr + slotValueOffset )
391
- memcpy (value , slotValue , uintptr ( m .valueSize ) )
391
+ memcpy (value , slotValue , m .valueSize )
392
392
it .bucketIndex ++
393
393
} else {
394
394
it .bucketIndex ++
395
395
396
396
// Our view of the buckets doesn't match the parent map.
397
397
// Look up the key in the new buckets and return that value if it exists
398
- hash := m .keyHash (key , uintptr ( m .keySize ) , m .seed )
399
- ok := hashmapGet (m , key , value , uintptr ( m .valueSize ) , hash )
398
+ hash := m .keyHash (key , m .keySize , m .seed )
399
+ ok := hashmapGet (m , key , value , m .valueSize , hash )
400
400
if ! ok {
401
401
// doesn't exist in parent map; try next key
402
402
continue
@@ -414,7 +414,7 @@ func hashmapBinarySet(m *hashmap, key, value unsafe.Pointer) {
414
414
if m == nil {
415
415
nilMapPanic ()
416
416
}
417
- hash := hash32 (key , uintptr ( m .keySize ) , m .seed )
417
+ hash := hash32 (key , m .keySize , m .seed )
418
418
hashmapSet (m , key , value , hash )
419
419
}
420
420
@@ -423,15 +423,15 @@ func hashmapBinaryGet(m *hashmap, key, value unsafe.Pointer, valueSize uintptr)
423
423
memzero (value , uintptr (valueSize ))
424
424
return false
425
425
}
426
- hash := hash32 (key , uintptr ( m .keySize ) , m .seed )
426
+ hash := hash32 (key , m .keySize , m .seed )
427
427
return hashmapGet (m , key , value , valueSize , hash )
428
428
}
429
429
430
430
func hashmapBinaryDelete (m * hashmap , key unsafe.Pointer ) {
431
431
if m == nil {
432
432
return
433
433
}
434
- hash := hash32 (key , uintptr ( m .keySize ) , m .seed )
434
+ hash := hash32 (key , m .keySize , m .seed )
435
435
hashmapDelete (m , key , hash )
436
436
}
437
437
0 commit comments