@grain/stdlib 0.5.13 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +193 -0
- package/LICENSE +1 -1
- package/README.md +25 -2
- package/array.gr +1512 -199
- package/array.md +2032 -94
- package/bigint.gr +239 -140
- package/bigint.md +450 -106
- package/buffer.gr +595 -102
- package/buffer.md +903 -145
- package/bytes.gr +401 -110
- package/bytes.md +551 -63
- package/char.gr +228 -49
- package/char.md +373 -7
- package/exception.gr +26 -12
- package/exception.md +29 -5
- package/float32.gr +130 -109
- package/float32.md +185 -57
- package/float64.gr +112 -99
- package/float64.md +185 -57
- package/hash.gr +47 -37
- package/hash.md +21 -3
- package/int16.gr +430 -0
- package/int16.md +618 -0
- package/int32.gr +200 -269
- package/int32.md +254 -289
- package/int64.gr +142 -225
- package/int64.md +254 -289
- package/int8.gr +511 -0
- package/int8.md +786 -0
- package/json.gr +2084 -0
- package/json.md +608 -0
- package/list.gr +120 -68
- package/list.md +125 -80
- package/map.gr +560 -57
- package/map.md +672 -56
- package/marshal.gr +239 -227
- package/marshal.md +36 -4
- package/number.gr +626 -676
- package/number.md +738 -153
- package/option.gr +33 -35
- package/option.md +58 -42
- package/package.json +2 -2
- package/path.gr +148 -187
- package/path.md +47 -96
- package/pervasives.gr +75 -416
- package/pervasives.md +85 -180
- package/priorityqueue.gr +433 -74
- package/priorityqueue.md +422 -54
- package/queue.gr +362 -80
- package/queue.md +433 -38
- package/random.gr +67 -75
- package/random.md +68 -40
- package/range.gr +135 -63
- package/range.md +198 -43
- package/rational.gr +284 -0
- package/rational.md +545 -0
- package/regex.gr +933 -1066
- package/regex.md +59 -60
- package/result.gr +23 -25
- package/result.md +54 -39
- package/runtime/atof/common.gr +78 -82
- package/runtime/atof/common.md +22 -10
- package/runtime/atof/decimal.gr +102 -127
- package/runtime/atof/decimal.md +28 -7
- package/runtime/atof/lemire.gr +56 -71
- package/runtime/atof/lemire.md +9 -1
- package/runtime/atof/parse.gr +83 -110
- package/runtime/atof/parse.md +12 -2
- package/runtime/atof/slow.gr +28 -35
- package/runtime/atof/slow.md +9 -1
- package/runtime/atof/table.gr +19 -18
- package/runtime/atof/table.md +10 -2
- package/runtime/atoi/parse.gr +153 -136
- package/runtime/atoi/parse.md +50 -1
- package/runtime/bigint.gr +410 -517
- package/runtime/bigint.md +71 -57
- package/runtime/compare.gr +176 -85
- package/runtime/compare.md +31 -1
- package/runtime/dataStructures.gr +144 -32
- package/runtime/dataStructures.md +267 -31
- package/runtime/debugPrint.gr +34 -15
- package/runtime/debugPrint.md +37 -5
- package/runtime/equal.gr +53 -52
- package/runtime/equal.md +30 -1
- package/runtime/exception.gr +38 -47
- package/runtime/exception.md +10 -8
- package/runtime/gc.gr +23 -152
- package/runtime/gc.md +13 -17
- package/runtime/malloc.gr +31 -31
- package/runtime/malloc.md +11 -3
- package/runtime/numberUtils.gr +191 -172
- package/runtime/numberUtils.md +17 -9
- package/runtime/numbers.gr +1695 -1021
- package/runtime/numbers.md +1098 -134
- package/runtime/string.gr +540 -242
- package/runtime/string.md +76 -6
- package/runtime/unsafe/constants.gr +30 -13
- package/runtime/unsafe/constants.md +80 -0
- package/runtime/unsafe/conv.gr +55 -28
- package/runtime/unsafe/conv.md +41 -9
- package/runtime/unsafe/memory.gr +10 -30
- package/runtime/unsafe/memory.md +15 -19
- package/runtime/unsafe/tags.gr +37 -21
- package/runtime/unsafe/tags.md +88 -8
- package/runtime/unsafe/wasmf32.gr +30 -36
- package/runtime/unsafe/wasmf32.md +64 -56
- package/runtime/unsafe/wasmf64.gr +30 -36
- package/runtime/unsafe/wasmf64.md +64 -56
- package/runtime/unsafe/wasmi32.gr +49 -66
- package/runtime/unsafe/wasmi32.md +102 -94
- package/runtime/unsafe/wasmi64.gr +52 -79
- package/runtime/unsafe/wasmi64.md +108 -100
- package/runtime/utils/printing.gr +13 -15
- package/runtime/utils/printing.md +11 -3
- package/runtime/wasi.gr +294 -295
- package/runtime/wasi.md +62 -42
- package/set.gr +574 -64
- package/set.md +634 -54
- package/stack.gr +181 -64
- package/stack.md +271 -42
- package/string.gr +453 -533
- package/string.md +241 -151
- package/uint16.gr +369 -0
- package/uint16.md +585 -0
- package/uint32.gr +470 -0
- package/uint32.md +737 -0
- package/uint64.gr +471 -0
- package/uint64.md +737 -0
- package/uint8.gr +369 -0
- package/uint8.md +585 -0
- package/uri.gr +1093 -0
- package/uri.md +477 -0
- package/{sys → wasi}/file.gr +914 -500
- package/{sys → wasi}/file.md +454 -50
- package/wasi/process.gr +292 -0
- package/{sys → wasi}/process.md +164 -6
- package/wasi/random.gr +77 -0
- package/wasi/random.md +80 -0
- package/{sys → wasi}/time.gr +15 -22
- package/{sys → wasi}/time.md +5 -5
- package/immutablearray.gr +0 -929
- package/immutablearray.md +0 -1038
- package/immutablemap.gr +0 -493
- package/immutablemap.md +0 -479
- package/immutablepriorityqueue.gr +0 -360
- package/immutablepriorityqueue.md +0 -291
- package/immutableset.gr +0 -498
- package/immutableset.md +0 -449
- package/runtime/debug.gr +0 -2
- package/runtime/debug.md +0 -6
- package/runtime/unsafe/errors.gr +0 -36
- package/runtime/unsafe/errors.md +0 -204
- package/sys/process.gr +0 -254
- package/sys/random.gr +0 -79
- package/sys/random.md +0 -66
package/float64.md
CHANGED
|
@@ -10,12 +10,28 @@ No other changes yet.
|
|
|
10
10
|
</details>
|
|
11
11
|
|
|
12
12
|
```grain
|
|
13
|
-
|
|
13
|
+
from "float64" include Float64
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
```grain
|
|
17
|
+
5.0d
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
```grain
|
|
21
|
+
-5.0d
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```grain
|
|
25
|
+
Infinityd
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```grain
|
|
29
|
+
NaNd
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Values
|
|
17
33
|
|
|
18
|
-
Float64
|
|
34
|
+
Functions and constants included in the Float64 module.
|
|
19
35
|
|
|
20
36
|
### Float64.**infinity**
|
|
21
37
|
|
|
@@ -29,6 +45,7 @@ infinity : Float64
|
|
|
29
45
|
```
|
|
30
46
|
|
|
31
47
|
Infinity represented as a Float64 value.
|
|
48
|
+
This is an alternative to the `Infinityd` literal.
|
|
32
49
|
|
|
33
50
|
### Float64.**nan**
|
|
34
51
|
|
|
@@ -42,6 +59,7 @@ nan : Float64
|
|
|
42
59
|
```
|
|
43
60
|
|
|
44
61
|
NaN (Not a Number) represented as a Float64 value.
|
|
62
|
+
This is an alternative to the `NaNd` literal.
|
|
45
63
|
|
|
46
64
|
### Float64.**pi**
|
|
47
65
|
|
|
@@ -82,10 +100,6 @@ e : Float64
|
|
|
82
100
|
|
|
83
101
|
Euler's number represented as a Float64 value.
|
|
84
102
|
|
|
85
|
-
## Conversions
|
|
86
|
-
|
|
87
|
-
Functions for converting between Numbers and the Float64 type.
|
|
88
|
-
|
|
89
103
|
### Float64.**fromNumber**
|
|
90
104
|
|
|
91
105
|
<details disabled>
|
|
@@ -94,7 +108,7 @@ No other changes yet.
|
|
|
94
108
|
</details>
|
|
95
109
|
|
|
96
110
|
```grain
|
|
97
|
-
fromNumber : Number
|
|
111
|
+
fromNumber : (number: Number) => Float64
|
|
98
112
|
```
|
|
99
113
|
|
|
100
114
|
Converts a Number to a Float64.
|
|
@@ -119,7 +133,7 @@ No other changes yet.
|
|
|
119
133
|
</details>
|
|
120
134
|
|
|
121
135
|
```grain
|
|
122
|
-
toNumber : Float64
|
|
136
|
+
toNumber : (float: Float64) => Number
|
|
123
137
|
```
|
|
124
138
|
|
|
125
139
|
Converts a Float64 to a Number.
|
|
@@ -136,19 +150,22 @@ Returns:
|
|
|
136
150
|
|----|-----------|
|
|
137
151
|
|`Number`|The Float64 represented as a Number|
|
|
138
152
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
<
|
|
146
|
-
|
|
147
|
-
|
|
153
|
+
### Float64.**(+)**
|
|
154
|
+
|
|
155
|
+
<details>
|
|
156
|
+
<summary>Added in <code>0.6.0</code></summary>
|
|
157
|
+
<table>
|
|
158
|
+
<thead>
|
|
159
|
+
<tr><th>version</th><th>changes</th></tr>
|
|
160
|
+
</thead>
|
|
161
|
+
<tbody>
|
|
162
|
+
<tr><td><code>0.2.0</code></td><td>Originally named `add`</td></tr>
|
|
163
|
+
</tbody>
|
|
164
|
+
</table>
|
|
148
165
|
</details>
|
|
149
166
|
|
|
150
167
|
```grain
|
|
151
|
-
|
|
168
|
+
(+) : (x: Float64, y: Float64) => Float64
|
|
152
169
|
```
|
|
153
170
|
|
|
154
171
|
Computes the sum of its operands.
|
|
@@ -166,15 +183,29 @@ Returns:
|
|
|
166
183
|
|----|-----------|
|
|
167
184
|
|`Float64`|The sum of the two operands|
|
|
168
185
|
|
|
169
|
-
|
|
186
|
+
Examples:
|
|
170
187
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
188
|
+
```grain
|
|
189
|
+
use Float64.{ (+) }
|
|
190
|
+
assert 1.0d + 1.0d == 2.0d
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Float64.**(-)**
|
|
194
|
+
|
|
195
|
+
<details>
|
|
196
|
+
<summary>Added in <code>0.6.0</code></summary>
|
|
197
|
+
<table>
|
|
198
|
+
<thead>
|
|
199
|
+
<tr><th>version</th><th>changes</th></tr>
|
|
200
|
+
</thead>
|
|
201
|
+
<tbody>
|
|
202
|
+
<tr><td><code>0.2.0</code></td><td>Originally named `sub`</td></tr>
|
|
203
|
+
</tbody>
|
|
204
|
+
</table>
|
|
174
205
|
</details>
|
|
175
206
|
|
|
176
207
|
```grain
|
|
177
|
-
|
|
208
|
+
(-) : (x: Float64, y: Float64) => Float64
|
|
178
209
|
```
|
|
179
210
|
|
|
180
211
|
Computes the difference of its operands.
|
|
@@ -192,15 +223,29 @@ Returns:
|
|
|
192
223
|
|----|-----------|
|
|
193
224
|
|`Float64`|The difference of the two operands|
|
|
194
225
|
|
|
195
|
-
|
|
226
|
+
Examples:
|
|
196
227
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
228
|
+
```grain
|
|
229
|
+
use Float64.{ (-) }
|
|
230
|
+
assert 5.0d - 4.0d == 1.0d
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### Float64.**(*)**
|
|
234
|
+
|
|
235
|
+
<details>
|
|
236
|
+
<summary>Added in <code>0.6.0</code></summary>
|
|
237
|
+
<table>
|
|
238
|
+
<thead>
|
|
239
|
+
<tr><th>version</th><th>changes</th></tr>
|
|
240
|
+
</thead>
|
|
241
|
+
<tbody>
|
|
242
|
+
<tr><td><code>0.2.0</code></td><td>Originally named `mul`</td></tr>
|
|
243
|
+
</tbody>
|
|
244
|
+
</table>
|
|
200
245
|
</details>
|
|
201
246
|
|
|
202
247
|
```grain
|
|
203
|
-
|
|
248
|
+
(*) : (x: Float64, y: Float64) => Float64
|
|
204
249
|
```
|
|
205
250
|
|
|
206
251
|
Computes the product of its operands.
|
|
@@ -218,15 +263,29 @@ Returns:
|
|
|
218
263
|
|----|-----------|
|
|
219
264
|
|`Float64`|The product of the two operands|
|
|
220
265
|
|
|
221
|
-
|
|
266
|
+
Examples:
|
|
222
267
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
268
|
+
```grain
|
|
269
|
+
use Float64.{ (*) }
|
|
270
|
+
assert -5.0d * 4.0d == -20.0d
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
### Float64.**(/)**
|
|
274
|
+
|
|
275
|
+
<details>
|
|
276
|
+
<summary>Added in <code>0.6.0</code></summary>
|
|
277
|
+
<table>
|
|
278
|
+
<thead>
|
|
279
|
+
<tr><th>version</th><th>changes</th></tr>
|
|
280
|
+
</thead>
|
|
281
|
+
<tbody>
|
|
282
|
+
<tr><td><code>0.2.0</code></td><td>Originally named `div`</td></tr>
|
|
283
|
+
</tbody>
|
|
284
|
+
</table>
|
|
226
285
|
</details>
|
|
227
286
|
|
|
228
287
|
```grain
|
|
229
|
-
|
|
288
|
+
(/) : (x: Float64, y: Float64) => Float64
|
|
230
289
|
```
|
|
231
290
|
|
|
232
291
|
Computes the quotient of its operands.
|
|
@@ -244,19 +303,29 @@ Returns:
|
|
|
244
303
|
|----|-----------|
|
|
245
304
|
|`Float64`|The quotient of the two operands|
|
|
246
305
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
Functions for comparing Float64 values.
|
|
306
|
+
Examples:
|
|
250
307
|
|
|
251
|
-
|
|
308
|
+
```grain
|
|
309
|
+
use Float64.{ (/) }
|
|
310
|
+
assert 25.0d / 4.0d == 6.25d
|
|
311
|
+
```
|
|
252
312
|
|
|
253
|
-
<
|
|
254
|
-
|
|
255
|
-
|
|
313
|
+
### Float64.**(<)**
|
|
314
|
+
|
|
315
|
+
<details>
|
|
316
|
+
<summary>Added in <code>0.6.0</code></summary>
|
|
317
|
+
<table>
|
|
318
|
+
<thead>
|
|
319
|
+
<tr><th>version</th><th>changes</th></tr>
|
|
320
|
+
</thead>
|
|
321
|
+
<tbody>
|
|
322
|
+
<tr><td><code>0.2.0</code></td><td>Originally named `lt`</td></tr>
|
|
323
|
+
</tbody>
|
|
324
|
+
</table>
|
|
256
325
|
</details>
|
|
257
326
|
|
|
258
327
|
```grain
|
|
259
|
-
|
|
328
|
+
(<) : (x: Float64, y: Float64) => Bool
|
|
260
329
|
```
|
|
261
330
|
|
|
262
331
|
Checks if the first value is less than the second value.
|
|
@@ -274,15 +343,29 @@ Returns:
|
|
|
274
343
|
|----|-----------|
|
|
275
344
|
|`Bool`|`true` if the first value is less than the second value or `false` otherwise|
|
|
276
345
|
|
|
277
|
-
|
|
346
|
+
Examples:
|
|
278
347
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
348
|
+
```grain
|
|
349
|
+
use Float64.{ (<) }
|
|
350
|
+
assert -5.0d < 5.0d
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
### Float64.**(>)**
|
|
354
|
+
|
|
355
|
+
<details>
|
|
356
|
+
<summary>Added in <code>0.6.0</code></summary>
|
|
357
|
+
<table>
|
|
358
|
+
<thead>
|
|
359
|
+
<tr><th>version</th><th>changes</th></tr>
|
|
360
|
+
</thead>
|
|
361
|
+
<tbody>
|
|
362
|
+
<tr><td><code>0.2.0</code></td><td>Originally named `gt`</td></tr>
|
|
363
|
+
</tbody>
|
|
364
|
+
</table>
|
|
282
365
|
</details>
|
|
283
366
|
|
|
284
367
|
```grain
|
|
285
|
-
|
|
368
|
+
(>) : (x: Float64, y: Float64) => Bool
|
|
286
369
|
```
|
|
287
370
|
|
|
288
371
|
Checks if the first value is greater than the second value.
|
|
@@ -300,15 +383,29 @@ Returns:
|
|
|
300
383
|
|----|-----------|
|
|
301
384
|
|`Bool`|`true` if the first value is greater than the second value or `false` otherwise|
|
|
302
385
|
|
|
303
|
-
|
|
386
|
+
Examples:
|
|
304
387
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
388
|
+
```grain
|
|
389
|
+
use Float64.{ (>) }
|
|
390
|
+
assert 6.0d > 5.0d
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
### Float64.**(<=)**
|
|
394
|
+
|
|
395
|
+
<details>
|
|
396
|
+
<summary>Added in <code>0.6.0</code></summary>
|
|
397
|
+
<table>
|
|
398
|
+
<thead>
|
|
399
|
+
<tr><th>version</th><th>changes</th></tr>
|
|
400
|
+
</thead>
|
|
401
|
+
<tbody>
|
|
402
|
+
<tr><td><code>0.2.0</code></td><td>Originally named `lte`</td></tr>
|
|
403
|
+
</tbody>
|
|
404
|
+
</table>
|
|
308
405
|
</details>
|
|
309
406
|
|
|
310
407
|
```grain
|
|
311
|
-
|
|
408
|
+
(<=) : (x: Float64, y: Float64) => Bool
|
|
312
409
|
```
|
|
313
410
|
|
|
314
411
|
Checks if the first value is less than or equal to the second value.
|
|
@@ -326,15 +423,34 @@ Returns:
|
|
|
326
423
|
|----|-----------|
|
|
327
424
|
|`Bool`|`true` if the first value is less than or equal to the second value or `false` otherwise|
|
|
328
425
|
|
|
329
|
-
|
|
426
|
+
Examples:
|
|
330
427
|
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
428
|
+
```grain
|
|
429
|
+
use Float64.{ (<=) }
|
|
430
|
+
assert 1.0d <= 2.0d
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
```grain
|
|
434
|
+
use Float64.{ (<=) }
|
|
435
|
+
assert 2.0d <= 2.0d
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
### Float64.**(>=)**
|
|
439
|
+
|
|
440
|
+
<details>
|
|
441
|
+
<summary>Added in <code>0.6.0</code></summary>
|
|
442
|
+
<table>
|
|
443
|
+
<thead>
|
|
444
|
+
<tr><th>version</th><th>changes</th></tr>
|
|
445
|
+
</thead>
|
|
446
|
+
<tbody>
|
|
447
|
+
<tr><td><code>0.2.0</code></td><td>Originally named `gte`</td></tr>
|
|
448
|
+
</tbody>
|
|
449
|
+
</table>
|
|
334
450
|
</details>
|
|
335
451
|
|
|
336
452
|
```grain
|
|
337
|
-
|
|
453
|
+
(>=) : (x: Float64, y: Float64) => Bool
|
|
338
454
|
```
|
|
339
455
|
|
|
340
456
|
Checks if the first value is greater than or equal to the second value.
|
|
@@ -352,3 +468,15 @@ Returns:
|
|
|
352
468
|
|----|-----------|
|
|
353
469
|
|`Bool`|`true` if the first value is greater than or equal to the second value or `false` otherwise|
|
|
354
470
|
|
|
471
|
+
Examples:
|
|
472
|
+
|
|
473
|
+
```grain
|
|
474
|
+
use Float64.{ (>=) }
|
|
475
|
+
assert 5.0d >= 2.0d
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
```grain
|
|
479
|
+
use Float64.{ (>=) }
|
|
480
|
+
assert -1.0d >= -1.0d
|
|
481
|
+
```
|
|
482
|
+
|
package/hash.gr
CHANGED
|
@@ -1,39 +1,46 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Utilities for hashing any value.
|
|
3
|
+
*
|
|
4
|
+
* @example from "hash" include Hash
|
|
5
|
+
*
|
|
6
|
+
* @example Hash.hash(1)
|
|
7
|
+
* @example Hash.hash("Hello World")
|
|
4
8
|
*
|
|
5
9
|
* @since v0.1.0
|
|
6
10
|
*/
|
|
11
|
+
module Hash
|
|
7
12
|
|
|
8
13
|
/**
|
|
9
14
|
This module implements MurmurHash3 for Grain data types.
|
|
10
15
|
https://en.wikipedia.org/wiki/MurmurHash
|
|
11
16
|
*/
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
+
from "runtime/unsafe/wasmi32" include WasmI32
|
|
18
|
+
use WasmI32.{
|
|
19
|
+
(+),
|
|
20
|
+
(-),
|
|
21
|
+
(*),
|
|
17
22
|
remU as (%),
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
(^),
|
|
24
|
+
(>>>),
|
|
25
|
+
(<<),
|
|
26
|
+
(&),
|
|
27
|
+
(|),
|
|
28
|
+
(==),
|
|
29
|
+
(!=),
|
|
25
30
|
gtU as (>),
|
|
26
31
|
ltU as (<),
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
32
|
+
}
|
|
33
|
+
from "runtime/unsafe/wasmi64" include WasmI64
|
|
34
|
+
from "runtime/unsafe/tags" include Tags
|
|
30
35
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
36
|
+
from "runtime/dataStructures" include DataStructures
|
|
37
|
+
use DataStructures.{ tagSimpleNumber }
|
|
38
|
+
from "runtime/numbers" include Numbers
|
|
39
|
+
use Numbers.{ coerceNumberToWasmI32 }
|
|
40
|
+
from "runtime/bigint" include Bigint as BI
|
|
34
41
|
|
|
35
|
-
|
|
36
|
-
|
|
42
|
+
from "wasi/random" include Random
|
|
43
|
+
from "result" include Result
|
|
37
44
|
|
|
38
45
|
@unsafe
|
|
39
46
|
let seed = {
|
|
@@ -73,9 +80,10 @@ let hash32 = k => {
|
|
|
73
80
|
|
|
74
81
|
@unsafe
|
|
75
82
|
let hash64 = k => {
|
|
83
|
+
use WasmI64.{ (>>>) }
|
|
76
84
|
// convenience function for hashing 64-bit values
|
|
77
85
|
hash32(WasmI32.wrapI64(k))
|
|
78
|
-
hash32(WasmI32.wrapI64(
|
|
86
|
+
hash32(WasmI32.wrapI64(k >>> 32N))
|
|
79
87
|
}
|
|
80
88
|
|
|
81
89
|
@unsafe
|
|
@@ -107,13 +115,12 @@ let rec hashOne = (val, depth) => {
|
|
|
107
115
|
} else if ((val & Tags._GRAIN_NUMBER_TAG_MASK) != 0n) {
|
|
108
116
|
hash32(val)
|
|
109
117
|
} else if (
|
|
110
|
-
(val & Tags._GRAIN_GENERIC_TAG_MASK) ==
|
|
118
|
+
(val & Tags._GRAIN_GENERIC_TAG_MASK) ==
|
|
119
|
+
Tags._GRAIN_GENERIC_HEAP_TAG_TYPE
|
|
111
120
|
) {
|
|
112
121
|
let heapPtr = val
|
|
113
122
|
match (WasmI32.load(heapPtr, 0n)) {
|
|
114
|
-
t when
|
|
115
|
-
t == Tags._GRAIN_STRING_HEAP_TAG || t == Tags._GRAIN_BYTES_HEAP_TAG
|
|
116
|
-
) => {
|
|
123
|
+
t when t == Tags._GRAIN_STRING_HEAP_TAG || t == Tags._GRAIN_BYTES_HEAP_TAG => {
|
|
117
124
|
let length = WasmI32.load(heapPtr, 4n)
|
|
118
125
|
let extra = length % 4n
|
|
119
126
|
let l = length - extra
|
|
@@ -182,9 +189,6 @@ let rec hashOne = (val, depth) => {
|
|
|
182
189
|
t when t == Tags._GRAIN_BOXED_NUM_HEAP_TAG => {
|
|
183
190
|
let tag = WasmI32.load(heapPtr, 4n)
|
|
184
191
|
match (tag) {
|
|
185
|
-
t when t == Tags._GRAIN_INT32_BOXED_NUM_TAG => {
|
|
186
|
-
hash32(WasmI32.load(heapPtr, 8n))
|
|
187
|
-
},
|
|
188
192
|
t when t == Tags._GRAIN_INT64_BOXED_NUM_TAG => {
|
|
189
193
|
hash32(WasmI32.load(heapPtr, 8n))
|
|
190
194
|
hash32(WasmI32.load(heapPtr, 12n))
|
|
@@ -198,9 +202,6 @@ let rec hashOne = (val, depth) => {
|
|
|
198
202
|
hash64(BI.getLimb(heapPtr, i))
|
|
199
203
|
}
|
|
200
204
|
},
|
|
201
|
-
t when t == Tags._GRAIN_FLOAT32_BOXED_NUM_TAG => {
|
|
202
|
-
hash32(WasmI32.load(heapPtr, 8n))
|
|
203
|
-
},
|
|
204
205
|
t when t == Tags._GRAIN_FLOAT64_BOXED_NUM_TAG => {
|
|
205
206
|
hash32(WasmI32.load(heapPtr, 8n))
|
|
206
207
|
hash32(WasmI32.load(heapPtr, 12n))
|
|
@@ -214,6 +215,15 @@ let rec hashOne = (val, depth) => {
|
|
|
214
215
|
},
|
|
215
216
|
}
|
|
216
217
|
},
|
|
218
|
+
t when t == Tags._GRAIN_INT32_HEAP_TAG ||
|
|
219
|
+
t == Tags._GRAIN_FLOAT32_HEAP_TAG ||
|
|
220
|
+
t == Tags._GRAIN_UINT32_HEAP_TAG => {
|
|
221
|
+
hash32(WasmI32.load(heapPtr, 4n))
|
|
222
|
+
},
|
|
223
|
+
t when t == Tags._GRAIN_UINT64_HEAP_TAG => {
|
|
224
|
+
hash32(WasmI32.load(heapPtr, 8n))
|
|
225
|
+
hash32(WasmI32.load(heapPtr, 12n))
|
|
226
|
+
},
|
|
217
227
|
_ => {
|
|
218
228
|
hash32(heapPtr)
|
|
219
229
|
},
|
|
@@ -223,9 +233,6 @@ let rec hashOne = (val, depth) => {
|
|
|
223
233
|
hash32(val)
|
|
224
234
|
}
|
|
225
235
|
}
|
|
226
|
-
/**
|
|
227
|
-
* @section Values: Functions for hashing.
|
|
228
|
-
*/
|
|
229
236
|
|
|
230
237
|
/**
|
|
231
238
|
* A generic hash function that produces an integer from any value. If `a == b` then `Hash.hash(a) == Hash.hash(b)`.
|
|
@@ -233,10 +240,13 @@ let rec hashOne = (val, depth) => {
|
|
|
233
240
|
* @param anything: The value to hash
|
|
234
241
|
* @returns A hash for the given value
|
|
235
242
|
*
|
|
243
|
+
* @example assert Hash.hash(1) == Hash.hash(1)
|
|
244
|
+
* @example assert Hash.hash("Hello World") == Hash.hash("Hello World")
|
|
245
|
+
*
|
|
236
246
|
* @since v0.1.0
|
|
237
247
|
*/
|
|
238
248
|
@unsafe
|
|
239
|
-
|
|
249
|
+
provide let hash = anything => {
|
|
240
250
|
h = seed
|
|
241
251
|
|
|
242
252
|
hashOne(WasmI32.fromGrain(anything), 0n)
|
package/hash.md
CHANGED
|
@@ -10,12 +10,20 @@ No other changes yet.
|
|
|
10
10
|
</details>
|
|
11
11
|
|
|
12
12
|
```grain
|
|
13
|
-
|
|
13
|
+
from "hash" include Hash
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
```grain
|
|
17
|
+
Hash.hash(1)
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
```grain
|
|
21
|
+
Hash.hash("Hello World")
|
|
14
22
|
```
|
|
15
23
|
|
|
16
24
|
## Values
|
|
17
25
|
|
|
18
|
-
Functions
|
|
26
|
+
Functions and constants included in the Hash module.
|
|
19
27
|
|
|
20
28
|
### Hash.**hash**
|
|
21
29
|
|
|
@@ -25,7 +33,7 @@ No other changes yet.
|
|
|
25
33
|
</details>
|
|
26
34
|
|
|
27
35
|
```grain
|
|
28
|
-
hash : a
|
|
36
|
+
hash : (anything: a) => Number
|
|
29
37
|
```
|
|
30
38
|
|
|
31
39
|
A generic hash function that produces an integer from any value. If `a == b` then `Hash.hash(a) == Hash.hash(b)`.
|
|
@@ -42,3 +50,13 @@ Returns:
|
|
|
42
50
|
|----|-----------|
|
|
43
51
|
|`Number`|A hash for the given value|
|
|
44
52
|
|
|
53
|
+
Examples:
|
|
54
|
+
|
|
55
|
+
```grain
|
|
56
|
+
assert Hash.hash(1) == Hash.hash(1)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
```grain
|
|
60
|
+
assert Hash.hash("Hello World") == Hash.hash("Hello World")
|
|
61
|
+
```
|
|
62
|
+
|