feat: new put implementation
This commit is contained in:
22
bucket.go
22
bucket.go
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user