On this page
critbits
This module implements a crit bit tree which is an efficient container for a sorted set of strings, or for a sorted mapping of strings. Based on the excellent paper by Adam Langley. (A crit bit tree is a form of radix tree or patricia trie.)
Example:
static:
  block:
    var critbitAsSet: CritBitTree[void]
    doAssert critbitAsSet.len == 0
    incl critbitAsSet, "kitten"
    doAssert critbitAsSet.len == 1
    incl critbitAsSet, "puppy"
    doAssert critbitAsSet.len == 2
    incl critbitAsSet, "kitten"
    doAssert critbitAsSet.len == 2
    incl critbitAsSet, ""
    doAssert critbitAsSet.len == 3
block:
  var critbitAsDict: CritBitTree[int]
  critbitAsDict["key"] = 42
  doAssert critbitAsDict["key"] == 42
  critbitAsDict["key"] = 0
  doAssert critbitAsDict["key"] == 0
  critbitAsDict["key"] = -int.high
  doAssert critbitAsDict["key"] == -int.high
  critbitAsDict["key"] = int.high
  doAssert critbitAsDict["key"] == int.high
  Imports
Types
- 
    
CritBitTree[T] = object root: Node[T] count: int - 
    The crit bit tree can either be used as a mapping from strings to some type 
Tor as a set of strings ifTis void. Source Edit 
Procs
- 
    
proc excl[T](c: var CritBitTree[T]; key: string) - 
    
Removes
key(and its associated value) from the setc. If thekeydoes not exist, nothing happens.See also:
Example:
Source Editvar c: CritBitTree[void] incl(c, "key") excl(c, "key") doAssert not c.contains("key") - 
    
proc missingOrExcl[T](c: var CritBitTree[T]; key: string): bool - 
    
Returns true if
cdoes not contain the givenkey. If the key does exist, c.excl(key) is performed.See also:
Example:
Source Editblock: var c: CritBitTree[void] doAssert c.missingOrExcl("key") block: var c: CritBitTree[void] incl(c, "key") doAssert not c.missingOrExcl("key") doAssert not c.contains("key") - 
    
proc containsOrIncl[T](c: var CritBitTree[T]; key: string; val: T): bool - 
    
Returns true if
ccontains the givenkey. If the key does not existc[key] = valis performed.See also:
Example:
Source Editblock: var c: CritBitTree[int] doAssert not c.containsOrIncl("key", 42) doAssert c.contains("key") block: var c: CritBitTree[int] incl(c, "key", 21) doAssert c.containsOrIncl("key", 42) doAssert c["key"] == 21 - 
    
proc containsOrIncl(c: var CritBitTree[void]; key: string): bool {...}{.raises: [], tags: [].} - 
    
Returns true if
ccontains the givenkey. If the key does not exist it is inserted intoc.See also:
Example:
Source Editblock: var c: CritBitTree[void] doAssert not c.containsOrIncl("key") doAssert c.contains("key") block: var c: CritBitTree[void] incl(c, "key") doAssert c.containsOrIncl("key") - 
    
proc inc(c: var CritBitTree[int]; key: string; val: int = 1) {...}{.raises: [], tags: [].} - 
    Increments 
c[key]byval.Example:
Source Editvar c: CritBitTree[int] c["key"] = 1 inc(c, "key") doAssert c["key"] == 2 - 
    
proc incl(c: var CritBitTree[void]; key: string) {...}{.raises: [], tags: [].} - 
    
Includes
keyinc.See also:
Example:
Source Editvar c: CritBitTree[void] incl(c, "key") doAssert c.hasKey("key") - 
    
proc incl[T](c: var CritBitTree[T]; key: string; val: T) - 
    
Inserts
keywith valuevalintoc.See also:
Example:
Source Editvar c: CritBitTree[int] incl(c, "key", 42) doAssert c["key"] == 42 - 
    
proc `[]=`[T](c: var CritBitTree[T]; key: string; val: T) - 
    
Puts a (key, value)-pair into
t.See also:
Example:
Source Editvar c: CritBitTree[int] c["key"] = 42 doAssert c["key"] == 42 
Funcs
- 
    
func len[T](c: CritBitTree[T]): int {...}{.inline.} - 
    Returns the number of elements in 
cin O(1).Example:
Source Editvar c: CritBitTree[void] incl(c, "key1") incl(c, "key2") doAssert c.len == 2 - 
    
func contains[T](c: CritBitTree[T]; key: string): bool {...}{.inline.} - 
    Returns true if 
ccontains the givenkey.Example:
Source Editvar c: CritBitTree[void] incl(c, "key") doAssert c.contains("key") - 
    
func hasKey[T](c: CritBitTree[T]; key: string): bool {...}{.inline.} - Alias for contains. Source Edit
 - 
    
func `[]`[T](c: CritBitTree[T]; key: string): T {...}{.inline.} - 
    
Retrieves the value at
c[key]. Ifkeyis not int, theKeyErrorexception is raised. One can check withhasKeywhether the key exists.See also:
Source Edit - 
    
func `[]`[T](c: var CritBitTree[T]; key: string): var T {...}{.inline.} - 
    
Retrieves the value at
c[key]. The value can be modified. Ifkeyis not int, theKeyErrorexception is raised.See also:
Source Edit - 
    
func `$`[T](c: CritBitTree[T]): string - 
    Turns 
cinto a string representation. Example outputs:{keyA: value, keyB: value},{:}IfTis void the outputs look like:{keyA, keyB},{}. Source Edit - 
    
func commonPrefixLen[T](c: CritBitTree[T]): int {...}{.inline.} - 
    Returns longest common prefix length of all keys of 
c. Ifcis empty, returns 0.Example:
Source Editvar c: CritBitTree[void] doAssert c.commonPrefixLen == 0 incl(c, "key1") doAssert c.commonPrefixLen == 4 incl(c, "key2") doAssert c.commonPrefixLen == 3 - 
    
func toCritBitTree[A, B](pairs: openArray[(A, B)]): CritBitTree[A] - 
    Creates a new 
CritBitTreethat contains the givenpairs.Example:
Source EditdoAssert {"a": "0", "b": "1", "c": "2"}.toCritBitTree is CritBitTree[string] - 
    
func toCritBitTree[T](items: openArray[T]): CritBitTree[void] - 
    Creates a new 
CritBitTreethat contains the givenitems.Example:
Source EditdoAssert ["a", "b", "c"].toCritBitTree is CritBitTree[void] 
Iterators
- 
    
iterator keys[T](c: CritBitTree[T]): string - 
    Yields all keys in lexicographical order. 
    
Example:
Source Editvar c: CritBitTree[int] c["key1"] = 1 c["key2"] = 2 var keys: seq[string] for key in c.keys: keys.add(key) doAssert keys == @["key1", "key2"] - 
    
iterator values[T](c: CritBitTree[T]): T - 
    Yields all values of 
cin the lexicographical order of the corresponding keys.Example:
Source Editvar c: CritBitTree[int] c["key1"] = 1 c["key2"] = 2 var vals: seq[int] for val in c.values: vals.add(val) doAssert vals == @[1, 2] - 
    
iterator mvalues[T](c: var CritBitTree[T]): var T - 
    
Yields all values of
cin the lexicographical order of the corresponding keys. The values can be modified.See also:
Source Edit - 
    
iterator items[T](c: CritBitTree[T]): string - 
    Yields all keys in lexicographical order. 
    
Example:
Source Editvar c: CritBitTree[int] c["key1"] = 1 c["key2"] = 2 var keys: seq[string] for key in c.items: keys.add(key) doAssert keys == @["key1", "key2"] - 
    
iterator pairs[T](c: CritBitTree[T]): tuple[key: string, val: T] - 
    Yields all (key, value)-pairs of 
c.Example:
Source Editvar c: CritBitTree[int] c["key1"] = 1 c["key2"] = 2 var ps: seq[tuple[key: string, val: int]] for p in c.pairs: ps.add(p) doAssert ps == @[(key: "key1", val: 1), (key: "key2", val: 2)] - 
    
iterator mpairs[T](c: var CritBitTree[T]): tuple[key: string, val: var T] - 
    
Yields all (key, value)-pairs of
c. The yielded values can be modified.See also:
Source Edit - 
    
iterator itemsWithPrefix[T](c: CritBitTree[T]; prefix: string; longestMatch = false): string - 
    Yields all keys starting with 
prefix. IflongestMatchis true, the longest match is returned, it doesn't have to be a complete match then.Example:
Source Editvar c: CritBitTree[int] c["key1"] = 42 c["key2"] = 43 var keys: seq[string] for key in c.itemsWithPrefix("key"): keys.add(key) doAssert keys == @["key1", "key2"] - 
    
iterator keysWithPrefix[T](c: CritBitTree[T]; prefix: string; longestMatch = false): string - 
    Yields all keys starting with 
prefix.Example:
Source Editvar c: CritBitTree[int] c["key1"] = 42 c["key2"] = 43 var keys: seq[string] for key in c.keysWithPrefix("key"): keys.add(key) doAssert keys == @["key1", "key2"] - 
    
iterator valuesWithPrefix[T](c: CritBitTree[T]; prefix: string; longestMatch = false): T - 
    Yields all values of 
cstarting withprefixof the corresponding keys.Example:
Source Editvar c: CritBitTree[int] c["key1"] = 42 c["key2"] = 43 var vals: seq[int] for val in c.valuesWithPrefix("key"): vals.add(val) doAssert vals == @[42, 43] - 
    
iterator mvaluesWithPrefix[T](c: var CritBitTree[T]; prefix: string; longestMatch = false): var T - 
    
Yields all values of
cstarting withprefixof the corresponding keys. The values can be modified.See also:
Source Edit - 
    
iterator pairsWithPrefix[T](c: CritBitTree[T]; prefix: string; longestMatch = false): tuple[key: string, val: T] - 
    Yields all (key, value)-pairs of 
cstarting withprefix.Example:
Source Editvar c: CritBitTree[int] c["key1"] = 42 c["key2"] = 43 var ps: seq[tuple[key: string, val: int]] for p in c.pairsWithPrefix("key"): ps.add(p) doAssert ps == @[(key: "key1", val: 42), (key: "key2", val: 43)] - 
    
iterator mpairsWithPrefix[T](c: var CritBitTree[T]; prefix: string; longestMatch = false): tuple[key: string, val: var T] - 
    
Yields all (key, value)-pairs of
cstarting withprefix. The yielded values can be modified.See also:
Source Edit 
© 2006–2021 Andreas Rumpf
Licensed under the MIT License.
 https://nim-lang.org/docs/critbits.html