Fixes CritBitTree.inc's bug (#7838)
* Fixes CritBitTree.inc's bug * Update changelog
This commit is contained in:
parent
508dfdabee
commit
06bdf8392b
2 changed files with 15 additions and 4 deletions
|
|
@ -74,6 +74,7 @@
|
||||||
deprecated.
|
deprecated.
|
||||||
- The `terminal` module now exports additional procs for generating ANSI color
|
- The `terminal` module now exports additional procs for generating ANSI color
|
||||||
codes as strings.
|
codes as strings.
|
||||||
|
- Added the parameter ``val`` for the ``CritBitTree[int].inc`` proc.
|
||||||
|
|
||||||
### Language additions
|
### Language additions
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -163,13 +163,13 @@ proc containsOrIncl*(c: var CritBitTree[void], key: string): bool =
|
||||||
var n = rawInsert(c, key)
|
var n = rawInsert(c, key)
|
||||||
result = c.count == oldCount
|
result = c.count == oldCount
|
||||||
|
|
||||||
proc inc*(c: var CritBitTree[int]; key: string) =
|
proc inc*(c: var CritBitTree[int]; key: string, val: int = 1) =
|
||||||
## counts the 'key'.
|
## increments `c[key]` by `val`.
|
||||||
let oldCount = c.count
|
let oldCount = c.count
|
||||||
var n = rawInsert(c, key)
|
var n = rawInsert(c, key)
|
||||||
if c.count == oldCount:
|
if c.count == oldCount or oldCount == 0:
|
||||||
# not a new key:
|
# not a new key:
|
||||||
inc n.val
|
inc n.val, val
|
||||||
|
|
||||||
proc incl*(c: var CritBitTree[void], key: string) =
|
proc incl*(c: var CritBitTree[void], key: string) =
|
||||||
## includes `key` in `c`.
|
## includes `key` in `c`.
|
||||||
|
|
@ -352,3 +352,13 @@ when isMainModule:
|
||||||
assert toSeq(r.items) == @["abc", "definition", "prefix", "xyz"]
|
assert toSeq(r.items) == @["abc", "definition", "prefix", "xyz"]
|
||||||
|
|
||||||
assert toSeq(r.itemsWithPrefix("de")) == @["definition"]
|
assert toSeq(r.itemsWithPrefix("de")) == @["definition"]
|
||||||
|
var c = CritBitTree[int]()
|
||||||
|
|
||||||
|
c.inc("a")
|
||||||
|
assert c["a"] == 1
|
||||||
|
|
||||||
|
c.inc("a", 4)
|
||||||
|
assert c["a"] == 5
|
||||||
|
|
||||||
|
c.inc("a", -5)
|
||||||
|
assert c["a"] == 0
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue