-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathbelt_Set.mli
523 lines (403 loc) · 14.4 KB
/
belt_Set.mli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
(* Copyright (C) 2017 Authors of ReScript
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
(** An _immutable_ sorted set module which allows customized _compare_ behavior.
The implementation uses balanced binary trees, and therefore searching
and insertion take time logarithmic in the size of the map.
For more info on this module's usage of identity, `make` and others, please see
the top level documentation of Belt, **A special encoding for collection safety**.
Example usage:
```res example
module PairComparator =
Belt.Id.MakeComparable({
type t = (int, int)
let cmp = ((a0, a1), (b0, b1)) =>
switch (Pervasives.compare(a0, b0)) {
| 0 => Pervasives.compare(a1, b1)
| c => c
}
})
let mySet = Belt.Set.make(~id=module(PairComparator))
let mySet2 = Belt.Set.add(mySet, (1, 2))
```
**Note:** This module's examples will assume a predeclared module for integers
called `IntCmp`. It is declared like this:
```res example
module IntCmp =
Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
```
*)
(** Specialized when value type is `int`, more efficient
than the generic type, its compare behavior is fixed using the built-in comparison
*)
module Int = Belt_SetInt
(** Specialized when value type is `string`, more efficient
than the generic type, its compare behavior is fixed using the built-in comparison
*)
module String = Belt_SetString
(** This module separates identity from data, it is a bit more verbose but slightly
more efficient due to the fact that there is no need to pack identity and data back
after each operation
*)
module Dict = Belt_SetDict
type ('value, 'identity) t
(**
`'value` is the element type
`'identity` the identity of the collection
*)
type ('value, 'id) id = ('value, 'id) Belt_Id.comparable
(**
The identity needed for making a set from scratch
*)
val make: id:('value, 'id) id -> ('value, 'id) t
(**
Creates a new set by taking in the comparator
```res example
let set = Belt.Set.make(~id=module(IntCmp))
```
*)
val fromArray: 'value array -> id:('value, 'id) id -> ('value, 'id) t
(**
Creates new set from array of elements.
```res example
let s0 = Belt.Set.fromArray([1, 3, 2, 4], ~id=module(IntCmp))
s0->Belt.Set.toArray /* [1, 2, 3, 4] */
```
*)
val fromSortedArrayUnsafe: 'value array -> id:('value, 'id) id -> ('value,'id) t
(**
The same as [fromArray][#fromarray] except it is after assuming the input array is already sorted.
*)
val isEmpty: _ t -> bool
(**
Checks if set is empty.
```res example
let empty = Belt.Set.fromArray([], ~id=module(IntCmp))
let notEmpty = Belt.Set.fromArray([1],~id=module(IntCmp))
Belt.Set.isEmpty(empty) /* true */
Belt.Set.isEmpty(notEmpty) /* false */
```
*)
val has: ('value, 'id) t -> 'value -> bool
(**
Checks if element exists in set.
```res example
let set = Belt.Set.fromArray([1, 4, 2, 5], ~id=module(IntCmp))
set->Belt.Set.has(3) /* false */
set->Belt.Set.has(1) /* true */
```
*)
val add:
('value, 'id) t -> 'value -> ('value, 'id) t
(**
Adds element to set. If element existed in set, value is unchanged.
```res example
let s0 = Belt.Set.make(~id=module(IntCmp))
let s1 = s0->Belt.Set.add(1)
let s2 = s1->Belt.Set.add(2)
let s3 = s2->Belt.Set.add(2)
s0->Belt.Set.toArray /* [] */
s1->Belt.Set.toArray /* [1] */
s2->Belt.Set.toArray /* [1, 2] */
s3->Belt.Set.toArray /* [1,2 ] */
s2 == s3 /* true */
```
*)
val mergeMany: ('value, 'id) t -> 'value array -> ('value, 'id) t
(**
Adds each element of array to set. Unlike [add](#add), the reference of return value might be changed even if all values in array already exist in set
```res example
let set = Belt.Set.make(~id=module(IntCmp))
let newSet = set->Belt.Set.mergeMany([5, 4, 3, 2, 1])
newSet->Belt.Set.toArray /* [1, 2, 3, 4, 5] */
```
*)
val remove: ('value, 'id) t -> 'value -> ('value, 'id) t
(**
Removes element from set. If element did not exist in set, value is unchanged.
```res example
let s0 = Belt.Set.fromArray([2,3,1,4,5], ~id=module(IntCmp))
let s1 = s0->Belt.Set.remove(1)
let s2 = s1->Belt.Set.remove(3)
let s3 = s2->Belt.Set.remove(3)
s1->Belt.Set.toArray /* [2,3,4,5] */
s2->Belt.Set.toArray /* [2,4,5] */
s2 == s3 /* true */
```
*)
val removeMany:
('value, 'id) t -> 'value array -> ('value, 'id) t
(**
Removes each element of array from set. Unlike [remove](#remove), the reference of return value might be changed even if none of values in array existed in set.
```res example
let set = Belt.Set.fromArray([1, 2, 3, 4],~id=module(IntCmp))
let newSet = set->Belt.Set.removeMany([5, 4, 3, 2, 1])
newSet->Belt.Set.toArray /* [] */
```
*)
val union: ('value, 'id) t -> ('value, 'id) t -> ('value, 'id) t
(**
Returns union of two sets.
```res example
let s0 = Belt.Set.fromArray([5,2,3,5,6], ~id=module(IntCmp))
let s1 = Belt.Set.fromArray([5,2,3,1,5,4], ~id=module(IntCmp))
let union = Belt.Set.union(s0, s1)
union->Belt.Set.toArray /* [1,2,3,4,5,6] */
```
*)
val intersect: ('value, 'id) t -> ('value, 'id) t -> ('value, 'id) t
(**
Returns intersection of two sets.
```res example
let s0 = Belt.Set.fromArray([5,2,3,5,6], ~id=module(IntCmp))
let s1 = Belt.Set.fromArray([5,2,3,1,5,4], ~id=module(IntCmp))
let intersect = Belt.Set.intersect(s0, s1)
intersect->Belt.Set.toArray /* [2,3,5] */
```
*)
val diff: ('value, 'id) t -> ('value, 'id) t -> ('value, 'id) t
(**
Returns elements from first set, not existing in second set.
```res example
let s0 = Belt.Set.fromArray([5,2,3,5,6], ~id=module(IntCmp))
let s1 = Belt.Set.fromArray([5,2,3,1,5,4], ~id=module(IntCmp))
Belt.Set.toArray(Belt.Set.diff(s0, s1)) /* [6] */
Belt.Set.toArray(Belt.Set.diff(s1,s0)) /* [1,4] */
```
*)
val subset: ('value, 'id) t -> ('value, 'id) t -> bool
(**
Checks if second set is subset of first set.
```res example
let s0 = Belt.Set.fromArray([5,2,3,5,6], ~id=module(IntCmp))
let s1 = Belt.Set.fromArray([5,2,3,1,5,4], ~id=module(IntCmp))
let s2 = Belt.Set.intersect(s0, s1)
Belt.Set.subset(s2, s0) /* true */
Belt.Set.subset(s2, s1) /* true */
Belt.Set.subset(s1, s0) /* false */
```
*)
val cmp: ('value, 'id) t -> ('value, 'id) t -> int
(**
Total ordering between sets. Can be used as the ordering function for doing sets of sets. It compares size first and then iterates over each element following the order of elements.
*)
val eq: ('value, 'id) t -> ('value, 'id) t -> bool
(**
Checks if two sets are equal.
```res example
let s0 = Belt.Set.fromArray([5,2,3], ~id=module(IntCmp))
let s1 = Belt.Set.fromArray([3,2,5], ~id=module(IntCmp))
Belt.Set.eq(s0, s1) /* true */
```
*)
val forEachU: ('value, 'id) t -> ('value -> unit [@bs]) -> unit
(**
Same as [forEach](##forEach) but takes uncurried functon.
*)
val forEach: ('value, 'id) t -> ('value -> unit ) -> unit
(**
Applies function `f` in turn to all elements of set in increasing order.
```res example
let s0 = Belt.Set.fromArray([5,2,3,5,6], ~id=module(IntCmp))
let acc = ref(list{})
s0->Belt.Set.forEach(x => {
acc := Belt.List.add(acc.contents, x)
})
acc /* [6,5,3,2] */
```
*)
val reduceU: ('value, 'id) t -> 'a -> ('a -> 'value -> 'a [@bs]) -> 'a
val reduce: ('value, 'id) t -> 'a -> ('a -> 'value -> 'a ) -> 'a
(**
Applies function `f` to each element of set in increasing order. Function `f` has two parameters: the item from the set and an “accumulator”, which starts with a value of `initialValue`. `reduce` returns the final value of the accumulator.
```res example
let s0 = Belt.Set.fromArray([5,2,3,5,6], ~id=module(IntCmp))
s0->Belt.Set.reduce(list{}, (acc, element) =>
acc->Belt.List.add(element)
) /* [6,5,3,2] */
```
*)
val everyU: ('value, 'id) t -> ('value -> bool [@bs]) -> bool
val every: ('value, 'id) t -> ('value -> bool ) -> bool
(**
Checks if all elements of the set satisfy the predicate. Order unspecified.
```res example
let isEven = x => mod(x, 2) == 0
let s0 = Belt.Set.fromArray([2,4,6,8], ~id=module(IntCmp))
s0->Belt.Set.every(isEven) /* true */
```
*)
val someU: ('value, 'id) t -> ('value -> bool [@bs]) -> bool
val some: ('value, 'id) t -> ('value -> bool ) -> bool
(**
Checks if at least one element of the set satisfies the predicate.
```res example
let isOdd = x => mod(x, 2) != 0
let s0 = Belt.Set.fromArray([1,2,4,6,8], ~id=module(IntCmp))
s0->Belt.Set.some(isOdd) /* true */
```
*)
val keepU: ('value, 'id) t -> ('value -> bool [@bs]) -> ('value, 'id) t
val keep: ('value, 'id) t -> ('value -> bool ) -> ('value, 'id) t
(**
Returns the set of all elements that satisfy the predicate.
```res example
let isEven = x => mod(x, 2) == 0
let s0 = Belt.Set.fromArray([1,2,3,4,5], ~id=module(IntCmp))
let s1 = s0->Belt.Set.keep(isEven)
s1->Belt.Set.toArray /* [2,4] */
```
*)
val partitionU: ('value, 'id) t -> ('value -> bool [@bs]) -> ('value, 'id) t * ('value, 'id) t
val partition: ('value, 'id) t -> ('value -> bool) -> ('value, 'id) t * ('value, 'id) t
(**
Returns a pair of sets, where first is the set of all the elements of set that satisfy the predicate, and second is the set of all the elements of set that do not satisfy the predicate.
```res example
let isOdd = x => mod(x, 2) != 0
let s0 = Belt.Set.fromArray([1,2,3,4,5], ~id=module(IntCmp))
let (s1, s2) = s0->Belt.Set.partition(isOdd)
s1->Belt.Set.toArray /* [1,3,5] */
s2->Belt.Set.toArray /* [2,4] */
```
*)
val size: ('value, 'id) t -> int
(**
Returns size of the set.
```res example
let s0 = Belt.Set.fromArray([1,2,3,4], ~id=module(IntCmp))
s0->Belt.Set.size /* 4 */
```
*)
val toArray: ('value, 'id) t -> 'value array
(**
Returns array of ordered set elements.
```res example
let s0 = Belt.Set.fromArray([3,2,1,5], ~id=module(IntCmp))
s0->Belt.Set.toArray /* [1,2,3,5] */
```
*)
val toList: ('value, 'id) t -> 'value list
(**
Returns list of ordered set elements.
```res example
let s0 = Belt.Set.fromArray([3,2,1,5], ~id=module(IntCmp))
s0->Belt.Set.toList /* [1,2,3,5] */
```
*)
val minimum: ('value, 'id) t -> 'value option
(**
Returns minimum value of the collection. `None` if collection is empty.
```res example
let s0 = Belt.Set.make(~id=module(IntCmp))
let s1 = Belt.Set.fromArray([3,2,1,5], ~id=module(IntCmp))
s0->Belt.Set.minimum /* None */
s1->Belt.Set.minimum /* Some(1) */
```
*)
val minUndefined: ('value, 'id) t -> 'value Js.undefined
(**
Returns minimum value of the collection. `undefined` if collection is empty.
```res example
let s0 = Belt.Set.make(~id=module(IntCmp))
let s1 = Belt.Set.fromArray([3,2,1,5], ~id=module(IntCmp))
s0->Belt.Set.minUndefined /* undefined */
s1->Belt.Set.minUndefined /* 1 */
```
*)
val maximum: ('value, 'id) t -> 'value option
(**
Returns maximum value of the collection. `None` if collection is empty.
```res example
let s0 = Belt.Set.make(~id=module(IntCmp))
let s1 = Belt.Set.fromArray([3,2,1,5], ~id=module(IntCmp))
s0->Belt.Set.maximum /* None */
s1->Belt.Set.maximum /* Some(5) */
```
*)
val maxUndefined: ('value, 'id) t -> 'value Js.undefined
(**
Returns maximum value of the collection. `undefined` if collection is empty.
```res example
let s0 = Belt.Set.make(~id=module(IntCmp))
let s1 = Belt.Set.fromArray([3,2,1,5], ~id=module(IntCmp))
s0->Belt.Set.maxUndefined /* undefined */
s1->Belt.Set.maxUndefined /* 5 */
```
*)
val get: ('value, 'id) t -> 'value -> 'value option
(**
Returns the reference of the value which is equivalent to value using the comparator specifiecd by this collection. Returns `None` if element does not exist.
```res example
let s0 = Belt.Set.fromArray([1,2,3,4,5], ~id=module(IntCmp))
s0->Belt.Set.get(3) /* Some(3) */
s0->Belt.Set.get(20) /* None */
```
*)
val getUndefined: ('value, 'id) t -> 'value -> 'value Js.undefined
(**
Same as [get](#get) but returns `undefined` when element does not exist.
*)
val getExn: ('value, 'id) t -> 'value -> 'value
(**
Same as [get](#get) but raise when element does not exist.
*)
val split: ('value, 'id) t -> 'value -> (('value, 'id) t * ('value, 'id) t) * bool
(**
Returns a tuple `((smaller, larger), present)`, `present` is true when element exist in set.
```res example
let s0 = Belt.Set.fromArray([1,2,3,4,5], ~id=module(IntCmp))
let ((smaller, larger), present) = s0->Belt.Set.split(3)
present /* true */
smaller->Belt.Set.toArray /* [1,2] */
larger->Belt.Set.toArray /* [4,5] */
```
*)
(**/**)
val checkInvariantInternal: _ t -> unit
(**
**raise** when invariant is not held
*)
(**/**)
(****************************************************************************)
(** Below are operations only when better performance needed,
it is still safe API but more verbose.
More API will be exposed by needs
*)
val getData: ('value, 'id) t -> ('value, 'id) Belt_SetDict.t
(**
**Advanced usage only**
Returns the raw data (detached from comparator), but its type is still manifested, so that user can pass identity directly without boxing.
*)
val getId: ('value, 'id) t -> ('value, 'id) id
(**
**Advanced usage only**
Returns the identity of set.
*)
val packIdData: id:('value, 'id) id -> data:('value, 'id) Belt_SetDict.t -> ('value, 'id) t
(**
**Advanced usage only**
Returns the packed collection.
*)