delete some dead code
This commit is contained in:
@@ -1,154 +1,96 @@
|
||||
signature SEARCH_LIST =
|
||||
sig
|
||||
type t = {left: int vector list, right: int vector list}
|
||||
val empty: t
|
||||
|
||||
val exists: int * t -> bool
|
||||
val insert: int * t -> t
|
||||
val append: int * t -> t
|
||||
val delete: int * int * string * t -> t
|
||||
|
||||
val goToNum: int * t -> t
|
||||
val mapFrom: int * int * t -> t
|
||||
|
||||
val toVector: t -> int vector
|
||||
val toString: t -> string
|
||||
end
|
||||
|
||||
structure SearchList: SEARCH_LIST =
|
||||
structure SearchList =
|
||||
struct
|
||||
structure IntSet =
|
||||
MakeGapSet
|
||||
(struct
|
||||
type key = int
|
||||
|
||||
val maxNodeSize = 32
|
||||
|
||||
fun l (a: int, b) = a < b
|
||||
fun eq (a: int, b) = a = b
|
||||
fun g (a: int, b) = a > b
|
||||
fun l (a, b: int) = a < b
|
||||
fun eq (a, b: int) = a = b
|
||||
fun g (a, b: int) = a > b
|
||||
end)
|
||||
|
||||
type t = IntSet.t
|
||||
|
||||
fun helpToVector (left, right) =
|
||||
case left of
|
||||
hd :: tl => helpToVector (tl, hd :: right)
|
||||
| [] => Vector.concat right
|
||||
|
||||
(* for testing *)
|
||||
fun toVector {left, right} = helpToVector (left, right)
|
||||
|
||||
|
||||
val empty = IntSet.empty
|
||||
|
||||
fun insert (num, set) =
|
||||
let val () = print ("adding num: " ^ Int.toString num ^ "\n")
|
||||
in IntSet.add (num, set)
|
||||
end
|
||||
|
||||
val append = IntSet.add
|
||||
|
||||
val goToNum = IntSet.moveTo
|
||||
|
||||
fun delete (start, length, searchString, set) =
|
||||
if length > 0 then
|
||||
fun cons (num, acc) =
|
||||
let
|
||||
val num = Vector.fromList [num]
|
||||
in
|
||||
case acc of
|
||||
hd :: tl =>
|
||||
if Vector.length hd < 32 then (Vector.concat [num, hd]) :: tl
|
||||
else num :: acc
|
||||
| [] => num :: acc
|
||||
end
|
||||
|
||||
fun searchStep (pos, hd, absIdx, tl, acc, searchPos, searchString) =
|
||||
if searchPos < 0 then
|
||||
cons (absIdx + 1, acc)
|
||||
else if pos < 0 then
|
||||
case tl of
|
||||
hd :: tl =>
|
||||
searchStep
|
||||
(String.size hd - 1, hd, absIdx, tl, acc, searchPos, searchString)
|
||||
| [] => acc
|
||||
else
|
||||
let
|
||||
val firstVec = toVector set
|
||||
val finish = start + length
|
||||
val start = start - String.size searchString + 1
|
||||
val result = IntSet.removeMany (start, finish, set)
|
||||
|
||||
val secondVec = toVector result
|
||||
|
||||
val () = print
|
||||
("delete start has " ^ Int.toString (Vector.length firstVec)
|
||||
^ "elements\n")
|
||||
val () = print
|
||||
("delete result has " ^ Int.toString (Vector.length secondVec)
|
||||
^ "elements\n")
|
||||
val bufferChr = String.sub (hd, pos)
|
||||
val searchChr = String.sub (searchString, searchPos)
|
||||
in
|
||||
result
|
||||
if bufferChr = searchChr then
|
||||
searchStep
|
||||
(pos - 1, hd, absIdx - 1, tl, acc, searchPos - 1, searchString)
|
||||
else
|
||||
acc
|
||||
end
|
||||
|
||||
fun loopSearch (pos, hd, absIdx, tl, acc, searchString) =
|
||||
if pos < 0 then
|
||||
case tl of
|
||||
hd :: tl =>
|
||||
loopSearch (String.size hd - 1, hd, absIdx, tl, acc, searchString)
|
||||
| [] => acc
|
||||
else
|
||||
let
|
||||
val acc = searchStep
|
||||
(pos, hd, absIdx, tl, acc, String.size searchString - 1, searchString)
|
||||
in
|
||||
loopSearch (pos - 1, hd, absIdx - 1, tl, acc, searchString)
|
||||
end
|
||||
|
||||
(* Prerequisite: move buffer/LineGap to end *)
|
||||
fun search (buffer: LineGap.t, searchString) =
|
||||
let
|
||||
val acc =
|
||||
if String.size searchString = 0 then
|
||||
[]
|
||||
else
|
||||
let
|
||||
val {leftStrings, idx = absIdx, ...} = buffer
|
||||
in
|
||||
case leftStrings of
|
||||
hd :: tl =>
|
||||
loopSearch
|
||||
(String.size hd - 1, hd, absIdx - 1, tl, [], searchString)
|
||||
| [] => []
|
||||
end
|
||||
in
|
||||
{left = [], right = acc}
|
||||
end
|
||||
|
||||
fun build (buffer, searchString) =
|
||||
if String.size searchString > 0 then
|
||||
let
|
||||
val buffer = LineGap.goToEnd buffer
|
||||
val searchList = search (buffer, searchString)
|
||||
in
|
||||
(buffer, searchList)
|
||||
end
|
||||
else
|
||||
set
|
||||
|
||||
fun isLessThanTarget (v1, v2) =
|
||||
Vector.length v1 + Vector.length v2 <= 32
|
||||
|
||||
fun joinEndOfLeft (new, left) =
|
||||
case left of
|
||||
hd :: tail =>
|
||||
if isLessThanTarget (new, hd) then
|
||||
let val newHd = Vector.concat [hd, new]
|
||||
in newHd :: tail
|
||||
end
|
||||
else
|
||||
new :: left
|
||||
| [] => new :: left
|
||||
|
||||
(* go all the way to the end of the list, mapping each hd,
|
||||
* joining the hd to the left,
|
||||
* and return when we have reached the end *)
|
||||
fun mapRight (mapBy, left, right) =
|
||||
case right of
|
||||
hd :: tl =>
|
||||
let val newHd = Vector.map (fn el => el + mapBy) hd
|
||||
in mapRight (mapBy, joinEndOfLeft (newHd, left), tl)
|
||||
end
|
||||
| [] => {left = left, right = right}
|
||||
|
||||
fun moveRightAndMap (from, mapBy, left, right) =
|
||||
case right of
|
||||
hd :: tl =>
|
||||
let
|
||||
val lastIdx = Vector.length hd - 1
|
||||
val last = Vector.sub (hd, lastIdx)
|
||||
in
|
||||
if from > last then
|
||||
moveRightAndMap (from, mapBy, joinEndOfLeft (hd, left), tl)
|
||||
else if from < last then
|
||||
(* need to map in middle *)
|
||||
let
|
||||
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
|
||||
in
|
||||
mapRight (mapBy, joinEndOfLeft (newHd, left), tl)
|
||||
end
|
||||
else
|
||||
(* from = last *)
|
||||
let
|
||||
val newHd =
|
||||
Vector.map (fn el => if el < from then el else el + mapBy) hd
|
||||
in
|
||||
mapRight (mapBy, joinEndOfLeft (newHd, left), tl)
|
||||
end
|
||||
end
|
||||
| [] => {left = left, right = right}
|
||||
|
||||
fun mapFrom (num, mapBy, lst) =
|
||||
let
|
||||
(* goToNum always places vector where num was found to the right list *)
|
||||
val () = print ("mapping by " ^ Int.toString num ^ "\n")
|
||||
val {left, right} = goToNum (0, lst)
|
||||
in
|
||||
moveRightAndMap (num, 0, left, right)
|
||||
end
|
||||
|
||||
val exists = IntSet.exists
|
||||
|
||||
fun toString {left, right} =
|
||||
let
|
||||
val vec = toVector {left = left, right = right}
|
||||
|
||||
val () = print
|
||||
("toString has " ^ Int.toString (Vector.length vec) ^ "elements\n")
|
||||
|
||||
val strList =
|
||||
Vector.foldr (fn (num, acc) => Int.toString num :: acc) [] vec
|
||||
in
|
||||
""
|
||||
end
|
||||
(buffer, empty)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user