address a bug where Vector.sub (keys, ~1) was used, solving subscript error, but revealing another error in a subsequent test case

This commit is contained in:
2024-09-07 21:35:31 +01:00
parent 352e4ff611
commit 01d79cd801
3 changed files with 33 additions and 14 deletions

View File

@@ -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

Binary file not shown.

View File

@@ -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")