@gkucmierz/utils 1.19.0 → 1.21.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,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
+ ListNode
37
+ } from './src/list-node.mjs'
32
38
  import {
33
39
  memoize
34
40
  } from './src/memoize.mjs'
@@ -59,8 +65,10 @@ export * from './src/factors.mjs';
59
65
  export * from './src/gcd.mjs';
60
66
  export * from './src/get-type.mjs';
61
67
  export * from './src/gpn.mjs';
68
+ export * from './src/heap.mjs';
62
69
  export * from './src/herons-formula.mjs';
63
70
  export * from './src/lcm.mjs';
71
+ export * from './src/list-node.mjs';
64
72
  export * from './src/memoize.mjs';
65
73
  export * from './src/mod.mjs';
66
74
  export * from './src/phi.mjs';
@@ -70,5 +78,5 @@ export * from './src/square-root.mjs';
70
78
  export * from './src/tonelli-shanks.mjs';
71
79
 
72
80
  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
81
+ SetCnt, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, egcd, factors, factorsBI, gcd, gcdBI, getType, gpn, gpnBI, Heap, heronsFormula, heronsFormulaBI, lcm, lcmBI, ListNode, memoize, mod, modBI, phi, phiBI, powMod, powModBI, array2range, range2array, squareRoot, squareRootBI, tonelliShanksBI
74
82
  ];
package/package-lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "1.19.0",
3
+ "version": "1.21.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.21.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.21.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
+ });
@@ -0,0 +1,27 @@
1
+
2
+ import { ListNode } from '../src/list-node.mjs';
3
+
4
+ describe('ListNode', () => {
5
+ it('constructor', () => {
6
+ expect(new ListNode().val).toEqual(0);
7
+ expect(new ListNode().next).toEqual(null);
8
+ expect(new ListNode(123).val).toEqual(123);
9
+ expect(new ListNode(123).next).toEqual(null);
10
+ expect(new ListNode(123, 'next').next).toEqual('next');
11
+ });
12
+
13
+ it('toArr', () => {
14
+ expect(new ListNode().toArr()).toEqual([0]);
15
+ expect(new ListNode(123).toArr()).toEqual([123]);
16
+ expect(new ListNode(1, new ListNode(2)).toArr()).toEqual([1, 2]);
17
+ });
18
+
19
+ it('fromArr', () => {
20
+ expect(ListNode.fromArr([1, 2])).toEqual(new ListNode(1, new ListNode(2)));
21
+ });
22
+
23
+ it('both', () => {
24
+ const arr = [1, 2, 3, 4, 5];
25
+ expect(ListNode.fromArr(arr).toArr()).toEqual(arr);
26
+ });
27
+ });
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
+ };
@@ -0,0 +1,28 @@
1
+
2
+
3
+ export class ListNode {
4
+ constructor(val = 0, next = null) {
5
+ this.val = val;
6
+ this.next = next;
7
+ }
8
+
9
+ toArr() {
10
+ const ret = [];
11
+ let node = this;
12
+ while (node) {
13
+ ret.push(node.val);
14
+ node = node.next;
15
+ }
16
+ return ret;
17
+ }
18
+
19
+ static fromArr(iter) {
20
+ const head = new ListNode;
21
+ let node = head;
22
+ for (let val of iter) {
23
+ node.next = new ListNode(val);
24
+ node = node.next;
25
+ }
26
+ return head.next;
27
+ }
28
+ };