@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.
- package/CHANGELOG.md +128 -0
- package/README.md +60 -18
- package/dist/combinatorics/cartesian-product.d.ts +46 -0
- package/dist/combinatorics/cartesian-product.d.ts.map +1 -0
- package/dist/combinatorics/cartesian-product.js +79 -0
- package/dist/combinatorics/cartesian-product.js.map +1 -0
- package/dist/combinatorics/permutations.d.ts +41 -0
- package/dist/combinatorics/permutations.d.ts.map +1 -0
- package/dist/combinatorics/permutations.js +70 -0
- package/dist/combinatorics/permutations.js.map +1 -0
- package/dist/combinatorics/power-set.d.ts +40 -0
- package/dist/combinatorics/power-set.d.ts.map +1 -0
- package/dist/combinatorics/power-set.js +67 -0
- package/dist/combinatorics/power-set.js.map +1 -0
- package/dist/graph/bfs.d.ts +66 -0
- package/dist/graph/bfs.d.ts.map +1 -0
- package/dist/graph/bfs.js +89 -0
- package/dist/graph/bfs.js.map +1 -0
- package/dist/graph/connected-components.d.ts +35 -0
- package/dist/graph/connected-components.d.ts.map +1 -0
- package/dist/graph/connected-components.js +75 -0
- package/dist/graph/connected-components.js.map +1 -0
- package/dist/graph/find-cycle.d.ts +5 -3
- package/dist/graph/find-cycle.d.ts.map +1 -1
- package/dist/graph/find-cycle.js +23 -3
- package/dist/graph/find-cycle.js.map +1 -1
- package/dist/graph/floyd-warshall.d.ts +67 -0
- package/dist/graph/floyd-warshall.d.ts.map +1 -0
- package/dist/graph/floyd-warshall.js +106 -0
- package/dist/graph/floyd-warshall.js.map +1 -0
- package/dist/graph/graph-cycle-error.d.ts +16 -0
- package/dist/graph/graph-cycle-error.d.ts.map +1 -0
- package/dist/graph/graph-cycle-error.js +20 -0
- package/dist/graph/graph-cycle-error.js.map +1 -0
- package/dist/graph/hierarchy-from-parents.js.map +1 -1
- package/dist/graph/lowest-common-ancestor.d.ts +32 -0
- package/dist/graph/lowest-common-ancestor.d.ts.map +1 -0
- package/dist/graph/lowest-common-ancestor.js +56 -0
- package/dist/graph/lowest-common-ancestor.js.map +1 -0
- package/dist/graph/topological-sort.d.ts +51 -0
- package/dist/graph/topological-sort.d.ts.map +1 -0
- package/dist/graph/topological-sort.js +124 -0
- package/dist/graph/topological-sort.js.map +1 -0
- package/dist/group/_nest-by.d.ts +19 -0
- package/dist/group/_nest-by.d.ts.map +1 -0
- package/dist/group/_nest-by.js +85 -0
- package/dist/group/_nest-by.js.map +1 -0
- package/dist/group/count-by.d.ts +31 -0
- package/dist/group/count-by.d.ts.map +1 -0
- package/dist/group/count-by.js +35 -0
- package/dist/group/count-by.js.map +1 -0
- package/dist/group/flat-group-by.d.ts +28 -0
- package/dist/group/flat-group-by.d.ts.map +1 -0
- package/dist/group/flat-group-by.js +30 -0
- package/dist/group/flat-group-by.js.map +1 -0
- package/dist/group/group-adjacent-by.d.ts +31 -0
- package/dist/group/group-adjacent-by.d.ts.map +1 -0
- package/dist/group/group-adjacent-by.js +50 -0
- package/dist/group/group-adjacent-by.js.map +1 -0
- package/dist/group/group-sort.d.ts +38 -0
- package/dist/group/group-sort.d.ts.map +1 -0
- package/dist/group/group-sort.js +49 -0
- package/dist/group/group-sort.js.map +1 -0
- package/dist/group/index-by.d.ts +33 -0
- package/dist/group/index-by.d.ts.map +1 -0
- package/dist/group/index-by.js +42 -0
- package/dist/group/index-by.js.map +1 -0
- package/dist/group/nested-map.d.ts +12 -0
- package/dist/group/nested-map.d.ts.map +1 -0
- package/dist/group/nested-map.js +2 -0
- package/dist/group/nested-map.js.map +1 -0
- package/dist/group/rollup-by.d.ts +62 -0
- package/dist/group/rollup-by.d.ts.map +1 -0
- package/dist/group/rollup-by.js +72 -0
- package/dist/group/rollup-by.js.map +1 -0
- package/dist/group/run-length-encode.d.ts +41 -0
- package/dist/group/run-length-encode.d.ts.map +1 -0
- package/dist/group/run-length-encode.js +66 -0
- package/dist/group/run-length-encode.js.map +1 -0
- package/dist/index.d.ts +39 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +39 -1
- package/dist/index.js.map +1 -1
- package/dist/ordered/_fractional-key.d.ts +38 -0
- package/dist/ordered/_fractional-key.d.ts.map +1 -0
- package/dist/ordered/_fractional-key.js +251 -0
- package/dist/ordered/_fractional-key.js.map +1 -0
- package/dist/ordered/bisect-right-numbers-array.d.ts +20 -0
- package/dist/ordered/bisect-right-numbers-array.d.ts.map +1 -0
- package/dist/ordered/bisect-right-numbers-array.js +42 -0
- package/dist/ordered/bisect-right-numbers-array.js.map +1 -0
- package/dist/ordered/bisect-right-numbers.d.ts +19 -0
- package/dist/ordered/bisect-right-numbers.d.ts.map +1 -0
- package/dist/ordered/bisect-right-numbers.js +32 -0
- package/dist/ordered/bisect-right-numbers.js.map +1 -0
- package/dist/ordered/create-bisector.js.map +1 -1
- package/dist/ordered/fractional-key-alphabet.d.ts +41 -0
- package/dist/ordered/fractional-key-alphabet.d.ts.map +1 -0
- package/dist/ordered/fractional-key-alphabet.js +67 -0
- package/dist/ordered/fractional-key-alphabet.js.map +1 -0
- package/dist/ordered/fractional-key-error.d.ts +15 -0
- package/dist/ordered/fractional-key-error.d.ts.map +1 -0
- package/dist/ordered/fractional-key-error.js +20 -0
- package/dist/ordered/fractional-key-error.js.map +1 -0
- package/dist/ordered/fractional-key.d.ts +82 -0
- package/dist/ordered/fractional-key.d.ts.map +1 -0
- package/dist/ordered/fractional-key.js +199 -0
- package/dist/ordered/fractional-key.js.map +1 -0
- package/dist/ordered/is-sorted.d.ts +33 -0
- package/dist/ordered/is-sorted.d.ts.map +1 -0
- package/dist/ordered/is-sorted.js +39 -0
- package/dist/ordered/is-sorted.js.map +1 -0
- package/dist/sample/sample-without-replacement.d.ts +26 -0
- package/dist/sample/sample-without-replacement.d.ts.map +1 -0
- package/dist/sample/sample-without-replacement.js +41 -0
- package/dist/sample/sample-without-replacement.js.map +1 -0
- package/dist/sample/shuffle.d.ts +2 -2
- package/dist/sample/shuffle.d.ts.map +1 -1
- package/dist/sample/shuffle.js +3 -3
- package/dist/sample/shuffle.js.map +1 -1
- package/dist/sample/weighted-choice-index.d.ts +29 -0
- package/dist/sample/weighted-choice-index.d.ts.map +1 -0
- package/dist/sample/weighted-choice-index.js +62 -0
- package/dist/sample/weighted-choice-index.js.map +1 -0
- package/dist/sample/weighted-choice.d.ts +28 -0
- package/dist/sample/weighted-choice.d.ts.map +1 -0
- package/dist/sample/weighted-choice.js +31 -0
- package/dist/sample/weighted-choice.js.map +1 -0
- package/dist/select/minmax.d.ts +52 -0
- package/dist/select/minmax.d.ts.map +1 -0
- package/dist/select/minmax.js +109 -0
- package/dist/select/minmax.js.map +1 -0
- package/dist/select/{quickselect.d.ts → quickselect-in-place.d.ts} +2 -2
- package/dist/select/quickselect-in-place.d.ts.map +1 -0
- package/dist/select/{quickselect.js → quickselect-in-place.js} +3 -3
- package/dist/select/quickselect-in-place.js.map +1 -0
- package/dist/sequence/_matching-runs.d.ts +16 -0
- package/dist/sequence/_matching-runs.d.ts.map +1 -0
- package/dist/sequence/_matching-runs.js +178 -0
- package/dist/sequence/_matching-runs.js.map +1 -0
- package/dist/sequence/common-prefix-length.d.ts +26 -0
- package/dist/sequence/common-prefix-length.d.ts.map +1 -0
- package/dist/sequence/common-prefix-length.js +34 -0
- package/dist/sequence/common-prefix-length.js.map +1 -0
- package/dist/sequence/diff-sequences.d.ts +59 -0
- package/dist/sequence/diff-sequences.d.ts.map +1 -0
- package/dist/sequence/diff-sequences.js +67 -0
- package/dist/sequence/diff-sequences.js.map +1 -0
- package/dist/sequence/levenshtein-distance.d.ts +43 -0
- package/dist/sequence/levenshtein-distance.d.ts.map +1 -0
- package/dist/sequence/levenshtein-distance.js +102 -0
- package/dist/sequence/levenshtein-distance.js.map +1 -0
- package/dist/sequence/longest-common-subsequence.d.ts +28 -0
- package/dist/sequence/longest-common-subsequence.d.ts.map +1 -0
- package/dist/sequence/longest-common-subsequence.js +39 -0
- package/dist/sequence/longest-common-subsequence.js.map +1 -0
- package/dist/sequence/longest-increasing-subsequence.d.ts +28 -0
- package/dist/sequence/longest-increasing-subsequence.d.ts.map +1 -0
- package/dist/sequence/longest-increasing-subsequence.js +47 -0
- package/dist/sequence/longest-increasing-subsequence.js.map +1 -0
- package/dist/sort/_sort-primitives.d.ts +6 -0
- package/dist/sort/_sort-primitives.d.ts.map +1 -0
- package/dist/sort/_sort-primitives.js +18 -0
- package/dist/sort/_sort-primitives.js.map +1 -0
- package/dist/sort/counting-sort.d.ts +40 -0
- package/dist/sort/counting-sort.d.ts.map +1 -0
- package/dist/sort/counting-sort.js +51 -0
- package/dist/sort/counting-sort.js.map +1 -0
- package/dist/sort/merge-sort.d.ts +26 -0
- package/dist/sort/merge-sort.d.ts.map +1 -0
- package/dist/sort/merge-sort.js +73 -0
- package/dist/sort/merge-sort.js.map +1 -0
- package/dist/sort/quick-sort-in-place.d.ts +35 -0
- package/dist/sort/quick-sort-in-place.d.ts.map +1 -0
- package/dist/sort/quick-sort-in-place.js +98 -0
- package/dist/sort/quick-sort-in-place.js.map +1 -0
- package/dist/sort/radix-index-sorter.d.ts +57 -0
- package/dist/sort/radix-index-sorter.d.ts.map +1 -0
- package/dist/sort/radix-index-sorter.js +204 -0
- package/dist/sort/radix-index-sorter.js.map +1 -0
- package/dist/sort/rank.d.ts +64 -0
- package/dist/sort/rank.d.ts.map +1 -0
- package/dist/sort/rank.js +107 -0
- package/dist/sort/rank.js.map +1 -0
- package/dist/sort/sort-range.d.ts +48 -0
- package/dist/sort/sort-range.d.ts.map +1 -0
- package/dist/sort/sort-range.js +267 -0
- package/dist/sort/sort-range.js.map +1 -0
- package/package.json +6 -5
- package/dist/select/quickselect.d.ts.map +0 -1
- package/dist/select/quickselect.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,133 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.7.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 5c203e7: **Breaking:** two names change to follow the package's naming rules, with no
|
|
8
|
+
alias kept for the old ones.
|
|
9
|
+
|
|
10
|
+
- `quickselect` is now `quickselectInPlace`: the suffix says that it rearranges
|
|
11
|
+
the array it is given. Its signature and behaviour are unchanged.
|
|
12
|
+
- `ShuffleOptions.rand` is now `random`, typed `RandomFn` from `@ac-kit/core`,
|
|
13
|
+
the name every other sampling function in the package uses.
|
|
14
|
+
|
|
15
|
+
Migration: `quickselect(items, k, compare)` becomes
|
|
16
|
+
`quickselectInPlace(items, k, compare)`, and `shuffle(items, { rand })` becomes
|
|
17
|
+
`shuffle(items, { random: rand })`. A `rand` option is now an excess property
|
|
18
|
+
and a type error; untyped callers that keep passing it get `Math.random`.
|
|
19
|
+
- 5c203e7: New graph functions over `childrenOf`/`neighboursOf` callbacks (so
|
|
20
|
+
`@ac-kit/data`'s `Graph` and `DirectedGraph` drive them through `neighbors` and
|
|
21
|
+
`outNeighbors`), all iterative: `bfs(root, traits)`, the level-order walk whose
|
|
22
|
+
`onEnter(node, depth, parent)` gives shortest paths in edge count, with
|
|
23
|
+
`BfsWalkSkipChildren`/`BfsWalkStop`; `topologicalSort(roots, childrenOf,
|
|
24
|
+
options)`, Kahn's order, which throws a `GraphCycleError` carrying one `cycle`
|
|
25
|
+
instead of returning a partial order, with an optional work budget;
|
|
26
|
+
`connectedComponents(nodes, neighboursOf)`, where an edge listed from either end
|
|
27
|
+
counts both ways; `lowestCommonAncestor(a, b, parentOf)`; and
|
|
28
|
+
`floydWarshall(nodes, traits)`, all-pairs `distances` and `nextHops` tables
|
|
29
|
+
that allow negative edges and reject a negative cycle.
|
|
30
|
+
|
|
31
|
+
Sorting and selection: `mergeSort` (stable, O(n log n) guaranteed),
|
|
32
|
+
`quickSortInPlace` (three-way, O(log n) space, arrays and typed arrays),
|
|
33
|
+
`countingSort(items, valueOf, { min, max })`, `isSorted(items, compare, {
|
|
34
|
+
strict })`, and `minmax`/`minmaxBy`, both extremes in one pass (`NaN` keys
|
|
35
|
+
skipped by `minmaxBy`).
|
|
36
|
+
|
|
37
|
+
Grouping: `countBy`, `rollupBy`, `indexBy` (throws on a repeated key),
|
|
38
|
+
`flatGroupBy`, `flatRollupBy` and `groupSort`, with `groupBy`'s array of key
|
|
39
|
+
accessors and nested-`Map` result (`NestedMap`); `groupAdjacentBy` for
|
|
40
|
+
consecutive runs under any equality; `runLengthEncode`/`runLengthDecode`.
|
|
41
|
+
|
|
42
|
+
Sequences, over any array-like (strings included) with an
|
|
43
|
+
`EqualityComparisonStrategy` defaulting to `"strict"`: `levenshteinDistance(a,
|
|
44
|
+
b, { maxDistance })`, which answers `Infinity` past the bound in banded time;
|
|
45
|
+
`diffSequences`, a shortest keep/delete/insert script (Myers, linear space);
|
|
46
|
+
`longestCommonSubsequence`; `longestIncreasingSubsequence`; and
|
|
47
|
+
`commonPrefixLength`.
|
|
48
|
+
|
|
49
|
+
Combinatorics: `permutations`/`permutationsLazy` (Heap's order),
|
|
50
|
+
`powerSet`/`powerSetLazy` and `cartesianProduct`/`cartesianProductLazy`; the
|
|
51
|
+
eager forms throw a `RangeError` past the largest array length. Sampling:
|
|
52
|
+
`weightedChoice`/`weightedChoiceIndex` (`undefined`/`-1` when no weight is
|
|
53
|
+
positive) and `sampleWithoutReplacement`, O(size) whatever the input length.
|
|
54
|
+
- 5c203e7: `rank(values, compare, options)` and `rankBy(items, valueOf, options)` return
|
|
55
|
+
the 1-based rank of each value at its own position, as a `Float64Array`. Ties
|
|
56
|
+
share ranks by `options.ties`: `"average"` (the default), `"min"`, `"max"`,
|
|
57
|
+
`"dense"` or `"ordinal"` (input order). `rankBy` ties `-0` with `0` and gives a
|
|
58
|
+
`NaN` key a `NaN` rank, ranking the other items among themselves.
|
|
59
|
+
|
|
60
|
+
### Patch Changes
|
|
61
|
+
|
|
62
|
+
- Updated dependencies [5c203e7]
|
|
63
|
+
- Updated dependencies [5c203e7]
|
|
64
|
+
- Updated dependencies [5c203e7]
|
|
65
|
+
- Updated dependencies [5c203e7]
|
|
66
|
+
- Updated dependencies [5c203e7]
|
|
67
|
+
- @ac-kit/core@0.14.0
|
|
68
|
+
- @ac-kit/data@0.6.0
|
|
69
|
+
|
|
70
|
+
## 0.6.0
|
|
71
|
+
|
|
72
|
+
### Minor Changes
|
|
73
|
+
|
|
74
|
+
- 0b4fe43: `bisectRightNumbers(items, value, low?, high?)` returns how many elements of a
|
|
75
|
+
sorted numeric array or typed array are `≤ value`, searching only the window
|
|
76
|
+
`[low, high)`. It is `bisectRight` for numbers: no comparator, nothing
|
|
77
|
+
allocated. A NaN `value` returns `low`. `bisectRightNumbersArray(out, items,
|
|
78
|
+
values)` does it for every value into an integer or float `out`, inside one
|
|
79
|
+
loop, so nothing is allocated wherever it is called from.
|
|
80
|
+
- 0b4fe43: Fractional indexing: string keys that sort strictly between two others under
|
|
81
|
+
plain string comparison, so a user-ordered list inserts or moves an item by
|
|
82
|
+
writing one key.
|
|
83
|
+
|
|
84
|
+
- `generateFractionalKeyBetween(lower, upper, options?)` returns the shortest
|
|
85
|
+
key strictly between `lower` and `upper` (`null` for an open end; `(null,
|
|
86
|
+
null)` gives `"V0"`). Appends and prepends step an integer part and grow
|
|
87
|
+
logarithmically; between two keys the result is at most one code unit longer
|
|
88
|
+
than the longer bound.
|
|
89
|
+
- `generateFractionalKeysBetween(lower, upper, count, options?)` returns
|
|
90
|
+
`count` ascending keys: consecutive appends or prepends at an open end,
|
|
91
|
+
otherwise the free integers first and then fractions dividing each gap
|
|
92
|
+
evenly, each at most `⌈log_base(count + 1)⌉` code units longer than the
|
|
93
|
+
longer bound. `count = 1` matches `generateFractionalKeyBetween`.
|
|
94
|
+
- `isFractionalKey(key, options?)` and `assertFractionalKey(key, options?)`
|
|
95
|
+
check a key's form; a malformed bound throws `FractionalKeyError` (a
|
|
96
|
+
`SyntaxError` with `key` and `index`), bounds out of order throw
|
|
97
|
+
`RangeError`.
|
|
98
|
+
- `FractionalKeyAlphabet(digits)` accepts 2 to 94 ASCII graphic digits in
|
|
99
|
+
strictly increasing order, so keys sort identically in JavaScript, in UTF-8
|
|
100
|
+
byte order and under a binary collation; `BASE62_FRACTIONAL_KEY_ALPHABET`
|
|
101
|
+
(`0-9A-Za-z`) is the default.
|
|
102
|
+
- 0b4fe43: Add `RadixIndexSorter`: `sort(out, keys, order?, count?)` writes the positions of a `Float32Array`, `Float64Array`, `Int32Array` or `Uint32Array` of keys — from any realm, the element type read from the array's internal slot — in ascending or descending order into `out`, by least-significant-digit radix passes (11-bit digits, a pass skipped when every key shares its digit). Other key arrays throw a `TypeError`. Stable in both orders; floating-point keys order as numbers, `-0` equal to `0` and every `NaN` last. The sorter keeps its scratch, so it allocates only when a sort is larger than every earlier one.
|
|
103
|
+
|
|
104
|
+
### Patch Changes
|
|
105
|
+
|
|
106
|
+
- 0b4fe43: `findCycle`'s `options.budget` is also charged 1 per root read (a repeated root
|
|
107
|
+
included), so an endless `roots` iterable can be capped. Open iterators are
|
|
108
|
+
closed with `return()` before it returns or throws, the successors first and the
|
|
109
|
+
roots last.
|
|
110
|
+
- Updated dependencies [0b4fe43]
|
|
111
|
+
- Updated dependencies [0b4fe43]
|
|
112
|
+
- Updated dependencies [0b4fe43]
|
|
113
|
+
- Updated dependencies [0b4fe43]
|
|
114
|
+
- Updated dependencies [0b4fe43]
|
|
115
|
+
- Updated dependencies [0b4fe43]
|
|
116
|
+
- Updated dependencies [0b4fe43]
|
|
117
|
+
- Updated dependencies [0b4fe43]
|
|
118
|
+
- Updated dependencies [0b4fe43]
|
|
119
|
+
- Updated dependencies [0b4fe43]
|
|
120
|
+
- Updated dependencies [0b4fe43]
|
|
121
|
+
- Updated dependencies [0b4fe43]
|
|
122
|
+
- Updated dependencies [0b4fe43]
|
|
123
|
+
- Updated dependencies [0b4fe43]
|
|
124
|
+
- Updated dependencies [0b4fe43]
|
|
125
|
+
- Updated dependencies [0b4fe43]
|
|
126
|
+
- Updated dependencies [0b4fe43]
|
|
127
|
+
- Updated dependencies [0b4fe43]
|
|
128
|
+
- @ac-kit/core@0.13.0
|
|
129
|
+
- @ac-kit/data@0.5.0
|
|
130
|
+
|
|
3
131
|
## 0.5.0
|
|
4
132
|
|
|
5
133
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -3,15 +3,16 @@
|
|
|
3
3
|
Classic algorithms that work on **your** types, not on numbers.
|
|
4
4
|
|
|
5
5
|
Nothing here assumes what an element is. Ordering, equality, scoring and
|
|
6
|
-
neighbour expansion are all supplied as callbacks, so the same
|
|
7
|
-
sorts your invoices by due date and your pixels by
|
|
8
|
-
search walks your dependency tree and your maze.
|
|
6
|
+
neighbour expansion are all supplied as callbacks, so the same
|
|
7
|
+
`quickselectInPlace` sorts your invoices by due date and your pixels by
|
|
8
|
+
luminance, and the same graph search walks your dependency tree and your maze.
|
|
9
9
|
|
|
10
10
|
```ts
|
|
11
|
-
import { groupBy,
|
|
11
|
+
import { groupBy, quickselectInPlace, uniqBy } from "@ac-kit/algo";
|
|
12
12
|
|
|
13
|
-
// Order statistics without paying for a full sort — O(n) average.
|
|
14
|
-
|
|
13
|
+
// Order statistics without paying for a full sort — O(n) average. It
|
|
14
|
+
// rearranges the array it is given, hence the copy.
|
|
15
|
+
const median = quickselectInPlace(
|
|
15
16
|
[...durations],
|
|
16
17
|
durations.length >> 1,
|
|
17
18
|
(a, b) => a - b,
|
|
@@ -35,6 +36,14 @@ const distinct = [...uniqBy(events, (event) => event.correlationId)];
|
|
|
35
36
|
(or nothing), for an error message that names it. `hierarchyFromParents` turns rows with a key and a
|
|
36
37
|
parent key (a chart's frame) into the `childrenOf` those take, as flat
|
|
37
38
|
`Uint32Array` offsets and indices (d3 `stratify`).
|
|
39
|
+
- **Graph structure** — `bfs`, the level-order walk with depth and parent (the
|
|
40
|
+
shortest path in edge count), `topologicalSort` (Kahn's, throwing a
|
|
41
|
+
`GraphCycleError` that names one cycle rather than returning a partial
|
|
42
|
+
order), `connectedComponents` (union–find, so an edge listed from one end
|
|
43
|
+
counts both ways), `lowestCommonAncestor` over a `parentOf` callback, and
|
|
44
|
+
`floydWarshall`, all-pairs shortest paths with negative edges and next hops.
|
|
45
|
+
All iterative, over callbacks, so `@ac-kit/data`'s `Graph` and
|
|
46
|
+
`DirectedGraph` drive them through `neighbors`/`outNeighbors`.
|
|
38
47
|
- **Recursion without a stack** — `runTrampoline` runs a recursive algorithm
|
|
39
48
|
written as native synchronous generators (`const value = yield child`) on
|
|
40
49
|
an explicit stack, so a hostile nesting depth costs heap, not a stack
|
|
@@ -45,19 +54,50 @@ const distinct = [...uniqBy(events, (event) => event.correlationId)];
|
|
|
45
54
|
`backtrack` for constraint search that undoes its own moves on the way back
|
|
46
55
|
out of a dead end.
|
|
47
56
|
- **Binary search** — `bisect`, `bisectLeft`, `bisectRight`, `bisectCenter` and
|
|
48
|
-
a reusable `createBisector` bound to one accessor, `
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
57
|
+
a reusable `createBisector` bound to one accessor, `bisectRightNumbers` for a
|
|
58
|
+
sorted numeric array or typed array with no comparator or allocation (and
|
|
59
|
+
`bisectRightNumbersArray` for a whole array of values),
|
|
60
|
+
`binarySearch` over any integer range driven by a predicate alone, plus
|
|
61
|
+
membership testing against sorted, disjoint intervals. `isSorted` checks the
|
|
62
|
+
precondition they all assume.
|
|
63
|
+
- **Fractional indexing** — `generateFractionalKeyBetween(lower, upper)`
|
|
64
|
+
returns a string key that sorts strictly between two others (`null` for an
|
|
65
|
+
open end), so a user-ordered list moves an item by writing one key;
|
|
66
|
+
`generateFractionalKeysBetween(lower, upper, count)` spreads a batch evenly.
|
|
67
|
+
Keys are base 62 in byte order by default (any ASCII
|
|
68
|
+
`FractionalKeyAlphabet`), sort with plain string comparison, grow
|
|
69
|
+
logarithmically on appends and prepends, and are checked by
|
|
70
|
+
`isFractionalKey`/`assertFractionalKey`.
|
|
71
|
+
- **Selection** — `quickselectInPlace` for order statistics, `minBy`/`maxBy` for the
|
|
72
|
+
extreme item by a derived key, `minmax`/`minmaxBy` for both extremes in one
|
|
73
|
+
pass, `uniqueMinBy` for the smallest item under a
|
|
53
74
|
comparator only when no other ties with it, `selectAt` for picking positions.
|
|
54
|
-
- **Sorting** — `
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
75
|
+
- **Sorting** — `mergeSort`, stable with an O(n log n) guarantee,
|
|
76
|
+
`quickSortInPlace`, three-way quicksort in O(log n) space, `countingSort`
|
|
77
|
+
over a small integer key range, `radixSort`, a stable, comparator-free sort over an integer key,
|
|
78
|
+
`sortedIndices`, the stable permutation a comparator sorts values into
|
|
79
|
+
(an argsort), to reorder parallel arrays or write results back in place,
|
|
80
|
+
`rank` and `rankBy`, the 1-based rank of each value with ties by average,
|
|
81
|
+
min, max, dense or ordinal ranking (`NaN` keys left unranked), and
|
|
82
|
+
`RadixIndexSorter`, the same argsort over a typed array of numbers by radix
|
|
83
|
+
passes, with no allocation once warm (the depth sort a renderer redoes each
|
|
84
|
+
frame).
|
|
85
|
+
- **Combinatorics** — `combinations`, every `k`-subset of your items,
|
|
86
|
+
`permutations` (Heap's), `powerSet` and `cartesianProduct`, each with a
|
|
87
|
+
`…Lazy` generator for spaces too large to hold.
|
|
88
|
+
- **Sequences** — `levenshteinDistance` with an optional bound that cuts the
|
|
89
|
+
work to a band, `diffSequences`, a shortest keep/delete/insert script by
|
|
90
|
+
Myers' algorithm in linear space, `longestCommonSubsequence` from it,
|
|
91
|
+
`longestIncreasingSubsequence` by patience sorting, and
|
|
92
|
+
`commonPrefixLength` — over strings, arrays or any array-like, with your
|
|
93
|
+
equality.
|
|
59
94
|
- **Grouping** — `groupBy` (nesting one `Map` level per accessor), `partition`,
|
|
60
|
-
`partitionIndices`, `uniq` and `uniqBy`; `
|
|
95
|
+
`partitionIndices`, `uniq` and `uniqBy`; the rest of SQL's `GROUP BY`:
|
|
96
|
+
`countBy`, `rollupBy` (an aggregate per group), `indexBy` (unique keys),
|
|
97
|
+
`flatGroupBy`/`flatRollupBy` (one row per key tuple) and `groupSort` (keys
|
|
98
|
+
by a value of their group); `groupAdjacentBy` for consecutive runs only
|
|
99
|
+
(`itertools.groupby`), and `runLengthEncode`/`runLengthDecode`;
|
|
100
|
+
`groupIndices(items, groupOf,
|
|
61
101
|
groupCount)` groups positions by an integer group number into two flat
|
|
62
102
|
`Uint32Array`s (offsets and indices, the CSR layout of an adjacency list or a
|
|
63
103
|
parent → children table) by counting sort. Group numbers already in an array
|
|
@@ -70,7 +110,9 @@ const distinct = [...uniqBy(events, (event) => event.correlationId)];
|
|
|
70
110
|
- **Shuffling** — `shuffle` (Fisher–Yates or Sattolo) with the random source
|
|
71
111
|
injectable, so results can be made reproducible, and `spreadEvenly` to
|
|
72
112
|
interleave two collections at evenly spaced positions; `choice` draws one
|
|
73
|
-
element from an injectable random source
|
|
113
|
+
element from an injectable random source, `weightedChoice` and
|
|
114
|
+
`weightedChoiceIndex` one in proportion to a weight, and
|
|
115
|
+
`sampleWithoutReplacement` several distinct ones in O(size). (`randomInteger` and
|
|
74
116
|
`randomBoolean` live in `@ac-kit/math-random`.)
|
|
75
117
|
|
|
76
118
|
## What's not included
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/** The tuple type of one element from each of `Sets`, in order. */
|
|
2
|
+
export type CartesianTuple<Sets extends readonly Iterable<unknown>[]> = {
|
|
3
|
+
-readonly [I in keyof Sets]: Sets[I] extends Iterable<infer TElement> ? TElement : never;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Every tuple taking one element from each set — the eager form of
|
|
7
|
+
* {@link cartesianProductLazy}, in the same order. Each tuple is a fresh
|
|
8
|
+
* array.
|
|
9
|
+
*
|
|
10
|
+
* Time complexity: O(p · k) for `k` sets and `p` the product of their sizes.
|
|
11
|
+
* Memory: the same, for the result.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* cartesianProduct([["S", "M"], [1, 2]]);
|
|
16
|
+
* // [["S", 1], ["S", 2], ["M", 1], ["M", 2]]
|
|
17
|
+
* ```;
|
|
18
|
+
*
|
|
19
|
+
* @param sets - The sets, as an array of iterables rather than a rest
|
|
20
|
+
* parameter; each is read once, up front.
|
|
21
|
+
* @returns The `p` tuples; one empty tuple for no sets, none when a set is
|
|
22
|
+
* empty.
|
|
23
|
+
* @throws {RangeError} When `p` exceeds the largest array length.
|
|
24
|
+
*/
|
|
25
|
+
export declare function cartesianProduct<const Sets extends readonly Iterable<unknown>[]>(sets: Sets): CartesianTuple<Sets>[];
|
|
26
|
+
/**
|
|
27
|
+
* Every tuple taking one element from each set, yielded one at a time — the
|
|
28
|
+
* odometer: the last set's element changes fastest, so the tuples come in
|
|
29
|
+
* lexicographic order of their positions (Python's `itertools.product`).
|
|
30
|
+
*
|
|
31
|
+
* Time complexity: O(k) per tuple for `k` sets. Memory: the sets' elements,
|
|
32
|
+
* read up front, since every set but the first is walked many times.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* for (const [size, colour] of cartesianProductLazy([sizes, colours])) {
|
|
37
|
+
* // every variant of a product
|
|
38
|
+
* }
|
|
39
|
+
* ```;
|
|
40
|
+
*
|
|
41
|
+
* @param sets - The sets, as an array of iterables; each is read once, up
|
|
42
|
+
* front.
|
|
43
|
+
* @returns The tuples; one empty tuple for no sets, none when a set is empty.
|
|
44
|
+
*/
|
|
45
|
+
export declare function cartesianProductLazy<const Sets extends readonly Iterable<unknown>[]>(sets: Sets): Generator<CartesianTuple<Sets>, void, undefined>;
|
|
46
|
+
//# sourceMappingURL=cartesian-product.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cartesian-product.d.ts","sourceRoot":"","sources":["../../src/combinatorics/cartesian-product.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,MAAM,MAAM,cAAc,CAAC,IAAI,SAAS,SAAS,QAAQ,CAAC,OAAO,CAAC,EAAE,IAAI;IACvE,CAAC,UAAU,CAAC,IAAI,MAAM,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,SAAS,QAAQ,CAAC,MAAM,QAAQ,CAAC,GAClE,QAAQ,GACR,KAAK;CACR,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,gBAAgB,CAC/B,KAAK,CAAC,IAAI,SAAS,SAAS,QAAQ,CAAC,OAAO,CAAC,EAAE,EAC9C,IAAI,EAAE,IAAI,GAAG,cAAc,CAAC,IAAI,CAAC,EAAE,CAYpC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,oBAAoB,CACnC,KAAK,CAAC,IAAI,SAAS,SAAS,QAAQ,CAAC,OAAO,CAAC,EAAE,EAC9C,IAAI,EAAE,IAAI,GAAG,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,SAAS,CAAC,CAM9D"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every tuple taking one element from each set — the eager form of
|
|
3
|
+
* {@link cartesianProductLazy}, in the same order. Each tuple is a fresh
|
|
4
|
+
* array.
|
|
5
|
+
*
|
|
6
|
+
* Time complexity: O(p · k) for `k` sets and `p` the product of their sizes.
|
|
7
|
+
* Memory: the same, for the result.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* cartesianProduct([["S", "M"], [1, 2]]);
|
|
12
|
+
* // [["S", 1], ["S", 2], ["M", 1], ["M", 2]]
|
|
13
|
+
* ```;
|
|
14
|
+
*
|
|
15
|
+
* @param sets - The sets, as an array of iterables rather than a rest
|
|
16
|
+
* parameter; each is read once, up front.
|
|
17
|
+
* @returns The `p` tuples; one empty tuple for no sets, none when a set is
|
|
18
|
+
* empty.
|
|
19
|
+
* @throws {RangeError} When `p` exceeds the largest array length.
|
|
20
|
+
*/
|
|
21
|
+
export function cartesianProduct(sets) {
|
|
22
|
+
const pools = sets.map((set) => [...set]);
|
|
23
|
+
let count = 1;
|
|
24
|
+
for (const pool of pools) {
|
|
25
|
+
count *= pool.length;
|
|
26
|
+
}
|
|
27
|
+
if (count > 4294967295) {
|
|
28
|
+
throw new RangeError(`cartesianProduct: ${count} tuples exceed the largest array length; use cartesianProductLazy`);
|
|
29
|
+
}
|
|
30
|
+
return [...product_(pools)];
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Every tuple taking one element from each set, yielded one at a time — the
|
|
34
|
+
* odometer: the last set's element changes fastest, so the tuples come in
|
|
35
|
+
* lexicographic order of their positions (Python's `itertools.product`).
|
|
36
|
+
*
|
|
37
|
+
* Time complexity: O(k) per tuple for `k` sets. Memory: the sets' elements,
|
|
38
|
+
* read up front, since every set but the first is walked many times.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* for (const [size, colour] of cartesianProductLazy([sizes, colours])) {
|
|
43
|
+
* // every variant of a product
|
|
44
|
+
* }
|
|
45
|
+
* ```;
|
|
46
|
+
*
|
|
47
|
+
* @param sets - The sets, as an array of iterables; each is read once, up
|
|
48
|
+
* front.
|
|
49
|
+
* @returns The tuples; one empty tuple for no sets, none when a set is empty.
|
|
50
|
+
*/
|
|
51
|
+
export function cartesianProductLazy(sets) {
|
|
52
|
+
return product_(sets.map((set) => [...set]));
|
|
53
|
+
}
|
|
54
|
+
function* product_(pools) {
|
|
55
|
+
const count = pools.length;
|
|
56
|
+
for (const pool of pools) {
|
|
57
|
+
if (pool.length === 0) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const positions = new Uint32Array(count);
|
|
62
|
+
for (;;) {
|
|
63
|
+
const tuple = Array.from({ length: count });
|
|
64
|
+
for (let set = 0; set < count; set++) {
|
|
65
|
+
tuple[set] = pools[set][positions[set]];
|
|
66
|
+
}
|
|
67
|
+
yield tuple;
|
|
68
|
+
let set = count - 1;
|
|
69
|
+
while (set >= 0 && positions[set] === pools[set].length - 1) {
|
|
70
|
+
positions[set] = 0;
|
|
71
|
+
set--;
|
|
72
|
+
}
|
|
73
|
+
if (set < 0) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
positions[set]++;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=cartesian-product.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cartesian-product.js","sourceRoot":"","sources":["../../src/combinatorics/cartesian-product.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,gBAAgB,CAE9B,IAAU;IACX,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC;IAC1C,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QAC1B,KAAK,IAAI,IAAI,CAAC,MAAM,CAAC;IACtB,CAAC;IACD,IAAI,KAAK,GAAG,UAAW,EAAE,CAAC;QACzB,MAAM,IAAI,UAAU,CACnB,qBAAqB,KAAK,mEAAmE,CAC7F,CAAC;IACH,CAAC;IACD,OAAO,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAA2B,CAAC;AACvD,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,oBAAoB,CAElC,IAAU;IACX,OAAO,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAI1C,CAAC;AACH,CAAC;AAED,QAAQ,CAAC,CAAC,QAAQ,CACjB,KAAsC;IAEtC,MAAM,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC;IAC3B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QAC1B,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACvB,OAAO;QACR,CAAC;IACF,CAAC;IACD,MAAM,SAAS,GAAG,IAAI,WAAW,CAAC,KAAK,CAAC,CAAC;IACzC,SAAS,CAAC;QACT,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAU,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;QACrD,KAAK,IAAI,GAAG,GAAG,CAAC,EAAE,GAAG,GAAG,KAAK,EAAE,GAAG,EAAE,EAAE,CAAC;YACtC,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,GAAG,CAAE,CAAC,SAAS,CAAC,GAAG,CAAE,CAAC,CAAC;QAC3C,CAAC;QACD,MAAM,KAAK,CAAC;QAEZ,IAAI,GAAG,GAAG,KAAK,GAAG,CAAC,CAAC;QACpB,OAAO,GAAG,IAAI,CAAC,IAAI,SAAS,CAAC,GAAG,CAAC,KAAK,KAAK,CAAC,GAAG,CAAE,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC9D,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YACnB,GAAG,EAAE,CAAC;QACP,CAAC;QACD,IAAI,GAAG,GAAG,CAAC,EAAE,CAAC;YACb,OAAO;QACR,CAAC;QACD,SAAS,CAAC,GAAG,CAAE,EAAE,CAAC;IACnB,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every ordering of `items` — the eager form of {@link permutationsLazy}, in the
|
|
3
|
+
* same order. Each permutation is a fresh array.
|
|
4
|
+
*
|
|
5
|
+
* Reach for it to try every order of a small set; there are `n!` of them, so
|
|
6
|
+
* past a handful of items use {@link permutationsLazy} and stop early.
|
|
7
|
+
*
|
|
8
|
+
* Time complexity: O(n! · n). Memory: the same, for the result.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* permutations([1, 2, 3]);
|
|
13
|
+
* // [[1, 2, 3], [2, 1, 3], [3, 1, 2], [1, 3, 2], [2, 3, 1], [3, 2, 1]]
|
|
14
|
+
* ```;
|
|
15
|
+
*
|
|
16
|
+
* @param items - The items to order; equal items are not told apart, so repeats
|
|
17
|
+
* yield repeated permutations.
|
|
18
|
+
* @returns All `n!` permutations; one empty permutation for no items.
|
|
19
|
+
* @throws {RangeError} When `n!` exceeds the largest array length (`n > 12`).
|
|
20
|
+
*/
|
|
21
|
+
export declare function permutations<T>(items: readonly T[]): T[][];
|
|
22
|
+
/**
|
|
23
|
+
* Every ordering of `items`, yielded one at a time — Heap's algorithm (1963),
|
|
24
|
+
* iterative: each permutation differs from the previous one by a single swap,
|
|
25
|
+
* so the next one costs O(1) amortised before its copy. Each yielded array is
|
|
26
|
+
* fresh and owned by the caller. The order is Heap's, not lexicographic.
|
|
27
|
+
*
|
|
28
|
+
* Time complexity: O(n) per permutation, for its copy. Memory: O(n).
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* for (const order of permutationsLazy(stops)) {
|
|
33
|
+
* if (isFeasible(order)) return order;
|
|
34
|
+
* }
|
|
35
|
+
* ```;
|
|
36
|
+
*
|
|
37
|
+
* @param items - The items to order; read once, up front.
|
|
38
|
+
* @returns The `n!` permutations; one empty permutation for no items.
|
|
39
|
+
*/
|
|
40
|
+
export declare function permutationsLazy<T>(items: readonly T[]): Generator<T[], void, undefined>;
|
|
41
|
+
//# sourceMappingURL=permutations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"permutations.d.ts","sourceRoot":"","sources":["../../src/combinatorics/permutations.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS,CAAC,EAAE,GAAG,CAAC,EAAE,EAAE,CAO1D;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAiB,gBAAgB,CAAC,CAAC,EAClC,KAAK,EAAE,SAAS,CAAC,EAAE,GACjB,SAAS,CAAC,CAAC,EAAE,EAAE,IAAI,EAAE,SAAS,CAAC,CAuBjC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every ordering of `items` — the eager form of {@link permutationsLazy}, in the
|
|
3
|
+
* same order. Each permutation is a fresh array.
|
|
4
|
+
*
|
|
5
|
+
* Reach for it to try every order of a small set; there are `n!` of them, so
|
|
6
|
+
* past a handful of items use {@link permutationsLazy} and stop early.
|
|
7
|
+
*
|
|
8
|
+
* Time complexity: O(n! · n). Memory: the same, for the result.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* permutations([1, 2, 3]);
|
|
13
|
+
* // [[1, 2, 3], [2, 1, 3], [3, 1, 2], [1, 3, 2], [2, 3, 1], [3, 2, 1]]
|
|
14
|
+
* ```;
|
|
15
|
+
*
|
|
16
|
+
* @param items - The items to order; equal items are not told apart, so repeats
|
|
17
|
+
* yield repeated permutations.
|
|
18
|
+
* @returns All `n!` permutations; one empty permutation for no items.
|
|
19
|
+
* @throws {RangeError} When `n!` exceeds the largest array length (`n > 12`).
|
|
20
|
+
*/
|
|
21
|
+
export function permutations(items) {
|
|
22
|
+
if (items.length > 12) {
|
|
23
|
+
throw new RangeError(`permutations: ${items.length}! permutations exceed the largest array length; use permutationsLazy`);
|
|
24
|
+
}
|
|
25
|
+
return [...permutationsLazy(items)];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Every ordering of `items`, yielded one at a time — Heap's algorithm (1963),
|
|
29
|
+
* iterative: each permutation differs from the previous one by a single swap,
|
|
30
|
+
* so the next one costs O(1) amortised before its copy. Each yielded array is
|
|
31
|
+
* fresh and owned by the caller. The order is Heap's, not lexicographic.
|
|
32
|
+
*
|
|
33
|
+
* Time complexity: O(n) per permutation, for its copy. Memory: O(n).
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* for (const order of permutationsLazy(stops)) {
|
|
38
|
+
* if (isFeasible(order)) return order;
|
|
39
|
+
* }
|
|
40
|
+
* ```;
|
|
41
|
+
*
|
|
42
|
+
* @param items - The items to order; read once, up front.
|
|
43
|
+
* @returns The `n!` permutations; one empty permutation for no items.
|
|
44
|
+
*/
|
|
45
|
+
export function* permutationsLazy(items) {
|
|
46
|
+
const current = items.slice();
|
|
47
|
+
const length = current.length;
|
|
48
|
+
// counters[i]: how many swaps level i has made — the explicit form of the
|
|
49
|
+
// recursion's loop index at each depth.
|
|
50
|
+
const counters = new Uint32Array(length);
|
|
51
|
+
yield current.slice();
|
|
52
|
+
let level = 1;
|
|
53
|
+
while (level < length) {
|
|
54
|
+
const counter = counters[level];
|
|
55
|
+
if (counter < level) {
|
|
56
|
+
const other = level % 2 === 0 ? 0 : counter;
|
|
57
|
+
const swapped = current[other];
|
|
58
|
+
current[other] = current[level];
|
|
59
|
+
current[level] = swapped;
|
|
60
|
+
yield current.slice();
|
|
61
|
+
counters[level] = counter + 1;
|
|
62
|
+
level = 1;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
counters[level] = 0;
|
|
66
|
+
level++;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=permutations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"permutations.js","sourceRoot":"","sources":["../../src/combinatorics/permutations.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,YAAY,CAAI,KAAmB;IAClD,IAAI,KAAK,CAAC,MAAM,GAAG,EAAE,EAAE,CAAC;QACvB,MAAM,IAAI,UAAU,CACnB,iBAAiB,KAAK,CAAC,MAAM,sEAAsE,CACnG,CAAC;IACH,CAAC;IACD,OAAO,CAAC,GAAG,gBAAgB,CAAC,KAAK,CAAC,CAAC,CAAC;AACrC,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,SAAS,CAAC,CAAC,gBAAgB,CAChC,KAAmB;IAEnB,MAAM,OAAO,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC;IAC9B,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAC9B,0EAA0E;IAC1E,wCAAwC;IACxC,MAAM,QAAQ,GAAG,IAAI,WAAW,CAAC,MAAM,CAAC,CAAC;IACzC,MAAM,OAAO,CAAC,KAAK,EAAE,CAAC;IACtB,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,OAAO,KAAK,GAAG,MAAM,EAAE,CAAC;QACvB,MAAM,OAAO,GAAG,QAAQ,CAAC,KAAK,CAAE,CAAC;QACjC,IAAI,OAAO,GAAG,KAAK,EAAE,CAAC;YACrB,MAAM,KAAK,GAAG,KAAK,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;YAC5C,MAAM,OAAO,GAAG,OAAO,CAAC,KAAK,CAAE,CAAC;YAChC,OAAO,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,KAAK,CAAE,CAAC;YACjC,OAAO,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC;YACzB,MAAM,OAAO,CAAC,KAAK,EAAE,CAAC;YACtB,QAAQ,CAAC,KAAK,CAAC,GAAG,OAAO,GAAG,CAAC,CAAC;YAC9B,KAAK,GAAG,CAAC,CAAC;QACX,CAAC;aAAM,CAAC;YACP,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YACpB,KAAK,EAAE,CAAC;QACT,CAAC;IACF,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every subset of `items` — the eager form of {@link powerSetLazy}, in the same
|
|
3
|
+
* order. Each subset is a fresh array.
|
|
4
|
+
*
|
|
5
|
+
* There are `2ⁿ` subsets, so past a handful of items use {@link powerSetLazy}
|
|
6
|
+
* and stop early.
|
|
7
|
+
*
|
|
8
|
+
* Time complexity: O(2ⁿ · n). Memory: the same, for the result.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* powerSet(["a", "b", "c"]);
|
|
13
|
+
* // [[], ["a"], ["b"], ["a", "b"], ["c"], ["a", "c"], ["b", "c"], ["a", "b", "c"]]
|
|
14
|
+
* ```;
|
|
15
|
+
*
|
|
16
|
+
* @param items - The items; equal items are not told apart.
|
|
17
|
+
* @returns All `2ⁿ` subsets, each in the order its items appear in `items`.
|
|
18
|
+
* @throws {RangeError} When `2ⁿ` exceeds the largest array length (`n > 31`).
|
|
19
|
+
*/
|
|
20
|
+
export declare function powerSet<T>(items: readonly T[]): T[][];
|
|
21
|
+
/**
|
|
22
|
+
* Every subset of `items`, yielded one at a time, by a binary counter: subset
|
|
23
|
+
* number `c` holds `items[i]` exactly when bit `i` of `c` is set, so the empty
|
|
24
|
+
* subset comes first, the full one last, and each subset's items keep their
|
|
25
|
+
* order in `items`. The counter is an array of bits rather than a number, so
|
|
26
|
+
* any length works for as long as the caller keeps pulling.
|
|
27
|
+
*
|
|
28
|
+
* Time complexity: O(n) per subset, for its copy; the counter's increment is
|
|
29
|
+
* O(1) amortised. Memory: O(n).
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* [...powerSetLazy([1, 2])]; // [[], [1], [2], [1, 2]]
|
|
34
|
+
* ```;
|
|
35
|
+
*
|
|
36
|
+
* @param items - The items; read once, up front.
|
|
37
|
+
* @returns The `2ⁿ` subsets.
|
|
38
|
+
*/
|
|
39
|
+
export declare function powerSetLazy<T>(items: readonly T[]): Generator<T[], void, undefined>;
|
|
40
|
+
//# sourceMappingURL=power-set.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"power-set.d.ts","sourceRoot":"","sources":["../../src/combinatorics/power-set.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS,CAAC,EAAE,GAAG,CAAC,EAAE,EAAE,CAOtD;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAiB,YAAY,CAAC,CAAC,EAC9B,KAAK,EAAE,SAAS,CAAC,EAAE,GACjB,SAAS,CAAC,CAAC,EAAE,EAAE,IAAI,EAAE,SAAS,CAAC,CAuBjC"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every subset of `items` — the eager form of {@link powerSetLazy}, in the same
|
|
3
|
+
* order. Each subset is a fresh array.
|
|
4
|
+
*
|
|
5
|
+
* There are `2ⁿ` subsets, so past a handful of items use {@link powerSetLazy}
|
|
6
|
+
* and stop early.
|
|
7
|
+
*
|
|
8
|
+
* Time complexity: O(2ⁿ · n). Memory: the same, for the result.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* powerSet(["a", "b", "c"]);
|
|
13
|
+
* // [[], ["a"], ["b"], ["a", "b"], ["c"], ["a", "c"], ["b", "c"], ["a", "b", "c"]]
|
|
14
|
+
* ```;
|
|
15
|
+
*
|
|
16
|
+
* @param items - The items; equal items are not told apart.
|
|
17
|
+
* @returns All `2ⁿ` subsets, each in the order its items appear in `items`.
|
|
18
|
+
* @throws {RangeError} When `2ⁿ` exceeds the largest array length (`n > 31`).
|
|
19
|
+
*/
|
|
20
|
+
export function powerSet(items) {
|
|
21
|
+
if (items.length > 31) {
|
|
22
|
+
throw new RangeError(`powerSet: 2^${items.length} subsets exceed the largest array length; use powerSetLazy`);
|
|
23
|
+
}
|
|
24
|
+
return [...powerSetLazy(items)];
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Every subset of `items`, yielded one at a time, by a binary counter: subset
|
|
28
|
+
* number `c` holds `items[i]` exactly when bit `i` of `c` is set, so the empty
|
|
29
|
+
* subset comes first, the full one last, and each subset's items keep their
|
|
30
|
+
* order in `items`. The counter is an array of bits rather than a number, so
|
|
31
|
+
* any length works for as long as the caller keeps pulling.
|
|
32
|
+
*
|
|
33
|
+
* Time complexity: O(n) per subset, for its copy; the counter's increment is
|
|
34
|
+
* O(1) amortised. Memory: O(n).
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* [...powerSetLazy([1, 2])]; // [[], [1], [2], [1, 2]]
|
|
39
|
+
* ```;
|
|
40
|
+
*
|
|
41
|
+
* @param items - The items; read once, up front.
|
|
42
|
+
* @returns The `2ⁿ` subsets.
|
|
43
|
+
*/
|
|
44
|
+
export function* powerSetLazy(items) {
|
|
45
|
+
const pool = items.slice();
|
|
46
|
+
const length = pool.length;
|
|
47
|
+
const bits = new Uint8Array(length);
|
|
48
|
+
for (;;) {
|
|
49
|
+
const subset = [];
|
|
50
|
+
for (let index = 0; index < length; index++) {
|
|
51
|
+
if (bits[index] === 1) {
|
|
52
|
+
subset.push(pool[index]);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
yield subset;
|
|
56
|
+
let index = 0;
|
|
57
|
+
while (index < length && bits[index] === 1) {
|
|
58
|
+
bits[index] = 0;
|
|
59
|
+
index++;
|
|
60
|
+
}
|
|
61
|
+
if (index === length) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
bits[index] = 1;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=power-set.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"power-set.js","sourceRoot":"","sources":["../../src/combinatorics/power-set.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,QAAQ,CAAI,KAAmB;IAC9C,IAAI,KAAK,CAAC,MAAM,GAAG,EAAE,EAAE,CAAC;QACvB,MAAM,IAAI,UAAU,CACnB,eAAe,KAAK,CAAC,MAAM,4DAA4D,CACvF,CAAC;IACH,CAAC;IACD,OAAO,CAAC,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC;AACjC,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,SAAS,CAAC,CAAC,YAAY,CAC5B,KAAmB;IAEnB,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC;IAC3B,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;IAC3B,MAAM,IAAI,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;IACpC,SAAS,CAAC;QACT,MAAM,MAAM,GAAQ,EAAE,CAAC;QACvB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;YAC7C,IAAI,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;gBACvB,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAE,CAAC,CAAC;YAC3B,CAAC;QACF,CAAC;QACD,MAAM,MAAM,CAAC;QAEb,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,OAAO,KAAK,GAAG,MAAM,IAAI,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;YAC5C,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YAChB,KAAK,EAAE,CAAC;QACT,CAAC;QACD,IAAI,KAAK,KAAK,MAAM,EAAE,CAAC;YACtB,OAAO;QACR,CAAC;QACD,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACjB,CAAC;AACF,CAAC"}
|