Add critbits.toCritBitTree (#15444)

* Add critbits.toCritBitTree

* https://github.com/nim-lang/Nim/pull/15444#discussion_r498035342
This commit is contained in:
Juan Carlos 2020-10-01 10:11:10 -03:00 • committed by GitHub
commit 5e160bff1b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 8 deletions

View file

@ -288,6 +288,9 @@ proc mydiv(a, b): int {.raises: [].} =
- `items` no longer compiles with enum with holes as its behavior was error prone, see #14004 - `items` no longer compiles with enum with holes as its behavior was error prone, see #14004
- Added `critbits.toCritBitTree`, similar to `tables.toTable`, creates a new `CritBitTree` with given arguments.
## Compiler changes ## Compiler changes
- Specific warnings can now be turned into errors via `--warningAsError[X]:on|off`. - Specific warnings can now be turned into errors via `--warningAsError[X]:on|off`.

View file

@ -33,7 +33,7 @@ type
root: Node[T] root: Node[T]
count: int count: int
proc len*[T](c: CritBitTree[T]): int = func len*[T](c: CritBitTree[T]): int {.inline.} =
## Returns the number of elements in `c` in O(1). ## Returns the number of elements in `c` in O(1).
runnableExamples: runnableExamples:
var c: CritBitTree[void] var c: CritBitTree[void]
@ -53,7 +53,7 @@ proc rawGet[T](c: CritBitTree[T], key: string): Node[T] =
else: else:
return if it.key == key: it else: nil return if it.key == key: it else: nil
proc contains*[T](c: CritBitTree[T], key: string): bool {.inline.} = func contains*[T](c: CritBitTree[T], key: string): bool {.inline.} =
## Returns true if `c` contains the given `key`. ## Returns true if `c` contains the given `key`.
runnableExamples: runnableExamples:
var c: CritBitTree[void] var c: CritBitTree[void]
@ -62,7 +62,7 @@ proc contains*[T](c: CritBitTree[T], key: string): bool {.inline.} =
result = rawGet(c, key) != nil result = rawGet(c, key) != nil
proc hasKey*[T](c: CritBitTree[T], key: string): bool {.inline.} = func hasKey*[T](c: CritBitTree[T], key: string): bool {.inline.} =
## Alias for `contains <#contains,CritBitTree[T],string>`_. ## Alias for `contains <#contains,CritBitTree[T],string>`_.
result = rawGet(c, key) != nil result = rawGet(c, key) != nil
@ -116,7 +116,7 @@ proc rawInsert[T](c: var CritBitTree[T], key: string): Node[T] =
wherep[] = inner wherep[] = inner
inc c.count inc c.count
proc exclImpl[T](c: var CritBitTree[T], key: string): int = func exclImpl[T](c: var CritBitTree[T], key: string): int =
var p = c.root var p = c.root
var wherep = addr(c.root) var wherep = addr(c.root)
var whereq: ptr Node[T] = nil var whereq: ptr Node[T] = nil
@ -285,7 +285,7 @@ template get[T](c: CritBitTree[T], key: string): T =
n.val n.val
proc `[]`*[T](c: CritBitTree[T], key: string): T {.inline.} = func `[]`*[T](c: CritBitTree[T], key: string): T {.inline.} =
## Retrieves the value at ``c[key]``. If `key` is not in `t`, the ## Retrieves the value at ``c[key]``. If `key` is not in `t`, the
## ``KeyError`` exception is raised. One can check with ``hasKey`` whether ## ``KeyError`` exception is raised. One can check with ``hasKey`` whether
## the key exists. ## the key exists.
@ -295,7 +295,7 @@ proc `[]`*[T](c: CritBitTree[T], key: string): T {.inline.} =
## * `[]= proc <#[]=,CritBitTree[T],string,T>`_ ## * `[]= proc <#[]=,CritBitTree[T],string,T>`_
get(c, key) get(c, key)
proc `[]`*[T](c: var CritBitTree[T], key: string): var T {.inline.} = func `[]`*[T](c: var CritBitTree[T], key: string): var T {.inline.} =
## Retrieves the value at ``c[key]``. The value can be modified. ## Retrieves the value at ``c[key]``. The value can be modified.
## If `key` is not in `t`, the ``KeyError`` exception is raised. ## If `key` is not in `t`, the ``KeyError`` exception is raised.
## ##
@ -485,7 +485,7 @@ iterator mpairsWithPrefix*[T](c: var CritBitTree[T],
let top = allprefixedAux(c, prefix, longestMatch) let top = allprefixedAux(c, prefix, longestMatch)
for x in leaves(top): yield (x.key, x.val) for x in leaves(top): yield (x.key, x.val)
proc `$`*[T](c: CritBitTree[T]): string = func `$`*[T](c: CritBitTree[T]): string =
## Turns `c` into a string representation. Example outputs: ## Turns `c` into a string representation. Example outputs:
## ``{keyA: value, keyB: value}``, ``{:}`` ## ``{keyA: value, keyB: value}``, ``{:}``
## If `T` is void the outputs look like: ## If `T` is void the outputs look like:
@ -515,7 +515,7 @@ proc `$`*[T](c: CritBitTree[T]): string =
result.addQuoted(val) result.addQuoted(val)
result.add("}") result.add("}")
proc commonPrefixLen*[T](c: CritBitTree[T]): int {.inline, since((1, 3)).} = func commonPrefixLen*[T](c: CritBitTree[T]): int {.inline, since((1, 3)).} =
## Returns longest common prefix length of all keys of `c`. ## Returns longest common prefix length of all keys of `c`.
## If `c` is empty, returns 0. ## If `c` is empty, returns 0.
runnableExamples: runnableExamples:
@ -531,6 +531,18 @@ proc commonPrefixLen*[T](c: CritBitTree[T]): int {.inline, since((1, 3)).} =
else: c.root.byte else: c.root.byte
else: 0 else: 0
func toCritBitTree*[A, B](pairs: openArray[(A, B)]): CritBitTree[A] {.since: (1, 3).} =
## Creates a new `CritBitTree` that contains the given `pairs`.
runnableExamples:
doAssert {"a": "0", "b": "1", "c": "2"}.toCritBitTree is CritBitTree[string]
for item in pairs: result.incl item[0], item[1]
func toCritBitTree*[T](items: openArray[T]): CritBitTree[void] {.since: (1, 3).} =
## Creates a new `CritBitTree` that contains the given `items`.
runnableExamples:
doAssert ["a", "b", "c"].toCritBitTree is CritBitTree[void]
for item in items: result.incl item
runnableExamples: runnableExamples:
static: static: