@tktb-tess/util-fns 0.3.2 → 0.3.4
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/dist/baillie-psw.d.ts +4 -3
- package/dist/brand.d.ts +5 -5
- package/dist/bundle.d.ts +2 -1
- package/dist/bundle.js +148 -112
- package/dist/bundle.umd.cjs +2 -2
- package/dist/fraction.d.ts +4 -2
- package/dist/pcg-minimal.d.ts +1 -0
- package/dist/queue.d.ts +10 -0
- package/package.json +1 -1
package/dist/baillie-psw.d.ts
CHANGED
|
@@ -3,18 +3,19 @@
|
|
|
3
3
|
* @param n 判定したい整数
|
|
4
4
|
* @returns
|
|
5
5
|
*/
|
|
6
|
-
|
|
6
|
+
declare const bailliePSW: (n: bigint) => boolean;
|
|
7
7
|
/**
|
|
8
8
|
* 指定範囲内の確率的素数を返す
|
|
9
9
|
* @param min 下限
|
|
10
10
|
* @param max 上限
|
|
11
11
|
* @returns
|
|
12
12
|
*/
|
|
13
|
-
|
|
13
|
+
declare const getRandPrimeByRange: (min: bigint, max: bigint) => bigint;
|
|
14
14
|
/**
|
|
15
15
|
* 指定ビットの確率的素数を返す
|
|
16
16
|
* @param bitLength ビット長
|
|
17
17
|
* @param fixed true: 固定長, false (デフォルト値): `length` ビット以下の可変ビット長
|
|
18
18
|
* @returns
|
|
19
19
|
*/
|
|
20
|
-
|
|
20
|
+
declare const getRandPrimeByBitLength: (bitLength: number, fixed?: boolean) => bigint;
|
|
21
|
+
export { bailliePSW, getRandPrimeByBitLength, getRandPrimeByRange };
|
package/dist/brand.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
declare const
|
|
2
|
-
|
|
3
|
-
readonly [
|
|
4
|
-
readonly [key in K]:
|
|
1
|
+
declare const Brand: unique symbol;
|
|
2
|
+
type Brand<in out K extends string | symbol> = {
|
|
3
|
+
readonly [Brand]: {
|
|
4
|
+
readonly [key in K]: K;
|
|
5
5
|
};
|
|
6
6
|
};
|
|
7
|
-
export {};
|
|
7
|
+
export { Brand };
|
package/dist/bundle.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { bailliePSW, getRandPrimeByBitLength, getRandPrimeByRange } from './bail
|
|
|
5
5
|
import { default as Fraction } from './fraction';
|
|
6
6
|
import { base64ToU8Arr, base64UrlToU8Arr, u8ArrToBase64, u8ArrToBase64Url, BItoU8Arr, u8ArrToBI } from './u8arr-ext';
|
|
7
7
|
import { Brand } from './brand';
|
|
8
|
+
import { default as Queue } from './queue';
|
|
8
9
|
declare const Util: {
|
|
9
10
|
isEqual: (a: unknown, b: unknown) => boolean;
|
|
10
11
|
sleep: (delay: number) => Promise<void>;
|
|
@@ -42,4 +43,4 @@ declare const U8Ext: {
|
|
|
42
43
|
BItoU8Arr: (n: bigint) => Uint8Array<ArrayBuffer>;
|
|
43
44
|
u8ArrToBI: (buf: Uint8Array) => bigint;
|
|
44
45
|
};
|
|
45
|
-
export { isEqual, sleep, lazify, parseCSV, getHash, getRndInt, residue, toBigInt, isNode, getRandBIByBitLength, getRandBIByRange, modPow, exEuclidean, factorial, rot32, rot32BI, jacobiSymbol, isSquare, bailliePSW, getRandPrimeByBitLength, getRandPrimeByRange, base64ToU8Arr, base64UrlToU8Arr, u8ArrToBase64, u8ArrToBase64Url, BItoU8Arr, u8ArrToBI, PCGMinimal, Fraction, Util, Calc, U8Ext,
|
|
46
|
+
export { isEqual, sleep, lazify, parseCSV, getHash, getRndInt, residue, toBigInt, isNode, getRandBIByBitLength, getRandBIByRange, modPow, exEuclidean, factorial, rot32, rot32BI, jacobiSymbol, isSquare, bailliePSW, getRandPrimeByBitLength, getRandPrimeByRange, base64ToU8Arr, base64UrlToU8Arr, u8ArrToBase64, u8ArrToBase64Url, BItoU8Arr, u8ArrToBI, PCGMinimal, Fraction, Queue, Util, Calc, U8Ext, Brand, };
|
package/dist/bundle.js
CHANGED
|
@@ -2,11 +2,7 @@ const R = new TextEncoder(), p = (n, t) => {
|
|
|
2
2
|
if (typeof n != typeof t) return !1;
|
|
3
3
|
const r = Object.prototype.toString.call(n), e = Object.prototype.toString.call(t);
|
|
4
4
|
if (r !== e) return !1;
|
|
5
|
-
if (typeof n == "string" || typeof n == "bigint" || typeof n == "boolean" || typeof n == "symbol" || typeof n > "u")
|
|
6
|
-
return n === t;
|
|
7
|
-
if (typeof n == "number")
|
|
8
|
-
return Number.isNaN(n) && Number.isNaN(t) || n === t;
|
|
9
|
-
if (n === null) return n === t;
|
|
5
|
+
if (typeof n == "string" || typeof n == "number" || typeof n == "bigint" || typeof n == "boolean" || typeof n == "symbol" || typeof n > "u" || n === null) return n === t;
|
|
10
6
|
if (typeof n == "function")
|
|
11
7
|
throw Error("comparing these objects is still unavailable");
|
|
12
8
|
if (Array.isArray(n) && Array.isArray(t)) {
|
|
@@ -18,20 +14,20 @@ const R = new TextEncoder(), p = (n, t) => {
|
|
|
18
14
|
if (r === "[object Object]") {
|
|
19
15
|
const i = n, s = t, o = Object.getOwnPropertyNames(n), c = Object.getOwnPropertyNames(t);
|
|
20
16
|
o.push(...Object.getOwnPropertySymbols(n)), c.push(...Object.getOwnPropertySymbols(t));
|
|
21
|
-
for (const
|
|
22
|
-
const
|
|
23
|
-
if (
|
|
24
|
-
const [h, d] = [i[
|
|
17
|
+
for (const u of o) {
|
|
18
|
+
const l = c.find((j) => j === u);
|
|
19
|
+
if (l === void 0) return !1;
|
|
20
|
+
const [h, d] = [i[u], s[l]];
|
|
25
21
|
if (!p(h, d)) return !1;
|
|
26
22
|
}
|
|
27
23
|
return !0;
|
|
28
24
|
}
|
|
29
25
|
throw Error("comparing these objects is still unavailable");
|
|
30
|
-
},
|
|
26
|
+
}, T = (n) => new Promise((t) => {
|
|
31
27
|
setTimeout(() => {
|
|
32
28
|
t();
|
|
33
29
|
}, n);
|
|
34
|
-
}),
|
|
30
|
+
}), x = (n) => (...t) => () => n(...t), M = (n) => {
|
|
35
31
|
const t = [];
|
|
36
32
|
let r = [], e = "", i = !1;
|
|
37
33
|
for (let s = 0; s < n.length; s++) {
|
|
@@ -43,20 +39,20 @@ const R = new TextEncoder(), p = (n, t) => {
|
|
|
43
39
|
}, _ = async (n, t) => {
|
|
44
40
|
const r = R.encode(n), e = await crypto.subtle.digest(t, r);
|
|
45
41
|
return new Uint8Array(e);
|
|
46
|
-
},
|
|
42
|
+
}, v = (...n) => n.map((t) => BigInt(t)), b = () => typeof globalThis.process < "u" && typeof globalThis.require < "u", L = (n, t) => Math.floor(Math.random() * (t - n) + n), a = (n, t) => {
|
|
47
43
|
const r = n % t;
|
|
48
44
|
return r < 0n ? r + t : r;
|
|
49
|
-
},
|
|
45
|
+
}, S = (n, t = !1) => {
|
|
50
46
|
if (!Number.isFinite(n)) throw Error("`length` is not a valid number");
|
|
51
47
|
if (n <= 0) throw Error("`length` must be positive");
|
|
52
48
|
const r = Math.ceil(n / 8), e = crypto.getRandomValues(new Uint8Array(r));
|
|
53
49
|
let i = Array.from(e, (s) => s.toString(2).padStart(8, "0")).join("").slice(0, n);
|
|
54
50
|
return t && (i = i.replace(/^./, "1")), BigInt("0b" + i);
|
|
55
|
-
},
|
|
51
|
+
}, A = (n, t) => {
|
|
56
52
|
if (n >= t) throw Error("rangeError");
|
|
57
53
|
const r = t - n, e = r.toString(2).length, i = (() => {
|
|
58
54
|
for (let o = 0; o < 1e5; o++) {
|
|
59
|
-
const c =
|
|
55
|
+
const c = S(e);
|
|
60
56
|
if (c >= g(2n, BigInt(e), r))
|
|
61
57
|
return c % r;
|
|
62
58
|
}
|
|
@@ -74,7 +70,7 @@ const R = new TextEncoder(), p = (n, t) => {
|
|
|
74
70
|
for (; t > 0n; )
|
|
75
71
|
t & 1n && (e = e * n % r), n = n * n % r, t >>= 1n;
|
|
76
72
|
return e;
|
|
77
|
-
},
|
|
73
|
+
}, E = (n, t) => {
|
|
78
74
|
if (n === 0n && t === 0n) return { x: 0n, y: 0n, gcd: 0n };
|
|
79
75
|
if (n === 0n)
|
|
80
76
|
return t > 0n ? { x: 0n, y: -1n, gcd: t } : { x: 0n, y: 1n, gcd: -t };
|
|
@@ -82,9 +78,9 @@ const R = new TextEncoder(), p = (n, t) => {
|
|
|
82
78
|
return n > 0n ? { x: 1n, y: 0n, gcd: n } : { x: -1n, y: 0n, gcd: -n };
|
|
83
79
|
let [r, e, i] = [1n, 0n, n], [s, o, c] = [0n, -1n, t];
|
|
84
80
|
for (; ; ) {
|
|
85
|
-
const
|
|
86
|
-
if (
|
|
87
|
-
[r, s] = [s, r -
|
|
81
|
+
const u = i / c, l = i - u * c;
|
|
82
|
+
if (l === 0n) break;
|
|
83
|
+
[r, s] = [s, r - u * s], [e, o] = [o, e - u * o], [i, c] = [c, l];
|
|
88
84
|
}
|
|
89
85
|
return c < 0n && (s *= -1n, o *= -1n, c *= -1n), { x: s, y: o, gcd: c };
|
|
90
86
|
}, m = (n, t) => {
|
|
@@ -92,13 +88,13 @@ const R = new TextEncoder(), p = (n, t) => {
|
|
|
92
88
|
const r = BigInt((t - 2n).toString(2).length), e = (t - n) / 2n;
|
|
93
89
|
if (r * e < 63n) {
|
|
94
90
|
let c = n;
|
|
95
|
-
for (let
|
|
96
|
-
c *=
|
|
91
|
+
for (let u = n + 2n; u < t; u += 2n)
|
|
92
|
+
c *= u;
|
|
97
93
|
return c;
|
|
98
94
|
}
|
|
99
95
|
const i = n + e | 1n, s = m(n, i), o = m(i, t);
|
|
100
96
|
return s * o;
|
|
101
|
-
},
|
|
97
|
+
}, V = (n) => {
|
|
102
98
|
let t = 3n, r = 1n, e = 1n;
|
|
103
99
|
const i = BigInt(n.toString(2).length) - 1n;
|
|
104
100
|
for (let s = i - 1n; s > -1n; --s) {
|
|
@@ -110,8 +106,8 @@ const R = new TextEncoder(), p = (n, t) => {
|
|
|
110
106
|
if (n < 0n) throw Error("'n' must be non-negative");
|
|
111
107
|
if (n === 0n) return 1n;
|
|
112
108
|
const t = n - BigInt(n.toString(2).match(/1/g)?.length ?? 0);
|
|
113
|
-
return
|
|
114
|
-
},
|
|
109
|
+
return V(n) << t;
|
|
110
|
+
}, C = (n, t) => BigInt.asUintN(32, n >> (t & 31n) | n << (-t & 31n)), N = (n, t) => (n >>> (t & 31) | n << (-t & 31)) >>> 0, w = (n, t) => {
|
|
115
111
|
if (t < 1n || t % 2n === 0n)
|
|
116
112
|
throw Error("`n` is invalid");
|
|
117
113
|
for (; n < 0n; )
|
|
@@ -136,12 +132,15 @@ const R = new TextEncoder(), p = (n, t) => {
|
|
|
136
132
|
e ** 2n < n ? t = e : r = e;
|
|
137
133
|
}
|
|
138
134
|
return n === t ** 2n || n === (t + 1n) ** 2n;
|
|
139
|
-
},
|
|
140
|
-
class
|
|
135
|
+
}, I = [0x853c49e6748fea9bn, 0xda3e39cb94b95bdbn], D = 0x5851f42d4c957f2dn;
|
|
136
|
+
class X {
|
|
141
137
|
/**
|
|
142
138
|
* length = 2, `[state, increment]`
|
|
143
139
|
*/
|
|
144
140
|
#t = new BigUint64Array(2);
|
|
141
|
+
get [Symbol.toStringTag]() {
|
|
142
|
+
return "PCGMinimal";
|
|
143
|
+
}
|
|
145
144
|
/** シード値の配列を返す */
|
|
146
145
|
static getSeed() {
|
|
147
146
|
return crypto.getRandomValues(new BigUint64Array(2));
|
|
@@ -150,18 +149,18 @@ class Z {
|
|
|
150
149
|
* @param seeds 64bit整数の配列 (長さ2以上), 省略した場合, 常に同じ値によって初期化される
|
|
151
150
|
*/
|
|
152
151
|
constructor(t) {
|
|
153
|
-
t ? (this.#t[1] = t[1] << 1n | 1n, this.#n(), this.#t[0] = t[0], this.#n()) : (this.#t[0] =
|
|
152
|
+
t ? (this.#t[1] = t[1] << 1n | 1n, this.#n(), this.#t[0] = t[0], this.#n()) : (this.#t[0] = I[0], this.#t[1] = I[1]);
|
|
154
153
|
}
|
|
155
154
|
/** 内部状態を1サイクル進める */
|
|
156
155
|
#n() {
|
|
157
|
-
this.#t[0] = this.#t[0] *
|
|
156
|
+
this.#t[0] = this.#t[0] * D + this.#t[1];
|
|
158
157
|
}
|
|
159
158
|
/** 32bit 乱数を返す (内部状態は変わらない) */
|
|
160
159
|
get #r() {
|
|
161
160
|
const t = this.#t[0];
|
|
162
161
|
if (!t) throw Error("empty state");
|
|
163
162
|
const r = t >> 59n, e = BigInt.asUintN(32, (t ^ t >> 18n) >> 27n);
|
|
164
|
-
return
|
|
163
|
+
return N(Number(e), Number(r));
|
|
165
164
|
}
|
|
166
165
|
/** 内部状態を1進め、乱数を返す \
|
|
167
166
|
* 普通はこれを使う
|
|
@@ -188,7 +187,7 @@ class Z {
|
|
|
188
187
|
yield typeof r == "number" ? this.getBoundedRand(r) : this.getRand();
|
|
189
188
|
}
|
|
190
189
|
}
|
|
191
|
-
const
|
|
190
|
+
const $ = (n) => {
|
|
192
191
|
if (n <= 1n) return !1;
|
|
193
192
|
if (n % 2n === 0n) return n === 2n;
|
|
194
193
|
let t = n - 1n, r = 0n;
|
|
@@ -202,7 +201,7 @@ const K = (n) => {
|
|
|
202
201
|
o = o * o % n;
|
|
203
202
|
}
|
|
204
203
|
return !1;
|
|
205
|
-
},
|
|
204
|
+
}, K = (n) => {
|
|
206
205
|
let t = 5n, r = w(t, n);
|
|
207
206
|
for (; r > 0n; ) {
|
|
208
207
|
if (t = t > 0n ? t + 2n : t - 2n, t *= -1n, t === -15n && P(n))
|
|
@@ -216,21 +215,21 @@ const K = (n) => {
|
|
|
216
215
|
for (const c of o)
|
|
217
216
|
[i, s] = [a(i * s, t), y(s * s + e * i * i, t)], c === "1" && ([i, s] = [y(r * i + s, t), y(e * i + r * s, t)]);
|
|
218
217
|
return [i, s];
|
|
219
|
-
},
|
|
218
|
+
}, G = (n, t, r, e) => {
|
|
220
219
|
if (n % 2n !== 1n) throw Error("`n` must be odd");
|
|
221
220
|
let i = n + 1n, s = 0n;
|
|
222
221
|
for (; i % 2n === 0n; )
|
|
223
222
|
i >>= 1n, s += 1n;
|
|
224
223
|
const [o, c] = J(i, n, r, t);
|
|
225
|
-
let
|
|
224
|
+
let u = c;
|
|
226
225
|
if (o === 0n) return !0;
|
|
227
226
|
e = g(e, i, n);
|
|
228
|
-
for (let
|
|
229
|
-
if (
|
|
230
|
-
|
|
227
|
+
for (let l = 0n; l < s; l++) {
|
|
228
|
+
if (u === 0n) return !0;
|
|
229
|
+
u = a(u * u - 2n * e, n), e = g(e, 2n, n);
|
|
231
230
|
}
|
|
232
231
|
return !1;
|
|
233
|
-
},
|
|
232
|
+
}, B = (n) => {
|
|
234
233
|
if (n <= 1n) return !1;
|
|
235
234
|
if (n % 2n === 0n) return n === 2n;
|
|
236
235
|
const t = [
|
|
@@ -264,36 +263,39 @@ const K = (n) => {
|
|
|
264
263
|
for (const s of t)
|
|
265
264
|
if (n % s === 0n)
|
|
266
265
|
return n === s;
|
|
267
|
-
if (
|
|
266
|
+
if (!$(n))
|
|
268
267
|
return !1;
|
|
269
|
-
const [r, e] =
|
|
268
|
+
const [r, e] = K(n);
|
|
270
269
|
if (e === 0n) return !1;
|
|
271
270
|
const i = (1n - r) / 4n;
|
|
272
|
-
return
|
|
273
|
-
},
|
|
271
|
+
return G(n, r, 1n, i);
|
|
272
|
+
}, k = (n, t) => {
|
|
274
273
|
if (t < 2n)
|
|
275
274
|
throw Error("noPrimesFound");
|
|
276
275
|
for (let e = 0; e < 1e5; e++) {
|
|
277
|
-
const i =
|
|
278
|
-
if (
|
|
276
|
+
const i = A(n, t);
|
|
277
|
+
if (B(i)) return i;
|
|
279
278
|
}
|
|
280
279
|
throw Error("noPrimesFound");
|
|
281
|
-
},
|
|
280
|
+
}, z = (n, t = !1) => {
|
|
282
281
|
if (n < 2)
|
|
283
282
|
throw Error("noPrimesFound");
|
|
284
283
|
for (let e = 0; e < 1e5; e++) {
|
|
285
|
-
const i =
|
|
286
|
-
if (
|
|
284
|
+
const i = S(n, t);
|
|
285
|
+
if (B(i)) return i;
|
|
287
286
|
}
|
|
288
287
|
throw Error("noPrimesFound");
|
|
289
288
|
};
|
|
290
289
|
class f {
|
|
291
290
|
#t;
|
|
292
291
|
#n;
|
|
292
|
+
get [Symbol.toStringTag]() {
|
|
293
|
+
return "Fraction";
|
|
294
|
+
}
|
|
293
295
|
/**
|
|
294
|
-
*
|
|
295
|
-
* @param numerator
|
|
296
|
-
* @param denominator
|
|
296
|
+
*
|
|
297
|
+
* @param numerator
|
|
298
|
+
* @param denominator
|
|
297
299
|
*/
|
|
298
300
|
constructor(t, r) {
|
|
299
301
|
r === 0n ? (this.#n = 0n, this.#t = t === 0n ? 0n : t > 0n ? 1n : -1n) : r > 0n ? (this.#t = t, this.#n = r) : (this.#t = -t, this.#n = -r), this.#r();
|
|
@@ -314,20 +316,20 @@ class f {
|
|
|
314
316
|
if (s === 0)
|
|
315
317
|
return new f(e ? -i : i, 1n);
|
|
316
318
|
t = 1 / s;
|
|
317
|
-
let [o, c] = [1n, i], [
|
|
318
|
-
for (; `${
|
|
319
|
+
let [o, c] = [1n, i], [u, l] = [0n, 1n];
|
|
320
|
+
for (; `${l}`.length < r + 1; ) {
|
|
319
321
|
const h = BigInt(Math.floor(t)), d = t - Number(h);
|
|
320
|
-
if ([o, c] = [c, h * c + o], [
|
|
321
|
-
return new f(e ? -c : c,
|
|
322
|
+
if ([o, c] = [c, h * c + o], [u, l] = [l, h * l + u], d === 0)
|
|
323
|
+
return new f(e ? -c : c, l);
|
|
322
324
|
t = 1 / d;
|
|
323
325
|
}
|
|
324
|
-
return new f(e ? -o : o,
|
|
326
|
+
return new f(e ? -o : o, u);
|
|
325
327
|
}
|
|
326
328
|
/**
|
|
327
329
|
* reduction
|
|
328
330
|
*/
|
|
329
331
|
#r() {
|
|
330
|
-
const { gcd: t } =
|
|
332
|
+
const { gcd: t } = E(this.#t, this.#n);
|
|
331
333
|
t !== 0n && (this.#t /= t, this.#n /= t);
|
|
332
334
|
}
|
|
333
335
|
/**
|
|
@@ -359,7 +361,7 @@ class f {
|
|
|
359
361
|
* @returns
|
|
360
362
|
*/
|
|
361
363
|
substr(t) {
|
|
362
|
-
return
|
|
364
|
+
return this.add(t.minus());
|
|
363
365
|
}
|
|
364
366
|
/**
|
|
365
367
|
* returns `this * right`
|
|
@@ -376,14 +378,12 @@ class f {
|
|
|
376
378
|
* @returns
|
|
377
379
|
*/
|
|
378
380
|
divide(t) {
|
|
379
|
-
return
|
|
380
|
-
t.inverse()
|
|
381
|
-
);
|
|
381
|
+
return this.multiply(t.inverse());
|
|
382
382
|
}
|
|
383
383
|
/**
|
|
384
384
|
* mediant
|
|
385
|
-
* @param right
|
|
386
|
-
* @returns
|
|
385
|
+
* @param right
|
|
386
|
+
* @returns
|
|
387
387
|
*/
|
|
388
388
|
mediant(t) {
|
|
389
389
|
const r = this.#n + t.#n, e = this.#t + t.#t;
|
|
@@ -409,16 +409,19 @@ class f {
|
|
|
409
409
|
toJSON() {
|
|
410
410
|
return {
|
|
411
411
|
type: "Fraction",
|
|
412
|
-
|
|
413
|
-
denominator: "0x" + this.#n.toString(16)
|
|
412
|
+
value: ["0x" + this.#t.toString(16), "0x" + this.#n.toString(16)]
|
|
414
413
|
};
|
|
415
414
|
}
|
|
415
|
+
static fromData(t) {
|
|
416
|
+
const r = BigInt(t.value[0]), e = BigInt(t.value[1]);
|
|
417
|
+
return new f(r, e);
|
|
418
|
+
}
|
|
416
419
|
static parse(t) {
|
|
417
|
-
const r = JSON.parse(t), { type: e,
|
|
420
|
+
const r = JSON.parse(t), { type: e, value: i } = r;
|
|
418
421
|
if (e !== "Fraction") throw Error("cannot parse");
|
|
419
|
-
if (
|
|
420
|
-
const
|
|
421
|
-
return new f(
|
|
422
|
+
if (Array.isArray(i) && i.length === 2) {
|
|
423
|
+
const s = BigInt(i[0]), o = BigInt(i[1]);
|
|
424
|
+
return new f(s, o);
|
|
422
425
|
} else
|
|
423
426
|
throw Error("cannot parse");
|
|
424
427
|
}
|
|
@@ -431,86 +434,119 @@ const H = (n) => {
|
|
|
431
434
|
}, W = (n) => {
|
|
432
435
|
const t = Array.from(n, (r) => r.toString(16).padStart(2, "0")).join("") || "00";
|
|
433
436
|
return BigInt("0x" + t);
|
|
434
|
-
},
|
|
437
|
+
}, O = (n) => {
|
|
435
438
|
if (b())
|
|
436
439
|
return Buffer.copyBytesFrom(n).toString("base64");
|
|
437
440
|
{
|
|
438
441
|
const t = Array.from(n, (r) => String.fromCharCode(r)).join("");
|
|
439
442
|
return btoa(t);
|
|
440
443
|
}
|
|
441
|
-
},
|
|
444
|
+
}, U = (n) => {
|
|
442
445
|
if (b()) {
|
|
443
446
|
const t = Buffer.from(n, "base64");
|
|
444
447
|
return new Uint8Array(t.buffer, t.byteOffset, t.byteLength);
|
|
445
448
|
} else
|
|
446
449
|
return Uint8Array.from(atob(n), (t) => t.charCodeAt(0));
|
|
447
|
-
},
|
|
450
|
+
}, F = (n) => O(n).replaceAll(/\+/g, "-").replaceAll(/\//g, "_").replaceAll(/=*$/g, ""), Q = (n) => {
|
|
448
451
|
const t = n.length & 3;
|
|
449
452
|
t > 0 && (n = n + "=".repeat(4 - t));
|
|
450
453
|
const r = n.replaceAll(/-/g, "+").replaceAll(/_/g, "/");
|
|
451
|
-
return
|
|
452
|
-
},
|
|
454
|
+
return U(r);
|
|
455
|
+
}, Y = Symbol("for-branded-type");
|
|
456
|
+
class Z {
|
|
457
|
+
get [Symbol.toStringTag]() {
|
|
458
|
+
return "Queue";
|
|
459
|
+
}
|
|
460
|
+
#t;
|
|
461
|
+
#n = [];
|
|
462
|
+
constructor(...t) {
|
|
463
|
+
this.#t = t;
|
|
464
|
+
}
|
|
465
|
+
enqueue(...t) {
|
|
466
|
+
return this.#t.push(...t), this.#t.length + this.#n.length;
|
|
467
|
+
}
|
|
468
|
+
dequeue() {
|
|
469
|
+
if (this.#n.length === 0)
|
|
470
|
+
for (; this.#t.length > 0; ) {
|
|
471
|
+
const t = this.#t.pop();
|
|
472
|
+
t !== void 0 && this.#n.push(t);
|
|
473
|
+
}
|
|
474
|
+
return this.#n.pop();
|
|
475
|
+
}
|
|
476
|
+
toArray() {
|
|
477
|
+
return this.#n.slice().reverse().concat(this.#t);
|
|
478
|
+
}
|
|
479
|
+
toJSON() {
|
|
480
|
+
return this.toArray();
|
|
481
|
+
}
|
|
482
|
+
toString() {
|
|
483
|
+
return this.toArray().toString();
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
const tt = {
|
|
453
487
|
isEqual: p,
|
|
454
|
-
sleep:
|
|
455
|
-
lazify:
|
|
456
|
-
parseCSV:
|
|
488
|
+
sleep: T,
|
|
489
|
+
lazify: x,
|
|
490
|
+
parseCSV: M,
|
|
457
491
|
getHash: _,
|
|
458
492
|
isNode: b,
|
|
459
|
-
toBigInt:
|
|
460
|
-
},
|
|
461
|
-
getRndInt:
|
|
493
|
+
toBigInt: v
|
|
494
|
+
}, nt = {
|
|
495
|
+
getRndInt: L,
|
|
462
496
|
residue: a,
|
|
463
|
-
getRandBIByBitLength:
|
|
464
|
-
getRandBIByRange:
|
|
497
|
+
getRandBIByBitLength: S,
|
|
498
|
+
getRandBIByRange: A,
|
|
465
499
|
modPow: g,
|
|
466
|
-
exEuclidean:
|
|
500
|
+
exEuclidean: E,
|
|
467
501
|
factorial: q,
|
|
468
|
-
rot32:
|
|
469
|
-
rot32BI:
|
|
502
|
+
rot32: N,
|
|
503
|
+
rot32BI: C,
|
|
470
504
|
jacobiSymbol: w,
|
|
471
505
|
isSquare: P,
|
|
472
|
-
bailliePSW:
|
|
473
|
-
getRandPrimeByBitLength:
|
|
474
|
-
getRandPrimeByRange:
|
|
475
|
-
},
|
|
476
|
-
base64ToU8Arr:
|
|
477
|
-
base64UrlToU8Arr:
|
|
478
|
-
u8ArrToBase64:
|
|
479
|
-
u8ArrToBase64Url:
|
|
506
|
+
bailliePSW: B,
|
|
507
|
+
getRandPrimeByBitLength: z,
|
|
508
|
+
getRandPrimeByRange: k
|
|
509
|
+
}, rt = {
|
|
510
|
+
base64ToU8Arr: U,
|
|
511
|
+
base64UrlToU8Arr: Q,
|
|
512
|
+
u8ArrToBase64: O,
|
|
513
|
+
u8ArrToBase64Url: F,
|
|
480
514
|
BItoU8Arr: H,
|
|
481
515
|
u8ArrToBI: W
|
|
482
516
|
};
|
|
483
517
|
export {
|
|
484
518
|
H as BItoU8Arr,
|
|
485
|
-
|
|
519
|
+
Y as Brand,
|
|
520
|
+
nt as Calc,
|
|
486
521
|
f as Fraction,
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
522
|
+
X as PCGMinimal,
|
|
523
|
+
Z as Queue,
|
|
524
|
+
rt as U8Ext,
|
|
525
|
+
tt as Util,
|
|
526
|
+
B as bailliePSW,
|
|
527
|
+
U as base64ToU8Arr,
|
|
528
|
+
Q as base64UrlToU8Arr,
|
|
529
|
+
E as exEuclidean,
|
|
494
530
|
q as factorial,
|
|
495
531
|
_ as getHash,
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
532
|
+
S as getRandBIByBitLength,
|
|
533
|
+
A as getRandBIByRange,
|
|
534
|
+
z as getRandPrimeByBitLength,
|
|
535
|
+
k as getRandPrimeByRange,
|
|
536
|
+
L as getRndInt,
|
|
501
537
|
p as isEqual,
|
|
502
538
|
b as isNode,
|
|
503
539
|
P as isSquare,
|
|
504
540
|
w as jacobiSymbol,
|
|
505
|
-
|
|
541
|
+
x as lazify,
|
|
506
542
|
g as modPow,
|
|
507
|
-
|
|
543
|
+
M as parseCSV,
|
|
508
544
|
a as residue,
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
545
|
+
N as rot32,
|
|
546
|
+
C as rot32BI,
|
|
547
|
+
T as sleep,
|
|
548
|
+
v as toBigInt,
|
|
513
549
|
W as u8ArrToBI,
|
|
514
|
-
|
|
515
|
-
|
|
550
|
+
O as u8ArrToBase64,
|
|
551
|
+
F as u8ArrToBase64Url
|
|
516
552
|
};
|
package/dist/bundle.umd.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(function(
|
|
2
|
-
`&&!i?(e.push(r.trim()),t.push(e),e=[],r=""):r+=
|
|
1
|
+
(function(o,d){typeof exports=="object"&&typeof module<"u"?d(exports):typeof define=="function"&&define.amd?define(["exports"],d):(o=typeof globalThis<"u"?globalThis:o||self,d(o["util-fns"]={}))})(this,(function(o){"use strict";const d=new TextEncoder,m=(n,t)=>{if(typeof n!=typeof t)return!1;const e=Object.prototype.toString.call(n),r=Object.prototype.toString.call(t);if(e!==r)return!1;if(typeof n=="string"||typeof n=="number"||typeof n=="bigint"||typeof n=="boolean"||typeof n=="symbol"||typeof n>"u"||n===null)return n===t;if(typeof n=="function")throw Error("comparing these objects is still unavailable");if(Array.isArray(n)&&Array.isArray(t)){if(n.length!==t.length)return!1;for(let i=0;i<n.length;i++)if(!m(n[i],t[i]))return!1;return!0}if(e==="[object Object]"){const i=n,s=t,u=Object.getOwnPropertyNames(n),l=Object.getOwnPropertyNames(t);u.push(...Object.getOwnPropertySymbols(n)),l.push(...Object.getOwnPropertySymbols(t));for(const c of u){const h=l.find(rt=>rt===c);if(h===void 0)return!1;const[y,S]=[i[c],s[h]];if(!m(y,S))return!1}return!0}throw Error("comparing these objects is still unavailable")},j=n=>new Promise(t=>{setTimeout(()=>{t()},n)}),O=n=>(...t)=>()=>n(...t),M=n=>{const t=[];let e=[],r="",i=!1;for(let s=0;s<n.length;s++){const u=n[s];u==='"'&&(s===0||n[s-1]!=="\\")?i=!i:u===","&&!i?(e.push(r.trim()),r=""):u===`
|
|
2
|
+
`&&!i?(e.push(r.trim()),t.push(e),e=[],r=""):r+=u}return e.push(r.trim()),t.push(e),t},_=async(n,t)=>{const e=d.encode(n),r=await crypto.subtle.digest(t,e);return new Uint8Array(r)},C=(...n)=>n.map(t=>BigInt(t)),w=()=>typeof globalThis.process<"u"&&typeof globalThis.require<"u",L=(n,t)=>Math.floor(Math.random()*(t-n)+n),a=(n,t)=>{const e=n%t;return e<0n?e+t:e},b=(n,t=!1)=>{if(!Number.isFinite(n))throw Error("`length` is not a valid number");if(n<=0)throw Error("`length` must be positive");const e=Math.ceil(n/8),r=crypto.getRandomValues(new Uint8Array(e));let i=Array.from(r,s=>s.toString(2).padStart(8,"0")).join("").slice(0,n);return t&&(i=i.replace(/^./,"1")),BigInt("0b"+i)},I=(n,t)=>{if(n>=t)throw Error("rangeError");const e=t-n,r=e.toString(2).length,i=(()=>{for(let u=0;u<1e5;u++){const l=b(r);if(l>=g(2n,BigInt(r),e))return l%e}throw Error("Failed to generate a random bigint")})();return n+i},g=(n,t,e)=>{if(e<1n)throw Error("`mod` must be positive");if(t<0n)throw Error("`power` must not be negative");for(;n<0n;)n+=e;if(e===1n)return 0n;if(n%e===1n||n%e===0n)return n;if(n===e-1n)return t&1n?e-1n:1n;let r=1n;for(;t>0n;)t&1n&&(r=r*n%e),n=n*n%e,t>>=1n;return r},A=(n,t)=>{if(n===0n&&t===0n)return{x:0n,y:0n,gcd:0n};if(n===0n)return t>0n?{x:0n,y:-1n,gcd:t}:{x:0n,y:1n,gcd:-t};if(t===0n)return n>0n?{x:1n,y:0n,gcd:n}:{x:-1n,y:0n,gcd:-n};let[e,r,i]=[1n,0n,n],[s,u,l]=[0n,-1n,t];for(;;){const c=i/l,h=i-c*l;if(h===0n)break;[e,s]=[s,e-c*s],[r,u]=[u,r-c*u],[i,l]=[l,h]}return l<0n&&(s*=-1n,u*=-1n,l*=-1n),{x:s,y:u,gcd:l}},E=(n,t)=>{if(n>=t)return 1n;const e=BigInt((t-2n).toString(2).length),r=(t-n)/2n;if(e*r<63n){let l=n;for(let c=n+2n;c<t;c+=2n)l*=c;return l}const i=n+r|1n,s=E(n,i),u=E(i,t);return s*u},H=n=>{let t=3n,e=1n,r=1n;const i=BigInt(n.toString(2).length)-1n;for(let s=i-1n;s>-1n;--s){const u=(n>>s)+1n|1n;r*=E(t,u),t=u,e*=r}return e},q=n=>{if(n<0n)throw Error("'n' must be non-negative");if(n===0n)return 1n;const t=n-BigInt(n.toString(2).match(/1/g)?.length??0);return H(n)<<t},v=(n,t)=>BigInt.asUintN(32,n>>(t&31n)|n<<(-t&31n)),P=(n,t)=>(n>>>(t&31)|n<<(-t&31))>>>0,p=(n,t)=>{if(t<1n||t%2n===0n)throw Error("`n` is invalid");for(;n<0n;)n+=t;n%=t;let e=1n;for(;n!==0n;){for(;n%2n===0n;){n/=2n;const r=t%8n;(r===3n||r===5n)&&(e*=-1n)}[n,t]=[t,n],n%4n===3n&&t%4n===3n&&(e*=-1n),n%=t}return t===1n?e:0n},N=n=>{if(n<0n)return!1;if(n===0n)return!0;let t=1n,e=n;for(;t+1n<e;){const r=(t+e)/2n;r**2n<n?t=r:e=r}return n===t**2n||n===(t+1n)**2n},V=[0x853c49e6748fea9bn,0xda3e39cb94b95bdbn],W=0x5851f42d4c957f2dn;class k{#t=new BigUint64Array(2);get[Symbol.toStringTag](){return"PCGMinimal"}static getSeed(){return crypto.getRandomValues(new BigUint64Array(2))}constructor(t){t?(this.#t[1]=t[1]<<1n|1n,this.#n(),this.#t[0]=t[0],this.#n()):(this.#t[0]=V[0],this.#t[1]=V[1])}#n(){this.#t[0]=this.#t[0]*W+this.#t[1]}get#e(){const t=this.#t[0];if(!t)throw Error("empty state");const e=t>>59n,r=BigInt.asUintN(32,(t^t>>18n)>>27n);return P(Number(r),Number(e))}getRand(){return this.#n(),this.#e}getBoundedRand(t){if(t>4294967296)throw Error("`bound` exceeded limit (2^32)");const r=4294967296%t;for(;;){const i=this.getRand();if(i>=r)return i%t}}*genRands(t,e){for(let r=0;r<t;r++)yield typeof e=="number"?this.getBoundedRand(e):this.getRand()}}const F=n=>{if(n<=1n)return!1;if(n%2n===0n)return n===2n;let t=n-1n,e=0n;for(;t%2n===0n;)t>>=1n,e+=1n;const[r,i]=[t,e];let u=g(2n,r,n);if(u===1n)return!0;for(let l=0n;l<i;l++){if(u===n-1n)return!0;u=u*u%n}return!1},Q=n=>{let t=5n,e=p(t,n);for(;e>0n;){if(t=t>0n?t+2n:t-2n,t*=-1n,t===-15n&&N(n))return[0n,0n];e=p(t,n)}return[t,e]},T=(n,t)=>(n&1n)===1n?a(n+t>>1n,t):a(n>>1n,t),X=(n,t,e,r)=>{let i=1n,s=e;const u=n.toString(2).slice(1);for(const l of u)[i,s]=[a(i*s,t),T(s*s+r*i*i,t)],l==="1"&&([i,s]=[T(e*i+s,t),T(r*i+e*s,t)]);return[i,s]},Y=(n,t,e,r)=>{if(n%2n!==1n)throw Error("`n` must be odd");let i=n+1n,s=0n;for(;i%2n===0n;)i>>=1n,s+=1n;const[u,l]=X(i,n,e,t);let c=l;if(u===0n)return!0;r=g(r,i,n);for(let h=0n;h<s;h++){if(c===0n)return!0;c=a(c*c-2n*r,n),r=g(r,2n,n)}return!1},B=n=>{if(n<=1n)return!1;if(n%2n===0n)return n===2n;const t=[2n,3n,5n,7n,11n,13n,17n,19n,23n,29n,31n,37n,41n,43n,47n,53n,59n,61n,67n,71n,73n,79n,83n,89n,97n,101n];for(const s of t)if(n%s===0n)return n===s;if(!F(n))return!1;const[e,r]=Q(n);if(r===0n)return!1;const i=(1n-e)/4n;return Y(n,e,1n,i)},D=(n,t)=>{if(t<2n)throw Error("noPrimesFound");for(let r=0;r<1e5;r++){const i=I(n,t);if(B(i))return i}throw Error("noPrimesFound")},$=(n,t=!1)=>{if(n<2)throw Error("noPrimesFound");for(let r=0;r<1e5;r++){const i=b(n,t);if(B(i))return i}throw Error("noPrimesFound")};class f{#t;#n;get[Symbol.toStringTag](){return"Fraction"}constructor(t,e){e===0n?(this.#n=0n,this.#t=t===0n?0n:t>0n?1n:-1n):e>0n?(this.#t=t,this.#n=e):(this.#t=-t,this.#n=-e),this.#e()}static fromDecimal(t,e=20){if(Number.isNaN(t))return new f(0n,0n);if(Math.abs(t)===1/0)return new f(t>0?1n:-1n,0n);const r=t<0;r&&(t*=-1);const i=BigInt(Math.floor(t)),s=t-Number(i);if(s===0)return new f(r?-i:i,1n);t=1/s;let[u,l]=[1n,i],[c,h]=[0n,1n];for(;`${h}`.length<e+1;){const y=BigInt(Math.floor(t)),S=t-Number(y);if([u,l]=[l,y*l+u],[c,h]=[h,y*h+c],S===0)return new f(r?-l:l,h);t=1/S}return new f(r?-u:u,c)}#e(){const{gcd:t}=A(this.#t,this.#n);t!==0n&&(this.#t/=t,this.#n/=t)}minus(){return new f(-this.#t,this.#n)}inverse(){return new f(this.#n,this.#t)}add(t){const e=this.#n*t.#n,r=this.#t*t.#n+t.#t*this.#n;return new f(r,e)}substr(t){return this.add(t.minus())}multiply(t){const e=this.#n*t.#n,r=this.#t*t.#t;return new f(r,e)}divide(t){return this.multiply(t.inverse())}mediant(t){const e=this.#n+t.#n,r=this.#t+t.#t;return new f(r,e)}toDecimal(){return Number(this.#t)/Number(this.#n)}toString(){return this.#t===0n&&this.#n===0n?"NaN":this.#t===0n?"0":this.#n===0n?this.#t<0n?"-Infinity":"Infinity":this.#n===1n?`${this.#t}`:`${this.#t}/${this.#n}`}valueOf(){return this.toDecimal()}toJSON(){return{type:"Fraction",value:["0x"+this.#t.toString(16),"0x"+this.#n.toString(16)]}}static fromData(t){const e=BigInt(t.value[0]),r=BigInt(t.value[1]);return new f(e,r)}static parse(t){const e=JSON.parse(t),{type:r,value:i}=e;if(r!=="Fraction")throw Error("cannot parse");if(Array.isArray(i)&&i.length===2){const s=BigInt(i[0]),u=BigInt(i[1]);return new f(s,u)}else throw Error("cannot parse")}}const K=n=>{let t=n.toString(16);t.length&1&&(t="0"+t);const e=[...t.match(/.{2}/g)??[]];return Uint8Array.from(e,r=>Number.parseInt(r,16))},G=n=>{const t=Array.from(n,e=>e.toString(16).padStart(2,"0")).join("")||"00";return BigInt("0x"+t)},U=n=>{if(w())return Buffer.copyBytesFrom(n).toString("base64");{const t=Array.from(n,e=>String.fromCharCode(e)).join("");return btoa(t)}},R=n=>{if(w()){const t=Buffer.from(n,"base64");return new Uint8Array(t.buffer,t.byteOffset,t.byteLength)}else return Uint8Array.from(atob(n),t=>t.charCodeAt(0))},J=n=>U(n).replaceAll(/\+/g,"-").replaceAll(/\//g,"_").replaceAll(/=*$/g,""),z=n=>{const t=n.length&3;t>0&&(n=n+"=".repeat(4-t));const e=n.replaceAll(/-/g,"+").replaceAll(/_/g,"/");return R(e)},Z=Symbol("for-branded-type");class x{get[Symbol.toStringTag](){return"Queue"}#t;#n=[];constructor(...t){this.#t=t}enqueue(...t){return this.#t.push(...t),this.#t.length+this.#n.length}dequeue(){if(this.#n.length===0)for(;this.#t.length>0;){const t=this.#t.pop();t!==void 0&&this.#n.push(t)}return this.#n.pop()}toArray(){return this.#n.slice().reverse().concat(this.#t)}toJSON(){return this.toArray()}toString(){return this.toArray().toString()}}const tt={isEqual:m,sleep:j,lazify:O,parseCSV:M,getHash:_,isNode:w,toBigInt:C},nt={getRndInt:L,residue:a,getRandBIByBitLength:b,getRandBIByRange:I,modPow:g,exEuclidean:A,factorial:q,rot32:P,rot32BI:v,jacobiSymbol:p,isSquare:N,bailliePSW:B,getRandPrimeByBitLength:$,getRandPrimeByRange:D},et={base64ToU8Arr:R,base64UrlToU8Arr:z,u8ArrToBase64:U,u8ArrToBase64Url:J,BItoU8Arr:K,u8ArrToBI:G};o.BItoU8Arr=K,o.Brand=Z,o.Calc=nt,o.Fraction=f,o.PCGMinimal=k,o.Queue=x,o.U8Ext=et,o.Util=tt,o.bailliePSW=B,o.base64ToU8Arr=R,o.base64UrlToU8Arr=z,o.exEuclidean=A,o.factorial=q,o.getHash=_,o.getRandBIByBitLength=b,o.getRandBIByRange=I,o.getRandPrimeByBitLength=$,o.getRandPrimeByRange=D,o.getRndInt=L,o.isEqual=m,o.isNode=w,o.isSquare=N,o.jacobiSymbol=p,o.lazify=O,o.modPow=g,o.parseCSV=M,o.residue=a,o.rot32=P,o.rot32BI=v,o.sleep=j,o.toBigInt=C,o.u8ArrToBI=G,o.u8ArrToBase64=U,o.u8ArrToBase64Url=J,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})}));
|
package/dist/fraction.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
export type FractionData = {
|
|
2
2
|
type: 'Fraction';
|
|
3
|
-
numerator
|
|
4
|
-
|
|
3
|
+
/** `[numerator, denominator]` */
|
|
4
|
+
value: [string, string];
|
|
5
5
|
};
|
|
6
6
|
declare class Fraction {
|
|
7
7
|
#private;
|
|
8
|
+
get [Symbol.toStringTag](): string;
|
|
8
9
|
/**
|
|
9
10
|
*
|
|
10
11
|
* @param numerator
|
|
@@ -69,6 +70,7 @@ declare class Fraction {
|
|
|
69
70
|
toString(): string;
|
|
70
71
|
valueOf(): number;
|
|
71
72
|
toJSON(): FractionData;
|
|
73
|
+
static fromData(data: FractionData): Fraction;
|
|
72
74
|
static parse(text: string): Fraction;
|
|
73
75
|
}
|
|
74
76
|
export default Fraction;
|
package/dist/pcg-minimal.d.ts
CHANGED
package/dist/queue.d.ts
ADDED