Commit ac0c4f10 authored by François Bobot's avatar François Bobot

stdlib : add find_option and translate in maps

parent 09cd77de
......@@ -58,8 +58,11 @@ module type S =
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 find_option : key -> 'a t -> 'a option
val mapi_fold:
(key -> 'a -> 'acc -> 'acc * 'b) -> 'a t -> 'acc -> 'acc * 'b t
val translate : (key -> key) -> 'a t -> 'a t
module type Set =
sig
type elt = key
......@@ -91,6 +94,7 @@ module type S =
val union : t -> t -> t
val inter : t -> t -> t
val diff : t -> t -> t
val translate : (elt -> elt) -> t -> t
end
module Set : Set
......@@ -459,6 +463,13 @@ module Make(Ord: OrderedType) = struct
if c = 0 then d
else find_default x def (if c < 0 then l else r)
let rec find_option x = function
Empty -> None
| Node(l, v, d, r, _) ->
let c = Ord.compare x v in
if c = 0 then Some d
else find_option x (if c < 0 then l else r)
let rec mapi_fold f m acc =
match m with
......@@ -469,6 +480,22 @@ module Make(Ord: OrderedType) = struct
let acc,r' = mapi_fold f r acc in
acc,Node(l', v, d', r', h)
let translate f m =
let rec aux last = function
| Empty -> Empty,last
| Node(l, v, d, r, h) ->
let l,last = aux last l in
let v = f v in
begin match last with
| None -> ()
| Some last ->
if Ord.compare last v >= 0
then invalid_arg "Map.translate : given function incorrect"
end;
let r,last = aux (Some v) r in
Node(l,v,d,r,h),last in
let m,_ = aux None m in m
module type Set =
sig
type elt = key
......@@ -500,6 +527,7 @@ module Make(Ord: OrderedType) = struct
val union : t -> t -> t
val inter : t -> t -> t
val diff : t -> t -> t
val translate : (elt -> elt) -> t -> t
end
module Set =
......@@ -539,6 +567,7 @@ module Make(Ord: OrderedType) = struct
let union = union (fun _ _ _ -> Some ())
let inter = inter (fun _ _ _ -> Some ())
let diff = diff (fun _ _ _ -> None)
let translate = translate
end
end
......
......@@ -226,10 +226,19 @@ module type S =
(** [find_default x d m] returns the current binding of [x] in [m],
or return [d] if no such binding exists. *)
val find_option : key -> 'a t -> 'a option
(** [find_default x d m] returns the [Some] of the current binding
of [x] in [m], or return [None] if no such binding exists. *)
val mapi_fold:
(key -> 'a -> 'acc -> 'acc * 'b)-> 'a t -> 'acc -> 'acc * 'b t
(** fold and 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].
Otherwise it raises invalid_arg *)
module type Set =
sig
type elt = key
......@@ -341,6 +350,11 @@ module type S =
val diff : t -> t -> t
(** [diss f s1 s2] computes the difference of two sets *)
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].
Otherwise it raises invalid_arg *)
end
module Set : Set
......
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