@thi.ng/arrays 2.8.12 → 2.9.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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2024-03-27T09:53:45Z
3
+ - **Last updated**: 2024-03-28T10:57:28Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,22 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [2.9.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/arrays@2.9.0) (2024-03-28)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add findSequence() & tests ([5f4db56](https://github.com/thi-ng/umbrella/commit/5f4db56))
17
+
18
+ #### ♻️ Refactoring
19
+
20
+ - add support for typed arrays ([1383916](https://github.com/thi-ng/umbrella/commit/1383916))
21
+ - add function overrides to support typed arrays for:
22
+ - argSort()
23
+ - bisect(), bisectWith()
24
+ - floydRivest()
25
+ - update findSequence() ([f9e3c29](https://github.com/thi-ng/umbrella/commit/f9e3c29))
26
+ - reverse inner scan direction
27
+
12
28
  ## [2.8.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/arrays@2.8.0) (2024-02-19)
13
29
 
14
30
  #### 🚀 Features
package/README.md CHANGED
@@ -54,7 +54,7 @@ For Node.js REPL:
54
54
  const arrays = await import("@thi.ng/arrays");
55
55
  ```
56
56
 
57
- Package sizes (brotli'd, pre-treeshake): ESM: 2.90 KB
57
+ Package sizes (brotli'd, pre-treeshake): ESM: 3.00 KB
58
58
 
59
59
  ## Dependencies
60
60
 
@@ -84,7 +84,7 @@ directory are using this package:
84
84
  [Generated API docs](https://docs.thi.ng/umbrella/arrays/)
85
85
 
86
86
  - [`arrayIterator()`](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/iterator.ts)
87
- - [`argMin()` / argMax()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/argmin.ts)
87
+ - [`argMin()` / `argMax()`](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/argmin.ts)
88
88
  - [`argSort()`](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/arg-sort.ts)
89
89
  - [`binarySearch()`](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/binary-search.ts)
90
90
  - [`bisect()`](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/bisect.ts)
@@ -94,6 +94,7 @@ directory are using this package:
94
94
  - [`ensureIterable()`](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/ensure-iterable.ts)
95
95
  - [`fillRange()`](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/fill-range.ts)
96
96
  - [`find()`](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/find.ts)
97
+ - [`findSequence()`](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/find-sequence.ts)
97
98
  - [`floydRivest()`](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/floyd-rivest.ts)
98
99
  - [`fuzzyMatch()`](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/fuzzy-match.ts)
99
100
  - [`insert()`](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/insert.ts)
package/arg-sort.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { Comparator } from "@thi.ng/api";
1
2
  /**
2
3
  * Returns a new array of numeric indices in same order as sorted values of
3
4
  * `src` using given (optional) comparator (default: thi.ng/compare). The `src`
@@ -31,5 +32,5 @@
31
32
  * @param src -
32
33
  * @param cmp -
33
34
  */
34
- export declare const argSort: <T>(src: T[], cmp?: (a: any, b: any) => number) => any[];
35
+ export declare const argSort: <T>(src: Iterable<T>, cmp?: Comparator<T>) => number[];
35
36
  //# sourceMappingURL=arg-sort.d.ts.map
package/arg-sort.js CHANGED
@@ -1,7 +1,14 @@
1
1
  import { compare } from "@thi.ng/compare/compare";
2
2
  import { fillRange } from "./fill-range.js";
3
3
  import { sortByCachedKey } from "./sort-cached.js";
4
- const argSort = (src, cmp = compare) => sortByCachedKey(fillRange(new Array(src.length)), src.slice(), cmp);
4
+ const argSort = (src, cmp = compare) => {
5
+ const $src = [...src];
6
+ return sortByCachedKey(
7
+ fillRange(new Array($src.length)),
8
+ $src,
9
+ cmp
10
+ );
11
+ };
5
12
  export {
6
13
  argSort
7
14
  };
package/bisect.d.ts CHANGED
@@ -1,11 +1,13 @@
1
- import type { Predicate } from "@thi.ng/api";
1
+ import type { Predicate, TypedArray } from "@thi.ng/api";
2
2
  /**
3
- * Splits array at given index (default: floor(src.length/2)) and returns tuple of [lhs, rhs].
3
+ * Splits array at given index (default: floor(src.length/2)) and returns tuple
4
+ * of [lhs, rhs].
4
5
  *
5
6
  * @param src -
6
- * @param i -
7
+ * @param index -
7
8
  */
8
- export declare const bisect: <T>(src: T[], i?: number) => T[][];
9
+ export declare function bisect<T>(src: T[], index?: number): [T[], T[]];
10
+ export declare function bisect<T extends TypedArray>(src: T, index?: number): [T, T];
9
11
  /**
10
12
  * Similar to {@link bisect}, but first finds split index via provided
11
13
  * predicate. The item for which the predicate first returns a truthy result,
@@ -15,5 +17,6 @@ export declare const bisect: <T>(src: T[], i?: number) => T[][];
15
17
  * @param src -
16
18
  * @param pred -
17
19
  */
18
- export declare const bisectWith: <T>(src: T[], pred: Predicate<T>) => T[][];
20
+ export declare function bisectWith<T>(src: T[], pred: Predicate<T>): [T[], T[]];
21
+ export declare function bisectWith<T extends TypedArray>(src: T, pred: Predicate<number>): [T, T];
19
22
  //# sourceMappingURL=bisect.d.ts.map
package/bisect.js CHANGED
@@ -1,11 +1,10 @@
1
- const bisect = (src, i = src.length >>> 1) => [
2
- src.slice(0, i),
3
- src.slice(i)
4
- ];
5
- const bisectWith = (src, pred) => {
1
+ function bisect(src, index = src.length >>> 1) {
2
+ return [src.slice(0, index), src.slice(index)];
3
+ }
4
+ function bisectWith(src, pred) {
6
5
  const i = src.findIndex(pred);
7
- return i >= 0 ? bisect(src, i) : [src, []];
8
- };
6
+ return i >= 0 ? bisect(src, i) : [src, src.slice(0, 0)];
7
+ }
9
8
  export {
10
9
  bisect,
11
10
  bisectWith
package/fill-range.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { TypedArray } from "@thi.ng/api";
1
+ import type { NumericArray } from "@thi.ng/api";
2
2
  /**
3
3
  * Fills given array with values in [start .. end) interval from `index`
4
4
  * and with optional `step` size.
@@ -28,5 +28,5 @@ import type { TypedArray } from "@thi.ng/api";
28
28
  * @param end -
29
29
  * @param step -
30
30
  */
31
- export declare const fillRange: <T extends number[] | TypedArray>(buf: T, index?: number, start?: number, end?: number, step?: number) => T;
31
+ export declare const fillRange: <T extends NumericArray>(buf: T, index?: number, start?: number, end?: number, step?: number) => T;
32
32
  //# sourceMappingURL=fill-range.d.ts.map
package/find-seq.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import type { NumericArray } from "@thi.ng/api";
2
+ export declare const findSequence: (haystack: NumericArray, needle: NumericArray, i?: number) => number;
3
+ //# sourceMappingURL=find-seq.d.ts.map
package/find-seq.js ADDED
@@ -0,0 +1,25 @@
1
+ const findSequence = (haystack, needle, i = 0) => {
2
+ const m = haystack.length;
3
+ const n = needle.length;
4
+ const max = m - n;
5
+ if (max < 0)
6
+ return -1;
7
+ while (i < m) {
8
+ const idx = haystack.indexOf(needle[0], i);
9
+ if (idx < 0 || idx > max)
10
+ return -1;
11
+ let j;
12
+ for (j = 1; j < n; j++) {
13
+ if (haystack[idx + j] !== needle[j]) {
14
+ i = idx + 1;
15
+ break;
16
+ }
17
+ }
18
+ if (j >= n)
19
+ return idx;
20
+ }
21
+ return -1;
22
+ };
23
+ export {
24
+ findSequence
25
+ };
@@ -0,0 +1,26 @@
1
+ import type { TypedArray } from "@thi.ng/api";
2
+ /**
3
+ * Returns index of `needle` sequence in a larger buffer or -1 if unsuccessful.
4
+ * Search only starts at given `start` index.
5
+ *
6
+ * @remarks
7
+ * TODO Also add Boyer-Moore implementation as alternative for longer sequences
8
+ * (but with more overhead).
9
+ *
10
+ * @example
11
+ * ```ts tangle:../export/find-seq.ts
12
+ * import { findSequence } from "@thi.ng/arrays";
13
+ *
14
+ * console.log(
15
+ * findSequence([1, 2, 3, 1, 2, 3, 4], [1, 2, 3, 4])
16
+ * );
17
+ * // 3
18
+ * ```
19
+ *
20
+ * @param buf
21
+ * @param needle
22
+ * @param start
23
+ */
24
+ export declare function findSequence<T>(buf: Array<T>, needle: ArrayLike<T>, start?: number): number;
25
+ export declare function findSequence(buf: TypedArray, needle: ArrayLike<number>, start?: number): number;
26
+ //# sourceMappingURL=find-sequence.d.ts.map
@@ -0,0 +1,25 @@
1
+ function findSequence(buf, needle, start = 0) {
2
+ const m = buf.length;
3
+ const n = needle.length;
4
+ const max = m - n;
5
+ if (max < 0)
6
+ return -1;
7
+ while (start < m) {
8
+ const idx = buf.indexOf(needle[0], start);
9
+ if (idx < 0 || idx > max)
10
+ return -1;
11
+ let j = n;
12
+ while (j-- > 1) {
13
+ if (buf[idx + j] !== needle[j]) {
14
+ start = idx + 1;
15
+ break;
16
+ }
17
+ }
18
+ if (j === 0)
19
+ return idx;
20
+ }
21
+ return -1;
22
+ }
23
+ export {
24
+ findSequence
25
+ };
package/floyd-rivest.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Comparator } from "@thi.ng/api";
1
+ import type { Comparator, TypedArray } from "@thi.ng/api";
2
2
  /**
3
3
  * Implementation of the Floyd-Rivest selection algorithm to partially find &
4
4
  * sort the `k`th smallest elements in given array, according to supplied
@@ -30,5 +30,6 @@ import type { Comparator } from "@thi.ng/api";
30
30
  * @param left
31
31
  * @param right
32
32
  */
33
- export declare const floydRivest: <T>(buf: T[], k?: number, cmp?: Comparator<T>, left?: number, right?: number) => T[];
33
+ export declare function floydRivest<T>(buf: T[], k?: number, cmp?: Comparator<T>, left?: number, right?: number): T[];
34
+ export declare function floydRivest<T extends TypedArray>(buf: T, k?: number, cmp?: Comparator<number>, left?: number, right?: number): T;
34
35
  //# sourceMappingURL=floyd-rivest.d.ts.map
package/floyd-rivest.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { compare } from "@thi.ng/compare/compare";
2
2
  import { swap } from "./swap.js";
3
- const floydRivest = (buf, k = 1, cmp = compare, left = 0, right = buf.length - 1) => {
3
+ function floydRivest(buf, k = 1, cmp = compare, left = 0, right = buf.length - 1) {
4
4
  while (right > left) {
5
5
  if (right - left > 600) {
6
6
  const n = right - left + 1;
@@ -43,7 +43,7 @@ const floydRivest = (buf, k = 1, cmp = compare, left = 0, right = buf.length - 1
43
43
  right = j - 1;
44
44
  }
45
45
  return buf;
46
- };
46
+ }
47
47
  export {
48
48
  floydRivest
49
49
  };
package/index.d.ts CHANGED
@@ -7,8 +7,9 @@ export * from "./blit.js";
7
7
  export * from "./ends-with.js";
8
8
  export * from "./ensure-array.js";
9
9
  export * from "./ensure-iterable.js";
10
- export * from "./find.js";
11
10
  export * from "./fill-range.js";
11
+ export * from "./find.js";
12
+ export * from "./find-sequence.js";
12
13
  export * from "./floyd-rivest.js";
13
14
  export * from "./fuzzy-match.js";
14
15
  export * from "./is-sorted.js";
package/index.js CHANGED
@@ -7,8 +7,9 @@ export * from "./blit.js";
7
7
  export * from "./ends-with.js";
8
8
  export * from "./ensure-array.js";
9
9
  export * from "./ensure-iterable.js";
10
- export * from "./find.js";
11
10
  export * from "./fill-range.js";
11
+ export * from "./find.js";
12
+ export * from "./find-sequence.js";
12
13
  export * from "./floyd-rivest.js";
13
14
  export * from "./fuzzy-match.js";
14
15
  export * from "./is-sorted.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/arrays",
3
- "version": "2.8.12",
3
+ "version": "2.9.0",
4
4
  "description": "Array / Arraylike utilities",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -108,6 +108,9 @@
108
108
  "./fill-range": {
109
109
  "default": "./fill-range.js"
110
110
  },
111
+ "./find-sequence": {
112
+ "default": "./find-sequence.js"
113
+ },
111
114
  "./find": {
112
115
  "default": "./find.js"
113
116
  },
@@ -166,5 +169,5 @@
166
169
  "thi.ng": {
167
170
  "year": 2018
168
171
  },
169
- "gitHead": "feb3b24654f2c931cd3c3308c1c0c807ee14d0e4\n"
172
+ "gitHead": "db5f5a5d1b223a8e6b4999d67836678038fd3560\n"
170
173
  }