added getOrDefault; bootstrapping works again
This commit is contained in:
parent
d8b0edc323
commit
2fda95a4d6
9 changed files with 52 additions and 40 deletions
|
|
@ -96,18 +96,10 @@ proc len*[A, B](t: Table[A, B]): int =
|
|||
## returns the number of keys in `t`.
|
||||
result = t.counter
|
||||
|
||||
proc `[]`*[A, B](t: Table[A, B], key: A): B =
|
||||
## retrieves the value at ``t[key]``. If `key` is not in `t`,
|
||||
## default empty value for the type `B` is returned
|
||||
## and no exception is raised. One can check with ``hasKey`` whether the key
|
||||
## exists.
|
||||
var hc: Hash
|
||||
var index = rawGet(t, key, hc)
|
||||
if index >= 0: result = t.data[index].val
|
||||
|
||||
proc mget*[A, B](t: var Table[A, B], key: A): var B =
|
||||
template get(t, key): untyped {.immediate.} =
|
||||
## retrieves the value at ``t[key]``. The value can be modified.
|
||||
## If `key` is not in `t`, the ``KeyError`` exception is raised.
|
||||
mixin rawGet
|
||||
var hc: Hash
|
||||
var index = rawGet(t, key, hc)
|
||||
if index >= 0: result = t.data[index].val
|
||||
|
|
@ -117,6 +109,12 @@ proc mget*[A, B](t: var Table[A, B], key: A): var B =
|
|||
else:
|
||||
raise newException(KeyError, "key not found")
|
||||
|
||||
template getOrDefaultImpl(t, key): untyped {.immediate.} =
|
||||
mixin rawGet
|
||||
var hc: Hash
|
||||
var index = rawGet(t, key, hc)
|
||||
if index >= 0: result = t.data[index].val
|
||||
|
||||
proc `[]`*[A, B](t: Table[A, B], key: A): B =
|
||||
## retrieves the value at ``t[key]``. If `key` is not in `t`, the
|
||||
## ``KeyError`` exception is raised. One can check with ``hasKey`` whether
|
||||
|
|
@ -134,6 +132,8 @@ proc mget*[A, B](t: var Table[A, B], key: A): var B {.deprecated.} =
|
|||
## instead.
|
||||
get(t, key)
|
||||
|
||||
proc getOrDefault*[A, B](t: Table[A, B], key: A): B = getOrDefaultImpl(t, key)
|
||||
|
||||
iterator allValues*[A, B](t: Table[A, B]; key: A): B =
|
||||
## iterates over any value in the table `t` that belongs to the given `key`.
|
||||
var h: Hash = hash(key) and high(t.data)
|
||||
|
|
@ -305,6 +305,8 @@ proc mget*[A, B](t: TableRef[A, B], key: A): var B {.deprecated.} =
|
|||
## Use ```[]``` instead.
|
||||
t[][key]
|
||||
|
||||
proc getOrDefault*[A, B](t: TableRef[A, B], key: A): B = getOrDefault(t[], key)
|
||||
|
||||
proc mgetOrPut*[A, B](t: TableRef[A, B], key: A, val: B): var B =
|
||||
## retrieves value at ``t[key]`` or puts ``val`` if not present, either way
|
||||
## returning a value which can be modified.
|
||||
|
|
@ -416,20 +418,6 @@ proc rawGetDeep[A, B](t: OrderedTable[A, B], key: A, hc: var Hash): int {.inline
|
|||
proc rawGet[A, B](t: OrderedTable[A, B], key: A, hc: var Hash): int =
|
||||
rawGetImpl()
|
||||
|
||||
proc `[]`*[A, B](t: OrderedTable[A, B], key: A): B =
|
||||
## retrieves the value at ``t[key]``. If `key` is not in `t`,
|
||||
## default empty value for the type `B` is returned
|
||||
## and no exception is raised. One can check with ``hasKey`` whether the key
|
||||
## exists.
|
||||
var hc: Hash
|
||||
var index = rawGet(t, key, hc)
|
||||
if index >= 0: result = t.data[index].val
|
||||
else:
|
||||
when compiles($key):
|
||||
raise newException(KeyError, "key not found: " & $key)
|
||||
else:
|
||||
raise newException(KeyError, "key not found")
|
||||
|
||||
proc `[]`*[A, B](t: OrderedTable[A, B], key: A): B =
|
||||
## retrieves the value at ``t[key]``. If `key` is not in `t`, the
|
||||
## ``KeyError`` exception is raised. One can check with ``hasKey`` whether
|
||||
|
|
@ -438,11 +426,18 @@ proc `[]`*[A, B](t: OrderedTable[A, B], key: A): B =
|
|||
|
||||
proc `[]`*[A, B](t: var OrderedTable[A, B], key: A): var B =
|
||||
## retrieves the value at ``t[key]``. The value can be modified.
|
||||
## If `key` is not in `t`, the ``EInvalidKey`` exception is raised.
|
||||
var hc: Hash
|
||||
var index = rawGet(t, key, hc)
|
||||
if index >= 0: result = t.data[index].val
|
||||
else: raise newException(KeyError, "key not found: " & $key)
|
||||
## If `key` is not in `t`, the ``KeyError`` exception is raised.
|
||||
get(t, key)
|
||||
|
||||
proc mget*[A, B](t: var OrderedTable[A, B], key: A): var B {.deprecated.} =
|
||||
## retrieves the value at ``t[key]``. The value can be modified.
|
||||
## If `key` is not in `t`, the ``KeyError`` exception is raised.
|
||||
## Use ```[]``` instead.
|
||||
get(t, key)
|
||||
|
||||
proc getOrDefault*[A, B](t: OrderedTable[A, B], key: A): B =
|
||||
getOrDefaultImpl(t, key)
|
||||
|
||||
|
||||
proc hasKey*[A, B](t: OrderedTable[A, B], key: A): bool =
|
||||
## returns true iff `key` is in the table `t`.
|
||||
|
|
@ -612,6 +607,9 @@ proc mget*[A, B](t: OrderedTableRef[A, B], key: A): var B {.deprecated.} =
|
|||
## Use ```[]``` instead.
|
||||
result = t[][key]
|
||||
|
||||
proc getOrDefault*[A, B](t: OrderedTableRef[A, B], key: A): B =
|
||||
getOrDefault(t[], key)
|
||||
|
||||
proc mgetOrPut*[A, B](t: OrderedTableRef[A, B], key: A, val: B): var B =
|
||||
## retrieves value at ``t[key]`` or puts ``val`` if not present, either way
|
||||
## returning a value which can be modified.
|
||||
|
|
@ -711,7 +709,7 @@ proc rawGet[A](t: CountTable[A], key: A): int =
|
|||
h = nextTry(h, high(t.data))
|
||||
result = -1 - h # < 0 => MISSING; insert idx = -1 - result
|
||||
|
||||
template get[A](t: CountTable[A], key: A): int {.immediate.} =
|
||||
template ctget(t, key: untyped): untyped {.immediate.} =
|
||||
var index = rawGet(t, key)
|
||||
if index >= 0: result = t.data[index].val
|
||||
else:
|
||||
|
|
@ -724,18 +722,22 @@ proc `[]`*[A](t: CountTable[A], key: A): int =
|
|||
## retrieves the value at ``t[key]``. If `key` is not in `t`,
|
||||
## the ``KeyError`` exception is raised. One can check with ``hasKey``
|
||||
## whether the key exists.
|
||||
get(t, key)
|
||||
ctget(t, key)
|
||||
|
||||
proc `[]`*[A](t: var CountTable[A], key: A): var int =
|
||||
## retrieves the value at ``t[key]``. The value can be modified.
|
||||
## If `key` is not in `t`, the ``KeyError`` exception is raised.
|
||||
get(t, key)
|
||||
ctget(t, key)
|
||||
|
||||
proc mget*[A](t: var CountTable[A], key: A): var int {.deprecated.} =
|
||||
## retrieves the value at ``t[key]``. The value can be modified.
|
||||
## If `key` is not in `t`, the ``KeyError`` exception is raised.
|
||||
## Use ```[]``` instead.
|
||||
get(t, key)
|
||||
ctget(t, key)
|
||||
|
||||
proc getOrDefault*[A](t: CountTable[A], key: A): int =
|
||||
var index = rawGet(t, key)
|
||||
if index >= 0: result = t.data[index].val
|
||||
|
||||
proc hasKey*[A](t: CountTable[A], key: A): bool =
|
||||
## returns true iff `key` is in the table `t`.
|
||||
|
|
@ -882,6 +884,9 @@ proc mget*[A](t: CountTableRef[A], key: A): var int {.deprecated.} =
|
|||
## Use ```[]``` instead.
|
||||
result = t[][key]
|
||||
|
||||
proc getOrDefault*[A](t: CountTableRef[A], key: A): int =
|
||||
getOrDefaultImpl(t, key)
|
||||
|
||||
proc hasKey*[A](t: CountTableRef[A], key: A): bool =
|
||||
## returns true iff `key` is in the table `t`.
|
||||
result = t[].hasKey(key)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue