Nim: CountTable is not reliable.

Created on 16 Sep 2019  Â·  6Comments  Â·  Source: nim-lang/Nim

Today I got a very strange output from my app. After digging, I found the bug is in the CountTable.

Example

import tables

var ct = initCountTable[int]()
ct.inc(130, 1)
ct.inc(132, 1)
ct.inc(258, 1)

ct.inc(131, 100)
echo ct[131] # should output 100

ct.inc(132, -1)
echo ct[131] # should output 100, too

Current Output

0

Expected Output

100

Additional Information

Nim Compiler Version 0.20.99 [Windows: amd64]
High Priority Stdlib

Most helpful comment

That should be documented, and the argument of inc should be a Natural then

All 6 comments

Thanks for using the issue template and coming up with the precise* small example of the problem. It really makes debugging much easier.

* because even small changes don't exhibit this behaviour — this bug has been with us for a very long time, and only now somebody has spotted it.

You really can't count negative values like that... The CountTable is designed that 0 occurrences are not stored.

You really can't count negative values like that... The CountTable is designed that 0 occurrences are not stored.

You mean CountTable cannot inc() negative value or cannot store negative value?
However, I still cannot understand why access [132] will influence the value in [131].

inc should take a Positive number, yes.

That should be documented, and the argument of inc should be a Natural then

That should be documented, and the argument of inc should be a Natural then

Done. https://github.com/nim-lang/Nim/pull/12208

Was this page helpful?
0 / 5 - 0 ratings