@thi.ng/arrays 2.1.1 → 2.2.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**: 2021-11-19T07:59:50Z
3
+ - **Last updated**: 2022-03-11T12:13:49Z
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,21 @@ 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.2.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/arrays@2.2.0) (2022-03-11)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add argSort() ([4b65c36](https://github.com/thi-ng/umbrella/commit/4b65c36))
17
+
18
+ ### [2.1.3](https://github.com/thi-ng/umbrella/tree/@thi.ng/arrays@2.1.3) (2021-12-13)
19
+
20
+ #### 🩹 Bug fixes
21
+
22
+ - off-by-one error in shuffleRange() ([f832d2f](https://github.com/thi-ng/umbrella/commit/f832d2f))
23
+ - existing shuffle would keep some indices untouched, resulting in
24
+ still perceivable ordering
25
+ - update tests
26
+
12
27
  ## [2.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/arrays@2.1.0) (2021-11-17)
13
28
 
14
29
  #### 🚀 Features
package/README.md CHANGED
@@ -51,7 +51,7 @@ node --experimental-repl-await
51
51
  > const arrays = await import("@thi.ng/arrays");
52
52
  ```
53
53
 
54
- Package sizes (gzipped, pre-treeshake): ESM: 2.24 KB
54
+ Package sizes (gzipped, pre-treeshake): ESM: 2.30 KB
55
55
 
56
56
  ## Dependencies
57
57
 
@@ -67,6 +67,7 @@ Package sizes (gzipped, pre-treeshake): ESM: 2.24 KB
67
67
  [Generated API docs](https://docs.thi.ng/umbrella/arrays/)
68
68
 
69
69
  - [arrayIterator()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/iterator.ts)
70
+ - [argSort()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/arg-sort.ts)
70
71
  - [binarySearch()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/binary-search.ts)
71
72
  - [bisect()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/bisect.ts)
72
73
  - [endsWith()](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays/src/ends-with.ts)
@@ -134,4 +135,4 @@ If this project contributes to an academic publication, please cite it as:
134
135
 
135
136
  ## License
136
137
 
137
- © 2018 - 2021 Karsten Schmidt // Apache Software License 2.0
138
+ © 2018 - 2022 Karsten Schmidt // Apache Software License 2.0
package/arg-sort.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Returns a new array of numeric indices in same order as sorted values of
3
+ * `src` using given (optional) comparator (default: thi.ng/compare). The `src`
4
+ * array will remain unmodified.
5
+ *
6
+ * @remarks
7
+ * Also see {@link swizzle} to read an array in custom order.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const src = ["a", "c", "d", "b"];
12
+ *
13
+ * argSort(src)
14
+ * // [ 0, 3, 1, 2 ]
15
+ *
16
+ * // src[0] => "a"
17
+ * // src[3] => "b"
18
+ * // src[1] => "c"
19
+ * // src[2] => "d"
20
+ *
21
+ * swizzle(argSort(src))(src)
22
+ * // [ 'a', 'b', 'c', 'd' ]
23
+ * ```
24
+ *
25
+ * @param src -
26
+ * @param cmp -
27
+ */
28
+ export declare const argSort: <T>(src: T[], cmp?: (a: any, b: any) => number) => any[];
29
+ //# sourceMappingURL=arg-sort.d.ts.map
package/arg-sort.js ADDED
@@ -0,0 +1,31 @@
1
+ import { compare } from "@thi.ng/compare/compare";
2
+ import { fillRange } from "./fill-range.js";
3
+ import { sortByCachedKey } from "./sort-cached.js";
4
+ /**
5
+ * Returns a new array of numeric indices in same order as sorted values of
6
+ * `src` using given (optional) comparator (default: thi.ng/compare). The `src`
7
+ * array will remain unmodified.
8
+ *
9
+ * @remarks
10
+ * Also see {@link swizzle} to read an array in custom order.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * const src = ["a", "c", "d", "b"];
15
+ *
16
+ * argSort(src)
17
+ * // [ 0, 3, 1, 2 ]
18
+ *
19
+ * // src[0] => "a"
20
+ * // src[3] => "b"
21
+ * // src[1] => "c"
22
+ * // src[2] => "d"
23
+ *
24
+ * swizzle(argSort(src))(src)
25
+ * // [ 'a', 'b', 'c', 'd' ]
26
+ * ```
27
+ *
28
+ * @param src -
29
+ * @param cmp -
30
+ */
31
+ export const argSort = (src, cmp = compare) => sortByCachedKey(fillRange(new Array(src.length)), src.slice(), cmp);
@@ -47,32 +47,32 @@ export declare const binarySearch2: (buf: ArrayLike<number>, x: number) => numbe
47
47
  * Non-recursive, optimized binary search for fixed size numeric arrays of 4
48
48
  * values. Returns index of `x` or `-index-1` if not found.
49
49
  *
50
- * @param buf
51
- * @param x
50
+ * @param buf -
51
+ * @param x -
52
52
  */
53
53
  export declare const binarySearch4: (buf: ArrayLike<number>, x: number) => number;
54
54
  /**
55
55
  * Non-recursive, optimized binary search for fixed size numeric arrays of 8
56
56
  * values. Returns index of `x` or `-index-1` if not found.
57
57
  *
58
- * @param buf
59
- * @param x
58
+ * @param buf -
59
+ * @param x -
60
60
  */
61
61
  export declare const binarySearch8: (buf: ArrayLike<number>, x: number) => number;
62
62
  /**
63
63
  * Non-recursive, optimized binary search for fixed size numeric arrays of 16
64
64
  * values. Returns index of `x` or `-index-1` if not found.
65
65
  *
66
- * @param buf
67
- * @param x
66
+ * @param buf -
67
+ * @param x -
68
68
  */
69
69
  export declare const binarySearch16: (buf: ArrayLike<number>, x: number) => number;
70
70
  /**
71
71
  * Non-recursive, optimized binary search for fixed size numeric arrays of 32
72
72
  * values. Returns index of `x` or `-index-1` if not found.
73
73
  *
74
- * @param buf
75
- * @param x
74
+ * @param buf -
75
+ * @param x -
76
76
  */
77
77
  export declare const binarySearch32: (buf: ArrayLike<number>, x: number) => number;
78
78
  /**
package/binary-search.js CHANGED
@@ -82,8 +82,8 @@ export const binarySearch2 = (buf, x) => {
82
82
  * Non-recursive, optimized binary search for fixed size numeric arrays of 4
83
83
  * values. Returns index of `x` or `-index-1` if not found.
84
84
  *
85
- * @param buf
86
- * @param x
85
+ * @param buf -
86
+ * @param x -
87
87
  */
88
88
  export const binarySearch4 = (buf, x) => {
89
89
  let idx = buf[2] <= x ? 2 : 0;
@@ -94,8 +94,8 @@ export const binarySearch4 = (buf, x) => {
94
94
  * Non-recursive, optimized binary search for fixed size numeric arrays of 8
95
95
  * values. Returns index of `x` or `-index-1` if not found.
96
96
  *
97
- * @param buf
98
- * @param x
97
+ * @param buf -
98
+ * @param x -
99
99
  */
100
100
  export const binarySearch8 = (buf, x) => {
101
101
  let idx = buf[4] <= x ? 4 : 0;
@@ -107,8 +107,8 @@ export const binarySearch8 = (buf, x) => {
107
107
  * Non-recursive, optimized binary search for fixed size numeric arrays of 16
108
108
  * values. Returns index of `x` or `-index-1` if not found.
109
109
  *
110
- * @param buf
111
- * @param x
110
+ * @param buf -
111
+ * @param x -
112
112
  */
113
113
  export const binarySearch16 = (buf, x) => {
114
114
  let idx = buf[8] <= x ? 8 : 0;
@@ -121,8 +121,8 @@ export const binarySearch16 = (buf, x) => {
121
121
  * Non-recursive, optimized binary search for fixed size numeric arrays of 32
122
122
  * values. Returns index of `x` or `-index-1` if not found.
123
123
  *
124
- * @param buf
125
- * @param x
124
+ * @param buf -
125
+ * @param x -
126
126
  */
127
127
  export const binarySearch32 = (buf, x) => {
128
128
  let idx = buf[16] <= x ? 16 : 0;
package/bisect.d.ts CHANGED
@@ -2,8 +2,8 @@ import type { Predicate } from "@thi.ng/api";
2
2
  /**
3
3
  * Splits array at given index (default: floor(src.length/2)) and returns tuple of [lhs, rhs].
4
4
  *
5
- * @param src
6
- * @param i
5
+ * @param src -
6
+ * @param i -
7
7
  */
8
8
  export declare const bisect: <T>(src: T[], i?: number) => T[][];
9
9
  /**
@@ -12,8 +12,8 @@ export declare const bisect: <T>(src: T[], i?: number) => T[][];
12
12
  * will be the first item in the RHS array. If the predicate never succeeds, the
13
13
  * function returns `[src, []]`, i.e. all items will remain in the LHS.
14
14
  *
15
- * @param src
16
- * @param pred
15
+ * @param src -
16
+ * @param pred -
17
17
  */
18
18
  export declare const bisectWith: <T>(src: T[], pred: Predicate<T>) => T[][];
19
19
  //# sourceMappingURL=bisect.d.ts.map
package/bisect.js CHANGED
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * Splits array at given index (default: floor(src.length/2)) and returns tuple of [lhs, rhs].
3
3
  *
4
- * @param src
5
- * @param i
4
+ * @param src -
5
+ * @param i -
6
6
  */
7
7
  export const bisect = (src, i = src.length >>> 1) => [
8
8
  src.slice(0, i),
@@ -14,8 +14,8 @@ export const bisect = (src, i = src.length >>> 1) => [
14
14
  * will be the first item in the RHS array. If the predicate never succeeds, the
15
15
  * function returns `[src, []]`, i.e. all items will remain in the LHS.
16
16
  *
17
- * @param src
18
- * @param pred
17
+ * @param src -
18
+ * @param pred -
19
19
  */
20
20
  export const bisectWith = (src, pred) => {
21
21
  const i = src.findIndex(pred);
package/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./api.js";
2
+ export * from "./arg-sort.js";
2
3
  export * from "./binary-search.js";
3
4
  export * from "./bisect.js";
4
5
  export * from "./ends-with.js";
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./api.js";
2
+ export * from "./arg-sort.js";
2
3
  export * from "./binary-search.js";
3
4
  export * from "./bisect.js";
4
5
  export * from "./ends-with.js";
package/insert.d.ts CHANGED
@@ -12,19 +12,19 @@
12
12
  * faster than `Array.prototype.splice()` (for sizes < ~32). See
13
13
  * `/bench/insert.ts`
14
14
  *
15
- * @param buf
16
- * @param x
17
- * @param i
18
- * @param k
15
+ * @param buf -
16
+ * @param x -
17
+ * @param i -
18
+ * @param k -
19
19
  */
20
20
  export declare const insert: <T>(buf: T[], x: T, i: number, k?: number) => T[];
21
21
  /**
22
22
  * Same as {@link insert} but without any bounds/index checks.
23
23
  *
24
- * @param buf
25
- * @param x
26
- * @param i
27
- * @param k
24
+ * @param buf -
25
+ * @param x -
26
+ * @param i -
27
+ * @param k -
28
28
  */
29
29
  export declare const insertUnsafe: <T>(buf: T[], x: T, i: number, k?: number) => T[];
30
30
  //# sourceMappingURL=insert.d.ts.map
package/insert.js CHANGED
@@ -12,19 +12,19 @@
12
12
  * faster than `Array.prototype.splice()` (for sizes < ~32). See
13
13
  * `/bench/insert.ts`
14
14
  *
15
- * @param buf
16
- * @param x
17
- * @param i
18
- * @param k
15
+ * @param buf -
16
+ * @param x -
17
+ * @param i -
18
+ * @param k -
19
19
  */
20
20
  export const insert = (buf, x, i, k = Infinity) => i < 0 || i >= k || k < 1 ? buf : insertUnsafe(buf, x, i, k);
21
21
  /**
22
22
  * Same as {@link insert} but without any bounds/index checks.
23
23
  *
24
- * @param buf
25
- * @param x
26
- * @param i
27
- * @param k
24
+ * @param buf -
25
+ * @param x -
26
+ * @param i -
27
+ * @param k -
28
28
  */
29
29
  export const insertUnsafe = (buf, x, i, k = Infinity) => {
30
30
  let j = buf.length < k ? buf.length + 1 : k;
package/into.d.ts CHANGED
@@ -2,9 +2,9 @@
2
2
  * Appends `max` items (default: all) from `src` iterable to `dest` array.
3
3
  * Returns `dest`.
4
4
  *
5
- * @param dest
6
- * @param src
7
- * @param max
5
+ * @param dest -
6
+ * @param src -
7
+ * @param max -
8
8
  */
9
9
  export declare const into: <T>(dest: T[], src: Iterable<T>, max?: number) => T[];
10
10
  //# sourceMappingURL=into.d.ts.map
package/into.js CHANGED
@@ -2,9 +2,9 @@
2
2
  * Appends `max` items (default: all) from `src` iterable to `dest` array.
3
3
  * Returns `dest`.
4
4
  *
5
- * @param dest
6
- * @param src
7
- * @param max
5
+ * @param dest -
6
+ * @param src -
7
+ * @param max -
8
8
  */
9
9
  export const into = (dest, src, max = Infinity) => {
10
10
  for (let x of src) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/arrays",
3
- "version": "2.1.1",
3
+ "version": "2.2.0",
4
4
  "description": "Array / Arraylike utilities",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -34,20 +34,20 @@
34
34
  "test": "testament test"
35
35
  },
36
36
  "dependencies": {
37
- "@thi.ng/api": "^8.3.1",
38
- "@thi.ng/checks": "^3.1.1",
39
- "@thi.ng/compare": "^2.1.1",
40
- "@thi.ng/equiv": "^2.1.1",
41
- "@thi.ng/errors": "^2.1.1",
42
- "@thi.ng/random": "^3.2.1"
37
+ "@thi.ng/api": "^8.3.4",
38
+ "@thi.ng/checks": "^3.1.4",
39
+ "@thi.ng/compare": "^2.1.4",
40
+ "@thi.ng/equiv": "^2.1.4",
41
+ "@thi.ng/errors": "^2.1.4",
42
+ "@thi.ng/random": "^3.2.4"
43
43
  },
44
44
  "devDependencies": {
45
- "@microsoft/api-extractor": "^7.18.19",
46
- "@thi.ng/testament": "^0.2.1",
45
+ "@microsoft/api-extractor": "^7.19.4",
46
+ "@thi.ng/testament": "^0.2.4",
47
47
  "rimraf": "^3.0.2",
48
48
  "tools": "^0.0.1",
49
- "typedoc": "^0.22.9",
50
- "typescript": "^4.5.2"
49
+ "typedoc": "^0.22.13",
50
+ "typescript": "^4.6.2"
51
51
  },
52
52
  "keywords": [
53
53
  "aos",
@@ -76,6 +76,9 @@
76
76
  "./api": {
77
77
  "import": "./api.js"
78
78
  },
79
+ "./arg-sort": {
80
+ "import": "./arg-sort.js"
81
+ },
79
82
  "./binary-search": {
80
83
  "import": "./binary-search.js"
81
84
  },
@@ -140,5 +143,5 @@
140
143
  "thi.ng": {
141
144
  "year": 2018
142
145
  },
143
- "gitHead": "8bd27c8bde0b770e7c001943f11c951cd345d668\n"
146
+ "gitHead": "0fc692a3225c068aacafdc4cb6140cf603c67ad8\n"
144
147
  }
package/shuffle.js CHANGED
@@ -16,15 +16,12 @@ import { SYSTEM } from "@thi.ng/random/system";
16
16
  */
17
17
  export const shuffleRange = (buf, start = 0, end = buf.length, rnd = SYSTEM) => {
18
18
  assert(start >= 0 && end >= start && end <= buf.length, `illegal range ${start}..${end}`);
19
- let n = end - start;
20
- const l = n;
21
- if (l > 1) {
22
- while (n-- > 0) {
23
- const a = (start + rnd.float(l)) | 0;
24
- const b = (start + rnd.float(l)) | 0;
19
+ if (end - start > 1) {
20
+ for (let i = end; i-- > start;) {
21
+ const a = rnd.minmax(start, i + 1) | 0;
25
22
  const t = buf[a];
26
- buf[a] = buf[b];
27
- buf[b] = t;
23
+ buf[a] = buf[i];
24
+ buf[i] = t;
28
25
  }
29
26
  }
30
27
  return buf;
package/sort-cached.d.ts CHANGED
@@ -22,9 +22,9 @@ import type { Comparator, Fn } from "@thi.ng/api";
22
22
  * // [ 'd', 'c', 'b', 'a' ]
23
23
  * ```
24
24
  *
25
- * @param src
26
- * @param key
27
- * @param cmp
25
+ * @param src -
26
+ * @param key -
27
+ * @param cmp -
28
28
  */
29
29
  export declare const sortByCachedKey: <T, K>(src: T[], key: Fn<T, K> | K[], cmp?: Comparator<K>) => T[];
30
30
  //# sourceMappingURL=sort-cached.d.ts.map
package/sort-cached.js CHANGED
@@ -26,9 +26,9 @@ import { multiSwap } from "./swap.js";
26
26
  * // [ 'd', 'c', 'b', 'a' ]
27
27
  * ```
28
28
  *
29
- * @param src
30
- * @param key
31
- * @param cmp
29
+ * @param src -
30
+ * @param key -
31
+ * @param cmp -
32
32
  */
33
33
  export const sortByCachedKey = (src, key, cmp = compare) => {
34
34
  const keys = isFunction(key) ? src.map(key) : key;