File: mapext.ml

package info (click to toggle)
xen-api-libs 0.5.2-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,940 kB
  • sloc: ml: 13,925; sh: 2,930; ansic: 1,699; makefile: 1,240; python: 83
file content (47 lines) | stat: -rw-r--r-- 1,428 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

module type S =
  sig
    type key
    type +'a t
    val empty: 'a t
    val is_empty: 'a t -> bool
    val add: key -> 'a -> 'a t -> 'a t
    val find: key -> 'a t -> 'a
    val remove: key -> 'a t -> 'a t
    val mem:  key -> 'a t -> bool
    val iter: (key -> 'a -> unit) -> 'a t -> unit
    val map: ('a -> 'b) -> 'a t -> 'b t
    val mapi: (key -> 'a -> 'b) -> 'a t -> 'b t
    val fold: (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
    val compare: ('a -> 'a -> int) -> 'a t -> 'a t -> int
    val equal: ('a -> 'a -> bool) -> 'a t -> 'a t -> bool

    val fromHash : (key, 'a) Hashtbl.t -> 'a t

    val filter : ('a -> bool) -> 'a t -> 'a t

    (* values: gives the list of values of the map. *)
    val values : 'a t -> 'a list

    val fromListWith : ('a -> 'a -> 'a) -> (key * 'a) list -> 'a t
    val adjust : ('a -> 'a) -> key -> 'a t -> 'a t

  end

module Make(Ord: Map.OrderedType) = struct
    include Map.Make (Ord)
	
    let fromHash h = Hashtbl.fold add h empty
    let filter pred m = fold (fun k v acc -> (if pred v then add k v else Fun.id) acc) m empty
	(* values: gives the list of values of the map. *)
    let values m = fold (Fun.const Listext.List.cons) m []
	
    let fromListWith op list = List.fold_left (fun map (k,v) ->
						 add k (if mem k map
							then op v (find k map)
							else v) map)
	empty list
    let adjust op k m = try add k (op (find k m)) m with Not_found -> m
	
    
end