feat: add drop key functionality #6
@@ -17,6 +17,8 @@ type bucket[K, V any] struct {
|
||||
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 {
|
||||
return b.hash(key) % b.capacity
|
||||
}
|
||||
|
||||
7
table.go
7
table.go
@@ -45,6 +45,9 @@ func (t Table[K, V]) load() float64 {
|
||||
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 {
|
||||
entries := make([]entry[K, V], 0, t.Size())
|
||||
for k, v := range t.Entries() {
|
||||
@@ -63,6 +66,8 @@ func (t *Table[K, V]) resize(capacity uint64) error {
|
||||
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 {
|
||||
var newCapacity uint64
|
||||
|
||||
@@ -75,6 +80,8 @@ func (t *Table[K, V]) grow() error {
|
||||
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 {
|
||||
return t.resize(t.bucketA.capacity / t.growthFactor)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user