make collision a value type

doesn't really matter, and it's basically just a slice header
master
magical 2022-01-23 01:39:18 +00:00
parent bb1aa352aa
commit 9553b9a9e4
1 changed files with 8 additions and 7 deletions

15
pmap.go
View File

@ -82,7 +82,7 @@ func (m *node) getNode(shift, hash uint32, key Key) interface{} {
return m.child[i] return m.child[i]
} }
func (m *collision) getNode(hash uint32, key Key) interface{} { func (m collision) getNode(hash uint32, key Key) interface{} {
if hash != m.hash { if hash != m.hash {
return nil return nil
} }
@ -109,7 +109,7 @@ func lookup(root interface{}, shift, hash uint32, key Key, zero Key) (Value, boo
case *node: case *node:
cur = n.getNode(shift, hash, key) cur = n.getNode(shift, hash, key)
shift += nodeShift shift += nodeShift
case *collision: case collision:
cur = n.getNode(hash, key) cur = n.getNode(hash, key)
default: default:
panic("pmap: unhandled case in lookup") panic("pmap: unhandled case in lookup")
@ -148,7 +148,7 @@ func insert(n interface{}, shift, hash uint32, key Key, val Value, hashFn HashFu
} else if h := hashFn(n.k); h == hash { } else if h := hashFn(n.k); h == hash {
// collision // collision
added = true added = true
return &collision{hash, []leaf{{key, val}, n}} return collision{hash, []leaf{{key, val}, n}}
} else { } else {
if h>>shift == hash>>shift { if h>>shift == hash>>shift {
panic("pmap: infinite loop in insert") panic("pmap: infinite loop in insert")
@ -172,7 +172,7 @@ func insert(n interface{}, shift, hash uint32, key Key, val Value, hashFn HashFu
x.child[idx] = c x.child[idx] = c
x.bitmap |= 1 << idx x.bitmap |= 1 << idx
return x return x
case *collision: case collision:
if n.hash != hash { if n.hash != hash {
// not a collision, so we must still have some hash bits left // not a collision, so we must still have some hash bits left
// split the trie // split the trie
@ -187,12 +187,12 @@ func insert(n interface{}, shift, hash uint32, key Key, val Value, hashFn HashFu
l = append(l, n.leaf[:i]...) l = append(l, n.leaf[:i]...)
l = append(l, n.leaf[i+1:]...) l = append(l, n.leaf[i+1:]...)
added = false added = false
return &collision{hash, l} return collision{hash, l}
} }
} }
// new collision // new collision
added = true added = true
return &collision{hash, append([]leaf{{key, val}}, n.leaf...)} return collision{hash, append([]leaf{{key, val}}, n.leaf...)}
default: default:
panic("pmap: unhandled case in insert") panic("pmap: unhandled case in insert")
} }
@ -237,7 +237,8 @@ func (p pmap) stats() stats {
visit(n.child[i], h+1) visit(n.child[i], h+1)
} }
} }
case *collision: case collision:
s.count++
s.collisionCount++ s.collisionCount++
s.collidedKeys += len(n.leaf) s.collidedKeys += len(n.leaf)
default: default: