From c57649e7586906c74beef943a51d2103bae9e5c7 Mon Sep 17 00:00:00 2001 From: magical Date: Sun, 23 Jan 2022 01:25:42 +0000 Subject: [PATCH] stop hardcoding the hash function --- pmap.go | 26 +++++++++++--------------- pmap_test.go | 13 ++++++++++--- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/pmap.go b/pmap.go index d0a5e66..3785b9e 100644 --- a/pmap.go +++ b/pmap.go @@ -43,8 +43,13 @@ type leaf struct { v Value } -func New() Map { - return pmap{} +type HashFunc = func(Key) uint32 + +func New(hash HashFunc) Map { + if hash == nil { + panic("pmap.New: nil hash") + } + return pmap{hash: hash} } func (p pmap) Len() int { @@ -52,13 +57,13 @@ func (p pmap) Len() int { } func (p pmap) Get(k Key) (Value, bool) { - var zero Key - h := hash(k) // TODO: p.hash + var zero Value + h := p.hash(k) return lookup(p.root, 0, h, k, zero) } func (p pmap) Set(k Key, v Value) Map { - h := hash(k) // TODO: p.hash + h := p.hash(k) root, added := insert(p.root, 0, h, k, v, hash) p.root = root if added { @@ -72,13 +77,6 @@ func (p pmap) Del(k Key) Map { return p } -func hash(k Key) uint32 { - //u := uint(k.(int))*6364136223846793005 + 1 - //return uint32(u >> 32) - u := uint(k.(int)) - return uint32(u + u>>32) -} - func (m *node) getNode(shift, hash uint32, key Key) interface{} { i := hash >> shift & nodeMask return m.child[i] @@ -131,9 +129,7 @@ func newnode(child interface{}, hash, shift uint32) *node { return n } -type hashFunc = func(Key) uint32 - -func insert(n interface{}, shift, hash uint32, key Key, val Value, hashFn hashFunc) (newNode interface{}, added bool) { +func insert(n interface{}, shift, hash uint32, key Key, val Value, hashFn HashFunc) (newNode interface{}, added bool) { if n == nil { return leaf{key, val}, true } diff --git a/pmap_test.go b/pmap_test.go index 3916a27..868ca7a 100644 --- a/pmap_test.go +++ b/pmap_test.go @@ -5,8 +5,15 @@ import ( "testing" ) +func hash(k Key) uint32 { + //u := uint(k.(int))*6364136223846793005 + 1 + //return uint32(u >> 32) + u := uint(k.(int)) + return uint32(u + u>>32) +} + func TestPmap(t *testing.T) { - p := New() + p := New(hash) const numElems = 100 for i := range iter(numElems) { p = p.Set(i, i) @@ -38,7 +45,7 @@ func BenchmarkGetAbsent(b *testing.B) { } func benchmarkGet(b *testing.B, numElems int) { - p := New() + p := New(hash) for i := range iter(numElems) { p = p.Set(i, i) } @@ -60,7 +67,7 @@ func benchmarkGet(b *testing.B, numElems int) { } func benchmarkGetAbsent(b *testing.B, numElems int) { - p := New() + p := New(hash) for i := range iter(numElems) { p = p.Set(-i-1, i) }