Debian Codegraph @

stdlib/templates/map.template.mli

raw symbols SCIP
1 (**************************************************************************)
2 (* *)
3 (* OCaml *)
4 (* *)
5 (* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
6 (* *)
7 (* Copyright 1996 Institut National de Recherche en Informatique et *)
8 (* en Automatique. *)
9 (* *)
10 (* All rights reserved. This file is distributed under the terms of *)
11 (* the GNU Lesser General Public License version 2.1, with the *)
12 (* special exception on linking described in the file LICENSE. *)
13 (* *)
14 (**************************************************************************)
15
16 (* NOTE: If this file is map.mli, do not edit it directly! Instead,
17 edit templates/map.template.mli and run tools/sync_stdlib_docs *)
18
19 (** Association tables over ordered types.
20
21 This module implements applicative association tables, also known as
22 finite maps or dictionaries, given a total ordering function
23 over the keys.
24 All operations over maps are purely applicative (no side-effects).
25 The implementation uses balanced binary trees, and therefore searching
26 and insertion take time logarithmic in the size of the map.
27
28 For instance:
29 {[
30 module IntPairs =
31 struct
32 type t = int * int
33 let compare (x0,y0) (x1,y1) =
34 match Stdlib.compare x0 x1 with
35 0 -> Stdlib.compare y0 y1
36 | c -> c
37 end
38
39 module PairsMap = Map.Make(IntPairs)
40
41 let m = PairsMap.(empty |> add (0,1) "hello" |> add (1,0) "world")
42 ]}
43
44 This creates a new module [PairsMap], with a new type ['a PairsMap.t]
45 of maps from [int * int] to ['a]. In this example, [m] contains [string]
46 values so its type is [string PairsMap.t].
47 *)
48
49 module type OrderedType =
50 sig
51 type t
52 (** The type of the map keys. *)
53
54 val compare : t -> t -> int
55 (** A total ordering function over the keys.
56 This is a two-argument function [f] such that
57 [f e1 e2] is zero if the keys [e1] and [e2] are equal,
58 [f e1 e2] is strictly negative if [e1] is smaller than [e2],
59 and [f e1 e2] is strictly positive if [e1] is greater than [e2].
60 Example: a suitable ordering function is the generic structural
61 comparison function {!Stdlib.compare}. *)
62 end
63 (** Input signature of the functor {!Make}. *)
64
65 module type S =
66 sig
67
68 (** {1:maps Maps} *)
69
70 type key
71 (** The type of the map keys. *)
72
73 type !+'a t
74 (** The type of maps from type [key] to type ['a]. *)
75
76 val empty: 'a t
77 (** The empty map. *)
78
79 val add: key:key -> data:'a -> 'a t -> 'a t
80 (** [add ~key ~data m] returns a map containing the same bindings as
81 [m], plus a binding of [key] to [data]. If [key] was already bound
82 in [m] to a value that is physically equal to [data],
83 [m] is returned unchanged (the result of the function is
84 then physically equal to [m]). Otherwise, the previous binding
85 of [key] in [m] disappears.
86 @before 4.03 Physical equality was not ensured. *)
87
88 val add_to_list: key:key -> data:'a -> 'a list t -> 'a list t
89 (** [add_to_list ~key ~data m] is [m] with [key] mapped to [l] such
90 that [l] is [data :: Map.find key m] if [key] was bound in
91 [m] and [[data]] otherwise.
92 @since 5.1 *)
93
94 val update: key:key -> f:('a option -> 'a option) -> 'a t -> 'a t
95 (** [update ~key ~f m] returns a map containing the same bindings as
96 [m], except for the binding of [key]. Depending on the value of
97 [y] where [y] is [f (find_opt key m)], the binding of [key] is
98 added, removed or updated. If [y] is [None], the binding is
99 removed if it exists; otherwise, if [y] is [Some z] then [key]
100 is associated to [z] in the resulting map. If [key] was already
101 bound in [m] to a value that is physically equal to [z], [m]
102 is returned unchanged (the result of the function is then
103 physically equal to [m]).
104 @since 4.06 *)
105
106 val singleton: key -> 'a -> 'a t
107 (** [singleton x y] returns the one-element map that contains a binding
108 [y] for [x].
109 @since 3.12 *)
110
111 val remove: key -> 'a t -> 'a t
112 (** [remove x m] returns a map containing the same bindings as
113 [m], except for [x] which is unbound in the returned map.
114 If [x] was not in [m], [m] is returned unchanged
115 (the result of the function is then physically equal to [m]).
116 @before 4.03 Physical equality was not ensured. *)
117
118 val merge:
119 f:(key -> 'a option -> 'b option -> 'c option) ->
120 'a t -> 'b t -> 'c t
121 (** [merge ~f m1 m2] computes a map whose keys are a subset of the keys of
122 [m1] and of [m2]. The presence of each such binding, and the
123 corresponding value, is determined with the function [f].
124 In terms of the [find_opt] operation, we have
125 [find_opt x (merge f m1 m2) = f x (find_opt x m1) (find_opt x m2)]
126 for any key [x], provided that [f x None None = None].
127 @since 3.12 *)
128
129 val union: f:(key -> 'a -> 'a -> 'a option) -> 'a t -> 'a t -> 'a t
130 (** [union ~f m1 m2] computes a map whose keys are a subset of the keys
131 of [m1] and of [m2]. When the same binding is defined in both
132 arguments, the function [f] is used to combine them.
133 This is a special case of [merge]: [union f m1 m2] is equivalent
134 to [merge f' m1 m2], where
135 - [f' _key None None = None]
136 - [f' _key (Some v) None = Some v]
137 - [f' _key None (Some v) = Some v]
138 - [f' key (Some v1) (Some v2) = f key v1 v2]
139
140 @since 4.03 *)
141
142 val cardinal: 'a t -> int
143 (** Return the number of bindings of a map.
144 @since 3.12 *)
145
146 (** {1:bindings Bindings} *)
147
148 val bindings: 'a t -> (key * 'a) list
149 (** Return the list of all bindings of the given map.
150 The returned list is sorted in increasing order of keys with respect
151 to the ordering [Ord.compare], where [Ord] is the argument
152 given to {!Map.Make}.
153 @since 3.12 *)
154
155 val min_binding: 'a t -> (key * 'a)
156 (** Return the binding with the smallest key in a given map
157 (with respect to the [Ord.compare] ordering), or raise
158 [Not_found] if the map is empty.
159 @since 3.12 *)
160
161 val min_binding_opt: 'a t -> (key * 'a) option
162 (** Return the binding with the smallest key in the given map
163 (with respect to the [Ord.compare] ordering), or [None]
164 if the map is empty.
165 @since 4.05 *)
166
167 val max_binding: 'a t -> (key * 'a)
168 (** Same as {!min_binding}, but returns the binding with
169 the largest key in the given map.
170 @since 3.12 *)
171
172 val max_binding_opt: 'a t -> (key * 'a) option
173 (** Same as {!min_binding_opt}, but returns the binding with
174 the largest key in the given map.
175 @since 4.05 *)
176
177 val choose: 'a t -> (key * 'a)
178 (** Return one binding of the given map, or raise [Not_found] if
179 the map is empty. Which binding is chosen is unspecified,
180 but equal bindings will be chosen for equal maps.
181 @since 3.12 *)
182
183 val choose_opt: 'a t -> (key * 'a) option
184 (** Return one binding of the given map, or [None] if
185 the map is empty. Which binding is chosen is unspecified,
186 but equal bindings will be chosen for equal maps.
187 @since 4.05 *)
188
189 (** {1:searching Searching} *)
190
191 val find: key -> 'a t -> 'a
192 (** [find x m] returns the current value of [x] in [m],
193 or raises [Not_found] if no binding for [x] exists. *)
194
195 val find_opt: key -> 'a t -> 'a option
196 (** [find_opt x m] returns [Some v] if the current value of [x]
197 in [m] is [v], or [None] if no binding for [x] exists.
198 @since 4.05 *)
199
200 val find_first: f:(key -> bool) -> 'a t -> key * 'a
201 (** [find_first ~f m], where [f] is a monotonically increasing function,
202 returns the binding of [m] with the lowest key [k] such that [f k],
203 or raises [Not_found] if no such key exists.
204
205 For example, [find_first (fun k -> Ord.compare k x >= 0) m] will
206 return the first binding [k, v] of [m] where [Ord.compare k x >= 0]
207 (intuitively: [k >= x]), or raise [Not_found] if [x] is greater than
208 any element of [m].
209
210 @since 4.05 *)
211
212 val find_first_opt: f:(key -> bool) -> 'a t -> (key * 'a) option
213 (** [find_first_opt ~f m], where [f] is a monotonically increasing
214 function, returns an option containing the binding of [m] with the
215 lowest key [k] such that [f k], or [None] if no such key exists.
216 @since 4.05 *)
217
218 val find_last: f:(key -> bool) -> 'a t -> key * 'a
219 (** [find_last ~f m], where [f] is a monotonically decreasing function,
220 returns the binding of [m] with the highest key [k] such that [f k],
221 or raises [Not_found] if no such key exists.
222 @since 4.05 *)
223
224 val find_last_opt: f:(key -> bool) -> 'a t -> (key * 'a) option
225 (** [find_last_opt ~f m], where [f] is a monotonically decreasing
226 function, returns an option containing the binding of [m] with
227 the highest key [k] such that [f k], or [None] if no such key
228 exists.
229 @since 4.05 *)
230
231 (** {1:traversing Traversing} *)
232
233 val iter: f:(key:key -> data:'a -> unit) -> 'a t -> unit
234 (** [iter ~f m] applies [f] to all bindings in map [m].
235 [f] receives the key as first argument, and the associated value
236 as second argument. The bindings are passed to [f] in increasing
237 order with respect to the ordering over the type of the keys. *)
238
239 val fold:
240 f:(key:key -> data:'a -> 'acc -> 'acc) -> 'a t -> init:'acc -> 'acc
241 (** [fold ~f m ~init] computes [(f kN dN ... (f k1 d1 init)...)],
242 where [k1 ... kN] are the keys of all bindings in [m]
243 (in increasing order), and [d1 ... dN] are the associated data. *)
244
245 (** {1:transforming Transforming} *)
246
247 val map: f:('a -> 'b) -> 'a t -> 'b t
248 (** [map ~f m] returns a map with same domain as [m], where the
249 associated value [a] of all bindings of [m] has been
250 replaced by the result of the application of [f] to [a].
251 The bindings are passed to [f] in increasing order
252 with respect to the ordering over the type of the keys. *)
253
254 val mapi: f:(key -> 'a -> 'b) -> 'a t -> 'b t
255 (** Same as {!map}, but the function receives as arguments both the
256 key and the associated value for each binding of the map. *)
257
258 val filter: f:(key -> 'a -> bool) -> 'a t -> 'a t
259 (** [filter ~f m] returns the map with all the bindings in [m]
260 that satisfy predicate [p]. If every binding in [m] satisfies [f],
261 [m] is returned unchanged (the result of the function is then
262 physically equal to [m])
263 @since 3.12
264 @before 4.03 Physical equality was not ensured. *)
265
266 val filter_map: f:(key -> 'a -> 'b option) -> 'a t -> 'b t
267 (** [filter_map ~f m] applies the function [f] to every binding of
268 [m], and builds a map from the results. For each binding
269 [(k, v)] in the input map:
270 - if [f k v] is [None] then [k] is not in the result,
271 - if [f k v] is [Some v'] then the binding [(k, v')]
272 is in the output map.
273
274 For example, the following function on maps whose values are lists
275 {[
276 filter_map
277 (fun _k li -> match li with [] -> None | _::tl -> Some tl)
278 m
279 ]}
280 drops all bindings of [m] whose value is an empty list, and pops
281 the first element of each value that is non-empty.
282
283 @since 4.11 *)
284
285 val partition: f:(key -> 'a -> bool) -> 'a t -> 'a t * 'a t
286 (** [partition ~f m] returns a pair of maps [(m1, m2)], where
287 [m1] contains all the bindings of [m] that satisfy the
288 predicate [f], and [m2] is the map with all the bindings of
289 [m] that do not satisfy [f].
290 @since 3.12 *)
291
292 val split: key -> 'a t -> 'a t * 'a option * 'a t
293 (** [split x m] returns a triple [(l, data, r)], where
294 [l] is the map with all the bindings of [m] whose key
295 is strictly less than [x];
296 [r] is the map with all the bindings of [m] whose key
297 is strictly greater than [x];
298 [data] is [None] if [m] contains no binding for [x],
299 or [Some v] if [m] binds [v] to [x].
300 @since 3.12 *)
301
302 (** {1:predicates Predicates and comparisons} *)
303
304 val is_empty: 'a t -> bool
305 (** Test whether a map is empty or not. *)
306
307 val mem: key -> 'a t -> bool
308 (** [mem x m] returns [true] if [m] contains a binding for [x],
309 and [false] otherwise. *)
310
311 val equal: cmp:('a -> 'a -> bool) -> 'a t -> 'a t -> bool
312 (** [equal ~cmp m1 m2] tests whether the maps [m1] and [m2] are
313 equal, that is, contain equal keys and associate them with
314 equal data. [cmp] is the equality predicate used to compare
315 the data associated with the keys. *)
316
317 val compare: cmp:('a -> 'a -> int) -> 'a t -> 'a t -> int
318 (** Total ordering between maps. The first argument is a total ordering
319 used to compare data associated with equal keys in the two maps. *)
320
321 val for_all: f:(key -> 'a -> bool) -> 'a t -> bool
322 (** [for_all ~f m] checks if all the bindings of the map
323 satisfy the predicate [f].
324 @since 3.12 *)
325
326 val exists: f:(key -> 'a -> bool) -> 'a t -> bool
327 (** [exists ~f m] checks if at least one binding of the map
328 satisfies the predicate [f].
329 @since 3.12 *)
330
331 (** {1:converting Converting} *)
332
333 val to_list : 'a t -> (key * 'a) list
334 (** [to_list m] is {!bindings}[ m].
335 @since 5.1 *)
336
337 val of_list : (key * 'a) list -> 'a t
338 (** [of_list bs] adds the bindings of [bs] to the empty map,
339 in list order (if a key is bound twice in [bs] the last one
340 takes over).
341 @since 5.1 *)
342
343 val to_seq : 'a t -> (key * 'a) Seq.t
344 (** Iterate on the whole map, in ascending order of keys
345 @since 4.07 *)
346
347 val to_rev_seq : 'a t -> (key * 'a) Seq.t
348 (** Iterate on the whole map, in descending order of keys
349 @since 4.12 *)
350
351 val to_seq_from : key -> 'a t -> (key * 'a) Seq.t
352 (** [to_seq_from k m] iterates on a subset of the bindings of [m],
353 in ascending order of keys, from key [k] or above.
354 @since 4.07 *)
355
356 val add_seq : (key * 'a) Seq.t -> 'a t -> 'a t
357 (** Add the given bindings to the map, in order.
358 @since 4.07 *)
359
360 val of_seq : (key * 'a) Seq.t -> 'a t
361 (** Build a map from the given bindings
362 @since 4.07 *)
363 end
364 (** Output signature of the functor {!Make}. *)
365
366 module Make (Ord : OrderedType) : S with type key = Ord.t
367 (** Functor building an implementation of the map structure
368 given a totally ordered type. *)