begin implementation of gap_map.sml

This commit is contained in:
2025-02-12 01:51:39 +00:00
parent 713d17fdd3
commit 0ba1679e9a
3 changed files with 55 additions and 1 deletions

Binary file not shown.

52
src/gap_map.sml Normal file
View File

@@ -0,0 +1,52 @@
signature GAP_MAP_PAIR =
sig
type key
type value
val l: key * key -> bool
val eq: key * key -> bool
val g: key * key -> bool
val maxNodeSize: int
end
signature GAP_MAP =
sig
structure Fn: GAP_MAP_PAIR
type t
val empty: t
val isEmpty: t -> bool
val add: Fn.key * Fn.value * t -> t
val remove: Fn.key * t -> t
val get: Fn.key * t -> Fn.value option
val min: t -> (Fn.key * Fn.value) option
val max: t -> (Fn.key * Fn.value) option
val moveToStart: t -> t
val moveToEnd: t -> t
val moveTo: Fn.key * t -> t
end
functor MakeGapMap(Fn: GAP_MAP_PAIR): GAP_MAP =
struct
structure Fn = Fn
type t =
{ leftKeys: Fn.key vector list
, letVals: Fn.value vector list
, rightKeys: Fn.key vector list
, rightVals: Fn.value vector list
}
val empty = {leftKeys = [], leftVals = [], rightKeys = [], rightVals = []}
fun isEmpty {leftKeys = [], rightKeys = [], ...} = true
| isEmpty _ = false
fun isLessThanTarget (v1, v2) =
Vector.length v1 + Vector.length v2 <= Fn.maxNodeSize
end

View File

@@ -18,6 +18,8 @@ sig
val empty: t
val isEmpty: t -> bool
val singleton: Fn.key -> t
val add: Fn.key * t -> t
val remove: Fn.key * t -> t
val removeMany: Fn.key * Fn.key * t -> t
@@ -46,7 +48,7 @@ struct
| isEmpty _ = false
fun singleton x =
{left = [], right = Vector.fromList [x]}
{left = [], right = [Vector.fromList [x]]}
fun isLessThanTarget (v1, v2) =
Vector.length v1 + Vector.length v2 <= Fn.maxNodeSize