mirror of
https://github.com/status-im/nim-stew.git
synced 2025-01-09 11:45:42 +00:00
Stop mixing Result and Exceptions in some of the KeyedQueue APIs
This commit is contained in:
parent
b464505b4d
commit
bb705bf17b
@ -370,19 +370,22 @@ proc popValue*[K,V](rq: var KeyedQueue[K,V]):
|
|||||||
|
|
||||||
|
|
||||||
proc delete*[K,V](rq: var KeyedQueue[K,V]; key: K):
|
proc delete*[K,V](rq: var KeyedQueue[K,V]; key: K):
|
||||||
Result[KeyedQueuePair[K,V],void] {.gcsafe,raises: [Defect,KeyError].} =
|
Result[KeyedQueuePair[K,V], void] =
|
||||||
## Delete the item with key `key` from the queue and returns the key-value
|
## Delete the item with key `key` from the queue and returns the key-value
|
||||||
## item pair just deleted (if any).
|
## item pair just deleted (if any).
|
||||||
if rq.tab.hasKey(key):
|
if rq.tab.hasKey(key):
|
||||||
|
try:
|
||||||
let kvp = KeyedQueuePair[K,V](
|
let kvp = KeyedQueuePair[K,V](
|
||||||
key: key,
|
key: key,
|
||||||
data: rq.tab[key].data)
|
data: rq.tab[key].data)
|
||||||
rq.deleteImpl(key)
|
rq.deleteImpl(key)
|
||||||
return ok(kvp)
|
return ok(kvp)
|
||||||
|
except KeyError:
|
||||||
|
raiseAssert "We've checked that the key is present above"
|
||||||
err()
|
err()
|
||||||
|
|
||||||
proc del*[K,V](rq: var KeyedQueue[K,V]; key: K)
|
proc del*[K,V](rq: var KeyedQueue[K,V]; key: K)
|
||||||
{.gcsafe,raises: [Defect,KeyError].} =
|
{.gcsafe,raises: [Defect, KeyError].} =
|
||||||
## Similar to `delete()` but without return code.
|
## Similar to `delete()` but without return code.
|
||||||
if rq.tab.hasKey(key):
|
if rq.tab.hasKey(key):
|
||||||
rq.deleteImpl(key)
|
rq.deleteImpl(key)
|
||||||
@ -460,20 +463,23 @@ proc `[]`*[K,V](rq: var KeyedQueue[K,V]; key: K): V
|
|||||||
# Public functions, LRU mode
|
# Public functions, LRU mode
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
proc lruFetch*[K,V](rq: var KeyedQueue[K,V]; key: K): Result[V,void]
|
proc lruFetch*[K,V](rq: var KeyedQueue[K,V]; key: K): Result[V,void] =
|
||||||
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
||||||
## Fetch in *last-recently-used* mode: If the argument `key` exists in the
|
## Fetch in *last-recently-used* mode: If the argument `key` exists in the
|
||||||
## queue, move the key-value item pair to the *right end* (see `append()`)
|
## queue, move the key-value item pair to the *right end* (see `append()`)
|
||||||
## of the queue and return the value associated with the key.
|
## of the queue and return the value associated with the key.
|
||||||
let rc = rq.delete(key)
|
let rc = rq.delete(key)
|
||||||
if rc.isErr:
|
if rc.isErr:
|
||||||
return err()
|
return err()
|
||||||
|
|
||||||
# Unlink and re-append item
|
# Unlink and re-append item
|
||||||
|
try:
|
||||||
rq.appendImpl(key, rc.value.data)
|
rq.appendImpl(key, rc.value.data)
|
||||||
|
except KeyError:
|
||||||
|
raiseAssert "Not possible"
|
||||||
|
|
||||||
ok(rc.value.data)
|
ok(rc.value.data)
|
||||||
|
|
||||||
proc lruAppend*[K,V](rq: var KeyedQueue[K,V]; key: K; val: V; maxItems: int): V
|
proc lruAppend*[K,V](rq: var KeyedQueue[K,V]; key: K; val: V; maxItems: int): V =
|
||||||
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
||||||
## Append in *last-recently-used* mode: If the queue has at least `maxItems`
|
## Append in *last-recently-used* mode: If the queue has at least `maxItems`
|
||||||
## item entries, do `shift()` out the *left-most* one. Then `append()` the
|
## item entries, do `shift()` out the *left-most* one. Then `append()` the
|
||||||
## key-value argument pair `(key,val)` to the *right end*. Together with
|
## key-value argument pair `(key,val)` to the *right end*. Together with
|
||||||
@ -496,11 +502,14 @@ proc lruAppend*[K,V](rq: var KeyedQueue[K,V]; key: K; val: V; maxItems: int): V
|
|||||||
## err()
|
## err()
|
||||||
##
|
##
|
||||||
# Limit number of cached items
|
# Limit number of cached items
|
||||||
|
try:
|
||||||
if maxItems <= rq.tab.len:
|
if maxItems <= rq.tab.len:
|
||||||
rq.shiftImpl
|
rq.shiftImpl
|
||||||
# Append new value
|
# Append new value
|
||||||
rq.appendImpl(key, val)
|
rq.appendImpl(key, val)
|
||||||
val
|
return val
|
||||||
|
except KeyError:
|
||||||
|
raiseAssert "Not possible"
|
||||||
|
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
# Public traversal functions, fetch keys
|
# Public traversal functions, fetch keys
|
||||||
|
Loading…
x
Reference in New Issue
Block a user