mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 22:10:33 +00:00
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:
@@ -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.
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user