From 86f171cd08be1ac461f1ca3744fc08b31dd40cfe Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Wed, 4 Sep 2024 15:26:21 +0100 Subject: [PATCH] try implementing getPrefixList function, but it seems insert function has an error, so made a note of it to fix this error in a comment; fixing this error is the first priority --- src/string-set.sml | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/src/string-set.sml b/src/string-set.sml index 30da941..fe5fde6 100644 --- a/src/string-set.sml +++ b/src/string-set.sml @@ -180,6 +180,32 @@ struct fun getPrefixSubtrie (prefix, trie) = helpGetPrefixSubtrie (prefix, 0, trie) + fun recurseHelpGetPrefixList (pos, keys, children, acc) = + if pos < 0 then + acc + else + let + val curChild = Vector.sub (children, pos) + val acc = helpGetPrefixList (curChild, acc) + val acc = + if isFoundNode curChild then Vector.sub (keys, pos) :: acc else acc + in + recurseHelpGetPrefixList (pos - 1, keys, children, acc) + end + + and helpGetPrefixList (trie, acc) = + case trie of + CHILDREN {keys, children} => + recurseHelpGetPrefixList (Vector.length keys - 1, keys, children, acc) + | FOUND_WITH_CHILDREN {keys, children} => + recurseHelpGetPrefixList (Vector.length keys - 1, keys, children, acc) + | FOUND => acc + + fun getPrefixList (prefix, trie) = + case getPrefixSubtrie (prefix, trie) of + SOME subtrie => helpGetPrefixList (trie, []) + | NONE => [] + datatype insert_string_match = NO_INSERT_MATCH (* may need to split string if difference found but prefix matched *) @@ -464,8 +490,18 @@ struct end | fromList ([]) = raise Empty -(* - * todo: - * Implement prefix searching, returning all keys that match a given string prefix. - *) + (* + * todo: + * - Fix insert function. + * - Once insert is fixed, test prefix searching, + * which turns found strings to list. + * + * Problem with insert function: + * The "trie" value below does not contain "hello". It somehow disappeared. + * Should probably attempt to create a test suite, to catch regressions as well. + *) + + val trie = fromList ["hello", "hit", "hi"] + val lst = getPrefixList ("hi", trie) + val helloExists = exists ("hello", trie) end