@@ -76,6 +76,8 @@ module String = Belt_MapString
76
76
(* * This module seprate identity from data, it is a bit more verbsoe but slightly
77
77
more efficient due to the fact that there is no need to pack identity and data back
78
78
after each operation
79
+
80
+ {b Advanced usage only}
79
81
*)
80
82
module Dict = Belt_MapDict
81
83
@@ -120,16 +122,29 @@ val make: id:('k, 'id) id -> ('k, 'a, 'id) t
120
122
(* * [make ~id]
121
123
122
124
@example {[
123
- module IntCmp = (val IntCmp.comparable ~cmp:(fun (x:int) y -> Pervasives.comapre x y))
125
+ module IntCmp = (val IntCmp.comparable ~cmp:(fun (x:int) y -> Pervasives.comapre x y));;
124
126
let s = make ~id:(module IntCmp)
125
127
]}
126
128
127
129
*)
128
130
129
131
130
132
val isEmpty : _ t -> bool
133
+ (* * [isEmpty s0]
134
+ @example {[
135
+ module IntCmp = (val IntCmp.comparable ~cmp:(fun (x:int) y -> Pervasives.comapre x y));;
136
+ isEmpty (ofArray [|1,"1"|] ~id:(module IntCmp)) = false;;
137
+ ]}
138
+ *)
131
139
132
- val has : ('k , 'a , 'id ) t -> 'k -> bool
140
+ val has : ('k , 'a , 'id ) t -> 'k -> bool
141
+ (* * [has s k]
142
+
143
+ @example {[
144
+ module IntCmp = (val IntCmp.comparable ~cmp:(fun (x:int) y -> Pervasives.comapre x y));;
145
+ has (ofArray [|1,"1"|] ~id:(module IntCmp)) 1 = true;;
146
+ ]}
147
+ *)
133
148
134
149
val cmpU :
135
150
('k , 'v , 'id ) t ->
@@ -141,6 +156,12 @@ val cmp:
141
156
('k , 'v , 'id ) t ->
142
157
('v -> 'v -> int ) ->
143
158
int
159
+ (* * [cmp s0 s1 vcmp]
160
+
161
+ Totoal ordering of map given total ordering of value function.
162
+
163
+ It will compare size first and each element following the order one by one.
164
+ *)
144
165
145
166
val eqU :
146
167
('k , 'a , 'id ) t ->
@@ -152,23 +173,44 @@ val eq:
152
173
('k , 'a , 'id ) t ->
153
174
('a -> 'a -> bool ) ->
154
175
bool
155
- (* * [eq m1 m2 cmp ] tests whether the maps [m1] and [m2] are
176
+ (* * [eq m1 m2 veq ] tests whether the maps [m1] and [m2] are
156
177
equal, that is, contain equal keys and associate them with
157
- equal data. [cmp ] is the equality predicate used to compare
178
+ equal data. [veq ] is the equality predicate used to compare
158
179
the data associated with the keys. *)
159
180
160
181
val forEachU : ('k , 'a , 'id ) t -> ('k -> 'a -> unit [@ bs]) -> unit
161
182
val forEach : ('k , 'a , 'id ) t -> ('k -> 'a -> unit ) -> unit
162
183
(* * [forEach m f] applies [f] to all bindings in map [m].
163
184
[f] receives the 'k as first argument, and the associated value
164
185
as second argument. The bindings are passed to [f] in increasing
165
- order with respect to the ordering over the type of the keys. *)
186
+ order with respect to the ordering over the type of the keys.
187
+
188
+ @example {[
189
+ module IntCmp =
190
+ (val IntCmp.comparableU ~cmp:(fun[\@bs] (x:int) y -> Pervasives.comapre x y));;
191
+
192
+ let s0 = ofArray ~id:(module IntCmp) [|4,"4";1,"1";2,"2,"3""|];;
193
+ let acc = ref [] ;;
194
+ forEach s0 (fun k v -> acc := (k,v) :: !acc);;
195
+
196
+ !acc = [4,"4"; 3,"3"; 2,"2"; 1,"1"]
197
+ ]}
198
+ *)
166
199
167
200
val reduceU : ('k , 'a , 'id ) t -> 'b -> ('b -> 'k -> 'a -> 'b [@ bs]) -> 'b
168
- val reduce : ('k , 'a , 'id ) t -> 'b -> ('b -> 'k -> 'a -> 'b ) -> 'b
201
+ val reduce : ('k , 'a , 'id ) t -> 'acc -> ('acc -> 'k -> 'a -> 'acc ) -> 'acc
169
202
(* * [reduce m a f] computes [(f kN dN ... (f k1 d1 a)...)],
170
203
where [k1 ... kN] are the keys of all bindings in [m]
171
- (in increasing order), and [d1 ... dN] are the associated data. *)
204
+ (in increasing order), and [d1 ... dN] are the associated data.
205
+
206
+ @example {[
207
+ module IntCmp =
208
+ (val IntCmp.comparableU ~cmp:(fun[\@bs] (x:int) y -> Pervasives.comapre x y));;
209
+
210
+ let s0 = ofArray ~id:(module IntCmp) [|4,"4";1,"1";2,"2,"3""|];;
211
+ reduce s0 [] (fun acc k v -> (k,v) acc ) = [4,"4";3,"3";2,"2";1,"1"];;
212
+ ]}
213
+ *)
172
214
173
215
val everyU : ('k , 'a , 'id ) t -> ('k -> 'a -> bool [@ bs]) -> bool
174
216
val every : ('k , 'a , 'id ) t -> ('k -> 'a -> bool ) -> bool
@@ -181,45 +223,192 @@ val some: ('k, 'a, 'id) t -> ('k -> 'a -> bool) -> bool
181
223
satisfy the predicate [p]. Order unspecified *)
182
224
183
225
val size : ('k , 'a , 'id ) t -> int
184
- val toList : ('k , 'a , 'id ) t -> ('k * 'a ) list
185
- (* * In increasing order*)
226
+ (* * [size s]
227
+
228
+ @example {[
229
+ module IntCmp =
230
+ (val IntCmp.comparableU ~cmp:(fun[\@bs] (x:int) y -> Pervasives.comapre x y));;
231
+ size (ofArray [2,"2"; 2,"1"; 3,"3"] ~id:(module IntCmp)) = 2 ;;
232
+ ]}
233
+ *)
186
234
val toArray : ('k , 'a , 'id ) t -> ('k * 'a ) array
187
- val ofArray : ('k * 'a ) array -> id :('k ,'id ) id -> ('k ,'a,'id) t
235
+ (* * [toArray s]
236
+
237
+ @example {[
238
+ module IntCmp =
239
+ (val IntCmp.comparableU ~cmp:(fun[\@bs] (x:int) y -> Pervasives.comapre x y));;
240
+ toArray (ofArray [2,"2"; 1,"1"; 3,"3"] ~id:(module IntCmp)) = [1,"1";2,"2";3,"3"]
241
+ ]}
242
+
243
+ *)
244
+ val toList : ('k , 'a , 'id ) t -> ('k * 'a ) list
245
+ (* * In increasing order
246
+
247
+ {b See} {!toArray}
248
+ *)
249
+
250
+ val ofArray : ('k * 'a ) array -> id :('k ,'id ) id -> ('k ,'a,'id) t
251
+ (* * [ofArray kvs ~id]
252
+ @example {[
253
+ module IntCmp =
254
+ (val IntCmp.comparableU ~cmp:(fun[\@bs] (x:int) y -> Pervasives.comapre x y));;
255
+ toArray (ofArray [2,"2"; 1,"1"; 3,"3"] ~id:(module IntCmp)) = [1,"1";2,"2";3,"3"]
256
+ ]}
257
+ *)
188
258
val keysToArray : ('k , 'a , 'id ) t -> 'k array
259
+ (* * [keysToArray s]
260
+
261
+ @example {[
262
+ module IntCmp =
263
+ (val IntCmp.comparableU ~cmp:(fun[\@bs] (x:int) y -> Pervasives.comapre x y));;
264
+ keysToArray (ofArray [2,"2"; 1,"1"; 3,"3"] ~id:(module IntCmp)) =
265
+ [|1;2;3|];;
266
+ ]}
267
+ *)
189
268
val valuesToArray : ('k , 'a , 'id ) t -> 'a array
269
+ (* * [valuesToArray s]
270
+
271
+ @example {[
272
+ module IntCmp =
273
+ (val IntCmp.comparableU ~cmp:(fun[\@bs] (x:int) y -> Pervasives.comapre x y));;
274
+ valuesToArray (ofArray [2,"2"; 1,"1"; 3,"3"] ~id:(module IntCmp)) =
275
+ [|"1";"2";"3"|];;
276
+ ]}
277
+
278
+ *)
279
+
190
280
val minKey : ('k , _ , _ ) t -> 'k option
281
+ (* * [minKey s]
282
+ @return thte minimum key, None if not exist
283
+ *)
284
+
191
285
val minKeyUndefined : ('k , _ , _ ) t -> 'k Js .undefined
286
+ (* * {b See} {!minKey}*)
287
+
192
288
val maxKey : ('k , _ , _ ) t -> 'k option
289
+ (* * [maxKey s]
290
+ @return thte maximum key, None if not exist
291
+ *)
292
+
193
293
val maxKeyUndefined : ('k , _ , _ ) t -> 'k Js .undefined
294
+ (* * {b See} {!maxKey} *)
295
+
194
296
val minimum : ('k , 'a , _ ) t -> ('k * 'a ) option
297
+ (* * [minimum s]
298
+ @return thte minimum key value pair, None if not exist
299
+ *)
300
+
195
301
val minUndefined : ('k , 'a , _ ) t -> ('k * 'a ) Js .undefined
302
+ (* * {b See} {!minimum} *)
303
+
196
304
val maximum : ('k , 'a , _ ) t -> ('k * 'a ) option
305
+ (* * [maximum s]
306
+ @return thte maximum key value pair, None if not exist
307
+ *)
308
+
197
309
val maxUndefined :('k , 'a , _ ) t -> ('k * 'a ) Js .undefined
310
+ (* * {b See} {!maximum}
311
+ *)
312
+
198
313
val get : ('k , 'a , 'id ) t -> 'k -> 'a option
314
+ (* * [get s k]
315
+
316
+ @example {[
317
+ module IntCmp =
318
+ (val IntCmp.comparableU ~cmp:(fun[\@bs] (x:int) y -> Pervasives.comapre x y));;
319
+ get (ofArray [2,"2"; 1,"1"; 3,"3"] ~id:(module IntCmp)) 2 =
320
+ Some "2";;
321
+ get (ofArray [2,"2"; 1,"1"; 3,"3"] ~id:(module IntCmp)) 2 =
322
+ None;;
323
+ ]}
324
+ *)
325
+
199
326
val getUndefined : ('k , 'a , 'id ) t -> 'k -> 'a Js .undefined
327
+ (* * {b See} {!get}
328
+
329
+ @return [undefined] when not found
330
+ *)
200
331
val getWithDefault :
201
- ('k , 'a , 'id ) t -> 'k -> 'a -> 'a
332
+ ('k , 'a , 'id ) t -> 'k -> 'a -> 'a
333
+ (* * [getWithDefault s k default]
334
+
335
+ {b See} {!get}
336
+
337
+ @return [default] when [k] is not found
338
+
339
+ *)
202
340
val getExn : ('k , 'a , 'id ) t -> 'k -> 'a
203
- val checkInvariantInternal : _ t -> unit
204
- (* *
205
- {b raise} when invariant is not helld
206
- *)
207
-
341
+ (* * [getExn s k]
342
+
343
+ {b See} {!getExn}
344
+
345
+ {b raise} when [k] not exist
346
+ *)
347
+
208
348
(* ***************************************************************************)
209
349
210
350
val remove : ('k , 'a , 'id ) t -> 'k -> ('k , 'a , 'id ) t
211
- (* * [remove m x] when [x] is not in [m], [m] is returned reference unchanged *)
351
+ (* * [remove m x] when [x] is not in [m], [m] is returned reference unchanged.
352
+
353
+ @example {[
354
+ module IntCmp =
355
+ (val IntCmp.comparableU ~cmp:(fun[\@bs] (x:int) y -> Pervasives.comapre x y));;
356
+
357
+ let s0 = (ofArray [2,"2"; 1,"1"; 3,"3"] ~id:(module IntCmp));;
358
+
359
+ let s1 = remove s0 1;;
360
+ let s2 = remove s1 1;;
361
+ s1 == s2 ;;
362
+ keysToArray s1 = [|2;3|];;
363
+ ]}
364
+
365
+ *)
366
+
212
367
val removeMany : ('k , 'a , 'id ) t -> 'k array -> ('k , 'a , 'id ) t
368
+ (* * [removeMany s xs]
369
+
370
+ Removing each of [xs] to [s], note unlike {!remove},
371
+ the reference of return value might be changed even if none in [xs]
372
+ exists [s]
373
+ *)
213
374
214
375
val set :
215
376
('k , 'a , 'id ) t -> 'k -> 'a -> ('k , 'a , 'id ) t
216
377
(* * [set m x y ] returns a map containing the same bindings as
217
378
[m], with a new binding of [x] to [y]. If [x] was already bound
218
- in [m], its previous binding disappears. *)
379
+ in [m], its previous binding disappears.
380
+
381
+ @example {[
382
+ module IntCmp =
383
+ (val IntCmp.comparableU ~cmp:(fun[\@bs] (x:int) y -> Pervasives.comapre x y));;
384
+
385
+ let s0 = (ofArray [2,"2"; 1,"1"; 3,"3"] ~id:(module IntCmp));;
386
+
387
+ let s1 = set s0 2 "3";;
388
+
389
+ valuesToArray s1 = ["1";"3";"3"];;
390
+ ]}
391
+ *)
392
+
219
393
val updateU : ('k , 'a , 'id ) t -> 'k -> ('a option -> 'a option [@ bs]) -> ('k , 'a , 'id ) t
220
394
val update : ('k , 'a , 'id ) t -> 'k -> ('a option -> 'a option ) -> ('k , 'a , 'id ) t
395
+ (* * [update m x f] returns a map containing the same bindings as
396
+ [m], except for the binding of [x].
397
+ Depending on the value of
398
+ [y] where [y] is [f (get x m)], the binding of [x] is
399
+ added, removed or updated. If [y] is [None], the binding is
400
+ removed if it exists; otherwise, if [y] is [Some z] then [x]
401
+ is associated to [z] in the resulting map.
402
+ *)
403
+
221
404
val mergeMany :
222
405
('k , 'a , 'id ) t -> ('k * 'a ) array -> ('k , 'a , 'id ) t
406
+ (* * [mergeMany s xs]
407
+
408
+ Adding each of [xs] to [s], note unlike {!add},
409
+ the reference of return value might be changed even if all values in [xs]
410
+ exist [s]
411
+ *)
223
412
224
413
val mergeU :
225
414
('k , 'a , 'id ) t ->
@@ -265,7 +454,7 @@ val partition:
265
454
val split :
266
455
('k , 'a , 'id ) t -> 'k ->
267
456
(('k , 'a , 'id ) t * ('k , 'a , 'id ) t )* 'a option
268
- (* * [split x m] returns a triple [(l, data, r) ], where
457
+ (* * [split x m] returns a tuple [(l r), data ], where
269
458
[l] is the map with all the bindings of [m] whose 'k
270
459
is strictly less than [x];
271
460
[r] is the map with all the bindings of [m] whose 'k
@@ -284,10 +473,44 @@ val map: ('k, 'a, 'id) t -> ('a -> 'b) -> ('k ,'b,'id ) t
284
473
285
474
val mapWithKeyU : ('k , 'a , 'id ) t -> ('k -> 'a -> 'b [@ bs]) -> ('k , 'b , 'id ) t
286
475
val mapWithKey : ('k , 'a , 'id ) t -> ('k -> 'a -> 'b ) -> ('k , 'b , 'id ) t
476
+ (* * [mapWithKey m f]
477
+
478
+ The same as {!map} except that [f] is supplied with one more argument: the key
479
+
480
+
481
+ *)
482
+
287
483
288
- val getId : ('a , 'b , 'c ) t -> ('a , 'c ) id
289
484
290
485
val getData : ('a , 'b , 'c ) t -> ('a , 'b , 'c ) Belt_MapDict .t
486
+ (* * [getData s0]
487
+
488
+ {b Advanced usage only}
489
+
490
+ @return the raw data (detached from comparator),
491
+ but its type is still manifested, so that user can pass identity directly
492
+ without boxing
493
+ *)
494
+
495
+ val getId : ('a , 'b , 'c ) t -> ('a , 'c ) id
496
+ (* * [getId s0]
497
+
498
+ {b Advanced usage only}
291
499
500
+ @return the identity of [s0]
501
+ *)
502
+
292
503
val packIdData : id :('a , 'b ) id -> data :('a , 'c , 'b ) Belt_MapDict .t -> ('a , 'c , 'b ) t
504
+ (* * [packIdData ~id ~data]
293
505
506
+ {b Advanced usage only}
507
+
508
+ @return the packed collection
509
+ *)
510
+
511
+ (* */**)
512
+ val checkInvariantInternal : _ t -> unit
513
+ (* *
514
+ {b raise} when invariant is not helld
515
+ *)
516
+ (* */**)
0 commit comments