Add critbits.commonPrefixLen (#14072)

* Add critbits.commonPrefixLen

* add inline and since annotations, as well as a changelog entry

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
This commit is contained in:
Phil Krylov
2020-04-24 10:31:30 +03:00
committed by GitHub
parent b2141fc2a1
commit d5d3594457
2 changed files with 17 additions and 0 deletions

View File

@@ -31,6 +31,7 @@
- The file descriptors created for internal bookkeeping by `ioselector_kqueue`
and `ioselector_epoll` will no longer be leaked to child processes.
- `critbits` adds `commonPrefixLen`.
- `relativePath(rel, abs)` and `relativePath(abs, rel)` used to silently give wrong results
(see #13222); instead they now use `getCurrentDir` to resolve those cases,
and this can now throw in edge cases where `getCurrentDir` throws.

View File

@@ -518,6 +518,22 @@ proc `$`*[T](c: CritBitTree[T]): string =
result.addQuoted(val)
result.add("}")
proc commonPrefixLen*[T](c: CritBitTree[T]): int {.inline, since((1, 3)).} =
## Returns longest common prefix length of all keys of `c`.
## If `c` is empty, returns 0.
runnableExamples:
var c: CritBitTree[void]
doAssert c.commonPrefixLen == 0
incl(c, "key1")
doAssert c.commonPrefixLen == 4
incl(c, "key2")
doAssert c.commonPrefixLen == 3
if c.root != nil:
if c.root.isLeaf: len(c.root.key)
else: c.root.byte
else: 0
runnableExamples:
static: