@tktb-tess/util-fns 0.3.0 → 0.3.2

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.
@@ -0,0 +1,7 @@
1
+ declare const __brand: unique symbol;
2
+ export type Brand<in out K extends string | symbol> = {
3
+ readonly [__brand]: {
4
+ readonly [key in K]: unknown;
5
+ };
6
+ };
7
+ export {};
package/dist/bundle.d.ts CHANGED
@@ -1,20 +1,22 @@
1
- import { isEqArray, sleep, lazify, parseCSV, getHash } from './util';
2
- import { getRndInt, residue, toBigInt, getRandBIByBitLength, getRandBIByRange, modPow, exEuclidean, factorial, rot32, rot32BI, jacobiSymbol, isSquare } from './math';
1
+ import { isEqual, sleep, lazify, parseCSV, getHash, toBigInt, isNode } from './util';
2
+ import { getRndInt, residue, getRandBIByBitLength, getRandBIByRange, modPow, exEuclidean, factorial, rot32, rot32BI, jacobiSymbol, isSquare } from './math';
3
3
  import { default as PCGMinimal } from './pcg-minimal';
4
4
  import { bailliePSW, getRandPrimeByBitLength, getRandPrimeByRange } from './baillie-psw';
5
5
  import { default as Fraction } from './fraction';
6
6
  import { base64ToU8Arr, base64UrlToU8Arr, u8ArrToBase64, u8ArrToBase64Url, BItoU8Arr, u8ArrToBI } from './u8arr-ext';
7
+ import { Brand } from './brand';
7
8
  declare const Util: {
8
- isEqArray: <T>(arr1: T[], arr2: T[]) => boolean;
9
+ isEqual: (a: unknown, b: unknown) => boolean;
9
10
  sleep: (delay: number) => Promise<void>;
10
11
  lazify: <ArgT extends unknown[], RetT>(func: (...args: ArgT) => RetT) => (...args: ArgT) => () => RetT;
11
12
  parseCSV: (csv: string) => string[][];
12
13
  getHash: (str: string, algorithm: AlgorithmIdentifier) => Promise<Uint8Array<ArrayBuffer>>;
14
+ isNode: () => boolean;
15
+ toBigInt: (...nums: number[]) => bigint[];
13
16
  };
14
- declare const AdMath: {
17
+ declare const Calc: {
15
18
  getRndInt: (min: number, max: number) => number;
16
19
  residue: (n: bigint, mod: bigint) => bigint;
17
- toBigInt: (...nums: number[]) => bigint[];
18
20
  getRandBIByBitLength: (length: number, fixed?: boolean) => bigint;
19
21
  getRandBIByRange: (min: bigint, max: bigint) => bigint;
20
22
  modPow: (base: bigint, power: bigint, mod: bigint) => bigint;
@@ -28,8 +30,6 @@ declare const AdMath: {
28
30
  rot32BI: (value: bigint, rot: bigint) => bigint;
29
31
  jacobiSymbol: (a: bigint, n: bigint) => bigint;
30
32
  isSquare: (n: bigint) => boolean;
31
- };
32
- declare const Prime: {
33
33
  bailliePSW: (n: bigint) => boolean;
34
34
  getRandPrimeByBitLength: (bitLength: number, fixed?: boolean) => bigint;
35
35
  getRandPrimeByRange: (min: bigint, max: bigint) => bigint;
@@ -42,4 +42,4 @@ declare const U8Ext: {
42
42
  BItoU8Arr: (n: bigint) => Uint8Array<ArrayBuffer>;
43
43
  u8ArrToBI: (buf: Uint8Array) => bigint;
44
44
  };
45
- export { isEqArray, sleep, lazify, parseCSV, getHash, getRndInt, residue, toBigInt, getRandBIByBitLength, getRandBIByRange, modPow, exEuclidean, factorial, rot32, rot32BI, jacobiSymbol, isSquare, bailliePSW, getRandPrimeByBitLength, getRandPrimeByRange, PCGMinimal, Fraction, Util, AdMath, Prime, base64ToU8Arr, base64UrlToU8Arr, u8ArrToBase64, u8ArrToBase64Url, BItoU8Arr, u8ArrToBI, U8Ext, };
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, type Brand, };
package/dist/bundle.js CHANGED
@@ -1,45 +1,69 @@
1
- const R = new TextEncoder(), x = (n, t) => {
2
- if (n.length !== t.length) return !1;
3
- for (let r = 0; r < n.length; r++)
4
- if (n[r] !== t[r]) return !1;
5
- return !0;
6
- }, _ = (n) => new Promise((t) => {
1
+ const R = new TextEncoder(), p = (n, t) => {
2
+ if (typeof n != typeof t) return !1;
3
+ const r = Object.prototype.toString.call(n), e = Object.prototype.toString.call(t);
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;
10
+ if (typeof n == "function")
11
+ throw Error("comparing these objects is still unavailable");
12
+ if (Array.isArray(n) && Array.isArray(t)) {
13
+ if (n.length !== t.length) return !1;
14
+ for (let i = 0; i < n.length; i++)
15
+ if (!p(n[i], t[i])) return !1;
16
+ return !0;
17
+ }
18
+ if (r === "[object Object]") {
19
+ const i = n, s = t, o = Object.getOwnPropertyNames(n), c = Object.getOwnPropertyNames(t);
20
+ o.push(...Object.getOwnPropertySymbols(n)), c.push(...Object.getOwnPropertySymbols(t));
21
+ for (const l of o) {
22
+ const u = c.find((O) => O === l);
23
+ if (u === void 0) return !1;
24
+ const [h, d] = [i[l], s[u]];
25
+ if (!p(h, d)) return !1;
26
+ }
27
+ return !0;
28
+ }
29
+ throw Error("comparing these objects is still unavailable");
30
+ }, x = (n) => new Promise((t) => {
7
31
  setTimeout(() => {
8
32
  t();
9
33
  }, n);
10
34
  }), M = (n) => (...t) => () => n(...t), T = (n) => {
11
35
  const t = [];
12
- let r = [], e = "", s = !1;
13
- for (let i = 0; i < n.length; i++) {
14
- const o = n[i];
15
- o === '"' && (i === 0 || n[i - 1] !== "\\") ? s = !s : o === "," && !s ? (r.push(e.trim()), e = "") : o === `
16
- ` && !s ? (r.push(e.trim()), t.push(r), r = [], e = "") : e += o;
36
+ let r = [], e = "", i = !1;
37
+ for (let s = 0; s < n.length; s++) {
38
+ const o = n[s];
39
+ o === '"' && (s === 0 || n[s - 1] !== "\\") ? i = !i : o === "," && !i ? (r.push(e.trim()), e = "") : o === `
40
+ ` && !i ? (r.push(e.trim()), t.push(r), r = [], e = "") : e += o;
17
41
  }
18
42
  return r.push(e.trim()), t.push(r), t;
19
- }, L = async (n, t) => {
43
+ }, _ = async (n, t) => {
20
44
  const r = R.encode(n), e = await crypto.subtle.digest(t, r);
21
45
  return new Uint8Array(e);
22
- }, I = typeof process < "u" && typeof process.versions.node < "u", V = (n, t) => Math.floor(Math.random() * (t - n) + n), h = (n, t) => {
46
+ }, L = (...n) => n.map((t) => BigInt(t)), b = () => typeof globalThis.process < "u" && typeof globalThis.require < "u", V = (n, t) => Math.floor(Math.random() * (t - n) + n), a = (n, t) => {
23
47
  const r = n % t;
24
48
  return r < 0n ? r + t : r;
25
- }, j = (...n) => n.map((t) => BigInt(t)), w = (n, t = !1) => {
49
+ }, B = (n, t = !1) => {
26
50
  if (!Number.isFinite(n)) throw Error("`length` is not a valid number");
27
51
  if (n <= 0) throw Error("`length` must be positive");
28
52
  const r = Math.ceil(n / 8), e = crypto.getRandomValues(new Uint8Array(r));
29
- let s = Array.from(e, (i) => i.toString(2).padStart(8, "0")).join("").slice(0, n);
30
- return t && (s = s.replace(/^./, "1")), BigInt("0b" + s);
53
+ let i = Array.from(e, (s) => s.toString(2).padStart(8, "0")).join("").slice(0, n);
54
+ return t && (i = i.replace(/^./, "1")), BigInt("0b" + i);
31
55
  }, S = (n, t) => {
32
56
  if (n >= t) throw Error("rangeError");
33
- const r = t - n, e = r.toString(2).length, s = (() => {
57
+ const r = t - n, e = r.toString(2).length, i = (() => {
34
58
  for (let o = 0; o < 1e5; o++) {
35
- const c = w(e);
36
- if (c >= a(2n, BigInt(e), r))
59
+ const c = B(e);
60
+ if (c >= g(2n, BigInt(e), r))
37
61
  return c % r;
38
62
  }
39
63
  throw Error("Failed to generate a random bigint");
40
64
  })();
41
- return n + s;
42
- }, a = (n, t, r) => {
65
+ return n + i;
66
+ }, g = (n, t, r) => {
43
67
  if (r < 1n) throw Error("`mod` must be positive");
44
68
  if (t < 0n) throw Error("`power` must not be negative");
45
69
  for (; n < 0n; ) n += r;
@@ -56,29 +80,29 @@ const R = new TextEncoder(), x = (n, t) => {
56
80
  return t > 0n ? { x: 0n, y: -1n, gcd: t } : { x: 0n, y: 1n, gcd: -t };
57
81
  if (t === 0n)
58
82
  return n > 0n ? { x: 1n, y: 0n, gcd: n } : { x: -1n, y: 0n, gcd: -n };
59
- let [r, e, s] = [1n, 0n, n], [i, o, c] = [0n, -1n, t];
83
+ let [r, e, i] = [1n, 0n, n], [s, o, c] = [0n, -1n, t];
60
84
  for (; ; ) {
61
- const u = s / c, f = s - u * c;
62
- if (f === 0n) break;
63
- [r, i] = [i, r - u * i], [e, o] = [o, e - u * o], [s, c] = [c, f];
85
+ const l = i / c, u = i - l * c;
86
+ if (u === 0n) break;
87
+ [r, s] = [s, r - l * s], [e, o] = [o, e - l * o], [i, c] = [c, u];
64
88
  }
65
- return c < 0n && (i *= -1n, o *= -1n, c *= -1n), { x: i, y: o, gcd: c };
89
+ return c < 0n && (s *= -1n, o *= -1n, c *= -1n), { x: s, y: o, gcd: c };
66
90
  }, m = (n, t) => {
67
91
  if (n >= t) return 1n;
68
92
  const r = BigInt((t - 2n).toString(2).length), e = (t - n) / 2n;
69
93
  if (r * e < 63n) {
70
94
  let c = n;
71
- for (let u = n + 2n; u < t; u += 2n)
72
- c *= u;
95
+ for (let l = n + 2n; l < t; l += 2n)
96
+ c *= l;
73
97
  return c;
74
98
  }
75
- const s = n + e | 1n, i = m(n, s), o = m(s, t);
76
- return i * o;
99
+ const i = n + e | 1n, s = m(n, i), o = m(i, t);
100
+ return s * o;
77
101
  }, C = (n) => {
78
102
  let t = 3n, r = 1n, e = 1n;
79
- const s = BigInt(n.toString(2).length) - 1n;
80
- for (let i = s - 1n; i > -1n; --i) {
81
- const o = (n >> i) + 1n | 1n;
103
+ const i = BigInt(n.toString(2).length) - 1n;
104
+ for (let s = i - 1n; s > -1n; --s) {
105
+ const o = (n >> s) + 1n | 1n;
82
106
  e *= m(t, o), t = o, r *= e;
83
107
  }
84
108
  return r;
@@ -87,7 +111,7 @@ const R = new TextEncoder(), x = (n, t) => {
87
111
  if (n === 0n) return 1n;
88
112
  const t = n - BigInt(n.toString(2).match(/1/g)?.length ?? 0);
89
113
  return C(n) << t;
90
- }, D = (n, t) => BigInt.asUintN(32, n >> (t & 31n) | n << (-t & 31n)), E = (n, t) => (n >>> (t & 31) | n << (-t & 31)) >>> 0, p = (n, t) => {
114
+ }, D = (n, t) => BigInt.asUintN(32, n >> (t & 31n) | n << (-t & 31n)), E = (n, t) => (n >>> (t & 31) | n << (-t & 31)) >>> 0, w = (n, t) => {
91
115
  if (t < 1n || t % 2n === 0n)
92
116
  throw Error("`n` is invalid");
93
117
  for (; n < 0n; )
@@ -103,7 +127,7 @@ const R = new TextEncoder(), x = (n, t) => {
103
127
  [n, t] = [t, n], n % 4n === 3n && t % 4n === 3n && (r *= -1n), n %= t;
104
128
  }
105
129
  return t === 1n ? r : 0n;
106
- }, N = (n) => {
130
+ }, P = (n) => {
107
131
  if (n < 0n) return !1;
108
132
  if (n === 0n) return !0;
109
133
  let t = 1n, r = n;
@@ -112,13 +136,12 @@ const R = new TextEncoder(), x = (n, t) => {
112
136
  e ** 2n < n ? t = e : r = e;
113
137
  }
114
138
  return n === t ** 2n || n === (t + 1n) ** 2n;
115
- }, O = Symbol(), b = [0x853c49e6748fea9bn, 0xda3e39cb94b95bdbn], $ = 0x5851f42d4c957f2dn;
116
- class F {
139
+ }, N = [0x853c49e6748fea9bn, 0xda3e39cb94b95bdbn], $ = 0x5851f42d4c957f2dn;
140
+ class Z {
117
141
  /**
118
142
  * length = 2, `[state, increment]`
119
143
  */
120
144
  #t = new BigUint64Array(2);
121
- [O];
122
145
  /** シード値の配列を返す */
123
146
  static getSeed() {
124
147
  return crypto.getRandomValues(new BigUint64Array(2));
@@ -127,14 +150,14 @@ class F {
127
150
  * @param seeds 64bit整数の配列 (長さ2以上), 省略した場合, 常に同じ値によって初期化される
128
151
  */
129
152
  constructor(t) {
130
- t ? (this.#t[1] = t[1] << 1n | 1n, this.step(), this.#t[0] = t[0], this.step()) : (this.#t[0] = b[0], this.#t[1] = b[1]);
153
+ t ? (this.#t[1] = t[1] << 1n | 1n, this.#n(), this.#t[0] = t[0], this.#n()) : (this.#t[0] = N[0], this.#t[1] = N[1]);
131
154
  }
132
155
  /** 内部状態を1サイクル進める */
133
- step() {
156
+ #n() {
134
157
  this.#t[0] = this.#t[0] * $ + this.#t[1];
135
158
  }
136
159
  /** 32bit 乱数を返す (内部状態は変わらない) */
137
- get value() {
160
+ get #r() {
138
161
  const t = this.#t[0];
139
162
  if (!t) throw Error("empty state");
140
163
  const r = t >> 59n, e = BigInt.asUintN(32, (t ^ t >> 18n) >> 27n);
@@ -144,15 +167,15 @@ class F {
144
167
  * 普通はこれを使う
145
168
  */
146
169
  getRand() {
147
- return this.step(), this.value;
170
+ return this.#n(), this.#r;
148
171
  }
149
172
  /** `bound` 未満の乱数を返す */
150
173
  getBoundedRand(t) {
151
174
  if (t > 4294967296) throw Error("`bound` exceeded limit (2^32)");
152
175
  const e = 4294967296 % t;
153
176
  for (; ; ) {
154
- const s = this.getRand();
155
- if (s >= e) return s % t;
177
+ const i = this.getRand();
178
+ if (i >= e) return i % t;
156
179
  }
157
180
  }
158
181
  /**
@@ -165,49 +188,49 @@ class F {
165
188
  yield typeof r == "number" ? this.getBoundedRand(r) : this.getRand();
166
189
  }
167
190
  }
168
- const v = (n) => {
191
+ const K = (n) => {
169
192
  if (n <= 1n) return !1;
170
193
  if (n % 2n === 0n) return n === 2n;
171
194
  let t = n - 1n, r = 0n;
172
195
  for (; t % 2n === 0n; )
173
196
  t >>= 1n, r += 1n;
174
- const [e, s] = [t, r];
175
- let o = a(2n, e, n);
197
+ const [e, i] = [t, r];
198
+ let o = g(2n, e, n);
176
199
  if (o === 1n) return !0;
177
- for (let c = 0n; c < s; c++) {
200
+ for (let c = 0n; c < i; c++) {
178
201
  if (o === n - 1n) return !0;
179
202
  o = o * o % n;
180
203
  }
181
204
  return !1;
182
- }, J = (n) => {
183
- let t = 5n, r = p(t, n);
205
+ }, v = (n) => {
206
+ let t = 5n, r = w(t, n);
184
207
  for (; r > 0n; ) {
185
- if (t = t > 0n ? t + 2n : t - 2n, t *= -1n, t === -15n && N(n))
208
+ if (t = t > 0n ? t + 2n : t - 2n, t *= -1n, t === -15n && P(n))
186
209
  return [0n, 0n];
187
- r = p(t, n);
210
+ r = w(t, n);
188
211
  }
189
212
  return [t, r];
190
- }, g = (n, t) => (n & 1n) === 1n ? h(n + t >> 1n, t) : h(n >> 1n, t), k = (n, t, r, e) => {
191
- let s = 1n, i = r;
213
+ }, y = (n, t) => (n & 1n) === 1n ? a(n + t >> 1n, t) : a(n >> 1n, t), J = (n, t, r, e) => {
214
+ let i = 1n, s = r;
192
215
  const o = n.toString(2).slice(1);
193
216
  for (const c of o)
194
- [s, i] = [h(s * i, t), g(i * i + e * s * s, t)], c === "1" && ([s, i] = [g(r * s + i, t), g(e * s + r * i, t)]);
195
- return [s, i];
196
- }, z = (n, t, r, e) => {
217
+ [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
+ return [i, s];
219
+ }, k = (n, t, r, e) => {
197
220
  if (n % 2n !== 1n) throw Error("`n` must be odd");
198
- let s = n + 1n, i = 0n;
199
- for (; s % 2n === 0n; )
200
- s >>= 1n, i += 1n;
201
- const [o, c] = k(s, n, r, t);
202
- let u = c;
221
+ let i = n + 1n, s = 0n;
222
+ for (; i % 2n === 0n; )
223
+ i >>= 1n, s += 1n;
224
+ const [o, c] = J(i, n, r, t);
225
+ let l = c;
203
226
  if (o === 0n) return !0;
204
- e = a(e, s, n);
205
- for (let f = 0n; f < i; f++) {
206
- if (u === 0n) return !0;
207
- u = h(u * u - 2n * e, n), e = a(e, 2n, n);
227
+ e = g(e, i, n);
228
+ for (let u = 0n; u < s; u++) {
229
+ if (l === 0n) return !0;
230
+ l = a(l * l - 2n * e, n), e = g(e, 2n, n);
208
231
  }
209
232
  return !1;
210
- }, y = (n) => {
233
+ }, I = (n) => {
211
234
  if (n <= 1n) return !1;
212
235
  if (n % 2n === 0n) return n === 2n;
213
236
  const t = [
@@ -238,36 +261,35 @@ const v = (n) => {
238
261
  97n,
239
262
  101n
240
263
  ];
241
- for (const i of t)
242
- if (n % i === 0n)
243
- return n === i;
244
- if (!v(n))
264
+ for (const s of t)
265
+ if (n % s === 0n)
266
+ return n === s;
267
+ if (!K(n))
245
268
  return !1;
246
- const [r, e] = J(n);
269
+ const [r, e] = v(n);
247
270
  if (e === 0n) return !1;
248
- const s = (1n - r) / 4n;
249
- return z(n, r, 1n, s);
250
- }, G = (n, t) => {
271
+ const i = (1n - r) / 4n;
272
+ return k(n, r, 1n, i);
273
+ }, z = (n, t) => {
251
274
  if (t < 2n)
252
275
  throw Error("noPrimesFound");
253
276
  for (let e = 0; e < 1e5; e++) {
254
- const s = S(n, t);
255
- if (y(s)) return s;
277
+ const i = S(n, t);
278
+ if (I(i)) return i;
256
279
  }
257
280
  throw Error("noPrimesFound");
258
- }, H = (n, t = !1) => {
281
+ }, G = (n, t = !1) => {
259
282
  if (n < 2)
260
283
  throw Error("noPrimesFound");
261
284
  for (let e = 0; e < 1e5; e++) {
262
- const s = w(n, t);
263
- if (y(s)) return s;
285
+ const i = B(n, t);
286
+ if (I(i)) return i;
264
287
  }
265
288
  throw Error("noPrimesFound");
266
- }, W = Symbol();
267
- class l {
289
+ };
290
+ class f {
268
291
  #t;
269
292
  #n;
270
- [W];
271
293
  /**
272
294
  *
273
295
  * @param numerator
@@ -283,23 +305,23 @@ class l {
283
305
  */
284
306
  static fromDecimal(t, r = 20) {
285
307
  if (Number.isNaN(t))
286
- return new l(0n, 0n);
308
+ return new f(0n, 0n);
287
309
  if (Math.abs(t) === 1 / 0)
288
- return new l(t > 0 ? 1n : -1n, 0n);
310
+ return new f(t > 0 ? 1n : -1n, 0n);
289
311
  const e = t < 0;
290
312
  e && (t *= -1);
291
- const s = BigInt(Math.floor(t)), i = t - Number(s);
292
- if (i === 0)
293
- return new l(e ? -s : s, 1n);
294
- t = 1 / i;
295
- let [o, c] = [1n, s], [u, f] = [0n, 1n];
296
- for (; `${f}`.length < r + 1; ) {
297
- const d = BigInt(Math.floor(t)), B = t - Number(d);
298
- if ([o, c] = [c, d * c + o], [u, f] = [f, d * f + u], B === 0)
299
- return new l(e ? -c : c, f);
300
- t = 1 / B;
313
+ const i = BigInt(Math.floor(t)), s = t - Number(i);
314
+ if (s === 0)
315
+ return new f(e ? -i : i, 1n);
316
+ t = 1 / s;
317
+ let [o, c] = [1n, i], [l, u] = [0n, 1n];
318
+ for (; `${u}`.length < r + 1; ) {
319
+ const h = BigInt(Math.floor(t)), d = t - Number(h);
320
+ if ([o, c] = [c, h * c + o], [l, u] = [u, h * u + l], d === 0)
321
+ return new f(e ? -c : c, u);
322
+ t = 1 / d;
301
323
  }
302
- return new l(e ? -o : o, u);
324
+ return new f(e ? -o : o, l);
303
325
  }
304
326
  /**
305
327
  * reduction
@@ -313,14 +335,14 @@ class l {
313
335
  * @returns
314
336
  */
315
337
  minus() {
316
- return new l(-this.#t, this.#n);
338
+ return new f(-this.#t, this.#n);
317
339
  }
318
340
  /**
319
341
  * returns inverse of this fraction
320
342
  * @returns
321
343
  */
322
344
  inverse() {
323
- return new l(this.#n, this.#t);
345
+ return new f(this.#n, this.#t);
324
346
  }
325
347
  /**
326
348
  * returns `this + right`
@@ -329,7 +351,7 @@ class l {
329
351
  */
330
352
  add(t) {
331
353
  const r = this.#n * t.#n, e = this.#t * t.#n + t.#t * this.#n;
332
- return new l(e, r);
354
+ return new f(e, r);
333
355
  }
334
356
  /**
335
357
  * returns `this - right`
@@ -337,7 +359,7 @@ class l {
337
359
  * @returns
338
360
  */
339
361
  substr(t) {
340
- return new l(this.#t, this.#n).add(t.minus());
362
+ return new f(this.#t, this.#n).add(t.minus());
341
363
  }
342
364
  /**
343
365
  * returns `this * right`
@@ -346,7 +368,7 @@ class l {
346
368
  */
347
369
  multiply(t) {
348
370
  const r = this.#n * t.#n, e = this.#t * t.#t;
349
- return new l(e, r);
371
+ return new f(e, r);
350
372
  }
351
373
  /**
352
374
  * returns `this / right`
@@ -354,7 +376,7 @@ class l {
354
376
  * @returns
355
377
  */
356
378
  divide(t) {
357
- return new l(this.#t, this.#n).multiply(
379
+ return new f(this.#t, this.#n).multiply(
358
380
  t.inverse()
359
381
  );
360
382
  }
@@ -365,7 +387,7 @@ class l {
365
387
  */
366
388
  mediant(t) {
367
389
  const r = this.#n + t.#n, e = this.#t + t.#t;
368
- return new l(e, r);
390
+ return new f(e, r);
369
391
  }
370
392
  /**
371
393
  * `number` 型の小数に変換
@@ -392,103 +414,103 @@ class l {
392
414
  };
393
415
  }
394
416
  static parse(t) {
395
- const r = JSON.parse(t), { type: e, numerator: s, denominator: i } = r;
417
+ const r = JSON.parse(t), { type: e, numerator: i, denominator: s } = r;
396
418
  if (e !== "Fraction") throw Error("cannot parse");
397
- if (typeof s == "string" && typeof i == "string") {
398
- const o = BigInt(s), c = BigInt(i);
399
- return new l(o, c);
419
+ if (typeof i == "string" && typeof s == "string") {
420
+ const o = BigInt(i), c = BigInt(s);
421
+ return new f(o, c);
400
422
  } else
401
423
  throw Error("cannot parse");
402
424
  }
403
425
  }
404
- const K = (n) => {
426
+ const H = (n) => {
405
427
  let t = n.toString(16);
406
428
  t.length & 1 && (t = "0" + t);
407
429
  const r = [...t.match(/.{2}/g) ?? []];
408
430
  return Uint8Array.from(r, (e) => Number.parseInt(e, 16));
409
- }, X = (n) => {
431
+ }, W = (n) => {
410
432
  const t = Array.from(n, (r) => r.toString(16).padStart(2, "0")).join("") || "00";
411
433
  return BigInt("0x" + t);
412
434
  }, U = (n) => {
413
- if (I)
435
+ if (b())
414
436
  return Buffer.copyBytesFrom(n).toString("base64");
415
437
  {
416
438
  const t = Array.from(n, (r) => String.fromCharCode(r)).join("");
417
439
  return btoa(t);
418
440
  }
419
- }, P = (n) => {
420
- if (I) {
441
+ }, j = (n) => {
442
+ if (b()) {
421
443
  const t = Buffer.from(n, "base64");
422
444
  return new Uint8Array(t.buffer, t.byteOffset, t.byteLength);
423
445
  } else
424
446
  return Uint8Array.from(atob(n), (t) => t.charCodeAt(0));
425
- }, Y = (n) => U(n).replaceAll(/\+/g, "-").replaceAll(/\//g, "_").replaceAll(/=*$/g, ""), Z = (n) => {
447
+ }, X = (n) => U(n).replaceAll(/\+/g, "-").replaceAll(/\//g, "_").replaceAll(/=*$/g, ""), Y = (n) => {
426
448
  const t = n.length & 3;
427
449
  t > 0 && (n = n + "=".repeat(4 - t));
428
450
  const r = n.replaceAll(/-/g, "+").replaceAll(/_/g, "/");
429
- return P(r);
430
- }, Q = {
431
- isEqArray: x,
432
- sleep: _,
451
+ return j(r);
452
+ }, F = {
453
+ isEqual: p,
454
+ sleep: x,
433
455
  lazify: M,
434
456
  parseCSV: T,
435
- getHash: L
436
- }, tt = {
457
+ getHash: _,
458
+ isNode: b,
459
+ toBigInt: L
460
+ }, Q = {
437
461
  getRndInt: V,
438
- residue: h,
439
- toBigInt: j,
440
- getRandBIByBitLength: w,
462
+ residue: a,
463
+ getRandBIByBitLength: B,
441
464
  getRandBIByRange: S,
442
- modPow: a,
465
+ modPow: g,
443
466
  exEuclidean: A,
444
467
  factorial: q,
445
468
  rot32: E,
446
469
  rot32BI: D,
447
- jacobiSymbol: p,
448
- isSquare: N
449
- }, nt = {
450
- bailliePSW: y,
451
- getRandPrimeByBitLength: H,
452
- getRandPrimeByRange: G
453
- }, rt = {
454
- base64ToU8Arr: P,
455
- base64UrlToU8Arr: Z,
470
+ jacobiSymbol: w,
471
+ isSquare: P,
472
+ bailliePSW: I,
473
+ getRandPrimeByBitLength: G,
474
+ getRandPrimeByRange: z
475
+ }, tt = {
476
+ base64ToU8Arr: j,
477
+ base64UrlToU8Arr: Y,
456
478
  u8ArrToBase64: U,
457
- u8ArrToBase64Url: Y,
458
- BItoU8Arr: K,
459
- u8ArrToBI: X
479
+ u8ArrToBase64Url: X,
480
+ BItoU8Arr: H,
481
+ u8ArrToBI: W
460
482
  };
461
483
  export {
462
- tt as AdMath,
463
- K as BItoU8Arr,
464
- l as Fraction,
465
- F as PCGMinimal,
466
- nt as Prime,
467
- rt as U8Ext,
468
- Q as Util,
469
- y as bailliePSW,
470
- P as base64ToU8Arr,
471
- Z as base64UrlToU8Arr,
484
+ H as BItoU8Arr,
485
+ Q as Calc,
486
+ f as Fraction,
487
+ Z as PCGMinimal,
488
+ tt as U8Ext,
489
+ F as Util,
490
+ I as bailliePSW,
491
+ j as base64ToU8Arr,
492
+ Y as base64UrlToU8Arr,
472
493
  A as exEuclidean,
473
494
  q as factorial,
474
- L as getHash,
475
- w as getRandBIByBitLength,
495
+ _ as getHash,
496
+ B as getRandBIByBitLength,
476
497
  S as getRandBIByRange,
477
- H as getRandPrimeByBitLength,
478
- G as getRandPrimeByRange,
498
+ G as getRandPrimeByBitLength,
499
+ z as getRandPrimeByRange,
479
500
  V as getRndInt,
480
- x as isEqArray,
481
- N as isSquare,
482
- p as jacobiSymbol,
501
+ p as isEqual,
502
+ b as isNode,
503
+ P as isSquare,
504
+ w as jacobiSymbol,
483
505
  M as lazify,
484
- a as modPow,
506
+ g as modPow,
485
507
  T as parseCSV,
486
- h as residue,
508
+ a as residue,
487
509
  E as rot32,
488
510
  D as rot32BI,
489
- _ as sleep,
490
- j as toBigInt,
491
- X as u8ArrToBI,
511
+ x as sleep,
512
+ L as toBigInt,
513
+ W as u8ArrToBI,
492
514
  U as u8ArrToBase64,
493
- Y as u8ArrToBase64Url
515
+ X as u8ArrToBase64Url
494
516
  };
@@ -1,2 +1,2 @@
1
- (function(o,g){typeof exports=="object"&&typeof module<"u"?g(exports):typeof define=="function"&&define.amd?define(["exports"],g):(o=typeof globalThis<"u"?globalThis:o||self,g(o["util-fns"]={}))})(this,(function(o){"use strict";const g=new TextEncoder,R=(t,n)=>{if(t.length!==n.length)return!1;for(let e=0;e<t.length;e++)if(t[e]!==n[e])return!1;return!0},N=t=>new Promise(n=>{setTimeout(()=>{n()},t)}),T=t=>(...n)=>()=>t(...n),M=t=>{const n=[];let e=[],r="",i=!1;for(let s=0;s<t.length;s++){const c=t[s];c==='"'&&(s===0||t[s-1]!=="\\")?i=!i:c===","&&!i?(e.push(r.trim()),r=""):c===`
2
- `&&!i?(e.push(r.trim()),n.push(e),e=[],r=""):r+=c}return e.push(r.trim()),n.push(e),n},_=async(t,n)=>{const e=g.encode(t),r=await crypto.subtle.digest(n,e);return new Uint8Array(r)},L=typeof process<"u"&&typeof process.versions.node<"u",j=(t,n)=>Math.floor(Math.random()*(n-t)+t),a=(t,n)=>{const e=t%n;return e<0n?e+n:e},C=(...t)=>t.map(n=>BigInt(n)),m=(t,n=!1)=>{if(!Number.isFinite(t))throw Error("`length` is not a valid number");if(t<=0)throw Error("`length` must be positive");const e=Math.ceil(t/8),r=crypto.getRandomValues(new Uint8Array(e));let i=Array.from(r,s=>s.toString(2).padStart(8,"0")).join("").slice(0,t);return n&&(i=i.replace(/^./,"1")),BigInt("0b"+i)},B=(t,n)=>{if(t>=n)throw Error("rangeError");const e=n-t,r=e.toString(2).length,i=(()=>{for(let c=0;c<1e5;c++){const u=m(r);if(u>=d(2n,BigInt(r),e))return u%e}throw Error("Failed to generate a random bigint")})();return t+i},d=(t,n,e)=>{if(e<1n)throw Error("`mod` must be positive");if(n<0n)throw Error("`power` must not be negative");for(;t<0n;)t+=e;if(e===1n)return 0n;if(t%e===1n||t%e===0n)return t;if(t===e-1n)return n&1n?e-1n:1n;let r=1n;for(;n>0n;)n&1n&&(r=r*t%e),t=t*t%e,n>>=1n;return r},b=(t,n)=>{if(t===0n&&n===0n)return{x:0n,y:0n,gcd:0n};if(t===0n)return n>0n?{x:0n,y:-1n,gcd:n}:{x:0n,y:1n,gcd:-n};if(n===0n)return t>0n?{x:1n,y:0n,gcd:t}:{x:-1n,y:0n,gcd:-t};let[e,r,i]=[1n,0n,t],[s,c,u]=[0n,-1n,n];for(;;){const l=i/u,h=i-l*u;if(h===0n)break;[e,s]=[s,e-l*s],[r,c]=[c,r-l*c],[i,u]=[u,h]}return u<0n&&(s*=-1n,c*=-1n,u*=-1n),{x:s,y:c,gcd:u}},I=(t,n)=>{if(t>=n)return 1n;const e=BigInt((n-2n).toString(2).length),r=(n-t)/2n;if(e*r<63n){let u=t;for(let l=t+2n;l<n;l+=2n)u*=l;return u}const i=t+r|1n,s=I(t,i),c=I(i,n);return s*c},W=t=>{let n=3n,e=1n,r=1n;const i=BigInt(t.toString(2).length)-1n;for(let s=i-1n;s>-1n;--s){const c=(t>>s)+1n|1n;r*=I(n,c),n=c,e*=r}return e},V=t=>{if(t<0n)throw Error("'n' must be non-negative");if(t===0n)return 1n;const n=t-BigInt(t.toString(2).match(/1/g)?.length??0);return W(t)<<n},q=(t,n)=>BigInt.asUintN(32,t>>(n&31n)|t<<(-n&31n)),S=(t,n)=>(t>>>(n&31)|t<<(-n&31))>>>0,y=(t,n)=>{if(n<1n||n%2n===0n)throw Error("`n` is invalid");for(;t<0n;)t+=n;t%=n;let e=1n;for(;t!==0n;){for(;t%2n===0n;){t/=2n;const r=n%8n;(r===3n||r===5n)&&(e*=-1n)}[t,n]=[n,t],t%4n===3n&&n%4n===3n&&(e*=-1n),t%=n}return n===1n?e:0n},p=t=>{if(t<0n)return!1;if(t===0n)return!0;let n=1n,e=t;for(;n+1n<e;){const r=(n+e)/2n;r**2n<t?n=r:e=r}return t===n**2n||t===(n+1n)**2n},k=Symbol(),O=[0x853c49e6748fea9bn,0xda3e39cb94b95bdbn],K=0x5851f42d4c957f2dn;class X{#n=new BigUint64Array(2);[k];static getSeed(){return crypto.getRandomValues(new BigUint64Array(2))}constructor(n){n?(this.#n[1]=n[1]<<1n|1n,this.step(),this.#n[0]=n[0],this.step()):(this.#n[0]=O[0],this.#n[1]=O[1])}step(){this.#n[0]=this.#n[0]*K+this.#n[1]}get value(){const n=this.#n[0];if(!n)throw Error("empty state");const e=n>>59n,r=BigInt.asUintN(32,(n^n>>18n)>>27n);return S(Number(r),Number(e))}getRand(){return this.step(),this.value}getBoundedRand(n){if(n>4294967296)throw Error("`bound` exceeded limit (2^32)");const r=4294967296%n;for(;;){const i=this.getRand();if(i>=r)return i%n}}*genRands(n,e){for(let r=0;r<n;r++)yield typeof e=="number"?this.getBoundedRand(e):this.getRand()}}const Y=t=>{if(t<=1n)return!1;if(t%2n===0n)return t===2n;let n=t-1n,e=0n;for(;n%2n===0n;)n>>=1n,e+=1n;const[r,i]=[n,e];let c=d(2n,r,t);if(c===1n)return!0;for(let u=0n;u<i;u++){if(c===t-1n)return!0;c=c*c%t}return!1},Z=t=>{let n=5n,e=y(n,t);for(;e>0n;){if(n=n>0n?n+2n:n-2n,n*=-1n,n===-15n&&p(t))return[0n,0n];e=y(n,t)}return[n,e]},A=(t,n)=>(t&1n)===1n?a(t+n>>1n,n):a(t>>1n,n),F=(t,n,e,r)=>{let i=1n,s=e;const c=t.toString(2).slice(1);for(const u of c)[i,s]=[a(i*s,n),A(s*s+r*i*i,n)],u==="1"&&([i,s]=[A(e*i+s,n),A(r*i+e*s,n)]);return[i,s]},Q=(t,n,e,r)=>{if(t%2n!==1n)throw Error("`n` must be odd");let i=t+1n,s=0n;for(;i%2n===0n;)i>>=1n,s+=1n;const[c,u]=F(i,t,e,n);let l=u;if(c===0n)return!0;r=d(r,i,t);for(let h=0n;h<s;h++){if(l===0n)return!0;l=a(l*l-2n*r,t),r=d(r,2n,t)}return!1},w=t=>{if(t<=1n)return!1;if(t%2n===0n)return t===2n;const n=[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 n)if(t%s===0n)return t===s;if(!Y(t))return!1;const[e,r]=Z(t);if(r===0n)return!1;const i=(1n-e)/4n;return Q(t,e,1n,i)},D=(t,n)=>{if(n<2n)throw Error("noPrimesFound");for(let r=0;r<1e5;r++){const i=B(t,n);if(w(i))return i}throw Error("noPrimesFound")},$=(t,n=!1)=>{if(t<2)throw Error("noPrimesFound");for(let r=0;r<1e5;r++){const i=m(t,n);if(w(i))return i}throw Error("noPrimesFound")},x=Symbol();class f{#n;#t;[x];constructor(n,e){e===0n?(this.#t=0n,this.#n=n===0n?0n:n>0n?1n:-1n):e>0n?(this.#n=n,this.#t=e):(this.#n=-n,this.#t=-e),this.#e()}static fromDecimal(n,e=20){if(Number.isNaN(n))return new f(0n,0n);if(Math.abs(n)===1/0)return new f(n>0?1n:-1n,0n);const r=n<0;r&&(n*=-1);const i=BigInt(Math.floor(n)),s=n-Number(i);if(s===0)return new f(r?-i:i,1n);n=1/s;let[c,u]=[1n,i],[l,h]=[0n,1n];for(;`${h}`.length<e+1;){const P=BigInt(Math.floor(n)),J=n-Number(P);if([c,u]=[u,P*u+c],[l,h]=[h,P*h+l],J===0)return new f(r?-u:u,h);n=1/J}return new f(r?-c:c,l)}#e(){const{gcd:n}=b(this.#n,this.#t);n!==0n&&(this.#n/=n,this.#t/=n)}minus(){return new f(-this.#n,this.#t)}inverse(){return new f(this.#t,this.#n)}add(n){const e=this.#t*n.#t,r=this.#n*n.#t+n.#n*this.#t;return new f(r,e)}substr(n){return new f(this.#n,this.#t).add(n.minus())}multiply(n){const e=this.#t*n.#t,r=this.#n*n.#n;return new f(r,e)}divide(n){return new f(this.#n,this.#t).multiply(n.inverse())}mediant(n){const e=this.#t+n.#t,r=this.#n+n.#n;return new f(r,e)}toDecimal(){return Number(this.#n)/Number(this.#t)}toString(){return this.#n===0n&&this.#t===0n?"NaN":this.#n===0n?"0":this.#t===0n?this.#n<0n?"-Infinity":"Infinity":this.#t===1n?`${this.#n}`:`${this.#n}/${this.#t}`}valueOf(){return this.toDecimal()}toJSON(){return{type:"Fraction",numerator:"0x"+this.#n.toString(16),denominator:"0x"+this.#t.toString(16)}}static parse(n){const e=JSON.parse(n),{type:r,numerator:i,denominator:s}=e;if(r!=="Fraction")throw Error("cannot parse");if(typeof i=="string"&&typeof s=="string"){const c=BigInt(i),u=BigInt(s);return new f(c,u)}else throw Error("cannot parse")}}const v=t=>{let n=t.toString(16);n.length&1&&(n="0"+n);const e=[...n.match(/.{2}/g)??[]];return Uint8Array.from(e,r=>Number.parseInt(r,16))},z=t=>{const n=Array.from(t,e=>e.toString(16).padStart(2,"0")).join("")||"00";return BigInt("0x"+n)},E=t=>{if(L)return Buffer.copyBytesFrom(t).toString("base64");{const n=Array.from(t,e=>String.fromCharCode(e)).join("");return btoa(n)}},U=t=>{if(L){const n=Buffer.from(t,"base64");return new Uint8Array(n.buffer,n.byteOffset,n.byteLength)}else return Uint8Array.from(atob(t),n=>n.charCodeAt(0))},G=t=>E(t).replaceAll(/\+/g,"-").replaceAll(/\//g,"_").replaceAll(/=*$/g,""),H=t=>{const n=t.length&3;n>0&&(t=t+"=".repeat(4-n));const e=t.replaceAll(/-/g,"+").replaceAll(/_/g,"/");return U(e)},nn={isEqArray:R,sleep:N,lazify:T,parseCSV:M,getHash:_},tn={getRndInt:j,residue:a,toBigInt:C,getRandBIByBitLength:m,getRandBIByRange:B,modPow:d,exEuclidean:b,factorial:V,rot32:S,rot32BI:q,jacobiSymbol:y,isSquare:p},en={bailliePSW:w,getRandPrimeByBitLength:$,getRandPrimeByRange:D},rn={base64ToU8Arr:U,base64UrlToU8Arr:H,u8ArrToBase64:E,u8ArrToBase64Url:G,BItoU8Arr:v,u8ArrToBI:z};o.AdMath=tn,o.BItoU8Arr=v,o.Fraction=f,o.PCGMinimal=X,o.Prime=en,o.U8Ext=rn,o.Util=nn,o.bailliePSW=w,o.base64ToU8Arr=U,o.base64UrlToU8Arr=H,o.exEuclidean=b,o.factorial=V,o.getHash=_,o.getRandBIByBitLength=m,o.getRandBIByRange=B,o.getRandPrimeByBitLength=$,o.getRandPrimeByRange=D,o.getRndInt=j,o.isEqArray=R,o.isSquare=p,o.jacobiSymbol=y,o.lazify=T,o.modPow=d,o.parseCSV=M,o.residue=a,o.rot32=S,o.rot32BI=q,o.sleep=N,o.toBigInt=C,o.u8ArrToBI=z,o.u8ArrToBase64=E,o.u8ArrToBase64Url=G,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(l,g){typeof exports=="object"&&typeof module<"u"?g(exports):typeof define=="function"&&define.amd?define(["exports"],g):(l=typeof globalThis<"u"?globalThis:l||self,g(l["util-fns"]={}))})(this,(function(l){"use strict";const g=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=="bigint"||typeof n=="boolean"||typeof n=="symbol"||typeof n>"u")return n===t;if(typeof n=="number")return Number.isNaN(n)&&Number.isNaN(t)||n===t;if(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,o=Object.getOwnPropertyNames(n),c=Object.getOwnPropertyNames(t);o.push(...Object.getOwnPropertySymbols(n)),c.push(...Object.getOwnPropertySymbols(t));for(const u of o){const a=c.find(nt=>nt===u);if(a===void 0)return!1;const[y,I]=[i[u],s[a]];if(!m(y,I))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 o=n[s];o==='"'&&(s===0||n[s-1]!=="\\")?i=!i:o===","&&!i?(e.push(r.trim()),r=""):o===`
2
+ `&&!i?(e.push(r.trim()),t.push(e),e=[],r=""):r+=o}return e.push(r.trim()),t.push(e),t},L=async(n,t)=>{const e=g.encode(n),r=await crypto.subtle.digest(t,e);return new Uint8Array(r)},_=(...n)=>n.map(t=>BigInt(t)),w=()=>typeof globalThis.process<"u"&&typeof globalThis.require<"u",C=(n,t)=>Math.floor(Math.random()*(t-n)+n),h=(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)},S=(n,t)=>{if(n>=t)throw Error("rangeError");const e=t-n,r=e.toString(2).length,i=(()=>{for(let o=0;o<1e5;o++){const c=b(r);if(c>=d(2n,BigInt(r),e))return c%e}throw Error("Failed to generate a random bigint")})();return n+i},d=(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},N=(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,o,c]=[0n,-1n,t];for(;;){const u=i/c,a=i-u*c;if(a===0n)break;[e,s]=[s,e-u*s],[r,o]=[o,r-u*o],[i,c]=[c,a]}return c<0n&&(s*=-1n,o*=-1n,c*=-1n),{x:s,y:o,gcd:c}},A=(n,t)=>{if(n>=t)return 1n;const e=BigInt((t-2n).toString(2).length),r=(t-n)/2n;if(e*r<63n){let c=n;for(let u=n+2n;u<t;u+=2n)c*=u;return c}const i=n+r|1n,s=A(n,i),o=A(i,t);return s*o},J=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 o=(n>>s)+1n|1n;r*=A(t,o),t=o,e*=r}return e},V=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 J(n)<<t},q=(n,t)=>BigInt.asUintN(32,n>>(t&31n)|n<<(-t&31n)),E=(n,t)=>(n>>>(t&31)|n<<(-t&31))>>>0,B=(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},P=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},D=[0x853c49e6748fea9bn,0xda3e39cb94b95bdbn],W=0x5851f42d4c957f2dn;class k{#t=new BigUint64Array(2);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]=D[0],this.#t[1]=D[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 E(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 X=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 o=d(2n,r,n);if(o===1n)return!0;for(let c=0n;c<i;c++){if(o===n-1n)return!0;o=o*o%n}return!1},Y=n=>{let t=5n,e=B(t,n);for(;e>0n;){if(t=t>0n?t+2n:t-2n,t*=-1n,t===-15n&&P(n))return[0n,0n];e=B(t,n)}return[t,e]},U=(n,t)=>(n&1n)===1n?h(n+t>>1n,t):h(n>>1n,t),Z=(n,t,e,r)=>{let i=1n,s=e;const o=n.toString(2).slice(1);for(const c of o)[i,s]=[h(i*s,t),U(s*s+r*i*i,t)],c==="1"&&([i,s]=[U(e*i+s,t),U(r*i+e*s,t)]);return[i,s]},F=(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[o,c]=Z(i,n,e,t);let u=c;if(o===0n)return!0;r=d(r,i,n);for(let a=0n;a<s;a++){if(u===0n)return!0;u=h(u*u-2n*r,n),r=d(r,2n,n)}return!1},p=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(!X(n))return!1;const[e,r]=Y(n);if(r===0n)return!1;const i=(1n-e)/4n;return F(n,e,1n,i)},$=(n,t)=>{if(t<2n)throw Error("noPrimesFound");for(let r=0;r<1e5;r++){const i=S(n,t);if(p(i))return i}throw Error("noPrimesFound")},v=(n,t=!1)=>{if(n<2)throw Error("noPrimesFound");for(let r=0;r<1e5;r++){const i=b(n,t);if(p(i))return i}throw Error("noPrimesFound")};class f{#t;#n;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[o,c]=[1n,i],[u,a]=[0n,1n];for(;`${a}`.length<e+1;){const y=BigInt(Math.floor(t)),I=t-Number(y);if([o,c]=[c,y*c+o],[u,a]=[a,y*a+u],I===0)return new f(r?-c:c,a);t=1/I}return new f(r?-o:o,u)}#e(){const{gcd:t}=N(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 new f(this.#t,this.#n).add(t.minus())}multiply(t){const e=this.#n*t.#n,r=this.#t*t.#t;return new f(r,e)}divide(t){return new f(this.#t,this.#n).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",numerator:"0x"+this.#t.toString(16),denominator:"0x"+this.#n.toString(16)}}static parse(t){const e=JSON.parse(t),{type:r,numerator:i,denominator:s}=e;if(r!=="Fraction")throw Error("cannot parse");if(typeof i=="string"&&typeof s=="string"){const o=BigInt(i),c=BigInt(s);return new f(o,c)}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))},z=n=>{const t=Array.from(n,e=>e.toString(16).padStart(2,"0")).join("")||"00";return BigInt("0x"+t)},R=n=>{if(w())return Buffer.copyBytesFrom(n).toString("base64");{const t=Array.from(n,e=>String.fromCharCode(e)).join("");return btoa(t)}},T=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))},G=n=>R(n).replaceAll(/\+/g,"-").replaceAll(/\//g,"_").replaceAll(/=*$/g,""),H=n=>{const t=n.length&3;t>0&&(n=n+"=".repeat(4-t));const e=n.replaceAll(/-/g,"+").replaceAll(/_/g,"/");return T(e)},Q={isEqual:m,sleep:j,lazify:O,parseCSV:M,getHash:L,isNode:w,toBigInt:_},x={getRndInt:C,residue:h,getRandBIByBitLength:b,getRandBIByRange:S,modPow:d,exEuclidean:N,factorial:V,rot32:E,rot32BI:q,jacobiSymbol:B,isSquare:P,bailliePSW:p,getRandPrimeByBitLength:v,getRandPrimeByRange:$},tt={base64ToU8Arr:T,base64UrlToU8Arr:H,u8ArrToBase64:R,u8ArrToBase64Url:G,BItoU8Arr:K,u8ArrToBI:z};l.BItoU8Arr=K,l.Calc=x,l.Fraction=f,l.PCGMinimal=k,l.U8Ext=tt,l.Util=Q,l.bailliePSW=p,l.base64ToU8Arr=T,l.base64UrlToU8Arr=H,l.exEuclidean=N,l.factorial=V,l.getHash=L,l.getRandBIByBitLength=b,l.getRandBIByRange=S,l.getRandPrimeByBitLength=v,l.getRandPrimeByRange=$,l.getRndInt=C,l.isEqual=m,l.isNode=w,l.isSquare=P,l.jacobiSymbol=B,l.lazify=O,l.modPow=d,l.parseCSV=M,l.residue=h,l.rot32=E,l.rot32BI=q,l.sleep=j,l.toBigInt=_,l.u8ArrToBI=z,l.u8ArrToBase64=R,l.u8ArrToBase64Url=G,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})}));
@@ -1,12 +1,10 @@
1
- declare const __frac_brand: unique symbol;
2
1
  export type FractionData = {
3
2
  type: 'Fraction';
4
3
  numerator: string;
5
4
  denominator: string;
6
5
  };
7
- export default class Fraction {
6
+ declare class Fraction {
8
7
  #private;
9
- readonly [__frac_brand]: unknown;
10
8
  /**
11
9
  *
12
10
  * @param numerator
@@ -73,4 +71,4 @@ export default class Fraction {
73
71
  toJSON(): FractionData;
74
72
  static parse(text: string): Fraction;
75
73
  }
76
- export {};
74
+ export default Fraction;
package/dist/math.d.ts CHANGED
@@ -12,12 +12,6 @@ export declare const getRndInt: (min: number, max: number) => number;
12
12
  * @returns
13
13
  */
14
14
  export declare const residue: (n: bigint, mod: bigint) => bigint;
15
- /**
16
- * number を bigint に変換
17
- * @param nums
18
- * @returns
19
- */
20
- export declare const toBigInt: (...nums: number[]) => bigint[];
21
15
  /**
22
16
  * `length` ビットの乱数 or `length` ビット以下の乱数を出力する
23
17
  * @param length ビット長
@@ -1,20 +1,14 @@
1
- declare const __pcg_brand: unique symbol;
2
1
  /**
3
2
  * PCG-XSH-RR (Permuted congruential generator) 乱数のクラス
4
3
  */
5
4
  export default class PCGMinimal {
6
5
  #private;
7
- readonly [__pcg_brand]: unknown;
8
6
  /** シード値の配列を返す */
9
7
  static getSeed(): BigUint64Array<ArrayBuffer>;
10
8
  /**
11
9
  * @param seeds 64bit整数の配列 (長さ2以上), 省略した場合, 常に同じ値によって初期化される
12
10
  */
13
11
  constructor(seeds?: BigUint64Array<ArrayBuffer>);
14
- /** 内部状態を1サイクル進める */
15
- step(): void;
16
- /** 32bit 乱数を返す (内部状態は変わらない) */
17
- get value(): number;
18
12
  /** 内部状態を1進め、乱数を返す \
19
13
  * 普通はこれを使う
20
14
  */
@@ -28,4 +22,3 @@ export default class PCGMinimal {
28
22
  */
29
23
  genRands(step: number, bound?: number): Generator<number, void, unknown>;
30
24
  }
31
- export {};
package/dist/util.d.ts CHANGED
@@ -1,29 +1,37 @@
1
1
  /**
2
- * 配列が等しいかどうかの真偽値を返す
2
+ * オブジェクトが等しいかどうかの真偽値を返す
3
3
  * @returns
4
4
  */
5
- declare const isEqArray: <T>(arr1: T[], arr2: T[]) => boolean;
5
+ declare const isEqual: (a: unknown, b: unknown) => boolean;
6
6
  /**
7
- * スリープ
8
- * @param delay スリープ時間 (ms)
7
+ * sleep
8
+ * @param delay milliseconds
9
9
  * @returns
10
10
  */
11
11
  declare const sleep: (delay: number) => Promise<void>;
12
- /** 遅延評価関数化する */
12
+ /**
13
+ * makes function lazy
14
+ */
13
15
  declare const lazify: <ArgT extends unknown[], RetT>(func: (...args: ArgT) => RetT) => (...args: ArgT) => () => RetT;
14
16
  /**
15
- * CSVをパースする \
16
- * クォート対応
17
+ * parses CSV string \
18
+ * can deal with CSV with escaping doublequote
17
19
  * @param csv CSV
18
20
  * @returns 2次元配列
19
21
  */
20
22
  declare const parseCSV: (csv: string) => string[][];
21
23
  /**
22
- * 文字列のハッシュ値を返す
24
+ * returns hash of a string
23
25
  * @param str 文字列
24
26
  * @param algorithm アルゴリズム
25
27
  * @returns ハッシュ値
26
28
  */
27
29
  declare const getHash: (str: string, algorithm: AlgorithmIdentifier) => Promise<Uint8Array<ArrayBuffer>>;
28
- declare const isNode: boolean;
29
- export { isEqArray, sleep, lazify, parseCSV, getHash, isNode };
30
+ /**
31
+ * number bigint に変換
32
+ * @param nums
33
+ * @returns
34
+ */
35
+ declare const toBigInt: (...nums: number[]) => bigint[];
36
+ declare const isNode: () => boolean;
37
+ export { isEqual, sleep, lazify, parseCSV, getHash, isNode, toBigInt };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tktb-tess/util-fns",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "Utility functions for personal use",
5
5
  "files": [
6
6
  "dist"
@@ -23,6 +23,7 @@
23
23
  "type": "git",
24
24
  "url": "https://github.com/tktb-tess/util-fns"
25
25
  },
26
+ "homepage": "https://github.com/tktb-tess/util-fns/blob/main/README.md",
26
27
  "scripts": {
27
28
  "dev": "vite",
28
29
  "build": "tsc && vite build",