feat: add drop key functionality #6

Merged
mvhutz merged 12 commits from feat/drop-item into main 2026-03-20 01:59:55 +00:00
2 changed files with 9 additions and 0 deletions
Showing only changes of commit 0c81bc5cae - Show all commits

View File

@@ -17,6 +17,8 @@ type bucket[K, V any] struct {
compare EqualFunc[K] compare EqualFunc[K]
} }
// location determines where in the bucket a certain key would be placed. If the
// capacity is 0, this will panic.
func (b bucket[K, V]) location(key K) uint64 { func (b bucket[K, V]) location(key K) uint64 {
return b.hash(key) % b.capacity return b.hash(key) % b.capacity
} }

View File

@@ -45,6 +45,9 @@ func (t Table[K, V]) load() float64 {
return float64(t.Size()) / float64(t.TotalCapacity()) return float64(t.Size()) / float64(t.TotalCapacity())
} }
// resize clears all buckets, changes the sizes of them to a specific capacity,
// and fills them back up again. It is a helper function for [Table.grow] and
// [Table.shrink]; use them instead.
func (t *Table[K, V]) resize(capacity uint64) error { func (t *Table[K, V]) resize(capacity uint64) error {
entries := make([]entry[K, V], 0, t.Size()) entries := make([]entry[K, V], 0, t.Size())
for k, v := range t.Entries() { for k, v := range t.Entries() {
@@ -63,6 +66,8 @@ func (t *Table[K, V]) resize(capacity uint64) error {
return nil return nil
} }
// grow increases the table's capacity by the [Table.growthFactor]. If the
// capacity is 0, it increases it to 1.
func (t *Table[K, V]) grow() error { func (t *Table[K, V]) grow() error {
var newCapacity uint64 var newCapacity uint64
@@ -75,6 +80,8 @@ func (t *Table[K, V]) grow() error {
return t.resize(newCapacity) return t.resize(newCapacity)
} }
// shrink reduces the table's capacity by the [Table.growthFactor]. It may
// reduce it down to 0.
func (t *Table[K, V]) shrink() error { func (t *Table[K, V]) shrink() error {
return t.resize(t.bucketA.capacity / t.growthFactor) return t.resize(t.bucketA.capacity / t.growthFactor)
} }