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

BIN
shf

Binary file not shown.