fixed two issues related to insertion in search-list.sml. First issue: in top level 'insert' function, we called 'insLeft' if new value is greater than first value on right, and we called 'insRight' in the opposite case; this is backwards. Second issue: in base cases of 'insLeft' and 'insRight', when we have reached the end of one side of the list, we used to return the same list we got back instead of adding the new value to this end of the list; this is now fixed.

This commit is contained in:
2024-11-16 19:51:29 +00:00
parent 09d9945201
commit 5e9f118b15
2 changed files with 21 additions and 20 deletions

View File

@@ -16,7 +16,7 @@ struct
type t = {left: int vector list, right: int vector list}
(* temp function for testing *)
fun printlst (left, right) =
fun printlst {left, right} =
let
val left = List.rev left
val lst = List.concat [left, right]
@@ -124,7 +124,10 @@ struct
(* new = first *)
{left = left, right = right}
end
| [] => {left = left, right = right}
| [] =>
let val new = Vector.fromList [new]
in {left = left, right = joinStartOfRight (new, right)}
end
fun insRight (new, left, right) =
case right of
@@ -155,13 +158,16 @@ struct
(* new = last *)
{left = left, right = right}
end
| [] => {left = left, right = right}
| [] =>
let val new = Vector.fromList [new]
in {left = joinEndOfLeft (new, left), right = right}
end
fun insert (new, {left, right}: t) =
(* look at elements to see which way to traverse *)
case right of
hd :: _ =>
if Vector.sub (hd, 0) >= new then insRight (new, left, right)
if new > Vector.sub (hd, 0) then insRight (new, left, right)
else insLeft (new, left, right)
| [] => insLeft (new, left, right)
@@ -231,8 +237,7 @@ struct
(* finish = last *)
{left = left, right = tl}
end
| [] =>
{left = left, right = right}
| [] => {left = left, right = right}
fun moveRightAndDelete (start, finish, left, right) =
case right of
@@ -400,13 +405,10 @@ struct
let
val llast = Vector.sub (lhd, Vector.length lhd - 1)
in
if finish = llast then
delLeftFromHere (start, left, right)
else
moveLeftAndDelete (start, finish, left, right)
if finish = llast then delLeftFromHere (start, left, right)
else moveLeftAndDelete (start, finish, left, right)
end
| [] =>
{left = left, right = right})
| [] => {left = left, right = right})
else
(* finish >= rfirst *)
delFromLeftAndRight (start, finish, left, right)
@@ -469,8 +471,7 @@ struct
val startIdx = BinSearch.equalOrMore (from, hd)
val mapEl = Vector.sub (hd, startIdx)
val newHd =
Vector.map (fn el => if el < from then el else el + mapBy)
hd
Vector.map (fn el => if el < from then el else el + mapBy) hd
in
mapRight (mapBy, joinEndOfLeft (newHd, left), tl)
end

BIN
shf

Binary file not shown.