functionalscript 0.3.7 → 0.3.9
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/com/cpp/module.f.js +0 -1
- package/com/cs/module.f.js +5 -6
- package/com/rust/module.f.d.ts +3 -3
- package/com/rust/module.f.js +24 -29
- package/com/test/build.f.js +2 -2
- package/com/types/module.f.d.ts +1 -1
- package/com/types/module.f.js +1 -2
- package/com/types/testlib.f.js +0 -1
- package/commonjs/build/module.f.d.ts +2 -2
- package/commonjs/build/module.f.js +3 -6
- package/commonjs/build/test.f.js +0 -4
- package/commonjs/module/function/module.f.d.ts +1 -1
- package/commonjs/module/function/module.f.js +1 -1
- package/commonjs/module/module.f.d.ts +1 -1
- package/commonjs/module/module.f.js +2 -3
- package/commonjs/path/module.f.d.ts +5 -5
- package/commonjs/path/module.f.js +6 -8
- package/commonjs/path/test.f.js +1 -2
- package/crypto/prime_field/module.f.d.ts +1 -1
- package/crypto/prime_field/module.f.js +1 -1
- package/crypto/secp/module.f.d.ts +1 -1
- package/crypto/secp/module.f.js +2 -2
- package/crypto/sha2/module.f.d.ts +75 -12
- package/crypto/sha2/module.f.js +246 -139
- package/crypto/sha2/test.f.d.ts +22 -3
- package/crypto/sha2/test.f.js +120 -67
- package/dev/test/module.f.d.ts +1 -1
- package/dev/test/module.f.js +4 -5
- package/dev/test.f.js +0 -4
- package/djs/module.f.d.ts +1 -1
- package/djs/module.f.js +0 -1
- package/djs/parser/module.f.d.ts +1 -1
- package/djs/parser/module.f.js +0 -2
- package/djs/serializer/module.f.d.ts +2 -2
- package/djs/serializer/module.f.js +0 -2
- package/djs/tokenizer/module.f.js +0 -1
- package/fsc/test.f.js +0 -1
- package/js/tokenizer/module.f.d.ts +1 -1
- package/js/tokenizer/module.f.js +1 -1
- package/json/parser/module.f.d.ts +2 -2
- package/json/parser/module.f.js +0 -3
- package/json/serializer/module.f.js +0 -2
- package/json/tokenizer/module.f.js +0 -1
- package/package.json +1 -1
- package/text/ascii/module.f.d.ts +1 -1
- package/text/ascii/module.f.js +2 -3
- package/text/ascii/test.f.js +2 -4
- package/text/module.f.d.ts +18 -3
- package/text/module.f.js +22 -6
- package/text/sgr/module.f.js +1 -1
- package/text/test.f.d.ts +4 -1
- package/text/test.f.js +24 -16
- package/text/utf16/module.f.d.ts +5 -6
- package/text/utf16/module.f.js +9 -10
- package/text/utf16/test.f.js +29 -30
- package/text/utf8/module.f.d.ts +8 -5
- package/text/utf8/module.f.js +3 -5
- package/text/utf8/test.f.js +34 -35
- package/types/bigfloat/module.f.js +1 -1
- package/types/bigint/module.f.d.ts +36 -7
- package/types/bigint/module.f.js +40 -11
- package/types/bigint/test.f.d.ts +8 -0
- package/types/bigint/test.f.js +81 -2
- package/types/bit_vec/module.f.d.ts +103 -83
- package/types/bit_vec/module.f.js +68 -110
- package/types/bit_vec/test.f.d.ts +20 -8
- package/types/bit_vec/test.f.js +110 -117
- package/types/btree/find/module.f.d.ts +11 -10
- package/types/btree/find/module.f.js +1 -5
- package/types/btree/find/test.f.js +4 -6
- package/types/btree/module.f.d.ts +3 -3
- package/types/btree/module.f.js +2 -5
- package/types/btree/remove/module.f.d.ts +2 -2
- package/types/btree/remove/module.f.js +0 -3
- package/types/btree/remove/test.f.js +4 -7
- package/types/btree/set/module.f.d.ts +2 -2
- package/types/btree/set/module.f.js +2 -6
- package/types/btree/set/test.f.js +3 -6
- package/types/btree/test.f.js +9 -13
- package/types/btree/types/module.f.d.ts +1 -3
- package/types/byte_set/module.f.d.ts +3 -3
- package/types/byte_set/module.f.js +1 -4
- package/types/byte_set/test.f.js +2 -4
- package/types/function/compare/module.f.d.ts +1 -5
- package/types/function/compare/module.f.js +0 -1
- package/types/function/compare/test.f.js +1 -2
- package/types/function/module.f.d.ts +1 -1
- package/types/function/module.f.js +1 -1
- package/types/function/operator/module.f.d.ts +2 -3
- package/types/function/operator/module.f.js +5 -5
- package/types/function/test.f.js +1 -2
- package/types/list/module.f.d.ts +1 -1
- package/types/list/module.f.js +9 -9
- package/types/list/test.f.js +2 -4
- package/types/map/module.f.d.ts +7 -9
- package/types/map/module.f.js +6 -15
- package/types/map/test.f.js +3 -4
- package/types/nibble_set/module.f.d.ts +2 -3
- package/types/nibble_set/test.f.js +26 -27
- package/types/nullable/test.f.js +2 -2
- package/types/number/module.f.d.ts +2 -2
- package/types/number/module.f.js +1 -2
- package/types/number/test.f.js +1 -2
- package/types/object/module.f.d.ts +5 -5
- package/types/object/module.f.js +2 -4
- package/types/object/test.f.js +3 -3
- package/types/range/test.f.js +6 -6
- package/types/range_map/module.f.d.ts +8 -8
- package/types/range_map/module.f.js +3 -9
- package/types/range_map/test.f.js +2 -4
- package/types/result/module.f.d.ts +2 -3
- package/types/sorted_list/module.f.d.ts +8 -8
- package/types/sorted_list/module.f.js +2 -6
- package/types/sorted_list/test.f.js +12 -17
- package/types/sorted_set/module.f.d.ts +2 -2
- package/types/sorted_set/module.f.js +2 -5
- package/types/sorted_set/test.f.js +14 -19
- package/types/string/module.f.d.ts +5 -5
- package/types/string/module.f.js +2 -4
- package/types/string/test.f.js +2 -4
- package/types/string_set/module.f.d.ts +2 -2
- package/types/string_set/module.f.js +5 -11
- package/types/string_set/test.f.js +25 -25
package/text/utf8/test.f.js
CHANGED
|
@@ -1,67 +1,66 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { toCodePointList, fromCodePointList } from "./module.f.js";
|
|
2
2
|
import * as json from "../../json/module.f.js";
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
import * as list from "../../types/list/module.f.js";
|
|
3
|
+
import { sort } from "../../types/object/module.f.js";
|
|
4
|
+
import { toArray } from "../../types/list/module.f.js";
|
|
6
5
|
const stringify = json.stringify(sort);
|
|
7
6
|
export default {
|
|
8
7
|
toCodePoint: [
|
|
9
8
|
() => {
|
|
10
|
-
const result = stringify(
|
|
9
|
+
const result = stringify(toArray(toCodePointList([-1, 256])));
|
|
11
10
|
if (result !== '[2147483648,2147483648]') {
|
|
12
11
|
throw result;
|
|
13
12
|
}
|
|
14
13
|
},
|
|
15
14
|
() => {
|
|
16
|
-
const result = stringify(
|
|
15
|
+
const result = stringify(toArray(toCodePointList([128, 193, 245, 255])));
|
|
17
16
|
if (result !== '[-2147483520,-2147483455,-2147483403,-2147483393]') {
|
|
18
17
|
throw result;
|
|
19
18
|
}
|
|
20
19
|
},
|
|
21
20
|
() => {
|
|
22
|
-
const result = stringify(
|
|
21
|
+
const result = stringify(toArray(toCodePointList([0, 1, 127])));
|
|
23
22
|
if (result !== '[0,1,127]') {
|
|
24
23
|
throw result;
|
|
25
24
|
}
|
|
26
25
|
},
|
|
27
26
|
() => {
|
|
28
|
-
const result = stringify(
|
|
27
|
+
const result = stringify(toArray(toCodePointList([194, 128, 194, 169, 223, 191])));
|
|
29
28
|
if (result !== '[128,169,2047]') {
|
|
30
29
|
throw result;
|
|
31
30
|
}
|
|
32
31
|
},
|
|
33
32
|
() => {
|
|
34
|
-
const result = stringify(
|
|
33
|
+
const result = stringify(toArray(toCodePointList([194, 194, 127, 194, 192, 194])));
|
|
35
34
|
if (result !== '[-2147483454,-2147483454,127,-2147483454,-2147483456,-2147483454]') {
|
|
36
35
|
throw result;
|
|
37
36
|
}
|
|
38
37
|
},
|
|
39
38
|
() => {
|
|
40
|
-
const result = stringify(
|
|
39
|
+
const result = stringify(toArray(toCodePointList([224, 160, 128, 224, 160, 129, 239, 191, 191])));
|
|
41
40
|
if (result !== '[2048,2049,65535]') {
|
|
42
41
|
throw result;
|
|
43
42
|
}
|
|
44
43
|
},
|
|
45
44
|
() => {
|
|
46
|
-
const result = stringify(
|
|
45
|
+
const result = stringify(toArray(toCodePointList([224, 224, 160, 127, 239, 191])));
|
|
47
46
|
if (result !== '[-2147483424,-2147482592,127,-2147481601]') {
|
|
48
47
|
throw result;
|
|
49
48
|
}
|
|
50
49
|
},
|
|
51
50
|
() => {
|
|
52
|
-
const result = stringify(
|
|
51
|
+
const result = stringify(toArray(toCodePointList([240, 144, 128, 128, 240, 144, 128, 129, 244, 143, 191, 191])));
|
|
53
52
|
if (result !== '[65536,65537,1114111]') {
|
|
54
53
|
throw result;
|
|
55
54
|
}
|
|
56
55
|
},
|
|
57
56
|
() => {
|
|
58
|
-
const result = stringify(
|
|
57
|
+
const result = stringify(toArray(toCodePointList([240, 240, 160, 127, 244, 191])));
|
|
59
58
|
if (result !== '[-2147483408,-2147483104,127,-2147482817]') {
|
|
60
59
|
throw result;
|
|
61
60
|
}
|
|
62
61
|
},
|
|
63
62
|
() => {
|
|
64
|
-
const result = stringify(
|
|
63
|
+
const result = stringify(toArray(toCodePointList([240, 160, 160, 244, 160, 160])));
|
|
65
64
|
if (result !== '[-2147448800,-2147432416]') {
|
|
66
65
|
throw result;
|
|
67
66
|
}
|
|
@@ -69,67 +68,67 @@ export default {
|
|
|
69
68
|
],
|
|
70
69
|
fromCodePointList: [
|
|
71
70
|
() => {
|
|
72
|
-
const result = stringify(
|
|
71
|
+
const result = stringify(toArray(fromCodePointList([0, 1, 0x7F])));
|
|
73
72
|
if (result !== '[0,1,127]') {
|
|
74
73
|
throw result;
|
|
75
74
|
}
|
|
76
75
|
},
|
|
77
76
|
() => {
|
|
78
|
-
const result = stringify(
|
|
77
|
+
const result = stringify(toArray(fromCodePointList([0x80])));
|
|
79
78
|
if (result !== '[194,128]') {
|
|
80
79
|
throw result;
|
|
81
80
|
}
|
|
82
81
|
},
|
|
83
82
|
() => {
|
|
84
|
-
const result = stringify(
|
|
83
|
+
const result = stringify(toArray(fromCodePointList([0xa9])));
|
|
85
84
|
if (result !== '[194,169]') {
|
|
86
85
|
throw result;
|
|
87
86
|
}
|
|
88
87
|
},
|
|
89
88
|
() => {
|
|
90
|
-
const result = stringify(
|
|
89
|
+
const result = stringify(toArray(fromCodePointList([0x7ff])));
|
|
91
90
|
if (result !== '[223,191]') {
|
|
92
91
|
throw result;
|
|
93
92
|
}
|
|
94
93
|
},
|
|
95
94
|
() => {
|
|
96
|
-
const result = stringify(
|
|
95
|
+
const result = stringify(toArray(fromCodePointList([0x800])));
|
|
97
96
|
if (result !== '[224,160,128]') {
|
|
98
97
|
throw result;
|
|
99
98
|
}
|
|
100
99
|
},
|
|
101
100
|
() => {
|
|
102
|
-
const result = stringify(
|
|
101
|
+
const result = stringify(toArray(fromCodePointList([0x801])));
|
|
103
102
|
if (result !== '[224,160,129]') {
|
|
104
103
|
throw result;
|
|
105
104
|
}
|
|
106
105
|
},
|
|
107
106
|
() => {
|
|
108
|
-
const result = stringify(
|
|
107
|
+
const result = stringify(toArray(fromCodePointList([0xffff])));
|
|
109
108
|
if (result !== '[239,191,191]') {
|
|
110
109
|
throw result;
|
|
111
110
|
}
|
|
112
111
|
},
|
|
113
112
|
() => {
|
|
114
|
-
const result = stringify(
|
|
113
|
+
const result = stringify(toArray(fromCodePointList([0x10000])));
|
|
115
114
|
if (result !== '[240,144,128,128]') {
|
|
116
115
|
throw result;
|
|
117
116
|
}
|
|
118
117
|
},
|
|
119
118
|
() => {
|
|
120
|
-
const result = stringify(
|
|
119
|
+
const result = stringify(toArray(fromCodePointList([0x10001])));
|
|
121
120
|
if (result !== '[240,144,128,129]') {
|
|
122
121
|
throw result;
|
|
123
122
|
}
|
|
124
123
|
},
|
|
125
124
|
() => {
|
|
126
|
-
const result = stringify(
|
|
125
|
+
const result = stringify(toArray(fromCodePointList([0x10FFFF])));
|
|
127
126
|
if (result !== '[244,143,191,191]') {
|
|
128
127
|
throw result;
|
|
129
128
|
}
|
|
130
129
|
},
|
|
131
130
|
() => {
|
|
132
|
-
const result = stringify(
|
|
131
|
+
const result = stringify(toArray(fromCodePointList([0x110000, 2147483648])));
|
|
133
132
|
if (result !== '[2147483648,2147483648]') {
|
|
134
133
|
throw result;
|
|
135
134
|
}
|
|
@@ -137,36 +136,36 @@ export default {
|
|
|
137
136
|
],
|
|
138
137
|
toFrom: [
|
|
139
138
|
() => {
|
|
140
|
-
const codePointList =
|
|
141
|
-
const result = stringify(
|
|
139
|
+
const codePointList = toCodePointList([128, 193, 245, 255]);
|
|
140
|
+
const result = stringify(toArray(fromCodePointList(codePointList)));
|
|
142
141
|
if (result !== '[128,193,245,255]') {
|
|
143
142
|
throw result;
|
|
144
143
|
}
|
|
145
144
|
},
|
|
146
145
|
() => {
|
|
147
|
-
const codePointList =
|
|
148
|
-
const result = stringify(
|
|
146
|
+
const codePointList = toCodePointList([194, 194, 127, 194, 192, 194]);
|
|
147
|
+
const result = stringify(toArray(fromCodePointList(codePointList)));
|
|
149
148
|
if (result !== '[194,194,127,194,192,194]') {
|
|
150
149
|
throw result;
|
|
151
150
|
}
|
|
152
151
|
},
|
|
153
152
|
() => {
|
|
154
|
-
const codePointList =
|
|
155
|
-
const result = stringify(
|
|
153
|
+
const codePointList = toCodePointList([224, 224, 160, 127, 239, 191]);
|
|
154
|
+
const result = stringify(toArray(fromCodePointList(codePointList)));
|
|
156
155
|
if (result !== '[224,224,160,127,239,191]') {
|
|
157
156
|
throw result;
|
|
158
157
|
}
|
|
159
158
|
},
|
|
160
159
|
() => {
|
|
161
|
-
const codePointList =
|
|
162
|
-
const result = stringify(
|
|
160
|
+
const codePointList = toCodePointList([240, 240, 160, 127, 244, 191]);
|
|
161
|
+
const result = stringify(toArray(fromCodePointList(codePointList)));
|
|
163
162
|
if (result !== '[240,240,160,127,244,191]') {
|
|
164
163
|
throw result;
|
|
165
164
|
}
|
|
166
165
|
},
|
|
167
166
|
() => {
|
|
168
|
-
const codePointList =
|
|
169
|
-
const result = stringify(
|
|
167
|
+
const codePointList = toCodePointList([240, 160, 160, 244, 160, 160]);
|
|
168
|
+
const result = stringify(toArray(fromCodePointList(codePointList)));
|
|
170
169
|
if (result !== '[240,160,160,244,160,160]') {
|
|
171
170
|
throw result;
|
|
172
171
|
}
|
|
@@ -1,12 +1,29 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @module
|
|
3
|
+
*
|
|
4
|
+
* This module provides a collection of utility functions for working with `bigint` values.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```js
|
|
9
|
+
* import { sum, abs, log2, bitLength, mask } from './module.f.ts'
|
|
10
|
+
*
|
|
11
|
+
* const total = sum([1n, 2n, 3n]) // 6n
|
|
12
|
+
* const absoluteValue = abs(-42n) // 42n
|
|
13
|
+
* const logValue = log2(8n) // 3n
|
|
14
|
+
* const bitCount = bitLength(255n) // 8n
|
|
15
|
+
* const bitmask = mask(5n) // 31n
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
import { type Sign } from '../function/compare/module.f.ts';
|
|
2
19
|
import type * as Operator from '../function/operator/module.f.ts';
|
|
3
20
|
import { type List } from '../list/module.f.ts';
|
|
4
|
-
type Unary = Operator.Unary<bigint, bigint>;
|
|
5
|
-
type Reduce = Operator.Reduce<bigint>;
|
|
21
|
+
export type Unary = Operator.Unary<bigint, bigint>;
|
|
22
|
+
export type Reduce = Operator.Reduce<bigint>;
|
|
6
23
|
export declare const addition: Reduce;
|
|
7
24
|
export declare const sum: (input: List<bigint>) => bigint;
|
|
8
25
|
export declare const abs: Unary;
|
|
9
|
-
export declare const sign: (a: bigint) =>
|
|
26
|
+
export declare const sign: (a: bigint) => Sign;
|
|
10
27
|
export declare const serialize: (a: bigint) => string;
|
|
11
28
|
/**
|
|
12
29
|
* Calculates the base-2 logarithm (floor).
|
|
@@ -44,8 +61,20 @@ export declare const log2: (v: bigint) => bigint;
|
|
|
44
61
|
* @returns The bit length of the input BigInt.
|
|
45
62
|
*
|
|
46
63
|
* @remark
|
|
47
|
-
* The function uses the `log2` function to calculate the position of the most significant bit(MSB)
|
|
48
|
-
* and adds `1n` to account for the MSB itself.For negative numbers, the absolute value is used.
|
|
64
|
+
* The function uses the `log2` function to calculate the position of the most significant bit (MSB)
|
|
65
|
+
* and adds `1n` to account for the MSB itself. For negative numbers, the absolute value is used.
|
|
49
66
|
*/
|
|
50
67
|
export declare const bitLength: (v: bigint) => bigint;
|
|
51
|
-
|
|
68
|
+
/**
|
|
69
|
+
* Generates a bitmask with the specified number of bits set to 1.
|
|
70
|
+
*
|
|
71
|
+
* @param len - The number of bits to set in the mask. Must be a non-negative integer.
|
|
72
|
+
* @returns A bigint representing the bitmask, where the least significant `len` bits are 1.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
*
|
|
76
|
+
* ```js
|
|
77
|
+
* const result = mask(3n) // 7n
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export declare const mask: (len: bigint) => bigint;
|
package/types/bigint/module.f.js
CHANGED
|
@@ -1,11 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @module
|
|
3
|
+
*
|
|
4
|
+
* This module provides a collection of utility functions for working with `bigint` values.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```js
|
|
9
|
+
* import { sum, abs, log2, bitLength, mask } from './module.f.ts'
|
|
10
|
+
*
|
|
11
|
+
* const total = sum([1n, 2n, 3n]) // 6n
|
|
12
|
+
* const absoluteValue = abs(-42n) // 42n
|
|
13
|
+
* const logValue = log2(8n) // 3n
|
|
14
|
+
* const bitCount = bitLength(255n) // 8n
|
|
15
|
+
* const bitmask = mask(5n) // 31n
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
import { unsafeCmp } from "../function/compare/module.f.js";
|
|
3
19
|
import { reduce } from "../list/module.f.js";
|
|
4
20
|
export const addition = a => b => a + b;
|
|
5
21
|
export const sum = reduce(addition)(0n);
|
|
6
22
|
export const abs = a => a >= 0 ? a : -a;
|
|
7
|
-
export const sign = a => unsafeCmp(a)(0n);
|
|
8
|
-
export const serialize = a => `${a}n`;
|
|
23
|
+
export const sign = (a) => unsafeCmp(a)(0n);
|
|
24
|
+
export const serialize = (a) => `${a}n`;
|
|
9
25
|
/**
|
|
10
26
|
* Calculates the base-2 logarithm (floor).
|
|
11
27
|
*
|
|
@@ -28,8 +44,8 @@ export const log2 = (v) => {
|
|
|
28
44
|
if (v <= 0n) {
|
|
29
45
|
return -1n;
|
|
30
46
|
}
|
|
31
|
-
let result =
|
|
32
|
-
let i =
|
|
47
|
+
let result = 31n;
|
|
48
|
+
let i = 32n;
|
|
33
49
|
while (true) {
|
|
34
50
|
const n = v >> i;
|
|
35
51
|
if (n === 0n) {
|
|
@@ -41,8 +57,8 @@ export const log2 = (v) => {
|
|
|
41
57
|
i <<= 1n;
|
|
42
58
|
}
|
|
43
59
|
// We know that `v` is not 0 so it doesn't make sense to check `n` when `i` is 0.
|
|
44
|
-
// Because of this, We check if `i` is greater than
|
|
45
|
-
while (i !==
|
|
60
|
+
// Because of this, We check if `i` is greater than 32 before we divide it by 2.
|
|
61
|
+
while (i !== 32n) {
|
|
46
62
|
i >>= 1n;
|
|
47
63
|
const n = v >> i;
|
|
48
64
|
if (n !== 0n) {
|
|
@@ -50,7 +66,7 @@ export const log2 = (v) => {
|
|
|
50
66
|
v = n;
|
|
51
67
|
}
|
|
52
68
|
}
|
|
53
|
-
return result;
|
|
69
|
+
return result - BigInt(Math.clz32(Number(v)));
|
|
54
70
|
};
|
|
55
71
|
/**
|
|
56
72
|
* Calculates the bit length of a given BigInt.
|
|
@@ -69,8 +85,8 @@ export const log2 = (v) => {
|
|
|
69
85
|
* @returns The bit length of the input BigInt.
|
|
70
86
|
*
|
|
71
87
|
* @remark
|
|
72
|
-
* The function uses the `log2` function to calculate the position of the most significant bit(MSB)
|
|
73
|
-
* and adds `1n` to account for the MSB itself.For negative numbers, the absolute value is used.
|
|
88
|
+
* The function uses the `log2` function to calculate the position of the most significant bit (MSB)
|
|
89
|
+
* and adds `1n` to account for the MSB itself. For negative numbers, the absolute value is used.
|
|
74
90
|
*/
|
|
75
91
|
export const bitLength = (v) => {
|
|
76
92
|
if (v <= 0n) {
|
|
@@ -81,3 +97,16 @@ export const bitLength = (v) => {
|
|
|
81
97
|
}
|
|
82
98
|
return log2(v) + 1n;
|
|
83
99
|
};
|
|
100
|
+
/**
|
|
101
|
+
* Generates a bitmask with the specified number of bits set to 1.
|
|
102
|
+
*
|
|
103
|
+
* @param len - The number of bits to set in the mask. Must be a non-negative integer.
|
|
104
|
+
* @returns A bigint representing the bitmask, where the least significant `len` bits are 1.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
*
|
|
108
|
+
* ```js
|
|
109
|
+
* const result = mask(3n) // 7n
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export const mask = (len) => (1n << len) - 1n;
|
package/types/bigint/test.f.d.ts
CHANGED
package/types/bigint/test.f.js
CHANGED
|
@@ -1,6 +1,85 @@
|
|
|
1
|
-
import
|
|
2
|
-
const
|
|
1
|
+
import { sum, abs, serialize, log2, bitLength, mask } from "./module.f.js";
|
|
2
|
+
const oldLog2 = (v) => {
|
|
3
|
+
if (v <= 0n) {
|
|
4
|
+
return -1n;
|
|
5
|
+
}
|
|
6
|
+
let result = 0n;
|
|
7
|
+
let i = 1n;
|
|
8
|
+
while (true) {
|
|
9
|
+
const n = v >> i;
|
|
10
|
+
if (n === 0n) {
|
|
11
|
+
// overshot
|
|
12
|
+
break;
|
|
13
|
+
}
|
|
14
|
+
v = n;
|
|
15
|
+
result += i;
|
|
16
|
+
i <<= 1n; // multiple by two
|
|
17
|
+
}
|
|
18
|
+
// We know that `v` is not 0 so it doesn't make sense to check `n` when `i` is 0.
|
|
19
|
+
// Because of this, We check if `i` is greater than 1 before we divide it by 2.
|
|
20
|
+
while (i !== 1n) {
|
|
21
|
+
i >>= 1n;
|
|
22
|
+
const n = v >> i;
|
|
23
|
+
if (n !== 0n) {
|
|
24
|
+
result += i;
|
|
25
|
+
v = n;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return result;
|
|
29
|
+
};
|
|
30
|
+
const stringLog2 = (v) => BigInt(v.toString(2).length) - 1n;
|
|
31
|
+
const stringHexLog2 = (v) => {
|
|
32
|
+
const len = (BigInt(v.toString(16).length) - 1n) << 2n;
|
|
33
|
+
const x = v >> len;
|
|
34
|
+
return len + 31n - BigInt(Math.clz32(Number(x)));
|
|
35
|
+
};
|
|
36
|
+
const benchmark = (f) => () => {
|
|
37
|
+
let e = 1048575n;
|
|
38
|
+
let c = 1n << e;
|
|
39
|
+
for (let i = 0n; i < 1_000; ++i) {
|
|
40
|
+
const x = f(c);
|
|
41
|
+
if (x !== e) {
|
|
42
|
+
throw x;
|
|
43
|
+
}
|
|
44
|
+
c >>= 1n;
|
|
45
|
+
--e;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
3
48
|
export default {
|
|
49
|
+
example: () => {
|
|
50
|
+
const total = sum([1n, 2n, 3n]); // 6n
|
|
51
|
+
if (total !== 6n) {
|
|
52
|
+
throw total;
|
|
53
|
+
}
|
|
54
|
+
const absoluteValue = abs(-42n); // 42n
|
|
55
|
+
if (absoluteValue !== 42n) {
|
|
56
|
+
throw total;
|
|
57
|
+
}
|
|
58
|
+
const logValue = log2(8n); // 3n
|
|
59
|
+
if (logValue !== 3n) {
|
|
60
|
+
throw total;
|
|
61
|
+
}
|
|
62
|
+
const bitCount = bitLength(255n); // 8n
|
|
63
|
+
if (bitCount !== 8n) {
|
|
64
|
+
throw total;
|
|
65
|
+
}
|
|
66
|
+
const bitmask = mask(5n); // 31n
|
|
67
|
+
if (bitmask !== 31n) {
|
|
68
|
+
throw total;
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
benchmark: {
|
|
72
|
+
str: benchmark(stringLog2),
|
|
73
|
+
stringHexLog2: benchmark(stringHexLog2),
|
|
74
|
+
oldLog2: benchmark(oldLog2),
|
|
75
|
+
log2: benchmark(log2),
|
|
76
|
+
},
|
|
77
|
+
mask: () => {
|
|
78
|
+
const result = mask(3n); // 7n
|
|
79
|
+
if (result !== 7n) {
|
|
80
|
+
throw result;
|
|
81
|
+
}
|
|
82
|
+
},
|
|
4
83
|
sum: () => {
|
|
5
84
|
const result = sum([2n, 3n, 4n, 5n]);
|
|
6
85
|
if (result !== 14n) {
|