2024-11-13 12:54:47 +00:00
|
|
|
signature SEARCH_LIST =
|
|
|
|
|
sig
|
|
|
|
|
type t = {left: int vector list, right: int vector list}
|
|
|
|
|
val empty: t
|
2024-11-15 05:37:53 +00:00
|
|
|
|
2024-11-24 21:38:58 +00:00
|
|
|
val exists: int * t -> bool
|
2024-11-13 12:54:47 +00:00
|
|
|
val insert: int * t -> t
|
2024-11-15 05:37:53 +00:00
|
|
|
val append: int * t -> t
|
2024-11-16 09:05:47 +00:00
|
|
|
val delete: int * int * string * t -> t
|
2024-11-15 05:37:53 +00:00
|
|
|
|
|
|
|
|
val goToNum: int * t -> t
|
2024-11-16 08:33:26 +00:00
|
|
|
val mapFrom: int * int * t -> t
|
2025-08-04 12:08:35 +01:00
|
|
|
|
|
|
|
|
val toVector: t -> int vector
|
|
|
|
|
val toString: t -> string
|
2024-11-13 12:54:47 +00:00
|
|
|
end
|
|
|
|
|
|
2025-08-06 00:16:50 +01:00
|
|
|
structure SearchList: SEARCH_LIST =
|
2024-11-11 13:23:37 +00:00
|
|
|
struct
|
2025-08-06 00:16:50 +01:00
|
|
|
structure IntSet =
|
|
|
|
|
MakeGapSet
|
|
|
|
|
(struct
|
|
|
|
|
type key = int
|
2024-11-11 13:23:37 +00:00
|
|
|
|
2025-08-06 00:16:50 +01:00
|
|
|
val maxNodeSize = 32
|
2024-11-11 13:23:37 +00:00
|
|
|
|
2025-08-06 00:16:50 +01:00
|
|
|
fun l (a: int, b) = a < b
|
|
|
|
|
fun eq (a: int, b) = a = b
|
|
|
|
|
fun g (a: int, b) = a > b
|
|
|
|
|
end)
|
2024-11-11 13:23:37 +00:00
|
|
|
|
2025-08-05 13:24:55 +01:00
|
|
|
type t = IntSet.t
|
2024-11-11 13:23:37 +00:00
|
|
|
|
2025-08-05 13:24:55 +01:00
|
|
|
fun helpToVector (left, right) =
|
2024-11-11 13:23:37 +00:00
|
|
|
case left of
|
2025-08-05 13:24:55 +01:00
|
|
|
hd :: tl => helpToVector (tl, hd :: right)
|
|
|
|
|
| [] => Vector.concat right
|
2024-11-11 13:23:37 +00:00
|
|
|
|
2025-08-05 13:24:55 +01:00
|
|
|
(* for testing *)
|
|
|
|
|
fun toVector {left, right} = helpToVector (left, right)
|
2024-11-11 13:23:37 +00:00
|
|
|
|
2024-11-12 03:05:14 +00:00
|
|
|
|
2025-08-05 13:24:55 +01:00
|
|
|
val empty = IntSet.empty
|
|
|
|
|
|
|
|
|
|
fun insert (num, set) =
|
2025-08-06 00:16:50 +01:00
|
|
|
let val () = print ("adding num: " ^ Int.toString num ^ "\n")
|
|
|
|
|
in IntSet.add (num, set)
|
2024-11-12 03:05:14 +00:00
|
|
|
end
|
2024-11-11 13:23:37 +00:00
|
|
|
|
2025-08-05 13:24:55 +01:00
|
|
|
val append = IntSet.add
|
2024-11-11 13:23:37 +00:00
|
|
|
|
2025-08-05 13:24:55 +01:00
|
|
|
val goToNum = IntSet.moveTo
|
2024-11-12 07:57:36 +00:00
|
|
|
|
2025-08-05 13:24:55 +01:00
|
|
|
fun delete (start, length, searchString, set) =
|
2024-11-16 09:05:47 +00:00
|
|
|
if length > 0 then
|
|
|
|
|
let
|
2025-08-05 13:24:55 +01:00
|
|
|
val firstVec = toVector set
|
2024-11-16 09:05:47 +00:00
|
|
|
val finish = start + length
|
|
|
|
|
val start = start - String.size searchString + 1
|
2025-08-05 13:24:55 +01:00
|
|
|
val result = IntSet.removeMany (start, finish, set)
|
|
|
|
|
|
|
|
|
|
val secondVec = toVector result
|
|
|
|
|
|
2025-08-06 00:16:50 +01:00
|
|
|
val () = print
|
|
|
|
|
("delete start has " ^ Int.toString (Vector.length firstVec)
|
|
|
|
|
^ "elements\n")
|
|
|
|
|
val () = print
|
|
|
|
|
("delete result has " ^ Int.toString (Vector.length secondVec)
|
|
|
|
|
^ "elements\n")
|
2024-11-16 09:05:47 +00:00
|
|
|
in
|
2025-08-05 13:24:55 +01:00
|
|
|
result
|
2024-11-16 09:05:47 +00:00
|
|
|
end
|
|
|
|
|
else
|
2025-08-05 13:24:55 +01:00
|
|
|
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
|
2024-11-13 12:54:47 +00:00
|
|
|
|
|
|
|
|
(* 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}
|
|
|
|
|
|
2024-11-16 04:55:02 +00:00
|
|
|
fun moveRightAndMap (from, mapBy, left, right) =
|
2024-11-13 12:54:47 +00:00
|
|
|
case right of
|
|
|
|
|
hd :: tl =>
|
|
|
|
|
let
|
|
|
|
|
val lastIdx = Vector.length hd - 1
|
|
|
|
|
val last = Vector.sub (hd, lastIdx)
|
|
|
|
|
in
|
2024-11-16 04:55:02 +00:00
|
|
|
if from > last then
|
|
|
|
|
moveRightAndMap (from, mapBy, joinEndOfLeft (hd, left), tl)
|
|
|
|
|
else if from < last then
|
2024-11-13 12:54:47 +00:00
|
|
|
(* need to map in middle *)
|
|
|
|
|
let
|
2024-11-16 04:55:02 +00:00
|
|
|
val startIdx = BinSearch.equalOrMore (from, hd)
|
2024-11-13 12:54:47 +00:00
|
|
|
val mapEl = Vector.sub (hd, startIdx)
|
|
|
|
|
val newHd =
|
2024-11-16 19:51:29 +00:00
|
|
|
Vector.map (fn el => if el < from then el else el + mapBy) hd
|
2024-11-13 12:54:47 +00:00
|
|
|
in
|
|
|
|
|
mapRight (mapBy, joinEndOfLeft (newHd, left), tl)
|
|
|
|
|
end
|
|
|
|
|
else
|
2024-11-16 04:55:02 +00:00
|
|
|
(* from = last *)
|
2024-11-13 12:54:47 +00:00
|
|
|
let
|
|
|
|
|
val newHd =
|
2024-11-16 04:55:02 +00:00
|
|
|
Vector.map (fn el => if el < from then el else el + mapBy) hd
|
2024-11-13 12:54:47 +00:00
|
|
|
in
|
|
|
|
|
mapRight (mapBy, joinEndOfLeft (newHd, left), tl)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
| [] => {left = left, right = right}
|
|
|
|
|
|
2024-11-16 08:33:26 +00:00
|
|
|
fun mapFrom (num, mapBy, lst) =
|
2024-11-13 12:54:47 +00:00
|
|
|
let
|
|
|
|
|
(* goToNum always places vector where num was found to the right list *)
|
2025-08-05 13:24:55 +01:00
|
|
|
val () = print ("mapping by " ^ Int.toString num ^ "\n")
|
2025-08-06 00:16:50 +01:00
|
|
|
val {left, right} = goToNum (0, lst)
|
2024-11-13 12:54:47 +00:00
|
|
|
in
|
2025-08-05 13:24:55 +01:00
|
|
|
moveRightAndMap (num, 0, left, right)
|
2024-11-13 12:54:47 +00:00
|
|
|
end
|
2024-11-24 21:38:58 +00:00
|
|
|
|
2025-08-05 13:24:55 +01:00
|
|
|
val exists = IntSet.exists
|
2025-08-04 12:08:35 +01:00
|
|
|
|
|
|
|
|
fun toString {left, right} =
|
|
|
|
|
let
|
|
|
|
|
val vec = toVector {left = left, right = right}
|
|
|
|
|
|
2025-08-06 00:16:50 +01:00
|
|
|
val () = print
|
|
|
|
|
("toString has " ^ Int.toString (Vector.length vec) ^ "elements\n")
|
2025-08-05 13:24:55 +01:00
|
|
|
|
2025-08-06 00:16:50 +01:00
|
|
|
val strList =
|
|
|
|
|
Vector.foldr (fn (num, acc) => Int.toString num :: acc) [] vec
|
2025-08-04 12:08:35 +01:00
|
|
|
in
|
2025-08-05 13:24:55 +01:00
|
|
|
""
|
2025-08-04 12:08:35 +01:00
|
|
|
end
|
2024-11-11 13:23:37 +00:00
|
|
|
end
|