myMap.mli (5495B)
1 (* 2 This file is taken from the Objective Caml standard library. 3 Some functions have been added to suit Unison's needs. 4 *) 5 (***********************************************************************) 6 (* *) 7 (* Objective Caml *) 8 (* *) 9 (* Xavier Leroy, projet Cristal, INRIA Rocquencourt *) 10 (* *) 11 (* Copyright 1996 Institut National de Recherche en Informatique et *) 12 (* en Automatique. All rights reserved. This file is distributed *) 13 (* under the terms of the GNU Library General Public License, with *) 14 (* the special exception on linking described in file ../LICENSE. *) 15 (* *) 16 (***********************************************************************) 17 18 (** Association tables over ordered types. 19 20 This module implements applicative association tables, also known as 21 finite maps or dictionaries, given a total ordering function 22 over the keys. 23 All operations over maps are purely applicative (no side-effects). 24 The implementation uses balanced binary trees, and therefore searching 25 and insertion take time logarithmic in the size of the map. 26 *) 27 28 module type OrderedType = 29 sig 30 type t 31 (** The type of the map keys. *) 32 val m : t Umarshal.t 33 val compare : t -> t -> int 34 (** A total ordering function over the keys. 35 This is a two-argument function [f] such that 36 [f e1 e2] is zero if the keys [e1] and [e2] are equal, 37 [f e1 e2] is strictly negative if [e1] is smaller than [e2], 38 and [f e1 e2] is strictly positive if [e1] is greater than [e2]. 39 Example: a suitable ordering function is the generic structural 40 comparison function {!Pervasives.compare}. *) 41 end 42 (** Input signature of the functor {!Map.Make}. *) 43 44 module type S = 45 sig 46 type key 47 (** The type of the map keys. *) 48 49 type (+'a) t 50 (** The type of maps from type [key] to type ['a]. *) 51 52 val m : 'a Umarshal.t -> 'a t Umarshal.t 53 54 val empty: 'a t 55 (** The empty map. *) 56 57 val is_empty: 'a t -> bool 58 (** Test whether a map is empty or not. *) 59 60 val add: key -> 'a -> 'a t -> 'a t 61 (** [add x y m] returns a map containing the same bindings as 62 [m], plus a binding of [x] to [y]. If [x] was already bound 63 in [m], its previous binding disappears. *) 64 65 val find: key -> 'a t -> 'a 66 (** [find x m] returns the current binding of [x] in [m], 67 or raises [Not_found] if no such binding exists. *) 68 69 val findi: key -> 'a t -> key * 'a 70 (** [find x m] returns the current binding of [x] in [m], 71 or raises [Not_found] if no such binding exists. *) 72 73 val remove: key -> 'a t -> 'a t 74 (** [remove x m] returns a map containing the same bindings as 75 [m], except for [x] which is unbound in the returned map. *) 76 77 val mem: key -> 'a t -> bool 78 (** [mem x m] returns [true] if [m] contains a binding for [x], 79 and [false] otherwise. *) 80 81 val iter: (key -> 'a -> unit) -> 'a t -> unit 82 (** [iter f m] applies [f] to all bindings in map [m]. 83 [f] receives the key as first argument, and the associated value 84 as second argument. The bindings are passed to [f] in increasing 85 order with respect to the ordering over the type of the keys. 86 Only current bindings are presented to [f]: 87 bindings hidden by more recent bindings are not passed to [f]. *) 88 89 val map: ('a -> 'b) -> 'a t -> 'b t 90 (** [map f m] returns a map with same domain as [m], where the 91 associated value [a] of all bindings of [m] has been 92 replaced by the result of the application of [f] to [a]. 93 The bindings are passed to [f] in increasing order 94 with respect to the ordering over the type of the keys. *) 95 96 val mapi: (key -> 'a -> 'b) -> 'a t -> 'b t 97 (** Same as {!Map.S.map}, but the function receives as arguments both the 98 key and the associated value for each binding of the map. *) 99 100 val mapii: (key -> 'a -> key * 'b) -> 'a t -> 'b t 101 (** Same as {!Map.S.map}, but the function receives as arguments both the 102 key and the associated value for each binding of the map. *) 103 104 val fold: (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b 105 (** [fold f m a] computes [(f kN dN ... (f k1 d1 a)...)], 106 where [k1 ... kN] are the keys of all bindings in [m] 107 (in increasing order), and [d1 ... dN] are the associated data. *) 108 109 val compare: ('a -> 'a -> int) -> 'a t -> 'a t -> int 110 (** Total ordering between maps. The first argument is a total ordering 111 used to compare data associated with equal keys in the two maps. *) 112 113 val equal: ('a -> 'a -> bool) -> 'a t -> 'a t -> bool 114 (** [equal cmp m1 m2] tests whether the maps [m1] and [m2] are 115 equal, that is, contain equal keys and associate them with 116 equal data. [cmp] is the equality predicate used to compare 117 the data associated with the keys. *) 118 119 val validate: 'a t -> [`Ok | `Duplicate of key | `Invalid of key * key] 120 end 121 (** Output signature of the functor {!Map.Make}. *) 122 123 module Make (Ord : OrderedType) : S with type key = Ord.t 124 (** Functor building an implementation of the map structure 125 given a totally ordered type. *)