1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
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
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
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
|
2001-10-26 15:38:48 -07:00
|
|
|
and insertion take time logarithmic in the size of the map.
|
|
|
|
*)
|
1995-05-30 06:33:57 -07:00
|
|
|
|
2001-12-10 04:33:55 -08:00
|
|
|
module type OrderedType =
|
|
|
|
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. *)
|
|
|
|
|
|
|
|
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. *)
|
|
|
|
|
|
|
|
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. *)
|
|
|
|
|
|
|
|
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. *)
|
|
|
|
|
|
|
|
val mem: key -> 'a t -> bool
|
|
|
|
(** [mem x m] returns [true] if [m] contains a binding for [x],
|
|
|
|
and [false] otherwise. *)
|
|
|
|
|
|
|
|
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
|
|
|
|
order with respect to the ordering over the type of the keys.
|
|
|
|
Only current bindings are presented to [f]:
|
2001-12-04 07:42:07 -08:00
|
|
|
bindings hidden by more recent bindings are not passed to [f]. *)
|
|
|
|
|
|
|
|
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. *)
|
|
|
|
|
|
|
|
val fold: (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
|
|
|
|
(** [fold f m a] computes [(f kN dN ... (f k1 d1 a)...)],
|
2004-02-14 08:52:22 -08:00
|
|
|
where [k1 ... kN] are the keys of all bindings in [m]
|
|
|
|
(in increasing order), and [d1 ... dN] are the associated data. *)
|
|
|
|
|
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. *)
|
|
|
|
|