feat: add drop key functionality (#6)
Currently, the `Table.Drop()` function is deprecated because it is not implemented yet. Let's add that functionality. - Adds true drop functionality to the table, through `Table.Drop()`. - Adds tests for functionality. - Rewrites fuzz test using `go_fuzz_utils`, to test arbitrary usage patterns. - Rewrite `bucket` to allow a capacity of zero. - Rename `Table.Capacity()` to `Table.TotalCapacity()`, to reflect to different between the capacity of the buckets vs. the whole table. - Enforce 100% mutation test coverage. Reviewed-on: #6 Co-authored-by: M.V. Hutz <git@maximhutz.me> Co-committed-by: M.V. Hutz <git@maximhutz.me>
This commit was merged in pull request #6.
This commit is contained in:
62
table.go
62
table.go
@@ -16,9 +16,9 @@ type Table[K, V any] struct {
|
||||
minLoadFactor float64
|
||||
}
|
||||
|
||||
// Capacity returns the number of slots allocated for the [Table]. To get the
|
||||
// TotalCapacity returns the number of slots allocated for the [Table]. To get the
|
||||
// number of slots filled, look at [Table.Size].
|
||||
func (t Table[K, V]) Capacity() uint64 {
|
||||
func (t Table[K, V]) TotalCapacity() uint64 {
|
||||
return t.bucketA.capacity + t.bucketB.capacity
|
||||
}
|
||||
|
||||
@@ -28,25 +28,34 @@ func (t Table[K, V]) Size() int {
|
||||
}
|
||||
|
||||
func log2(n uint64) (m int) {
|
||||
return bits.Len64(n) - 1
|
||||
return max(0, bits.Len64(n)-1)
|
||||
}
|
||||
|
||||
func (t Table[K, V]) maxEvictions() int {
|
||||
return 3 * log2(t.Capacity())
|
||||
return 3 * log2(t.TotalCapacity())
|
||||
}
|
||||
|
||||
func (t Table[K, V]) load() float64 {
|
||||
return float64(t.Size()) / float64(t.Capacity())
|
||||
// When there are no slots in the table, we still treat the load as 100%.
|
||||
// Every slot in the table is full.
|
||||
if t.TotalCapacity() == 0 {
|
||||
return 1.0
|
||||
}
|
||||
|
||||
return float64(t.Size()) / float64(t.TotalCapacity())
|
||||
}
|
||||
|
||||
func (t *Table[K, V]) resize() error {
|
||||
// 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() {
|
||||
entries = append(entries, entry[K, V]{k, v})
|
||||
}
|
||||
|
||||
t.bucketA.resize(t.growthFactor * t.bucketA.capacity)
|
||||
t.bucketB.resize(t.growthFactor * t.bucketB.capacity)
|
||||
t.bucketA.resize(capacity)
|
||||
t.bucketB.resize(capacity)
|
||||
|
||||
for _, entry := range entries {
|
||||
if err := t.Put(entry.key, entry.value); err != nil {
|
||||
@@ -57,6 +66,26 @@ func (t *Table[K, V]) resize() 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
|
||||
|
||||
if t.TotalCapacity() == 0 {
|
||||
newCapacity = 1
|
||||
} else {
|
||||
newCapacity = t.bucketA.capacity * t.growthFactor
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// Get fetches the value for a key in the [Table]. Returns an error if no value
|
||||
// is found.
|
||||
func (t Table[K, V]) Get(key K) (value V, err error) {
|
||||
@@ -99,10 +128,10 @@ func (t *Table[K, V]) Put(key K, value V) (err error) {
|
||||
}
|
||||
|
||||
if t.load() < t.minLoadFactor {
|
||||
return fmt.Errorf("bad hash: resize on load %d/%d = %f", t.Size(), t.Capacity(), t.load())
|
||||
return fmt.Errorf("bad hash: resize on load %d/%d = %f", t.Size(), t.TotalCapacity(), t.load())
|
||||
}
|
||||
|
||||
if err := t.resize(); err != nil {
|
||||
if err := t.grow(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -111,10 +140,15 @@ func (t *Table[K, V]) Put(key K, value V) (err error) {
|
||||
|
||||
// Drop removes a value for a key in the table. Returns an error if its value
|
||||
// cannot be removed.
|
||||
//
|
||||
// Deprecated: Do not use.
|
||||
func (t Table[K, V]) Drop(_ K) {
|
||||
panic("Not implemented")
|
||||
func (t *Table[K, V]) Drop(key K) (err error) {
|
||||
t.bucketA.drop(key)
|
||||
t.bucketB.drop(key)
|
||||
|
||||
if t.load() < t.minLoadFactor {
|
||||
return t.shrink()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Entries returns an unordered sequence of all key-value pairs in the table.
|
||||
|
||||
Reference in New Issue
Block a user