@gkucmierz/utils 1.19.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 CHANGED
@@ -23,6 +23,9 @@ 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'
@@ -59,6 +62,7 @@ export * from './src/factors.mjs';
59
62
  export * from './src/gcd.mjs';
60
63
  export * from './src/get-type.mjs';
61
64
  export * from './src/gpn.mjs';
65
+ export * from './src/heap.mjs';
62
66
  export * from './src/herons-formula.mjs';
63
67
  export * from './src/lcm.mjs';
64
68
  export * from './src/memoize.mjs';
@@ -70,5 +74,5 @@ export * from './src/square-root.mjs';
70
74
  export * from './src/tonelli-shanks.mjs';
71
75
 
72
76
  export default [
73
- SetCnt, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, egcd, factors, factorsBI, gcd, gcdBI, getType, gpn, gpnBI, heronsFormula, heronsFormulaBI, lcm, lcmBI, memoize, 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
74
78
  ];
package/package-lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "1.19.0",
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.19.0",
9
+ "version": "1.20.0",
10
10
  "license": "MIT",
11
11
  "devDependencies": {
12
12
  "husky": "^8.0.1",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "1.19.0",
3
+ "version": "1.20.0",
4
4
  "description": "Usefull functions for solving programming tasks",
5
5
  "main": "main.mjs",
6
6
  "scripts": {
@@ -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
+ });
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
+ };