Rename mget to []
- In sets, tables, strtabs, critbits, xmltree - This uses the new var parameter overloading - mget variants still exist, but are deprecated in favor of `[]` - Includes tests and fixed tests and usages of mget - The non-var `[]` now throws an exception instead of returning binary 0 or an empty string
This commit is contained in:
parent
3751019823
commit
63f9385327
12 changed files with 322 additions and 122 deletions
|
|
@ -17,11 +17,11 @@ type
|
||||||
otherbits: char
|
otherbits: char
|
||||||
case isLeaf: bool
|
case isLeaf: bool
|
||||||
of false: child: array[0..1, ref NodeObj[T]]
|
of false: child: array[0..1, ref NodeObj[T]]
|
||||||
of true:
|
of true:
|
||||||
key: string
|
key: string
|
||||||
when T isnot void:
|
when T isnot void:
|
||||||
val: T
|
val: T
|
||||||
|
|
||||||
Node[T] = ref NodeObj[T]
|
Node[T] = ref NodeObj[T]
|
||||||
CritBitTree*[T] = object ## The crit bit tree can either be used
|
CritBitTree*[T] = object ## The crit bit tree can either be used
|
||||||
## as a mapping from strings to
|
## as a mapping from strings to
|
||||||
|
|
@ -66,7 +66,7 @@ proc rawInsert[T](c: var CritBitTree[T], key: string): Node[T] =
|
||||||
let ch = if it.byte < key.len: key[it.byte] else: '\0'
|
let ch = if it.byte < key.len: key[it.byte] else: '\0'
|
||||||
let dir = (1 + (ch.ord or it.otherBits.ord)) shr 8
|
let dir = (1 + (ch.ord or it.otherBits.ord)) shr 8
|
||||||
it = it.child[dir]
|
it = it.child[dir]
|
||||||
|
|
||||||
var newOtherBits = 0
|
var newOtherBits = 0
|
||||||
var newByte = 0
|
var newByte = 0
|
||||||
block blockX:
|
block blockX:
|
||||||
|
|
@ -84,7 +84,7 @@ proc rawInsert[T](c: var CritBitTree[T], key: string): Node[T] =
|
||||||
newOtherBits = newOtherBits xor 255
|
newOtherBits = newOtherBits xor 255
|
||||||
let ch = it.key[newByte]
|
let ch = it.key[newByte]
|
||||||
let dir = (1 + (ord(ch) or newOtherBits)) shr 8
|
let dir = (1 + (ord(ch) or newOtherBits)) shr 8
|
||||||
|
|
||||||
var inner: Node[T]
|
var inner: Node[T]
|
||||||
new inner
|
new inner
|
||||||
new result
|
new result
|
||||||
|
|
@ -93,7 +93,7 @@ proc rawInsert[T](c: var CritBitTree[T], key: string): Node[T] =
|
||||||
inner.otherBits = chr(newOtherBits)
|
inner.otherBits = chr(newOtherBits)
|
||||||
inner.byte = newByte
|
inner.byte = newByte
|
||||||
inner.child[1 - dir] = result
|
inner.child[1 - dir] = result
|
||||||
|
|
||||||
var wherep = addr(c.root)
|
var wherep = addr(c.root)
|
||||||
while true:
|
while true:
|
||||||
var p = wherep[]
|
var p = wherep[]
|
||||||
|
|
@ -132,20 +132,31 @@ proc `[]=`*[T](c: var CritBitTree[T], key: string, val: T) =
|
||||||
var n = rawInsert(c, key)
|
var n = rawInsert(c, key)
|
||||||
n.val = val
|
n.val = val
|
||||||
|
|
||||||
proc `[]`*[T](c: CritBitTree[T], key: string): T {.inline.} =
|
template get[T](c: CritBitTree[T], key: string): T {.immediate.} =
|
||||||
## retrieves the value at ``c[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.
|
|
||||||
let n = rawGet(c, key)
|
let n = rawGet(c, key)
|
||||||
if n != nil: result = n.val
|
if n != nil: result = n.val
|
||||||
|
else:
|
||||||
|
when compiles($key):
|
||||||
|
raise newException(KeyError, "key not found: " & $key)
|
||||||
|
else:
|
||||||
|
raise newException(KeyError, "key not found")
|
||||||
|
|
||||||
proc mget*[T](c: var CritBitTree[T], key: string): var T {.inline.} =
|
proc `[]`*[T](c: CritBitTree[T], key: string): T {.inline.} =
|
||||||
|
## retrieves the value at ``c[key]``. If `key` is not in `t`, the
|
||||||
|
## ``KeyError`` exception is raised. One can check with ``hasKey`` whether
|
||||||
|
## the key exists.
|
||||||
|
get(c, key)
|
||||||
|
|
||||||
|
proc `[]`*[T](c: var CritBitTree[T], key: string): var T {.inline.} =
|
||||||
## retrieves the value at ``c[key]``. The value can be modified.
|
## retrieves the value at ``c[key]``. The value can be modified.
|
||||||
## If `key` is not in `t`, the ``KeyError`` exception is raised.
|
## If `key` is not in `t`, the ``KeyError`` exception is raised.
|
||||||
let n = rawGet(c, key)
|
get(c, key)
|
||||||
if n != nil: result = n.val
|
|
||||||
else: raise newException(KeyError, "key not found: " & $key)
|
proc mget*[T](c: var CritBitTree[T], key: string): var T {.inline, deprecated.} =
|
||||||
|
## retrieves the value at ``c[key]``. The value can be modified.
|
||||||
|
## If `key` is not in `t`, the ``KeyError`` exception is raised.
|
||||||
|
## Use ```[]``` instead.
|
||||||
|
get(c, key)
|
||||||
|
|
||||||
proc excl*[T](c: var CritBitTree[T], key: string) =
|
proc excl*[T](c: var CritBitTree[T], key: string) =
|
||||||
## removes `key` (and its associated value) from the set `c`.
|
## removes `key` (and its associated value) from the set `c`.
|
||||||
|
|
@ -176,7 +187,7 @@ iterator leaves[T](n: Node[T]): Node[T] =
|
||||||
# XXX actually we could compute the necessary stack size in advance:
|
# XXX actually we could compute the necessary stack size in advance:
|
||||||
# it's roughly log2(c.count).
|
# it's roughly log2(c.count).
|
||||||
var stack = @[n]
|
var stack = @[n]
|
||||||
while stack.len > 0:
|
while stack.len > 0:
|
||||||
var it = stack.pop
|
var it = stack.pop
|
||||||
while not it.isLeaf:
|
while not it.isLeaf:
|
||||||
stack.add(it.child[1])
|
stack.add(it.child[1])
|
||||||
|
|
@ -205,7 +216,7 @@ iterator items*[T](c: CritBitTree[T]): string =
|
||||||
iterator pairs*[T](c: CritBitTree[T]): tuple[key: string, val: T] =
|
iterator pairs*[T](c: CritBitTree[T]): tuple[key: string, val: T] =
|
||||||
## yields all (key, value)-pairs of `c`.
|
## yields all (key, value)-pairs of `c`.
|
||||||
for x in leaves(c.root): yield (x.key, x.val)
|
for x in leaves(c.root): yield (x.key, x.val)
|
||||||
|
|
||||||
iterator mpairs*[T](c: var CritBitTree[T]): tuple[key: string, val: var T] =
|
iterator mpairs*[T](c: var CritBitTree[T]): tuple[key: string, val: var T] =
|
||||||
## yields all (key, value)-pairs of `c`. The yielded values can be modified.
|
## yields all (key, value)-pairs of `c`. The yielded values can be modified.
|
||||||
for x in leaves(c.root): yield (x.key, x.val)
|
for x in leaves(c.root): yield (x.key, x.val)
|
||||||
|
|
@ -251,7 +262,7 @@ iterator pairsWithPrefix*[T](c: CritBitTree[T],
|
||||||
## yields all (key, value)-pairs of `c` starting with `prefix`.
|
## yields all (key, value)-pairs of `c` starting with `prefix`.
|
||||||
let top = allprefixedAux(c, prefix)
|
let top = allprefixedAux(c, prefix)
|
||||||
for x in leaves(top): yield (x.key, x.val)
|
for x in leaves(top): yield (x.key, x.val)
|
||||||
|
|
||||||
iterator mpairsWithPrefix*[T](c: var CritBitTree[T],
|
iterator mpairsWithPrefix*[T](c: var CritBitTree[T],
|
||||||
prefix: string): tuple[key: string, val: var T] =
|
prefix: string): tuple[key: string, val: var T] =
|
||||||
## yields all (key, value)-pairs of `c` starting with `prefix`.
|
## yields all (key, value)-pairs of `c` starting with `prefix`.
|
||||||
|
|
@ -297,7 +308,6 @@ when isMainModule:
|
||||||
|
|
||||||
for w in r.items:
|
for w in r.items:
|
||||||
echo w
|
echo w
|
||||||
|
|
||||||
for w in r.itemsWithPrefix("de"):
|
for w in r.itemsWithPrefix("de"):
|
||||||
echo w
|
echo w
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -154,7 +154,7 @@ proc rawGetKnownHC[A](s: HashSet[A], key: A, hc: THash): int {.inline.} =
|
||||||
proc rawGet[A](s: HashSet[A], key: A, hc: var THash): int {.inline.} =
|
proc rawGet[A](s: HashSet[A], key: A, hc: var THash): int {.inline.} =
|
||||||
rawGetImpl()
|
rawGetImpl()
|
||||||
|
|
||||||
proc mget*[A](s: var HashSet[A], key: A): var A =
|
proc `[]`*[A](s: var HashSet[A], key: A): var A =
|
||||||
## returns the element that is actually stored in 's' which has the same
|
## returns the element that is actually stored in 's' which has the same
|
||||||
## value as 'key' or raises the ``EInvalidKey`` exception. This is useful
|
## value as 'key' or raises the ``EInvalidKey`` exception. This is useful
|
||||||
## when one overloaded 'hash' and '==' but still needs reference semantics
|
## when one overloaded 'hash' and '==' but still needs reference semantics
|
||||||
|
|
@ -165,6 +165,13 @@ proc mget*[A](s: var HashSet[A], key: A): var A =
|
||||||
if index >= 0: result = s.data[index].key
|
if index >= 0: result = s.data[index].key
|
||||||
else: raise newException(KeyError, "key not found: " & $key)
|
else: raise newException(KeyError, "key not found: " & $key)
|
||||||
|
|
||||||
|
proc mget*[A](s: var HashSet[A], key: A): var A {.deprecated.} =
|
||||||
|
## returns the element that is actually stored in 's' which has the same
|
||||||
|
## value as 'key' or raises the ``EInvalidKey`` exception. This is useful
|
||||||
|
## when one overloaded 'hash' and '==' but still needs reference semantics
|
||||||
|
## for sharing. Use ```[]``` instead.
|
||||||
|
s[key]
|
||||||
|
|
||||||
proc contains*[A](s: HashSet[A], key: A): bool =
|
proc contains*[A](s: HashSet[A], key: A): bool =
|
||||||
## Returns true iff `key` is in `s`.
|
## Returns true iff `key` is in `s`.
|
||||||
##
|
##
|
||||||
|
|
|
||||||
|
|
@ -181,18 +181,7 @@ proc rawGetDeep[A, B](t: Table[A, B], key: A, hc: var THash): int {.inline.} =
|
||||||
proc rawGet[A, B](t: Table[A, B], key: A, hc: var THash): int {.inline.} =
|
proc rawGet[A, B](t: Table[A, B], key: A, hc: var THash): int {.inline.} =
|
||||||
rawGetImpl()
|
rawGetImpl()
|
||||||
|
|
||||||
proc `[]`*[A, B](t: Table[A, B], key: A): B =
|
template get[A, B](t: Table[A, B], key: A): B {.immediate.} =
|
||||||
## 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: THash
|
|
||||||
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 =
|
|
||||||
## retrieves the value at ``t[key]``. The value can be modified.
|
|
||||||
## If `key` is not in `t`, the ``KeyError`` exception is raised.
|
|
||||||
var hc: THash
|
var hc: THash
|
||||||
var index = rawGet(t, key, hc)
|
var index = rawGet(t, key, hc)
|
||||||
if index >= 0: result = t.data[index].val
|
if index >= 0: result = t.data[index].val
|
||||||
|
|
@ -202,6 +191,23 @@ proc mget*[A, B](t: var Table[A, B], key: A): var B =
|
||||||
else:
|
else:
|
||||||
raise newException(KeyError, "key not found")
|
raise newException(KeyError, "key not found")
|
||||||
|
|
||||||
|
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
|
||||||
|
## the key exists.
|
||||||
|
get(t, key)
|
||||||
|
|
||||||
|
proc `[]`*[A, B](t: var Table[A, B], key: A): var B =
|
||||||
|
## 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)
|
||||||
|
|
||||||
|
proc mget*[A, B](t: var Table[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)
|
||||||
|
|
||||||
iterator allValues*[A, B](t: Table[A, B]; key: A): B =
|
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`.
|
## iterates over any value in the table `t` that belongs to the given `key`.
|
||||||
var h: THash = hash(key) and high(t.data)
|
var h: THash = hash(key) and high(t.data)
|
||||||
|
|
@ -386,17 +392,17 @@ iterator mvalues*[A, B](t: TableRef[A, B]): var B =
|
||||||
for h in 0..high(t.data):
|
for h in 0..high(t.data):
|
||||||
if isFilled(t.data[h].hcode): yield t.data[h].val
|
if isFilled(t.data[h].hcode): yield t.data[h].val
|
||||||
|
|
||||||
proc `[]`*[A, B](t: TableRef[A, B], key: A): B =
|
proc `[]`*[A, B](t: TableRef[A, B], key: A): var B =
|
||||||
## retrieves the value at ``t[key]``. If `key` is not in `t`,
|
## retrieves the value at ``t[key]``. If `key` is not in `t`, the
|
||||||
## default empty value for the type `B` is returned
|
## ``KeyError`` exception is raised. One can check with ``hasKey`` whether
|
||||||
## and no exception is raised. One can check with ``hasKey`` whether the key
|
## the key exists.
|
||||||
## exists.
|
|
||||||
result = t[][key]
|
result = t[][key]
|
||||||
|
|
||||||
proc mget*[A, B](t: TableRef[A, B], key: A): var B =
|
proc mget*[A, B](t: TableRef[A, B], key: A): var B {.deprecated.} =
|
||||||
## retrieves the value at ``t[key]``. The value can be modified.
|
## retrieves the value at ``t[key]``. The value can be modified.
|
||||||
## If `key` is not in `t`, the ``EInvalidKey`` exception is raised.
|
## If `key` is not in `t`, the ``KeyError`` exception is raised.
|
||||||
t[].mget(key)
|
## Use ```[]``` instead.
|
||||||
|
t[][key]
|
||||||
|
|
||||||
proc mgetOrPut*[A, B](t: TableRef[A, B], key: A, val: B): var B =
|
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
|
## retrieves value at ``t[key]`` or puts ``val`` if not present, either way
|
||||||
|
|
@ -510,22 +516,32 @@ proc rawGetDeep[A, B](t: OrderedTable[A, B], key: A, hc: var THash): int {.inlin
|
||||||
proc rawGet[A, B](t: OrderedTable[A, B], key: A, hc: var THash): int =
|
proc rawGet[A, B](t: OrderedTable[A, B], key: A, hc: var THash): int =
|
||||||
rawGetImpl()
|
rawGetImpl()
|
||||||
|
|
||||||
proc `[]`*[A, B](t: OrderedTable[A, B], key: A): B =
|
template get[A, B](t: OrderedTable[A, B], key: A): B {.immediate.} =
|
||||||
## 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: THash
|
var hc: THash
|
||||||
var index = rawGet(t, key, hc)
|
var index = rawGet(t, key, hc)
|
||||||
if index >= 0: result = t.data[index].val
|
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 mget*[A, B](t: var OrderedTable[A, B], key: A): var B =
|
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
|
||||||
|
## the key exists.
|
||||||
|
get(t, key)
|
||||||
|
|
||||||
|
proc `[]`*[A, B](t: var OrderedTable[A, B], key: A): var B =
|
||||||
## retrieves the value at ``t[key]``. The value can be modified.
|
## retrieves the value at ``t[key]``. The value can be modified.
|
||||||
## If `key` is not in `t`, the ``EInvalidKey`` exception is raised.
|
## If `key` is not in `t`, the ``KeyError`` exception is raised.
|
||||||
var hc: THash
|
get(t, key)
|
||||||
var index = rawGet(t, key, hc)
|
|
||||||
if index >= 0: result = t.data[index].val
|
proc mget*[A, B](t: var OrderedTable[A, B], key: A): var B {.deprecated.} =
|
||||||
else: raise newException(KeyError, "key not found: " & $key)
|
## 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 hasKey*[A, B](t: OrderedTable[A, B], key: A): bool =
|
proc hasKey*[A, B](t: OrderedTable[A, B], key: A): bool =
|
||||||
## returns true iff `key` is in the table `t`.
|
## returns true iff `key` is in the table `t`.
|
||||||
|
|
@ -679,17 +695,17 @@ iterator mvalues*[A, B](t: OrderedTableRef[A, B]): var B =
|
||||||
forAllOrderedPairs:
|
forAllOrderedPairs:
|
||||||
yield t.data[h].val
|
yield t.data[h].val
|
||||||
|
|
||||||
proc `[]`*[A, B](t: OrderedTableRef[A, B], key: A): B =
|
proc `[]`*[A, B](t: OrderedTableRef[A, B], key: A): var B =
|
||||||
## retrieves the value at ``t[key]``. If `key` is not in `t`,
|
## retrieves the value at ``t[key]``. If `key` is not in `t`, the
|
||||||
## default empty value for the type `B` is returned
|
## ``KeyError`` exception is raised. One can check with ``hasKey`` whether
|
||||||
## and no exception is raised. One can check with ``hasKey`` whether the key
|
## the key exists.
|
||||||
## exists.
|
|
||||||
result = t[][key]
|
result = t[][key]
|
||||||
|
|
||||||
proc mget*[A, B](t: OrderedTableRef[A, B], key: A): var B =
|
proc mget*[A, B](t: OrderedTableRef[A, B], key: A): var B {.deprecated.} =
|
||||||
## retrieves the value at ``t[key]``. The value can be modified.
|
## retrieves the value at ``t[key]``. The value can be modified.
|
||||||
## If `key` is not in `t`, the ``EInvalidKey`` exception is raised.
|
## If `key` is not in `t`, the ``KeyError`` exception is raised.
|
||||||
result = t[].mget(key)
|
## Use ```[]``` instead.
|
||||||
|
result = t[][key]
|
||||||
|
|
||||||
proc mgetOrPut*[A, B](t: OrderedTableRef[A, B], key: A, val: B): var B =
|
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
|
## retrieves value at ``t[key]`` or puts ``val`` if not present, either way
|
||||||
|
|
@ -786,19 +802,31 @@ proc rawGet[A](t: CountTable[A], key: A): int =
|
||||||
h = nextTry(h, high(t.data))
|
h = nextTry(h, high(t.data))
|
||||||
result = -1 - h # < 0 => MISSING; insert idx = -1 - result
|
result = -1 - h # < 0 => MISSING; insert idx = -1 - result
|
||||||
|
|
||||||
|
template get[A](t: CountTable[A], key: A): int {.immediate.} =
|
||||||
|
var index = rawGet(t, key)
|
||||||
|
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](t: CountTable[A], key: A): int =
|
proc `[]`*[A](t: CountTable[A], key: A): int =
|
||||||
## retrieves the value at ``t[key]``. If `key` is not in `t`,
|
## retrieves the value at ``t[key]``. If `key` is not in `t`,
|
||||||
## 0 is returned. One can check with ``hasKey`` whether the key
|
## the ``KeyError`` exception is raised. One can check with ``hasKey``
|
||||||
## exists.
|
## whether the key exists.
|
||||||
var index = rawGet(t, key)
|
get(t, key)
|
||||||
if index >= 0: result = t.data[index].val
|
|
||||||
|
|
||||||
proc mget*[A](t: var CountTable[A], key: A): var int =
|
proc `[]`*[A](t: var CountTable[A], key: A): var int =
|
||||||
## retrieves the value at ``t[key]``. The value can be modified.
|
## retrieves the value at ``t[key]``. The value can be modified.
|
||||||
## If `key` is not in `t`, the ``EInvalidKey`` exception is raised.
|
## If `key` is not in `t`, the ``KeyError`` exception is raised.
|
||||||
var index = rawGet(t, key)
|
get(t, key)
|
||||||
if index >= 0: result = t.data[index].val
|
|
||||||
else: raise newException(KeyError, "key not found: " & $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)
|
||||||
|
|
||||||
proc hasKey*[A](t: CountTable[A], key: A): bool =
|
proc hasKey*[A](t: CountTable[A], key: A): bool =
|
||||||
## returns true iff `key` is in the table `t`.
|
## returns true iff `key` is in the table `t`.
|
||||||
|
|
@ -927,16 +955,16 @@ iterator mvalues*[A](t: CountTableRef[A]): var int =
|
||||||
for h in 0..high(t.data):
|
for h in 0..high(t.data):
|
||||||
if t.data[h].val != 0: yield t.data[h].val
|
if t.data[h].val != 0: yield t.data[h].val
|
||||||
|
|
||||||
proc `[]`*[A](t: CountTableRef[A], key: A): int =
|
proc `[]`*[A](t: CountTableRef[A], key: A): var int =
|
||||||
## retrieves the value at ``t[key]``. If `key` is not in `t`,
|
## retrieves the value at ``t[key]``. The value can be modified.
|
||||||
## 0 is returned. One can check with ``hasKey`` whether the key
|
## If `key` is not in `t`, the ``KeyError`` exception is raised.
|
||||||
## exists.
|
|
||||||
result = t[][key]
|
result = t[][key]
|
||||||
|
|
||||||
proc mget*[A](t: CountTableRef[A], key: A): var int =
|
proc mget*[A](t: CountTableRef[A], key: A): var int {.deprecated.} =
|
||||||
## retrieves the value at ``t[key]``. The value can be modified.
|
## retrieves the value at ``t[key]``. The value can be modified.
|
||||||
## If `key` is not in `t`, the ``EInvalidKey`` exception is raised.
|
## If `key` is not in `t`, the ``KeyError`` exception is raised.
|
||||||
result = t[].mget(key)
|
## Use ```[]``` instead.
|
||||||
|
result = t[][key]
|
||||||
|
|
||||||
proc hasKey*[A](t: CountTableRef[A], key: A): bool =
|
proc hasKey*[A](t: CountTableRef[A], key: A): bool =
|
||||||
## returns true iff `key` is in the table `t`.
|
## returns true iff `key` is in the table `t`.
|
||||||
|
|
|
||||||
|
|
@ -101,21 +101,26 @@ proc rawGet(t: StringTableRef, key: string): int =
|
||||||
h = nextTry(h, high(t.data))
|
h = nextTry(h, high(t.data))
|
||||||
result = - 1
|
result = - 1
|
||||||
|
|
||||||
proc `[]`*(t: StringTableRef, key: string): string {.rtl, extern: "nstGet".} =
|
template get(t: StringTableRef, key: string): stmt {.immediate.} =
|
||||||
## retrieves the value at ``t[key]``. If `key` is not in `t`, "" is returned
|
|
||||||
## and no exception is raised. One can check with ``hasKey`` whether the key
|
|
||||||
## exists.
|
|
||||||
var index = rawGet(t, key)
|
var index = rawGet(t, key)
|
||||||
if index >= 0: result = t.data[index].val
|
if index >= 0: result = t.data[index].val
|
||||||
else: result = ""
|
else:
|
||||||
|
when compiles($key):
|
||||||
|
raise newException(KeyError, "key not found: " & $key)
|
||||||
|
else:
|
||||||
|
raise newException(KeyError, "key not found")
|
||||||
|
|
||||||
proc mget*(t: StringTableRef, key: string): var string {.
|
proc `[]`*(t: StringTableRef, key: string): var string {.
|
||||||
rtl, extern: "nstTake".} =
|
rtl, extern: "nstTake".} =
|
||||||
## retrieves the location at ``t[key]``. If `key` is not in `t`, the
|
## retrieves the location at ``t[key]``. If `key` is not in `t`, the
|
||||||
## ``KeyError`` exception is raised.
|
## ``KeyError`` exception is raised. One can check with ``hasKey`` whether
|
||||||
var index = rawGet(t, key)
|
## the key exists.
|
||||||
if index >= 0: result = t.data[index].val
|
get(t, key)
|
||||||
else: raise newException(KeyError, "key does not exist: " & key)
|
|
||||||
|
proc mget*(t: StringTableRef, key: string): var string {.deprecated.} =
|
||||||
|
## retrieves the location at ``t[key]``. If `key` is not in `t`, the
|
||||||
|
## ``KeyError`` exception is raised. Use ```[]``` instead.
|
||||||
|
get(t, key)
|
||||||
|
|
||||||
proc hasKey*(t: StringTableRef, key: string): bool {.rtl, extern: "nst$1".} =
|
proc hasKey*(t: StringTableRef, key: string): bool {.rtl, extern: "nst$1".} =
|
||||||
## returns true iff `key` is in the table `t`.
|
## returns true iff `key` is in the table `t`.
|
||||||
|
|
@ -227,7 +232,7 @@ proc `$`*(t: StringTableRef): string {.rtl, extern: "nstDollar".} =
|
||||||
result = "{:}"
|
result = "{:}"
|
||||||
else:
|
else:
|
||||||
result = "{"
|
result = "{"
|
||||||
for key, val in pairs(t):
|
for key, val in pairs(t):
|
||||||
if result.len > 1: result.add(", ")
|
if result.len > 1: result.add(", ")
|
||||||
result.add(key)
|
result.add(key)
|
||||||
result.add(": ")
|
result.add(": ")
|
||||||
|
|
@ -239,6 +244,6 @@ when isMainModule:
|
||||||
assert x["k"] == "v"
|
assert x["k"] == "v"
|
||||||
assert x["11"] == "22"
|
assert x["11"] == "22"
|
||||||
assert x["565"] == "67"
|
assert x["565"] == "67"
|
||||||
x.mget("11") = "23"
|
x["11"] = "23"
|
||||||
assert x["11"] == "23"
|
assert x["11"] == "23"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -115,11 +115,16 @@ proc `[]`* (n: XmlNode, i: int): XmlNode {.inline.} =
|
||||||
assert n.k == xnElement
|
assert n.k == xnElement
|
||||||
result = n.s[i]
|
result = n.s[i]
|
||||||
|
|
||||||
proc mget* (n: var XmlNode, i: int): var XmlNode {.inline.} =
|
proc `[]`* (n: var XmlNode, i: int): var XmlNode {.inline.} =
|
||||||
## returns the `i`'th child of `n` so that it can be modified
|
## returns the `i`'th child of `n` so that it can be modified
|
||||||
assert n.k == xnElement
|
assert n.k == xnElement
|
||||||
result = n.s[i]
|
result = n.s[i]
|
||||||
|
|
||||||
|
proc mget* (n: var XmlNode, i: int): var XmlNode {.inline, deprecated.} =
|
||||||
|
## returns the `i`'th child of `n` so that it can be modified. Use ```[]```
|
||||||
|
## instead.
|
||||||
|
n[i]
|
||||||
|
|
||||||
iterator items*(n: XmlNode): XmlNode {.inline.} =
|
iterator items*(n: XmlNode): XmlNode {.inline.} =
|
||||||
## iterates over any child of `n`.
|
## iterates over any child of `n`.
|
||||||
assert n.k == xnElement
|
assert n.k == xnElement
|
||||||
|
|
@ -128,7 +133,7 @@ iterator items*(n: XmlNode): XmlNode {.inline.} =
|
||||||
iterator mitems*(n: var XmlNode): var XmlNode {.inline.} =
|
iterator mitems*(n: var XmlNode): var XmlNode {.inline.} =
|
||||||
## iterates over any child of `n`.
|
## iterates over any child of `n`.
|
||||||
assert n.k == xnElement
|
assert n.k == xnElement
|
||||||
for i in 0 .. n.len-1: yield mget(n, i)
|
for i in 0 .. n.len-1: yield n[i]
|
||||||
|
|
||||||
proc attrs*(n: XmlNode): XmlAttributes {.inline.} =
|
proc attrs*(n: XmlNode): XmlAttributes {.inline.} =
|
||||||
## gets the attributes belonging to `n`.
|
## gets the attributes belonging to `n`.
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ proc countBits32(n: int32): int {.compilerproc.} =
|
||||||
v = (v and 0x33333333'i32) +% ((v shr 2'i32) and 0x33333333'i32)
|
v = (v and 0x33333333'i32) +% ((v shr 2'i32) and 0x33333333'i32)
|
||||||
result = ((v +% (v shr 4'i32) and 0xF0F0F0F'i32) *% 0x1010101'i32) shr 24'i32
|
result = ((v +% (v shr 4'i32) and 0xF0F0F0F'i32) *% 0x1010101'i32) shr 24'i32
|
||||||
|
|
||||||
proc countBits64(n: int64): int {.compilerproc.} =
|
proc countBits64(n: int64): int {.compilerproc.} =
|
||||||
result = countBits32(toU32(n and 0xffff'i64)) +
|
result = countBits32(toU32(n and 0xffff'i64)) +
|
||||||
countBits32(toU32(n shr 16'i64))
|
countBits32(toU32(n shr 16'i64))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,24 +4,23 @@ discard """
|
||||||
|
|
||||||
import tables
|
import tables
|
||||||
|
|
||||||
proc action1(arg: string) =
|
proc action1(arg: string) =
|
||||||
echo "action 1 ", arg
|
echo "action 1 ", arg
|
||||||
|
|
||||||
proc action2(arg: string) =
|
proc action2(arg: string) =
|
||||||
echo "action 2 ", arg
|
echo "action 2 ", arg
|
||||||
|
|
||||||
proc action3(arg: string) =
|
proc action3(arg: string) =
|
||||||
echo "action 3 ", arg
|
echo "action 3 ", arg
|
||||||
|
|
||||||
proc action4(arg: string) =
|
proc action4(arg: string) =
|
||||||
echo "action 4 ", arg
|
echo "action 4 ", arg
|
||||||
|
|
||||||
var
|
var
|
||||||
actionTable = {
|
actionTable = {
|
||||||
"A": action1,
|
"A": action1,
|
||||||
"B": action2,
|
"B": action2,
|
||||||
"C": action3,
|
"C": action3,
|
||||||
"D": action4}.toTable
|
"D": action4}.toTable
|
||||||
|
|
||||||
actionTable["C"]("arg")
|
actionTable["C"]("arg")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,15 +22,15 @@ const
|
||||||
"---00": 346677844,
|
"---00": 346677844,
|
||||||
"0": 34404,
|
"0": 34404,
|
||||||
"1": 344004,
|
"1": 344004,
|
||||||
"10": 34484,
|
"10": 34484,
|
||||||
"11": 34474,
|
"11": 34474,
|
||||||
"12": 789,
|
"12": 789,
|
||||||
"19": 34464,
|
"19": 34464,
|
||||||
"2": 344774, "20": 34454,
|
"2": 344774, "20": 34454,
|
||||||
"3": 342244, "30": 34141244,
|
"3": 342244, "30": 34141244,
|
||||||
"34": 123456,
|
"34": 123456,
|
||||||
"4": 3412344, "40": 344114,
|
"4": 3412344, "40": 344114,
|
||||||
"5": 341232144, "50": 344490,
|
"5": 341232144, "50": 344490,
|
||||||
"6": 34214544, "60": 344491,
|
"6": 34214544, "60": 344491,
|
||||||
"7": 3434544, "70": 344492,
|
"7": 3434544, "70": 344492,
|
||||||
"8": 344544, "80": 344497,
|
"8": 344544, "80": 344497,
|
||||||
|
|
@ -46,7 +46,7 @@ block tableTest1:
|
||||||
for x in 0..1:
|
for x in 0..1:
|
||||||
for y in 0..1:
|
for y in 0..1:
|
||||||
assert t[(x,y)] == $x & $y
|
assert t[(x,y)] == $x & $y
|
||||||
assert($t ==
|
assert($t ==
|
||||||
"{(x: 0, y: 1): 01, (x: 0, y: 0): 00, (x: 1, y: 0): 10, (x: 1, y: 1): 11}")
|
"{(x: 0, y: 1): 01, (x: 0, y: 0): 00, (x: 1, y: 0): 10, (x: 1, y: 1): 11}")
|
||||||
|
|
||||||
block tableTest2:
|
block tableTest2:
|
||||||
|
|
@ -55,14 +55,17 @@ block tableTest2:
|
||||||
t["111"] = 1.000043
|
t["111"] = 1.000043
|
||||||
t["123"] = 1.23
|
t["123"] = 1.23
|
||||||
t.del("111")
|
t.del("111")
|
||||||
|
|
||||||
t["012"] = 67.9
|
t["012"] = 67.9
|
||||||
t["123"] = 1.5 # test overwriting
|
t["123"] = 1.5 # test overwriting
|
||||||
|
|
||||||
assert t["123"] == 1.5
|
assert t["123"] == 1.5
|
||||||
assert t["111"] == 0.0 # deleted
|
try:
|
||||||
|
echo t["111"] # deleted
|
||||||
|
except KeyError:
|
||||||
|
discard
|
||||||
assert(not hasKey(t, "111"))
|
assert(not hasKey(t, "111"))
|
||||||
|
|
||||||
for key, val in items(data): t[key] = val.toFloat
|
for key, val in items(data): t[key] = val.toFloat
|
||||||
for key, val in items(data): assert t[key] == val.toFloat
|
for key, val in items(data): assert t[key] == val.toFloat
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,15 +22,15 @@ const
|
||||||
"---00": 346677844,
|
"---00": 346677844,
|
||||||
"0": 34404,
|
"0": 34404,
|
||||||
"1": 344004,
|
"1": 344004,
|
||||||
"10": 34484,
|
"10": 34484,
|
||||||
"11": 34474,
|
"11": 34474,
|
||||||
"12": 789,
|
"12": 789,
|
||||||
"19": 34464,
|
"19": 34464,
|
||||||
"2": 344774, "20": 34454,
|
"2": 344774, "20": 34454,
|
||||||
"3": 342244, "30": 34141244,
|
"3": 342244, "30": 34141244,
|
||||||
"34": 123456,
|
"34": 123456,
|
||||||
"4": 3412344, "40": 344114,
|
"4": 3412344, "40": 344114,
|
||||||
"5": 341232144, "50": 344490,
|
"5": 341232144, "50": 344490,
|
||||||
"6": 34214544, "60": 344491,
|
"6": 34214544, "60": 344491,
|
||||||
"7": 3434544, "70": 344492,
|
"7": 3434544, "70": 344492,
|
||||||
"8": 344544, "80": 344497,
|
"8": 344544, "80": 344497,
|
||||||
|
|
@ -46,7 +46,7 @@ block tableTest1:
|
||||||
for x in 0..1:
|
for x in 0..1:
|
||||||
for y in 0..1:
|
for y in 0..1:
|
||||||
assert t[(x,y)] == $x & $y
|
assert t[(x,y)] == $x & $y
|
||||||
assert($t ==
|
assert($t ==
|
||||||
"{(x: 0, y: 1): 01, (x: 0, y: 0): 00, (x: 1, y: 0): 10, (x: 1, y: 1): 11}")
|
"{(x: 0, y: 1): 01, (x: 0, y: 0): 00, (x: 1, y: 0): 10, (x: 1, y: 1): 11}")
|
||||||
|
|
||||||
block tableTest2:
|
block tableTest2:
|
||||||
|
|
@ -55,17 +55,20 @@ block tableTest2:
|
||||||
t["111"] = 1.000043
|
t["111"] = 1.000043
|
||||||
t["123"] = 1.23
|
t["123"] = 1.23
|
||||||
t.del("111")
|
t.del("111")
|
||||||
|
|
||||||
t["012"] = 67.9
|
t["012"] = 67.9
|
||||||
t["123"] = 1.5 # test overwriting
|
t["123"] = 1.5 # test overwriting
|
||||||
|
|
||||||
assert t["123"] == 1.5
|
assert t["123"] == 1.5
|
||||||
assert t["111"] == 0.0 # deleted
|
try:
|
||||||
|
echo t["111"] # deleted
|
||||||
|
except KeyError:
|
||||||
|
discard
|
||||||
assert(not hasKey(t, "111"))
|
assert(not hasKey(t, "111"))
|
||||||
|
|
||||||
for key, val in items(data): t[key] = val.toFloat
|
for key, val in items(data): t[key] = val.toFloat
|
||||||
for key, val in items(data): assert t[key] == val.toFloat
|
for key, val in items(data): assert t[key] == val.toFloat
|
||||||
|
|
||||||
|
|
||||||
block orderedTableTest1:
|
block orderedTableTest1:
|
||||||
var t = newOrderedTable[string, int](2)
|
var t = newOrderedTable[string, int](2)
|
||||||
|
|
|
||||||
|
|
@ -13,21 +13,20 @@ proc emit*(emitter: EventEmitter, event: string, args: EventArgs) =
|
||||||
for fn in nodes(emitter.events[event]):
|
for fn in nodes(emitter.events[event]):
|
||||||
fn.value(args) #call function with args.
|
fn.value(args) #call function with args.
|
||||||
|
|
||||||
proc on*(emitter: var EventEmitter, event: string,
|
proc on*(emitter: var EventEmitter, event: string,
|
||||||
fn: proc(e: EventArgs) {.nimcall.}) =
|
fn: proc(e: EventArgs) {.nimcall.}) =
|
||||||
if not hasKey(emitter.events, event):
|
if not hasKey(emitter.events, event):
|
||||||
var list: DoublyLinkedList[proc(e: EventArgs) {.nimcall.}]
|
var list: DoublyLinkedList[proc(e: EventArgs) {.nimcall.}]
|
||||||
add(emitter.events, event, list) #if not, add it.
|
add(emitter.events, event, list) #if not, add it.
|
||||||
append(emitter.events.mget(event), fn)
|
append(emitter.events[event], fn)
|
||||||
|
|
||||||
proc initEmitter(emitter: var EventEmitter) =
|
proc initEmitter(emitter: var EventEmitter) =
|
||||||
emitter.events = initTable[string,
|
emitter.events = initTable[string,
|
||||||
DoublyLinkedList[proc(e: EventArgs) {.nimcall.}]]()
|
DoublyLinkedList[proc(e: EventArgs) {.nimcall.}]]()
|
||||||
|
|
||||||
var
|
var
|
||||||
ee: EventEmitter
|
ee: EventEmitter
|
||||||
args: EventArgs
|
args: EventArgs
|
||||||
initEmitter(ee)
|
initEmitter(ee)
|
||||||
ee.on("print", proc(e: EventArgs) = echo("pie"))
|
ee.on("print", proc(e: EventArgs) = echo("pie"))
|
||||||
ee.emit("print", args)
|
ee.emit("print", args)
|
||||||
|
|
||||||
|
|
|
||||||
141
tests/stdlib/tmget.nim
Normal file
141
tests/stdlib/tmget.nim
Normal file
|
|
@ -0,0 +1,141 @@
|
||||||
|
discard """
|
||||||
|
output: '''Can't access 6
|
||||||
|
10
|
||||||
|
11
|
||||||
|
Can't access 6
|
||||||
|
10
|
||||||
|
11
|
||||||
|
Can't access 6
|
||||||
|
10
|
||||||
|
11
|
||||||
|
Can't access 6
|
||||||
|
10
|
||||||
|
11
|
||||||
|
Can't access 6
|
||||||
|
10
|
||||||
|
11
|
||||||
|
Can't access 6
|
||||||
|
10
|
||||||
|
11
|
||||||
|
Can't access 6
|
||||||
|
5
|
||||||
|
Can't access 6
|
||||||
|
10
|
||||||
|
11
|
||||||
|
Can't access 6
|
||||||
|
10
|
||||||
|
11'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
import tables
|
||||||
|
|
||||||
|
block:
|
||||||
|
var x = initTable[int, int]()
|
||||||
|
x[5] = 10
|
||||||
|
try:
|
||||||
|
echo x[6]
|
||||||
|
except KeyError:
|
||||||
|
echo "Can't access 6"
|
||||||
|
echo x[5]
|
||||||
|
x[5] += 1
|
||||||
|
var c = x[5]
|
||||||
|
echo c
|
||||||
|
|
||||||
|
block:
|
||||||
|
var x = newTable[int, int]()
|
||||||
|
x[5] = 10
|
||||||
|
try:
|
||||||
|
echo x[6]
|
||||||
|
except KeyError:
|
||||||
|
echo "Can't access 6"
|
||||||
|
echo x[5]
|
||||||
|
x[5] += 1
|
||||||
|
var c = x[5]
|
||||||
|
echo c
|
||||||
|
|
||||||
|
block:
|
||||||
|
var x = initOrderedTable[int, int]()
|
||||||
|
x[5] = 10
|
||||||
|
try:
|
||||||
|
echo x[6]
|
||||||
|
except KeyError:
|
||||||
|
echo "Can't access 6"
|
||||||
|
echo x[5]
|
||||||
|
x[5] += 1
|
||||||
|
var c = x[5]
|
||||||
|
echo c
|
||||||
|
|
||||||
|
block:
|
||||||
|
var x = newOrderedTable[int, int]()
|
||||||
|
x[5] = 10
|
||||||
|
try:
|
||||||
|
echo x[6]
|
||||||
|
except KeyError:
|
||||||
|
echo "Can't access 6"
|
||||||
|
echo x[5]
|
||||||
|
x[5] += 1
|
||||||
|
var c = x[5]
|
||||||
|
echo c
|
||||||
|
|
||||||
|
block:
|
||||||
|
var x = initCountTable[int]()
|
||||||
|
x[5] = 10
|
||||||
|
try:
|
||||||
|
echo x[6]
|
||||||
|
except KeyError:
|
||||||
|
echo "Can't access 6"
|
||||||
|
echo x[5]
|
||||||
|
x[5] += 1
|
||||||
|
var c = x[5]
|
||||||
|
echo c
|
||||||
|
|
||||||
|
block:
|
||||||
|
var x = newCountTable[int]()
|
||||||
|
x[5] = 10
|
||||||
|
try:
|
||||||
|
echo x[6]
|
||||||
|
except KeyError:
|
||||||
|
echo "Can't access 6"
|
||||||
|
echo x[5]
|
||||||
|
x[5] += 1
|
||||||
|
var c = x[5]
|
||||||
|
echo c
|
||||||
|
|
||||||
|
import sets
|
||||||
|
|
||||||
|
block:
|
||||||
|
var x = initSet[int]()
|
||||||
|
x.incl 5
|
||||||
|
try:
|
||||||
|
echo x[6]
|
||||||
|
except KeyError:
|
||||||
|
echo "Can't access 6"
|
||||||
|
echo x[5]
|
||||||
|
|
||||||
|
import critbits
|
||||||
|
|
||||||
|
block:
|
||||||
|
var x: CritBitTree[int]
|
||||||
|
x["5"] = 10
|
||||||
|
try:
|
||||||
|
echo x["6"]
|
||||||
|
except KeyError:
|
||||||
|
echo "Can't access 6"
|
||||||
|
echo x["5"]
|
||||||
|
x["5"] += 1
|
||||||
|
var c = x["5"]
|
||||||
|
echo c
|
||||||
|
|
||||||
|
import strtabs
|
||||||
|
|
||||||
|
block:
|
||||||
|
var x = newStringTable()
|
||||||
|
x["5"] = "10"
|
||||||
|
try:
|
||||||
|
echo x["6"]
|
||||||
|
except KeyError:
|
||||||
|
echo "Can't access 6"
|
||||||
|
echo x["5"]
|
||||||
|
x["5"][1] = '1'
|
||||||
|
var c = x["5"]
|
||||||
|
echo c
|
||||||
|
|
@ -132,5 +132,5 @@ block:
|
||||||
</Students>""")
|
</Students>""")
|
||||||
for x in d.mitems:
|
for x in d.mitems:
|
||||||
x = <>Student(Name=x.attrs["Name"] & "foo")
|
x = <>Student(Name=x.attrs["Name"] & "foo")
|
||||||
d.mget(1).attrs["Name"] = "bar"
|
d[1].attrs["Name"] = "bar"
|
||||||
echo d
|
echo d
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue