@ac-kit/algo 0.5.0 → 0.7.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.
Files changed (191) hide show
  1. package/CHANGELOG.md +128 -0
  2. package/README.md +60 -18
  3. package/dist/combinatorics/cartesian-product.d.ts +46 -0
  4. package/dist/combinatorics/cartesian-product.d.ts.map +1 -0
  5. package/dist/combinatorics/cartesian-product.js +79 -0
  6. package/dist/combinatorics/cartesian-product.js.map +1 -0
  7. package/dist/combinatorics/permutations.d.ts +41 -0
  8. package/dist/combinatorics/permutations.d.ts.map +1 -0
  9. package/dist/combinatorics/permutations.js +70 -0
  10. package/dist/combinatorics/permutations.js.map +1 -0
  11. package/dist/combinatorics/power-set.d.ts +40 -0
  12. package/dist/combinatorics/power-set.d.ts.map +1 -0
  13. package/dist/combinatorics/power-set.js +67 -0
  14. package/dist/combinatorics/power-set.js.map +1 -0
  15. package/dist/graph/bfs.d.ts +66 -0
  16. package/dist/graph/bfs.d.ts.map +1 -0
  17. package/dist/graph/bfs.js +89 -0
  18. package/dist/graph/bfs.js.map +1 -0
  19. package/dist/graph/connected-components.d.ts +35 -0
  20. package/dist/graph/connected-components.d.ts.map +1 -0
  21. package/dist/graph/connected-components.js +75 -0
  22. package/dist/graph/connected-components.js.map +1 -0
  23. package/dist/graph/find-cycle.d.ts +5 -3
  24. package/dist/graph/find-cycle.d.ts.map +1 -1
  25. package/dist/graph/find-cycle.js +23 -3
  26. package/dist/graph/find-cycle.js.map +1 -1
  27. package/dist/graph/floyd-warshall.d.ts +67 -0
  28. package/dist/graph/floyd-warshall.d.ts.map +1 -0
  29. package/dist/graph/floyd-warshall.js +106 -0
  30. package/dist/graph/floyd-warshall.js.map +1 -0
  31. package/dist/graph/graph-cycle-error.d.ts +16 -0
  32. package/dist/graph/graph-cycle-error.d.ts.map +1 -0
  33. package/dist/graph/graph-cycle-error.js +20 -0
  34. package/dist/graph/graph-cycle-error.js.map +1 -0
  35. package/dist/graph/hierarchy-from-parents.js.map +1 -1
  36. package/dist/graph/lowest-common-ancestor.d.ts +32 -0
  37. package/dist/graph/lowest-common-ancestor.d.ts.map +1 -0
  38. package/dist/graph/lowest-common-ancestor.js +56 -0
  39. package/dist/graph/lowest-common-ancestor.js.map +1 -0
  40. package/dist/graph/topological-sort.d.ts +51 -0
  41. package/dist/graph/topological-sort.d.ts.map +1 -0
  42. package/dist/graph/topological-sort.js +124 -0
  43. package/dist/graph/topological-sort.js.map +1 -0
  44. package/dist/group/_nest-by.d.ts +19 -0
  45. package/dist/group/_nest-by.d.ts.map +1 -0
  46. package/dist/group/_nest-by.js +85 -0
  47. package/dist/group/_nest-by.js.map +1 -0
  48. package/dist/group/count-by.d.ts +31 -0
  49. package/dist/group/count-by.d.ts.map +1 -0
  50. package/dist/group/count-by.js +35 -0
  51. package/dist/group/count-by.js.map +1 -0
  52. package/dist/group/flat-group-by.d.ts +28 -0
  53. package/dist/group/flat-group-by.d.ts.map +1 -0
  54. package/dist/group/flat-group-by.js +30 -0
  55. package/dist/group/flat-group-by.js.map +1 -0
  56. package/dist/group/group-adjacent-by.d.ts +31 -0
  57. package/dist/group/group-adjacent-by.d.ts.map +1 -0
  58. package/dist/group/group-adjacent-by.js +50 -0
  59. package/dist/group/group-adjacent-by.js.map +1 -0
  60. package/dist/group/group-sort.d.ts +38 -0
  61. package/dist/group/group-sort.d.ts.map +1 -0
  62. package/dist/group/group-sort.js +49 -0
  63. package/dist/group/group-sort.js.map +1 -0
  64. package/dist/group/index-by.d.ts +33 -0
  65. package/dist/group/index-by.d.ts.map +1 -0
  66. package/dist/group/index-by.js +42 -0
  67. package/dist/group/index-by.js.map +1 -0
  68. package/dist/group/nested-map.d.ts +12 -0
  69. package/dist/group/nested-map.d.ts.map +1 -0
  70. package/dist/group/nested-map.js +2 -0
  71. package/dist/group/nested-map.js.map +1 -0
  72. package/dist/group/rollup-by.d.ts +62 -0
  73. package/dist/group/rollup-by.d.ts.map +1 -0
  74. package/dist/group/rollup-by.js +72 -0
  75. package/dist/group/rollup-by.js.map +1 -0
  76. package/dist/group/run-length-encode.d.ts +41 -0
  77. package/dist/group/run-length-encode.d.ts.map +1 -0
  78. package/dist/group/run-length-encode.js +66 -0
  79. package/dist/group/run-length-encode.js.map +1 -0
  80. package/dist/index.d.ts +39 -1
  81. package/dist/index.d.ts.map +1 -1
  82. package/dist/index.js +39 -1
  83. package/dist/index.js.map +1 -1
  84. package/dist/ordered/_fractional-key.d.ts +38 -0
  85. package/dist/ordered/_fractional-key.d.ts.map +1 -0
  86. package/dist/ordered/_fractional-key.js +251 -0
  87. package/dist/ordered/_fractional-key.js.map +1 -0
  88. package/dist/ordered/bisect-right-numbers-array.d.ts +20 -0
  89. package/dist/ordered/bisect-right-numbers-array.d.ts.map +1 -0
  90. package/dist/ordered/bisect-right-numbers-array.js +42 -0
  91. package/dist/ordered/bisect-right-numbers-array.js.map +1 -0
  92. package/dist/ordered/bisect-right-numbers.d.ts +19 -0
  93. package/dist/ordered/bisect-right-numbers.d.ts.map +1 -0
  94. package/dist/ordered/bisect-right-numbers.js +32 -0
  95. package/dist/ordered/bisect-right-numbers.js.map +1 -0
  96. package/dist/ordered/create-bisector.js.map +1 -1
  97. package/dist/ordered/fractional-key-alphabet.d.ts +41 -0
  98. package/dist/ordered/fractional-key-alphabet.d.ts.map +1 -0
  99. package/dist/ordered/fractional-key-alphabet.js +67 -0
  100. package/dist/ordered/fractional-key-alphabet.js.map +1 -0
  101. package/dist/ordered/fractional-key-error.d.ts +15 -0
  102. package/dist/ordered/fractional-key-error.d.ts.map +1 -0
  103. package/dist/ordered/fractional-key-error.js +20 -0
  104. package/dist/ordered/fractional-key-error.js.map +1 -0
  105. package/dist/ordered/fractional-key.d.ts +82 -0
  106. package/dist/ordered/fractional-key.d.ts.map +1 -0
  107. package/dist/ordered/fractional-key.js +199 -0
  108. package/dist/ordered/fractional-key.js.map +1 -0
  109. package/dist/ordered/is-sorted.d.ts +33 -0
  110. package/dist/ordered/is-sorted.d.ts.map +1 -0
  111. package/dist/ordered/is-sorted.js +39 -0
  112. package/dist/ordered/is-sorted.js.map +1 -0
  113. package/dist/sample/sample-without-replacement.d.ts +26 -0
  114. package/dist/sample/sample-without-replacement.d.ts.map +1 -0
  115. package/dist/sample/sample-without-replacement.js +41 -0
  116. package/dist/sample/sample-without-replacement.js.map +1 -0
  117. package/dist/sample/shuffle.d.ts +2 -2
  118. package/dist/sample/shuffle.d.ts.map +1 -1
  119. package/dist/sample/shuffle.js +3 -3
  120. package/dist/sample/shuffle.js.map +1 -1
  121. package/dist/sample/weighted-choice-index.d.ts +29 -0
  122. package/dist/sample/weighted-choice-index.d.ts.map +1 -0
  123. package/dist/sample/weighted-choice-index.js +62 -0
  124. package/dist/sample/weighted-choice-index.js.map +1 -0
  125. package/dist/sample/weighted-choice.d.ts +28 -0
  126. package/dist/sample/weighted-choice.d.ts.map +1 -0
  127. package/dist/sample/weighted-choice.js +31 -0
  128. package/dist/sample/weighted-choice.js.map +1 -0
  129. package/dist/select/minmax.d.ts +52 -0
  130. package/dist/select/minmax.d.ts.map +1 -0
  131. package/dist/select/minmax.js +109 -0
  132. package/dist/select/minmax.js.map +1 -0
  133. package/dist/select/{quickselect.d.ts → quickselect-in-place.d.ts} +2 -2
  134. package/dist/select/quickselect-in-place.d.ts.map +1 -0
  135. package/dist/select/{quickselect.js → quickselect-in-place.js} +3 -3
  136. package/dist/select/quickselect-in-place.js.map +1 -0
  137. package/dist/sequence/_matching-runs.d.ts +16 -0
  138. package/dist/sequence/_matching-runs.d.ts.map +1 -0
  139. package/dist/sequence/_matching-runs.js +178 -0
  140. package/dist/sequence/_matching-runs.js.map +1 -0
  141. package/dist/sequence/common-prefix-length.d.ts +26 -0
  142. package/dist/sequence/common-prefix-length.d.ts.map +1 -0
  143. package/dist/sequence/common-prefix-length.js +34 -0
  144. package/dist/sequence/common-prefix-length.js.map +1 -0
  145. package/dist/sequence/diff-sequences.d.ts +59 -0
  146. package/dist/sequence/diff-sequences.d.ts.map +1 -0
  147. package/dist/sequence/diff-sequences.js +67 -0
  148. package/dist/sequence/diff-sequences.js.map +1 -0
  149. package/dist/sequence/levenshtein-distance.d.ts +43 -0
  150. package/dist/sequence/levenshtein-distance.d.ts.map +1 -0
  151. package/dist/sequence/levenshtein-distance.js +102 -0
  152. package/dist/sequence/levenshtein-distance.js.map +1 -0
  153. package/dist/sequence/longest-common-subsequence.d.ts +28 -0
  154. package/dist/sequence/longest-common-subsequence.d.ts.map +1 -0
  155. package/dist/sequence/longest-common-subsequence.js +39 -0
  156. package/dist/sequence/longest-common-subsequence.js.map +1 -0
  157. package/dist/sequence/longest-increasing-subsequence.d.ts +28 -0
  158. package/dist/sequence/longest-increasing-subsequence.d.ts.map +1 -0
  159. package/dist/sequence/longest-increasing-subsequence.js +47 -0
  160. package/dist/sequence/longest-increasing-subsequence.js.map +1 -0
  161. package/dist/sort/_sort-primitives.d.ts +6 -0
  162. package/dist/sort/_sort-primitives.d.ts.map +1 -0
  163. package/dist/sort/_sort-primitives.js +18 -0
  164. package/dist/sort/_sort-primitives.js.map +1 -0
  165. package/dist/sort/counting-sort.d.ts +40 -0
  166. package/dist/sort/counting-sort.d.ts.map +1 -0
  167. package/dist/sort/counting-sort.js +51 -0
  168. package/dist/sort/counting-sort.js.map +1 -0
  169. package/dist/sort/merge-sort.d.ts +26 -0
  170. package/dist/sort/merge-sort.d.ts.map +1 -0
  171. package/dist/sort/merge-sort.js +73 -0
  172. package/dist/sort/merge-sort.js.map +1 -0
  173. package/dist/sort/quick-sort-in-place.d.ts +35 -0
  174. package/dist/sort/quick-sort-in-place.d.ts.map +1 -0
  175. package/dist/sort/quick-sort-in-place.js +98 -0
  176. package/dist/sort/quick-sort-in-place.js.map +1 -0
  177. package/dist/sort/radix-index-sorter.d.ts +57 -0
  178. package/dist/sort/radix-index-sorter.d.ts.map +1 -0
  179. package/dist/sort/radix-index-sorter.js +204 -0
  180. package/dist/sort/radix-index-sorter.js.map +1 -0
  181. package/dist/sort/rank.d.ts +64 -0
  182. package/dist/sort/rank.d.ts.map +1 -0
  183. package/dist/sort/rank.js +107 -0
  184. package/dist/sort/rank.js.map +1 -0
  185. package/dist/sort/sort-range.d.ts +48 -0
  186. package/dist/sort/sort-range.d.ts.map +1 -0
  187. package/dist/sort/sort-range.js +267 -0
  188. package/dist/sort/sort-range.js.map +1 -0
  189. package/package.json +6 -5
  190. package/dist/select/quickselect.d.ts.map +0 -1
  191. package/dist/select/quickselect.js.map +0 -1
@@ -0,0 +1,67 @@
1
+ import { resolveEqualityComparator, } from "@ac-kit/core";
2
+ import { matchingRuns_ } from "./_matching-runs.js";
3
+ /**
4
+ * A shortest edit script turning `a` into `b`: the runs of kept, deleted and
5
+ * inserted items, in order — what a diff view, a patch or a keyed list update
6
+ * consumes. Generic over the items, so it diffs characters, lines, tokens or
7
+ * records alike.
8
+ *
9
+ * Myers' O(ND) algorithm in linear space (the one behind `git diff` and GNU
10
+ * diff), without recursion. "Shortest" counts deleted and inserted items, so
11
+ * the kept items form a longest common subsequence (see
12
+ * {@link longestCommonSubsequence}). Between two kept runs, the deletion comes
13
+ * before the insertion; consecutive runs never share an operation; the kept and
14
+ * deleted runs tile `a`, and the kept and inserted runs tile `b`.
15
+ *
16
+ * Time complexity: O((n + m) · D) for `D` the number of deleted and inserted
17
+ * items, so near-identical inputs diff in near-linear time; O((n + m)²) in the
18
+ * worst case. Memory: O(n + m).
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * diffSequences(["a", "b", "c"], ["a", "x", "c"]);
23
+ * // [
24
+ * // { operation: "keep", aIndex: 0, bIndex: 0, length: 1 },
25
+ * // { operation: "delete", aIndex: 1, bIndex: 1, length: 1 },
26
+ * // { operation: "insert", aIndex: 2, bIndex: 1, length: 1 },
27
+ * // { operation: "keep", aIndex: 2, bIndex: 2, length: 1 },
28
+ * // ]
29
+ * ```;
30
+ *
31
+ * @param a - The source sequence.
32
+ * @param b - The target sequence.
33
+ * @param comparisonStrategy - How an item of `a` is compared with an item of
34
+ * `b`, always in that argument order. Default `"strict"`.
35
+ * @returns The runs, in order; empty when both sequences are.
36
+ */
37
+ export function diffSequences(a, b, comparisonStrategy = "strict") {
38
+ const matches = matchingRuns_(a, b, resolveEqualityComparator(comparisonStrategy));
39
+ const runs = [];
40
+ let aIndex = 0;
41
+ let bIndex = 0;
42
+ const addChanges = (aEnd, bEnd) => {
43
+ if (aEnd > aIndex) {
44
+ runs.push({ operation: "delete", aIndex, bIndex, length: aEnd - aIndex });
45
+ }
46
+ if (bEnd > bIndex) {
47
+ runs.push({
48
+ operation: "insert",
49
+ aIndex: aEnd,
50
+ bIndex,
51
+ length: bEnd - bIndex,
52
+ });
53
+ }
54
+ };
55
+ for (let match = 0; match < matches.length; match += 3) {
56
+ const aStart = matches[match];
57
+ const bStart = matches[match + 1];
58
+ const length = matches[match + 2];
59
+ addChanges(aStart, bStart);
60
+ runs.push({ operation: "keep", aIndex: aStart, bIndex: bStart, length });
61
+ aIndex = aStart + length;
62
+ bIndex = bStart + length;
63
+ }
64
+ addChanges(a.length, b.length);
65
+ return runs;
66
+ }
67
+ //# sourceMappingURL=diff-sequences.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"diff-sequences.js","sourceRoot":"","sources":["../../src/sequence/diff-sequences.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,yBAAyB,GAEzB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AA6BpD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,MAAM,UAAU,aAAa,CAC5B,CAAe,EACf,CAAe,EACf,kBAAkB,GAAqC,QAAQ;IAE/D,MAAM,OAAO,GAAG,aAAa,CAC5B,CAAC,EACD,CAAC,EACD,yBAAyB,CAAC,kBAAkB,CAAC,CAC7C,CAAC;IACF,MAAM,IAAI,GAAc,EAAE,CAAC;IAC3B,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,IAAI,MAAM,GAA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enshteinDistanceOptions<T> = {
3
+ /** How an item of `a` is compared with an item of `b`. Default `"strict"`. */
4
+ comparisonStrategy?: EqualityComparisonStrategy<T, T>;
5
+ /**
6
+ * The largest distance of interest, a non-negative number (`Infinity` for no
7
+ * bound, the default). A distance above it is answered `Infinity`, and the
8
+ * bound turns the work from O(n · m) into O(min(n, m) · maxDistance): only
9
+ * the band of the table within `maxDistance` of its diagonal can hold such a
10
+ * distance, and the computation stops at the first row past it. Most callers
11
+ * — "did you mean", fuzzy filtering — only need to know whether the distance
12
+ * is under a small bound.
13
+ */
14
+ maxDistance?: number;
15
+ };
16
+ /**
17
+ * The edit distance between two sequences: the fewest single-item insertions,
18
+ * deletions and substitutions that turn `a` into `b` (Levenshtein, 1965). The
19
+ * measure behind spelling suggestions, fuzzy matching and record linkage.
20
+ *
21
+ * Wagner–Fischer dynamic programming over one row of the table, after trimming
22
+ * the common prefix and suffix, which cost nothing; with
23
+ * {@link LevenshteinDistanceOptions.maxDistance}, restricted to a diagonal band
24
+ * (Ukkonen, 1985). No recursion. Strings work as they are, one item per UTF-16
25
+ * code unit; split them into code points or graphemes first when that matters.
26
+ *
27
+ * Time complexity: O(n · m) for `n` and `m` the lengths left after trimming,
28
+ * O(min(n, m) · k) under a bound `k`. Memory: O(min(n, m)).
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * levenshteinDistance("kitten", "sitting"); // 3
33
+ * levenshteinDistance("kitten", "sitting", { maxDistance: 2 }); // Infinity
34
+ * ```;
35
+ *
36
+ * @param a - The source sequence.
37
+ * @param b - The target sequence.
38
+ * @param options - Item equality and a distance bound.
39
+ * @returns The distance, or `Infinity` when it exceeds `options.maxDistance`.
40
+ * @throws {RangeError} When `options.maxDistance` is negative or `NaN`.
41
+ */
42
+ export declare function levenshteinDistance<T>(a: ArrayLike<T>, b: ArrayLike<T>, options?: LevenshteinDistanceOptions<T>): number;
43
+ //# sourceMappingURL=levenshtein-distance.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"levenshtein-distance.d.ts","sourceRoot":"","sources":["../../src/sequence/levenshtein-distance.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,0BAA0B,EAC/B,MAAM,cAAc,CAAC;AAEtB,MAAM,MAAM,0BAA0B,CAAC,CAAC,IAAI;IAC3C,8EAA8E;IAC9E,kBAAkB,CAAC,EAAE,0BAA0B,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAEtD;;;;;;;;OAQG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EACpC,CAAC,EAAE,SAAS,CAAC,CAAC,CAAC,EACf,CAAC,EAAE,SAAS,CAAC,CAAC,CAAC,EACf,OAAO,GAAE,0BAA0B,CAAC,CAAC,CAAM,GACzC,MAAM,CA8ER"}
@@ -0,0 +1,102 @@
1
+ import { resolveEqualityComparator, } from "@ac-kit/core";
2
+ /**
3
+ * The edit distance between two sequences: the fewest single-item insertions,
4
+ * deletions and substitutions that turn `a` into `b` (Levenshtein, 1965). The
5
+ * measure behind spelling suggestions, fuzzy matching and record linkage.
6
+ *
7
+ * Wagner–Fischer dynamic programming over one row of the table, after trimming
8
+ * the common prefix and suffix, which cost nothing; with
9
+ * {@link LevenshteinDistanceOptions.maxDistance}, restricted to a diagonal band
10
+ * (Ukkonen, 1985). No recursion. Strings work as they are, one item per UTF-16
11
+ * code unit; split them into code points or graphemes first when that matters.
12
+ *
13
+ * Time complexity: O(n · m) for `n` and `m` the lengths left after trimming,
14
+ * O(min(n, m) · k) under a bound `k`. Memory: O(min(n, m)).
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * levenshteinDistance("kitten", "sitting"); // 3
19
+ * levenshteinDistance("kitten", "sitting", { maxDistance: 2 }); // Infinity
20
+ * ```;
21
+ *
22
+ * @param a - The source sequence.
23
+ * @param b - The target sequence.
24
+ * @param options - Item equality and a distance bound.
25
+ * @returns The distance, or `Infinity` when it exceeds `options.maxDistance`.
26
+ * @throws {RangeError} When `options.maxDistance` is negative or `NaN`.
27
+ */
28
+ export function levenshteinDistance(a, b, options = {}) {
29
+ const equals = resolveEqualityComparator(options.comparisonStrategy);
30
+ const maxDistance = options.maxDistance ?? Infinity;
31
+ if (!(maxDistance >= 0)) {
32
+ throw new RangeError(`levenshteinDistance: maxDistance must be a non-negative number, got ${maxDistance}`);
33
+ }
34
+ let start = 0;
35
+ let aEnd = a.length;
36
+ let bEnd = b.length;
37
+ while (start < aEnd && start < bEnd && equals(a[start], b[start])) {
38
+ start++;
39
+ }
40
+ while (aEnd > start && bEnd > start && equals(a[aEnd - 1], b[bEnd - 1])) {
41
+ aEnd--;
42
+ bEnd--;
43
+ }
44
+ const aLength = aEnd - start;
45
+ const bLength = bEnd - start;
46
+ const shortLength = Math.min(aLength, bLength);
47
+ const longLength = Math.max(aLength, bLength);
48
+ // No distance exceeds the longer length, so a larger bound changes nothing,
49
+ // and distances are integers, so a fractional one rounds down.
50
+ const bound = Math.floor(Math.min(maxDistance, longLength));
51
+ if (longLength - shortLength > bound) {
52
+ return Infinity;
53
+ }
54
+ if (shortLength === 0) {
55
+ return longLength;
56
+ }
57
+ // `row[i]` is the distance between the first `i` items of the shorter
58
+ // remainder and the first `j` of the longer, for the row `j` in progress.
59
+ // Values are capped at `cap`, which outside the band stands for "too far".
60
+ const cap = bound + 1;
61
+ const aIsShort = aLength <= bLength;
62
+ const row = new Uint32Array(shortLength + 1);
63
+ for (let index = 0; index <= shortLength; index++) {
64
+ row[index] = index <= bound ? index : cap;
65
+ }
66
+ for (let column = 1; column <= longLength; column++) {
67
+ const low = Math.max(1, column - bound);
68
+ const high = Math.min(shortLength, column + bound);
69
+ let diagonal = row[low - 1];
70
+ let left = low === 1 && column <= bound ? column : cap;
71
+ row[low - 1] = left;
72
+ let rowMin = left;
73
+ for (let index = low; index <= high; index++) {
74
+ const up = row[index];
75
+ const same = aIsShort
76
+ ? equals(a[start + index - 1], b[start + column - 1])
77
+ : equals(a[start + column - 1], b[start + index - 1]);
78
+ let distance = diagonal + (same ? 0 : 1);
79
+ if (up + 1 < distance) {
80
+ distance = up + 1;
81
+ }
82
+ if (left + 1 < distance) {
83
+ distance = left + 1;
84
+ }
85
+ if (distance > cap) {
86
+ distance = cap;
87
+ }
88
+ diagonal = up;
89
+ row[index] = distance;
90
+ left = distance;
91
+ if (distance < rowMin) {
92
+ rowMin = distance;
93
+ }
94
+ }
95
+ if (rowMin > bound) {
96
+ return Infinity;
97
+ }
98
+ }
99
+ const distance = row[shortLength];
100
+ return distance > bound ? Infinity : distance;
101
+ }
102
+ //# sourceMappingURL=levenshtein-distance.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"levenshtein-distance.js","sourceRoot":"","sources":["../../src/sequence/levenshtein-distance.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,yBAAyB,GAEzB,MAAM,cAAc,CAAC;AAkBtB;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,mBAAmB,CAClC,CAAe,EACf,CAAe,EACf,OAAO,GAAkC,EAAE;IAE3C,MAAM,MAAM,GAAG,yBAAyB,CAAC,OAAO,CAAC,kBAAkB,CAAC,CAAC;IACrE,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,QAAQ,CAAC;IACpD,IAAI,CAAC,CAAC,WAAW,IAAI,CAAC,CAAC,EAAE,CAAC;QACzB,MAAM,IAAI,UAAU,CACnB,uEAAuE,WAAW,EAAE,CACpF,CAAC;IACH,CAAC;IAED,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,IAAI,IAAI,GAAG,CAAC,CAAC,MAAM,CAAC;IACpB,IAAI,IAAI,GAAG,CAAC,CAAC,MAAM,CAAC;IACpB,OAAO,KAAK,GAAG,IAAI,IAAI,KAAK,GAAG,IAAI,IAAI,MAAM,CAAC,CAAC,CAAC,KAAK,CAAE,EAAE,CAAC,CAAC,KAAK,CAAE,CAAC,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AAG,EAAE,GAAG,CAAC,CAAC;YACnB,CAAC;YACD,IAAI,IAAI,GAAG,CAAC,GAAG,QAAQ,EAAE,CAAC;gBACzB,QAAQ,GAAG,IAAI,GAAG,CAAC,CAAC;YACrB,CAAC;YACD,IAAI,QAAQ,GAAG,GAAG,EAAE,CAAC;gBACpB,QAAQ,GAAG,GAAG,CAAC;YAChB,CAAC;YACD,QAAQ,GAAG,EAAE,CAAC;YACd,GAAG,CAAC,KAAK,CAAC,GAAG,QAAQ,CAAC;YACtB,IAAI,GAAG,QAAQ,CAAC;YAChB,IAAI,QAAQ,GAAG,MAAM,EAAE,CAAC;gBACvB,MAAM,GAAG,QAAQ,CAAC;YACnB,CAAC;QACF,CAAC;QACD,IAAI,MAAM,GAAG,KAAK,EAAE,CAAC;YACpB,OAAO,QAAQ,CAAC;QACjB,CAAC;IACF,CAAC;IACD,MAAM,QAAQ,GAAG,GAAG,CAAC,WAAW,CAAE,CAAC;IACnC,OAAO,QAAQ,GAAG,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC;AAC/C,CAAC"}
@@ -0,0 +1,28 @@
1
+ import { type EqualityComparisonStrategy } from "@ac-kit/core";
2
+ /**
3
+ * A longest sequence of items appearing in both `a` and `b` in the same order,
4
+ * not necessarily contiguously — the quantity every line diff maximises: what
5
+ * stays when as little as possible is deleted and inserted. When several
6
+ * subsequences are longest, one of them is returned.
7
+ *
8
+ * The kept runs of {@link diffSequences}' Myers diff, which needs O(n + m)
9
+ * memory where the textbook dynamic program needs O(n · m), and runs in
10
+ * near-linear time on similar inputs. For the contiguous counterpart, see
11
+ * `longestCommonSubstring` (planned).
12
+ *
13
+ * Time complexity: O((n + m) · D) for `D = n + m − 2 · L`, `L` the result's
14
+ * length; O((n + m)²) in the worst case. Memory: O(n + m).
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * longestCommonSubsequence("ABCBDAB", "BDCABA").length; // 4, e.g. "BCBA"
19
+ * ```;
20
+ *
21
+ * @param a - One sequence.
22
+ * @param b - The other sequence.
23
+ * @param comparisonStrategy - How an item of `a` is compared with an item of
24
+ * `b`, always in that argument order. Default `"strict"`.
25
+ * @returns The subsequence, as the matched items of `a`, in order.
26
+ */
27
+ export declare function longestCommonSubsequence<T>(a: ArrayLike<T>, b: ArrayLike<T>, comparisonStrategy?: EqualityComparisonStrategy<T, T>): T[];
28
+ //# sourceMappingURL=longest-common-subsequence.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"longest-common-subsequence.d.ts","sourceRoot":"","sources":["../../src/sequence/longest-common-subsequence.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,0BAA0B,EAC/B,MAAM,cAAc,CAAC;AAItB;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,wBAAwB,CAAC,CAAC,EACzC,CAAC,EAAE,SAAS,CAAC,CAAC,CAAC,EACf,CAAC,EAAE,SAAS,CAAC,CAAC,CAAC,EACf,kBAAkB,GAAE,0BAA0B,CAAC,CAAC,EAAE,CAAC,CAAY,GAC7D,CAAC,EAAE,CAcL"}
@@ -0,0 +1,39 @@
1
+ import { resolveEqualityComparator, } from "@ac-kit/core";
2
+ import { matchingRuns_ } from "./_matching-runs.js";
3
+ /**
4
+ * A longest sequence of items appearing in both `a` and `b` in the same order,
5
+ * not necessarily contiguously — the quantity every line diff maximises: what
6
+ * stays when as little as possible is deleted and inserted. When several
7
+ * subsequences are longest, one of them is returned.
8
+ *
9
+ * The kept runs of {@link diffSequences}' Myers diff, which needs O(n + m)
10
+ * memory where the textbook dynamic program needs O(n · m), and runs in
11
+ * near-linear time on similar inputs. For the contiguous counterpart, see
12
+ * `longestCommonSubstring` (planned).
13
+ *
14
+ * Time complexity: O((n + m) · D) for `D = n + m − 2 · L`, `L` the result's
15
+ * length; O((n + m)²) in the worst case. Memory: O(n + m).
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * longestCommonSubsequence("ABCBDAB", "BDCABA").length; // 4, e.g. "BCBA"
20
+ * ```;
21
+ *
22
+ * @param a - One sequence.
23
+ * @param b - The other sequence.
24
+ * @param comparisonStrategy - How an item of `a` is compared with an item of
25
+ * `b`, always in that argument order. Default `"strict"`.
26
+ * @returns The subsequence, as the matched items of `a`, in order.
27
+ */
28
+ export function longestCommonSubsequence(a, b, comparisonStrategy = "strict") {
29
+ const matches = matchingRuns_(a, b, resolveEqualityComparator(comparisonStrategy));
30
+ const subsequence = [];
31
+ for (let match = 0; match < matches.length; match += 3) {
32
+ const end = matches[match] + matches[match + 2];
33
+ for (let index = matches[match]; index < end; index++) {
34
+ subsequence.push(a[index]);
35
+ }
36
+ }
37
+ return subsequence;
38
+ }
39
+ //# sourceMappingURL=longest-common-subsequence.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"longest-common-subsequence.js","sourceRoot":"","sources":["../../src/sequence/longest-common-subsequence.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,yBAAyB,GAEzB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,wBAAwB,CACvC,CAAe,EACf,CAAe,EACf,kBAAkB,GAAqC,QAAQ;IAE/D,MAAM,OAAO,GAAG,aAAa,CAC5B,CAAC,EACD,CAAC,EACD,yBAAyB,CAAC,kBAAkB,CAAC,CAC7C,CAAC;IACF,MAAM,WAAW,GAAQ,EAAE,CAAC;IAC5B,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,OAAO,CAAC,MAAM,EAAE,KAAK,IAAI,CAAC,EAAE,CAAC;QACxD,MAAM,GAAG,GAAG,OAAO,CAAC,KAAK,CAAE,GAAG,OAAO,CAAC,KAAK,GAAG,CAAC,CAAE,CAAC;QAClD,KAAK,IAAI,KAAK,GAAG,OAAO,CAAC,KAAK,CAAE,EAAE,KAAK,GAAG,GAAG,EAAE,KAAK,EAAE,EAAE,CAAC;YACxD,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAE,CAAC,CAAC;QAC7B,CAAC;IACF,CAAC;IACD,OAAO,WAAW,CAAC;AACpB,CAAC"}
@@ -0,0 +1,28 @@
1
+ import type { Comparator } from "@ac-kit/core";
2
+ /**
3
+ * A longest strictly increasing subsequence of `items` under `compare`: the
4
+ * most items that can be kept, in their order, with each before the next. The
5
+ * fewest moves to sort a list are the items outside it, which is how a keyed
6
+ * list renderer keeps the most nodes in place. When several subsequences are
7
+ * longest, one of them is returned.
8
+ *
9
+ * Patience sorting: for each length, the position of the smallest item that can
10
+ * end an increasing subsequence of that length, kept sorted, so each item finds
11
+ * its place by `bisectLeft`; back-links then rebuild the subsequence. No
12
+ * recursion.
13
+ *
14
+ * Time complexity: O(n log n) comparisons. Memory: O(n).
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * longestIncreasingSubsequence([3, 1, 4, 1, 5, 9, 2, 6], compareNaturalAscending);
19
+ * // [1, 4, 5, 6]
20
+ * ```;
21
+ *
22
+ * @param items - The items, in order.
23
+ * @param compare - A strict weak ordering; equivalent items do not increase, so
24
+ * at most one of them is kept.
25
+ * @returns The subsequence, as the kept items in input order.
26
+ */
27
+ export declare function longestIncreasingSubsequence<T>(items: readonly T[], compare: Comparator<T>): T[];
28
+ //# sourceMappingURL=longest-increasing-subsequence.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"longest-increasing-subsequence.d.ts","sourceRoot":"","sources":["../../src/sequence/longest-increasing-subsequence.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAI/C;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,4BAA4B,CAAC,CAAC,EAC7C,KAAK,EAAE,SAAS,CAAC,EAAE,EACnB,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,GACpB,CAAC,EAAE,CAsBL"}
@@ -0,0 +1,47 @@
1
+ import { bisectLeft } from "../ordered/bisect-left.js";
2
+ /**
3
+ * A longest strictly increasing subsequence of `items` under `compare`: the
4
+ * most items that can be kept, in their order, with each before the next. The
5
+ * fewest moves to sort a list are the items outside it, which is how a keyed
6
+ * list renderer keeps the most nodes in place. When several subsequences are
7
+ * longest, one of them is returned.
8
+ *
9
+ * Patience sorting: for each length, the position of the smallest item that can
10
+ * end an increasing subsequence of that length, kept sorted, so each item finds
11
+ * its place by `bisectLeft`; back-links then rebuild the subsequence. No
12
+ * recursion.
13
+ *
14
+ * Time complexity: O(n log n) comparisons. Memory: O(n).
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * longestIncreasingSubsequence([3, 1, 4, 1, 5, 9, 2, 6], compareNaturalAscending);
19
+ * // [1, 4, 5, 6]
20
+ * ```;
21
+ *
22
+ * @param items - The items, in order.
23
+ * @param compare - A strict weak ordering; equivalent items do not increase, so
24
+ * at most one of them is kept.
25
+ * @returns The subsequence, as the kept items in input order.
26
+ */
27
+ export function longestIncreasingSubsequence(items, compare) {
28
+ const length = items.length;
29
+ // `tails[l]`: the position of the smallest item ending an increasing
30
+ // subsequence of length l + 1 so far.
31
+ const tails = [];
32
+ const previous = new Int32Array(length);
33
+ for (let position = 0; position < length; position++) {
34
+ const item = items[position];
35
+ const place = bisectLeft(tails, item, (tail, value) => compare(items[tail], value));
36
+ previous[position] = place > 0 ? tails[place - 1] : -1;
37
+ tails[place] = position;
38
+ }
39
+ const subsequence = Array.from({ length: tails.length });
40
+ let position = tails.length > 0 ? tails[tails.length - 1] : -1;
41
+ for (let index = tails.length - 1; index >= 0; index--) {
42
+ subsequence[index] = items[position];
43
+ position = previous[position];
44
+ }
45
+ return subsequence;
46
+ }
47
+ //# sourceMappingURL=longest-increasing-subsequence.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"longest-increasing-subsequence.js","sourceRoot":"","sources":["../../src/sequence/longest-increasing-subsequence.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAA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,KAAK,CAAC,QAAQ,CAAE,CAAC;QACtC,QAAQ,GAAG,QAAQ,CAAC,QAAQ,CAAE,CAAC;IAChC,CAAC;IACD,OAAO,WAAW,CAAC;AACpB,CAAC"}
@@ -0,0 +1,6 @@
1
+ import type { Comparator } from "@ac-kit/core";
2
+ import type { WritableArrayLike } from "./sort-range.js";
3
+ export declare function swap_<T>(items: WritableArrayLike<T>, a: number, b: number): void;
4
+ /** Stable insertion sort of `items[from … to)`, O(n²), O(n) on sorted input. */
5
+ export declare function insertionSort_<T>(items: WritableArrayLike<T>, compare: Comparator<T>, from: number, to: number): void;
6
+ //# sourceMappingURL=_sort-primitives.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_sort-primitives.d.ts","sourceRoot":"","sources":["../../src/sort/_sort-primitives.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEzD,wBAAgB,KAAK,CAAC,CAAC,EACtB,KAAK,EAAE,iBAAiB,CAAC,CAAC,CAAC,EAC3B,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,GACP,IAAI,CAIN;AAED,gFAAgF;AAChF,wBAAgB,cAAc,CAAC,CAAC,EAC/B,KAAK,EAAE,iBAAiB,CAAC,CAAC,CAAC,EAC3B,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,EACtB,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,GACR,IAAI,CAUN"}
@@ -0,0 +1,18 @@
1
+ export function swap_(items, a, b) {
2
+ const item = items[a];
3
+ items[a] = items[b];
4
+ items[b] = item;
5
+ }
6
+ /** Stable insertion sort of `items[from … to)`, O(n²), O(n) on sorted input. */
7
+ export function insertionSort_(items, compare, from, to) {
8
+ for (let position = from + 1; position < to; position++) {
9
+ const item = items[position];
10
+ let hole = position;
11
+ while (hole > from && compare(items[hole - 1], item) > 0) {
12
+ items[hole] = items[hole - 1];
13
+ hole--;
14
+ }
15
+ items[hole] = item;
16
+ }
17
+ }
18
+ //# sourceMappingURL=_sort-primitives.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_sort-primitives.js","sourceRoot":"","sources":["../../src/sort/_sort-primitives.ts"],"names":[],"mappings":"AAIA,MAAM,UAAU,KAAK,CACpB,KAA2B,EAC3B,CAAS,EACT,CAAS;IAET,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAE,CAAC;IACvB,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAE,CAAC;IACrB,KAAK,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;AACjB,CAAC;AAED,gFAAgF;AAChF,MAAM,UAAU,cAAc,CAC7B,KAA2B,EAC3B,OAAsB,EACtB,IAAY,EACZ,EAAU;IAEV,KAAK,IAAI,QAAQ,GAAG,IAAI,GAAG,CAAC,EAAE,QAAQ,GAAG,EAAE,EAAE,QAAQ,EAAE,EAAE,CAAC;QACzD,MAAM,IAAI,GAAG,KAAK,CAAC,QAAQ,CAAE,CAAC;QAC9B,IAAI,IAAI,GAAG,QAAQ,CAAC;QACpB,OAAO,IAAI,GAAG,IAAI,IAAI,OAAO,CAAC,KAAK,CAAC,IAAI,GAAG,CAAC,CAAE,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;YAC3D,KAAK,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,IAAI,GAAG,CAAC,CAAE,CAAC;YAC/B,IAAI,EAAE,CAAC;QACR,CAAC;QACD,KAAK,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IACpB,CAAC;AACF,CAAC"}
@@ -0,0 +1,40 @@
1
+ import { type Callable } from "@ac-kit/core";
2
+ export type CountingSortOptions = {
3
+ /** The smallest key, an integer. Default `0`. */
4
+ min?: number;
5
+ /**
6
+ * The largest key, an integer at least `min`, with at most `2³² − 1` keys in
7
+ * `[min, max]`. The work and memory grow with the range, not with how many
8
+ * keys occur, so this is the bound to keep small.
9
+ */
10
+ max: number;
11
+ };
12
+ /**
13
+ * Sorts `items` ascending by an integer key drawn from a small known range,
14
+ * stable — counting sort: count each key, prefix-sum the counts into positions,
15
+ * place each item at its key's next position. Never calls a comparator, and
16
+ * beats `radixSort` whenever the range `k` is small, since `radixSort` pays one
17
+ * pass per 8-bit digit of the largest key whatever the range: days of the
18
+ * month, scores, priorities, letter grades.
19
+ *
20
+ * Runs over `groupIndices`, which does the count, prefix sum and stable fill on
21
+ * positions. `valueOf` is called once per item.
22
+ *
23
+ * Time complexity: O(n + k) for `k = max − min + 1`. Memory: O(n + k).
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * countingSort(tasks, (task) => task.priority, { min: 1, max: 5 });
28
+ * ```;
29
+ *
30
+ * @param items - The items to sort. Not modified; a new array is returned.
31
+ * @param valueOf - The integer key of an item, in `[min, max]`.
32
+ * @param options - The key range.
33
+ * @returns A new array of `items` sorted ascending by `valueOf`, equal keys in
34
+ * input order.
35
+ * @throws {RangeError} When `min` or `max` is not a safe integer, the range is
36
+ * empty or wider than `2³² − 1` keys, or a key is not an integer in `[min,
37
+ * max]` (the message names the position).
38
+ */
39
+ export declare function countingSort<T>(items: readonly T[], valueOf: Callable<[T], number>, options: CountingSortOptions): T[];
40
+ //# sourceMappingURL=counting-sort.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"counting-sort.d.ts","sourceRoot":"","sources":["../../src/sort/counting-sort.ts"],"names":[],"mappings":"AAAA,OAAO,EAIN,KAAK,QAAQ,EACb,MAAM,cAAc,CAAC;AAItB,MAAM,MAAM,mBAAmB,GAAG;IACjC,iDAAiD;IACjD,GAAG,CAAC,EAAE,MAAM,CAAC;IAEb;;;;OAIG;IACH,GAAG,EAAE,MAAM,CAAC;CACZ,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAC7B,KAAK,EAAE,SAAS,CAAC,EAAE,EACnB,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,EAC9B,OAAO,EAAE,mBAAmB,GAC1B,CAAC,EAAE,CAwBL"}
@@ -0,0 +1,51 @@
1
+ import { assertInteger, describeValue, isIntegerInRange, } from "@ac-kit/core";
2
+ import { groupIndices } from "../group/group-indices.js";
3
+ /**
4
+ * Sorts `items` ascending by an integer key drawn from a small known range,
5
+ * stable — counting sort: count each key, prefix-sum the counts into positions,
6
+ * place each item at its key's next position. Never calls a comparator, and
7
+ * beats `radixSort` whenever the range `k` is small, since `radixSort` pays one
8
+ * pass per 8-bit digit of the largest key whatever the range: days of the
9
+ * month, scores, priorities, letter grades.
10
+ *
11
+ * Runs over `groupIndices`, which does the count, prefix sum and stable fill on
12
+ * positions. `valueOf` is called once per item.
13
+ *
14
+ * Time complexity: O(n + k) for `k = max − min + 1`. Memory: O(n + k).
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * countingSort(tasks, (task) => task.priority, { min: 1, max: 5 });
19
+ * ```;
20
+ *
21
+ * @param items - The items to sort. Not modified; a new array is returned.
22
+ * @param valueOf - The integer key of an item, in `[min, max]`.
23
+ * @param options - The key range.
24
+ * @returns A new array of `items` sorted ascending by `valueOf`, equal keys in
25
+ * input order.
26
+ * @throws {RangeError} When `min` or `max` is not a safe integer, the range is
27
+ * empty or wider than `2³² − 1` keys, or a key is not an integer in `[min,
28
+ * max]` (the message names the position).
29
+ */
30
+ export function countingSort(items, valueOf, options) {
31
+ const { min = 0, max } = options;
32
+ assertInteger(min, "countingSort: options.min");
33
+ assertInteger(max, "countingSort: options.max", min, min + 4294967294);
34
+ const keyCount = max - min + 1;
35
+ const length = items.length;
36
+ const keys = new Uint32Array(length);
37
+ for (let position = 0; position < length; position++) {
38
+ const key = valueOf(items[position]);
39
+ if (!isIntegerInRange(key, min, max + 1)) {
40
+ throw new RangeError(`countingSort: the key of items[${position}] must be an integer in [${min}, ${max}], got ${describeValue(key)}`);
41
+ }
42
+ keys[position] = key - min;
43
+ }
44
+ const { indices } = groupIndices(keys, (key) => key, keyCount);
45
+ const sorted = Array.from({ length });
46
+ for (let position = 0; position < length; position++) {
47
+ sorted[position] = items[indices[position]];
48
+ }
49
+ return sorted;
50
+ }
51
+ //# sourceMappingURL=counting-sort.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"counting-sort.js","sourceRoot":"","sources":["../../src/sort/counting-sort.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,aAAa,EACb,aAAa,EACb,gBAAgB,GAEhB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAczD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,UAAU,YAAY,CAC3B,KAAmB,EACnB,OAA8B,EAC9B,OAA4B;IAE5B,MAAM,EAAE,GAAG,GAAG,CAAC,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC;IACjC,aAAa,CAAC,GAAG,EAAE,2BAA2B,CAAC,CAAC;IAChD,aAAa,CAAC,GAAG,EAAE,2BAA2B,EAAE,GAAG,EAAE,GAAG,GAAG,UAAW,CAAC,CAAC;IACxE,MAAM,QAAQ,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC,CAAC;IAE/B,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;IAC5B,MAAM,IAAI,GAAG,IAAI,WAAW,CAAC,MAAM,CAAC,CAAC;IACrC,KAAK,IAAI,QAAQ,GAAG,CAAC,EAAE,QAAQ,GAAG,MAAM,EAAE,QAAQ,EAAE,EAAE,CAAC;QACtD,MAAM,GAAG,GAAG,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAE,CAAC,CAAC;QACtC,IAAI,CAAC,gBAAgB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,GAAG,CAAC,CAAC,EAAE,CAAC;YAC1C,MAAM,IAAI,UAAU,CACnB,kCAAkC,QAAQ,4BAA4B,GAAG,KAAK,GAAG,UAAU,aAAa,CAAC,GAAG,CAAC,EAAE,CAC/G,CAAC;QACH,CAAC;QACD,IAAI,CAAC,QAAQ,CAAC,GAAG,GAAG,GAAG,GAAG,CAAC;IAC5B,CAAC;IAED,MAAM,EAAE,OAAO,EAAE,GAAG,YAAY,CAAC,IAAI,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;IAC/D,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAI,EAAE,MAAM,EAAE,CAAC,CAAC;IACzC,KAAK,IAAI,QAAQ,GAAG,CAAC,EAAE,QAAQ,GAAG,MAAM,EAAE,QAAQ,EAAE,EAAE,CAAC;QACtD,MAAM,CAAC,QAAQ,CAAC,GAAG,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAE,CAAE,CAAC;IAC/C,CAAC;IACD,OAAO,MAAM,CAAC;AACf,CAAC"}
@@ -0,0 +1,26 @@
1
+ import type { Comparator } from "@ac-kit/core";
2
+ /**
3
+ * Sorts `items` by `compare` into a new array, stable — equivalent items keep
4
+ * their input order — with an O(n log n) guarantee whatever the input.
5
+ *
6
+ * Bottom-up merge sort, so no recursion: runs of 16 are sorted by insertion,
7
+ * then merged pairwise in passes of doubling width between two buffers. A pair
8
+ * whose halves are already in order is copied without merging, so sorted and
9
+ * nearly sorted input takes O(n) comparisons. Unlike
10
+ * `Array.prototype.toSorted`, whose stability the specification guarantees but
11
+ * whose complexity it does not, the bound here is part of the contract.
12
+ *
13
+ * Time complexity: O(n log n) comparisons in the worst case, O(n) on sorted
14
+ * input. Memory: O(n) auxiliary, one buffer beside the result.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * mergeSort(people, (a, b) => compareNaturalAscending(a.age, b.age));
19
+ * ```;
20
+ *
21
+ * @param items - The items to sort. Not modified; a new array is returned.
22
+ * @param compare - A strict weak ordering of the items.
23
+ * @returns A new array of `items` in ascending order.
24
+ */
25
+ export declare function mergeSort<T>(items: readonly T[], compare: Comparator<T>): T[];
26
+ //# sourceMappingURL=merge-sort.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"merge-sort.d.ts","sourceRoot":"","sources":["../../src/sort/merge-sort.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAO/C;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS,CAAC,EAAE,EAAE,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAqD7E"}
@@ -0,0 +1,73 @@
1
+ import { insertionSort_ } from "./_sort-primitives.js";
2
+ // Runs this short are first sorted by insertion, which beats merging them.
3
+ const RUN_LENGTH_ = 16;
4
+ /**
5
+ * Sorts `items` by `compare` into a new array, stable — equivalent items keep
6
+ * their input order — with an O(n log n) guarantee whatever the input.
7
+ *
8
+ * Bottom-up merge sort, so no recursion: runs of 16 are sorted by insertion,
9
+ * then merged pairwise in passes of doubling width between two buffers. A pair
10
+ * whose halves are already in order is copied without merging, so sorted and
11
+ * nearly sorted input takes O(n) comparisons. Unlike
12
+ * `Array.prototype.toSorted`, whose stability the specification guarantees but
13
+ * whose complexity it does not, the bound here is part of the contract.
14
+ *
15
+ * Time complexity: O(n log n) comparisons in the worst case, O(n) on sorted
16
+ * input. Memory: O(n) auxiliary, one buffer beside the result.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * mergeSort(people, (a, b) => compareNaturalAscending(a.age, b.age));
21
+ * ```;
22
+ *
23
+ * @param items - The items to sort. Not modified; a new array is returned.
24
+ * @param compare - A strict weak ordering of the items.
25
+ * @returns A new array of `items` in ascending order.
26
+ */
27
+ export function mergeSort(items, compare) {
28
+ const length = items.length;
29
+ let source = items.slice();
30
+ if (length <= 1) {
31
+ return source;
32
+ }
33
+ for (let start = 0; start < length; start += RUN_LENGTH_) {
34
+ insertionSort_(source, compare, start, Math.min(start + RUN_LENGTH_, length));
35
+ }
36
+ let target = Array.from({ length });
37
+ for (let width = RUN_LENGTH_; width < length; width *= 2) {
38
+ for (let start = 0; start < length; start += 2 * width) {
39
+ const middle = Math.min(start + width, length);
40
+ const end = Math.min(start + 2 * width, length);
41
+ if (middle === end ||
42
+ compare(source[middle - 1], source[middle]) <= 0) {
43
+ for (let position = start; position < end; position++) {
44
+ target[position] = source[position];
45
+ }
46
+ continue;
47
+ }
48
+ let left = start;
49
+ let right = middle;
50
+ let out = start;
51
+ while (left < middle && right < end) {
52
+ // `<=` takes the left item on a tie, which is what keeps it stable.
53
+ if (compare(source[left], source[right]) <= 0) {
54
+ target[out++] = source[left++];
55
+ }
56
+ else {
57
+ target[out++] = source[right++];
58
+ }
59
+ }
60
+ while (left < middle) {
61
+ target[out++] = source[left++];
62
+ }
63
+ while (right < end) {
64
+ target[out++] = source[right++];
65
+ }
66
+ }
67
+ const merged = target;
68
+ target = source;
69
+ source = merged;
70
+ }
71
+ return source;
72
+ }
73
+ //# sourceMappingURL=merge-sort.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"merge-sort.js","sourceRoot":"","sources":["../../src/sort/merge-sort.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAEvD,2EAA2E;AAC3E,MAAM,WAAW,GAAG,EAAE,CAAC;AAEvB;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,SAAS,CAAI,KAAmB,EAAE,OAAsB;IACvE,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;IAC5B,IAAI,MAAM,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC;IAC3B,IAAI,MAAM,IAAI,CAAC,EAAE,CAAC;QACjB,OAAO,MAAM,CAAC;IACf,CAAC;IAED,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,MAAM,EAAE,KAAK,IAAI,WAAW,EAAE,CAAC;QAC1D,cAAc,CAC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