feat!: Drop returns bool, Put doesn't stack-overflow #21

Merged
mvhutz merged 12 commits from feat/safe-put into main 2026-04-17 01:31:02 +00:00
2 changed files with 13 additions and 13 deletions
Showing only changes of commit ca66ccd040 - Show all commits

View File

@@ -2,8 +2,8 @@ package cuckoo
// An Entry is a key-value pair.
type Entry[K, V any] struct {
key K
value V
Key K
Value V
}
type slot[K, V any] struct {
@@ -30,7 +30,7 @@ func (b bucket[K, V]) get(key K) (value V, found bool) {
}
slot := b.slots[b.location(key)]
return slot.value, slot.occupied && b.compare(slot.key, key)
return slot.Value, slot.occupied && b.compare(slot.Key, key)
}
func (b *bucket[K, V]) drop(key K) (occupied bool) {
@@ -40,7 +40,7 @@ func (b *bucket[K, V]) drop(key K) (occupied bool) {
slot := &b.slots[b.location(key)]
if slot.occupied && b.compare(slot.key, key) {
if slot.occupied && b.compare(slot.Key, key) {
slot.occupied = false
b.size--
return true
@@ -65,8 +65,8 @@ func (b bucket[K, V]) update(key K, value V) (updated bool) {
slot := &b.slots[b.location(key)]
if slot.occupied && b.compare(slot.key, key) {
slot.value = value
if slot.occupied && b.compare(slot.Key, key) {
slot.Value = value
return true
}
@@ -78,7 +78,7 @@ func (b *bucket[K, V]) insert(insertion Entry[K, V]) (evicted Entry[K, V], evict
return insertion, true
}
slot := &b.slots[b.location(insertion.key)]
slot := &b.slots[b.location(insertion.Key)]
if !slot.occupied {
slot.Entry = insertion
@@ -87,8 +87,8 @@ func (b *bucket[K, V]) insert(insertion Entry[K, V]) (evicted Entry[K, V], evict
return
}
if b.compare(slot.key, insertion.key) {
slot.value = insertion.value
if b.compare(slot.Key, insertion.Key) {
slot.Value = insertion.Value
return
}

View File

@@ -49,11 +49,11 @@ func (t Table[K, V]) load() float64 {
// size of the table. Returns a displaced entry and 'homeless = true' if an
// entry could not be placed after exhausting evictions.
func (t *Table[K, V]) insert(entry Entry[K, V]) (displaced Entry[K, V], homeless bool) {
if t.bucketA.update(entry.key, entry.value) {
if t.bucketA.update(entry.Key, entry.Value) {
return
}
if t.bucketB.update(entry.key, entry.value) {
if t.bucketB.update(entry.Key, entry.Value) {
return
}
@@ -190,7 +190,7 @@ func (t Table[K, V]) Entries() iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
for _, slot := range t.bucketA.slots {
if slot.occupied {
if !yield(slot.key, slot.value) {
if !yield(slot.Key, slot.Value) {
return
}
}
@@ -198,7 +198,7 @@ func (t Table[K, V]) Entries() iter.Seq2[K, V] {
for _, slot := range t.bucketB.slots {
if slot.occupied {
if !yield(slot.key, slot.value) {
if !yield(slot.Key, slot.Value) {
return
}
}