feat: drop item returns bool, whether item existed
This commit is contained in:
@@ -68,12 +68,13 @@ func FuzzInsertLookup(f *testing.F) {
|
|||||||
|
|
||||||
for _, step := range scenario.steps {
|
for _, step := range scenario.steps {
|
||||||
if step.drop {
|
if step.drop {
|
||||||
err := actual.Drop(step.key)
|
ok := actual.Drop(step.key)
|
||||||
assert.NoError(err)
|
_, has := expected[step.key]
|
||||||
|
assert.Equal(ok, has)
|
||||||
|
|
||||||
delete(expected, step.key)
|
delete(expected, step.key)
|
||||||
|
|
||||||
_, err = actual.Get(step.key)
|
_, err := actual.Get(step.key)
|
||||||
assert.Error(err)
|
assert.Error(err)
|
||||||
} else {
|
} else {
|
||||||
_, err := actual.Put(step.key, step.value)
|
_, err := actual.Put(step.key, step.value)
|
||||||
|
|||||||
@@ -124,9 +124,9 @@ func TestDropExistingItem(t *testing.T) {
|
|||||||
table := cuckoo.NewTable[int, bool]()
|
table := cuckoo.NewTable[int, bool]()
|
||||||
(table.Put(key, value))
|
(table.Put(key, value))
|
||||||
|
|
||||||
err := table.Drop(key)
|
had := table.Drop(key)
|
||||||
|
|
||||||
assert.NoError(err)
|
assert.True(had)
|
||||||
assert.Equal(0, table.Size())
|
assert.Equal(0, table.Size())
|
||||||
assert.False(table.Has(key))
|
assert.False(table.Has(key))
|
||||||
}
|
}
|
||||||
@@ -136,9 +136,9 @@ func TestDropNoItem(t *testing.T) {
|
|||||||
key := 0
|
key := 0
|
||||||
table := cuckoo.NewTable[int, bool]()
|
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.Equal(0, table.Size())
|
||||||
assert.False(table.Has(key))
|
assert.False(table.Has(key))
|
||||||
}
|
}
|
||||||
@@ -152,10 +152,9 @@ func TestDropItemCapacity(t *testing.T) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
startingCapacity := table.TotalCapacity()
|
startingCapacity := table.TotalCapacity()
|
||||||
err := table.Drop(key)
|
table.Drop(key)
|
||||||
endingCapacity := table.TotalCapacity()
|
endingCapacity := table.TotalCapacity()
|
||||||
|
|
||||||
assert.NoError(err)
|
|
||||||
assert.Equal(0, table.Size())
|
assert.Equal(0, table.Size())
|
||||||
assert.Equal(uint64(128), startingCapacity)
|
assert.Equal(uint64(128), startingCapacity)
|
||||||
assert.Equal(uint64(64), endingCapacity)
|
assert.Equal(uint64(64), endingCapacity)
|
||||||
@@ -203,9 +202,9 @@ func TestDropResizeCapacity(t *testing.T) {
|
|||||||
|
|
||||||
_, err1 := table.Put(0, true)
|
_, err1 := table.Put(0, true)
|
||||||
_, err2 := table.Put(1, 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())
|
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)
|
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
|
// Drop removes a value for a key in the table. Returns whether the key had
|
||||||
// cannot be removed.
|
// existed.
|
||||||
func (t *Table[K, V]) Drop(key K) (err error) {
|
func (t *Table[K, V]) Drop(key K) bool {
|
||||||
t.bucketA.drop(key)
|
occupied := t.bucketA.drop(key) || t.bucketB.drop(key)
|
||||||
t.bucketB.drop(key)
|
|
||||||
|
|
||||||
if t.load() < t.minLoadFactor {
|
if t.load() < t.minLoadFactor {
|
||||||
// The error is not handled here, because table-shrinking is an internal
|
// 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()
|
t.shrink()
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return occupied
|
||||||
}
|
}
|
||||||
|
|
||||||
// Entries returns an unordered sequence of all key-value pairs in the table.
|
// Entries returns an unordered sequence of all key-value pairs in the table.
|
||||||
|
|||||||
Reference in New Issue
Block a user