add getPrefixList to bro-tree (just folds over the tree in O(n) time, which is the best we can do for that data structure). Will add a benchmark comparison of this.

This commit is contained in:
2024-09-11 11:25:33 +01:00
parent af504350c7
commit db8fa8ae80
2 changed files with 50 additions and 0 deletions

View File

@@ -64,4 +64,24 @@ struct
else if str > k then exists (str, r)
else true
| _ => raise Match
fun foldr (f, acc, tree: bro) =
case tree of
N0 => acc
| N1 t => foldr (f, acc, t)
| N2 (l, k, r) =>
let
val acc = foldr (f, acc, r)
val acc = f (k, acc)
in
foldr (f, acc, l)
end
| _ => raise Match
fun getPrefixList (prefix, tree) =
foldr
( (fn (k, acc) => if String.isSubstring prefix k then k :: acc else acc)
, []
, tree
)
end

View File

@@ -490,4 +490,34 @@ struct
fun insertBinSearch (findChr, keyPos, children) =
helpInsertBinSearch
(findChr, keyPos, children, 0, Vector.length children - 1)
fun insertDifferenceFoundAtLeft
( insKey
, insIdx
, splitTrieKeyStart
, trieChild
, childLeftKeys
, childLeftChildren
, childRightKeys
, childRightChildren
, parentKeys
, parentChildren
, parentConstructor
) =
let
(* child node should always have CHILDREN case,
* because we are splitting prefix into two,
* when neither trieKey nor insKey match the prefix. *)
val childNode = CHILDREN {keys = childKeys, children = childChildren}
val keys =
Vector.mapi
(fn (idx, key) => if idx <> insIdx then key else splitTrieKeyStart)
parentKeys
val children =
Vector.mapi (fn (idx, elt) => if idx <> insIdx then elt else childNode)
parentChildren
in
parentConstructor {keys = keys, children = children}
end
end