Skip to content

Commit

Permalink
[backport] fix #12813, fix #13079 (#13099)
Browse files Browse the repository at this point in the history
Correctly remove a key from CountTable when it is set to zero.
  • Loading branch information
narimiran authored Jan 10, 2020
1 parent f7ce4e8 commit c0973d1
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/pure/collections/tables.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2320,11 +2320,14 @@ proc `[]=`*[A](t: var CountTable[A], key: A, val: int) =
## value of a key
assert(not t.isSorted, "CountTable must not be used after sorting")
assert val >= 0
let h = rawGet(t, key)
if h >= 0:
t.data[h].val = val
if val == 0:
t.remove(key)
else:
insertImpl()
let h = rawGet(t, key)
if h >= 0:
t.data[h].val = val
else:
insertImpl()

proc inc*[A](t: var CountTable[A], key: A, val: Positive = 1) =
## Increments ``t[key]`` by ``val`` (default: 1).
Expand Down Expand Up @@ -3113,6 +3116,13 @@ when isMainModule:
doAssert t_mut['z'] == 1
doAssert t_mut.hasKey('z') == true

block: #12813 #13079
var t = toCountTable("abracadabra")
doAssert len(t) == 5

t['a'] = 0 # remove a key
doAssert len(t) == 4

block:
var tp: Table[string, string] = initTable[string, string]()
doAssert "test1" == tp.getOrDefault("test1", "test1")
Expand Down

0 comments on commit c0973d1

Please sign in to comment.