Permalink
Browse files

[feature] Map: Adding the function choose

I needed "choose" in my map, hence I added it, as in set.
  • Loading branch information...
1 parent 5b18026 commit 708e8656f74b1504b4b9fa9a5faa5aa8b48772e4 Arthur Milchior committed with Mathieu Barbin Jun 28, 2011
Showing with 17 additions and 0 deletions.
  1. +4 −0 libbase/baseMap.ml
  2. +6 −0 libbase/baseMapSig.ml
  3. +7 −0 libbase/intMap.ml
View
@@ -238,6 +238,10 @@ struct
| Node (l, _, _, _, _) -> aux l
in aux t
+ let choose = function
+ | Empty -> raise Not_found
+ | Node (_,v,d,_,_) -> v,d
+
let max t =
let rec aux = function
| Empty -> raise Not_found
View
@@ -74,6 +74,12 @@ sig
val mem : key -> 'a t -> bool
val iter : (key -> 'a -> unit) -> 'a t -> unit
val rev_iter : (key -> 'a -> unit) -> 'a t -> unit
+
+ (**
+ Returns an arbitrary element of the map.
+ @raise Not_found on the empty list
+ *)
+ val choose : 'a t -> (key * 'a)
val map : ('a -> 'b) -> 'a t -> 'b t
val mapi : (key -> 'a -> 'b) -> 'a t -> 'b t
val fold_map : (key -> 'a -> 'c -> 'c * 'b) -> 'a t -> 'c -> 'c * 'b t
View
@@ -331,6 +331,13 @@ let rec min t = match t with
| Empty -> raise Not_found
let min t = ordertop min t
+(*
+ Choose is min, because they are no value except on leaf
+ hence I must go to a leaf to find something. And min already
+ find a leaf
+*)
+let choose = min
+
let rec max t = match t with
| Br (_,_m,_tl,tr) -> max tr
| Lf (k,v) -> k,v

0 comments on commit 708e865

Please sign in to comment.