Commit 9d50363e authored by Andrei Paskevich's avatar Andrei Paskevich Committed by François Bobot

provide the Set interface via a Map submodule

parent bab273bb
......@@ -54,10 +54,45 @@ module type S =
(** Added into why stdlib version *)
val change : key -> ('a option -> 'a option) -> 'a t -> 'a t
val union : (key -> 'a -> 'a -> 'a option) -> 'a t -> 'a t -> 'a t
val inter : (key -> 'a -> 'a -> 'a option) -> 'a t -> 'a t -> 'a t
val inter : (key -> 'a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c t
val diff : (key -> 'a -> 'b -> 'a option) -> 'a t -> 'b t -> 'a t
val submap : (key -> 'a -> 'b -> bool) -> 'a t -> 'b t -> bool
val find_default : key -> 'a -> 'a t -> 'a
val mapi_fold:
(key -> 'a -> 'acc -> 'acc * 'b)-> 'a t -> 'acc -> 'acc * 'b t
(key -> 'a -> 'acc -> 'acc * 'b) -> 'a t -> 'acc -> 'acc * 'b t
module S :
sig
type elt
type t
val empty: t
val is_empty: t -> bool
val mem: elt -> t -> bool
val add: elt -> t -> t
val singleton: elt -> t
val remove: elt -> t -> t
val merge: (elt -> bool -> bool -> bool) -> t -> t -> t
val compare: t -> t -> int
val equal: t -> t -> bool
val subset: t -> t -> bool
val iter: (elt -> unit) -> t -> unit
val fold: (elt -> 'a -> 'a) -> t -> 'a -> 'a
val for_all: (elt -> bool) -> t -> bool
val exists: (elt -> bool) -> t -> bool
val filter: (elt -> bool) -> t -> t
val partition: (elt -> bool) -> t -> t * t
val cardinal: t -> int
val elements: t -> elt list
val min_elt: t -> elt
val max_elt: t -> elt
val choose: t -> elt
val split: elt -> t -> t * bool * t
val change : elt -> (bool -> bool) -> t -> t
val union : t -> t -> t
val inter : t -> t -> t
val diff : t -> t -> t
end with type elt = key and type t = unit t
end
module Make(Ord: OrderedType) = struct
......@@ -314,6 +349,13 @@ module Make(Ord: OrderedType) = struct
Empty -> 0
| Node(l, _, _, r, _) -> cardinal l + 1 + cardinal r
let rec keys_aux accu = function
Empty -> accu
| Node(l, v, _, r, _) -> keys_aux (v :: keys_aux accu r) l
let keys s =
keys_aux [] s
let rec bindings_aux accu = function
Empty -> accu
| Node(l, v, d, r, _) -> bindings_aux ((v, d) :: bindings_aux accu r) l
......@@ -382,6 +424,32 @@ module Make(Ord: OrderedType) = struct
| (l2, Some d2, r2) ->
concat_or_join (inter f l1 l2) v1 (f v1 d1 d2) (inter f r1 r2)
let rec diff f s1 s2 =
match (s1, s2) with
(Empty, _t2) -> Empty
| (t1, Empty) -> t1
| (Node(l1, v1, d1, r1, _), t2) ->
match split v1 t2 with
| (l2, None, r2) -> join (diff f l1 l2) v1 d1 (diff f r1 r2)
| (l2, Some d2, r2) ->
concat_or_join (diff f l1 l2) v1 (f v1 d1 d2) (diff f r1 r2)
let rec submap pr s1 s2 =
match (s1, s2) with
| Empty, _ -> true
| _, Empty -> false
| Node (l1, v1, d1, r1, _), (Node (l2, v2, d2, r2, _) as t2) ->
let c = Ord.compare v1 v2 in
if c = 0 then
pr v1 d1 d2 && submap pr l1 l2 && submap pr r1 r2
else if c < 0 then
submap pr (Node (l1, v1, d1, Empty, 0)) l2 && submap pr r1 t2
else
submap pr (Node (Empty, v1, d1, r1, 0)) r2 && submap pr l1 t2
let rec find_default x def = function
Empty -> def
| Node(l, v, d, r, _) ->
......@@ -399,7 +467,44 @@ module Make(Ord: OrderedType) = struct
let acc,r' = mapi_fold f r acc in
acc,Node(l', v, d', r', h)
module S =
struct
type elt = Ord.t
type set = unit t
type t = set
let is_true b = if b then Some () else None
let is_some o = o <> None
let const f e _ = f e
let empty = empty
let is_empty = is_empty
let mem = mem
let add e = add e ()
let singleton e = singleton e ()
let remove = remove
let merge f = merge (fun e a b ->
is_true (f e (is_some a) (is_some b)))
let compare = compare (fun _ _ -> 0)
let equal = equal (fun _ _ -> true)
let subset = submap (fun _ _ _ -> true)
let iter f = iter (const f)
let fold f = fold (const f)
let for_all f = for_all (const f)
let exists f = exists (const f)
let filter f = filter (const f)
let partition f = partition (const f)
let cardinal = cardinal
let elements = keys
let min_elt t = fst (min_binding t)
let max_elt t = fst (max_binding t)
let choose t = fst (choose t)
let split e t = let l,m,r = split e t in l,(m <> None),r
let change e f = change e (fun a -> is_true (f (is_some a)))
let union = union (fun _ _ _ -> Some ())
let inter = inter (fun _ _ _ -> Some ())
let diff = diff (fun _ _ _ -> None)
end
end
......
......@@ -190,7 +190,6 @@ module type S =
(** {3} Added into why stdlib version *)
val change : key -> ('a option -> 'a option) -> 'a t -> 'a t
(** [change x f m] returns a map containing the same bindings as
[m], except the binding of [x] in [m] is changed from [y] to
......@@ -208,11 +207,21 @@ module type S =
function [f] is called only in ambiguous cases.
*)
val inter : (key -> 'a -> 'a -> 'a option) -> 'a t -> 'a t -> 'a t
(** [inter f m1 m2] computes a map whose keys is a subset of keys of [m1]
and of [m2].
val inter : (key -> 'a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c t
(** [inter f m1 m2] computes a map whose keys is a subset of
the intersection of keys of [m1] and of [m2].
*)
val diff : (key -> 'a -> 'b -> 'a option) -> 'a t -> 'b t -> 'a t
(** [diff f m1 m2] computes a map whose keys is a subset of keys
of [m1]. [f] is applied on key which belongs to [m1] and [m2]
if [f] returns [None] the binding is removed from [m1],
otherwise [Some d1] is returned, the key binds to [d1] in [m1] *)
val submap : (key -> 'a -> 'b -> bool) -> 'a t -> 'b t -> bool
(** [submap pr m1 m2] verifies that all the keys in m1 are in m2
and that for each such binding pr is verified. *)
val find_default : key -> 'a -> 'a t -> 'a
(** [find_default x d m] returns the current binding of [x] in [m],
or return [d] if no such binding exists. *)
......@@ -221,11 +230,126 @@ module type S =
(key -> 'a -> 'acc -> 'acc * 'b)-> 'a t -> 'acc -> 'acc * 'b t
(** fold and map at the same time *)
module S :
sig
type elt
type t
(** The type of sets of type [elt]. *)
val empty: t
(** The empty set. *)
val is_empty: t -> bool
(** Test whether a set is empty or not. *)
val mem: elt -> t -> bool
(** [mem x s] returns [true] if [s] contains [x],
and [false] otherwise. *)
val add: elt -> t -> t
(** [add x s] returns a set containing the same elements as
[s], plus [x]. *)
val singleton: elt -> t
(** [singleton x] returns the one-element set that contains [x]. *)
val remove: elt -> t -> t
(** [remove x s] returns a set containing the same elements as [s],
except for [x]. *)
val merge: (elt -> bool -> bool -> bool) -> t -> t -> t
(** [merge f s1 s2] computes a set whose elts is a subset of elts
of [s1] and of [s2]. The presence of each such element is
determined with the function [f]. *)
val compare: t -> t -> int
(** Total ordering between sets. *)
val equal: t -> t -> bool
(** [equal s1 s2] tests whether the sets [s1] and [s2] are equal. *)
val subset: t -> t -> bool
(** [subset s1 s2] tests whether the set [s1] is a subset of [s2]. *)
val iter: (elt -> unit) -> t -> unit
(** [iter f s] applies [f] to all elements of [s].
The elements are passed to [f] in increasing order with respect
to the ordering over the type of the elts. *)
val fold: (elt -> 'a -> 'a) -> t -> 'a -> 'a
(** [fold f s a] computes [(f eN ... (f e1 a)...)],
where [e1 ... eN] are the element of [s] in increasing order. *)
val for_all: (elt -> bool) -> t -> bool
(** [for_all p s] checks if all the elements of [s] satisfy
the predicate [p]. *)
val exists: (elt -> bool) -> t -> bool
(** [exists p s] checks if at least one element of [s] satisfies
the predicate [p]. *)
val filter: (elt -> bool) -> t -> t
(** [filter p s] returns the set with all the elements of [s]
that satisfy predicate [p]. *)
val partition: (elt -> bool) -> t -> t * t
(** [partition p s] returns a pair of sets [(s1, s2)], where
[s1] contains all the elements of [s] that satisfy the
predicate [p], and [s2] is the map with all the elements of
[s] that do not satisfy [p]. *)
val cardinal: t -> int
(** Return the number of elements in a set. *)
val elements: t -> elt list
(** Return the list of all elements of the given set.
The returned list is sorted in increasing order. *)
val min_elt: t -> elt
(** Return the smallest element of the given set or raise
[Not_found] if the set is empty. *)
val max_elt: t -> elt
(** Return the largest element of the given set or raise
[Not_found] if the set is empty. *)
val choose: t -> elt
(** Return one element of the given set, or raise [Not_found] if
the set is empty. Which element is chosen is unspecified,
but equal elements will be chosen for equal sets. *)
val split: elt -> t -> t * bool * t
(** [split x s] returns a triple [(l, mem, r)], where
[l] is the set with all the elements of [s] that are
strictly less than [x];
[r] is the set with all the elements of [s] that are
strictly greater than [x];
[mem] is [true] if [x] belongs to [s] and [false] otherwise. *)
val change : elt -> (bool -> bool) -> t -> t
(** [change x f s] returns a set containing the same elements as
[s], except [x] which is added to [s] if [f (mem x s)] returns
[true] and removed otherwise. *)
val union : t -> t -> t
(** [union f s1 s2] computes the union of two sets *)
val inter : t -> t -> t
(** [inter f s1 s2] computes the intersection of two sets *)
val diff : t -> t -> t
(** [diss f s1 s2] computes the difference of two sets *)
end with type elt = key and type t = unit t
end
(** Output signature of the functor {!Map.Make}. *)
module Make (Ord : OrderedType) : S with type key = Ord.t
(** Functor building an implementation of the map structure
module Make (Ord : OrderedType) : S with
type key = Ord.t and type S.elt = Ord.t
(** Functor building an implementation of the map/set structure
given a totally ordered type. *)
end
......@@ -158,8 +158,8 @@ end
module StructMake (X : Tagged) =
struct
module T = OrderedHash(X)
module S = Set.Make(T)
module M = Map.Make(T)
module S = M.S
module H = Hashtbl.Make(T)
end
......@@ -172,8 +172,8 @@ end
module WeakStructMake (X : Hashweak.Weakey) =
struct
module T = OrderedHash(MakeTagged(X))
module S = Set.Make(T)
module M = Map.Make(T)
module S = M.S
module H = Hashtbl.Make(T)
module W = Hashweak.Make(X)
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment