From c6343cac403838de0d9fe6856bcac4de9be102e1 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Sun, 24 Nov 2024 20:42:17 +0000 Subject: [PATCH] in fcore/search-list.sml, only call 'insMiddle' function if new value is not in middle --- fcore/search-list.sml | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/fcore/search-list.sml b/fcore/search-list.sml index 77fb669..420ba3c 100644 --- a/fcore/search-list.sml +++ b/fcore/search-list.sml @@ -63,9 +63,8 @@ struct new :: right | [] => new :: right - fun insMiddle (new, hd, tl, left, right) = + fun insMiddle (new, middle, hd, tl, left, right) = let - val middle = BinSearch.equalOrMore (new, hd) val leftSlice = VectorSlice.slice (hd, 0, SOME middle) val rightLength = Vector.length hd - middle val rightSlice = VectorSlice.slice (hd, middle, SOME rightLength) @@ -111,8 +110,17 @@ struct val last = Vector.sub (hd, Vector.length hd - 1) in if new < last then - (* have to insert in middle *) - insMiddle (new, hd, tl, left, right) + (* new is or should be in middle *) + let + val middle = BinSearch.equalOrMore (new, hd) + in + if new <> Vector.sub (hd, middle) then + (* not in middle, so insert *) + insMiddle (new, middle, hd, tl, left, right) + else + (* new is in middle, so just return *) + {left = left, right = right} + end else if new > last then (* have to insert new at end of left * or start of right (both are equivalent) *) @@ -146,7 +154,16 @@ struct in if new > first then (* have to insert in middle *) - insMiddle (new, hd, tl, left, right) + let + val middle = BinSearch.equalOrMore (new, hd) + in + if new <> Vector.sub (hd, middle) then + (* not in middle, so insert *) + insMiddle (new, middle, hd, tl, left, right) + else + (* new is in middle, so return *) + {left = left, right = right} + end else if new < first then (* have to insert new at start of right * or end of left (both are equivalent) *)