@gkucmierz/utils 1.21.0 → 1.23.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
@@ -8,6 +8,9 @@ import {
8
8
  import {
9
9
  bijective2num, bijective2numBI, num2bijective, num2bijectiveBI
10
10
  } from './src/bijective-numeration.mjs'
11
+ import {
12
+ bisectionSearchArr
13
+ } from './src/bisection-search.mjs'
11
14
  import {
12
15
  egcd
13
16
  } from './src/egcd.mjs'
@@ -35,6 +38,9 @@ import {
35
38
  import {
36
39
  ListNode
37
40
  } from './src/list-node.mjs'
41
+ import {
42
+ matrixAsArray
43
+ } from './src/matrix.mjs'
38
44
  import {
39
45
  memoize
40
46
  } from './src/memoize.mjs'
@@ -60,6 +66,7 @@ import {
60
66
  export * from './src/SetCnt.mjs';
61
67
  export * from './src/base64.mjs';
62
68
  export * from './src/bijective-numeration.mjs';
69
+ export * from './src/bisection-search.mjs';
63
70
  export * from './src/egcd.mjs';
64
71
  export * from './src/factors.mjs';
65
72
  export * from './src/gcd.mjs';
@@ -69,6 +76,7 @@ export * from './src/heap.mjs';
69
76
  export * from './src/herons-formula.mjs';
70
77
  export * from './src/lcm.mjs';
71
78
  export * from './src/list-node.mjs';
79
+ export * from './src/matrix.mjs';
72
80
  export * from './src/memoize.mjs';
73
81
  export * from './src/mod.mjs';
74
82
  export * from './src/phi.mjs';
@@ -78,5 +86,5 @@ export * from './src/square-root.mjs';
78
86
  export * from './src/tonelli-shanks.mjs';
79
87
 
80
88
  export default [
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
89
+ SetCnt, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, bisectionSearchArr, egcd, factors, factorsBI, gcd, gcdBI, getType, gpn, gpnBI, Heap, heronsFormula, heronsFormulaBI, lcm, lcmBI, ListNode, matrixAsArray, memoize, mod, modBI, phi, phiBI, powMod, powModBI, array2range, range2array, squareRoot, squareRootBI, tonelliShanksBI
82
90
  ];
package/package-lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "1.21.0",
3
+ "version": "1.23.0",
4
4
  "lockfileVersion": 2,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@gkucmierz/utils",
9
- "version": "1.21.0",
9
+ "version": "1.23.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.21.0",
3
+ "version": "1.23.0",
4
4
  "description": "Usefull functions for solving programming tasks",
5
5
  "main": "main.mjs",
6
6
  "scripts": {
@@ -0,0 +1,15 @@
1
+
2
+ import {
3
+ bisectionSearchArr,
4
+ } from '../src/bisection-search.mjs';
5
+
6
+ describe('bisectionSearchArr', () => {
7
+ it('few cases', () => {
8
+ expect(bisectionSearchArr([1, 2, 3, 4], 1)).toBe(0);
9
+ expect(bisectionSearchArr([1, 2, 3, 4], 2)).toBe(1);
10
+ expect(bisectionSearchArr([1, 2, 3, 4], 3)).toBe(2);
11
+ expect(bisectionSearchArr([1, 2, 3, 4], 4)).toBe(3);
12
+ expect(bisectionSearchArr([1, 2, 3, 4], 0)).toBe(-1);
13
+ expect(bisectionSearchArr([1, 2, 3, 4], 5)).toBe(-1);
14
+ });
15
+ });
@@ -24,4 +24,10 @@ describe('ListNode', () => {
24
24
  const arr = [1, 2, 3, 4, 5];
25
25
  expect(ListNode.fromArr(arr).toArr()).toEqual(arr);
26
26
  });
27
+
28
+ it('cyclic reference', () => {
29
+ const node = ListNode.fromArr([1, 2]);
30
+ node.next.next = node;
31
+ expect(() => node.toArr()).toThrow(new Error('Cyclic reference detected'));
32
+ });
27
33
  });
@@ -0,0 +1,19 @@
1
+
2
+ import {
3
+ matrixAsArray,
4
+ } from '../src/matrix.mjs';
5
+
6
+ describe('matrix', () => {
7
+ it('matrixAsArray', () => {
8
+ const m = [
9
+ [1, 2],
10
+ [3, 4],
11
+ ];
12
+ const arr = matrixAsArray(m);
13
+ expect(arr.length).toBe(4);
14
+ expect(arr[0]).toBe(1);
15
+ expect(arr[1]).toBe(2);
16
+ expect(arr[2]).toBe(3);
17
+ expect(arr[3]).toBe(4);
18
+ });
19
+ });
@@ -0,0 +1,19 @@
1
+
2
+ export const bisectionSearchArr = (arr, target) => {
3
+ let [a, b] = [0, arr.length];
4
+ let lm;
5
+ while (b - a > 0) {
6
+ const mid = (a + b) / 2 | 0;
7
+ const val = arr[mid];
8
+ if (target < val) {
9
+ b = mid;
10
+ } else if (val < target) {
11
+ a = mid;
12
+ } else {
13
+ return mid;
14
+ }
15
+ if (lm === mid) break;
16
+ lm = mid;
17
+ }
18
+ return -1;
19
+ };
package/src/list-node.mjs CHANGED
@@ -9,7 +9,10 @@ export class ListNode {
9
9
  toArr() {
10
10
  const ret = [];
11
11
  let node = this;
12
+ const set = new Set();
12
13
  while (node) {
14
+ if (set.has(node)) throw Error('Cyclic reference detected');
15
+ set.add(node);
13
16
  ret.push(node.val);
14
17
  node = node.next;
15
18
  }
package/src/matrix.mjs ADDED
@@ -0,0 +1,17 @@
1
+
2
+
3
+ // represent matrix using array using Proxy
4
+ // todo: iterator can be added
5
+
6
+ export const matrixAsArray = matrix => {
7
+ const [h, w] = [matrix.length, matrix[0].length];
8
+ return new Proxy([], {
9
+ get(target, prop, receiver) {
10
+ if (prop === 'length') return h * w;
11
+ const idx = +prop;
12
+ const col = idx % w;
13
+ const row = (idx - col) / w;
14
+ return matrix[row][col];
15
+ }
16
+ });
17
+ };