Merge branch 'devel' of github.com:nim-lang/Nim into devel

This commit is contained in:
Andreas Rumpf 2017-05-06 11:53:22 +02:00
commit a3e0a7ebbb
2 changed files with 45 additions and 11 deletions

View file

@ -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)
@ -625,7 +644,7 @@ proc `==`*[A, B](s, t: OrderedTable[A, B]): bool =
while ht >= 0 and hs >= 0: while ht >= 0 and hs >= 0:
var nxtt = t.data[ht].next var nxtt = t.data[ht].next
var nxts = s.data[hs].next var nxts = s.data[hs].next
if isFilled(t.data[ht].hcode) and isFilled(s.data[hs].hcode): if isFilled(t.data[ht].hcode) and isFilled(s.data[hs].hcode):
if (s.data[hs].key != t.data[ht].key) and (s.data[hs].val != t.data[ht].val): if (s.data[hs].key != t.data[ht].key) and (s.data[hs].val != t.data[ht].val):
return false return false
ht = nxtt ht = nxtt
@ -829,7 +848,7 @@ proc clear*[A](t: CountTableRef[A]) =
proc clear*[A](t: var CountTable[A]) = proc clear*[A](t: var CountTable[A]) =
## Resets the table so that it is empty. ## Resets the table so that it is empty.
clearImpl() clearImpl()
iterator pairs*[A](t: CountTable[A]): (A, int) = iterator pairs*[A](t: CountTable[A]): (A, int) =
## iterates over any (key, value) pair in the table `t`. ## iterates over any (key, value) pair in the table `t`.
for h in 0..high(t.data): for h in 0..high(t.data):
@ -1256,17 +1275,17 @@ when isMainModule:
var b = newOrderedTable[string, string](initialSize=2) var b = newOrderedTable[string, string](initialSize=2)
b.add("wrong?", "foo") b.add("wrong?", "foo")
b.add("wrong?", "foo2") b.add("wrong?", "foo2")
assert a == b assert a == b
block: #5482 block: #5482
var a = {"wrong?": "foo", "wrong?": "foo2"}.newOrderedTable() var a = {"wrong?": "foo", "wrong?": "foo2"}.newOrderedTable()
var b = newOrderedTable[string, string](initialSize=2) var b = newOrderedTable[string, string](initialSize=2)
b.add("wrong?", "foo") b.add("wrong?", "foo")
b.add("wrong?", "foo2") b.add("wrong?", "foo2")
assert a == b assert a == b
block: #5487 block: #5487
var a = {"wrong?": "foo", "wrong?": "foo2"}.newOrderedTable() var a = {"wrong?": "foo", "wrong?": "foo2"}.newOrderedTable()
var b = newOrderedTable[string, string]() # notice, default size! var b = newOrderedTable[string, string]() # notice, default size!
b.add("wrong?", "foo") b.add("wrong?", "foo")
b.add("wrong?", "foo2") b.add("wrong?", "foo2")
@ -1279,13 +1298,13 @@ when isMainModule:
b.add("wrong?", "foo2") b.add("wrong?", "foo2")
assert a == b assert a == b
block: block:
var a = {"wrong?": "foo", "wrong?": "foo2"}.newOrderedTable() var a = {"wrong?": "foo", "wrong?": "foo2"}.newOrderedTable()
var b = [("wrong?","foo"), ("wrong?", "foo2")].newOrderedTable() var b = [("wrong?","foo"), ("wrong?", "foo2")].newOrderedTable()
var c = newOrderedTable[string, string]() # notice, default size! var c = newOrderedTable[string, string]() # notice, default size!
c.add("wrong?", "foo") c.add("wrong?", "foo")
c.add("wrong?", "foo2") c.add("wrong?", "foo2")
assert a == b assert a == b
assert a == c assert a == c

View file

@ -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