Merge branch 'devel' of github.com:nim-lang/Nim into devel
This commit is contained in:
commit
a3e0a7ebbb
2 changed files with 45 additions and 11 deletions
|
|
@ -269,6 +269,18 @@ proc del*[A, B](t: var Table[A, B], key: A) =
|
||||||
## deletes `key` from hash table `t`.
|
## deletes `key` from hash table `t`.
|
||||||
delImpl()
|
delImpl()
|
||||||
|
|
||||||
|
proc take*[A, B](t: var Table[A, B], key: A, val: var B): bool =
|
||||||
|
## Deletes the ``key`` from the table.
|
||||||
|
## Returns ``true``, if the ``key`` existed, and sets ``val`` to the
|
||||||
|
## mapping of the key. Otherwise, returns ``false``, and the ``val`` is
|
||||||
|
## unchanged.
|
||||||
|
var hc: Hash
|
||||||
|
var index = rawGet(t, key, hc)
|
||||||
|
result = index >= 0
|
||||||
|
if result:
|
||||||
|
shallowCopy(val, t.data[index].val)
|
||||||
|
delImplIdx(t, index)
|
||||||
|
|
||||||
proc enlarge[A, B](t: var Table[A, B]) =
|
proc enlarge[A, B](t: var Table[A, B]) =
|
||||||
var n: KeyValuePairSeq[A, B]
|
var n: KeyValuePairSeq[A, B]
|
||||||
newSeq(n, len(t.data) * growthFactor)
|
newSeq(n, len(t.data) * growthFactor)
|
||||||
|
|
@ -424,6 +436,13 @@ proc del*[A, B](t: TableRef[A, B], key: A) =
|
||||||
## deletes `key` from hash table `t`.
|
## deletes `key` from hash table `t`.
|
||||||
t[].del(key)
|
t[].del(key)
|
||||||
|
|
||||||
|
proc take*[A, B](t: TableRef[A, B], key: A, val: var B): bool =
|
||||||
|
## Deletes the ``key`` from the table.
|
||||||
|
## Returns ``true``, if the ``key`` existed, and sets ``val`` to the
|
||||||
|
## mapping of the key. Otherwise, returns ``false``, and the ``val`` is
|
||||||
|
## unchanged.
|
||||||
|
result = t[].take(key, val)
|
||||||
|
|
||||||
proc newTable*[A, B](initialSize=64): TableRef[A, B] =
|
proc newTable*[A, B](initialSize=64): TableRef[A, B] =
|
||||||
new(result)
|
new(result)
|
||||||
result[] = initTable[A, B](initialSize)
|
result[] = initTable[A, B](initialSize)
|
||||||
|
|
|
||||||
|
|
@ -235,6 +235,21 @@ block withKeyTest:
|
||||||
except KeyError:
|
except KeyError:
|
||||||
discard
|
discard
|
||||||
|
|
||||||
|
block takeTest:
|
||||||
|
var t = initTable[string, int]()
|
||||||
|
t["key"] = 123
|
||||||
|
|
||||||
|
var val = 0
|
||||||
|
assert(t.take("key", val))
|
||||||
|
assert(val == 123)
|
||||||
|
|
||||||
|
val = -1
|
||||||
|
assert(not t.take("key", val))
|
||||||
|
assert(val == -1)
|
||||||
|
|
||||||
|
assert(not t.take("otherkey", val))
|
||||||
|
assert(val == -1)
|
||||||
|
|
||||||
proc orderedTableSortTest() =
|
proc orderedTableSortTest() =
|
||||||
var t = initOrderedTable[string, int](2)
|
var t = initOrderedTable[string, int](2)
|
||||||
for key, val in items(data): t[key] = val
|
for key, val in items(data): t[key] = val
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue