Compare commits
2 Commits
95687acade
...
572e59f33d
| Author | SHA1 | Date | |
|---|---|---|---|
|
572e59f33d
|
|||
| 867a1d49df |
11
table.go
11
table.go
@@ -1,12 +1,21 @@
|
|||||||
package cuckoo
|
package cuckoo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"iter"
|
"iter"
|
||||||
"math/bits"
|
"math/bits"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ErrBadHash occurs when the hashes given to a [Table] cause too many key
|
||||||
|
// collisions. Try rebuilding the table using:
|
||||||
|
//
|
||||||
|
// 1. Different hash seeds. Equal seeds produce equal hash functions, which
|
||||||
|
// always cycle.
|
||||||
|
// 2. A different [Hash] algorithm.
|
||||||
|
var ErrBadHash = errors.New("bad hash")
|
||||||
|
|
||||||
// A Table is hash table that uses cuckoo hashing to resolve collision. Create
|
// A Table is hash table that uses cuckoo hashing to resolve collision. Create
|
||||||
// one with [NewTable]. Or if you want more granularity, use [NewTableBy] or
|
// one with [NewTable]. Or if you want more granularity, use [NewTableBy] or
|
||||||
// [NewCustomTable].
|
// [NewCustomTable].
|
||||||
@@ -127,7 +136,7 @@ func (t *Table[K, V]) Put(key K, value V) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if t.load() < t.minLoadFactor {
|
if t.load() < t.minLoadFactor {
|
||||||
return fmt.Errorf("bad hash: resize on load %d/%d = %f", t.Size(), t.TotalCapacity(), t.load())
|
return fmt.Errorf("hash functions produced a cycle at load %d/%d: %w", t.Size(), t.TotalCapacity(), ErrBadHash)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := t.grow(); err != nil {
|
if err := t.grow(); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user