Implement package-global maps (of max 8 entries)

This commit is contained in:
Ayke van Laethem
2018-08-24 00:56:20 +02:00
parent 2b78b6d7e8
commit 179cf74b01
4 changed files with 139 additions and 5 deletions
+4
View File
@@ -14,6 +14,8 @@ type Stringer interface {
const SIX = 6
var testmap = map[string]int{"data": 3}
func main() {
println("Hello world from Go!")
println("The answer is:", calculateAnswer())
@@ -25,6 +27,7 @@ func main() {
m := map[string]int{"answer": 42, "foo": 3}
readMap(m, "answer")
readMap(testmap, "data")
foo := []int{1, 2, 4, 5}
println("len/cap foo:", len(foo), cap(foo))
@@ -50,6 +53,7 @@ func runFunc(f func(int), arg int) {
}
func readMap(m map[string]int, key string) {
println("map length:", len(m))
println("map read:", key, "=", m[key])
}
+11 -5
View File
@@ -42,6 +42,16 @@ func stringhash(s *string) uint32 {
return result
}
// Get the topmost 8 bits of the hash, without using a special value (like 0).
func hashmapTopHash(hash uint32) uint8 {
tophash := uint8(hash >> 24)
if tophash < 1 {
// 0 means empty slot, so make it bigger.
tophash += 1
}
return tophash
}
// Create a new hashmap with the given keySize and valueSize.
func hashmapMake(keySize, valueSize uint8) *hashmap {
bucketBufSize := unsafe.Sizeof(hashmapBucket{}) + uintptr(keySize)*8 + uintptr(valueSize)*8
@@ -63,11 +73,7 @@ func hashmapSet(m *hashmap, key string, value unsafe.Pointer) {
bucketAddr := uintptr(m.buckets) + bucketSize*bucketNumber
bucket := (*hashmapBucket)(unsafe.Pointer(bucketAddr))
tophash := uint8(hash >> 24)
if tophash < 1 {
// 0 means empty slot, so make it bigger.
tophash += 1
}
tophash := hashmapTopHash(hash)
// See whether the key already exists somewhere.
var emptySlotKey *string