3 Commits

Author SHA1 Message Date
572e59f33d Merge remote-tracking branch 'origin' into feat/get-no-error
All checks were successful
CI / Check PR Title (pull_request) Successful in 29s
CI / Go Lint (pull_request) Successful in 41s
CI / Makefile Lint (pull_request) Successful in 37s
CI / Markdown Lint (pull_request) Successful in 32s
CI / Unit Tests (pull_request) Successful in 39s
CI / Fuzz Tests (pull_request) Successful in 1m37s
CI / Mutation Tests (pull_request) Successful in 1m19s
2026-04-13 21:36:59 -04:00
867a1d49df feat: sentinel error ErrBadHash (#19)
All checks were successful
CI / Check PR Title (push) Has been skipped
CI / Makefile Lint (push) Successful in 1m4s
CI / Markdown Lint (push) Successful in 32s
CI / Go Lint (push) Successful in 1m15s
CI / Unit Tests (push) Successful in 38s
CI / Fuzz Tests (push) Successful in 1m34s
CI / Mutation Tests (push) Successful in 1m31s
## Description

Currently, the errors are not sentinel, and so are hard to test for. This PR makes sure hash collision errors are accounted for.

## Changes

- Add `ErrBadHash`. Happens when there are too many collisions for an item to be added.

### Design Decisions

- Chose to name `ErrBadHash` over `ErrCycle` because the feedbach that the user should be given is to evaluate their hash functions. Cycle collision is a bit esoteric.

## Checklist

- [x] Tests pass
- [x] Docs updated

Reviewed-on: #19
Co-authored-by: M.V. Hutz <git@maximhutz.me>
Co-committed-by: M.V. Hutz <git@maximhutz.me>
2026-04-14 00:38:11 +00:00
95687acade feat!: update contract for 'Table.Get()'
All checks were successful
CI / Check PR Title (pull_request) Successful in 19s
CI / Go Lint (pull_request) Successful in 42s
CI / Markdown Lint (pull_request) Successful in 22s
CI / Makefile Lint (pull_request) Successful in 40s
CI / Unit Tests (pull_request) Successful in 37s
CI / Fuzz Tests (pull_request) Successful in 1m12s
CI / Mutation Tests (pull_request) Successful in 1m18s
2026-04-03 21:27:48 +02:00
4 changed files with 29 additions and 21 deletions

View File

@@ -73,16 +73,16 @@ func FuzzInsertLookup(f *testing.F) {
delete(expected, step.key)
_, err = actual.Get(step.key)
assert.Error(err)
_, ok := actual.Get(step.key)
assert.False(ok)
} else {
err := actual.Put(step.key, step.value)
assert.NoError(err)
expected[step.key] = step.value
found, err := actual.Get(step.key)
assert.NoError(err)
found, ok := actual.Get(step.key)
assert.True(ok)
assert.Equal(step.value, found)
}

View File

@@ -108,12 +108,12 @@ func TestGetMany(t *testing.T) {
}
for i := range 2_000 {
value, err := table.Get(i)
value, ok := table.Get(i)
if i < 1_000 {
assert.NoError(err)
assert.True(ok)
assert.Equal(value, true)
} else {
assert.Error(err)
assert.False(ok)
}
}
}

View File

@@ -14,19 +14,19 @@ func Example_basic() {
fmt.Println("Put error:", err)
}
if item, err := table.Get(1); err != nil {
fmt.Println("Error:", err)
if item, ok := table.Get(1); !ok {
fmt.Println("Not Found 1!")
} else {
fmt.Println("Found 1:", item)
}
if item, err := table.Get(0); err != nil {
fmt.Println("Error:", err)
if item, ok := table.Get(0); !ok {
fmt.Println("Not Found 0!")
} else {
fmt.Println("Found 0:", item)
}
// Output:
// Found 1: Hello, World!
// Error: key '0' not found
// Not Found 0!
}

View File

@@ -1,12 +1,21 @@
package cuckoo
import (
"errors"
"fmt"
"iter"
"math/bits"
"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
// one with [NewTable]. Or if you want more granularity, use [NewTableBy] or
// [NewCustomTable].
@@ -86,24 +95,23 @@ func (t *Table[K, V]) shrink() error {
return t.resize(t.bucketA.capacity / t.growthFactor)
}
// Get fetches the value for a key in the [Table]. Returns an error if no value
// is found.
func (t Table[K, V]) Get(key K) (value V, err error) {
// Get fetches the value for a key in the [Table].
func (t Table[K, V]) Get(key K) (value V, ok bool) {
if item, ok := t.bucketA.get(key); ok {
return item, nil
return item, true
}
if item, ok := t.bucketB.get(key); ok {
return item, nil
return item, true
}
return value, fmt.Errorf("key '%v' not found", key)
return
}
// Has returns true if a key has a value in the table.
func (t Table[K, V]) Has(key K) (exists bool) {
_, err := t.Get(key)
return err == nil
_, exists = t.Get(key)
return
}
// Put sets the value for a key. Returns error if its value cannot be set.
@@ -128,7 +136,7 @@ func (t *Table[K, V]) Put(key K, value V) (err error) {
}
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 {