diff options
-rw-r--r-- | changelog.md | 1 | ||||
-rw-r--r-- | lib/pure/collections/critbits.nim | 20 |
2 files changed, 21 insertions, 0 deletions
diff --git a/changelog.md b/changelog.md index 6fd12e62f..aae275c1c 100644 --- a/changelog.md +++ b/changelog.md @@ -85,6 +85,7 @@ - The `terminal` module now exports additional procs for generating ANSI color codes as strings. - Added the parameter ``val`` for the ``CritBitTree[int].inc`` proc. +- Added the parameter ``val`` for the ``CritBitTree[T].incl`` proc. - An exception raised from ``test`` block of ``unittest`` now shows its type in the error message - The proc ``tgamma`` was renamed to ``gamma``. ``tgamma`` is deprecated. diff --git a/lib/pure/collections/critbits.nim b/lib/pure/collections/critbits.nim index eaba257ae..2ab65f002 100644 --- a/lib/pure/collections/critbits.nim +++ b/lib/pure/collections/critbits.nim @@ -175,6 +175,11 @@ proc incl*(c: var CritBitTree[void], key: string) = ## includes `key` in `c`. discard rawInsert(c, key) +proc incl*[T](c: var CritBitTree[T], key: string, val: T) = + ## inserts `key` with value `val` into `c`. + var n = rawInsert(c, key) + n.val = val + proc `[]=`*[T](c: var CritBitTree[T], key: string, val: T) = ## puts a (key, value)-pair into `t`. var n = rawInsert(c, key) @@ -375,3 +380,18 @@ when isMainModule: c.inc("a", 1) assert c["a"] == 1 + + var cf = CritBitTree[float]() + + cf.incl("a", 1.0) + assert cf["a"] == 1.0 + + cf.incl("b", 2.0) + assert cf["b"] == 2.0 + + cf.incl("c", 3.0) + assert cf["c"] == 3.0 + + assert cf.len == 3 + cf.excl("c") + assert cf.len == 2 |