From 0d25b88caa8105c6a199cdfd9e0526f93057d831 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Wed, 4 Sep 2024 12:05:43 +0100 Subject: [PATCH] change 'helpInsert' function to return trie instead of trie option (I would guess there are fewer cases where the same string is inserted into the trie repeatedly) --- src/string-set | Bin 283368 -> 283368 bytes src/string-set.sml | 59 +++++++++++++-------------------------------- 2 files changed, 17 insertions(+), 42 deletions(-) diff --git a/src/string-set b/src/string-set index 13ce0a95295bfcc8d07cd096db2c13ac1c855b53..c57ee60bf8fd240b3d594df7a13d148b13942bd8 100755 GIT binary patch delta 456 zcmZusPbfoi9DkqR@9nkLqW7W=v1h!o<^OA=IY>!d9NfiQa&S(Ki)4qD zcCb0pd115!y)xdhvo-D+!hp5M2hNDbCbh7 J+b#Cgd;<0vjne=C delta 462 zcmZusPbh<782^6n`|V3>(f39Eh57h4TOqX39ApO}B*{U^zQb~$h)kqeq1Bvr@!n*# z%an1@dr-sC#bGJsBsuJ+D1R>U*=84>!}Ij(d47MMOkB>y<&#@ds?E8##_JDFD@Ut0 z=kw`Z@6NisnISww&`+es`smox0(YA*0=)8V33o+M`BVum03WN7kt~jpP#U4Sk9SHE zR*B!s@K~VpvRX_NvM=(06%i;0#+7PZ!74=GsgO`5uBb>+MbFb8R3REtgwK7EJ!%Ld z#&jFXi7K%*8$8h2nGM~_uUT(aCTmFSI7V?nhL$G|M3!nYOk;e&feq2%UN@ulzX;|Q zypcZnNUX*Ob&@aYrzVh3JOSfJtOsS4x$aKHr~aFNSr7SyPPP3D`8 zjcU})oCf3(jizX&UC32$Uv46g?M!#jX|%E{2kn6thLdtQ#V@loyF56!G|WvdF-|1^ OjNdyn!B0BHuIg{5AdmC_ diff --git a/src/string-set.sml b/src/string-set.sml index 2f3e031..bdade84 100644 --- a/src/string-set.sml +++ b/src/string-set.sml @@ -220,7 +220,6 @@ struct ) = let val childNode = CHILDREN {keys = childKeys, children = childChildren} - val keys = Vector.mapi (fn (idx, key) => if idx <> insIdx then key else splitTrieKeyStart) @@ -230,9 +229,8 @@ struct Vector.mapi (fn (idx, elt) => if idx <> insIdx then elt else childNode) parentChildren - val node = CHILDREN {keys = keys, children = children} in - SOME node + CHILDREN {keys = keys, children = children} end (* @@ -240,21 +238,16 @@ struct * - Complete code for FOUND_WITH_CHILDREN case in insert function. *) - fun helpInsert (insKey, keyPos, trie) : t option = + fun helpInsert (insKey, keyPos, trie) : t = case trie of FOUND => - let - val node = - if keyPos = String.size insKey then - FOUND - else - FOUND_WITH_CHILDREN - { keys = Vector.fromList [insKey] - , children = Vector.fromList [FOUND] - } - in - SOME node - end + if keyPos = String.size insKey then + FOUND + else + FOUND_WITH_CHILDREN + { keys = Vector.fromList [insKey] + , children = Vector.fromList [FOUND] + } | CHILDREN {keys, children} => let val findChr = String.sub (insKey, keyPos) @@ -273,9 +266,8 @@ struct else if idx > insIdx then Vector.sub (children, idx - 1) else FOUND) - val node = CHILDREN {keys = newKeys, children = newChildren} in - SOME node + CHILDREN {keys = newKeys, children = newChildren} end | FOUND_INSERT_POS insIdx => let @@ -283,7 +275,7 @@ struct val nextKeyPos = keyPos + 1 in (case insertKeyMatch (insKey, trieKey, nextKeyPos) of - NO_INSERT_MATCH => SOME trie + NO_INSERT_MATCH => trie (* may need to split string if difference found but prefix matched *) | DIFFERENCE_FOUND_AT diffIdx => let @@ -342,23 +334,14 @@ struct * however, if this is a non-found node, then I need to change * the tag/case. *) | FULL_INSERT_MATCH => - let - val node = - FOUND_WITH_CHILDREN {keys = keys, children = children} - in - SOME node - end + FOUND_WITH_CHILDREN {keys = keys, children = children} (* if insert key contains trie key, need to recurse down node *) | INSERT_KEY_CONTAINS_TRIE_KEY => let val trieChild = Vector.sub (children, insIdx) in - (case - helpInsert - (insKey, keyPos + String.size trieKey, trieChild) - of - (result as SOME _) => result - | NONE => SOME trie) + helpInsert + (insKey, keyPos + String.size trieKey, trieChild) end (* if trie key contains insert key, need to split node *) | TRIE_KEY_CONTAINS_INSERT_KEY => @@ -385,10 +368,8 @@ struct if idx <> insIdx then elt else newTrieChild) children - val node = - CHILDREN {keys = newKeys, children = newChildren} in - SOME node + CHILDREN {keys = newKeys, children = newChildren} end) end | APPEND_NEW_CHILD => @@ -396,20 +377,14 @@ struct val newKeys = Vector.concat [keys, Vector.fromList [insKey]] val newChildren = Vector.concat [children, Vector.fromList [FOUND]] - val node = CHILDREN {keys = newKeys, children = newChildren} in - SOME node + CHILDREN {keys = newKeys, children = newChildren} end) end | FOUND_WITH_CHILDREN {keys, children} => raise Empty (* todo *) fun insert (insKey, trie) = - if String.size insKey > 0 then - case helpInsert (insKey, 0, trie) of - SOME trie => trie - | NONE => trie - else - trie + if String.size insKey > 0 then helpInsert (insKey, 0, trie) else trie fun fromString str = CHILDREN {keys = Vector.fromList [str], children = Vector.fromList [FOUND]}