tested getPrefixList, fixed errors, and now it seems to work fine

This commit is contained in:
2024-09-04 19:30:29 +01:00
parent dd41c971e3
commit 7cfa393e05

View File

@@ -157,11 +157,11 @@ struct
end end
| FULL_SEARCH_MATCH => | FULL_SEARCH_MATCH =>
let val node = Vector.sub (children, idx) let val node = Vector.sub (children, idx)
in SOME node in SOME (prefix, node)
end end
| TRIE_KEY_CONTAINS_SEARCH_KEY => | TRIE_KEY_CONTAINS_SEARCH_KEY =>
let val node = Vector.sub (children, idx) let val node = Vector.sub (children, idx)
in SOME node in SOME (trieKey, node)
end) end)
end end
| NONE => NONE | NONE => NONE
@@ -173,13 +173,7 @@ struct
helpGetPrefixSubtrieChildren (prefix, keyPos, keys, children, trie) helpGetPrefixSubtrieChildren (prefix, keyPos, keys, children, trie)
| FOUND_WITH_CHILDREN {keys, children} => | FOUND_WITH_CHILDREN {keys, children} =>
helpGetPrefixSubtrieChildren (prefix, keyPos, keys, children, trie) helpGetPrefixSubtrieChildren (prefix, keyPos, keys, children, trie)
| FOUND => | FOUND => NONE
if keyPos = String.size prefix - 1 then
let val node = fromString prefix
in SOME node
end
else
NONE
fun getPrefixSubtrie (prefix, trie) = helpGetPrefixSubtrie (prefix, 0, trie) fun getPrefixSubtrie (prefix, trie) = helpGetPrefixSubtrie (prefix, 0, trie)
@@ -206,7 +200,10 @@ struct
fun getPrefixList (prefix, trie) = fun getPrefixList (prefix, trie) =
case getPrefixSubtrie (prefix, trie) of case getPrefixSubtrie (prefix, trie) of
SOME subtrie => helpGetPrefixList (subtrie, []) SOME (prefix, subtrie) =>
let val lst = helpGetPrefixList (subtrie, [])
in if isFoundNode subtrie then prefix :: lst else lst
end
| NONE => [] | NONE => []
datatype insert_string_match = datatype insert_string_match =
@@ -502,12 +499,8 @@ struct
(* (*
* todo: * todo:
* - Test prefix searching, which turns found strings to list. * - Add removal functionality to remove a key from the list,
* or to mark it is non-found if the key is a prefix
* of other children.
*) *)
val trie = fromList ["hello", "hit", "hi", "henlo", "help"]
val lst = getPrefixList ("hi", trie)
val helloExists = exists ("hello", trie)
val hitExists = exists ("hit", trie)
val hiExists = exists ("hi", trie)
end end