feat!: Drop returns bool, Put doesn't stack-overflow
#21
@@ -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)
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
|
||||
11
table.go
11
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.
|
||||
|
||||
Reference in New Issue
Block a user