Skip to content

Commit 5e160bf

Browse files
Add critbits.toCritBitTree (#15444)
* Add critbits.toCritBitTree * #15444 (comment)
1 parent 8f90ac6 commit 5e160bf

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ proc mydiv(a, b): int {.raises: [].} =
288288

289289
- `items` no longer compiles with enum with holes as its behavior was error prone, see #14004
290290

291+
- Added `critbits.toCritBitTree`, similar to `tables.toTable`, creates a new `CritBitTree` with given arguments.
292+
293+
291294
## Compiler changes
292295

293296
- Specific warnings can now be turned into errors via `--warningAsError[X]:on|off`.

lib/pure/collections/critbits.nim

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type
3333
root: Node[T]
3434
count: int
3535

36-
proc len*[T](c: CritBitTree[T]): int =
36+
func len*[T](c: CritBitTree[T]): int {.inline.} =
3737
## Returns the number of elements in `c` in O(1).
3838
runnableExamples:
3939
var c: CritBitTree[void]
@@ -53,7 +53,7 @@ proc rawGet[T](c: CritBitTree[T], key: string): Node[T] =
5353
else:
5454
return if it.key == key: it else: nil
5555

56-
proc contains*[T](c: CritBitTree[T], key: string): bool {.inline.} =
56+
func contains*[T](c: CritBitTree[T], key: string): bool {.inline.} =
5757
## Returns true if `c` contains the given `key`.
5858
runnableExamples:
5959
var c: CritBitTree[void]
@@ -62,7 +62,7 @@ proc contains*[T](c: CritBitTree[T], key: string): bool {.inline.} =
6262

6363
result = rawGet(c, key) != nil
6464

65-
proc hasKey*[T](c: CritBitTree[T], key: string): bool {.inline.} =
65+
func hasKey*[T](c: CritBitTree[T], key: string): bool {.inline.} =
6666
## Alias for `contains <#contains,CritBitTree[T],string>`_.
6767
result = rawGet(c, key) != nil
6868

@@ -116,7 +116,7 @@ proc rawInsert[T](c: var CritBitTree[T], key: string): Node[T] =
116116
wherep[] = inner
117117
inc c.count
118118

119-
proc exclImpl[T](c: var CritBitTree[T], key: string): int =
119+
func exclImpl[T](c: var CritBitTree[T], key: string): int =
120120
var p = c.root
121121
var wherep = addr(c.root)
122122
var whereq: ptr Node[T] = nil
@@ -285,7 +285,7 @@ template get[T](c: CritBitTree[T], key: string): T =
285285

286286
n.val
287287

288-
proc `[]`*[T](c: CritBitTree[T], key: string): T {.inline.} =
288+
func `[]`*[T](c: CritBitTree[T], key: string): T {.inline.} =
289289
## Retrieves the value at ``c[key]``. If `key` is not in `t`, the
290290
## ``KeyError`` exception is raised. One can check with ``hasKey`` whether
291291
## the key exists.
@@ -295,7 +295,7 @@ proc `[]`*[T](c: CritBitTree[T], key: string): T {.inline.} =
295295
## * `[]= proc <#[]=,CritBitTree[T],string,T>`_
296296
get(c, key)
297297

298-
proc `[]`*[T](c: var CritBitTree[T], key: string): var T {.inline.} =
298+
func `[]`*[T](c: var CritBitTree[T], key: string): var T {.inline.} =
299299
## Retrieves the value at ``c[key]``. The value can be modified.
300300
## If `key` is not in `t`, the ``KeyError`` exception is raised.
301301
##
@@ -485,7 +485,7 @@ iterator mpairsWithPrefix*[T](c: var CritBitTree[T],
485485
let top = allprefixedAux(c, prefix, longestMatch)
486486
for x in leaves(top): yield (x.key, x.val)
487487

488-
proc `$`*[T](c: CritBitTree[T]): string =
488+
func `$`*[T](c: CritBitTree[T]): string =
489489
## Turns `c` into a string representation. Example outputs:
490490
## ``{keyA: value, keyB: value}``, ``{:}``
491491
## If `T` is void the outputs look like:
@@ -515,7 +515,7 @@ proc `$`*[T](c: CritBitTree[T]): string =
515515
result.addQuoted(val)
516516
result.add("}")
517517

518-
proc commonPrefixLen*[T](c: CritBitTree[T]): int {.inline, since((1, 3)).} =
518+
func commonPrefixLen*[T](c: CritBitTree[T]): int {.inline, since((1, 3)).} =
519519
## Returns longest common prefix length of all keys of `c`.
520520
## If `c` is empty, returns 0.
521521
runnableExamples:
@@ -531,6 +531,18 @@ proc commonPrefixLen*[T](c: CritBitTree[T]): int {.inline, since((1, 3)).} =
531531
else: c.root.byte
532532
else: 0
533533

534+
func toCritBitTree*[A, B](pairs: openArray[(A, B)]): CritBitTree[A] {.since: (1, 3).} =
535+
## Creates a new `CritBitTree` that contains the given `pairs`.
536+
runnableExamples:
537+
doAssert {"a": "0", "b": "1", "c": "2"}.toCritBitTree is CritBitTree[string]
538+
for item in pairs: result.incl item[0], item[1]
539+
540+
func toCritBitTree*[T](items: openArray[T]): CritBitTree[void] {.since: (1, 3).} =
541+
## Creates a new `CritBitTree` that contains the given `items`.
542+
runnableExamples:
543+
doAssert ["a", "b", "c"].toCritBitTree is CritBitTree[void]
544+
for item in items: result.incl item
545+
534546

535547
runnableExamples:
536548
static:

0 commit comments

Comments
 (0)