Commit 4c1eb31a authored by François Bobot's avatar François Bobot

map : add fold2_inter and fold2_union

parent f4a1e81b
......@@ -63,6 +63,9 @@ module type S =
val mapi_filter: (key -> 'a -> 'b option) -> 'a t -> 'b t
val mapi_fold:
(key -> 'a -> 'acc -> 'acc * 'b) -> 'a t -> 'acc -> 'acc * 'b t
val fold2_inter: (key -> 'a -> 'b -> 'c -> 'c) -> 'a t -> 'b t -> 'c -> 'c
val fold2_union: (key -> 'a option -> 'b option -> 'c -> 'c) ->
'a t -> 'b t -> 'c -> 'c
val translate : (key -> key) -> 'a t -> 'a t
val mapi_filter_fold:
(key -> 'a -> 'acc -> 'acc * 'b option) -> 'a t -> 'acc -> 'acc * 'b t
......@@ -101,6 +104,7 @@ module type S =
val union : t -> t -> t
val inter : t -> t -> t
val diff : t -> t -> t
val fold2: (elt -> 'a -> 'a) -> t -> t -> 'a -> 'a
val translate : (elt -> elt) -> t -> t
val add_new : elt -> exn -> t -> t
end
......@@ -506,6 +510,40 @@ module Make(Ord: OrderedType) = struct
let acc,r' = mapi_fold f r acc in
acc,Node(l', v, d', r', h)
let fold2_inter f m1 m2 acc =
let rec aux acc e1_0 e2_0 =
match (e1_0, e2_0) with
(End, End) -> acc
| (End, _) -> acc
| (_, End) -> acc
| (More(v1, d1, r1, e1), More(v2, d2, r2, e2)) ->
let c = Ord.compare v1 v2 in
if c = 0 then
aux (f v1 d1 d2 acc) (cons_enum r1 e1) (cons_enum r2 e2)
else if c < 0 then
aux acc (cons_enum r1 e1) e2_0
else
aux acc e1_0 (cons_enum r2 e2)
in aux acc (cons_enum m1 End) (cons_enum m2 End)
let fold2_union f m1 m2 acc =
let rec aux acc e1_0 e2_0 =
match (e1_0, e2_0) with
(End, End) -> acc
| (End, More(v2, d2, r2, e2)) ->
aux (f v2 None (Some d2) acc) End (cons_enum r2 e2)
| (More(v1, d1, r1, e1), End) ->
aux (f v1 (Some d1) None acc) (cons_enum r1 e1) End
| (More(v1, d1, r1, e1), More(v2, d2, r2, e2)) ->
let c = Ord.compare v1 v2 in
if c = 0 then
aux (f v1 (Some d1) (Some d2) acc)
(cons_enum r1 e1) (cons_enum r2 e2)
else if c < 0 then
aux (f v1 (Some d1) None acc) (cons_enum r1 e1) e2_0
else
aux (f v2 None (Some d2) acc) e1_0 (cons_enum r2 e2)
in aux acc (cons_enum m1 End) (cons_enum m2 End)
let translate f m =
let rec aux last = function
......@@ -567,6 +605,7 @@ module Make(Ord: OrderedType) = struct
val union : t -> t -> t
val inter : t -> t -> t
val diff : t -> t -> t
val fold2: (elt -> 'a -> 'a) -> t -> t -> 'a -> 'a
val translate : (elt -> elt) -> t -> t
val add_new : elt -> exn -> t -> t
end
......@@ -608,6 +647,7 @@ module Make(Ord: OrderedType) = struct
let union = union (fun _ _ _ -> Some ())
let inter = inter (fun _ _ _ -> Some ())
let diff = diff (fun _ _ _ -> None)
let fold2 f = fold2_union (fun k _ _ acc -> f k acc)
let translate = translate
let add_new x = add_new x ()
end
......
......@@ -226,6 +226,13 @@ module type S =
(key -> 'a -> 'acc -> 'acc * 'b) -> 'a t -> 'acc -> 'acc * 'b t
(** fold and map at the same time *)
val fold2_inter: (key -> 'a -> 'b -> 'c -> 'c) -> 'a t -> 'b t -> 'c -> 'c
(** fold the common keys of two map at the same time *)
val fold2_union: (key -> 'a option -> 'b option -> 'c -> 'c)
-> 'a t -> 'b t -> 'c -> 'c
(** fold the keys which appear in one of the two map at the same time *)
val translate : (key -> key) -> 'a t -> 'a t
(** [translate f m] translates the keys in the map [m] by the
function [f]. [f] must be strictly monotone on the key of [m].
......@@ -363,6 +370,11 @@ module type S =
val diff : t -> t -> t
(** [diss f s1 s2] computes the difference of two sets *)
val fold2: (elt -> 'a -> 'a) -> t -> t -> 'a -> 'a
(** [fold f s1 s2 a] computes [(f eN ... (f e1 a)...)],
where [e1 ... eN] are the element of [s1] and [s2]
in increasing order. *)
val translate : (elt -> elt) -> t -> t
(** [translate f s] translates the elements in the set [s] by the
function [f]. [f] must be strictly monotone on the elements of [s].
......
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