| 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 set.mli, do not edit it directly! Instead, |
| 17 | edit templates/set.template.mli and run tools/sync_stdlib_docs *) |
| 18 | |
| 19 | (** Sets over ordered types. |
| 20 | |
| 21 | This module implements the set data structure, given a total ordering |
| 22 | function over the set elements. All operations over sets |
| 23 | are purely applicative (no side-effects). |
| 24 | The implementation uses balanced binary trees, and is therefore |
| 25 | reasonably efficient: insertion and membership take time |
| 26 | logarithmic in the size of the set, for instance. |
| 27 | |
| 28 | The {!Make} functor constructs implementations for any type, given a |
| 29 | [compare] function. |
| 30 | For instance: |
| 31 | {[ |
| 32 | module IntPairs = |
| 33 | struct |
| 34 | type t = int * int |
| 35 | let compare (x0,y0) (x1,y1) = |
| 36 | match Stdlib.compare x0 x1 with |
| 37 | 0 -> Stdlib.compare y0 y1 |
| 38 | | c -> c |
| 39 | end |
| 40 | |
| 41 | module PairsSet = Set.Make(IntPairs) |
| 42 | |
| 43 | let m = PairsSet.(empty |> add (2,3) |> add (5,7) |> add (11,13)) |
| 44 | ]} |
| 45 | |
| 46 | This creates a new module [PairsSet], with a new type [PairsSet.t] |
| 47 | of sets of [int * int]. |
| 48 | *) |
| 49 | |
| 50 | module type OrderedType = |
| 51 | sig |
| 52 | type t |
| 53 | (** The type of the set elements. *) |
| 54 | |
| 55 | val compare : t -> t -> int |
| 56 | (** A total ordering function over the set elements. |
| 57 | This is a two-argument function [f] such that |
| 58 | [f e1 e2] is zero if the elements [e1] and [e2] are equal, |
| 59 | [f e1 e2] is strictly negative if [e1] is smaller than [e2], |
| 60 | and [f e1 e2] is strictly positive if [e1] is greater than [e2]. |
| 61 | Example: a suitable ordering function is the generic structural |
| 62 | comparison function {!Stdlib.compare}. *) |
| 63 | end |
| 64 | (** Input signature of the functor {!Make}. *) |
| 65 | |
| 66 | module type S = |
| 67 | sig |
| 68 | |
| 69 | (** {1:sets Sets} *) |
| 70 | |
| 71 | type elt |
| 72 | (** The type of the set elements. *) |
| 73 | |
| 74 | type t |
| 75 | (** The type of sets. *) |
| 76 | |
| 77 | val empty: t |
| 78 | (** The empty set. *) |
| 79 | |
| 80 | val add: elt -> t -> t |
| 81 | (** [add x s] returns a set containing all elements of [s], |
| 82 | plus [x]. If [x] was already in [s], [s] is returned unchanged |
| 83 | (the result of the function is then physically equal to [s]). |
| 84 | @before 4.03 Physical equality was not ensured. *) |
| 85 | |
| 86 | val singleton: elt -> t |
| 87 | (** [singleton x] returns the one-element set containing only [x]. *) |
| 88 | |
| 89 | val remove: elt -> t -> t |
| 90 | (** [remove x s] returns a set containing all elements of [s], |
| 91 | except [x]. If [x] was not in [s], [s] is returned unchanged |
| 92 | (the result of the function is then physically equal to [s]). |
| 93 | @before 4.03 Physical equality was not ensured. *) |
| 94 | |
| 95 | val union: t -> t -> t |
| 96 | (** Set union. *) |
| 97 | |
| 98 | val inter: t -> t -> t |
| 99 | (** Set intersection. *) |
| 100 | |
| 101 | val disjoint: t -> t -> bool |
| 102 | (** Test if two sets are disjoint. |
| 103 | @since 4.08 *) |
| 104 | |
| 105 | val diff: t -> t -> t |
| 106 | (** Set difference: [diff s1 s2] contains the elements of [s1] |
| 107 | that are not in [s2]. *) |
| 108 | |
| 109 | val cardinal: t -> int |
| 110 | (** Return the number of elements of a set. *) |
| 111 | |
| 112 | (** {1:elements Elements} *) |
| 113 | |
| 114 | val elements: t -> elt list |
| 115 | (** Return the list of all elements of the given set. |
| 116 | The returned list is sorted in increasing order with respect |
| 117 | to the ordering [Ord.compare], where [Ord] is the argument |
| 118 | given to {!Set.Make}. *) |
| 119 | |
| 120 | val min_elt: t -> elt |
| 121 | (** Return the smallest element of the given set |
| 122 | (with respect to the [Ord.compare] ordering), or raise |
| 123 | [Not_found] if the set is empty. *) |
| 124 | |
| 125 | val min_elt_opt: t -> elt option |
| 126 | (** Return the smallest element of the given set |
| 127 | (with respect to the [Ord.compare] ordering), or [None] |
| 128 | if the set is empty. |
| 129 | @since 4.05 *) |
| 130 | |
| 131 | val max_elt: t -> elt |
| 132 | (** Same as {!min_elt}, but returns the largest element of the |
| 133 | given set. *) |
| 134 | |
| 135 | val max_elt_opt: t -> elt option |
| 136 | (** Same as {!min_elt_opt}, but returns the largest element of the |
| 137 | given set. |
| 138 | @since 4.05 *) |
| 139 | |
| 140 | val choose: t -> elt |
| 141 | (** Return one element of the given set, or raise [Not_found] if |
| 142 | the set is empty. Which element is chosen is unspecified, |
| 143 | but equal elements will be chosen for equal sets. *) |
| 144 | |
| 145 | val choose_opt: t -> elt option |
| 146 | (** Return one element of the given set, or [None] if |
| 147 | the set is empty. Which element is chosen is unspecified, |
| 148 | but equal elements will be chosen for equal sets. |
| 149 | @since 4.05 *) |
| 150 | |
| 151 | (** {1:searching Searching} *) |
| 152 | |
| 153 | val find: elt -> t -> elt |
| 154 | (** [find x s] returns the element of [s] equal to [x] (according |
| 155 | to [Ord.compare]), or raise [Not_found] if no such element |
| 156 | exists. |
| 157 | @since 4.01 *) |
| 158 | |
| 159 | val find_opt: elt -> t -> elt option |
| 160 | (** [find_opt x s] returns the element of [s] equal to [x] (according |
| 161 | to [Ord.compare]), or [None] if no such element |
| 162 | exists. |
| 163 | @since 4.05 *) |
| 164 | |
| 165 | val find_first: f:(elt -> bool) -> t -> elt |
| 166 | (** [find_first ~f s], where [f] is a monotonically increasing function, |
| 167 | returns the lowest element [e] of [s] such that [f e], |
| 168 | or raises [Not_found] if no such element exists. |
| 169 | |
| 170 | For example, [find_first (fun e -> Ord.compare e x >= 0) s] will |
| 171 | return the first element [e] of [s] where [Ord.compare e x >= 0] |
| 172 | (intuitively: [e >= x]), or raise [Not_found] if [x] is greater than |
| 173 | any element of [s]. |
| 174 | |
| 175 | @since 4.05 *) |
| 176 | |
| 177 | val find_first_opt: f:(elt -> bool) -> t -> elt option |
| 178 | (** [find_first_opt ~f s], where [f] is a monotonically increasing |
| 179 | function, returns an option containing the lowest element [e] of [s] |
| 180 | such that [f e], or [None] if no such element exists. |
| 181 | @since 4.05 |
| 182 | *) |
| 183 | |
| 184 | val find_last: f:(elt -> bool) -> t -> elt |
| 185 | (** [find_last ~f s], where [f] is a monotonically decreasing function, |
| 186 | returns the highest element [e] of [s] such that [f e], |
| 187 | or raises [Not_found] if no such element exists. |
| 188 | @since 4.05 *) |
| 189 | |
| 190 | val find_last_opt: f:(elt -> bool) -> t -> elt option |
| 191 | (** [find_last_opt ~f s], where [f] is a monotonically decreasing |
| 192 | function, returns an option containing the highest element [e] of [s] |
| 193 | such that [f e], or [None] if no such element exists. |
| 194 | @since 4.05 *) |
| 195 | |
| 196 | (** {1:traversing Traversing} *) |
| 197 | |
| 198 | val iter: f:(elt -> unit) -> t -> unit |
| 199 | (** [iter ~f s] applies [f] in turn to all elements of [s]. |
| 200 | The elements of [s] are presented to [f] in increasing order |
| 201 | with respect to the ordering over the type of the elements. *) |
| 202 | |
| 203 | val fold: f:(elt -> 'acc -> 'acc) -> t -> init:'acc -> 'acc |
| 204 | (** [fold ~f s init] computes [(f xN ... (f x2 (f x1 init))...)], |
| 205 | where [x1 ... xN] are the elements of [s], in increasing order. *) |
| 206 | |
| 207 | (** {1:transforming Transforming} *) |
| 208 | |
| 209 | val map: f:(elt -> elt) -> t -> t |
| 210 | (** [map ~f s] is the set whose elements are [f a0],[f a1]... [f |
| 211 | aN], where [a0],[a1]...[aN] are the elements of [s]. |
| 212 | |
| 213 | The elements are passed to [f] in increasing order |
| 214 | with respect to the ordering over the type of the elements. |
| 215 | |
| 216 | If no element of [s] is changed by [f], [s] is returned |
| 217 | unchanged. (If each output of [f] is physically equal to its |
| 218 | input, the returned set is physically equal to [s].) |
| 219 | @since 4.04 *) |
| 220 | |
| 221 | val filter: f:(elt -> bool) -> t -> t |
| 222 | (** [filter ~f s] returns the set of all elements in [s] |
| 223 | that satisfy predicate [f]. If [f] satisfies every element in [s], |
| 224 | [s] is returned unchanged (the result of the function is then |
| 225 | physically equal to [s]). |
| 226 | @before 4.03 Physical equality was not ensured.*) |
| 227 | |
| 228 | val filter_map: f:(elt -> elt option) -> t -> t |
| 229 | (** [filter_map ~f s] returns the set of all [v] such that |
| 230 | [f x = Some v] for some element [x] of [s]. |
| 231 | |
| 232 | For example, |
| 233 | {[filter_map (fun n -> if n mod 2 = 0 then Some (n / 2) else None) s]} |
| 234 | is the set of halves of the even elements of [s]. |
| 235 | |
| 236 | If no element of [s] is changed or dropped by [f] (if |
| 237 | [f x = Some x] for each element [x]), then |
| 238 | [s] is returned unchanged: the result of the function |
| 239 | is then physically equal to [s]. |
| 240 | |
| 241 | @since 4.11 *) |
| 242 | |
| 243 | val partition: f:(elt -> bool) -> t -> t * t |
| 244 | (** [partition ~f s] returns a pair of sets [(s1, s2)], where |
| 245 | [s1] is the set of all the elements of [s] that satisfy the |
| 246 | predicate [f], and [s2] is the set of all the elements of |
| 247 | [s] that do not satisfy [f]. *) |
| 248 | |
| 249 | val split: elt -> t -> t * bool * t |
| 250 | (** [split x s] returns a triple [(l, present, r)], where |
| 251 | [l] is the set of elements of [s] that are |
| 252 | strictly less than [x]; |
| 253 | [r] is the set of elements of [s] that are |
| 254 | strictly greater than [x]; |
| 255 | [present] is [false] if [s] contains no element equal to [x], |
| 256 | or [true] if [s] contains an element equal to [x]. *) |
| 257 | |
| 258 | (** {1:predicates Predicates and comparisons} *) |
| 259 | |
| 260 | val is_empty: t -> bool |
| 261 | (** Test whether a set is empty or not. *) |
| 262 | |
| 263 | val mem: elt -> t -> bool |
| 264 | (** [mem x s] tests whether [x] belongs to the set [s]. *) |
| 265 | |
| 266 | val equal: t -> t -> bool |
| 267 | (** [equal s1 s2] tests whether the sets [s1] and [s2] are |
| 268 | equal, that is, contain equal elements. *) |
| 269 | |
| 270 | val compare: t -> t -> int |
| 271 | (** Total ordering between sets. Can be used as the ordering function |
| 272 | for doing sets of sets. *) |
| 273 | |
| 274 | val subset: t -> t -> bool |
| 275 | (** [subset s1 s2] tests whether the set [s1] is a subset of |
| 276 | the set [s2]. *) |
| 277 | |
| 278 | val for_all: f:(elt -> bool) -> t -> bool |
| 279 | (** [for_all ~f s] checks if all elements of the set |
| 280 | satisfy the predicate [f]. *) |
| 281 | |
| 282 | val exists: f:(elt -> bool) -> t -> bool |
| 283 | (** [exists ~f s] checks if at least one element of |
| 284 | the set satisfies the predicate [f]. *) |
| 285 | |
| 286 | (** {1:converting Converting} *) |
| 287 | |
| 288 | val to_list : t -> elt list |
| 289 | (** [to_list s] is {!elements}[ s]. |
| 290 | @since 5.1 *) |
| 291 | |
| 292 | val of_list: elt list -> t |
| 293 | (** [of_list l] creates a set from a list of elements. |
| 294 | This is usually more efficient than folding [add] over the list, |
| 295 | except perhaps for lists with many duplicated elements. |
| 296 | @since 4.02 *) |
| 297 | |
| 298 | val to_seq_from : elt -> t -> elt Seq.t |
| 299 | (** [to_seq_from x s] iterates on a subset of the elements of [s] |
| 300 | in ascending order, from [x] or above. |
| 301 | @since 4.07 *) |
| 302 | |
| 303 | val to_seq : t -> elt Seq.t |
| 304 | (** Iterate on the whole set, in ascending order |
| 305 | @since 4.07 *) |
| 306 | |
| 307 | val to_rev_seq : t -> elt Seq.t |
| 308 | (** Iterate on the whole set, in descending order |
| 309 | @since 4.12 *) |
| 310 | |
| 311 | val add_seq : elt Seq.t -> t -> t |
| 312 | (** Add the given elements to the set, in order. |
| 313 | @since 4.07 *) |
| 314 | |
| 315 | val of_seq : elt Seq.t -> t |
| 316 | (** Build a set from the given bindings |
| 317 | @since 4.07 *) |
| 318 | end |
| 319 | (** Output signature of the functor {!Make}. *) |
| 320 | |
| 321 | module Make (Ord : OrderedType) : S with type elt = Ord.t |
| 322 | (** Functor building an implementation of the set structure |
| 323 | given a totally ordered type. *) |