@gkucmierz/utils 1.18.0 → 1.20.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/main.mjs +9 -1
- package/package-lock.json +2 -2
- package/package.json +1 -1
- package/spec/heap.spec.mjs +32 -0
- package/spec/memoize.spec.mjs +32 -0
- package/src/heap.mjs +46 -0
- package/src/memoize.mjs +38 -0
package/main.mjs
CHANGED
|
@@ -23,12 +23,18 @@ import {
|
|
|
23
23
|
import {
|
|
24
24
|
gpn, gpnBI
|
|
25
25
|
} from './src/gpn.mjs'
|
|
26
|
+
import {
|
|
27
|
+
Heap
|
|
28
|
+
} from './src/heap.mjs'
|
|
26
29
|
import {
|
|
27
30
|
heronsFormula, heronsFormulaBI
|
|
28
31
|
} from './src/herons-formula.mjs'
|
|
29
32
|
import {
|
|
30
33
|
lcm, lcmBI
|
|
31
34
|
} from './src/lcm.mjs'
|
|
35
|
+
import {
|
|
36
|
+
memoize
|
|
37
|
+
} from './src/memoize.mjs'
|
|
32
38
|
import {
|
|
33
39
|
mod, modBI
|
|
34
40
|
} from './src/mod.mjs'
|
|
@@ -56,8 +62,10 @@ export * from './src/factors.mjs';
|
|
|
56
62
|
export * from './src/gcd.mjs';
|
|
57
63
|
export * from './src/get-type.mjs';
|
|
58
64
|
export * from './src/gpn.mjs';
|
|
65
|
+
export * from './src/heap.mjs';
|
|
59
66
|
export * from './src/herons-formula.mjs';
|
|
60
67
|
export * from './src/lcm.mjs';
|
|
68
|
+
export * from './src/memoize.mjs';
|
|
61
69
|
export * from './src/mod.mjs';
|
|
62
70
|
export * from './src/phi.mjs';
|
|
63
71
|
export * from './src/pow-mod.mjs';
|
|
@@ -66,5 +74,5 @@ export * from './src/square-root.mjs';
|
|
|
66
74
|
export * from './src/tonelli-shanks.mjs';
|
|
67
75
|
|
|
68
76
|
export default [
|
|
69
|
-
SetCnt, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, egcd, factors, factorsBI, gcd, gcdBI, getType, gpn, gpnBI, heronsFormula, heronsFormulaBI, lcm, lcmBI, mod, modBI, phi, phiBI, powMod, powModBI, array2range, range2array, squareRoot, squareRootBI, tonelliShanksBI
|
|
77
|
+
SetCnt, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, egcd, factors, factorsBI, gcd, gcdBI, getType, gpn, gpnBI, Heap, heronsFormula, heronsFormulaBI, lcm, lcmBI, memoize, mod, modBI, phi, phiBI, powMod, powModBI, array2range, range2array, squareRoot, squareRootBI, tonelliShanksBI
|
|
70
78
|
];
|
package/package-lock.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gkucmierz/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.20.0",
|
|
4
4
|
"lockfileVersion": 2,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "@gkucmierz/utils",
|
|
9
|
-
"version": "1.
|
|
9
|
+
"version": "1.20.0",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"devDependencies": {
|
|
12
12
|
"husky": "^8.0.1",
|
package/package.json
CHANGED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
|
|
2
|
+
import { Heap } from '../src/heap.mjs';
|
|
3
|
+
|
|
4
|
+
describe('Heap', () => {
|
|
5
|
+
it('size', () => {
|
|
6
|
+
const heap = Heap();
|
|
7
|
+
expect(heap.size()).toBe(0);
|
|
8
|
+
heap.add(1);
|
|
9
|
+
expect(heap.size()).toBe(1);
|
|
10
|
+
heap.add(1);
|
|
11
|
+
heap.add(1);
|
|
12
|
+
expect(heap.size()).toBe(3);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('take', () => {
|
|
16
|
+
const heap = Heap();
|
|
17
|
+
heap.add(2);
|
|
18
|
+
heap.add(1);
|
|
19
|
+
heap.add(3);
|
|
20
|
+
expect(heap.take()).toBe(1);
|
|
21
|
+
expect(heap.take()).toBe(2);
|
|
22
|
+
expect(heap.take()).toBe(3);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('valFn', () => {
|
|
26
|
+
const heap = Heap(obj => obj.value);
|
|
27
|
+
heap.add({ value: 42, other: 'second' });
|
|
28
|
+
heap.add({ value: 23, other: 'first' });
|
|
29
|
+
expect(heap.take().other).toBe('first');
|
|
30
|
+
expect(heap.take().other).toBe('second');
|
|
31
|
+
});
|
|
32
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
|
|
2
|
+
import {
|
|
3
|
+
memoize,
|
|
4
|
+
} from '../src/memoize.mjs';
|
|
5
|
+
|
|
6
|
+
describe('memoize', () => {
|
|
7
|
+
it('called once', () => {
|
|
8
|
+
let cnt = 0;
|
|
9
|
+
const add = memoize((a, b) => {
|
|
10
|
+
++cnt;
|
|
11
|
+
return a + b;
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
expect(add(1, 2)).toBe(3);
|
|
15
|
+
expect(cnt).toBe(1);
|
|
16
|
+
expect(add(1, 2)).toBe(3);
|
|
17
|
+
expect(cnt).toBe(1);
|
|
18
|
+
|
|
19
|
+
expect(add(2, 1)).toBe(3);
|
|
20
|
+
expect(cnt).toBe(2);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('variable args length', () => {
|
|
24
|
+
const fn = memoize((...args) => args.length);
|
|
25
|
+
|
|
26
|
+
expect(fn()).toBe(0);
|
|
27
|
+
expect(fn(1)).toBe(1);
|
|
28
|
+
expect(fn(1, 2)).toBe(2);
|
|
29
|
+
expect(fn(1, 2, 3)).toBe(3);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
package/src/heap.mjs
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
|
|
2
|
+
export const Heap = (valFn = n => n) => {
|
|
3
|
+
const arr = [-1];
|
|
4
|
+
|
|
5
|
+
const up = idx => {
|
|
6
|
+
while (idx > 1) {
|
|
7
|
+
const ni = idx / 2 | 0;
|
|
8
|
+
if (valFn(arr[idx]) < valFn(arr[ni])) {
|
|
9
|
+
[arr[idx], arr[ni]] = [arr[ni], arr[idx]];
|
|
10
|
+
}
|
|
11
|
+
idx = ni;
|
|
12
|
+
}
|
|
13
|
+
return idx;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
add: el => up(arr.push(el) - 1),
|
|
18
|
+
take: () => {
|
|
19
|
+
const len = arr.length;
|
|
20
|
+
if (len <= 1) return [][0];
|
|
21
|
+
let idx = 1;
|
|
22
|
+
const res = arr[idx];
|
|
23
|
+
while (idx < len) {
|
|
24
|
+
const ia = idx * 2;
|
|
25
|
+
const ib = idx * 2 + 1;
|
|
26
|
+
if (ia >= len) break;
|
|
27
|
+
if (ib >= len || valFn(arr[ia]) < valFn(arr[ib])) {
|
|
28
|
+
arr[idx] = arr[ia];
|
|
29
|
+
idx = ia;
|
|
30
|
+
} else {
|
|
31
|
+
arr[idx] = arr[ib];
|
|
32
|
+
idx = ib;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (idx === arr.length - 1) {
|
|
36
|
+
arr.pop();
|
|
37
|
+
} else {
|
|
38
|
+
arr[idx] = arr.pop();
|
|
39
|
+
up(idx);
|
|
40
|
+
}
|
|
41
|
+
return res;
|
|
42
|
+
},
|
|
43
|
+
size: () => arr.length - 1,
|
|
44
|
+
data: () => arr.slice(1),
|
|
45
|
+
};
|
|
46
|
+
};
|
package/src/memoize.mjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
|
|
2
|
+
export const memoize = fn => {
|
|
3
|
+
const maps = [];
|
|
4
|
+
|
|
5
|
+
const getLeafMap = args => {
|
|
6
|
+
if (!maps[args.length]) maps[args.length] = new Map();
|
|
7
|
+
const map = maps[args.length];
|
|
8
|
+
|
|
9
|
+
const leaf = args.reduce((map, arg) => {
|
|
10
|
+
if (map.has(arg)) return map.get(arg);
|
|
11
|
+
const tmp = new Map();
|
|
12
|
+
map.set(arg, tmp);
|
|
13
|
+
return tmp;
|
|
14
|
+
}, map);
|
|
15
|
+
|
|
16
|
+
return leaf;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const zero = {
|
|
20
|
+
called: false,
|
|
21
|
+
ret: null,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
return function() {
|
|
25
|
+
const args = [...arguments];
|
|
26
|
+
if (args.length === 0) {
|
|
27
|
+
if (zero.called) return zero.ret;
|
|
28
|
+
zero.called = true;
|
|
29
|
+
return zero.ret = fn();
|
|
30
|
+
}
|
|
31
|
+
const [firstArg, ...restArgs] = args;
|
|
32
|
+
const map = getLeafMap(restArgs);
|
|
33
|
+
if (map.has(firstArg)) return map.get(firstArg);
|
|
34
|
+
const ret = fn(...args);
|
|
35
|
+
map.set(firstArg, ret);
|
|
36
|
+
return ret;
|
|
37
|
+
};
|
|
38
|
+
};
|