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 *)
|
|
|
|
(* under the terms of the GNU Library General Public License. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-08-09 06:15:01 -07:00
|
|
|
(* Module [Map]: association tables over ordered types *)
|
|
|
|
|
|
|
|
(* This module implements applicative association tables, also known as
|
|
|
|
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
|
|
|
|
and insertion take time logarithmic in the size of the map. *)
|
1995-05-30 06:33:57 -07:00
|
|
|
|
|
|
|
module type OrderedType =
|
|
|
|
sig
|
|
|
|
type t
|
|
|
|
val compare: t -> t -> int
|
|
|
|
end
|
1995-08-09 06:15:01 -07:00
|
|
|
(* The input signature of the functor [Map.Make].
|
|
|
|
[t] is the type of the map keys.
|
|
|
|
[compare] is 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].
|
1997-12-09 01:11:48 -08:00
|
|
|
Example: a suitable ordering function is
|
|
|
|
the generic structural comparison function [compare]. *)
|
1995-05-30 06:33:57 -07:00
|
|
|
|
|
|
|
module type S =
|
|
|
|
sig
|
|
|
|
type key
|
1995-08-09 06:15:01 -07:00
|
|
|
(* The type of the map keys. *)
|
2001-02-12 15:46:18 -08:00
|
|
|
type (+'a) t
|
1995-08-09 06:15:01 -07:00
|
|
|
(* The type of maps from type [key] to type ['a]. *)
|
1995-05-30 06:33:57 -07:00
|
|
|
val empty: 'a t
|
1995-08-09 06:15:01 -07:00
|
|
|
(* The empty map. *)
|
1999-11-30 08:07:38 -08:00
|
|
|
val add: key:key -> data:'a -> 'a t -> 'a t
|
1995-08-09 06:15:01 -07:00
|
|
|
(* [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. *)
|
2000-04-02 18:57:52 -07:00
|
|
|
val find: key -> 'a t -> 'a
|
1995-08-09 06:15:01 -07:00
|
|
|
(* [find x m] returns the current binding of [x] in [m],
|
|
|
|
or raises [Not_found] if no such binding exists. *)
|
2000-04-02 18:57:52 -07:00
|
|
|
val remove: key -> 'a t -> 'a t
|
1996-04-04 07:57:00 -08:00
|
|
|
(* [remove x m] returns a map containing the same bindings as
|
|
|
|
[m], except for [x] which is unbound in the returned map. *)
|
2001-02-05 00:54:28 -08:00
|
|
|
val mem: key -> 'a t -> bool
|
1999-02-25 02:26:38 -08:00
|
|
|
(* [mem x m] returns [true] if [m] contains a binding for [m],
|
|
|
|
and [false] otherwise. *)
|
2000-04-02 18:57:52 -07:00
|
|
|
val iter: f:(key:key -> data:'a -> unit) -> 'a t -> unit
|
1997-10-31 04:59:29 -08:00
|
|
|
(* [iter f m] applies [f] to all bindings in map [m].
|
1995-08-09 06:15:01 -07:00
|
|
|
[f] receives the key as first argument, and the associated value
|
|
|
|
as second argument. The order in which the bindings are passed to
|
|
|
|
[f] is unspecified. Only current bindings are presented to [f]:
|
|
|
|
bindings hidden by more recent bindings are not passed to [f]. *)
|
2000-04-02 18:57:52 -07:00
|
|
|
val map: f:('a -> 'b) -> 'a t -> 'b t
|
1997-05-10 08:38:44 -07:00
|
|
|
(* [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].
|
|
|
|
The order in which the associated values are passed to [f]
|
|
|
|
is unspecified. *)
|
2000-04-02 18:57:52 -07:00
|
|
|
val mapi: f:(key -> 'a -> 'b) -> 'a t -> 'b t
|
2000-02-21 10:12:16 -08:00
|
|
|
(* Same as [map], but the function receives as arguments both the
|
|
|
|
key and the associated value for each binding of the map. *)
|
2000-04-02 18:57:52 -07:00
|
|
|
val fold: f:(key:key -> data:'a -> 'b -> 'b) -> 'a t -> init:'b -> 'b
|
1995-08-09 06:15:01 -07:00
|
|
|
(* [fold f m a] computes [(f kN dN ... (f k1 d1 a)...)],
|
|
|
|
where [k1 ... kN] are the keys of all bindings in [m],
|
|
|
|
and [d1 ... dN] are the associated data.
|
|
|
|
The order in which the bindings are presented to [f] is
|
1997-10-31 04:59:29 -08:00
|
|
|
unspecified. *)
|
1995-05-30 06:33:57 -07:00
|
|
|
end
|
|
|
|
|
1995-10-01 06:39:43 -07:00
|
|
|
module Make(Ord: OrderedType): (S with type key = Ord.t)
|
1995-08-09 06:15:01 -07:00
|
|
|
(* Functor building an implementation of the map structure
|
|
|
|
given a totally ordered type. *)
|