@tktb-tess/util-fns 0.9.2-alpha.0 → 0.9.2-alpha.1
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/bundle.esm.js +896 -0
- package/dist/bundle.js +2 -896
- package/package.json +5 -5
- package/dist/bundle.umd.js +0 -2
|
@@ -0,0 +1,896 @@
|
|
|
1
|
+
const v = new TextEncoder(), Y = (n, t) => [n].includes(t), g = (n, t) => {
|
|
2
|
+
if (typeof n != typeof t) return !1;
|
|
3
|
+
const e = Object.prototype.toString.call(n), r = Object.prototype.toString.call(t);
|
|
4
|
+
if (e !== r) return !1;
|
|
5
|
+
if (typeof n == "string" || typeof n == "bigint" || typeof n == "boolean" || typeof n == "symbol" || n == null)
|
|
6
|
+
return n === t;
|
|
7
|
+
if (typeof n == "number")
|
|
8
|
+
return n !== n && t !== t || n === t;
|
|
9
|
+
if (typeof n == "function")
|
|
10
|
+
return !1;
|
|
11
|
+
if (Array.isArray(n) && Array.isArray(t)) {
|
|
12
|
+
if (n.length !== t.length) return !1;
|
|
13
|
+
for (let s = 0; s < n.length; s++)
|
|
14
|
+
if (!g(n[s], t[s])) return !1;
|
|
15
|
+
return !0;
|
|
16
|
+
}
|
|
17
|
+
if (n instanceof Set && t instanceof Set) {
|
|
18
|
+
const s = [...n.values()], o = [...t.values()];
|
|
19
|
+
return !!g(s, o);
|
|
20
|
+
}
|
|
21
|
+
if (n instanceof Map && t instanceof Map) {
|
|
22
|
+
const s = [...n.keys()], o = [...t.keys()];
|
|
23
|
+
if (!g(s, o)) return !1;
|
|
24
|
+
const i = [...n.values()], c = [...t.values()];
|
|
25
|
+
return !!g(i, c);
|
|
26
|
+
}
|
|
27
|
+
if (e === "[object Object]") {
|
|
28
|
+
const s = n, o = t, i = Object.keys(s), c = Object.keys(o);
|
|
29
|
+
for (const a of i) {
|
|
30
|
+
const l = c.find((d) => d === a);
|
|
31
|
+
if (l === void 0) return !1;
|
|
32
|
+
const [f, h] = [s[a], o[l]];
|
|
33
|
+
if (!g(f, h)) return !1;
|
|
34
|
+
}
|
|
35
|
+
return !0;
|
|
36
|
+
}
|
|
37
|
+
throw Error(`comparing these objects is unavailable: ${n}, ${t}`, {
|
|
38
|
+
cause: [n, t]
|
|
39
|
+
});
|
|
40
|
+
}, G = () => {
|
|
41
|
+
let n, t;
|
|
42
|
+
return {
|
|
43
|
+
promise: new Promise((r, s) => {
|
|
44
|
+
n = r, t = s;
|
|
45
|
+
}),
|
|
46
|
+
resolve: n,
|
|
47
|
+
reject: t
|
|
48
|
+
};
|
|
49
|
+
}, H = (n) => new Promise((t) => {
|
|
50
|
+
setTimeout(() => t(), n);
|
|
51
|
+
}), X = (n) => (...t) => () => n(...t), Z = (n) => {
|
|
52
|
+
const t = [];
|
|
53
|
+
let e = [], r = "", s = !1;
|
|
54
|
+
for (let o = 0; o < n.length; o++) {
|
|
55
|
+
const i = n[o];
|
|
56
|
+
i === '"' && (o === 0 || n[o - 1] !== "\\") ? s = !s : i === "," && !s ? (e.push(r.trim()), r = "") : i === `
|
|
57
|
+
` && !s ? (e.push(r.trim()), t.push(e), e = [], r = "") : r += i;
|
|
58
|
+
}
|
|
59
|
+
return e.push(r.trim()), t.push(e), t;
|
|
60
|
+
}, Q = async (n, t) => {
|
|
61
|
+
const e = v.encode(n), r = await crypto.subtle.digest(t, e);
|
|
62
|
+
return new Uint8Array(r);
|
|
63
|
+
}, tt = () => !!globalThis.process && typeof process.version < "u" && typeof process.versions.node < "u", nt = (n) => encodeURIComponent(n).replace(
|
|
64
|
+
/[!'()*]/g,
|
|
65
|
+
(e) => `%${e.charCodeAt(0).toString(16).toUpperCase()}`
|
|
66
|
+
), et = (n) => {
|
|
67
|
+
if (n.includes("+"))
|
|
68
|
+
throw Error("An input string has '+'");
|
|
69
|
+
return decodeURIComponent(n);
|
|
70
|
+
}, rt = async (n, t) => {
|
|
71
|
+
const r = new Blob([n]).stream().pipeThrough(new CompressionStream(t));
|
|
72
|
+
return new Response(r).bytes();
|
|
73
|
+
}, st = async (n, t) => {
|
|
74
|
+
const r = new Blob([n]).stream().pipeThrough(new DecompressionStream(t));
|
|
75
|
+
return new Response(r).bytes();
|
|
76
|
+
}, ot = (n, t) => Math.floor(Math.random() * (t - n) + n), m = (n, t) => {
|
|
77
|
+
t < 0n && (t *= -1n);
|
|
78
|
+
const e = n % t;
|
|
79
|
+
return e < 0n ? e + t : e;
|
|
80
|
+
}, E = (n, t = !1) => {
|
|
81
|
+
if (!Number.isFinite(n)) throw Error("`length` is not a valid number");
|
|
82
|
+
if (n <= 0) throw Error("`length` must be positive");
|
|
83
|
+
const e = Math.ceil(n / 8), r = crypto.getRandomValues(new Uint8Array(e));
|
|
84
|
+
let s = Array.from(r, (o) => o.toString(2).padStart(8, "0")).join("").slice(0, n);
|
|
85
|
+
return t && (s = s.replace(/^\d/, "1")), BigInt("0b" + s);
|
|
86
|
+
}, _ = (n, t) => {
|
|
87
|
+
if (n >= t) throw Error("rangeError");
|
|
88
|
+
const e = t - n, r = e.toString(2).length, s = (() => {
|
|
89
|
+
for (let i = 0; i < 1e5; i++) {
|
|
90
|
+
const c = E(r);
|
|
91
|
+
if (c >= p(2n, BigInt(r), e))
|
|
92
|
+
return c % e;
|
|
93
|
+
}
|
|
94
|
+
throw Error("Failed to generate a random bigint");
|
|
95
|
+
})();
|
|
96
|
+
return n + s;
|
|
97
|
+
}, p = (n, t, e) => {
|
|
98
|
+
if (e < 1n) throw Error("`mod` must be positive");
|
|
99
|
+
if (t < 0n) throw Error("`power` must not be negative");
|
|
100
|
+
if (n = m(n, e), e === 1n) return 0n;
|
|
101
|
+
if (n % e === 1n || n % e === 0n) return n;
|
|
102
|
+
if (n === e - 1n) return t & 1n ? e - 1n : 1n;
|
|
103
|
+
let r = 1n;
|
|
104
|
+
for (; t > 0n; )
|
|
105
|
+
t & 1n && (r = r * n % e), n = n * n % e, t >>= 1n;
|
|
106
|
+
return r;
|
|
107
|
+
}, j = (n, t) => {
|
|
108
|
+
if (n === 0n && t === 0n) return { x: 0n, y: 0n, gcd: 0n };
|
|
109
|
+
if (n === 0n)
|
|
110
|
+
return t > 0n ? { x: 0n, y: -1n, gcd: t } : { x: 0n, y: 1n, gcd: -t };
|
|
111
|
+
if (t === 0n)
|
|
112
|
+
return n > 0n ? { x: 1n, y: 0n, gcd: n } : { x: -1n, y: 0n, gcd: -n };
|
|
113
|
+
let [e, r, s] = [1n, 0n, n], [o, i, c] = [0n, -1n, t];
|
|
114
|
+
for (; ; ) {
|
|
115
|
+
const a = s / c, l = s - a * c;
|
|
116
|
+
if (l === 0n) break;
|
|
117
|
+
[e, o] = [o, e - a * o], [r, i] = [i, r - a * i], [s, c] = [c, l];
|
|
118
|
+
}
|
|
119
|
+
return c < 0n && (o *= -1n, i *= -1n, c *= -1n), { x: o, y: i, gcd: c };
|
|
120
|
+
}, b = (n, t) => {
|
|
121
|
+
if (n >= t) return 1n;
|
|
122
|
+
const e = BigInt((t - 2n).toString(2).length), r = (t - n) / 2n;
|
|
123
|
+
if (e * r < 63n) {
|
|
124
|
+
let c = n;
|
|
125
|
+
for (let a = n + 2n; a < t; a += 2n)
|
|
126
|
+
c *= a;
|
|
127
|
+
return c;
|
|
128
|
+
}
|
|
129
|
+
const s = n + r | 1n, o = b(n, s), i = b(s, t);
|
|
130
|
+
return o * i;
|
|
131
|
+
}, C = (n) => {
|
|
132
|
+
let t = 3n, e = 1n, r = 1n;
|
|
133
|
+
const s = BigInt(n.toString(2).length) - 1n;
|
|
134
|
+
for (let o = s - 1n; o > -1n; --o) {
|
|
135
|
+
const i = (n >> o) + 1n | 1n;
|
|
136
|
+
r *= b(t, i), t = i, e *= r;
|
|
137
|
+
}
|
|
138
|
+
return e;
|
|
139
|
+
}, it = (n) => {
|
|
140
|
+
if (n < 0n) throw Error("'n' must be non-negative");
|
|
141
|
+
if (n === 0n) return 1n;
|
|
142
|
+
const t = n - BigInt(n.toString(2).match(/1/g)?.length ?? 0);
|
|
143
|
+
return C(n) << t;
|
|
144
|
+
}, F = (n, t) => (n >>> (t & 31) | n << (-t & 31)) >>> 0, I = (n, t) => BigInt.asUintN(64, n >> (t & 63n) | n << (-t & 63n)), U = (n, t) => {
|
|
145
|
+
if (t < 1n || t % 2n === 0n)
|
|
146
|
+
throw Error("`n` is invalid");
|
|
147
|
+
for (; n < 0n; )
|
|
148
|
+
n += t;
|
|
149
|
+
n %= t;
|
|
150
|
+
let e = 1n;
|
|
151
|
+
for (; n !== 0n; ) {
|
|
152
|
+
for (; n % 2n === 0n; ) {
|
|
153
|
+
n /= 2n;
|
|
154
|
+
const r = t % 8n;
|
|
155
|
+
(r === 3n || r === 5n) && (e *= -1n);
|
|
156
|
+
}
|
|
157
|
+
[n, t] = [t, n], n % 4n === 3n && t % 4n === 3n && (e *= -1n), n %= t;
|
|
158
|
+
}
|
|
159
|
+
return t === 1n ? e : 0n;
|
|
160
|
+
}, P = (n) => {
|
|
161
|
+
if (n < 0n) return !1;
|
|
162
|
+
if (n === 0n) return !0;
|
|
163
|
+
let t = 1n, e = n;
|
|
164
|
+
for (; t + 1n < e; ) {
|
|
165
|
+
const r = (t + e) / 2n;
|
|
166
|
+
r ** 2n < n ? t = r : e = r;
|
|
167
|
+
}
|
|
168
|
+
return n === t ** 2n || n === (t + 1n) ** 2n;
|
|
169
|
+
}, O = (n) => {
|
|
170
|
+
if (n <= 1n) return !1;
|
|
171
|
+
if (n % 2n === 0n) return n === 2n;
|
|
172
|
+
let t = n - 1n, e = 0n;
|
|
173
|
+
for (; t % 2n === 0n; )
|
|
174
|
+
t >>= 1n, e += 1n;
|
|
175
|
+
const [r, s] = [t, e];
|
|
176
|
+
let i = p(2n, r, n);
|
|
177
|
+
if (i === 1n) return !0;
|
|
178
|
+
for (let c = 0n; c < s; c++) {
|
|
179
|
+
if (i === n - 1n) return !0;
|
|
180
|
+
i = i * i % n;
|
|
181
|
+
}
|
|
182
|
+
return !1;
|
|
183
|
+
}, V = (n) => {
|
|
184
|
+
let t = 5n, e = U(t, n);
|
|
185
|
+
for (; e > 0n; ) {
|
|
186
|
+
if (t = t > 0n ? t + 2n : t - 2n, t *= -1n, t === -15n && P(n))
|
|
187
|
+
return [0n, 0n];
|
|
188
|
+
e = U(t, n);
|
|
189
|
+
}
|
|
190
|
+
return [t, e];
|
|
191
|
+
}, w = (n, t) => (n & 1n) === 1n ? m(n + t >> 1n, t) : m(n >> 1n, t), $ = (n, t, e, r) => {
|
|
192
|
+
let s = 1n, o = e;
|
|
193
|
+
const i = n.toString(2).slice(1);
|
|
194
|
+
for (const c of i)
|
|
195
|
+
[s, o] = [m(s * o, t), w(o * o + r * s * s, t)], c === "1" && ([s, o] = [w(e * s + o, t), w(r * s + e * o, t)]);
|
|
196
|
+
return [s, o];
|
|
197
|
+
}, k = (n, t, e, r) => {
|
|
198
|
+
if (n % 2n !== 1n) throw Error("`n` must be odd");
|
|
199
|
+
let s = n + 1n, o = 0n;
|
|
200
|
+
for (; s % 2n === 0n; )
|
|
201
|
+
s >>= 1n, o += 1n;
|
|
202
|
+
const [i, c] = $(s, n, e, t);
|
|
203
|
+
let a = c;
|
|
204
|
+
if (i === 0n) return !0;
|
|
205
|
+
r = p(r, s, n);
|
|
206
|
+
for (let l = 0n; l < o; l++) {
|
|
207
|
+
if (a === 0n) return !0;
|
|
208
|
+
a = m(a * a - 2n * r, n), r = p(r, 2n, n);
|
|
209
|
+
}
|
|
210
|
+
return !1;
|
|
211
|
+
}, x = (n) => {
|
|
212
|
+
if (n <= 1n) return !1;
|
|
213
|
+
if (n % 2n === 0n) return n === 2n;
|
|
214
|
+
const t = [
|
|
215
|
+
2n,
|
|
216
|
+
3n,
|
|
217
|
+
5n,
|
|
218
|
+
7n,
|
|
219
|
+
11n,
|
|
220
|
+
13n,
|
|
221
|
+
17n,
|
|
222
|
+
19n,
|
|
223
|
+
23n,
|
|
224
|
+
29n,
|
|
225
|
+
31n,
|
|
226
|
+
37n,
|
|
227
|
+
41n,
|
|
228
|
+
43n,
|
|
229
|
+
47n,
|
|
230
|
+
53n,
|
|
231
|
+
59n,
|
|
232
|
+
61n,
|
|
233
|
+
67n,
|
|
234
|
+
71n,
|
|
235
|
+
73n,
|
|
236
|
+
79n,
|
|
237
|
+
83n,
|
|
238
|
+
89n,
|
|
239
|
+
97n,
|
|
240
|
+
101n
|
|
241
|
+
];
|
|
242
|
+
for (const o of t)
|
|
243
|
+
if (n % o === 0n)
|
|
244
|
+
return n === o;
|
|
245
|
+
if (!O(n))
|
|
246
|
+
return !1;
|
|
247
|
+
const [e, r] = V(n);
|
|
248
|
+
if (r === 0n) return !1;
|
|
249
|
+
const s = (1n - e) / 4n;
|
|
250
|
+
return k(n, e, 1n, s);
|
|
251
|
+
}, ct = (n, t) => {
|
|
252
|
+
if (t < 2n)
|
|
253
|
+
throw Error("noPrimesFound");
|
|
254
|
+
for (let r = 0; r < 1e5; r++) {
|
|
255
|
+
const s = _(n, t);
|
|
256
|
+
if (x(s)) return s;
|
|
257
|
+
}
|
|
258
|
+
throw Error("noPrimesFound");
|
|
259
|
+
}, at = (n, t = !1) => {
|
|
260
|
+
if (n < 2)
|
|
261
|
+
throw Error("noPrimesFound");
|
|
262
|
+
for (let r = 0; r < 1e5; r++) {
|
|
263
|
+
const s = E(n, t);
|
|
264
|
+
if (x(s)) return s;
|
|
265
|
+
}
|
|
266
|
+
throw Error("noPrimesFound");
|
|
267
|
+
};
|
|
268
|
+
class u {
|
|
269
|
+
#t;
|
|
270
|
+
#n;
|
|
271
|
+
static name = "Rational";
|
|
272
|
+
[Symbol.toStringTag] = u.name;
|
|
273
|
+
/**
|
|
274
|
+
* fraction (rational) class
|
|
275
|
+
* @param numerator
|
|
276
|
+
* @param denominator
|
|
277
|
+
*/
|
|
278
|
+
constructor(t, e) {
|
|
279
|
+
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();
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* generates fraction from `number` decimal using continued fraction
|
|
283
|
+
* @param value decimal
|
|
284
|
+
* @param denominatorDigits limit of digits of denominator
|
|
285
|
+
*/
|
|
286
|
+
static fromDecimal(t, e = 5) {
|
|
287
|
+
if (Number.isNaN(t))
|
|
288
|
+
return new u(0n, 0n);
|
|
289
|
+
if (Math.abs(t) === 1 / 0)
|
|
290
|
+
return new u(t > 0 ? 1n : -1n, 0n);
|
|
291
|
+
const r = t < 0;
|
|
292
|
+
r && (t *= -1);
|
|
293
|
+
const s = BigInt(Math.floor(t)), o = t - Number(s);
|
|
294
|
+
if (o === 0)
|
|
295
|
+
return new u(r ? -s : s, 1n);
|
|
296
|
+
t = 1 / o;
|
|
297
|
+
let [i, c] = [1n, s], [a, l] = [0n, 1n];
|
|
298
|
+
for (; `${l}`.length < e + 1; ) {
|
|
299
|
+
const f = BigInt(Math.floor(t)), h = t - Number(f);
|
|
300
|
+
if ([i, c] = [c, f * c + i], [a, l] = [l, f * l + a], h === 0)
|
|
301
|
+
return new u(r ? -c : c, l);
|
|
302
|
+
t = 1 / h;
|
|
303
|
+
}
|
|
304
|
+
return new u(r ? -i : i, a);
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* reduction
|
|
308
|
+
*/
|
|
309
|
+
#e() {
|
|
310
|
+
const { gcd: t } = j(this.#t, this.#n);
|
|
311
|
+
t !== 0n && (this.#t /= t, this.#n /= t);
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* returns a fraction multiplied by -1
|
|
315
|
+
* @returns
|
|
316
|
+
*/
|
|
317
|
+
minus() {
|
|
318
|
+
return new u(-this.#t, this.#n);
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* returns inverse of this fraction
|
|
322
|
+
* @returns
|
|
323
|
+
*/
|
|
324
|
+
inverse() {
|
|
325
|
+
return new u(this.#n, this.#t);
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* returns `this + right`
|
|
329
|
+
* @param right
|
|
330
|
+
* @returns
|
|
331
|
+
*/
|
|
332
|
+
add(t) {
|
|
333
|
+
const e = this.#n * t.#n, r = this.#t * t.#n + t.#t * this.#n;
|
|
334
|
+
return new u(r, e);
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* returns `this - right`
|
|
338
|
+
* @param right
|
|
339
|
+
* @returns
|
|
340
|
+
*/
|
|
341
|
+
substr(t) {
|
|
342
|
+
return this.add(t.minus());
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* returns `this * right`
|
|
346
|
+
* @param right
|
|
347
|
+
* @returns
|
|
348
|
+
*/
|
|
349
|
+
multiply(t) {
|
|
350
|
+
const e = this.#n * t.#n, r = this.#t * t.#t;
|
|
351
|
+
return new u(r, e);
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* returns `this / right`
|
|
355
|
+
* @param right
|
|
356
|
+
* @returns
|
|
357
|
+
*/
|
|
358
|
+
divide(t) {
|
|
359
|
+
return this.multiply(t.inverse());
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* returns mediant
|
|
363
|
+
* @param this `a/b`
|
|
364
|
+
* @param right `c/d`
|
|
365
|
+
* @returns `(a+c)/(b+d)`
|
|
366
|
+
*/
|
|
367
|
+
mediant(t) {
|
|
368
|
+
const e = this.#n + t.#n, r = this.#t + t.#t;
|
|
369
|
+
return new u(r, e);
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* returns `number` type decimal
|
|
373
|
+
* @returns decimal
|
|
374
|
+
*/
|
|
375
|
+
toDecimal() {
|
|
376
|
+
return Number(this.#t) / Number(this.#n);
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* returns `'numerator/denominator'`
|
|
380
|
+
* @returns string form
|
|
381
|
+
*/
|
|
382
|
+
toString() {
|
|
383
|
+
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}`;
|
|
384
|
+
}
|
|
385
|
+
valueOf() {
|
|
386
|
+
return this.toDecimal();
|
|
387
|
+
}
|
|
388
|
+
toJSON() {
|
|
389
|
+
return {
|
|
390
|
+
type: "Rational",
|
|
391
|
+
value: ["0x" + this.#t.toString(16), "0x" + this.#n.toString(16)]
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* makes instance from RationalData
|
|
396
|
+
* @param data
|
|
397
|
+
* @returns
|
|
398
|
+
*/
|
|
399
|
+
static fromData(t) {
|
|
400
|
+
const e = BigInt(t.value[0]), r = BigInt(t.value[1]);
|
|
401
|
+
return new u(e, r);
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* pases string whose form is `'numerator/denominator'`
|
|
405
|
+
* @param fracStr
|
|
406
|
+
* @returns
|
|
407
|
+
*/
|
|
408
|
+
static parse(t) {
|
|
409
|
+
const r = /^\s*(\d+)\s*\/\s*(\d+)\s*$/.exec(t);
|
|
410
|
+
if (!r)
|
|
411
|
+
throw Error("cannot parse");
|
|
412
|
+
const s = r.at(1), o = r.at(2);
|
|
413
|
+
if (!s || !o)
|
|
414
|
+
throw Error("cannot parse");
|
|
415
|
+
return new u(BigInt(s), BigInt(o));
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
const D = new TextEncoder(), q = new TextDecoder(), lt = (n, t) => {
|
|
419
|
+
switch (t) {
|
|
420
|
+
case "utf-8":
|
|
421
|
+
return D.encode(n);
|
|
422
|
+
case "base64":
|
|
423
|
+
return Uint8Array.from(atob(n), (e) => e.charCodeAt(0));
|
|
424
|
+
case "base64url": {
|
|
425
|
+
const e = n.length & 3;
|
|
426
|
+
return e > 0 && (n = n + "=".repeat(4 - e)), n = n.replaceAll("-", "+").replaceAll("_", "/"), Uint8Array.from(atob(n), (r) => r.charCodeAt(0));
|
|
427
|
+
}
|
|
428
|
+
case "hex": {
|
|
429
|
+
n.length & 1 && (n = "0" + n);
|
|
430
|
+
const r = n.matchAll(/.{2}/g);
|
|
431
|
+
return Uint8Array.from(r, (s) => Number.parseInt(s[0], 16));
|
|
432
|
+
}
|
|
433
|
+
case "oct": {
|
|
434
|
+
const e = n.length & 7;
|
|
435
|
+
e && (n = "0".repeat(8 - e) + n);
|
|
436
|
+
const r = n.matchAll(/.{8}/g), o = Array.from(r, (i) => {
|
|
437
|
+
const c = i[0];
|
|
438
|
+
return Number.parseInt(c, 8).toString(16).padStart(6, "0");
|
|
439
|
+
}).join("").matchAll(/.{2}/g);
|
|
440
|
+
return Uint8Array.from(
|
|
441
|
+
o,
|
|
442
|
+
(i) => Number.parseInt(i[0], 16)
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
case "bin": {
|
|
446
|
+
const e = n.length & 7;
|
|
447
|
+
e && (n = "0".repeat(8 - e) + n);
|
|
448
|
+
const r = n.matchAll(/.{8}/g), s = Array.from(r, (o) => Number.parseInt(o[0], 2));
|
|
449
|
+
return Uint8Array.from(s);
|
|
450
|
+
}
|
|
451
|
+
default:
|
|
452
|
+
throw Error(`Invalid encoding: ${t}`, { cause: t });
|
|
453
|
+
}
|
|
454
|
+
}, ut = (n, t) => {
|
|
455
|
+
switch (t) {
|
|
456
|
+
case "utf-8":
|
|
457
|
+
return q.decode(n);
|
|
458
|
+
case "base64": {
|
|
459
|
+
const e = Array.from(n, (r) => String.fromCharCode(r)).join("");
|
|
460
|
+
return btoa(e);
|
|
461
|
+
}
|
|
462
|
+
case "base64url": {
|
|
463
|
+
const e = Array.from(n, (s) => String.fromCharCode(s)).join("");
|
|
464
|
+
return btoa(e).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
|
|
465
|
+
}
|
|
466
|
+
case "hex":
|
|
467
|
+
return Array.from(n, (r) => r.toString(16).padStart(2, "0")).join("").replace(/^0+/, "");
|
|
468
|
+
case "oct": {
|
|
469
|
+
let e = Array.from(
|
|
470
|
+
n,
|
|
471
|
+
(i) => i.toString(16).padStart(2, "0")
|
|
472
|
+
).join("");
|
|
473
|
+
const r = e.length % 6;
|
|
474
|
+
r && (e = "0".repeat(6 - r) + e);
|
|
475
|
+
const s = e.matchAll(/.{6}/g);
|
|
476
|
+
return Array.from(
|
|
477
|
+
s,
|
|
478
|
+
(i) => Number.parseInt(i[0], 16).toString(8).padStart(8, "0")
|
|
479
|
+
).join("").replace(/^0+/, "");
|
|
480
|
+
}
|
|
481
|
+
case "bin":
|
|
482
|
+
return Array.from(n, (r) => r.toString(2).padStart(8, "0")).join("").replace(/^0+/, "");
|
|
483
|
+
default:
|
|
484
|
+
throw Error(`Invalid encoding: ${t}`, { cause: t });
|
|
485
|
+
}
|
|
486
|
+
}, K = (n) => {
|
|
487
|
+
const t = n < 0n;
|
|
488
|
+
t && (n *= -1n);
|
|
489
|
+
const e = n.toString(2).length, r = n === 1n << BigInt(e - 1) && !(e % 7) && t, s = Math.floor(e / 7) + 1;
|
|
490
|
+
return r ? s - 1 : s;
|
|
491
|
+
}, ft = (n) => {
|
|
492
|
+
const t = K(n);
|
|
493
|
+
n = BigInt.asUintN(t * 7, n);
|
|
494
|
+
const e = new Uint8Array(t);
|
|
495
|
+
for (let s = 0; s < t - 1; ++s) {
|
|
496
|
+
const o = Number(n & 127n) | 128;
|
|
497
|
+
e[s] = o, n >>= 7n;
|
|
498
|
+
}
|
|
499
|
+
const r = Number(n & 127n);
|
|
500
|
+
return e[t - 1] = r, n >>= 7n, e;
|
|
501
|
+
}, ht = (n) => {
|
|
502
|
+
const t = n.length;
|
|
503
|
+
let e = 0n;
|
|
504
|
+
for (let r = 0; r < t; ++r) {
|
|
505
|
+
const s = BigInt(n[r] & 127);
|
|
506
|
+
e += s << BigInt(7 * r);
|
|
507
|
+
}
|
|
508
|
+
return BigInt.asIntN(7 * t, e);
|
|
509
|
+
};
|
|
510
|
+
class N {
|
|
511
|
+
#t;
|
|
512
|
+
#n;
|
|
513
|
+
static name = "Queue";
|
|
514
|
+
[Symbol.toStringTag] = N.name;
|
|
515
|
+
/**
|
|
516
|
+
* Queue
|
|
517
|
+
* @param data
|
|
518
|
+
*/
|
|
519
|
+
constructor(...t) {
|
|
520
|
+
this.#t = t, this.#n = [];
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* enqueue data
|
|
524
|
+
* @param data
|
|
525
|
+
* @returns current queue length
|
|
526
|
+
*/
|
|
527
|
+
enqueue(...t) {
|
|
528
|
+
return this.#t.push(...t), this.#t.length + this.#n.length;
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* dequeue data
|
|
532
|
+
* @returns data in the head
|
|
533
|
+
*/
|
|
534
|
+
dequeue() {
|
|
535
|
+
if (this.#n.length === 0)
|
|
536
|
+
for (; this.#t.length > 0; ) {
|
|
537
|
+
const t = this.#t.pop();
|
|
538
|
+
this.#n.push(t);
|
|
539
|
+
}
|
|
540
|
+
return this.#n.pop();
|
|
541
|
+
}
|
|
542
|
+
toArray() {
|
|
543
|
+
return this.#n.slice().reverse().concat(this.#t);
|
|
544
|
+
}
|
|
545
|
+
toJSON() {
|
|
546
|
+
return this.toArray();
|
|
547
|
+
}
|
|
548
|
+
toString() {
|
|
549
|
+
return this.toArray().toString();
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
class R extends Error {
|
|
553
|
+
name;
|
|
554
|
+
static name = "NamedError";
|
|
555
|
+
[Symbol.toStringTag] = R.name;
|
|
556
|
+
constructor(t, e, r) {
|
|
557
|
+
super(e, { cause: r }), this.name = t;
|
|
558
|
+
}
|
|
559
|
+
toJSON() {
|
|
560
|
+
const t = (() => {
|
|
561
|
+
const o = this.cause;
|
|
562
|
+
return typeof o == "string" || typeof o == "number" || typeof o == "boolean" ? o : typeof o == "bigint" ? o.toString() : typeof o == "object" && o !== null ? o instanceof Set || o instanceof Map ? Object.fromEntries(o) : o : void 0;
|
|
563
|
+
})(), { name: e, message: r, stack: s } = this;
|
|
564
|
+
return {
|
|
565
|
+
name: e,
|
|
566
|
+
message: r,
|
|
567
|
+
stack: s,
|
|
568
|
+
cause: t
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
const S = (n) => {
|
|
573
|
+
if (n === 0) return 32;
|
|
574
|
+
let t = 0;
|
|
575
|
+
for (; n > 0 && !(n & 1); )
|
|
576
|
+
++t, n >>>= 1;
|
|
577
|
+
return t;
|
|
578
|
+
}, B = (n) => {
|
|
579
|
+
if (n === 0n) return 64n;
|
|
580
|
+
let t = 0n;
|
|
581
|
+
for (; n > 0n && !(n & 1n); )
|
|
582
|
+
++t, n >>= 1n;
|
|
583
|
+
return BigInt.asUintN(64, t);
|
|
584
|
+
};
|
|
585
|
+
class A {
|
|
586
|
+
#t;
|
|
587
|
+
static name = "FloatRand";
|
|
588
|
+
[Symbol.toStringTag] = A.name;
|
|
589
|
+
constructor(t) {
|
|
590
|
+
this.#t = t;
|
|
591
|
+
}
|
|
592
|
+
#n() {
|
|
593
|
+
const r = (() => {
|
|
594
|
+
switch (this.#t.bits) {
|
|
595
|
+
case 32:
|
|
596
|
+
return this.#t.getU32Rand() >>> 0;
|
|
597
|
+
case 64: {
|
|
598
|
+
const f = this.#t.getU64Rand() & 0xffffffffn;
|
|
599
|
+
return Number(f);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
})(), s = r & 255;
|
|
603
|
+
let o = 126;
|
|
604
|
+
if (s === 0) {
|
|
605
|
+
o -= 8;
|
|
606
|
+
const f = 1e5;
|
|
607
|
+
let h = 0;
|
|
608
|
+
t: for (; ; ) {
|
|
609
|
+
if (h > f)
|
|
610
|
+
throw Error("loop exceeded limit");
|
|
611
|
+
const d = (() => {
|
|
612
|
+
switch (this.#t.bits) {
|
|
613
|
+
case 32:
|
|
614
|
+
return this.#t.getU32Rand() >>> 0;
|
|
615
|
+
case 64: {
|
|
616
|
+
const y = BigInt.asUintN(32, this.#t.getU64Rand());
|
|
617
|
+
return Number(y);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
})();
|
|
621
|
+
if (d === 0) {
|
|
622
|
+
if (o -= 32, o < 0) {
|
|
623
|
+
o = 0;
|
|
624
|
+
break t;
|
|
625
|
+
}
|
|
626
|
+
} else {
|
|
627
|
+
o -= S(d);
|
|
628
|
+
break t;
|
|
629
|
+
}
|
|
630
|
+
++h;
|
|
631
|
+
}
|
|
632
|
+
} else
|
|
633
|
+
o -= S(s);
|
|
634
|
+
const i = r >>> 8 & 8388607;
|
|
635
|
+
i === 0 && r >>> 31 && ++o;
|
|
636
|
+
const { buffer: c, byteOffset: a, length: l } = Uint32Array.from([o << 23 | i]);
|
|
637
|
+
return new Float32Array(c, a, l)[0];
|
|
638
|
+
}
|
|
639
|
+
/**
|
|
640
|
+
* returns a random single-precision floating-point number (float32) in the range of [0.0, 1.0)
|
|
641
|
+
* @returns
|
|
642
|
+
*/
|
|
643
|
+
getF32Rand() {
|
|
644
|
+
for (let e = 0; e < 1e5; ++e) {
|
|
645
|
+
const r = this.#n();
|
|
646
|
+
if (r < 1)
|
|
647
|
+
return r;
|
|
648
|
+
}
|
|
649
|
+
throw Error("exceeded loop limit");
|
|
650
|
+
}
|
|
651
|
+
#e() {
|
|
652
|
+
const t = 0n, e = 1023n, r = (() => {
|
|
653
|
+
switch (this.#t.bits) {
|
|
654
|
+
case 32: {
|
|
655
|
+
const f = BigInt(this.#t.getU32Rand() >>> 0), h = BigInt(this.#t.getU32Rand() >>> 0);
|
|
656
|
+
return f << 32n | h;
|
|
657
|
+
}
|
|
658
|
+
case 64:
|
|
659
|
+
return BigInt.asUintN(64, this.#t.getU64Rand());
|
|
660
|
+
}
|
|
661
|
+
})(), s = r & 0x7ffn;
|
|
662
|
+
let o = e - 1n;
|
|
663
|
+
if (s === 0n) {
|
|
664
|
+
o -= 11n;
|
|
665
|
+
const f = 1e5;
|
|
666
|
+
let h = 0;
|
|
667
|
+
t: for (; ; ) {
|
|
668
|
+
if (h > f)
|
|
669
|
+
throw Error("loop exceeded limit");
|
|
670
|
+
const d = (() => {
|
|
671
|
+
switch (this.#t.bits) {
|
|
672
|
+
case 32: {
|
|
673
|
+
const y = BigInt(this.#t.getU32Rand() >>> 0), L = BigInt(this.#t.getU32Rand() >>> 0);
|
|
674
|
+
return y << 32n | L;
|
|
675
|
+
}
|
|
676
|
+
case 64:
|
|
677
|
+
return BigInt.asUintN(64, this.#t.getU64Rand());
|
|
678
|
+
}
|
|
679
|
+
})();
|
|
680
|
+
if (d === 0n) {
|
|
681
|
+
if (o -= 64n, o < t) {
|
|
682
|
+
o = t;
|
|
683
|
+
break t;
|
|
684
|
+
}
|
|
685
|
+
} else {
|
|
686
|
+
o -= B(d);
|
|
687
|
+
break t;
|
|
688
|
+
}
|
|
689
|
+
++h;
|
|
690
|
+
}
|
|
691
|
+
} else
|
|
692
|
+
o -= B(s);
|
|
693
|
+
const i = BigInt.asUintN(52, r >> 11n);
|
|
694
|
+
i === 0n && r >> 63n && ++o;
|
|
695
|
+
const { buffer: c, byteOffset: a, length: l } = BigUint64Array.from([o << 52n | i]);
|
|
696
|
+
return new Float64Array(c, a, l)[0];
|
|
697
|
+
}
|
|
698
|
+
/**
|
|
699
|
+
* returns a random double-precision floating-point number (float64) in the range of [0.0, 1.0)
|
|
700
|
+
* @returns
|
|
701
|
+
*/
|
|
702
|
+
getF64Rand() {
|
|
703
|
+
for (let e = 0; e < 1e5; ++e) {
|
|
704
|
+
const r = this.#e();
|
|
705
|
+
if (r < 1)
|
|
706
|
+
return r;
|
|
707
|
+
}
|
|
708
|
+
throw Error("exceeded loop limit");
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
const z = [0x853c49e6748fea9bn, 0xda3e39cb94b95bdbn], J = 0x5851f42d4c957f2dn;
|
|
712
|
+
class M {
|
|
713
|
+
/**
|
|
714
|
+
* length = 2, `[state, increment]`
|
|
715
|
+
*/
|
|
716
|
+
#t;
|
|
717
|
+
static name = "PCGMinimal";
|
|
718
|
+
[Symbol.toStringTag] = M.name;
|
|
719
|
+
bits = 32;
|
|
720
|
+
/**
|
|
721
|
+
* @param seeds
|
|
722
|
+
* `BigUint64Array` with length 2. \
|
|
723
|
+
* if it is not given, initialized by default value
|
|
724
|
+
* @example
|
|
725
|
+
* // the following example is always initialized by the same seeds.
|
|
726
|
+
* // not recommended
|
|
727
|
+
* const rng = new PCGMinimal();
|
|
728
|
+
*
|
|
729
|
+
* // you should construct with random seeds.
|
|
730
|
+
* const seed = crypto.getRandomValues(new BigUint64Array(2));
|
|
731
|
+
* const betterRng = new PCGMinimal(seed);
|
|
732
|
+
*/
|
|
733
|
+
constructor(t) {
|
|
734
|
+
t && t.length >= 2 ? (this.#t = new BigUint64Array(2), this.#t[1] = t[1] << 1n | 1n, this.#n(), this.#t[0] += t[0], this.#n()) : this.#t = BigUint64Array.from(z);
|
|
735
|
+
}
|
|
736
|
+
/** step inner state */
|
|
737
|
+
#n() {
|
|
738
|
+
this.#t[0] = this.#t[0] * J + this.#t[1];
|
|
739
|
+
}
|
|
740
|
+
/** 32bit 乱数を返す (内部状態は変わらない) */
|
|
741
|
+
get #e() {
|
|
742
|
+
const t = this.#t[0], e = Number(t >> 59n), r = Number(BigInt.asUintN(32, (t ^ t >> 18n) >> 27n));
|
|
743
|
+
return F(r, e);
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
746
|
+
*
|
|
747
|
+
* @returns a random 32-bit unsigned integer
|
|
748
|
+
*/
|
|
749
|
+
getU32Rand() {
|
|
750
|
+
return this.#n(), this.#e;
|
|
751
|
+
}
|
|
752
|
+
/**
|
|
753
|
+
* @returns a random 32-bit unsigned integer less than `bound`
|
|
754
|
+
*/
|
|
755
|
+
getBoundedU32Rand(t) {
|
|
756
|
+
if (t > 4294967296) throw Error("`bound` exceeded limit (2^32)");
|
|
757
|
+
if (t <= 0)
|
|
758
|
+
throw Error("'bound' must be positive");
|
|
759
|
+
const r = 4294967296 % t, s = 1e5;
|
|
760
|
+
for (let o = 0; o < s; ++o) {
|
|
761
|
+
const i = this.getU32Rand();
|
|
762
|
+
if (i >= r)
|
|
763
|
+
return i % t;
|
|
764
|
+
}
|
|
765
|
+
throw Error("exceeded loop limit");
|
|
766
|
+
}
|
|
767
|
+
/**
|
|
768
|
+
*
|
|
769
|
+
* @param step the number of needed random integers
|
|
770
|
+
* @param bound upper limit
|
|
771
|
+
* @returns
|
|
772
|
+
* the iterator that generates random 32-bit unsigned integers `step` times \
|
|
773
|
+
* if `bound` is given, random integers are less than `bound`
|
|
774
|
+
*/
|
|
775
|
+
*genU32Rands(t, e) {
|
|
776
|
+
if (t <= 0)
|
|
777
|
+
throw Error("'step' must be positive");
|
|
778
|
+
for (let r = 0; r < t; r++)
|
|
779
|
+
yield typeof e == "number" ? this.getBoundedU32Rand(e) : this.getU32Rand();
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
const W = [
|
|
783
|
+
0xbe562cb412e2260en,
|
|
784
|
+
0x2e4284137d641affn,
|
|
785
|
+
0x4e19b36ee933e27en,
|
|
786
|
+
0x7581cf8c4f4d4f7dn
|
|
787
|
+
];
|
|
788
|
+
class T {
|
|
789
|
+
bits = 64;
|
|
790
|
+
#t;
|
|
791
|
+
static name = "XoshiroMinimal";
|
|
792
|
+
[Symbol.toStringTag] = T.name;
|
|
793
|
+
/**
|
|
794
|
+
* @param seeds
|
|
795
|
+
* `BigUint64Array` with length 4. \
|
|
796
|
+
* if it is not given, initialized by default value
|
|
797
|
+
* @example
|
|
798
|
+
* // the following example is always initialized by the same seeds.
|
|
799
|
+
* // not recommended
|
|
800
|
+
* const rng = new XoshiroMinimal();
|
|
801
|
+
*
|
|
802
|
+
* // you should construct with random seeds.
|
|
803
|
+
* const seed = crypto.getRandomValues(new BigUint64Array(4));
|
|
804
|
+
* const betterRng = new PCGMinimal(seed);
|
|
805
|
+
*/
|
|
806
|
+
constructor(t) {
|
|
807
|
+
t && t.length >= 4 ? (this.#t = new BigUint64Array(4), this.#t[0] = t[0], this.#t[1] = t[1], this.#n(), this.#t[2] += t[2], this.#t[3] += t[3]) : this.#t = BigUint64Array.from(W);
|
|
808
|
+
}
|
|
809
|
+
#n() {
|
|
810
|
+
const t = this.#t[1] << 17n;
|
|
811
|
+
this.#t[2] ^= this.#t[0], this.#t[3] ^= this.#t[1], this.#t[1] ^= this.#t[2], this.#t[0] ^= this.#t[3], this.#t[2] ^= t, this.#t[3] = I(this.#t[3], 45n);
|
|
812
|
+
}
|
|
813
|
+
get value() {
|
|
814
|
+
return I(this.#t[0] + this.#t[3], 23n) + this.#t[0];
|
|
815
|
+
}
|
|
816
|
+
getU64Rand() {
|
|
817
|
+
const t = this.value;
|
|
818
|
+
return this.#n(), t;
|
|
819
|
+
}
|
|
820
|
+
getU32Rand() {
|
|
821
|
+
const t = BigInt.asUintN(32, this.getU64Rand());
|
|
822
|
+
return Number(t);
|
|
823
|
+
}
|
|
824
|
+
getBoundedU64Rand(t) {
|
|
825
|
+
const e = 1n << 64n;
|
|
826
|
+
if (t > e)
|
|
827
|
+
throw Error("'bound' exceeded limit");
|
|
828
|
+
if (t <= 0n)
|
|
829
|
+
throw Error("'bound' must be positive");
|
|
830
|
+
const r = e % t, s = 1e5;
|
|
831
|
+
for (let o = 0; o < s; ++o) {
|
|
832
|
+
const i = this.getU64Rand();
|
|
833
|
+
if (i >= r)
|
|
834
|
+
return i % t;
|
|
835
|
+
}
|
|
836
|
+
throw Error("exceeded loop limit");
|
|
837
|
+
}
|
|
838
|
+
getBoundedU32Rand(t) {
|
|
839
|
+
if (t > 4294967296)
|
|
840
|
+
throw Error("'bound' exceeded limit");
|
|
841
|
+
if (t <= 0n)
|
|
842
|
+
throw Error("'bound' must be positive");
|
|
843
|
+
const r = this.getBoundedU64Rand(BigInt(t));
|
|
844
|
+
return Number(r);
|
|
845
|
+
}
|
|
846
|
+
*genU64Rands(t, e) {
|
|
847
|
+
if (t <= 0)
|
|
848
|
+
throw Error("'step' must be positive");
|
|
849
|
+
for (let r = 0; r < t; ++r)
|
|
850
|
+
yield e === void 0 ? this.getU64Rand() : this.getBoundedU64Rand(e);
|
|
851
|
+
}
|
|
852
|
+
*genU32Rands(t, e) {
|
|
853
|
+
if (t <= 0)
|
|
854
|
+
throw Error("'step' must be positive");
|
|
855
|
+
for (let r = 0; r < t; ++r)
|
|
856
|
+
yield e === void 0 ? this.getU32Rand() : this.getBoundedU32Rand(e);
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
export {
|
|
860
|
+
A as FloatRng,
|
|
861
|
+
R as NamedError,
|
|
862
|
+
M as PCGMinimal,
|
|
863
|
+
N as Queue,
|
|
864
|
+
u as Rational,
|
|
865
|
+
T as XoshiroMinimal,
|
|
866
|
+
x as bailliePSW,
|
|
867
|
+
rt as compress,
|
|
868
|
+
ht as decodeLEB128,
|
|
869
|
+
et as decodeRFC3986URIComponent,
|
|
870
|
+
st as decompress,
|
|
871
|
+
ft as encodeLEB128,
|
|
872
|
+
nt as encodeRFC3986URIComponent,
|
|
873
|
+
j as exEuclidean,
|
|
874
|
+
it as factorial,
|
|
875
|
+
lt as fromString,
|
|
876
|
+
Q as getHash,
|
|
877
|
+
E as getRandBIByBitLength,
|
|
878
|
+
_ as getRandBIByRange,
|
|
879
|
+
at as getRandPrimeByBitLength,
|
|
880
|
+
ct as getRandPrimeByRange,
|
|
881
|
+
ot as getRndInt,
|
|
882
|
+
g as isDeepStrictEqual,
|
|
883
|
+
tt as isNode,
|
|
884
|
+
P as isSquare,
|
|
885
|
+
U as jacobiSymbol,
|
|
886
|
+
X as lazify,
|
|
887
|
+
p as modPow,
|
|
888
|
+
Z as parseCSV,
|
|
889
|
+
G as promiseWithResolvers,
|
|
890
|
+
m as residue,
|
|
891
|
+
F as rot32,
|
|
892
|
+
I as rot64,
|
|
893
|
+
Y as sameValueZero,
|
|
894
|
+
H as sleep,
|
|
895
|
+
ut as toString
|
|
896
|
+
};
|