1Set.Make(3) OCaml library Set.Make(3)
2
3
4
6 Set.Make - Functor building an implementation of the set structure
7 given a totally ordered type.
8
10 Module Set.Make
11
13 Module Make
14 : functor (Ord : OrderedType) -> sig end
15
16
17 Functor building an implementation of the set structure given a totally
18 ordered type.
19
20
21 Parameters:
22
23 "Ord"
24
25 Set.OrderedType
26
27
28
29
30
31
32
33 type elt
34
35
36 The type of the set elements.
37
38
39 type t
40
41
42 The type of sets.
43
44
45
46 val empty : t
47
48 The empty set.
49
50
51
52 val is_empty : t -> bool
53
54 Test whether a set is empty or not.
55
56
57
58 val mem : elt -> t -> bool
59
60
61 mem x s tests whether x belongs to the set s .
62
63
64
65 val add : elt -> t -> t
66
67
68 add x s returns a set containing all elements of s , plus x . If x was
69 already in s , s is returned unchanged (the result of the function is
70 then physically equal to s ).
71
72
73 Before4.03 Physical equality was not ensured.
74
75
76
77
78 val singleton : elt -> t
79
80
81 singleton x returns the one-element set containing only x .
82
83
84
85 val remove : elt -> t -> t
86
87
88 remove x s returns a set containing all elements of s , except x . If x
89 was not in s , s is returned unchanged (the result of the function is
90 then physically equal to s ).
91
92
93 Before4.03 Physical equality was not ensured.
94
95
96
97
98 val union : t -> t -> t
99
100 Set union.
101
102
103
104 val inter : t -> t -> t
105
106 Set intersection.
107
108
109
110 val diff : t -> t -> t
111
112 Set difference.
113
114
115
116 val compare : t -> t -> int
117
118 Total ordering between sets. Can be used as the ordering function for
119 doing sets of sets.
120
121
122
123 val equal : t -> t -> bool
124
125
126 equal s1 s2 tests whether the sets s1 and s2 are equal, that is, con‐
127 tain equal elements.
128
129
130
131 val subset : t -> t -> bool
132
133
134 subset s1 s2 tests whether the set s1 is a subset of the set s2 .
135
136
137
138 val iter : (elt -> unit) -> t -> unit
139
140
141 iter f s applies f in turn to all elements of s . The elements of s
142 are presented to f in increasing order with respect to the ordering
143 over the type of the elements.
144
145
146
147 val map : (elt -> elt) -> t -> t
148
149
150 map f s is the set whose elements are f a0 , f a1 ... f aN , where a0
151 , a1 ... aN are the elements of s .
152
153 The elements are passed to f in increasing order with respect to the
154 ordering over the type of the elements.
155
156 If no element of s is changed by f , s is returned unchanged. (If each
157 output of f is physically equal to its input, the returned set is phys‐
158 ically equal to s .)
159
160
161 Since 4.04.0
162
163
164
165 val fold : (elt -> 'a -> 'a) -> t -> 'a -> 'a
166
167
168 fold f s a computes (f xN ... (f x2 (f x1 a))...) , where x1 ... xN
169 are the elements of s , in increasing order.
170
171
172
173 val for_all : (elt -> bool) -> t -> bool
174
175
176 for_all p s checks if all elements of the set satisfy the predicate p .
177
178
179
180 val exists : (elt -> bool) -> t -> bool
181
182
183 exists p s checks if at least one element of the set satisfies the
184 predicate p .
185
186
187
188 val filter : (elt -> bool) -> t -> t
189
190
191 filter p s returns the set of all elements in s that satisfy predicate
192 p . If p satisfies every element in s , s is returned unchanged (the
193 result of the function is then physically equal to s ).
194
195
196 Before4.03 Physical equality was not ensured.
197
198
199
200
201 val partition : (elt -> bool) -> t -> t * t
202
203
204 partition p s returns a pair of sets (s1, s2) , where s1 is the set of
205 all the elements of s that satisfy the predicate p , and s2 is the set
206 of all the elements of s that do not satisfy p .
207
208
209
210 val cardinal : t -> int
211
212 Return the number of elements of a set.
213
214
215
216 val elements : t -> elt list
217
218 Return the list of all elements of the given set. The returned list is
219 sorted in increasing order with respect to the ordering Ord.compare ,
220 where Ord is the argument given to Set.Make .
221
222
223
224 val min_elt : t -> elt
225
226 Return the smallest element of the given set (with respect to the
227 Ord.compare ordering), or raise Not_found if the set is empty.
228
229
230
231 val min_elt_opt : t -> elt option
232
233 Return the smallest element of the given set (with respect to the
234 Ord.compare ordering), or None if the set is empty.
235
236
237 Since 4.05
238
239
240
241 val max_elt : t -> elt
242
243 Same as Set.S.min_elt , but returns the largest element of the given
244 set.
245
246
247
248 val max_elt_opt : t -> elt option
249
250 Same as Set.S.min_elt_opt , but returns the largest element of the
251 given set.
252
253
254 Since 4.05
255
256
257
258 val choose : t -> elt
259
260 Return one element of the given set, or raise Not_found if the set is
261 empty. Which element is chosen is unspecified, but equal elements will
262 be chosen for equal sets.
263
264
265
266 val choose_opt : t -> elt option
267
268 Return one element of the given set, or None if the set is empty. Which
269 element is chosen is unspecified, but equal elements will be chosen for
270 equal sets.
271
272
273 Since 4.05
274
275
276
277 val split : elt -> t -> t * bool * t
278
279
280 split x s returns a triple (l, present, r) , where l is the set of ele‐
281 ments of s that are strictly less than x ; r is the set of elements of
282 s that are strictly greater than x ; present is false if s contains no
283 element equal to x , or true if s contains an element equal to x .
284
285
286
287 val find : elt -> t -> elt
288
289
290 find x s returns the element of s equal to x (according to Ord.compare
291 ), or raise Not_found if no such element exists.
292
293
294 Since 4.01.0
295
296
297
298 val find_opt : elt -> t -> elt option
299
300
301 find_opt x s returns the element of s equal to x (according to Ord.com‐
302 pare ), or None if no such element exists.
303
304
305 Since 4.05
306
307
308
309 val find_first : (elt -> bool) -> t -> elt
310
311
312 find_first f s , where f is a monotonically increasing function,
313 returns the lowest element e of s such that f e , or raises Not_found
314 if no such element exists.
315
316 For example, find_first (fun e -> Ord.compare e x >= 0) s will return
317 the first element e of s where Ord.compare e x >= 0 (intuitively: e >=
318 x ), or raise Not_found if x is greater than any element of s .
319
320
321 Since 4.05
322
323
324
325 val find_first_opt : (elt -> bool) -> t -> elt option
326
327
328 find_first_opt f s , where f is a monotonically increasing function,
329 returns an option containing the lowest element e of s such that f e ,
330 or None if no such element exists.
331
332
333 Since 4.05
334
335
336
337 val find_last : (elt -> bool) -> t -> elt
338
339
340 find_last f s , where f is a monotonically decreasing function, returns
341 the highest element e of s such that f e , or raises Not_found if no
342 such element exists.
343
344
345 Since 4.05
346
347
348
349 val find_last_opt : (elt -> bool) -> t -> elt option
350
351
352 find_last_opt f s , where f is a monotonically decreasing function,
353 returns an option containing the highest element e of s such that f e ,
354 or None if no such element exists.
355
356
357 Since 4.05
358
359
360
361 val of_list : elt list -> t
362
363
364 of_list l creates a set from a list of elements. This is usually more
365 efficient than folding add over the list, except perhaps for lists with
366 many duplicated elements.
367
368
369 Since 4.02.0
370
371
372
373
374 === Iterators ===
375
376
377 val to_seq_from : elt -> t -> elt Seq.t
378
379
380 to_seq_from x s iterates on a subset of the elements of s in ascending
381 order, from x or above.
382
383
384 Since 4.07
385
386
387
388 val to_seq : t -> elt Seq.t
389
390 Iterate on the whole set, in ascending order
391
392
393 Since 4.07
394
395
396
397 val add_seq : elt Seq.t -> t -> t
398
399 Add the given elements to the set, in order.
400
401
402 Since 4.07
403
404
405
406 val of_seq : elt Seq.t -> t
407
408 Build a set from the given bindings
409
410
411 Since 4.07
412
413
414
415
416
417OCamldoc 2019-02-02 Set.Make(3)