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
commit d5d3594457
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

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: