From afead3330a48572576d951c73b4c00330cad79ad Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Sat, 4 Apr 2026 00:20:34 +0200 Subject: [PATCH] feat: drop item returns bool, whether item existed --- cuckoo_fuzz_test.go | 7 ++++--- cuckoo_test.go | 15 +++++++-------- table.go | 11 +++++------ 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/cuckoo_fuzz_test.go b/cuckoo_fuzz_test.go index 8d30423..51aa112 100644 --- a/cuckoo_fuzz_test.go +++ b/cuckoo_fuzz_test.go @@ -68,12 +68,13 @@ func FuzzInsertLookup(f *testing.F) { for _, step := range scenario.steps { if step.drop { - err := actual.Drop(step.key) - assert.NoError(err) + ok := actual.Drop(step.key) + _, has := expected[step.key] + assert.Equal(ok, has) delete(expected, step.key) - _, err = actual.Get(step.key) + _, err := actual.Get(step.key) assert.Error(err) } else { _, err := actual.Put(step.key, step.value) diff --git a/cuckoo_test.go b/cuckoo_test.go index eac1734..738e501 100644 --- a/cuckoo_test.go +++ b/cuckoo_test.go @@ -124,9 +124,9 @@ func TestDropExistingItem(t *testing.T) { table := cuckoo.NewTable[int, bool]() (table.Put(key, value)) - err := table.Drop(key) + had := table.Drop(key) - assert.NoError(err) + assert.True(had) assert.Equal(0, table.Size()) assert.False(table.Has(key)) } @@ -136,9 +136,9 @@ func TestDropNoItem(t *testing.T) { key := 0 table := cuckoo.NewTable[int, bool]() - err := table.Drop(key) + had := table.Drop(key) - assert.NoError(err) + assert.False(had) assert.Equal(0, table.Size()) assert.False(table.Has(key)) } @@ -152,10 +152,9 @@ func TestDropItemCapacity(t *testing.T) { ) startingCapacity := table.TotalCapacity() - err := table.Drop(key) + table.Drop(key) endingCapacity := table.TotalCapacity() - assert.NoError(err) assert.Equal(0, table.Size()) assert.Equal(uint64(128), startingCapacity) assert.Equal(uint64(64), endingCapacity) @@ -203,9 +202,9 @@ func TestDropResizeCapacity(t *testing.T) { _, err1 := table.Put(0, true) _, err2 := table.Put(1, true) - err3 := table.Drop(1) + table.Drop(1) - assert.NoError(errors.Join(err1, err2, err3)) + assert.NoError(errors.Join(err1, err2)) assert.Equal(uint64(20), table.TotalCapacity()) } diff --git a/table.go b/table.go index e2b35ac..5aa0823 100644 --- a/table.go +++ b/table.go @@ -171,11 +171,10 @@ func (t *Table[K, V]) Put(key K, value V) (displaced Entry[K, V], err error) { return entry, fmt.Errorf("bad hash: could not place entry after %d resizes", defaultGrowthLimit) } -// Drop removes a value for a key in the table. Returns an error if its value -// cannot be removed. -func (t *Table[K, V]) Drop(key K) (err error) { - t.bucketA.drop(key) - t.bucketB.drop(key) +// Drop removes a value for a key in the table. Returns whether the key had +// existed. +func (t *Table[K, V]) Drop(key K) bool { + occupied := t.bucketA.drop(key) || t.bucketB.drop(key) if t.load() < t.minLoadFactor { // The error is not handled here, because table-shrinking is an internal @@ -183,7 +182,7 @@ func (t *Table[K, V]) Drop(key K) (err error) { t.shrink() } - return nil + return occupied } // Entries returns an unordered sequence of all key-value pairs in the table.