diff options
| author | ftphikari <ftphikari@gmail.com> | 2022-04-19 06:00:30 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-19 06:00:30 +0300 |
| commit | 240fb9b953eeabb07ff7bb75efa301da8c136d11 (patch) | |
| tree | cd2eb9604f2e209db6f7816055d2e7ec732545e9 /core/container | |
| parent | 4997a43763ca708700a9ec7171332c95095ad091 (diff) | |
| parent | d99ba9c073bcddce25a92d5af9afd59711df29d6 (diff) | |
Merge branch 'odin-lang:master' into master
Diffstat (limited to 'core/container')
| -rw-r--r-- | core/container/lru/lru_cache.odin | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/core/container/lru/lru_cache.odin b/core/container/lru/lru_cache.odin index f8e6f7b46..81f0142b0 100644 --- a/core/container/lru/lru_cache.odin +++ b/core/container/lru/lru_cache.odin @@ -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 { if e, ok := c.entries[key]; ok { e.value = value + _pop_node(c, e) + _push_front_node(c, e) return nil } @@ -67,10 +69,14 @@ set :: proc(c: ^$C/Cache($Key, $Value), key: Key, value: Value) -> runtime.Alloc e.key = key e.value = value - _push_front_node(c, e) - if c.count > c.capacity { + assert(c.count <= c.capacity) + if c.count == c.capacity { _remove_node(c, c.tail) } + else { + c.count += 1 + } + _push_front_node(c, e) c.entries[key] = e return nil @@ -122,6 +128,7 @@ remove :: proc(c: ^$C/Cache($Key, $Value), key: Key) -> bool { return false } _remove_node(c, e) + c.count -= 1 return true } @@ -143,8 +150,6 @@ _remove_node :: proc(c: ^$C/Cache($Key, $Value), node: ^Node(Key, Value)) { node.prev = nil node.next = nil - c.count -= 1 - delete_key(&c.entries, node.key) _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 } e.prev = nil - - c.count += 1 } @(private) @@ -180,6 +183,12 @@ _pop_node :: proc(c: ^$C/Cache($Key, $Value), e: ^Node(Key, Value)) { if e == nil { return } + if c.head == e { + c.head = e.next + } + if c.tail == e { + c.tail = e.prev + } if e.prev != nil { e.prev.next = e.next } |