feat: new put implementation

This commit is contained in:
2026-04-04 00:13:50 +02:00
parent 322d71f0be
commit 05b633afca
7 changed files with 111 additions and 67 deletions

View File

@@ -1,12 +1,13 @@
package cuckoo
type entry[K, V any] struct {
// An Entry is a key-value pair.
type Entry[K, V any] struct {
key K
value V
}
type slot[K, V any] struct {
entry[K, V]
Entry[K, V]
occupied bool
}
@@ -48,10 +49,13 @@ func (b *bucket[K, V]) drop(key K) (occupied bool) {
return false
}
func (b *bucket[K, V]) resize(capacity uint64) {
b.slots = make([]slot[K, V], capacity)
b.capacity = capacity
b.size = 0
func (b *bucket[K, V]) resized(capacity uint64) bucket[K, V] {
return bucket[K, V]{
slots: make([]slot[K, V], capacity),
capacity: capacity,
hash: b.hash,
compare: b.compare,
}
}
func (b bucket[K, V]) update(key K, value V) (updated bool) {
@@ -69,7 +73,7 @@ func (b bucket[K, V]) update(key K, value V) (updated bool) {
return false
}
func (b *bucket[K, V]) evict(insertion entry[K, V]) (evicted entry[K, V], eviction bool) {
func (b *bucket[K, V]) insert(insertion Entry[K, V]) (evicted Entry[K, V], eviction bool) {
if b.capacity == 0 {
return insertion, true
}
@@ -77,7 +81,7 @@ func (b *bucket[K, V]) evict(insertion entry[K, V]) (evicted entry[K, V], evicti
slot := &b.slots[b.location(insertion.key)]
if !slot.occupied {
slot.entry = insertion
slot.Entry = insertion
slot.occupied = true
b.size++
return
@@ -88,7 +92,7 @@ func (b *bucket[K, V]) evict(insertion entry[K, V]) (evicted entry[K, V], evicti
return
}
insertion, slot.entry = slot.entry, insertion
insertion, slot.Entry = slot.Entry, insertion
return insertion, true
}