diff --git a/src/string-set.sml b/src/string-set.sml index 267eff2..f0bf5d0 100644 --- a/src/string-set.sml +++ b/src/string-set.sml @@ -538,19 +538,37 @@ struct * - otherwise, just remove the key and child at this idx from parent * *) if Vector.length keys > 1 then - let - val newKeys = Vector.tabulate (Vector.length keys - 1, fn keyIdx => - Vector.sub (keys, if keyIdx >= idx then keyIdx - 1 else keyIdx)) + if idx > 0 then + let + val newKeys = Vector.tabulate (Vector.length keys - 1, fn keyIdx => + Vector.sub (keys, if keyIdx >= idx then keyIdx - 1 else keyIdx)) - val newChildren = - Vector.tabulate (Vector.length keys - 1, fn childIdx => - Vector.sub - (children, if childIdx >= idx then childIdx - 1 else childIdx)) + val newChildren = + Vector.tabulate (Vector.length keys - 1, fn childIdx => + Vector.sub + (children, if childIdx >= idx then childIdx - 1 else childIdx)) - val newNode = parentConstructor {keys = newKeys, children = newChildren} - in - CHANGED newNode - end + val newNode = + parentConstructor {keys = newKeys, children = newChildren} + in + CHANGED newNode + end + else + (* if idx = 0, then have to slice first element off from vector *) + let + val keySlice = VectorSlice.slice (keys, 1, SOME + (Vector.length keys - 1)) + val newKeys = VectorSlice.vector keySlice + + val childrenSlice = VectorSlice.slice (children, 1, SOME + (Vector.length children - 1)) + val newChildren = VectorSlice.vector childrenSlice + + val newNode = + parentConstructor {keys = newKeys, children = newChildren} + in + CHANGED newNode + end else MADE_EMPTY diff --git a/tests/string-set-tests b/tests/string-set-tests index 9f4d2a6..4b58792 100755 Binary files a/tests/string-set-tests and b/tests/string-set-tests differ diff --git a/tests/string-set-tests.sml b/tests/string-set-tests.sml index 5ca1c86..aecfd40 100644 --- a/tests/string-set-tests.sml +++ b/tests/string-set-tests.sml @@ -118,18 +118,19 @@ struct val trie3 = StringSet.remove ("abcd", trie) val _ = assertTrue (trie = trie3, "removing key (abcd) which doesn't exist in trie returns same trie") - (* ERROR: Removing "abcde" causes a subscript error. Need to fix. *) val _ = assertTrue (StringSet.exists ("abcde", trie), "abcde exists before remove in remove5") val trie = StringSet.remove ("abcde", trie) val _ = assertFalse (StringSet.exists ("abcde", trie), "abcde does not exist after remove in remove5") val _ = assertTrue (StringSet.exists ("x", trie), "x exists before remove in remove5") val trie = StringSet.remove ("x", trie) - val _ = assertTrue (StringSet.exists ("x", trie), "x does not exist after remove in remove5") + val _ = assertFalse (StringSet.exists ("x", trie), "x does not exist after remove in remove5") + (* error: "abc" should exist at this point, but it seems not to. + * find out why and fix. *) val _ = assertTrue (StringSet.exists ("abc", trie), "abc exists before remove in remove5") val trie = StringSet.remove ("abc", trie) - val _ = assertTrue (StringSet.exists ("abc", trie), "abc does not exist after remove in remove5") + val _ = assertFalse (StringSet.exists ("abc", trie), "abc does not exist after remove in remove5") val _ = assertTrue (StringSet.exists ("ab", trie), "trie still contains ab after removals in remove5") val _ = assertTrue (StringSet.exists ("ade", trie), "trie still contains ade after removals in remove5")