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
Showing only changes of commit 78f7d01d5f - Show all commits

View File

@@ -20,52 +20,52 @@ type subtable[K, V any] struct {
// location determines where in the bucket a certain key would be placed. If the
// capacity is 0, this will panic.
func (b *subtable[K, V]) location(key K) uint64 {
return b.hash(key) % b.capacity
func (t *subtable[K, V]) location(key K) uint64 {
return t.hash(key) % t.capacity
}
func (b *subtable[K, V]) get(key K) (value V, found bool) {
if b.capacity == 0 {
func (t *subtable[K, V]) get(key K) (value V, found bool) {
if t.capacity == 0 {
return
}
slot := b.slots[b.location(key)]
return slot.Value, slot.occupied && b.compare(slot.Key, key)
slot := t.slots[t.location(key)]
return slot.Value, slot.occupied && t.compare(slot.Key, key)
}
func (b *subtable[K, V]) drop(key K) (occupied bool) {
if b.capacity == 0 {
func (t *subtable[K, V]) drop(key K) (occupied bool) {
if t.capacity == 0 {
return
}
slot := &b.slots[b.location(key)]
slot := &t.slots[t.location(key)]
if slot.occupied && b.compare(slot.Key, key) {
if slot.occupied && t.compare(slot.Key, key) {
slot.occupied = false
b.size--
t.size--
return true
}
return false
}
func (b *subtable[K, V]) resized(capacity uint64) *subtable[K, V] {
func (t *subtable[K, V]) resized(capacity uint64) *subtable[K, V] {
return &subtable[K, V]{
slots: make([]slot[K, V], capacity),
capacity: capacity,
hash: b.hash,
compare: b.compare,
hash: t.hash,
compare: t.compare,
}
}
func (b *subtable[K, V]) update(key K, value V) (updated bool) {
if b.capacity == 0 {
func (t *subtable[K, V]) update(key K, value V) (updated bool) {
if t.capacity == 0 {
return
}
slot := &b.slots[b.location(key)]
slot := &t.slots[t.location(key)]
if slot.occupied && b.compare(slot.Key, key) {
if slot.occupied && t.compare(slot.Key, key) {
slot.Value = value
return true
}
@@ -73,21 +73,21 @@ func (b *subtable[K, V]) update(key K, value V) (updated bool) {
return false
}
func (b *subtable[K, V]) insert(insertion Entry[K, V]) (evicted Entry[K, V], eviction bool) {
if b.capacity == 0 {
func (t *subtable[K, V]) insert(insertion Entry[K, V]) (evicted Entry[K, V], eviction bool) {
if t.capacity == 0 {
return insertion, true
}
slot := &b.slots[b.location(insertion.Key)]
slot := &t.slots[t.location(insertion.Key)]
if !slot.occupied {
slot.Entry = insertion
slot.occupied = true
b.size++
t.size++
return
}
if b.compare(slot.Key, insertion.Key) {
if t.compare(slot.Key, insertion.Key) {
slot.Value = insertion.Value
return
}