mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 15:48:51 +00:00
Fix bugs in core:container/lru
This commit is contained in:
@@ -60,6 +60,8 @@ clear :: proc(c: ^$C/Cache($Key, $Value), call_on_remove: bool) {
|
|||||||
set :: proc(c: ^$C/Cache($Key, $Value), key: Key, value: Value) -> runtime.Allocator_Error {
|
set :: proc(c: ^$C/Cache($Key, $Value), key: Key, value: Value) -> runtime.Allocator_Error {
|
||||||
if e, ok := c.entries[key]; ok {
|
if e, ok := c.entries[key]; ok {
|
||||||
e.value = value
|
e.value = value
|
||||||
|
_pop_node(c, e)
|
||||||
|
_push_front_node(c, e)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,10 +69,14 @@ set :: proc(c: ^$C/Cache($Key, $Value), key: Key, value: Value) -> runtime.Alloc
|
|||||||
e.key = key
|
e.key = key
|
||||||
e.value = value
|
e.value = value
|
||||||
|
|
||||||
_push_front_node(c, e)
|
assert(c.count <= c.capacity)
|
||||||
if c.count > c.capacity {
|
if c.count == c.capacity {
|
||||||
_remove_node(c, c.tail)
|
_remove_node(c, c.tail)
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
c.count += 1
|
||||||
|
}
|
||||||
|
_push_front_node(c, e)
|
||||||
|
|
||||||
c.entries[key] = e
|
c.entries[key] = e
|
||||||
return nil
|
return nil
|
||||||
@@ -122,6 +128,7 @@ remove :: proc(c: ^$C/Cache($Key, $Value), key: Key) -> bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
_remove_node(c, e)
|
_remove_node(c, e)
|
||||||
|
c.count -= 1
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,8 +150,6 @@ _remove_node :: proc(c: ^$C/Cache($Key, $Value), node: ^Node(Key, Value)) {
|
|||||||
node.prev = nil
|
node.prev = nil
|
||||||
node.next = nil
|
node.next = nil
|
||||||
|
|
||||||
c.count -= 1
|
|
||||||
|
|
||||||
delete_key(&c.entries, node.key)
|
delete_key(&c.entries, node.key)
|
||||||
|
|
||||||
_call_on_remove(c, node)
|
_call_on_remove(c, node)
|
||||||
@@ -171,8 +176,6 @@ _push_front_node :: proc(c: ^$C/Cache($Key, $Value), e: ^Node(Key, Value)) {
|
|||||||
c.tail = e
|
c.tail = e
|
||||||
}
|
}
|
||||||
e.prev = nil
|
e.prev = nil
|
||||||
|
|
||||||
c.count += 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@(private)
|
@(private)
|
||||||
@@ -180,6 +183,12 @@ _pop_node :: proc(c: ^$C/Cache($Key, $Value), e: ^Node(Key, Value)) {
|
|||||||
if e == nil {
|
if e == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if c.head == e {
|
||||||
|
c.head = e.next
|
||||||
|
}
|
||||||
|
if c.tail == e {
|
||||||
|
c.tail = e.prev
|
||||||
|
}
|
||||||
if e.prev != nil {
|
if e.prev != nil {
|
||||||
e.prev.next = e.next
|
e.prev.next = e.next
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user