@ac-kit/algo 0.1.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 +7 -0
- package/LICENSE +28 -0
- package/README.md +70 -0
- package/dist/distribute/spread-evenly.d.ts +17 -0
- package/dist/distribute/spread-evenly.d.ts.map +1 -0
- package/dist/distribute/spread-evenly.js +41 -0
- package/dist/distribute/spread-evenly.js.map +1 -0
- package/dist/graph/_search-by-priority.d.ts +42 -0
- package/dist/graph/_search-by-priority.d.ts.map +1 -0
- package/dist/graph/_search-by-priority.js +56 -0
- package/dist/graph/_search-by-priority.js.map +1 -0
- package/dist/graph/a-star.d.ts +19 -0
- package/dist/graph/a-star.d.ts.map +1 -0
- package/dist/graph/a-star.js +20 -0
- package/dist/graph/a-star.js.map +1 -0
- package/dist/graph/best-first-search.d.ts +25 -0
- package/dist/graph/best-first-search.d.ts.map +1 -0
- package/dist/graph/best-first-search.js +26 -0
- package/dist/graph/best-first-search.js.map +1 -0
- package/dist/graph/dfs.d.ts +46 -0
- package/dist/graph/dfs.d.ts.map +1 -0
- package/dist/graph/dfs.js +49 -0
- package/dist/graph/dfs.js.map +1 -0
- package/dist/group/_partition-into.d.ts +3 -0
- package/dist/group/_partition-into.d.ts.map +1 -0
- package/dist/group/_partition-into.js +11 -0
- package/dist/group/_partition-into.js.map +1 -0
- package/dist/group/group-by.d.ts +33 -0
- package/dist/group/group-by.d.ts.map +1 -0
- package/dist/group/group-by.js +51 -0
- package/dist/group/group-by.js.map +1 -0
- package/dist/group/partition-indices.d.ts +11 -0
- package/dist/group/partition-indices.d.ts.map +1 -0
- package/dist/group/partition-indices.js +13 -0
- package/dist/group/partition-indices.js.map +1 -0
- package/dist/group/partition.d.ts +10 -0
- package/dist/group/partition.d.ts.map +1 -0
- package/dist/group/partition.js +12 -0
- package/dist/group/partition.js.map +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/interval/is-in-sorted-intervals.d.ts +17 -0
- package/dist/interval/is-in-sorted-intervals.d.ts.map +1 -0
- package/dist/interval/is-in-sorted-intervals.js +21 -0
- package/dist/interval/is-in-sorted-intervals.js.map +1 -0
- package/dist/optimize/backtrack.d.ts +38 -0
- package/dist/optimize/backtrack.d.ts.map +1 -0
- package/dist/optimize/backtrack.js +56 -0
- package/dist/optimize/backtrack.js.map +1 -0
- package/dist/optimize/beam-search.d.ts +34 -0
- package/dist/optimize/beam-search.d.ts.map +1 -0
- package/dist/optimize/beam-search.js +42 -0
- package/dist/optimize/beam-search.js.map +1 -0
- package/dist/ordered/_binary-search.d.ts +13 -0
- package/dist/ordered/_binary-search.d.ts.map +1 -0
- package/dist/ordered/_binary-search.js +24 -0
- package/dist/ordered/_binary-search.js.map +1 -0
- package/dist/ordered/bisect-center.d.ts +18 -0
- package/dist/ordered/bisect-center.d.ts.map +1 -0
- package/dist/ordered/bisect-center.js +29 -0
- package/dist/ordered/bisect-center.js.map +1 -0
- package/dist/ordered/bisect-left.d.ts +15 -0
- package/dist/ordered/bisect-left.d.ts.map +1 -0
- package/dist/ordered/bisect-left.js +18 -0
- package/dist/ordered/bisect-left.js.map +1 -0
- package/dist/ordered/bisect-right.d.ts +15 -0
- package/dist/ordered/bisect-right.d.ts.map +1 -0
- package/dist/ordered/bisect-right.js +18 -0
- package/dist/ordered/bisect-right.js.map +1 -0
- package/dist/ordered/bisect.d.ts +2 -0
- package/dist/ordered/bisect.d.ts.map +1 -0
- package/dist/ordered/bisect.js +2 -0
- package/dist/ordered/bisect.js.map +1 -0
- package/dist/ordered/create-bisector.d.ts +30 -0
- package/dist/ordered/create-bisector.d.ts.map +1 -0
- package/dist/ordered/create-bisector.js +32 -0
- package/dist/ordered/create-bisector.js.map +1 -0
- package/dist/sample/shuffle.d.ts +31 -0
- package/dist/sample/shuffle.d.ts.map +1 -0
- package/dist/sample/shuffle.js +33 -0
- package/dist/sample/shuffle.js.map +1 -0
- package/dist/select/_extreme-by.d.ts +11 -0
- package/dist/select/_extreme-by.d.ts.map +1 -0
- package/dist/select/_extreme-by.js +23 -0
- package/dist/select/_extreme-by.js.map +1 -0
- package/dist/select/max-by.d.ts +13 -0
- package/dist/select/max-by.d.ts.map +1 -0
- package/dist/select/max-by.js +15 -0
- package/dist/select/max-by.js.map +1 -0
- package/dist/select/min-by.d.ts +13 -0
- package/dist/select/min-by.d.ts.map +1 -0
- package/dist/select/min-by.js +15 -0
- package/dist/select/min-by.js.map +1 -0
- package/dist/select/quickselect.d.ts +15 -0
- package/dist/select/quickselect.d.ts.map +1 -0
- package/dist/select/quickselect.js +47 -0
- package/dist/select/quickselect.js.map +1 -0
- package/dist/select/select-at.d.ts +11 -0
- package/dist/select/select-at.d.ts.map +1 -0
- package/dist/select/select-at.js +13 -0
- package/dist/select/select-at.js.map +1 -0
- package/dist/set/difference.d.ts +13 -0
- package/dist/set/difference.d.ts.map +1 -0
- package/dist/set/difference.js +20 -0
- package/dist/set/difference.js.map +1 -0
- package/dist/set/intersection.d.ts +16 -0
- package/dist/set/intersection.d.ts.map +1 -0
- package/dist/set/intersection.js +43 -0
- package/dist/set/intersection.js.map +1 -0
- package/dist/set/is-disjoint-from.d.ts +13 -0
- package/dist/set/is-disjoint-from.d.ts.map +1 -0
- package/dist/set/is-disjoint-from.js +20 -0
- package/dist/set/is-disjoint-from.js.map +1 -0
- package/dist/set/is-subset-of.d.ts +13 -0
- package/dist/set/is-subset-of.d.ts.map +1 -0
- package/dist/set/is-subset-of.js +20 -0
- package/dist/set/is-subset-of.js.map +1 -0
- package/dist/set/is-superset-of.d.ts +14 -0
- package/dist/set/is-superset-of.d.ts.map +1 -0
- package/dist/set/is-superset-of.js +21 -0
- package/dist/set/is-superset-of.js.map +1 -0
- package/dist/set/symmetric-difference.d.ts +13 -0
- package/dist/set/symmetric-difference.d.ts.map +1 -0
- package/dist/set/symmetric-difference.js +20 -0
- package/dist/set/symmetric-difference.js.map +1 -0
- package/dist/set/union.d.ts +13 -0
- package/dist/set/union.d.ts.map +1 -0
- package/dist/set/union.js +28 -0
- package/dist/set/union.js.map +1 -0
- package/dist/set/uniq-by.d.ts +25 -0
- package/dist/set/uniq-by.d.ts.map +1 -0
- package/dist/set/uniq-by.js +48 -0
- package/dist/set/uniq-by.js.map +1 -0
- package/dist/set/uniq.d.ts +14 -0
- package/dist/set/uniq.d.ts.map +1 -0
- package/dist/set/uniq.js +34 -0
- package/dist/set/uniq.js.map +1 -0
- package/dist/sort/radix-sort.d.ts +14 -0
- package/dist/sort/radix-sort.d.ts.map +1 -0
- package/dist/sort/radix-sort.js +58 -0
- package/dist/sort/radix-sort.js.map +1 -0
- package/package.json +64 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"partition-indices.d.ts","sourceRoot":"","sources":["../../src/group/partition-indices.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAIzC;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EACjC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EACrB,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,GACvB,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,CAAC,CAEtB"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { partitionInto } from "./_partition-into.js";
|
|
2
|
+
/**
|
|
3
|
+
* {@link partition} reporting positions instead of elements — the single-pass
|
|
4
|
+
* equivalent of the `map`/`filter`/`map` chain for index extraction.
|
|
5
|
+
*
|
|
6
|
+
* @param iterable - The input iterable.
|
|
7
|
+
* @param predicate - Returns true for indices going into the first group.
|
|
8
|
+
* @returns A tuple `[matchingIndices, nonMatchingIndices]`.
|
|
9
|
+
*/
|
|
10
|
+
export function partitionIndices(iterable, predicate) {
|
|
11
|
+
return partitionInto(iterable, predicate, (_item, index) => index);
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=partition-indices.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"partition-indices.js","sourceRoot":"","sources":["../../src/group/partition-indices.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAErD;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAC/B,QAAqB,EACrB,SAAyB;IAEzB,OAAO,aAAa,CAAC,QAAQ,EAAE,SAAS,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC;AACpE,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Predicate } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Splits an iterable into two groups based on a predicate, in one pass.
|
|
4
|
+
*
|
|
5
|
+
* @param iterable - The input iterable.
|
|
6
|
+
* @param predicate - Returns true for items going into the first group.
|
|
7
|
+
* @returns A tuple `[matching, nonMatching]`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function partition<T>(iterable: Iterable<T>, predicate: Predicate<[T]>): [T[], T[]];
|
|
10
|
+
//# sourceMappingURL=partition.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"partition.d.ts","sourceRoot":"","sources":["../../src/group/partition.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAIzC;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAC1B,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EACrB,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,GACvB,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,CAEZ"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { partitionInto } from "./_partition-into.js";
|
|
2
|
+
/**
|
|
3
|
+
* Splits an iterable into two groups based on a predicate, in one pass.
|
|
4
|
+
*
|
|
5
|
+
* @param iterable - The input iterable.
|
|
6
|
+
* @param predicate - Returns true for items going into the first group.
|
|
7
|
+
* @returns A tuple `[matching, nonMatching]`.
|
|
8
|
+
*/
|
|
9
|
+
export function partition(iterable, predicate) {
|
|
10
|
+
return partitionInto(iterable, predicate, (item) => item);
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=partition.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"partition.js","sourceRoot":"","sources":["../../src/group/partition.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAErD;;;;;;GAMG;AACH,MAAM,UAAU,SAAS,CACxB,QAAqB,EACrB,SAAyB;IAEzB,OAAO,aAAa,CAAC,QAAQ,EAAE,SAAS,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC;AAC3D,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export * from "./distribute/spread-evenly.js";
|
|
2
|
+
export * from "./graph/a-star.js";
|
|
3
|
+
export * from "./graph/best-first-search.js";
|
|
4
|
+
export * from "./graph/dfs.js";
|
|
5
|
+
export * from "./group/group-by.js";
|
|
6
|
+
export * from "./group/partition-indices.js";
|
|
7
|
+
export * from "./group/partition.js";
|
|
8
|
+
export * from "./interval/is-in-sorted-intervals.js";
|
|
9
|
+
export * from "./optimize/backtrack.js";
|
|
10
|
+
export * from "./optimize/beam-search.js";
|
|
11
|
+
export * from "./ordered/bisect-center.js";
|
|
12
|
+
export * from "./ordered/bisect-left.js";
|
|
13
|
+
export * from "./ordered/bisect-right.js";
|
|
14
|
+
export * from "./ordered/bisect.js";
|
|
15
|
+
export * from "./ordered/create-bisector.js";
|
|
16
|
+
export * from "./sample/shuffle.js";
|
|
17
|
+
export * from "./select/max-by.js";
|
|
18
|
+
export * from "./select/min-by.js";
|
|
19
|
+
export * from "./select/quickselect.js";
|
|
20
|
+
export * from "./select/select-at.js";
|
|
21
|
+
export * from "./set/difference.js";
|
|
22
|
+
export * from "./set/intersection.js";
|
|
23
|
+
export * from "./set/is-disjoint-from.js";
|
|
24
|
+
export * from "./set/is-subset-of.js";
|
|
25
|
+
export * from "./set/is-superset-of.js";
|
|
26
|
+
export * from "./set/symmetric-difference.js";
|
|
27
|
+
export * from "./set/union.js";
|
|
28
|
+
export * from "./set/uniq-by.js";
|
|
29
|
+
export * from "./set/uniq.js";
|
|
30
|
+
export * from "./sort/radix-sort.js";
|
|
31
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,mBAAmB,CAAC;AAClC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qBAAqB,CAAC;AACpC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,sBAAsB,CAAC;AACrC,cAAc,sCAAsC,CAAC;AACrD,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export * from "./distribute/spread-evenly.js";
|
|
2
|
+
export * from "./graph/a-star.js";
|
|
3
|
+
export * from "./graph/best-first-search.js";
|
|
4
|
+
export * from "./graph/dfs.js";
|
|
5
|
+
export * from "./group/group-by.js";
|
|
6
|
+
export * from "./group/partition-indices.js";
|
|
7
|
+
export * from "./group/partition.js";
|
|
8
|
+
export * from "./interval/is-in-sorted-intervals.js";
|
|
9
|
+
export * from "./optimize/backtrack.js";
|
|
10
|
+
export * from "./optimize/beam-search.js";
|
|
11
|
+
export * from "./ordered/bisect-center.js";
|
|
12
|
+
export * from "./ordered/bisect-left.js";
|
|
13
|
+
export * from "./ordered/bisect-right.js";
|
|
14
|
+
export * from "./ordered/bisect.js";
|
|
15
|
+
export * from "./ordered/create-bisector.js";
|
|
16
|
+
export * from "./sample/shuffle.js";
|
|
17
|
+
export * from "./select/max-by.js";
|
|
18
|
+
export * from "./select/min-by.js";
|
|
19
|
+
export * from "./select/quickselect.js";
|
|
20
|
+
export * from "./select/select-at.js";
|
|
21
|
+
export * from "./set/difference.js";
|
|
22
|
+
export * from "./set/intersection.js";
|
|
23
|
+
export * from "./set/is-disjoint-from.js";
|
|
24
|
+
export * from "./set/is-subset-of.js";
|
|
25
|
+
export * from "./set/is-superset-of.js";
|
|
26
|
+
export * from "./set/symmetric-difference.js";
|
|
27
|
+
export * from "./set/union.js";
|
|
28
|
+
export * from "./set/uniq-by.js";
|
|
29
|
+
export * from "./set/uniq.js";
|
|
30
|
+
export * from "./sort/radix-sort.js";
|
|
31
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,mBAAmB,CAAC;AAClC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qBAAqB,CAAC;AACpC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,sBAAsB,CAAC;AACrC,cAAc,sCAAsC,CAAC;AACrD,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Comparator } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Tests whether `value` falls within any of the given sorted, disjoint ranges,
|
|
4
|
+
* each represented as a pair of inclusive bounds in a flat array.
|
|
5
|
+
*
|
|
6
|
+
* Finds the candidate range the same way `bisectRight` finds an insertion point
|
|
7
|
+
* (rightmost range whose start is at or before `value`), then checks `value`
|
|
8
|
+
* against that range's end.
|
|
9
|
+
*
|
|
10
|
+
* Complexity: O(log n) in the number of ranges (`ranges.length / 2`).
|
|
11
|
+
*
|
|
12
|
+
* @param ranges - Flat, sorted, non-overlapping `[start, end, ...]` pairs.
|
|
13
|
+
* @param value - The value to test for membership.
|
|
14
|
+
* @param compare - Total order between two `T` values.
|
|
15
|
+
*/
|
|
16
|
+
export declare function isInSortedIntervals<T>(ranges: readonly T[], value: T, compare: Comparator<T>): boolean;
|
|
17
|
+
//# sourceMappingURL=is-in-sorted-intervals.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-in-sorted-intervals.d.ts","sourceRoot":"","sources":["../../src/interval/is-in-sorted-intervals.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAI/C;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EACpC,MAAM,EAAE,SAAS,CAAC,EAAE,EACpB,KAAK,EAAE,CAAC,EACR,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,GACpB,OAAO,CAWT"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { binarySearch } from "../ordered/_binary-search.js";
|
|
2
|
+
/**
|
|
3
|
+
* Tests whether `value` falls within any of the given sorted, disjoint ranges,
|
|
4
|
+
* each represented as a pair of inclusive bounds in a flat array.
|
|
5
|
+
*
|
|
6
|
+
* Finds the candidate range the same way `bisectRight` finds an insertion point
|
|
7
|
+
* (rightmost range whose start is at or before `value`), then checks `value`
|
|
8
|
+
* against that range's end.
|
|
9
|
+
*
|
|
10
|
+
* Complexity: O(log n) in the number of ranges (`ranges.length / 2`).
|
|
11
|
+
*
|
|
12
|
+
* @param ranges - Flat, sorted, non-overlapping `[start, end, ...]` pairs.
|
|
13
|
+
* @param value - The value to test for membership.
|
|
14
|
+
* @param compare - Total order between two `T` values.
|
|
15
|
+
*/
|
|
16
|
+
export function isInSortedIntervals(ranges, value, compare) {
|
|
17
|
+
const rangeCount = ranges.length / 2;
|
|
18
|
+
const candidate = binarySearch(0, rangeCount, (index) => compare(ranges[index * 2], value) <= 0) - 1;
|
|
19
|
+
return candidate >= 0 && compare(value, ranges[candidate * 2 + 1]) <= 0;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=is-in-sorted-intervals.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-in-sorted-intervals.js","sourceRoot":"","sources":["../../src/interval/is-in-sorted-intervals.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAE5D;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,mBAAmB,CAClC,MAAoB,EACpB,KAAQ,EACR,OAAsB;IAEtB,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC;IAErC,MAAM,SAAS,GACd,YAAY,CACX,CAAC,EACD,UAAU,EACV,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,GAAG,CAAC,CAAE,EAAE,KAAK,CAAC,IAAI,CAAC,CAClD,GAAG,CAAC,CAAC;IAEP,OAAO,SAAS,IAAI,CAAC,IAAI,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,SAAS,GAAG,CAAC,GAAG,CAAC,CAAE,CAAC,IAAI,CAAC,CAAC;AAC1E,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { DefinedValue } from "@ac-kit/core";
|
|
2
|
+
/** Returned when the search gave up on a branch rather than exhausting it. */
|
|
3
|
+
export declare const BacktrackExhausted: unique symbol;
|
|
4
|
+
export type BacktrackResult = boolean | typeof BacktrackExhausted;
|
|
5
|
+
/** How a caller's state space is expanded, mutated and tested. */
|
|
6
|
+
export type BacktrackTraits<TState> = {
|
|
7
|
+
/** Expands a state into the candidates to try from it. */
|
|
8
|
+
candidatesOf: (state: TState | undefined) => readonly TState[];
|
|
9
|
+
/** Mutates the caller's world to reflect having chosen `state`. */
|
|
10
|
+
apply: (state: TState) => void;
|
|
11
|
+
/** Reverses {@link BacktrackTraits.apply} when a branch is abandoned. */
|
|
12
|
+
undo: (state: TState) => void;
|
|
13
|
+
/** Whether `state` is an acceptable solution. */
|
|
14
|
+
isGoal: (state: TState) => boolean;
|
|
15
|
+
/** Whether `state` is a dead end that should not be expanded. */
|
|
16
|
+
isExhausted?: (state: TState) => boolean;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Depth-first search over a mutable state space, restoring the caller's state
|
|
20
|
+
* on the way back out of every abandoned branch.
|
|
21
|
+
*
|
|
22
|
+
* Unlike {@link dfs}, which only reads a structure, this owns an apply/undo
|
|
23
|
+
* pair: the caller keeps one mutable world, `apply` advances it into a
|
|
24
|
+
* candidate and `undo` rewinds it, so no state is ever copied. That is what
|
|
25
|
+
* makes it the shape constraint problems (sudoku, N-queens, packing) need.
|
|
26
|
+
*
|
|
27
|
+
* Time complexity: O(b^d) in the branching factor and depth — this is an
|
|
28
|
+
* exhaustive search, bounded only by `isGoal` and `isExhausted`.
|
|
29
|
+
*
|
|
30
|
+
* @param initialState - The state to search from, or `undefined` to start from
|
|
31
|
+
* whatever `candidatesOf(undefined)` yields.
|
|
32
|
+
* @param traits - How to expand, mutate and test a state.
|
|
33
|
+
* @returns `true` if a goal state was found, `false` if the space was exhausted
|
|
34
|
+
* without one, or {@link BacktrackExhausted} if a branch reported itself
|
|
35
|
+
* exhausted first.
|
|
36
|
+
*/
|
|
37
|
+
export declare function backtrack<TState extends DefinedValue>(initialState: TState | undefined, traits: BacktrackTraits<TState>): BacktrackResult;
|
|
38
|
+
//# sourceMappingURL=backtrack.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"backtrack.d.ts","sourceRoot":"","sources":["../../src/optimize/backtrack.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAS5C,8EAA8E;AAC9E,eAAO,MAAM,kBAAkB,EAAE,OAAO,MAAqC,CAAC;AAE9E,MAAM,MAAM,eAAe,GAAG,OAAO,GAAG,OAAO,kBAAkB,CAAC;AAElE,kEAAkE;AAClE,MAAM,MAAM,eAAe,CAAC,MAAM,IAAI;IACrC,0DAA0D;IAC1D,YAAY,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,KAAK,SAAS,MAAM,EAAE,CAAC;IAE/D,mEAAmE;IACnE,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAE/B,yEAAyE;IACzE,IAAI,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAE9B,iDAAiD;IACjD,MAAM,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC;IAEnC,iEAAiE;IACjE,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC;CACzC,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,SAAS,CAAC,MAAM,SAAS,YAAY,EACpD,YAAY,EAAE,MAAM,GAAG,SAAS,EAChC,MAAM,EAAE,eAAe,CAAC,MAAM,CAAC,GAC7B,eAAe,CA0CjB"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { dfs, DfsWalkContinue, DfsWalkSkipChildren, DfsWalkStop, } from "../graph/dfs.js";
|
|
2
|
+
/** Returned when the search gave up on a branch rather than exhausting it. */
|
|
3
|
+
export const BacktrackExhausted = Symbol("BacktrackExhausted");
|
|
4
|
+
/**
|
|
5
|
+
* Depth-first search over a mutable state space, restoring the caller's state
|
|
6
|
+
* on the way back out of every abandoned branch.
|
|
7
|
+
*
|
|
8
|
+
* Unlike {@link dfs}, which only reads a structure, this owns an apply/undo
|
|
9
|
+
* pair: the caller keeps one mutable world, `apply` advances it into a
|
|
10
|
+
* candidate and `undo` rewinds it, so no state is ever copied. That is what
|
|
11
|
+
* makes it the shape constraint problems (sudoku, N-queens, packing) need.
|
|
12
|
+
*
|
|
13
|
+
* Time complexity: O(b^d) in the branching factor and depth — this is an
|
|
14
|
+
* exhaustive search, bounded only by `isGoal` and `isExhausted`.
|
|
15
|
+
*
|
|
16
|
+
* @param initialState - The state to search from, or `undefined` to start from
|
|
17
|
+
* whatever `candidatesOf(undefined)` yields.
|
|
18
|
+
* @param traits - How to expand, mutate and test a state.
|
|
19
|
+
* @returns `true` if a goal state was found, `false` if the space was exhausted
|
|
20
|
+
* without one, or {@link BacktrackExhausted} if a branch reported itself
|
|
21
|
+
* exhausted first.
|
|
22
|
+
*/
|
|
23
|
+
export function backtrack(initialState, traits) {
|
|
24
|
+
const { candidatesOf, apply, undo, isGoal, isExhausted } = traits;
|
|
25
|
+
let outcome = false;
|
|
26
|
+
// `dfs` walks `TState | undefined` so the root can be the empty state, which
|
|
27
|
+
// has no candidate of its own but does have successors.
|
|
28
|
+
const root = initialState;
|
|
29
|
+
dfs(root, {
|
|
30
|
+
childrenOf: (state) => candidatesOf(state),
|
|
31
|
+
onEnter: (state) => {
|
|
32
|
+
if (state === undefined) {
|
|
33
|
+
return DfsWalkContinue;
|
|
34
|
+
}
|
|
35
|
+
if (state !== root) {
|
|
36
|
+
apply(state);
|
|
37
|
+
}
|
|
38
|
+
if (isGoal(state)) {
|
|
39
|
+
outcome = true;
|
|
40
|
+
return DfsWalkStop;
|
|
41
|
+
}
|
|
42
|
+
if (isExhausted?.(state) === true) {
|
|
43
|
+
outcome = BacktrackExhausted;
|
|
44
|
+
return DfsWalkSkipChildren;
|
|
45
|
+
}
|
|
46
|
+
return DfsWalkContinue;
|
|
47
|
+
},
|
|
48
|
+
onExit: (state) => {
|
|
49
|
+
if (state !== undefined && state !== root && outcome !== true) {
|
|
50
|
+
undo(state);
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
return outcome;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=backtrack.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"backtrack.js","sourceRoot":"","sources":["../../src/optimize/backtrack.ts"],"names":[],"mappings":"AAEA,OAAO,EACN,GAAG,EACH,eAAe,EACf,mBAAmB,EACnB,WAAW,GACX,MAAM,iBAAiB,CAAC;AAEzB,8EAA8E;AAC9E,MAAM,CAAC,MAAM,kBAAkB,GAAkB,MAAM,CAAC,oBAAoB,CAAC,CAAC;AAsB9E;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,SAAS,CACxB,YAAgC,EAChC,MAA+B;IAE/B,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,MAAM,CAAC;IAElE,IAAI,OAAO,GAAoB,KAAK,CAAC;IAErC,6EAA6E;IAC7E,wDAAwD;IACxD,MAAM,IAAI,GAAG,YAAY,CAAC;IAE1B,GAAG,CAAqB,IAAI,EAAE;QAC7B,UAAU,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,YAAY,CAAC,KAAK,CAAC;QAE1C,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE;YAClB,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;gBACzB,OAAO,eAAe,CAAC;YACxB,CAAC;YAED,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;gBACpB,KAAK,CAAC,KAAK,CAAC,CAAC;YACd,CAAC;YAED,IAAI,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;gBACnB,OAAO,GAAG,IAAI,CAAC;gBACf,OAAO,WAAW,CAAC;YACpB,CAAC;YAED,IAAI,WAAW,EAAE,CAAC,KAAK,CAAC,KAAK,IAAI,EAAE,CAAC;gBACnC,OAAO,GAAG,kBAAkB,CAAC;gBAC7B,OAAO,mBAAmB,CAAC;YAC5B,CAAC;YAED,OAAO,eAAe,CAAC;QACxB,CAAC;QAED,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE;YACjB,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;gBAC/D,IAAI,CAAC,KAAK,CAAC,CAAC;YACb,CAAC;QACF,CAAC;KACD,CAAC,CAAC;IAEH,OAAO,OAAO,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { DefinedValue } from "@ac-kit/core";
|
|
2
|
+
/** How a caller's state space is expanded, scored and tested. */
|
|
3
|
+
export type BeamSearchTraits<TState> = {
|
|
4
|
+
/** Expands a state into its successors. Return `[]` for a dead end. */
|
|
5
|
+
nextStatesOf: (state: TState) => readonly TState[];
|
|
6
|
+
/** Scores a state. **Higher is better** — the beam keeps the highest. */
|
|
7
|
+
scoreOf: (state: TState) => number;
|
|
8
|
+
/** Whether a state is a complete, acceptable solution. */
|
|
9
|
+
isGoal: (state: TState) => boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Whether a state is a dead end to drop from the beam without expanding. Use
|
|
12
|
+
* `() => false` when `nextStatesOf` returning `[]` already covers it.
|
|
13
|
+
*/
|
|
14
|
+
isTerminal: (state: TState) => boolean;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Performs a beam search over a state space.
|
|
18
|
+
*
|
|
19
|
+
* Starting from `initialState`, each step expands every live state, then keeps
|
|
20
|
+
* only the top `beamWidth` candidates by score. The search stops on the first
|
|
21
|
+
* goal state in the beam, when no successors are produced, or at `maxDepth`.
|
|
22
|
+
*
|
|
23
|
+
* Time complexity: O(maxDepth · beamWidth · b log(beamWidth · b)) for a
|
|
24
|
+
* branching factor `b` — the sort at each step dominates.
|
|
25
|
+
*
|
|
26
|
+
* @param initialState - Seed state for the search.
|
|
27
|
+
* @param traits - How to expand, score and test a state.
|
|
28
|
+
* @param beamWidth - Maximum number of states kept after each expansion step.
|
|
29
|
+
* @param maxDepth - Maximum number of expansion steps before the search stops.
|
|
30
|
+
* @returns The highest-scoring surviving state, or `undefined` if no states
|
|
31
|
+
* survive.
|
|
32
|
+
*/
|
|
33
|
+
export declare function beamSearch<TState extends DefinedValue>(initialState: TState, traits: BeamSearchTraits<TState>, beamWidth: number, maxDepth: number): TState | undefined;
|
|
34
|
+
//# sourceMappingURL=beam-search.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"beam-search.d.ts","sourceRoot":"","sources":["../../src/optimize/beam-search.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,iEAAiE;AACjE,MAAM,MAAM,gBAAgB,CAAC,MAAM,IAAI;IACtC,uEAAuE;IACvE,YAAY,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,SAAS,MAAM,EAAE,CAAC;IAEnD,yEAAyE;IACzE,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC;IAEnC,0DAA0D;IAC1D,MAAM,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC;IAEnC;;;OAGG;IACH,UAAU,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC;CACvC,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,UAAU,CAAC,MAAM,SAAS,YAAY,EACrD,YAAY,EAAE,MAAM,EACpB,MAAM,EAAE,gBAAgB,CAAC,MAAM,CAAC,EAChC,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,GACd,MAAM,GAAG,SAAS,CA6BpB"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Performs a beam search over a state space.
|
|
3
|
+
*
|
|
4
|
+
* Starting from `initialState`, each step expands every live state, then keeps
|
|
5
|
+
* only the top `beamWidth` candidates by score. The search stops on the first
|
|
6
|
+
* goal state in the beam, when no successors are produced, or at `maxDepth`.
|
|
7
|
+
*
|
|
8
|
+
* Time complexity: O(maxDepth · beamWidth · b log(beamWidth · b)) for a
|
|
9
|
+
* branching factor `b` — the sort at each step dominates.
|
|
10
|
+
*
|
|
11
|
+
* @param initialState - Seed state for the search.
|
|
12
|
+
* @param traits - How to expand, score and test a state.
|
|
13
|
+
* @param beamWidth - Maximum number of states kept after each expansion step.
|
|
14
|
+
* @param maxDepth - Maximum number of expansion steps before the search stops.
|
|
15
|
+
* @returns The highest-scoring surviving state, or `undefined` if no states
|
|
16
|
+
* survive.
|
|
17
|
+
*/
|
|
18
|
+
export function beamSearch(initialState, traits, beamWidth, maxDepth) {
|
|
19
|
+
const { nextStatesOf, scoreOf, isGoal, isTerminal } = traits;
|
|
20
|
+
let states = [initialState];
|
|
21
|
+
for (let depth = 0; depth < maxDepth; depth++) {
|
|
22
|
+
if (states.some((state) => isGoal(state))) {
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
const nextStates = [];
|
|
26
|
+
for (const state of states) {
|
|
27
|
+
if (isTerminal(state)) {
|
|
28
|
+
// Dead end: drop from beam rather than carrying a stuck state forward.
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
nextStates.push(...nextStatesOf(state));
|
|
32
|
+
}
|
|
33
|
+
if (nextStates.length === 0) {
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
states = nextStates
|
|
37
|
+
.sort((stateA, stateB) => scoreOf(stateB) - scoreOf(stateA))
|
|
38
|
+
.slice(0, beamWidth);
|
|
39
|
+
}
|
|
40
|
+
return states.sort((stateA, stateB) => scoreOf(stateB) - scoreOf(stateA))[0];
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=beam-search.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"beam-search.js","sourceRoot":"","sources":["../../src/optimize/beam-search.ts"],"names":[],"mappings":"AAoBA;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,UAAU,CACzB,YAAoB,EACpB,MAAgC,EAChC,SAAiB,EACjB,QAAgB;IAEhB,MAAM,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,MAAM,CAAC;IAE7D,IAAI,MAAM,GAAG,CAAC,YAAY,CAAC,CAAC;IAE5B,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,QAAQ,EAAE,KAAK,EAAE,EAAE,CAAC;QAC/C,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;YAC3C,MAAM;QACP,CAAC;QAED,MAAM,UAAU,GAAa,EAAE,CAAC;QAChC,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC5B,IAAI,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;gBACvB,uEAAuE;gBACvE,SAAS;YACV,CAAC;YACD,UAAU,CAAC,IAAI,CAAC,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC;QACzC,CAAC;QAED,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC7B,MAAM;QACP,CAAC;QAED,MAAM,GAAG,UAAU;aACjB,IAAI,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;aAC3D,KAAK,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC;IACvB,CAAC;IAED,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC9E,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Half-open binary search over an abstract index range `[low, high)`,
|
|
3
|
+
* converging on the leftmost index where `goRight` is false — driven by a
|
|
4
|
+
* per-index predicate instead of a concrete array, so a caller can search any
|
|
5
|
+
* indexable structure (`bisectLeft`/`bisectRight`'s array, or
|
|
6
|
+
* `isInSortedIntervals`'s flat, stride-2 range array) without materializing one
|
|
7
|
+
* element per searched item.
|
|
8
|
+
*
|
|
9
|
+
* @param goRight - `true` if the answer lies strictly to the right of `index`.
|
|
10
|
+
* @returns The leftmost index in `[low, high]` where `goRight` is false.
|
|
11
|
+
*/
|
|
12
|
+
export declare function binarySearch(low: number, high: number, goRight: (index: number) => boolean): number;
|
|
13
|
+
//# sourceMappingURL=_binary-search.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_binary-search.d.ts","sourceRoot":"","sources":["../../src/ordered/_binary-search.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAC3B,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,GACjC,MAAM,CAYR"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Half-open binary search over an abstract index range `[low, high)`,
|
|
3
|
+
* converging on the leftmost index where `goRight` is false — driven by a
|
|
4
|
+
* per-index predicate instead of a concrete array, so a caller can search any
|
|
5
|
+
* indexable structure (`bisectLeft`/`bisectRight`'s array, or
|
|
6
|
+
* `isInSortedIntervals`'s flat, stride-2 range array) without materializing one
|
|
7
|
+
* element per searched item.
|
|
8
|
+
*
|
|
9
|
+
* @param goRight - `true` if the answer lies strictly to the right of `index`.
|
|
10
|
+
* @returns The leftmost index in `[low, high]` where `goRight` is false.
|
|
11
|
+
*/
|
|
12
|
+
export function binarySearch(low, high, goRight) {
|
|
13
|
+
while (low < high) {
|
|
14
|
+
const mid = (low + high) >>> 1;
|
|
15
|
+
if (goRight(mid)) {
|
|
16
|
+
low = mid + 1;
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
high = mid;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return low;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=_binary-search.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_binary-search.js","sourceRoot":"","sources":["../../src/ordered/_binary-search.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,MAAM,UAAU,YAAY,CAC3B,GAAW,EACX,IAAY,EACZ,OAAmC;IAEnC,OAAO,GAAG,GAAG,IAAI,EAAE,CAAC;QACnB,MAAM,GAAG,GAAG,CAAC,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC;QAE/B,IAAI,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;YAClB,GAAG,GAAG,GAAG,GAAG,CAAC,CAAC;QACf,CAAC;aAAM,CAAC;YACP,IAAI,GAAG,GAAG,CAAC;QACZ,CAAC;IACF,CAAC;IAED,OAAO,GAAG,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Callable } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Finds the index of the element in `items` whose key is closest to `value`.
|
|
4
|
+
*
|
|
5
|
+
* Takes a numeric key accessor rather than a comparator: "nearest" is a metric
|
|
6
|
+
* question, and a comparator only reports which side an element falls on, never
|
|
7
|
+
* how far away it is. Ties resolve to the right-hand neighbour.
|
|
8
|
+
*
|
|
9
|
+
* O(log n). `valueOf` must return finite, non-`NaN` keys — `NaN` has no
|
|
10
|
+
* magnitude, so a distance derived from one is meaningless.
|
|
11
|
+
*
|
|
12
|
+
* @param items Array sorted ascending by `valueOf`. Must be non-empty.
|
|
13
|
+
* @param value The key to find the closest element to.
|
|
14
|
+
* @param valueOf Extracts the numeric sort key from an item.
|
|
15
|
+
* @returns The index of the closest element.
|
|
16
|
+
*/
|
|
17
|
+
export declare function bisectCenter<T>(items: readonly T[], value: number, valueOf: Callable<[T], number>): number;
|
|
18
|
+
//# sourceMappingURL=bisect-center.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bisect-center.d.ts","sourceRoot":"","sources":["../../src/ordered/bisect-center.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAI7C;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAC7B,KAAK,EAAE,SAAS,CAAC,EAAE,EACnB,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,GAC5B,MAAM,CAkBR"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { binarySearch } from "./_binary-search.js";
|
|
2
|
+
/**
|
|
3
|
+
* Finds the index of the element in `items` whose key is closest to `value`.
|
|
4
|
+
*
|
|
5
|
+
* Takes a numeric key accessor rather than a comparator: "nearest" is a metric
|
|
6
|
+
* question, and a comparator only reports which side an element falls on, never
|
|
7
|
+
* how far away it is. Ties resolve to the right-hand neighbour.
|
|
8
|
+
*
|
|
9
|
+
* O(log n). `valueOf` must return finite, non-`NaN` keys — `NaN` has no
|
|
10
|
+
* magnitude, so a distance derived from one is meaningless.
|
|
11
|
+
*
|
|
12
|
+
* @param items Array sorted ascending by `valueOf`. Must be non-empty.
|
|
13
|
+
* @param value The key to find the closest element to.
|
|
14
|
+
* @param valueOf Extracts the numeric sort key from an item.
|
|
15
|
+
* @returns The index of the closest element.
|
|
16
|
+
*/
|
|
17
|
+
export function bisectCenter(items, value, valueOf) {
|
|
18
|
+
const index = binarySearch(0, items.length, (probe) => valueOf(items[probe]) < value);
|
|
19
|
+
if (index === 0) {
|
|
20
|
+
return 0;
|
|
21
|
+
}
|
|
22
|
+
if (index === items.length) {
|
|
23
|
+
return items.length - 1;
|
|
24
|
+
}
|
|
25
|
+
const toPrevious = value - valueOf(items[index - 1]);
|
|
26
|
+
const toNext = valueOf(items[index]) - value;
|
|
27
|
+
return toPrevious < toNext ? index - 1 : index;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=bisect-center.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bisect-center.js","sourceRoot":"","sources":["../../src/ordered/bisect-center.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,YAAY,CAC3B,KAAmB,EACnB,KAAa,EACb,OAA8B;IAE9B,MAAM,KAAK,GAAG,YAAY,CACzB,CAAC,EACD,KAAK,CAAC,MAAM,EACZ,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAE,CAAC,GAAG,KAAK,CACzC,CAAC;IAEF,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;QACjB,OAAO,CAAC,CAAC;IACV,CAAC;IACD,IAAI,KAAK,KAAK,KAAK,CAAC,MAAM,EAAE,CAAC;QAC5B,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;IACzB,CAAC;IAED,MAAM,UAAU,GAAG,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAE,CAAC,CAAC;IACtD,MAAM,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,KAAK,CAAE,CAAC,GAAG,KAAK,CAAC;IAE9C,OAAO,UAAU,GAAG,MAAM,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;AAChD,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finds the leftmost index in `items` where `value` could be inserted while
|
|
3
|
+
* keeping `items` sorted — before any existing element considered equivalent to
|
|
4
|
+
* it.
|
|
5
|
+
*
|
|
6
|
+
* O(log n) via binary search. `items` must already be sorted by `compare`.
|
|
7
|
+
*
|
|
8
|
+
* @param items Array sorted ascending by `compare`.
|
|
9
|
+
* @param value The value to find an insertion point for.
|
|
10
|
+
* @param compare Orders an item against `value`: negative if the item precedes
|
|
11
|
+
* it, positive if the item follows it, `0` if equivalent.
|
|
12
|
+
* @returns The leftmost valid insertion index, in `[0, items.length]`.
|
|
13
|
+
*/
|
|
14
|
+
export declare function bisectLeft<T, V = T>(items: readonly T[], value: V, compare: (item: T, value: V) => number): number;
|
|
15
|
+
//# sourceMappingURL=bisect-left.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bisect-left.d.ts","sourceRoot":"","sources":["../../src/ordered/bisect-left.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAClC,KAAK,EAAE,SAAS,CAAC,EAAE,EACnB,KAAK,EAAE,CAAC,EACR,OAAO,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,KAAK,MAAM,GACpC,MAAM,CAMR"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { binarySearch } from "./_binary-search.js";
|
|
2
|
+
/**
|
|
3
|
+
* Finds the leftmost index in `items` where `value` could be inserted while
|
|
4
|
+
* keeping `items` sorted — before any existing element considered equivalent to
|
|
5
|
+
* it.
|
|
6
|
+
*
|
|
7
|
+
* O(log n) via binary search. `items` must already be sorted by `compare`.
|
|
8
|
+
*
|
|
9
|
+
* @param items Array sorted ascending by `compare`.
|
|
10
|
+
* @param value The value to find an insertion point for.
|
|
11
|
+
* @param compare Orders an item against `value`: negative if the item precedes
|
|
12
|
+
* it, positive if the item follows it, `0` if equivalent.
|
|
13
|
+
* @returns The leftmost valid insertion index, in `[0, items.length]`.
|
|
14
|
+
*/
|
|
15
|
+
export function bisectLeft(items, value, compare) {
|
|
16
|
+
return binarySearch(0, items.length, (index) => compare(items[index], value) < 0);
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=bisect-left.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bisect-left.js","sourceRoot":"","sources":["../../src/ordered/bisect-left.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,UAAU,CACzB,KAAmB,EACnB,KAAQ,EACR,OAAsC;IAEtC,OAAO,YAAY,CAClB,CAAC,EACD,KAAK,CAAC,MAAM,EACZ,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAE,EAAE,KAAK,CAAC,GAAG,CAAC,CAC5C,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finds the rightmost index in `items` where `value` could be inserted while
|
|
3
|
+
* keeping `items` sorted — after any existing element considered equivalent to
|
|
4
|
+
* it.
|
|
5
|
+
*
|
|
6
|
+
* O(log n) via binary search. `items` must already be sorted by `compare`.
|
|
7
|
+
*
|
|
8
|
+
* @param items Array sorted ascending by `compare`.
|
|
9
|
+
* @param value The value to find an insertion point for.
|
|
10
|
+
* @param compare Orders an item against `value`: negative if the item precedes
|
|
11
|
+
* it, positive if the item follows it, `0` if equivalent.
|
|
12
|
+
* @returns The rightmost valid insertion index, in `[0, items.length]`.
|
|
13
|
+
*/
|
|
14
|
+
export declare function bisectRight<T, V = T>(items: readonly T[], value: V, compare: (item: T, value: V) => number): number;
|
|
15
|
+
//# sourceMappingURL=bisect-right.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bisect-right.d.ts","sourceRoot":"","sources":["../../src/ordered/bisect-right.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EACnC,KAAK,EAAE,SAAS,CAAC,EAAE,EACnB,KAAK,EAAE,CAAC,EACR,OAAO,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,KAAK,MAAM,GACpC,MAAM,CAMR"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { binarySearch } from "./_binary-search.js";
|
|
2
|
+
/**
|
|
3
|
+
* Finds the rightmost index in `items` where `value` could be inserted while
|
|
4
|
+
* keeping `items` sorted — after any existing element considered equivalent to
|
|
5
|
+
* it.
|
|
6
|
+
*
|
|
7
|
+
* O(log n) via binary search. `items` must already be sorted by `compare`.
|
|
8
|
+
*
|
|
9
|
+
* @param items Array sorted ascending by `compare`.
|
|
10
|
+
* @param value The value to find an insertion point for.
|
|
11
|
+
* @param compare Orders an item against `value`: negative if the item precedes
|
|
12
|
+
* it, positive if the item follows it, `0` if equivalent.
|
|
13
|
+
* @returns The rightmost valid insertion index, in `[0, items.length]`.
|
|
14
|
+
*/
|
|
15
|
+
export function bisectRight(items, value, compare) {
|
|
16
|
+
return binarySearch(0, items.length, (index) => compare(items[index], value) <= 0);
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=bisect-right.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bisect-right.js","sourceRoot":"","sources":["../../src/ordered/bisect-right.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,WAAW,CAC1B,KAAmB,EACnB,KAAQ,EACR,OAAsC;IAEtC,OAAO,YAAY,CAClB,CAAC,EACD,KAAK,CAAC,MAAM,EACZ,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAE,EAAE,KAAK,CAAC,IAAI,CAAC,CAC7C,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bisect.d.ts","sourceRoot":"","sources":["../../src/ordered/bisect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,IAAI,MAAM,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bisect.js","sourceRoot":"","sources":["../../src/ordered/bisect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,IAAI,MAAM,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type Callable } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* A `{left, right}` pair bound to one accessor, from {@link createBisector},
|
|
4
|
+
* gaining `center` when the accessor's key is numeric.
|
|
5
|
+
*/
|
|
6
|
+
export type Bisector<T, V> = {
|
|
7
|
+
left(items: readonly T[], value: V): number;
|
|
8
|
+
right(items: readonly T[], value: V): number;
|
|
9
|
+
} & (V extends number ? {
|
|
10
|
+
center(items: readonly T[], value: number): number;
|
|
11
|
+
} : object);
|
|
12
|
+
/**
|
|
13
|
+
* Builds a {@link Bisector} that locates `value` among `items` by comparing
|
|
14
|
+
* `valueOf(item)` instead of `item` itself, in ascending natural order
|
|
15
|
+
* (`<`/`>`) — so `items` can be sorted by any derived key, not just `T`'s own
|
|
16
|
+
* order.
|
|
17
|
+
*
|
|
18
|
+
* `valueOf` is called once per comparison during each search, not once per item
|
|
19
|
+
* up front — binary search stays O(log n), it never materialises a mapped
|
|
20
|
+
* array.
|
|
21
|
+
*
|
|
22
|
+
* `center` is exposed only for a numeric key, since "nearest" needs a distance
|
|
23
|
+
* and no distance can be derived from an arbitrary ordering.
|
|
24
|
+
*
|
|
25
|
+
* @param valueOf Extracts the sort key from an item.
|
|
26
|
+
* @returns Bound `left` and `right` bisect functions, plus `center` for a
|
|
27
|
+
* numeric key.
|
|
28
|
+
*/
|
|
29
|
+
export declare function createBisector<T, V>(valueOf: Callable<[T], V>): Bisector<T, V>;
|
|
30
|
+
//# sourceMappingURL=create-bisector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-bisector.d.ts","sourceRoot":"","sources":["../../src/ordered/create-bisector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAA2B,MAAM,cAAc,CAAC;AAMtE;;;GAGG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,EAAE,CAAC,IAAI;IAC5B,IAAI,CAAC,KAAK,EAAE,SAAS,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,GAAG,MAAM,CAAC;IAC5C,KAAK,CAAC,KAAK,EAAE,SAAS,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,GAAG,MAAM,CAAC;CAC7C,GAAG,CAAC,CAAC,SAAS,MAAM,GAClB;IAAE,MAAM,CAAC,KAAK,EAAE,SAAS,CAAC,EAAE,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,GACtD,MAAM,CAAC,CAAC;AAEX;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,CAAC,EAClC,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,GACvB,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAahB"}
|