refactor function in gap_buffer.sml to make use of two helper functions when inserting into the left/right
This commit is contained in:
124
gap_buffer.sml
124
gap_buffer.sml
@@ -42,6 +42,20 @@ struct
|
|||||||
, right = right
|
, right = right
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun joinEndOfLeft (newString, left) =
|
||||||
|
case left of
|
||||||
|
hd :: tail =>
|
||||||
|
if isLessThanTarget (newString, hd) then (hd ^ newString) :: tail
|
||||||
|
else newString :: left
|
||||||
|
| [] => newString :: left
|
||||||
|
|
||||||
|
fun joinStartOfRight (newString, right) =
|
||||||
|
case right of
|
||||||
|
hd :: tail =>
|
||||||
|
if isLessThanTarget (newString, hd) then (newString ^ hd) :: tail
|
||||||
|
else newString :: right
|
||||||
|
| [] => newString :: right
|
||||||
|
|
||||||
fun preferInsertLeft (curIdx, newString, left, right) =
|
fun preferInsertLeft (curIdx, newString, left, right) =
|
||||||
case left of
|
case left of
|
||||||
hd :: tail =>
|
hd :: tail =>
|
||||||
@@ -60,46 +74,12 @@ struct
|
|||||||
| [] => consLeft (curIdx, newString, left, right))
|
| [] => consLeft (curIdx, newString, left, right))
|
||||||
| [] => consLeft (curIdx, newString, left, right)
|
| [] => consLeft (curIdx, newString, left, right)
|
||||||
|
|
||||||
fun joinEndOfLeft (newString, left) =
|
fun insLeftLeaf (prevIdx, idx, newString, curIdx, right, hd, tail) =
|
||||||
case left of
|
|
||||||
hd :: tail =>
|
|
||||||
if isLessThanTarget (newString, hd) then (hd ^ newString) :: tail
|
|
||||||
else newString :: left
|
|
||||||
| [] => newString :: left
|
|
||||||
|
|
||||||
fun joinStartOfRight (newString, right) =
|
|
||||||
case right of
|
|
||||||
hd :: tail =>
|
|
||||||
if isLessThanTarget (newString, hd) then (newString ^ hd) :: tail
|
|
||||||
else newString :: right
|
|
||||||
| [] => newString :: right
|
|
||||||
|
|
||||||
fun ins (idx, newString, curIdx, left, right) : t =
|
|
||||||
if curIdx = idx then
|
|
||||||
preferInsertLeft (curIdx, newString, left, right)
|
|
||||||
else if idx < curIdx then
|
|
||||||
(* Need to insert on the left. *)
|
|
||||||
case left of
|
|
||||||
[] =>
|
|
||||||
(* If there is no string on the left, then add the new string there. *)
|
|
||||||
{idx = String.size newString, left = [newString], right = right}
|
|
||||||
| hd :: tail =>
|
|
||||||
let
|
|
||||||
val prevIdx = curIdx - String.size hd
|
|
||||||
in
|
|
||||||
if
|
|
||||||
idx < prevIdx
|
|
||||||
then
|
|
||||||
(* The requested index is prior to the string on the left,
|
|
||||||
* so move leftward one string. *)
|
|
||||||
ins (idx, newString, prevIdx, tail, joinStartOfRight (hd, right))
|
|
||||||
else
|
|
||||||
(* The requested index is either:
|
(* The requested index is either:
|
||||||
* - At the start of the left string
|
* - At the start of the left string
|
||||||
* - In the middle of the left string
|
* - In the middle of the left string
|
||||||
* Find out which and split the middle of the string if necessary. *) if
|
* Find out which and split the middle of the string if necessary. *)
|
||||||
idx = prevIdx
|
if idx = prevIdx then
|
||||||
then
|
|
||||||
(* At start of string. *)
|
(* At start of string. *)
|
||||||
{ idx = curIdx + String.size newString
|
{ idx = curIdx + String.size newString
|
||||||
, right = right
|
, right = right
|
||||||
@@ -117,18 +97,15 @@ struct
|
|||||||
* Using `newString ^ hd` with `newString :: hd` gives
|
* Using `newString ^ hd` with `newString :: hd` gives
|
||||||
* different contents in the case of a zipper.
|
* different contents in the case of a zipper.
|
||||||
* *)
|
* *)
|
||||||
if isLessThanTarget (newString, hd) then
|
if isLessThanTarget (newString, hd) then (newString ^ hd) :: tail
|
||||||
(newString ^ hd) :: tail
|
else hd :: newString :: tail
|
||||||
else
|
|
||||||
hd :: newString :: tail
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
(* In middle of string. *)
|
(* In middle of string. *)
|
||||||
let
|
let
|
||||||
val length = idx - prevIdx
|
val length = idx - prevIdx
|
||||||
val sub1 = String.substring (hd, 0, length)
|
val sub1 = String.substring (hd, 0, length)
|
||||||
val sub2 = String.substring
|
val sub2 = String.substring (hd, length, String.size hd - length)
|
||||||
(hd, length, String.size hd - length)
|
|
||||||
in
|
in
|
||||||
if isThreeLessThanTarget (sub1, newString, sub2) then
|
if isThreeLessThanTarget (sub1, newString, sub2) then
|
||||||
{ idx = curIdx + String.size newString
|
{ idx = curIdx + String.size newString
|
||||||
@@ -142,7 +119,7 @@ struct
|
|||||||
}
|
}
|
||||||
else if isLessThanTarget (newString, sub2) then
|
else if isLessThanTarget (newString, sub2) then
|
||||||
{ idx = prevIdx + String.size sub1
|
{ idx = prevIdx + String.size sub1
|
||||||
, left = sub1 :: tail
|
, left = joinEndOfLeft (sub1, tail)
|
||||||
, right = (newString ^ sub2) :: right
|
, right = (newString ^ sub2) :: right
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -151,32 +128,21 @@ struct
|
|||||||
, right = sub1 :: newString :: sub2 :: right
|
, right = sub1 :: newString :: sub2 :: right
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
|
||||||
else
|
fun insRightLeaf (nextIdx, idx, newString, curIdx, left, hd, tail) =
|
||||||
(* Need to insert to the right. *)
|
if idx = nextIdx then
|
||||||
case right of
|
|
||||||
[] => {idx = curIdx, left = left, right = [newString]}
|
|
||||||
| hd :: tail =>
|
|
||||||
let
|
|
||||||
val nextIdx = String.size hd + curIdx
|
|
||||||
in
|
|
||||||
if idx > nextIdx then
|
|
||||||
ins (idx, newString, nextIdx, joinEndOfLeft (hd, left), tail)
|
|
||||||
else if idx = nextIdx then
|
|
||||||
(* At end of next string. *)
|
(* At end of next string. *)
|
||||||
if isLessThanTarget (newString, hd) then
|
|
||||||
{idx = curIdx, left = left, right = (hd ^ newString) :: tail}
|
|
||||||
else
|
|
||||||
{ idx = curIdx
|
{ idx = curIdx
|
||||||
, left = left
|
, left = left
|
||||||
, right = hd :: (joinStartOfRight (newString, tail))
|
, right =
|
||||||
|
if isLessThanTarget (newString, hd) then (hd ^ newString) :: tail
|
||||||
|
else hd :: (joinStartOfRight (newString, tail))
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
let
|
let
|
||||||
val length = idx - curIdx
|
val length = idx - curIdx
|
||||||
val sub1 = String.substring (hd, 0, length)
|
val sub1 = String.substring (hd, 0, length)
|
||||||
val sub2 = String.substring
|
val sub2 = String.substring (hd, length, String.size hd - length)
|
||||||
(hd, length, String.size hd - length)
|
|
||||||
in
|
in
|
||||||
if isThreeLessThanTarget (sub1, newString, sub2) then
|
if isThreeLessThanTarget (sub1, newString, sub2) then
|
||||||
{ idx =
|
{ idx =
|
||||||
@@ -201,6 +167,40 @@ struct
|
|||||||
, right = joinStartOfRight (sub2, tail)
|
, right = joinStartOfRight (sub2, tail)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
fun ins (idx, newString, curIdx, left, right) : t =
|
||||||
|
if curIdx = idx then
|
||||||
|
preferInsertLeft (curIdx, newString, left, right)
|
||||||
|
else if idx < curIdx then
|
||||||
|
(* Need to insert on the left. *)
|
||||||
|
case left of
|
||||||
|
[] =>
|
||||||
|
(* If there is no string on the left, then add the new string there. *)
|
||||||
|
{idx = String.size newString, left = [newString], right = right}
|
||||||
|
| hd :: tail =>
|
||||||
|
let
|
||||||
|
val prevIdx = curIdx - String.size hd
|
||||||
|
in
|
||||||
|
if idx < prevIdx then
|
||||||
|
(* The requested index is prior to the string on the left,
|
||||||
|
* so move leftward one string. *)
|
||||||
|
ins (idx, newString, prevIdx, tail, joinStartOfRight (hd, right))
|
||||||
|
else
|
||||||
|
(* Call function to insert at the current node in the zipper. *)
|
||||||
|
insLeftLeaf (prevIdx, idx, newString, curIdx, right, hd, tail)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
(* Need to insert to the right. *)
|
||||||
|
case right of
|
||||||
|
[] => {idx = curIdx, left = left, right = [newString]}
|
||||||
|
| hd :: tail =>
|
||||||
|
let
|
||||||
|
val nextIdx = String.size hd + curIdx
|
||||||
|
in
|
||||||
|
if idx > nextIdx then
|
||||||
|
ins (idx, newString, nextIdx, joinEndOfLeft (hd, left), tail)
|
||||||
|
else
|
||||||
|
insRightLeaf (nextIdx, idx, newString, curIdx, right, hd, tail)
|
||||||
end
|
end
|
||||||
|
|
||||||
fun insert (idx, newString, buffer: t) =
|
fun insert (idx, newString, buffer: t) =
|
||||||
|
|||||||
3172
svelte_gap.txt
3172
svelte_gap.txt
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user