1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
2001-12-07 05:41:02 -08:00
|
|
|
(* under the terms of the GNU Library General Public License, with *)
|
|
|
|
(* the special exception on linking described in file ../LICENSE. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
2001-10-26 15:38:48 -07:00
|
|
|
(** Association tables over ordered types.
|
1995-08-09 06:15:01 -07:00
|
|
|
|
2001-10-26 15:38:48 -07:00
|
|
|
This module implements applicative association tables, also known as
|
1995-08-09 06:15:01 -07:00
|
|
|
finite maps or dictionaries, given a total ordering function
|
|
|
|
over the keys.
|
|
|
|
All operations over maps are purely applicative (no side-effects).
|
|
|
|
The implementation uses balanced binary trees, and therefore searching
|
2005-10-25 11:34:07 -07:00
|
|
|
and insertion take time logarithmic in the size of the map.
|
2001-10-26 15:38:48 -07:00
|
|
|
*)
|
1995-05-30 06:33:57 -07:00
|
|
|
|
2005-10-25 11:34:07 -07:00
|
|
|
module type OrderedType =
|
2001-12-10 04:33:55 -08:00
|
|
|
sig
|
|
|
|
type t
|
|
|
|
(** The type of the map keys. *)
|
|
|
|
val compare : t -> t -> int
|
|
|
|
(** A total ordering function over the keys.
|
|
|
|
This is a two-argument function [f] such that
|
|
|
|
[f e1 e2] is zero if the keys [e1] and [e2] are equal,
|
|
|
|
[f e1 e2] is strictly negative if [e1] is smaller than [e2],
|
|
|
|
and [f e1 e2] is strictly positive if [e1] is greater than [e2].
|
2003-10-16 06:05:34 -07:00
|
|
|
Example: a suitable ordering function is the generic structural
|
2003-11-21 08:06:08 -08:00
|
|
|
comparison function {!Pervasives.compare}. *)
|
2001-12-10 04:33:55 -08:00
|
|
|
end
|
|
|
|
(** Input signature of the functor {!Map.Make}. *)
|
1995-05-30 06:33:57 -07:00
|
|
|
|
|
|
|
module type S =
|
|
|
|
sig
|
|
|
|
type key
|
2001-12-04 07:42:07 -08:00
|
|
|
(** The type of the map keys. *)
|
|
|
|
|
|
|
|
type (+'a) t
|
|
|
|
(** The type of maps from type [key] to type ['a]. *)
|
|
|
|
|
|
|
|
val empty: 'a t
|
|
|
|
(** The empty map. *)
|
|
|
|
|
2004-04-23 03:01:34 -07:00
|
|
|
val is_empty: 'a t -> bool
|
|
|
|
(** Test whether a map is empty or not. *)
|
|
|
|
|
2010-05-25 06:29:43 -07:00
|
|
|
val mem: key -> 'a t -> bool
|
|
|
|
(** [mem x m] returns [true] if [m] contains a binding for [x],
|
|
|
|
and [false] otherwise. *)
|
|
|
|
|
2001-12-04 07:42:07 -08:00
|
|
|
val add: key -> 'a -> 'a t -> 'a t
|
|
|
|
(** [add x y m] returns a map containing the same bindings as
|
|
|
|
[m], plus a binding of [x] to [y]. If [x] was already bound
|
|
|
|
in [m], its previous binding disappears. *)
|
|
|
|
|
2010-05-25 06:29:43 -07:00
|
|
|
val singleton: key -> 'a -> 'a t
|
2010-05-31 05:48:13 -07:00
|
|
|
(** [singleton x y] returns the one-element map that contains a binding [y]
|
|
|
|
for [x].
|
2010-05-31 05:28:03 -07:00
|
|
|
@since 3.12.0
|
|
|
|
*)
|
2001-12-04 07:42:07 -08:00
|
|
|
|
|
|
|
val remove: key -> 'a t -> 'a t
|
|
|
|
(** [remove x m] returns a map containing the same bindings as
|
|
|
|
[m], except for [x] which is unbound in the returned map. *)
|
|
|
|
|
2010-05-31 05:48:13 -07:00
|
|
|
val merge:
|
|
|
|
(key -> 'a option -> 'b option -> 'c option) -> 'a t -> 'b t -> 'c t
|
|
|
|
(** [merge f m1 m2] computes a map whose keys is a subset of keys of [m1]
|
|
|
|
and of [m2]. The presence of each such binding, and the corresponding
|
|
|
|
value, is determined with the function [f].
|
2010-05-31 05:28:03 -07:00
|
|
|
@since 3.12.0
|
|
|
|
*)
|
2010-05-25 06:29:43 -07:00
|
|
|
|
|
|
|
val compare: ('a -> 'a -> int) -> 'a t -> 'a t -> int
|
|
|
|
(** Total ordering between maps. The first argument is a total ordering
|
|
|
|
used to compare data associated with equal keys in the two maps. *)
|
|
|
|
|
|
|
|
val equal: ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
|
|
|
|
(** [equal cmp m1 m2] tests whether the maps [m1] and [m2] are
|
|
|
|
equal, that is, contain equal keys and associate them with
|
|
|
|
equal data. [cmp] is the equality predicate used to compare
|
|
|
|
the data associated with the keys. *)
|
2001-12-04 07:42:07 -08:00
|
|
|
|
|
|
|
val iter: (key -> 'a -> unit) -> 'a t -> unit
|
|
|
|
(** [iter f m] applies [f] to all bindings in map [m].
|
|
|
|
[f] receives the key as first argument, and the associated value
|
2004-02-14 08:52:22 -08:00
|
|
|
as second argument. The bindings are passed to [f] in increasing
|
2009-05-20 04:52:42 -07:00
|
|
|
order with respect to the ordering over the type of the keys. *)
|
2001-12-04 07:42:07 -08:00
|
|
|
|
2010-05-25 06:29:43 -07:00
|
|
|
val fold: (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
|
|
|
|
(** [fold f m a] computes [(f kN dN ... (f k1 d1 a)...)],
|
|
|
|
where [k1 ... kN] are the keys of all bindings in [m]
|
|
|
|
(in increasing order), and [d1 ... dN] are the associated data. *)
|
|
|
|
|
|
|
|
val for_all: (key -> 'a -> bool) -> 'a t -> bool
|
|
|
|
(** [for_all p m] checks if all the bindings of the map
|
2010-05-31 05:28:03 -07:00
|
|
|
satisfy the predicate [p].
|
|
|
|
@since 3.12.0
|
|
|
|
*)
|
2010-05-25 06:29:43 -07:00
|
|
|
|
|
|
|
val exists: (key -> 'a -> bool) -> 'a t -> bool
|
|
|
|
(** [exists p m] checks if at least one binding of the map
|
2010-05-31 05:28:03 -07:00
|
|
|
satisfy the predicate [p].
|
|
|
|
@since 3.12.0
|
|
|
|
*)
|
2010-05-25 06:29:43 -07:00
|
|
|
|
|
|
|
val filter: (key -> 'a -> bool) -> 'a t -> 'a t
|
|
|
|
(** [filter p m] returns the map with all the bindings in [m]
|
2010-05-31 05:28:03 -07:00
|
|
|
that satisfy predicate [p].
|
|
|
|
@since 3.12.0
|
|
|
|
*)
|
2010-05-25 06:29:43 -07:00
|
|
|
|
|
|
|
val partition: (key -> 'a -> bool) -> 'a t -> 'a t * 'a t
|
|
|
|
(** [partition p m] returns a pair of maps [(m1, m2)], where
|
2010-05-31 05:28:03 -07:00
|
|
|
[m1] contains all the bindings of [s] that satisfy the
|
|
|
|
predicate [p], and [m2] is the map with all the bindings of
|
|
|
|
[s] that do not satisfy [p].
|
|
|
|
@since 3.12.0
|
|
|
|
*)
|
2010-05-25 06:29:43 -07:00
|
|
|
|
|
|
|
val cardinal: 'a t -> int
|
2010-05-31 05:28:03 -07:00
|
|
|
(** Return the number of bindings of a map.
|
|
|
|
@since 3.12.0
|
|
|
|
*)
|
2010-05-25 06:29:43 -07:00
|
|
|
|
|
|
|
val bindings: 'a t -> (key * 'a) list
|
|
|
|
(** Return the list of all bindings of the given map.
|
|
|
|
The returned list is sorted in increasing order with respect
|
|
|
|
to the ordering [Ord.compare], where [Ord] is the argument
|
2010-05-31 05:28:03 -07:00
|
|
|
given to {!Map.Make}.
|
|
|
|
@since 3.12.0
|
|
|
|
*)
|
2010-05-25 06:29:43 -07:00
|
|
|
|
|
|
|
val min_binding: 'a t -> (key * 'a)
|
|
|
|
(** Return the smallest binding of the given map
|
|
|
|
(with respect to the [Ord.compare] ordering), or raise
|
2010-05-31 05:28:03 -07:00
|
|
|
[Not_found] if the map is empty.
|
|
|
|
@since 3.12.0
|
|
|
|
*)
|
2010-05-25 06:29:43 -07:00
|
|
|
|
|
|
|
val max_binding: 'a t -> (key * 'a)
|
2010-08-02 07:37:22 -07:00
|
|
|
(** Same as {!Map.S.min_binding}, but returns the largest binding
|
2010-05-31 05:28:03 -07:00
|
|
|
of the given map.
|
|
|
|
@since 3.12.0
|
|
|
|
*)
|
2010-05-25 06:29:43 -07:00
|
|
|
|
|
|
|
val choose: 'a t -> (key * 'a)
|
|
|
|
(** Return one binding of the given map, or raise [Not_found] if
|
|
|
|
the map is empty. Which binding is chosen is unspecified,
|
2010-05-31 05:28:03 -07:00
|
|
|
but equal bindings will be chosen for equal maps.
|
|
|
|
@since 3.12.0
|
|
|
|
*)
|
2010-05-25 06:29:43 -07:00
|
|
|
|
|
|
|
val split: key -> 'a t -> 'a t * 'a option * 'a t
|
|
|
|
(** [split x m] returns a triple [(l, data, r)], where
|
|
|
|
[l] is the map with all the bindings of [m] whose key
|
|
|
|
is strictly less than [x];
|
|
|
|
[r] is the map with all the bindings of [m] whose key
|
|
|
|
is strictly greater than [x];
|
|
|
|
[data] is [None] if [m] contains no binding for [x],
|
2010-05-31 05:28:03 -07:00
|
|
|
or [Some v] if [m] binds [v] to [x].
|
|
|
|
@since 3.12.0
|
|
|
|
*)
|
2010-05-25 06:29:43 -07:00
|
|
|
|
|
|
|
val find: key -> 'a t -> 'a
|
|
|
|
(** [find x m] returns the current binding of [x] in [m],
|
|
|
|
or raises [Not_found] if no such binding exists. *)
|
|
|
|
|
2001-12-04 07:42:07 -08:00
|
|
|
val map: ('a -> 'b) -> 'a t -> 'b t
|
|
|
|
(** [map f m] returns a map with same domain as [m], where the
|
|
|
|
associated value [a] of all bindings of [m] has been
|
|
|
|
replaced by the result of the application of [f] to [a].
|
2004-02-14 08:52:22 -08:00
|
|
|
The bindings are passed to [f] in increasing order
|
|
|
|
with respect to the ordering over the type of the keys. *)
|
2001-12-04 07:42:07 -08:00
|
|
|
|
|
|
|
val mapi: (key -> 'a -> 'b) -> 'a t -> 'b t
|
|
|
|
(** Same as {!Map.S.map}, but the function receives as arguments both the
|
|
|
|
key and the associated value for each binding of the map. *)
|
|
|
|
|
2004-04-23 03:01:34 -07:00
|
|
|
|
1995-05-30 06:33:57 -07:00
|
|
|
end
|
2001-12-10 04:33:55 -08:00
|
|
|
(** Output signature of the functor {!Map.Make}. *)
|
1995-05-30 06:33:57 -07:00
|
|
|
|
2001-12-03 14:16:03 -08:00
|
|
|
module Make (Ord : OrderedType) : S with type key = Ord.t
|
2001-10-26 15:38:48 -07:00
|
|
|
(** Functor building an implementation of the map structure
|
|
|
|
given a totally ordered type. *)
|