diff options
author | Miran <narimiran@disroot.org> | 2020-01-10 15:24:33 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-10 15:24:33 +0100 |
commit | c0973d1b471644569d3ef459115afc675eb64544 (patch) | |
tree | 0623a064bab35fdaa096c72867e669be7c68296d /lib/pure | |
parent | f7ce4e8705283235d84e171faaaf63f71ef232b8 (diff) | |
download | Nim-c0973d1b471644569d3ef459115afc675eb64544.tar.gz |
[backport] fix #12813, fix #13079 (#13099)
Correctly remove a key from CountTable when it is set to zero.
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/collections/tables.nim | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim index d0e0b0886..20fe12fc8 100644 --- a/lib/pure/collections/tables.nim +++ b/lib/pure/collections/tables.nim @@ -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). @@ -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") |