change collision back to a pointer type

this is better for inplace_insert
master
magical 2022-01-23 05:38:36 +00:00
parent a67e1d12db
commit e4f0311afa
1 changed files with 7 additions and 7 deletions

14
pmap.go
View File

@ -100,7 +100,7 @@ func (n *node) getNode(shift, hash uint32, key Key) interface{} {
return n.child[n.index(m)] return n.child[n.index(m)]
} }
func (n collision) getNode(hash uint32, key Key) interface{} { func (n *collision) getNode(hash uint32, key Key) interface{} {
if hash != n.hash { if hash != n.hash {
return nil return nil
} }
@ -128,7 +128,7 @@ func lookup(root interface{}, hash uint32, key Key, zero Value) (Value, bool) {
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")
@ -168,7 +168,7 @@ func insert(n interface{}, hash uint32, key Key, val Value, hashFn HashFunc) (ne
} 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")
@ -206,7 +206,7 @@ func insert(n interface{}, hash uint32, key Key, val Value, hashFn HashFunc) (ne
x.check() x.check()
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
@ -221,12 +221,12 @@ func insert(n interface{}, hash uint32, key Key, val Value, hashFn HashFunc) (ne
copy(l[1:], n.leaf[:i]) copy(l[1:], n.leaf[:i])
copy(l[1+i:], n.leaf[i+1:]) copy(l[1+i:], 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")
} }
@ -272,7 +272,7 @@ func (p pmap) stats() stats {
s.emptySlots++ s.emptySlots++
} }
} }
case collision: case *collision:
s.count++ s.count++
s.collisionCount++ s.collisionCount++
s.collidedKeys += len(n.leaf) s.collidedKeys += len(n.leaf)