@tktb-tess/util-fns 0.8.6 → 0.9.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/dist/bundle.js CHANGED
@@ -1,4 +1,4 @@
1
- const T = new TextEncoder(), K = (n, t) => [n].includes(t), g = (n, t) => {
1
+ const M = new TextEncoder(), z = (n, t) => [n].includes(t), d = (n, t) => {
2
2
  if (typeof n != typeof t) return !1;
3
3
  const e = Object.prototype.toString.call(n), r = Object.prototype.toString.call(t);
4
4
  if (e !== r) return !1;
@@ -11,33 +11,33 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), g = (n, t) => {
11
11
  if (Array.isArray(n) && Array.isArray(t)) {
12
12
  if (n.length !== t.length) return !1;
13
13
  for (let s = 0; s < n.length; s++)
14
- if (!g(n[s], t[s])) return !1;
14
+ if (!d(n[s], t[s])) return !1;
15
15
  return !0;
16
16
  }
17
17
  if (n instanceof Set && t instanceof Set) {
18
18
  const s = [...n.values()], o = [...t.values()];
19
- return !!g(s, o);
19
+ return !!d(s, o);
20
20
  }
21
21
  if (n instanceof Map && t instanceof Map) {
22
22
  const s = [...n.keys()], o = [...t.keys()];
23
- if (!g(s, o)) return !1;
23
+ if (!d(s, o)) return !1;
24
24
  const i = [...n.values()], c = [...t.values()];
25
- return !!g(i, c);
25
+ return !!d(i, c);
26
26
  }
27
27
  if (e === "[object Object]") {
28
28
  const s = n, o = t, i = Object.keys(s), c = Object.keys(o);
29
29
  for (const a of i) {
30
- const f = c.find((d) => d === a);
31
- if (f === void 0) return !1;
32
- const [h, u] = [s[a], o[f]];
33
- if (!g(h, u)) return !1;
30
+ const u = c.find((p) => p === a);
31
+ if (u === void 0) return !1;
32
+ const [f, h] = [s[a], o[u]];
33
+ if (!d(f, h)) return !1;
34
34
  }
35
35
  return !0;
36
36
  }
37
37
  throw Error(`comparing these objects is unavailable: ${n}, ${t}`, {
38
38
  cause: [n, t]
39
39
  });
40
- }, z = () => {
40
+ }, J = () => {
41
41
  let n, t;
42
42
  return {
43
43
  promise: new Promise((r, s) => {
@@ -46,9 +46,9 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), g = (n, t) => {
46
46
  resolve: n,
47
47
  reject: t
48
48
  };
49
- }, J = (n) => new Promise((t) => {
49
+ }, W = (n) => new Promise((t) => {
50
50
  setTimeout(() => t(), n);
51
- }), W = (n) => (...t) => () => n(...t), G = (n) => {
51
+ }), Y = (n) => (...t) => () => n(...t), G = (n) => {
52
52
  const t = [];
53
53
  let e = [], r = "", s = !1;
54
54
  for (let o = 0; o < n.length; o++) {
@@ -58,53 +58,53 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), g = (n, t) => {
58
58
  }
59
59
  return e.push(r.trim()), t.push(e), t;
60
60
  }, H = async (n, t) => {
61
- const e = T.encode(n), r = await crypto.subtle.digest(t, e);
61
+ const e = M.encode(n), r = await crypto.subtle.digest(t, e);
62
62
  return new Uint8Array(r);
63
- }, Z = () => !!globalThis.process && typeof process.version < "u" && typeof process.versions.node < "u", X = (n) => encodeURIComponent(n).replace(
63
+ }, X = () => !!globalThis.process && typeof process.version < "u" && typeof process.versions.node < "u", Z = (n) => encodeURIComponent(n).replace(
64
64
  /[!'()*]/g,
65
65
  (e) => `%${e.charCodeAt(0).toString(16).toUpperCase()}`
66
- ), Y = (n) => {
66
+ ), Q = (n) => {
67
67
  if (n.includes("+"))
68
68
  throw Error("An input string has '+'");
69
69
  return decodeURIComponent(n);
70
- }, Q = async (n, t) => {
70
+ }, tt = async (n, t) => {
71
71
  const r = new Blob([n]).stream().pipeThrough(new CompressionStream(t));
72
72
  return new Response(r).bytes();
73
- }, tt = async (n, t) => {
73
+ }, nt = async (n, t) => {
74
74
  const r = new Blob([n]).stream().pipeThrough(new DecompressionStream(t));
75
75
  return new Response(r).bytes();
76
- }, nt = (n, t) => Math.floor(Math.random() * (t - n) + n), m = (n, t) => {
76
+ }, et = (n, t) => Math.floor(Math.random() * (t - n) + n), g = (n, t) => {
77
77
  t < 0n && (t *= -1n);
78
78
  const e = n % t;
79
79
  return e < 0n ? e + t : e;
80
- }, E = (n, t = !1) => {
80
+ }, B = (n, t = !1) => {
81
81
  if (!Number.isFinite(n)) throw Error("`length` is not a valid number");
82
82
  if (n <= 0) throw Error("`length` must be positive");
83
83
  const e = Math.ceil(n / 8), r = crypto.getRandomValues(new Uint8Array(e));
84
84
  let s = Array.from(r, (o) => o.toString(2).padStart(8, "0")).join("").slice(0, n);
85
85
  return t && (s = s.replace(/^\d/, "1")), BigInt("0b" + s);
86
- }, j = (n, t) => {
86
+ }, L = (n, t) => {
87
87
  if (n >= t) throw Error("rangeError");
88
88
  const e = t - n, r = e.toString(2).length, s = (() => {
89
89
  for (let i = 0; i < 1e5; i++) {
90
- const c = E(r);
91
- if (c >= p(2n, BigInt(r), e))
90
+ const c = B(r);
91
+ if (c >= m(2n, BigInt(r), e))
92
92
  return c % e;
93
93
  }
94
94
  throw Error("Failed to generate a random bigint");
95
95
  })();
96
96
  return n + s;
97
- }, p = (n, t, e) => {
97
+ }, m = (n, t, e) => {
98
98
  if (e < 1n) throw Error("`mod` must be positive");
99
99
  if (t < 0n) throw Error("`power` must not be negative");
100
- if (n = m(n, e), e === 1n) return 0n;
100
+ if (n = g(n, e), e === 1n) return 0n;
101
101
  if (n % e === 1n || n % e === 0n) return n;
102
102
  if (n === e - 1n) return t & 1n ? e - 1n : 1n;
103
103
  let r = 1n;
104
104
  for (; t > 0n; )
105
105
  t & 1n && (r = r * n % e), n = n * n % e, t >>= 1n;
106
106
  return r;
107
- }, _ = (n, t) => {
107
+ }, T = (n, t) => {
108
108
  if (n === 0n && t === 0n) return { x: 0n, y: 0n, gcd: 0n };
109
109
  if (n === 0n)
110
110
  return t > 0n ? { x: 0n, y: -1n, gcd: t } : { x: 0n, y: 1n, gcd: -t };
@@ -112,9 +112,9 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), g = (n, t) => {
112
112
  return n > 0n ? { x: 1n, y: 0n, gcd: n } : { x: -1n, y: 0n, gcd: -n };
113
113
  let [e, r, s] = [1n, 0n, n], [o, i, c] = [0n, -1n, t];
114
114
  for (; ; ) {
115
- const a = s / c, f = s - a * c;
116
- if (f === 0n) break;
117
- [e, o] = [o, e - a * o], [r, i] = [i, r - a * i], [s, c] = [c, f];
115
+ const a = s / c, u = s - a * c;
116
+ if (u === 0n) break;
117
+ [e, o] = [o, e - a * o], [r, i] = [i, r - a * i], [s, c] = [c, u];
118
118
  }
119
119
  return c < 0n && (o *= -1n, i *= -1n, c *= -1n), { x: o, y: i, gcd: c };
120
120
  }, w = (n, t) => {
@@ -136,12 +136,12 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), g = (n, t) => {
136
136
  r *= w(t, i), t = i, e *= r;
137
137
  }
138
138
  return e;
139
- }, et = (n) => {
139
+ }, rt = (n) => {
140
140
  if (n < 0n) throw Error("'n' must be non-negative");
141
141
  if (n === 0n) return 1n;
142
142
  const t = n - BigInt(n.toString(2).match(/1/g)?.length ?? 0);
143
143
  return v(n) << t;
144
- }, rt = (n, t) => BigInt.asUintN(32, n >> (t & 31n) | n << (-t & 31n)), C = (n, t) => (n >>> (t & 31) | n << (-t & 31)) >>> 0, b = (n, t) => {
144
+ }, _ = (n, t) => (n >>> (t & 31) | n << (-t & 31)) >>> 0, b = (n, t) => BigInt.asUintN(64, n >> (t & 63n) | n << (-t & 63n)), I = (n, t) => {
145
145
  if (t < 1n || t % 2n === 0n)
146
146
  throw Error("`n` is invalid");
147
147
  for (; n < 0n; )
@@ -157,7 +157,7 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), g = (n, t) => {
157
157
  [n, t] = [t, n], n % 4n === 3n && t % 4n === 3n && (e *= -1n), n %= t;
158
158
  }
159
159
  return t === 1n ? e : 0n;
160
- }, F = (n) => {
160
+ }, j = (n) => {
161
161
  if (n < 0n) return !1;
162
162
  if (n === 0n) return !0;
163
163
  let t = 1n, e = n;
@@ -166,49 +166,49 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), g = (n, t) => {
166
166
  r ** 2n < n ? t = r : e = r;
167
167
  }
168
168
  return n === t ** 2n || n === (t + 1n) ** 2n;
169
- }, L = (n) => {
169
+ }, C = (n) => {
170
170
  if (n <= 1n) return !1;
171
171
  if (n % 2n === 0n) return n === 2n;
172
172
  let t = n - 1n, e = 0n;
173
173
  for (; t % 2n === 0n; )
174
174
  t >>= 1n, e += 1n;
175
175
  const [r, s] = [t, e];
176
- let i = p(2n, r, n);
176
+ let i = m(2n, r, n);
177
177
  if (i === 1n) return !0;
178
178
  for (let c = 0n; c < s; c++) {
179
179
  if (i === n - 1n) return !0;
180
180
  i = i * i % n;
181
181
  }
182
182
  return !1;
183
- }, P = (n) => {
184
- let t = 5n, e = b(t, n);
183
+ }, F = (n) => {
184
+ let t = 5n, e = I(t, n);
185
185
  for (; e > 0n; ) {
186
- if (t = t > 0n ? t + 2n : t - 2n, t *= -1n, t === -15n && F(n))
186
+ if (t = t > 0n ? t + 2n : t - 2n, t *= -1n, t === -15n && j(n))
187
187
  return [0n, 0n];
188
- e = b(t, n);
188
+ e = I(t, n);
189
189
  }
190
190
  return [t, e];
191
- }, y = (n, t) => (n & 1n) === 1n ? m(n + t >> 1n, t) : m(n >> 1n, t), V = (n, t, e, r) => {
191
+ }, y = (n, t) => (n & 1n) === 1n ? g(n + t >> 1n, t) : g(n >> 1n, t), P = (n, t, e, r) => {
192
192
  let s = 1n, o = e;
193
193
  const i = n.toString(2).slice(1);
194
194
  for (const c of i)
195
- [s, o] = [m(s * o, t), y(o * o + r * s * s, t)], c === "1" && ([s, o] = [y(e * s + o, t), y(r * s + e * o, t)]);
195
+ [s, o] = [g(s * o, t), y(o * o + r * s * s, t)], c === "1" && ([s, o] = [y(e * s + o, t), y(r * s + e * o, t)]);
196
196
  return [s, o];
197
- }, O = (n, t, e, r) => {
197
+ }, V = (n, t, e, r) => {
198
198
  if (n % 2n !== 1n) throw Error("`n` must be odd");
199
199
  let s = n + 1n, o = 0n;
200
200
  for (; s % 2n === 0n; )
201
201
  s >>= 1n, o += 1n;
202
- const [i, c] = V(s, n, e, t);
202
+ const [i, c] = P(s, n, e, t);
203
203
  let a = c;
204
204
  if (i === 0n) return !0;
205
- r = p(r, s, n);
206
- for (let f = 0n; f < o; f++) {
205
+ r = m(r, s, n);
206
+ for (let u = 0n; u < o; u++) {
207
207
  if (a === 0n) return !0;
208
- a = m(a * a - 2n * r, n), r = p(r, 2n, n);
208
+ a = g(a * a - 2n * r, n), r = m(r, 2n, n);
209
209
  }
210
210
  return !1;
211
- }, x = (n) => {
211
+ }, E = (n) => {
212
212
  if (n <= 1n) return !1;
213
213
  if (n % 2n === 0n) return n === 2n;
214
214
  const t = [
@@ -242,26 +242,26 @@ const T = new TextEncoder(), K = (n, t) => [n].includes(t), g = (n, t) => {
242
242
  for (const o of t)
243
243
  if (n % o === 0n)
244
244
  return n === o;
245
- if (!L(n))
245
+ if (!C(n))
246
246
  return !1;
247
- const [e, r] = P(n);
247
+ const [e, r] = F(n);
248
248
  if (r === 0n) return !1;
249
249
  const s = (1n - e) / 4n;
250
- return O(n, e, 1n, s);
250
+ return V(n, e, 1n, s);
251
251
  }, st = (n, t) => {
252
252
  if (t < 2n)
253
253
  throw Error("noPrimesFound");
254
254
  for (let r = 0; r < 1e5; r++) {
255
- const s = j(n, t);
256
- if (x(s)) return s;
255
+ const s = L(n, t);
256
+ if (E(s)) return s;
257
257
  }
258
258
  throw Error("noPrimesFound");
259
259
  }, ot = (n, t = !1) => {
260
260
  if (n < 2)
261
261
  throw Error("noPrimesFound");
262
262
  for (let r = 0; r < 1e5; r++) {
263
- const s = E(n, t);
264
- if (x(s)) return s;
263
+ const s = B(n, t);
264
+ if (E(s)) return s;
265
265
  }
266
266
  throw Error("noPrimesFound");
267
267
  };
@@ -294,12 +294,12 @@ class l {
294
294
  if (o === 0)
295
295
  return new l(r ? -s : s, 1n);
296
296
  t = 1 / o;
297
- let [i, c] = [1n, s], [a, f] = [0n, 1n];
298
- for (; `${f}`.length < e + 1; ) {
299
- const h = BigInt(Math.floor(t)), u = t - Number(h);
300
- if ([i, c] = [c, h * c + i], [a, f] = [f, h * f + a], u === 0)
301
- return new l(r ? -c : c, f);
302
- t = 1 / u;
297
+ let [i, c] = [1n, s], [a, u] = [0n, 1n];
298
+ for (; `${u}`.length < e + 1; ) {
299
+ const f = BigInt(Math.floor(t)), h = t - Number(f);
300
+ if ([i, c] = [c, f * c + i], [a, u] = [u, f * u + a], h === 0)
301
+ return new l(r ? -c : c, u);
302
+ t = 1 / h;
303
303
  }
304
304
  return new l(r ? -i : i, a);
305
305
  }
@@ -307,7 +307,7 @@ class l {
307
307
  * reduction
308
308
  */
309
309
  #e() {
310
- const { gcd: t } = _(this.#t, this.#n);
310
+ const { gcd: t } = T(this.#t, this.#n);
311
311
  t !== 0n && (this.#t /= t, this.#n /= t);
312
312
  }
313
313
  /**
@@ -415,7 +415,7 @@ class l {
415
415
  return new l(BigInt(s), BigInt(o));
416
416
  }
417
417
  }
418
- const $ = new TextEncoder(), k = new TextDecoder(), it = (n, t) => {
418
+ const $ = new TextEncoder(), O = new TextDecoder(), it = (n, t) => {
419
419
  switch (t) {
420
420
  case "utf-8":
421
421
  return $.encode(n);
@@ -454,7 +454,7 @@ const $ = new TextEncoder(), k = new TextDecoder(), it = (n, t) => {
454
454
  }, ct = (n, t) => {
455
455
  switch (t) {
456
456
  case "utf-8":
457
- return k.decode(n);
457
+ return O.decode(n);
458
458
  case "base64": {
459
459
  const e = Array.from(n, (r) => String.fromCharCode(r)).join("");
460
460
  return btoa(e);
@@ -483,12 +483,35 @@ const $ = new TextEncoder(), k = new TextDecoder(), it = (n, t) => {
483
483
  default:
484
484
  throw Error(`Invalid encoding: ${t}`, { cause: t });
485
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
+ }, at = (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
+ }, ut = (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);
486
509
  };
487
- class A {
510
+ class x {
488
511
  #t;
489
512
  #n;
490
513
  static name = "Queue";
491
- [Symbol.toStringTag] = A.name;
514
+ [Symbol.toStringTag] = x.name;
492
515
  /**
493
516
  * Queue
494
517
  * @param data
@@ -526,10 +549,10 @@ class A {
526
549
  return this.toArray().toString();
527
550
  }
528
551
  }
529
- class B extends Error {
552
+ class N extends Error {
530
553
  name;
531
554
  static name = "NamedError";
532
- [Symbol.toStringTag] = B.name;
555
+ [Symbol.toStringTag] = N.name;
533
556
  constructor(t, e, r) {
534
557
  super(e, { cause: r }), this.name = t;
535
558
  }
@@ -546,7 +569,7 @@ class B extends Error {
546
569
  };
547
570
  }
548
571
  }
549
- const I = (n) => {
572
+ const U = (n) => {
550
573
  if (n === 0) return 32;
551
574
  let t = 0;
552
575
  for (; n > 0 && !(n & 1); )
@@ -559,43 +582,59 @@ const I = (n) => {
559
582
  ++t, n >>= 1n;
560
583
  return BigInt.asUintN(64, t);
561
584
  };
562
- class U {
585
+ class R {
563
586
  #t;
564
587
  static name = "FloatRand";
565
- [Symbol.toStringTag] = U.name;
588
+ [Symbol.toStringTag] = R.name;
566
589
  constructor(t) {
567
590
  this.#t = t;
568
591
  }
569
592
  #n() {
570
- const r = this.#t.getU32Rand() >>> 0, s = r & 255;
593
+ const r = (() => {
594
+ switch (this.#t.bits) {
595
+ case 32:
596
+ return this.#t.getU32Rand() >>> 0;
597
+ case 64: {
598
+ const a = this.#t.getU64Rand() & 0xffffffffn;
599
+ return Number(a);
600
+ }
601
+ }
602
+ })(), s = r & 255;
571
603
  let o = 126;
572
604
  if (s === 0) {
573
605
  o -= 8;
574
- const h = 1e5;
606
+ const a = 1e5;
575
607
  let u = 0;
576
608
  t: for (; ; ) {
577
- if (u > h)
609
+ if (u > a)
578
610
  throw Error("loop exceeded limit");
579
- const d = this.#t.getU32Rand() >>> 0;
580
- if (d === 0) {
611
+ const f = (() => {
612
+ switch (this.#t.bits) {
613
+ case 32:
614
+ return this.#t.getU32Rand() >>> 0;
615
+ case 64: {
616
+ const h = BigInt.asUintN(32, this.#t.getU64Rand());
617
+ return Number(h);
618
+ }
619
+ }
620
+ })();
621
+ if (f === 0) {
581
622
  if (o -= 32, o < 0) {
582
623
  o = 0;
583
624
  break t;
584
625
  }
585
626
  } else {
586
- o -= I(d);
627
+ o -= U(f);
587
628
  break t;
588
629
  }
589
630
  ++u;
590
631
  }
591
632
  } else
592
- o -= I(s);
633
+ o -= U(s);
593
634
  const i = r >>> 8 & 8388607;
594
635
  i === 0 && r >>> 31 && ++o;
595
- const { buffer: c, byteOffset: a, length: f } = Uint32Array.from([
596
- o << 23 | i
597
- ]);
598
- return new Float32Array(c, a, f)[0];
636
+ const { buffer: c } = Uint32Array.from([o << 23 | i]);
637
+ return new Float32Array(c)[0];
599
638
  }
600
639
  /**
601
640
  * returns a random single-precision floating-point number (float32) in the range of [0.0, 1.0)
@@ -611,40 +650,50 @@ class U {
611
650
  }
612
651
  #e() {
613
652
  const t = 0n, e = 1023n, r = (() => {
614
- const h = BigInt(this.#t.getU32Rand() >>> 0), u = BigInt(this.#t.getU32Rand() >>> 0);
615
- return h << 32n | u;
653
+ switch (this.#t.bits) {
654
+ case 32: {
655
+ const a = BigInt(this.#t.getU32Rand() >>> 0), u = BigInt(this.#t.getU32Rand() >>> 0);
656
+ return a << 32n | u;
657
+ }
658
+ case 64:
659
+ return BigInt.asUintN(64, this.#t.getU64Rand());
660
+ }
616
661
  })(), s = r & 0x7ffn;
617
662
  let o = e - 1n;
618
663
  if (s === 0n) {
619
664
  o -= 11n;
620
- const h = 1e5;
665
+ const a = 1e5;
621
666
  let u = 0;
622
667
  t: for (; ; ) {
623
- if (u > h)
668
+ if (u > a)
624
669
  throw Error("loop exceeded limit");
625
- const d = (() => {
626
- const R = BigInt(this.#t.getU32Rand() >>> 0), M = BigInt(this.#t.getU32Rand() >>> 0);
627
- return R << 32n | M;
670
+ const f = (() => {
671
+ switch (this.#t.bits) {
672
+ case 32: {
673
+ const h = BigInt(this.#t.getU32Rand() >>> 0), p = BigInt(this.#t.getU32Rand() >>> 0);
674
+ return h << 32n | p;
675
+ }
676
+ case 64:
677
+ return BigInt.asUintN(64, this.#t.getU64Rand());
678
+ }
628
679
  })();
629
- if (d === 0n) {
680
+ if (f === 0n) {
630
681
  if (o -= 64n, o < t) {
631
682
  o = t;
632
683
  break t;
633
684
  }
634
685
  } else {
635
- o -= S(d);
686
+ o -= S(f);
636
687
  break t;
637
688
  }
638
689
  ++u;
639
690
  }
640
691
  } else
641
692
  o -= S(s);
642
- const i = r >> 11n & 0xfffffffffffffn;
693
+ const i = BigInt.asUintN(52, r >> 11n);
643
694
  i === 0n && r >> 63n && ++o;
644
- const { buffer: c, byteOffset: a, length: f } = BigUint64Array.from([
645
- o << 52n | i
646
- ]);
647
- return new Float64Array(c, a, f)[0];
695
+ const { buffer: c } = BigUint64Array.from([o << 52n | i]);
696
+ return new Float64Array(c)[0];
648
697
  }
649
698
  /**
650
699
  * returns a random double-precision floating-point number (float64) in the range of [0.0, 1.0)
@@ -660,36 +709,29 @@ class U {
660
709
  }
661
710
  }
662
711
  const D = [0x853c49e6748fea9bn, 0xda3e39cb94b95bdbn], q = 0x5851f42d4c957f2dn;
663
- class N {
712
+ class A {
664
713
  /**
665
714
  * length = 2, `[state, increment]`
666
715
  */
667
716
  #t;
668
717
  static name = "PCGMinimal";
669
- [Symbol.toStringTag] = N.name;
670
- /**
671
- * returns seed for constructing `PCGMinimal` instance
672
- * @returns random seed `BigUint64Array` with length 2
673
- * @example
674
- * const rng = new PCGMinimal(PCGMinimal.getSeed())
675
- */
676
- static getSeed() {
677
- return crypto.getRandomValues(new BigUint64Array(2));
678
- }
718
+ [Symbol.toStringTag] = A.name;
719
+ bits = 32;
679
720
  /**
680
721
  * @param seeds
681
- * `BigUintArray` with length 2. \
722
+ * `BigUint64Array` with length 2. \
682
723
  * if it is not given, initialized by default value
683
724
  * @example
684
725
  * // the following example is always initialized by the same seeds.
685
- * // it's not recommended
686
- * const weakRng = new PCGMinimal();
726
+ * // not recommended
727
+ * const rng = new PCGMinimal();
687
728
  *
688
- * // you should construct with random seeds generated by `PCGMinimal.getSeed()`.
689
- * const betterRng = new PCGMinimal(PCGMinimal.getSeed());
729
+ * // you should construct with random seeds.
730
+ * const seed = crypto.getRandomValues(new BigUint64Array(2));
731
+ * const betterRng = new PCGMinimal(seed);
690
732
  */
691
733
  constructor(t) {
692
- 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(D);
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(D);
693
735
  }
694
736
  /** step inner state */
695
737
  #n() {
@@ -698,7 +740,7 @@ class N {
698
740
  /** 32bit 乱数を返す (内部状態は変わらない) */
699
741
  get #e() {
700
742
  const t = this.#t[0], e = Number(t >> 59n), r = Number(BigInt.asUintN(32, (t ^ t >> 18n) >> 27n));
701
- return C(r, e);
743
+ return _(r, e);
702
744
  }
703
745
  /**
704
746
  *
@@ -712,11 +754,15 @@ class N {
712
754
  */
713
755
  getBoundedU32Rand(t) {
714
756
  if (t > 4294967296) throw Error("`bound` exceeded limit (2^32)");
715
- const r = 4294967296 % t;
716
- for (; ; ) {
717
- const s = this.getU32Rand();
718
- if (s >= r) return s % t;
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;
719
764
  }
765
+ throw Error("exceeded loop limit");
720
766
  }
721
767
  /**
722
768
  *
@@ -727,42 +773,122 @@ class N {
727
773
  * if `bound` is given, random integers are less than `bound`
728
774
  */
729
775
  *genU32Rands(t, e) {
776
+ if (t <= 0)
777
+ throw Error("'step' must be positive");
730
778
  for (let r = 0; r < t; r++)
731
779
  yield typeof e == "number" ? this.getBoundedU32Rand(e) : this.getU32Rand();
732
780
  }
733
781
  }
782
+ const K = [
783
+ 0xbe562cb412e2260en,
784
+ 0x2e4284137d641affn,
785
+ 0x4e19b36ee933e27en,
786
+ 0x7581cf8c4f4d4f7dn
787
+ ];
788
+ class lt {
789
+ bits = 64;
790
+ #t;
791
+ /**
792
+ * @param seeds
793
+ * `BigUint64Array` with length 4. \
794
+ * if it is not given, initialized by default value
795
+ * @example
796
+ * // the following example is always initialized by the same seeds.
797
+ * // not recommended
798
+ * const rng = new XoshiroMinimal();
799
+ *
800
+ * // you should construct with random seeds.
801
+ * const seed = crypto.getRandomValues(new BigUint64Array(4));
802
+ * const betterRng = new PCGMinimal(seed);
803
+ */
804
+ constructor(t) {
805
+ 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(K);
806
+ }
807
+ #n() {
808
+ const t = this.#t[1] << 17n;
809
+ 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] = b(this.#t[3], 45n);
810
+ }
811
+ get value() {
812
+ return b(this.#t[0] + this.#t[3], 23n) + this.#t[0];
813
+ }
814
+ getU64Rand() {
815
+ const t = this.value;
816
+ return this.#n(), t;
817
+ }
818
+ getU32Rand() {
819
+ const t = BigInt.asUintN(32, this.getU64Rand());
820
+ return Number(t);
821
+ }
822
+ getBoundedU64Rand(t) {
823
+ const e = 1n << 64n;
824
+ if (t > e)
825
+ throw Error("'bound' exceeded limit");
826
+ if (t <= 0n)
827
+ throw Error("'bound' must be positive");
828
+ const r = e % t, s = 1e5;
829
+ for (let o = 0; o < s; ++o) {
830
+ const i = this.getU64Rand();
831
+ if (i >= r)
832
+ return i % t;
833
+ }
834
+ throw Error("exceeded loop limit");
835
+ }
836
+ getBoundedU32Rand(t) {
837
+ if (t > 4294967296)
838
+ throw Error("'bound' exceeded limit");
839
+ if (t <= 0n)
840
+ throw Error("'bound' must be positive");
841
+ const r = this.getBoundedU64Rand(BigInt(t));
842
+ return Number(r);
843
+ }
844
+ *genU64Rands(t, e) {
845
+ if (t <= 0)
846
+ throw Error("'step' must be positive");
847
+ for (let r = 0; r < t; ++r)
848
+ yield e === void 0 ? this.getU64Rand() : this.getBoundedU64Rand(e);
849
+ }
850
+ *genU32Rands(t, e) {
851
+ if (t <= 0)
852
+ throw Error("'step' must be positive");
853
+ for (let r = 0; r < t; ++r)
854
+ yield e === void 0 ? this.getU32Rand() : this.getBoundedU32Rand(e);
855
+ }
856
+ }
734
857
  export {
735
- U as FloatRand,
736
- B as NamedError,
737
- N as PCGMinimal,
738
- A as Queue,
858
+ R as FloatRand,
859
+ N as NamedError,
860
+ A as PCGMinimal,
861
+ x as Queue,
739
862
  l as Rational,
740
- x as bailliePSW,
741
- Q as compress,
742
- Y as decodeRFC3986URIComponent,
743
- tt as decompress,
744
- X as encodeRFC3986URIComponent,
745
- _ as exEuclidean,
746
- et as factorial,
863
+ lt as XoshiroMinimal,
864
+ E as bailliePSW,
865
+ tt as compress,
866
+ ut as decodeLEB128,
867
+ Q as decodeRFC3986URIComponent,
868
+ nt as decompress,
869
+ at as encodeLEB128,
870
+ Z as encodeRFC3986URIComponent,
871
+ T as exEuclidean,
872
+ rt as factorial,
747
873
  it as fromString,
748
874
  H as getHash,
749
- E as getRandBIByBitLength,
750
- j as getRandBIByRange,
875
+ B as getRandBIByBitLength,
876
+ L as getRandBIByRange,
751
877
  ot as getRandPrimeByBitLength,
752
878
  st as getRandPrimeByRange,
753
- nt as getRndInt,
754
- g as isDeepStrictEqual,
755
- Z as isNode,
756
- F as isSquare,
757
- b as jacobiSymbol,
758
- W as lazify,
759
- p as modPow,
879
+ et as getRndInt,
880
+ d as isDeepStrictEqual,
881
+ X as isNode,
882
+ j as isSquare,
883
+ I as jacobiSymbol,
884
+ Y as lazify,
885
+ m as modPow,
760
886
  G as parseCSV,
761
- z as promiseWithResolvers,
762
- m as residue,
763
- C as rot32,
764
- rt as rot32BI,
765
- K as sameValueZero,
766
- J as sleep,
887
+ J as promiseWithResolvers,
888
+ g as residue,
889
+ _ as rot32,
890
+ b as rot64,
891
+ z as sameValueZero,
892
+ W as sleep,
767
893
  ct as toString
768
894
  };
package/dist/main.d.ts CHANGED
@@ -7,3 +7,4 @@ export * from './queue';
7
7
  export * from './named-error';
8
8
  export * from './random';
9
9
  export * from './pcg-minimal';
10
+ export * from './xoshiro-minimal';
package/dist/math.d.ts CHANGED
@@ -56,23 +56,23 @@ export declare const exEuclidean: (a: bigint, b: bigint) => {
56
56
  */
57
57
  export declare const factorial: (n: bigint) => bigint;
58
58
  /**
59
- * 32-bit rotate (bigint)
60
- * @param value
61
- * @param rot 回転数
59
+ * 32-bit rorate (number)
60
+ * @param value
61
+ * @param rot
62
62
  * @returns
63
63
  */
64
- export declare const rot32BI: (value: bigint, rot: bigint) => bigint;
64
+ export declare const rot32: (value: number, rot: number) => number;
65
65
  /**
66
- * 32-bit rorate (number)
67
- * @param value
68
- * @param rot 回転数
66
+ * 64-bit rorate (bigint)
67
+ * @param value
68
+ * @param rot
69
69
  * @returns
70
70
  */
71
- export declare const rot32: (value: number, rot: number) => number;
71
+ export declare const rot64: (value: bigint, rot: bigint) => bigint;
72
72
  /**
73
73
  * Jacobi symbol `(a/n)`
74
- * @param a 正の整数
75
- * @param n 正の奇数
74
+ * @param a positive integer
75
+ * @param n positive odd integer
76
76
  */
77
77
  export declare const jacobiSymbol: (a: bigint, n: bigint) => bigint;
78
78
  /**
@@ -1,29 +1,26 @@
1
- import { RandomGenerator } from './random';
1
+ import { RandomGenerator32 } from './random';
2
2
  /**
3
- * PCG-XSH-RR (Permuted congruential generator)
3
+ * PCG-XSH-RR (Permuted congruential generator) \
4
+ * reference: https://github.com/imneme/pcg-c-basic/blob/bc39cd76ac3d541e618606bcc6e1e5ba5e5e6aa3/pcg_basic.c \
5
+ * by Melissa O'Neill
4
6
  */
5
- export declare class PCGMinimal implements RandomGenerator {
7
+ export declare class PCGMinimal implements RandomGenerator32 {
6
8
  #private;
7
9
  static readonly name = "PCGMinimal";
8
10
  readonly [Symbol.toStringTag] = "PCGMinimal";
9
- /**
10
- * returns seed for constructing `PCGMinimal` instance
11
- * @returns random seed `BigUint64Array` with length 2
12
- * @example
13
- * const rng = new PCGMinimal(PCGMinimal.getSeed())
14
- */
15
- static getSeed(): BigUint64Array<ArrayBuffer>;
11
+ readonly bits = 32;
16
12
  /**
17
13
  * @param seeds
18
- * `BigUintArray` with length 2. \
14
+ * `BigUint64Array` with length 2. \
19
15
  * if it is not given, initialized by default value
20
16
  * @example
21
17
  * // the following example is always initialized by the same seeds.
22
- * // it's not recommended
23
- * const weakRng = new PCGMinimal();
18
+ * // not recommended
19
+ * const rng = new PCGMinimal();
24
20
  *
25
- * // you should construct with random seeds generated by `PCGMinimal.getSeed()`.
26
- * const betterRng = new PCGMinimal(PCGMinimal.getSeed());
21
+ * // you should construct with random seeds.
22
+ * const seed = crypto.getRandomValues(new BigUint64Array(2));
23
+ * const betterRng = new PCGMinimal(seed);
27
24
  */
28
25
  constructor(seeds?: BigUint64Array<ArrayBuffer>);
29
26
  /**
package/dist/random.d.ts CHANGED
@@ -1,13 +1,16 @@
1
- export interface RandomGenerator {
1
+ export interface RandomGenerator32 {
2
+ readonly bits: 32;
2
3
  getU32Rand: () => number;
3
- getBoundedU32Rand: (bound: number) => number;
4
- genU32Rands: (step: number, bound?: number) => Generator<number, void, unknown>;
5
4
  }
6
- export declare class FloatRand {
5
+ export interface RandomGenerator64 {
6
+ readonly bits: 64;
7
+ getU64Rand: () => bigint;
8
+ }
9
+ export declare class FloatRand<TRng extends RandomGenerator32 | RandomGenerator64> {
7
10
  #private;
8
11
  static readonly name = "FloatRand";
9
12
  readonly [Symbol.toStringTag] = "FloatRand";
10
- constructor(rng: RandomGenerator);
13
+ constructor(rng: TRng);
11
14
  /**
12
15
  * returns a random single-precision floating-point number (float32) in the range of [0.0, 1.0)
13
16
  * @returns
@@ -1,3 +1,5 @@
1
1
  export type Encoding = 'utf-8' | 'base64' | 'base64url' | 'hex' | 'oct' | 'bin';
2
2
  export declare const fromString: (str: string, encoding: Encoding) => Uint8Array<ArrayBuffer>;
3
3
  export declare const toString: (u8Arr: Uint8Array, encoding: Encoding) => string;
4
+ export declare const encodeLEB128: (bigint: bigint) => Uint8Array<ArrayBuffer>;
5
+ export declare const decodeLEB128: (leb128: Uint8Array<ArrayBuffer>) => bigint;
package/dist/util.d.ts CHANGED
@@ -63,5 +63,5 @@ export declare const encodeRFC3986URIComponent: (URIComponent: string | number |
63
63
  * @returns
64
64
  */
65
65
  export declare const decodeRFC3986URIComponent: (encodedURIComponent: string) => string;
66
- export declare const compress: (bin: Uint8Array<ArrayBuffer>, format: CompressionFormat) => Promise<Uint8Array<ArrayBuffer>>;
67
- export declare const decompress: (comp: Uint8Array<ArrayBuffer>, format: CompressionFormat) => Promise<Uint8Array<ArrayBuffer>>;
66
+ export declare const compress: (raw: Uint8Array<ArrayBuffer>, format: CompressionFormat) => Promise<Uint8Array<ArrayBuffer>>;
67
+ export declare const decompress: (compressed: Uint8Array<ArrayBuffer>, format: CompressionFormat) => Promise<Uint8Array<ArrayBuffer>>;
@@ -0,0 +1,31 @@
1
+ import { RandomGenerator64 } from './random';
2
+ /**
3
+ * Xoshiro256++ \
4
+ * reference: https://prng.di.unimi.it/xoshiro256plusplus.c \
5
+ * by David Blackman and Sebastiano Vigna
6
+ */
7
+ export declare class XoshiroMinimal implements RandomGenerator64 {
8
+ #private;
9
+ readonly bits = 64;
10
+ /**
11
+ * @param seeds
12
+ * `BigUint64Array` with length 4. \
13
+ * if it is not given, initialized by default value
14
+ * @example
15
+ * // the following example is always initialized by the same seeds.
16
+ * // not recommended
17
+ * const rng = new XoshiroMinimal();
18
+ *
19
+ * // you should construct with random seeds.
20
+ * const seed = crypto.getRandomValues(new BigUint64Array(4));
21
+ * const betterRng = new PCGMinimal(seed);
22
+ */
23
+ constructor(seed?: BigUint64Array<ArrayBuffer>);
24
+ get value(): bigint;
25
+ getU64Rand(): bigint;
26
+ getU32Rand(): number;
27
+ getBoundedU64Rand(bound: bigint): bigint;
28
+ getBoundedU32Rand(bound: number): number;
29
+ genU64Rands(step: number, bound?: bigint): Generator<bigint, void, unknown>;
30
+ genU32Rands(step: number, bound?: number): Generator<number, void, unknown>;
31
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tktb-tess/util-fns",
3
- "version": "0.8.6",
3
+ "version": "0.9.0",
4
4
  "description": "Utility functions for personal use",
5
5
  "files": [
6
6
  "dist"