-
Notifications
You must be signed in to change notification settings - Fork 455
/
Copy pathBelt_HashMap.resi
479 lines (368 loc) · 11.4 KB
/
Belt_HashMap.resi
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
/* Copyright (C) 2018 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. */
/***
A **mutable** Hash map which allows customized [`hash`]() behavior.
All data are parameterized by not its only type but also a unique identity in
the time of initialization, so that two _HashMaps of ints_ initialized with different
_hash_ functions will have different type.
## Examples
```rescript
type t = int
module I0 = unpack(Belt.Id.hashable(~hash=(_: t) => 0xff_ff, ~eq=(a, b) => a == b))
let s0: Belt.HashMap.t<I0.t, int, I0.identity> = Belt.HashMap.make(~hintSize=40, ~id=module(I0))
module I1 = unpack(Belt.Id.hashable(~hash=(_: t) => 0xff, ~eq=(a, b) => a == b))
let s1: Belt.HashMap.t<I1.t, string, I1.identity> = Belt.HashMap.make(~hintSize=40, ~id=module(I1))
```
The invariant must be held: for two elements who are _equal_,
their hashed value should be the same
Here the compiler would infer `s0` and `s1` having different type so that
it would not mix.
## Examples
```
let s0: t<int, I0.identity>
let s1: t<int, I1.identity>
```
We can add elements to the collection:
## Examples
```rescript
let () = {
Belt.HashMap.set(s0, 0, 3)
Belt.HashMap.set(s1, 1, "3")
}
```
Since this is an mutable data strucure, `s1` will contain two pairs.
*/
/** Specalized when key type is `int`, more efficient than the generic type */
module Int = Belt_HashMapInt
/** Specalized when key type is `string`, more efficient than the generic type */
module String = Belt_HashMapString
/** The type of hash tables from type `'key` to type `'value`. */
type t<'key, 'value, 'id>
/** The identity needed for making an empty hash map. */
type id<'a, 'id> = Belt_Id.hashable<'a, 'id>
/**
`make(~hintSize=10, ~id)` creates a new map by taking in the comparator and `hintSize`.
## Examples
```rescript
module IntHash = Belt.Id.MakeHashable({
type t = int
let hash = a => a
let eq = (a, b) => a == b
})
let hMap = Belt.HashMap.make(~hintSize=10, ~id=module(IntHash))
Belt.HashMap.set(hMap, 0, "a")
```
*/
let make: (~hintSize: int, ~id: id<'key, 'id>) => t<'key, 'value, 'id>
/* TODO: allow randomization for security */
/**
Clears a hash table.
## Examples
```rescript
module IntHash = Belt.Id.MakeHashable({
type t = int
let hash = a => a
let eq = (a, b) => a == b
})
let hMap = Belt.HashMap.fromArray([(1, "1")], ~id=module(IntHash))
Belt.HashMap.clear(hMap)
Belt.HashMap.isEmpty(hMap) == true
```
*/
let clear: t<'key, 'value, 'id> => unit
/**
`isEmpty(m)` checks whether a hash map is empty.
## Examples
```rescript
module IntHash = Belt.Id.MakeHashable({
type t = int
let hash = a => a
let eq = (a, b) => a == b
})
Belt.HashMap.isEmpty(Belt.HashMap.fromArray([(1, "1")], ~id=module(IntHash))) == false
```
*/
let isEmpty: t<_> => bool
/**
`set(hMap, k, v)` if `k` does not exist, add the binding `k,v`, otherwise, update the old value with the new `v`.
## Examples
```rescript
module IntHash = Belt.Id.MakeHashable({
type t = int
let hash = a => a
let eq = (a, b) => a == b
})
let s0 = Belt.HashMap.fromArray([(2, "2"), (1, "1"), (3, "3")], ~id=module(IntHash))
Belt.HashMap.set(s0, 2, "3")
Belt.HashMap.valuesToArray(s0) == ["1", "3", "3"]
```
*/
let set: (t<'key, 'value, 'id>, 'key, 'value) => unit
/**
Creates copy of a hash map.
## Examples
```rescript
module IntHash = Belt.Id.MakeHashable({
type t = int
let hash = a => a
let eq = (a, b) => a == b
})
let s0 = Belt.HashMap.fromArray([(2, "2"), (1, "1"), (3, "3")], ~id=module(IntHash))
let s1 = Belt.HashMap.copy(s0)
Belt.HashMap.set(s0, 2, "3")
Belt.HashMap.get(s0, 2) != Belt.HashMap.get(s1, 2)
```
*/
let copy: t<'key, 'value, 'id> => t<'key, 'value, 'id>
/**
Returns value bound under specific key. If values not exist returns `None`.
## Examples
```rescript
module IntHash = Belt.Id.MakeHashable({
type t = int
let hash = a => a
let eq = (a, b) => a == b
})
let s0 = Belt.HashMap.make(~hintSize=10, ~id=module(IntHash))
Belt.HashMap.set(s0, 1, "value1")
Belt.HashMap.get(s0, 1) == Some("value1")
Belt.HashMap.get(s0, 2) == None
```
*/
let get: (t<'key, 'value, 'id>, 'key) => option<'value>
/**
Checks if `x` is bound in `tbl`.
## Examples
```rescript
module IntHash = Belt.Id.MakeHashable({
type t = int
let hash = a => a
let eq = (a, b) => a == b
})
let s0 = Belt.HashMap.make(~hintSize=10, ~id=module(IntHash))
Belt.HashMap.set(s0, 1, "value1")
Belt.HashMap.has(s0, 1) == true
Belt.HashMap.has(s0, 2) == false
```
*/
let has: (t<'key, 'value, 'id>, 'key) => bool
/**
If bound exists, removes it from the hash map.
## Examples
```rescript
module IntHash = Belt.Id.MakeHashable({
type t = int
let hash = a => a
let eq = (a, b) => a == b
})
let s0 = Belt.HashMap.make(~hintSize=10, ~id=module(IntHash))
Belt.HashMap.set(s0, 1, "value1")
Belt.HashMap.remove(s0, 1)
Belt.HashMap.has(s0, 1) == false
```
*/
let remove: (t<'key, 'value, 'id>, 'key) => unit
/** Same as [forEach](#forEach) but takes uncurried function. */
@deprecated("Use `forEach` instead")
let forEachU: (t<'key, 'value, 'id>, ('key, 'value) => unit) => unit
/**
`forEach(tbl, f)` applies `f` to all bindings in table `tbl`. `f` receives the key as first argument, and the associated value as second argument. Each binding is presented exactly once to `f`.
## Examples
```rescript
module IntHash = Belt.Id.MakeHashable({
type t = int
let hash = a => a
let eq = (a, b) => a == b
})
let s0 = Belt.HashMap.make(~hintSize=10, ~id=module(IntHash))
Belt.HashMap.set(s0, 1, "value1")
Belt.HashMap.forEach(s0, (key, value) => Js.log2(key, value))
// prints (1, "value1")
```
*/
let forEach: (t<'key, 'value, 'id>, ('key, 'value) => unit) => unit
@deprecated("Use `reduce` instead")
let reduceU: (t<'key, 'value, 'id>, 'c, ('c, 'key, 'value) => 'c) => 'c
/**
`reduce(tbl, init, f)` computes `(f(kN, dN) ... (f(k1, d1, init))...)`, where `k1 ... kN` are the keys of all bindings in `tbl`, and `d1 ... dN` are the associated values. Each binding is presented exactly once to `f`.
The order in which the bindings are passed to `f` is unspecified. However, if the table contains several bindings for the same key, they are passed to `f` in reverse order of introduction, that is, the most recent binding is passed first.
## Examples
```rescript
module IntHash = Belt.Id.MakeHashable({
type t = int
let hash = a => a
let eq = (a, b) => a == b
})
let s0 = Belt.HashMap.make(~hintSize=10, ~id=module(IntHash))
Belt.HashMap.set(s0, 1, "value1")
Belt.HashMap.set(s0, 2, "value2")
s0
->Belt.HashMap.reduce("", (acc, _, value) => acc ++ (", " ++ value))
->assertEqual(", value1, value2")
```
## More Examples
```rescript
Console.log("lol")
```
*/
let reduce: (t<'key, 'value, 'id>, 'c, ('c, 'key, 'value) => 'c) => 'c
/** Same as [keepMapInPlace](#keepMapInPlace) but takes uncurried function. */
@deprecated("Use `keepMapInPlace` instead")
let keepMapInPlaceU: (t<'key, 'value, 'id>, ('key, 'value) => option<'value>) => unit
/**
Filters out values for which function `f` returned `None`.
## Examples
```rescript
module IntHash = Belt.Id.MakeHashable({
type t = int
let hash = a => a
let eq = (a, b) => a == b
})
let s0 = Belt.HashMap.make(~hintSize=10, ~id=module(IntHash))
Belt.HashMap.set(s0, 1, "value1")
Belt.HashMap.set(s0, 2, "value2")
Belt.HashMap.keepMapInPlace(s0, (key, value) => key == 1 ? None : Some(value))
```
*/
let keepMapInPlace: (t<'key, 'value, 'id>, ('key, 'value) => option<'value>) => unit
/**
`size(tbl)` returns the number of bindings in `tbl`. It takes constant time.
## Examples
```rescript
module IntHash = Belt.Id.MakeHashable({
type t = int
let hash = a => a
let eq = (a, b) => a == b
})
let s0 = Belt.HashMap.make(~hintSize=10, ~id=module(IntHash))
Belt.HashMap.set(s0, 1, "value1")
Belt.HashMap.set(s0, 2, "value2")
Belt.HashMap.size(s0) == 2
```
*/
let size: t<_> => int
/**
Returns array of key value pairs.
## Examples
```rescript
module IntHash = Belt.Id.MakeHashable({
type t = int
let hash = a => a
let eq = (a, b) => a == b
})
let s0 = Belt.HashMap.make(~hintSize=10, ~id=module(IntHash))
Belt.HashMap.set(s0, 1, "value1")
Belt.HashMap.set(s0, 2, "value2")
Belt.HashMap.toArray(s0) == [(1, "value1"), (2, "value2")]
```
*/
let toArray: t<'key, 'value, 'id> => array<('key, 'value)>
/**
Returns array of keys.
## Examples
```rescript
module IntHash = Belt.Id.MakeHashable({
type t = int
let hash = a => a
let eq = (a, b) => a == b
})
let s0 = Belt.HashMap.make(~hintSize=10, ~id=module(IntHash))
Belt.HashMap.set(s0, 1, "value1")
Belt.HashMap.set(s0, 2, "value2")
Belt.HashMap.keysToArray(s0) == [1, 2]
```
*/
let keysToArray: t<'key, _, _> => array<'key>
/**
Returns array of values.
## Examples
```rescript
module IntHash = Belt.Id.MakeHashable({
type t = int
let hash = a => a
let eq = (a, b) => a == b
})
let s0 = Belt.HashMap.make(~hintSize=10, ~id=module(IntHash))
Belt.HashMap.set(s0, 1, "value1")
Belt.HashMap.set(s0, 2, "value2")
Belt.HashMap.valuesToArray(s0) == ["value1", "value2"]
```
*/
let valuesToArray: t<_, 'value, _> => array<'value>
/**
Creates new hash map from array of pairs.
Returns array of values.
## Examples
```rescript
module IntHash = Belt.Id.MakeHashable({
type t = int
let hash = a => a
let eq = (a, b) => a == b
})
let s0 = Belt.HashMap.fromArray([(1, "value1"), (2, "value2")], ~id=module(IntHash))
Belt.HashMap.toArray(s0) == [(1, "value1"), (2, "value2")]
```
*/
let fromArray: (array<('key, 'value)>, ~id: id<'key, 'id>) => t<'key, 'value, 'id>
/**
## Examples
```rescript
module IntHash = Belt.Id.MakeHashable({
type t = int
let hash = a => a
let eq = (a, b) => a == b
})
let hMap = Belt.HashMap.make(~hintSize=10, ~id=module(IntHash))
Belt.HashMap.mergeMany(hMap, [(1, "1"), (2, "2")])
```
*/
let mergeMany: (t<'key, 'value, 'id>, array<('key, 'value)>) => unit
/**
## Examples
```rescript
module IntHash = Belt.Id.MakeHashable({
type t = int
let hash = a => a
let eq = (a, b) => a == b
})
let hMap = Belt.HashMap.make(~hintSize=10, ~id=module(IntHash))
Belt.HashMap.set(hMap, 1, "1")
Belt.HashMap.getBucketHistogram(hMap)
```
*/
let getBucketHistogram: t<_> => array<int>
/**
## Examples
```rescript
module IntHash = Belt.Id.MakeHashable({
type t = int
let hash = a => a
let eq = (a, b) => a == b
})
let hMap = Belt.HashMap.make(~hintSize=10, ~id=module(IntHash))
Belt.HashMap.set(hMap, 1, "1")
Belt.HashMap.logStats(hMap)
```
*/
let logStats: t<_> => unit