@ac-kit/algo 0.6.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 (156) hide show
  1. package/CHANGELOG.md +67 -0
  2. package/README.md +44 -15
  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/floyd-warshall.d.ts +67 -0
  24. package/dist/graph/floyd-warshall.d.ts.map +1 -0
  25. package/dist/graph/floyd-warshall.js +106 -0
  26. package/dist/graph/floyd-warshall.js.map +1 -0
  27. package/dist/graph/graph-cycle-error.d.ts +16 -0
  28. package/dist/graph/graph-cycle-error.d.ts.map +1 -0
  29. package/dist/graph/graph-cycle-error.js +20 -0
  30. package/dist/graph/graph-cycle-error.js.map +1 -0
  31. package/dist/graph/lowest-common-ancestor.d.ts +32 -0
  32. package/dist/graph/lowest-common-ancestor.d.ts.map +1 -0
  33. package/dist/graph/lowest-common-ancestor.js +56 -0
  34. package/dist/graph/lowest-common-ancestor.js.map +1 -0
  35. package/dist/graph/topological-sort.d.ts +51 -0
  36. package/dist/graph/topological-sort.d.ts.map +1 -0
  37. package/dist/graph/topological-sort.js +124 -0
  38. package/dist/graph/topological-sort.js.map +1 -0
  39. package/dist/group/_nest-by.d.ts +19 -0
  40. package/dist/group/_nest-by.d.ts.map +1 -0
  41. package/dist/group/_nest-by.js +85 -0
  42. package/dist/group/_nest-by.js.map +1 -0
  43. package/dist/group/count-by.d.ts +31 -0
  44. package/dist/group/count-by.d.ts.map +1 -0
  45. package/dist/group/count-by.js +35 -0
  46. package/dist/group/count-by.js.map +1 -0
  47. package/dist/group/flat-group-by.d.ts +28 -0
  48. package/dist/group/flat-group-by.d.ts.map +1 -0
  49. package/dist/group/flat-group-by.js +30 -0
  50. package/dist/group/flat-group-by.js.map +1 -0
  51. package/dist/group/group-adjacent-by.d.ts +31 -0
  52. package/dist/group/group-adjacent-by.d.ts.map +1 -0
  53. package/dist/group/group-adjacent-by.js +50 -0
  54. package/dist/group/group-adjacent-by.js.map +1 -0
  55. package/dist/group/group-sort.d.ts +38 -0
  56. package/dist/group/group-sort.d.ts.map +1 -0
  57. package/dist/group/group-sort.js +49 -0
  58. package/dist/group/group-sort.js.map +1 -0
  59. package/dist/group/index-by.d.ts +33 -0
  60. package/dist/group/index-by.d.ts.map +1 -0
  61. package/dist/group/index-by.js +42 -0
  62. package/dist/group/index-by.js.map +1 -0
  63. package/dist/group/nested-map.d.ts +12 -0
  64. package/dist/group/nested-map.d.ts.map +1 -0
  65. package/dist/group/nested-map.js +2 -0
  66. package/dist/group/nested-map.js.map +1 -0
  67. package/dist/group/rollup-by.d.ts +62 -0
  68. package/dist/group/rollup-by.d.ts.map +1 -0
  69. package/dist/group/rollup-by.js +72 -0
  70. package/dist/group/rollup-by.js.map +1 -0
  71. package/dist/group/run-length-encode.d.ts +41 -0
  72. package/dist/group/run-length-encode.d.ts.map +1 -0
  73. package/dist/group/run-length-encode.js +66 -0
  74. package/dist/group/run-length-encode.js.map +1 -0
  75. package/dist/index.d.ts +32 -1
  76. package/dist/index.d.ts.map +1 -1
  77. package/dist/index.js +32 -1
  78. package/dist/index.js.map +1 -1
  79. package/dist/ordered/is-sorted.d.ts +33 -0
  80. package/dist/ordered/is-sorted.d.ts.map +1 -0
  81. package/dist/ordered/is-sorted.js +39 -0
  82. package/dist/ordered/is-sorted.js.map +1 -0
  83. package/dist/sample/sample-without-replacement.d.ts +26 -0
  84. package/dist/sample/sample-without-replacement.d.ts.map +1 -0
  85. package/dist/sample/sample-without-replacement.js +41 -0
  86. package/dist/sample/sample-without-replacement.js.map +1 -0
  87. package/dist/sample/shuffle.d.ts +2 -2
  88. package/dist/sample/shuffle.d.ts.map +1 -1
  89. package/dist/sample/shuffle.js +3 -3
  90. package/dist/sample/shuffle.js.map +1 -1
  91. package/dist/sample/weighted-choice-index.d.ts +29 -0
  92. package/dist/sample/weighted-choice-index.d.ts.map +1 -0
  93. package/dist/sample/weighted-choice-index.js +62 -0
  94. package/dist/sample/weighted-choice-index.js.map +1 -0
  95. package/dist/sample/weighted-choice.d.ts +28 -0
  96. package/dist/sample/weighted-choice.d.ts.map +1 -0
  97. package/dist/sample/weighted-choice.js +31 -0
  98. package/dist/sample/weighted-choice.js.map +1 -0
  99. package/dist/select/minmax.d.ts +52 -0
  100. package/dist/select/minmax.d.ts.map +1 -0
  101. package/dist/select/minmax.js +109 -0
  102. package/dist/select/minmax.js.map +1 -0
  103. package/dist/select/{quickselect.d.ts → quickselect-in-place.d.ts} +2 -2
  104. package/dist/select/quickselect-in-place.d.ts.map +1 -0
  105. package/dist/select/{quickselect.js → quickselect-in-place.js} +2 -2
  106. package/dist/select/quickselect-in-place.js.map +1 -0
  107. package/dist/sequence/_matching-runs.d.ts +16 -0
  108. package/dist/sequence/_matching-runs.d.ts.map +1 -0
  109. package/dist/sequence/_matching-runs.js +178 -0
  110. package/dist/sequence/_matching-runs.js.map +1 -0
  111. package/dist/sequence/common-prefix-length.d.ts +26 -0
  112. package/dist/sequence/common-prefix-length.d.ts.map +1 -0
  113. package/dist/sequence/common-prefix-length.js +34 -0
  114. package/dist/sequence/common-prefix-length.js.map +1 -0
  115. package/dist/sequence/diff-sequences.d.ts +59 -0
  116. package/dist/sequence/diff-sequences.d.ts.map +1 -0
  117. package/dist/sequence/diff-sequences.js +67 -0
  118. package/dist/sequence/diff-sequences.js.map +1 -0
  119. package/dist/sequence/levenshtein-distance.d.ts +43 -0
  120. package/dist/sequence/levenshtein-distance.d.ts.map +1 -0
  121. package/dist/sequence/levenshtein-distance.js +102 -0
  122. package/dist/sequence/levenshtein-distance.js.map +1 -0
  123. package/dist/sequence/longest-common-subsequence.d.ts +28 -0
  124. package/dist/sequence/longest-common-subsequence.d.ts.map +1 -0
  125. package/dist/sequence/longest-common-subsequence.js +39 -0
  126. package/dist/sequence/longest-common-subsequence.js.map +1 -0
  127. package/dist/sequence/longest-increasing-subsequence.d.ts +28 -0
  128. package/dist/sequence/longest-increasing-subsequence.d.ts.map +1 -0
  129. package/dist/sequence/longest-increasing-subsequence.js +47 -0
  130. package/dist/sequence/longest-increasing-subsequence.js.map +1 -0
  131. package/dist/sort/_sort-primitives.d.ts +6 -0
  132. package/dist/sort/_sort-primitives.d.ts.map +1 -0
  133. package/dist/sort/_sort-primitives.js +18 -0
  134. package/dist/sort/_sort-primitives.js.map +1 -0
  135. package/dist/sort/counting-sort.d.ts +40 -0
  136. package/dist/sort/counting-sort.d.ts.map +1 -0
  137. package/dist/sort/counting-sort.js +51 -0
  138. package/dist/sort/counting-sort.js.map +1 -0
  139. package/dist/sort/merge-sort.d.ts +26 -0
  140. package/dist/sort/merge-sort.d.ts.map +1 -0
  141. package/dist/sort/merge-sort.js +73 -0
  142. package/dist/sort/merge-sort.js.map +1 -0
  143. package/dist/sort/quick-sort-in-place.d.ts +35 -0
  144. package/dist/sort/quick-sort-in-place.d.ts.map +1 -0
  145. package/dist/sort/quick-sort-in-place.js +98 -0
  146. package/dist/sort/quick-sort-in-place.js.map +1 -0
  147. package/dist/sort/rank.d.ts +64 -0
  148. package/dist/sort/rank.d.ts.map +1 -0
  149. package/dist/sort/rank.js +107 -0
  150. package/dist/sort/rank.js.map +1 -0
  151. package/dist/sort/sort-range.d.ts.map +1 -1
  152. package/dist/sort/sort-range.js +1 -16
  153. package/dist/sort/sort-range.js.map +1 -1
  154. package/package.json +6 -6
  155. package/dist/select/quickselect.d.ts.map +0 -1
  156. package/dist/select/quickselect.js.map +0 -1
@@ -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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AChE,KAAK,IAAI,KAAK,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,KAAK,IAAI,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC;QACxD,WAAW,CAAC,KAAK,CAAC,GAAG,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,CAA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K,GAAG,GAAG,EAAE,CAAC;gBACrC,oEAAoE;gBACpE,IAAI,OAAO,CAAC,MAAM,CAAC,IAAI,CAAE,EAAE,MAAM,CAAC,KAAK,CAAE,CAAC,IAAI,CAAC,EAAE,CAAC;oBACjD,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,MAAM,CAAC,IAAI,EAAE,CAAE,CAAC;gBACjC,CAAC;qBAAM,CAAC;oBACP,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,CAAE,CAAC;gBAClC,CAAC;YACF,CAAC;YACD,OAAO,IAAI,GAAG,MAAM,EAAE,CAAC;gBACtB,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,MAAM,CAAC,IAAI,EAAE,CAAE,CAAC;YACjC,CAAC;YACD,OAAO,KAAK,GAAG,GAAG,EAAE,CAAC;gBACpB,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,CAAE,CAAC;YAClC,CAAC;QACF,CAAC;QACD,MAAM,MAAM,GAAG,MAAM,CAAC;QACtB,MAAM,GAAG,MAAM,CAAC;QAChB,MAAM,GAAG,MAAM,CAAC;IACjB,CAAC;IACD,OAAO,MAAM,CAAC;AACf,CAAC"}
@@ -0,0 +1,35 @@
1
+ import type { Comparator } from "@ac-kit/core";
2
+ import type { WritableArrayLike } from "./sort-range.js";
3
+ /**
4
+ * Sorts `items` in place by `compare` — quicksort, unstable, with O(log n)
5
+ * auxiliary space and no allocation beyond a small stack of ranges.
6
+ *
7
+ * Partitions three ways around the median of the first, middle and last items
8
+ * (Dijkstra's Dutch national flag): items equivalent to the pivot are settled
9
+ * in the pass that finds them, so many duplicates cost less, not more, and an
10
+ * all-equal input takes one pass. Ranges below 16 items are finished by
11
+ * insertion sort. No recursion: the larger side of each partition waits on an
12
+ * explicit stack while the smaller one is sorted, which bounds the stack to
13
+ * ⌊log₂ n⌋ ranges.
14
+ *
15
+ * The worst case stays O(n²) comparisons, on an input crafted against the
16
+ * median of three; `sortRange` (introsort) is the in-place sort that guarantees
17
+ * O(n log n).
18
+ *
19
+ * A comparator that is not a strict weak order leaves `items` in some
20
+ * permutation of its elements.
21
+ *
22
+ * Time complexity: O(n log n) comparisons on average, O(n · d) for `d` distinct
23
+ * keys, O(n²) in the worst case. Memory: O(log n).
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * const scores = [3, 1, 2];
28
+ * quickSortInPlace(scores, compareNaturalAscending); // scores is [1, 2, 3]
29
+ * ```;
30
+ *
31
+ * @param items - Sorted in place: an array or a typed array.
32
+ * @param compare - A strict weak ordering of the items.
33
+ */
34
+ export declare function quickSortInPlace<T>(items: WritableArrayLike<T>, compare: Comparator<T>): void;
35
+ //# sourceMappingURL=quick-sort-in-place.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quick-sort-in-place.d.ts","sourceRoot":"","sources":["../../src/sort/quick-sort-in-place.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAG/C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AA2BzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EACjC,KAAK,EAAE,iBAAiB,CAAC,CAAC,CAAC,EAC3B,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,GACpB,IAAI,CA2CN"}
@@ -0,0 +1,98 @@
1
+ import { insertionSort_, swap_ } from "./_sort-primitives.js";
2
+ // Below this length a range is finished by insertion sort, which beats any
3
+ // partitioning on a few elements.
4
+ const INSERTION_LENGTH_ = 16;
5
+ function medianOfThree_(items, compare, from, to) {
6
+ const first = items[from];
7
+ const middle = items[from + ((to - from) >> 1)];
8
+ const last = items[to - 1];
9
+ if (compare(first, middle) < 0) {
10
+ if (compare(middle, last) < 0) {
11
+ return middle;
12
+ }
13
+ return compare(first, last) < 0 ? last : first;
14
+ }
15
+ if (compare(first, last) < 0) {
16
+ return first;
17
+ }
18
+ return compare(middle, last) < 0 ? last : middle;
19
+ }
20
+ /**
21
+ * Sorts `items` in place by `compare` — quicksort, unstable, with O(log n)
22
+ * auxiliary space and no allocation beyond a small stack of ranges.
23
+ *
24
+ * Partitions three ways around the median of the first, middle and last items
25
+ * (Dijkstra's Dutch national flag): items equivalent to the pivot are settled
26
+ * in the pass that finds them, so many duplicates cost less, not more, and an
27
+ * all-equal input takes one pass. Ranges below 16 items are finished by
28
+ * insertion sort. No recursion: the larger side of each partition waits on an
29
+ * explicit stack while the smaller one is sorted, which bounds the stack to
30
+ * ⌊log₂ n⌋ ranges.
31
+ *
32
+ * The worst case stays O(n²) comparisons, on an input crafted against the
33
+ * median of three; `sortRange` (introsort) is the in-place sort that guarantees
34
+ * O(n log n).
35
+ *
36
+ * A comparator that is not a strict weak order leaves `items` in some
37
+ * permutation of its elements.
38
+ *
39
+ * Time complexity: O(n log n) comparisons on average, O(n · d) for `d` distinct
40
+ * keys, O(n²) in the worst case. Memory: O(log n).
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * const scores = [3, 1, 2];
45
+ * quickSortInPlace(scores, compareNaturalAscending); // scores is [1, 2, 3]
46
+ * ```;
47
+ *
48
+ * @param items - Sorted in place: an array or a typed array.
49
+ * @param compare - A strict weak ordering of the items.
50
+ */
51
+ export function quickSortInPlace(items, compare) {
52
+ const pending = [];
53
+ let from = 0;
54
+ let to = items.length;
55
+ for (;;) {
56
+ while (to - from > INSERTION_LENGTH_) {
57
+ const pivot = medianOfThree_(items, compare, from, to);
58
+ // [from, less) < pivot, [less, index) ≡ pivot, (greater, to) > pivot.
59
+ let less = from;
60
+ let index = from;
61
+ let greater = to - 1;
62
+ while (index <= greater) {
63
+ const order = compare(items[index], pivot);
64
+ if (order < 0) {
65
+ swap_(items, less++, index++);
66
+ }
67
+ else if (order > 0) {
68
+ swap_(items, index, greater--);
69
+ }
70
+ else {
71
+ index++;
72
+ }
73
+ }
74
+ const lowerLength = less - from;
75
+ const upperLength = to - greater - 1;
76
+ // Only a comparator that is not a strict weak order can leave the
77
+ // pivot's class empty with one side the whole range.
78
+ if (lowerLength === to - from || upperLength === to - from) {
79
+ break;
80
+ }
81
+ if (lowerLength < upperLength) {
82
+ pending.push(greater + 1, to);
83
+ to = less;
84
+ }
85
+ else {
86
+ pending.push(from, less);
87
+ from = greater + 1;
88
+ }
89
+ }
90
+ insertionSort_(items, compare, from, to);
91
+ if (pending.length === 0) {
92
+ return;
93
+ }
94
+ to = pending.pop();
95
+ from = pending.pop();
96
+ }
97
+ }
98
+ //# sourceMappingURL=quick-sort-in-place.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quick-sort-in-place.js","sourceRoot":"","sources":["../../src/sort/quick-sort-in-place.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAG9D,2EAA2E;AAC3E,kCAAkC;AAClC,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAE7B,SAAS,cAAc,CACtB,KAA2B,EAC3B,OAAsB,EACtB,IAAY,EACZ,EAAU;IAEV,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAE,CAAC;IAC3B,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,GAAG,CAAC,CAAC,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAE,CAAC;IACjD,MAAM,IAAI,GAAG,KAAK,CAAC,EAAE,GAAG,CAAC,CAAE,CAAC;IAC5B,IAAI,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;QAChC,IAAI,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;YAC/B,OAAO,MAAM,CAAC;QACf,CAAC;QACD,OAAO,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,C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AG,IAAI,GAAG,IAAI,CAAC;YAChC,MAAM,WAAW,GAAG,EAAE,GAAG,OAAO,GAAG,CAAC,CAAC;YACrC,kEAAkE;YAClE,qDAAqD;YACrD,IAAI,WAAW,KAAK,EAAE,GAAG,IAAI,IAAI,WAAW,KAAK,EAAE,GAAG,IAAI,EAAE,CAAC;gBAC5D,MAAM;YACP,CAAC;YACD,IAAI,WAAW,GAAG,WAAW,EAAE,CAAC;gBAC/B,OAAO,CAAC,IAAI,CAAC,OAAO,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC;gBAC9B,EAAE,GAAG,IAAI,CAAC;YACX,CAAC;iBAAM,CAAC;gBACP,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;gBACzB,IAAI,GAAG,OAAO,GAAG,CAAC,CAAC;YACpB,CAAC;QACF,CAAC;QACD,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;QACzC,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC1B,OAAO;QACR,CAAC;QACD,EAAE,GAAG,OAAO,CAAC,GAAG,EAAG,CAAC;QACpB,IAAI,GAAG,OAAO,CAAC,GAAG,EAAG,CAAC;IACvB,CAAC;AACF,CAAC"}
@@ -0,0 +1,64 @@
1
+ import type { Callable, Comparator } from "@ac-kit/core";
2
+ /**
3
+ * How tied values share ranks. With values `[10, 20, 20, 30]`:
4
+ *
5
+ * - `"average"` — the mean of the positions the ties span: `[1, 2.5, 2.5, 4]`
6
+ * (fractional ranking; what Spearman's and Kendall's coefficients and the
7
+ * rank tests need, since it keeps the rank sum `n(n + 1)/2`).
8
+ * - `"min"` — the lowest of those positions: `[1, 2, 2, 4]` (standard competition
9
+ * ranking, "1224").
10
+ * - `"max"` — the highest: `[1, 3, 3, 4]` (modified competition ranking, "1334").
11
+ * - `"dense"` — consecutive ranks for distinct values: `[1, 2, 2, 3]` ("1223").
12
+ * - `"ordinal"` — every value its own rank, ties in input order: `[1, 2, 3, 4]`
13
+ * ("1234").
14
+ *
15
+ * The names are SciPy `rankdata`'s and pandas `rank`'s methods (pandas calls
16
+ * `"ordinal"` `"first"`).
17
+ */
18
+ export type RankTies = "average" | "min" | "max" | "dense" | "ordinal";
19
+ /** Options of {@link rank} and {@link rankBy}. */
20
+ export type RankOptions = {
21
+ /** How tied values share ranks. Defaults to `"average"`. */
22
+ readonly ties?: RankTies;
23
+ };
24
+ /**
25
+ * The 1-based rank of each value in the order `compare` sorts them, written at
26
+ * the value's own position: `result[i]` is the rank of `values[i]`.
27
+ *
28
+ * Values `compare` finds equivalent are tied and share a rank by `options.ties`
29
+ * ({@link RankTies}, `"average"` by default).
30
+ *
31
+ * O(n log n) comparisons; one array of positions and the result.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * rank([30, 10, 20, 10], compareNaturalAscending); // [4, 1.5, 3, 1.5]
36
+ * rank([30, 10, 20, 10], compareNaturalAscending, { ties: "dense" }); // [3, 1, 2, 1]
37
+ * ```;
38
+ *
39
+ * @param values The values to rank. Not modified.
40
+ * @param compare A strict weak ordering of the values.
41
+ * @param options How ties share ranks.
42
+ * @returns A new array of `values.length` ranks in `[1, n]`.
43
+ */
44
+ export declare function rank<T>(values: ArrayLike<T>, compare: Comparator<T>, options?: RankOptions): Float64Array;
45
+ /**
46
+ * The 1-based rank of each item by the number `valueOf` gives it, ascending,
47
+ * written at the item's own position: `result[i]` is the rank of `items[i]`.
48
+ *
49
+ * Equal keys are tied (`-0` ties `0`) and share a rank by `options.ties`
50
+ * ({@link RankTies}, `"average"` by default). An item whose key is `NaN` has no
51
+ * place in the order: its rank is `NaN`, and the other items are ranked among
52
+ * themselves (pandas' `na_option="keep"`).
53
+ *
54
+ * O(n log n) comparisons and one `valueOf` call per item; one key, one position
55
+ * and one result array.
56
+ *
57
+ * @param items The items to rank. Not modified.
58
+ * @param valueOf The numeric key of an item.
59
+ * @param options How ties share ranks.
60
+ * @returns A new array of `items.length` ranks, each in `[1, m]` for the `m`
61
+ * items with a key, or `NaN`.
62
+ */
63
+ export declare function rankBy<T>(items: ArrayLike<T>, valueOf: Callable<[T], number>, options?: RankOptions): Float64Array;
64
+ //# sourceMappingURL=rank.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rank.d.ts","sourceRoot":"","sources":["../../src/sort/rank.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAIzD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,MAAM,QAAQ,GAAG,SAAS,GAAG,KAAK,GAAG,KAAK,GAAG,OAAO,GAAG,SAAS,CAAC;AAEvE,kDAAkD;AAClD,MAAM,MAAM,WAAW,GAAG;IACzB,4DAA4D;IAC5D,QAAQ,CAAC,IAAI,CAAC,EAAE,QAAQ,CAAC;CACzB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,IAAI,CAAC,CAAC,EACrB,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,EACpB,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,EACtB,OAAO,CAAC,EAAE,WAAW,GACnB,YAAY,CAWd;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,MAAM,CAAC,CAAC,EACvB,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,EACnB,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,EAC9B,OAAO,CAAC,EAAE,WAAW,GACnB,YAAY,CA+Bd"}
@@ -0,0 +1,107 @@
1
+ import { sortedIndices } from "./sorted-indices.js";
2
+ /**
3
+ * The 1-based rank of each value in the order `compare` sorts them, written at
4
+ * the value's own position: `result[i]` is the rank of `values[i]`.
5
+ *
6
+ * Values `compare` finds equivalent are tied and share a rank by `options.ties`
7
+ * ({@link RankTies}, `"average"` by default).
8
+ *
9
+ * O(n log n) comparisons; one array of positions and the result.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * rank([30, 10, 20, 10], compareNaturalAscending); // [4, 1.5, 3, 1.5]
14
+ * rank([30, 10, 20, 10], compareNaturalAscending, { ties: "dense" }); // [3, 1, 2, 1]
15
+ * ```;
16
+ *
17
+ * @param values The values to rank. Not modified.
18
+ * @param compare A strict weak ordering of the values.
19
+ * @param options How ties share ranks.
20
+ * @returns A new array of `values.length` ranks in `[1, n]`.
21
+ */
22
+ export function rank(values, compare, options) {
23
+ const order = sortedIndices(values, compare);
24
+ const ranks = new Float64Array(values.length);
25
+ assignRanks_(ranks, order, (left, right) => compare(values[left], values[right]) === 0, options?.ties ?? "average");
26
+ return ranks;
27
+ }
28
+ /**
29
+ * The 1-based rank of each item by the number `valueOf` gives it, ascending,
30
+ * written at the item's own position: `result[i]` is the rank of `items[i]`.
31
+ *
32
+ * Equal keys are tied (`-0` ties `0`) and share a rank by `options.ties`
33
+ * ({@link RankTies}, `"average"` by default). An item whose key is `NaN` has no
34
+ * place in the order: its rank is `NaN`, and the other items are ranked among
35
+ * themselves (pandas' `na_option="keep"`).
36
+ *
37
+ * O(n log n) comparisons and one `valueOf` call per item; one key, one position
38
+ * and one result array.
39
+ *
40
+ * @param items The items to rank. Not modified.
41
+ * @param valueOf The numeric key of an item.
42
+ * @param options How ties share ranks.
43
+ * @returns A new array of `items.length` ranks, each in `[1, m]` for the `m`
44
+ * items with a key, or `NaN`.
45
+ */
46
+ export function rankBy(items, valueOf, options) {
47
+ const count = items.length;
48
+ const keys = new Float64Array(count);
49
+ const order = [];
50
+ const ranks = new Float64Array(count);
51
+ for (let index = 0; index < count; index++) {
52
+ const key = valueOf(items[index]);
53
+ keys[index] = key;
54
+ if (key === key) {
55
+ order.push(index);
56
+ }
57
+ else {
58
+ ranks[index] = Number.NaN;
59
+ }
60
+ }
61
+ order.sort((left, right) => {
62
+ const leftKey = keys[left];
63
+ const rightKey = keys[right];
64
+ if (leftKey < rightKey) {
65
+ return -1;
66
+ }
67
+ return leftKey > rightKey ? 1 : left - right;
68
+ });
69
+ assignRanks_(ranks, order, (left, right) => keys[left] === keys[right], options?.ties ?? "average");
70
+ return ranks;
71
+ }
72
+ function assignRanks_(ranks, order, tied, ties) {
73
+ const count = order.length;
74
+ let distinct = 0;
75
+ let start = 0;
76
+ while (start < count) {
77
+ let end = start + 1;
78
+ while (end < count && tied(order[start], order[end])) {
79
+ end++;
80
+ }
81
+ distinct++;
82
+ for (let position = start; position < end; position++) {
83
+ ranks[order[position]] = groupRank_(ties, start, end, position, distinct);
84
+ }
85
+ start = end;
86
+ }
87
+ }
88
+ function groupRank_(ties, start, end, position, distinct) {
89
+ switch (ties) {
90
+ case "average": {
91
+ return (start + 1 + end) / 2;
92
+ }
93
+ case "min": {
94
+ return start + 1;
95
+ }
96
+ case "max": {
97
+ return end;
98
+ }
99
+ case "dense": {
100
+ return distinct;
101
+ }
102
+ case "ordinal": {
103
+ return position + 1;
104
+ }
105
+ }
106
+ }
107
+ //# sourceMappingURL=rank.js.map