feat: drop item returns bool, whether item existed

This commit is contained in:
2026-04-04 00:20:34 +02:00
parent 05b633afca
commit afead3330a
3 changed files with 16 additions and 17 deletions

View File

@@ -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.