@ac-kit/algo 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/CHANGELOG.md +82 -0
  2. package/README.md +18 -8
  3. package/dist/graph/children-of.d.ts +9 -0
  4. package/dist/graph/children-of.d.ts.map +1 -0
  5. package/dist/graph/children-of.js +2 -0
  6. package/dist/graph/children-of.js.map +1 -0
  7. package/dist/graph/find-cycle.d.ts +31 -0
  8. package/dist/graph/find-cycle.d.ts.map +1 -0
  9. package/dist/graph/find-cycle.js +84 -0
  10. package/dist/graph/find-cycle.js.map +1 -0
  11. package/dist/graph/hierarchy-from-parents.d.ts +41 -0
  12. package/dist/graph/hierarchy-from-parents.d.ts.map +1 -0
  13. package/dist/graph/hierarchy-from-parents.js +75 -0
  14. package/dist/graph/hierarchy-from-parents.js.map +1 -0
  15. package/dist/group/group-indices.d.ts +56 -0
  16. package/dist/group/group-indices.d.ts.map +1 -0
  17. package/dist/group/group-indices.js +112 -0
  18. package/dist/group/group-indices.js.map +1 -0
  19. package/dist/index.d.ts +6 -2
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +6 -2
  22. package/dist/index.js.map +1 -1
  23. package/dist/interval/is-in-sorted-intervals.js +2 -2
  24. package/dist/interval/is-in-sorted-intervals.js.map +1 -1
  25. package/dist/ordered/_binary-search.d.ts +6 -3
  26. package/dist/ordered/_binary-search.d.ts.map +1 -1
  27. package/dist/ordered/_binary-search.js +8 -5
  28. package/dist/ordered/_binary-search.js.map +1 -1
  29. package/dist/ordered/binary-search.d.ts +26 -0
  30. package/dist/ordered/binary-search.d.ts.map +1 -0
  31. package/dist/ordered/binary-search.js +34 -0
  32. package/dist/ordered/binary-search.js.map +1 -0
  33. package/dist/ordered/bisect-center.js +2 -2
  34. package/dist/ordered/bisect-center.js.map +1 -1
  35. package/dist/ordered/bisect-left.js +2 -2
  36. package/dist/ordered/bisect-left.js.map +1 -1
  37. package/dist/ordered/bisect-right.js +2 -2
  38. package/dist/ordered/bisect-right.js.map +1 -1
  39. package/dist/sample/_random-index.d.ts +4 -0
  40. package/dist/sample/_random-index.d.ts.map +1 -0
  41. package/dist/sample/_random-index.js +5 -0
  42. package/dist/sample/_random-index.js.map +1 -0
  43. package/dist/sample/choice.js +2 -2
  44. package/dist/sample/choice.js.map +1 -1
  45. package/dist/sample/shuffle.js +3 -3
  46. package/dist/sample/shuffle.js.map +1 -1
  47. package/dist/sort/sorted-indices.d.ts +18 -0
  48. package/dist/sort/sorted-indices.d.ts.map +1 -0
  49. package/dist/sort/sorted-indices.js +22 -0
  50. package/dist/sort/sorted-indices.js.map +1 -0
  51. package/package.json +5 -5
  52. package/dist/sample/random-boolean.d.ts +0 -23
  53. package/dist/sample/random-boolean.d.ts.map +0 -1
  54. package/dist/sample/random-boolean.js +0 -27
  55. package/dist/sample/random-boolean.js.map +0 -1
  56. package/dist/sample/random-integer.d.ts +0 -27
  57. package/dist/sample/random-integer.d.ts.map +0 -1
  58. package/dist/sample/random-integer.js +0 -40
  59. package/dist/sample/random-integer.js.map +0 -1
package/dist/index.js CHANGED
@@ -2,9 +2,13 @@ export * from "./combinatorics/combinations.js";
2
2
  export * from "./distribute/spread-evenly.js";
3
3
  export * from "./graph/a-star.js";
4
4
  export * from "./graph/best-first-search.js";
5
+ export * from "./graph/children-of.js";
5
6
  export * from "./graph/dfs.js";
7
+ export * from "./graph/find-cycle.js";
8
+ export * from "./graph/hierarchy-from-parents.js";
6
9
  export * from "./graph/walk-tree.js";
7
10
  export * from "./group/group-by.js";
11
+ export * from "./group/group-indices.js";
8
12
  export * from "./group/partition-indices.js";
9
13
  export * from "./group/partition.js";
10
14
  export * from "./interval/complement-intervals.js";
@@ -16,6 +20,7 @@ export * from "./interval/subtract-intervals.js";
16
20
  export * from "./optimize/backtrack.js";
17
21
  export * from "./optimize/beam-search.js";
18
22
  export * from "./optimize/viterbi.js";
23
+ export * from "./ordered/binary-search.js";
19
24
  export * from "./ordered/bisect-center.js";
20
25
  export * from "./ordered/bisect-left.js";
21
26
  export * from "./ordered/bisect-right.js";
@@ -23,8 +28,6 @@ export * from "./ordered/bisect.js";
23
28
  export * from "./ordered/create-bisector.js";
24
29
  export * from "./recursion/run-trampoline.js";
25
30
  export * from "./sample/choice.js";
26
- export * from "./sample/random-boolean.js";
27
- export * from "./sample/random-integer.js";
28
31
  export * from "./sample/shuffle.js";
29
32
  export * from "./select/max-by.js";
30
33
  export * from "./select/min-by.js";
@@ -43,4 +46,5 @@ export * from "./set/union.js";
43
46
  export * from "./set/uniq-by.js";
44
47
  export * from "./set/uniq.js";
45
48
  export * from "./sort/radix-sort.js";
49
+ export * from "./sort/sorted-indices.js";
46
50
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iCAAiC,CAAC;AAChD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,mBAAmB,CAAC;AAClC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,sBAAsB,CAAC;AACrC,cAAc,oCAAoC,CAAC;AACnD,cAAc,mCAAmC,CAAC;AAClD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,sCAAsC,CAAC;AACrD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,kCAAkC,CAAC;AACjD,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,oBAAoB,CAAC;AACnC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,sBAAsB,CAAC;AACrC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iCAAiC,CAAC;AAChD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,mBAAmB,CAAC;AAClC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,wBAAwB,CAAC;AACvC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,mCAAmC,CAAC;AAClD,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,0BAA0B,CAAC;AACzC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,sBAAsB,CAAC;AACrC,cAAc,oCAAoC,CAAC;AACnD,cAAc,mCAAmC,CAAC;AAClD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,sCAAsC,CAAC;AACrD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,kCAAkC,CAAC;AACjD,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,sBAAsB,CAAC;AACrC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAC;AACrC,cAAc,0BAA0B,CAAC"}
@@ -1,4 +1,4 @@
1
- import { binarySearch } from "../ordered/_binary-search.js";
1
+ import { binarySearch_ } from "../ordered/_binary-search.js";
2
2
  /**
3
3
  * Tests whether `value` falls within any of the given sorted, disjoint ranges,
4
4
  * each represented as a pair of inclusive bounds in a flat array.
@@ -15,7 +15,7 @@ import { binarySearch } from "../ordered/_binary-search.js";
15
15
  */
16
16
  export function isInSortedIntervals(ranges, value, compare) {
17
17
  const rangeCount = ranges.length / 2;
18
- const candidate = binarySearch(0, rangeCount, (index) => compare(ranges[index * 2], value) <= 0) - 1;
18
+ const candidate = binarySearch_(0, rangeCount, (index) => compare(ranges[index * 2], value) <= 0) - 1;
19
19
  return candidate >= 0 && compare(value, ranges[candidate * 2 + 1]) <= 0;
20
20
  }
21
21
  //# sourceMappingURL=is-in-sorted-intervals.js.map
@@ -1 +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"}
1
+ {"version":3,"file":"is-in-sorted-intervals.js","sourceRoot":"","sources":["../../src/interval/is-in-sorted-intervals.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAE7D;;;;;;;;;;;;;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,aAAa,CACZ,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"}
@@ -6,8 +6,11 @@
6
6
  * `isInSortedIntervals`'s flat, stride-2 range array) without materializing one
7
7
  * element per searched item.
8
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.
9
+ * Unchecked: the public, validating form is `binarySearch`.
10
+ *
11
+ * @param predicate - `true` if the answer lies strictly to the right of
12
+ * `index`.
13
+ * @returns The leftmost index in `[low, high]` where `predicate` is false.
11
14
  */
12
- export declare function binarySearch(low: number, high: number, goRight: (index: number) => boolean): number;
15
+ export declare function binarySearch_(low: number, high: number, predicate: (index: number) => boolean): number;
13
16
  //# sourceMappingURL=_binary-search.d.ts.map
@@ -1 +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"}
1
+ {"version":3,"file":"_binary-search.d.ts","sourceRoot":"","sources":["../../src/ordered/_binary-search.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,aAAa,CAC5B,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,GACnC,MAAM,CAYR"}
@@ -6,13 +6,16 @@
6
6
  * `isInSortedIntervals`'s flat, stride-2 range array) without materializing one
7
7
  * element per searched item.
8
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.
9
+ * Unchecked: the public, validating form is `binarySearch`.
10
+ *
11
+ * @param predicate - `true` if the answer lies strictly to the right of
12
+ * `index`.
13
+ * @returns The leftmost index in `[low, high]` where `predicate` is false.
11
14
  */
12
- export function binarySearch(low, high, goRight) {
15
+ export function binarySearch_(low, high, predicate) {
13
16
  while (low < high) {
14
- const mid = (low + high) >>> 1;
15
- if (goRight(mid)) {
17
+ const mid = low + Math.floor((high - low) / 2);
18
+ if (predicate(mid)) {
16
19
  low = mid + 1;
17
20
  }
18
21
  else {
@@ -1 +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"}
1
+ {"version":3,"file":"_binary-search.js","sourceRoot":"","sources":["../../src/ordered/_binary-search.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,aAAa,CAC5B,GAAW,EACX,IAAY,EACZ,SAAqC;IAErC,OAAO,GAAG,GAAG,IAAI,EAAE,CAAC;QACnB,MAAM,GAAG,GAAG,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;QAE/C,IAAI,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC;YACpB,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,26 @@
1
+ import type { Predicate } from "@ac-kit/core";
2
+ /**
3
+ * The first integer in `[low, high)` at which `predicate` is `false`, or `high`
4
+ * when it is `true` for all of them: the partition point of a predicate that is
5
+ * `true` up to some integer and `false` from it on — `std::partition_point` in
6
+ * C++, `partition_point` in Rust. It searches an answer rather than an array:
7
+ * the smallest capacity that fits, the first millisecond at which a condition
8
+ * holds.
9
+ *
10
+ * Monotonicity is a precondition, not checked. For any predicate the result `i`
11
+ * is still a boundary: `i === low` or `predicate(i - 1)` was `true`, and `i ===
12
+ * high` or `predicate(i)` was `false`.
13
+ *
14
+ * `predicate` is called only with integers in `[low, high)`, never when `low
15
+ * === high`, and at most ⌈log₂(high − low + 1)⌉ times. What it throws
16
+ * propagates.
17
+ *
18
+ * @param low The first candidate, a safe integer.
19
+ * @param high One past the last candidate, a safe integer `>= low`.
20
+ * @param predicate `true` while the answer lies further right.
21
+ * @returns An integer in `[low, high]`.
22
+ * @throws {RangeError} When `low` or `high` is not a safe integer, or `low >
23
+ * high`.
24
+ */
25
+ export declare function binarySearch(low: number, high: number, predicate: Predicate<[candidate: number]>): number;
26
+ //# 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,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAI9C;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,YAAY,CAC3B,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,SAAS,CAAC,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,GACvC,MAAM,CASR"}
@@ -0,0 +1,34 @@
1
+ import { binarySearch_ } from "./_binary-search.js";
2
+ /**
3
+ * The first integer in `[low, high)` at which `predicate` is `false`, or `high`
4
+ * when it is `true` for all of them: the partition point of a predicate that is
5
+ * `true` up to some integer and `false` from it on — `std::partition_point` in
6
+ * C++, `partition_point` in Rust. It searches an answer rather than an array:
7
+ * the smallest capacity that fits, the first millisecond at which a condition
8
+ * holds.
9
+ *
10
+ * Monotonicity is a precondition, not checked. For any predicate the result `i`
11
+ * is still a boundary: `i === low` or `predicate(i - 1)` was `true`, and `i ===
12
+ * high` or `predicate(i)` was `false`.
13
+ *
14
+ * `predicate` is called only with integers in `[low, high)`, never when `low
15
+ * === high`, and at most ⌈log₂(high − low + 1)⌉ times. What it throws
16
+ * propagates.
17
+ *
18
+ * @param low The first candidate, a safe integer.
19
+ * @param high One past the last candidate, a safe integer `>= low`.
20
+ * @param predicate `true` while the answer lies further right.
21
+ * @returns An integer in `[low, high]`.
22
+ * @throws {RangeError} When `low` or `high` is not a safe integer, or `low >
23
+ * high`.
24
+ */
25
+ export function binarySearch(low, high, predicate) {
26
+ if (!Number.isSafeInteger(low) || !Number.isSafeInteger(high)) {
27
+ throw new RangeError("low and high must be safe integers.");
28
+ }
29
+ if (low > high) {
30
+ throw new RangeError("low must not be greater than high.");
31
+ }
32
+ return binarySearch_(low, high, predicate);
33
+ }
34
+ //# sourceMappingURL=binary-search.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"binary-search.js","sourceRoot":"","sources":["../../src/ordered/binary-search.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,YAAY,CAC3B,GAAW,EACX,IAAY,EACZ,SAAyC;IAEzC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,EAAE,CAAC;QAC/D,MAAM,IAAI,UAAU,CAAC,qCAAqC,CAAC,CAAC;IAC7D,CAAC;IACD,IAAI,GAAG,GAAG,IAAI,EAAE,CAAC;QAChB,MAAM,IAAI,UAAU,CAAC,oCAAoC,CAAC,CAAC;IAC5D,CAAC;IAED,OAAO,aAAa,CAAC,GAAG,EAAE,IAAI,EAAE,SAAS,CAAC,CAAC;AAC5C,CAAC"}
@@ -1,4 +1,4 @@
1
- import { binarySearch } from "./_binary-search.js";
1
+ import { binarySearch_ } from "./_binary-search.js";
2
2
  /**
3
3
  * Finds the index of the element in `items` whose key is closest to `value`.
4
4
  *
@@ -15,7 +15,7 @@ import { binarySearch } from "./_binary-search.js";
15
15
  * @returns The index of the closest element.
16
16
  */
17
17
  export function bisectCenter(items, value, valueOf) {
18
- const index = binarySearch(0, items.length, (probe) => valueOf(items[probe]) < value);
18
+ const index = binarySearch_(0, items.length, (probe) => valueOf(items[probe]) < value);
19
19
  if (index === 0) {
20
20
  return 0;
21
21
  }
@@ -1 +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"}
1
+ {"version":3,"file":"bisect-center.js","sourceRoot":"","sources":["../../src/ordered/bisect-center.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,YAAY,CAC3B,KAAmB,EACnB,KAAa,EACb,OAA8B;IAE9B,MAAM,KAAK,GAAG,aAAa,CAC1B,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"}
@@ -1,4 +1,4 @@
1
- import { binarySearch } from "./_binary-search.js";
1
+ import { binarySearch_ } from "./_binary-search.js";
2
2
  /**
3
3
  * Finds the leftmost index in `items` where `value` could be inserted while
4
4
  * keeping `items` sorted — before any existing element considered equivalent to
@@ -13,6 +13,6 @@ import { binarySearch } from "./_binary-search.js";
13
13
  * @returns The leftmost valid insertion index, in `[0, items.length]`.
14
14
  */
15
15
  export function bisectLeft(items, value, compare) {
16
- return binarySearch(0, items.length, (index) => compare(items[index], value) < 0);
16
+ return binarySearch_(0, items.length, (index) => compare(items[index], value) < 0);
17
17
  }
18
18
  //# sourceMappingURL=bisect-left.js.map
@@ -1 +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"}
1
+ {"version":3,"file":"bisect-left.js","sourceRoot":"","sources":["../../src/ordered/bisect-left.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,UAAU,CACzB,KAAmB,EACnB,KAAQ,EACR,OAAsC;IAEtC,OAAO,aAAa,CACnB,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"}
@@ -1,4 +1,4 @@
1
- import { binarySearch } from "./_binary-search.js";
1
+ import { binarySearch_ } from "./_binary-search.js";
2
2
  /**
3
3
  * Finds the rightmost index in `items` where `value` could be inserted while
4
4
  * keeping `items` sorted — after any existing element considered equivalent to
@@ -13,6 +13,6 @@ import { binarySearch } from "./_binary-search.js";
13
13
  * @returns The rightmost valid insertion index, in `[0, items.length]`.
14
14
  */
15
15
  export function bisectRight(items, value, compare) {
16
- return binarySearch(0, items.length, (index) => compare(items[index], value) <= 0);
16
+ return binarySearch_(0, items.length, (index) => compare(items[index], value) <= 0);
17
17
  }
18
18
  //# sourceMappingURL=bisect-right.js.map
@@ -1 +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"}
1
+ {"version":3,"file":"bisect-right.js","sourceRoot":"","sources":["../../src/ordered/bisect-right.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,WAAW,CAC1B,KAAmB,EACnB,KAAQ,EACR,OAAsC;IAEtC,OAAO,aAAa,CACnB,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,4 @@
1
+ import type { RandomFn } from "@ac-kit/core";
2
+ /** Draws an index uniformly from `[0, length)`; `length` is at least 1. */
3
+ export declare function randomIndex(length: number, random: RandomFn): number;
4
+ //# sourceMappingURL=_random-index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_random-index.d.ts","sourceRoot":"","sources":["../../src/sample/_random-index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C,2EAA2E;AAC3E,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,GAAG,MAAM,CAEpE"}
@@ -0,0 +1,5 @@
1
+ /** Draws an index uniformly from `[0, length)`; `length` is at least 1. */
2
+ export function randomIndex(length, random) {
3
+ return Math.min(Math.floor(random() * length), length - 1);
4
+ }
5
+ //# sourceMappingURL=_random-index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_random-index.js","sourceRoot":"","sources":["../../src/sample/_random-index.ts"],"names":[],"mappings":"AAEA,2EAA2E;AAC3E,MAAM,UAAU,WAAW,CAAC,MAAc,EAAE,MAAgB;IAC3D,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,GAAG,MAAM,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,CAAC;AAC5D,CAAC"}
@@ -1,4 +1,4 @@
1
- import { randomInteger } from "./random-integer.js";
1
+ import { randomIndex } from "./_random-index.js";
2
2
  /**
3
3
  * Picks one element of `items` uniformly.
4
4
  *
@@ -20,6 +20,6 @@ export function choice(items, random) {
20
20
  if (items.length === 0) {
21
21
  return undefined;
22
22
  }
23
- return items[randomInteger(0, items.length, random)];
23
+ return items[randomIndex(items.length, random)];
24
24
  }
25
25
  //# sourceMappingURL=choice.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"choice.js","sourceRoot":"","sources":["../../src/sample/choice.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,MAAM,CACrB,KAAmB,EACnB,MAAgB;IAEhB,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACxB,OAAO,SAAS,CAAC;IAClB,CAAC;IACD,OAAO,KAAK,CAAC,aAAa,CAAC,CAAC,EAAE,KAAK,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AACtD,CAAC"}
1
+ {"version":3,"file":"choice.js","sourceRoot":"","sources":["../../src/sample/choice.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEjD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,MAAM,CACrB,KAAmB,EACnB,MAAgB;IAEhB,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACxB,OAAO,SAAS,CAAC;IAClB,CAAC;IACD,OAAO,KAAK,CAAC,WAAW,CAAC,KAAK,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AACjD,CAAC"}
@@ -1,4 +1,4 @@
1
- import { randomInteger } from "./random-integer.js";
1
+ import { randomIndex } from "./_random-index.js";
2
2
  /**
3
3
  * Shuffles `items` using the Fisher-Yates algorithm and returns a new shuffled
4
4
  * array.
@@ -16,10 +16,10 @@ export function shuffle(items, options) {
16
16
  const algorithm = options?.algorithm ?? "fisherYates";
17
17
  const randomize = (i) => {
18
18
  if (algorithm === "sattolo") {
19
- return randomInteger(0, i, rand);
19
+ return randomIndex(i, rand);
20
20
  }
21
21
  else {
22
- return randomInteger(0, i + 1, rand);
22
+ return randomIndex(i + 1, rand);
23
23
  }
24
24
  };
25
25
  const out = items.slice();
@@ -1 +1 @@
1
- {"version":3,"file":"shuffle.js","sourceRoot":"","sources":["../../src/sample/shuffle.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAqBpD;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,OAAO,CAAI,KAAmB,EAAE,OAAwB;IACvE,MAAM,IAAI,GAAG,OAAO,EAAE,IAAI,IAAI,IAAI,CAAC,MAAM,CAAC;IAC1C,MAAM,SAAS,GAAG,OAAO,EAAE,SAAS,IAAI,aAAa,CAAC;IAEtD,MAAM,SAAS,GAAG,CAAC,CAAS,EAAU,EAAE;QACvC,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;YAC7B,OAAO,aAAa,CAAC,CAAC,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC;QAClC,CAAC;aAAM,CAAC;YACP,OAAO,aAAa,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC;QACtC,CAAC;IACF,CAAC,CAAC;IAEF,MAAM,GAAG,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC;IAE1B,KAAK,IAAI,CAAC,GAAG,GAAG,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QACzC,MAAM,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC;QAEvB,MAAM,GAAG,GAAG,GAAG,CAAC,CAAC,CAAE,CAAC;QACpB,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAE,CAAC;QACjB,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC;IACd,CAAC;IAED,OAAO,GAAG,CAAC;AACZ,CAAC"}
1
+ {"version":3,"file":"shuffle.js","sourceRoot":"","sources":["../../src/sample/shuffle.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAqBjD;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,OAAO,CAAI,KAAmB,EAAE,OAAwB;IACvE,MAAM,IAAI,GAAG,OAAO,EAAE,IAAI,IAAI,IAAI,CAAC,MAAM,CAAC;IAC1C,MAAM,SAAS,GAAG,OAAO,EAAE,SAAS,IAAI,aAAa,CAAC;IAEtD,MAAM,SAAS,GAAG,CAAC,CAAS,EAAU,EAAE;QACvC,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;YAC7B,OAAO,WAAW,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;QAC7B,CAAC;aAAM,CAAC;YACP,OAAO,WAAW,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC;QACjC,CAAC;IACF,CAAC,CAAC;IAEF,MAAM,GAAG,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC;IAE1B,KAAK,IAAI,CAAC,GAAG,GAAG,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QACzC,MAAM,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC;QAEvB,MAAM,GAAG,GAAG,GAAG,CAAC,CAAC,CAAE,CAAC;QACpB,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAE,CAAC;QACjB,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC;IACd,CAAC;IAED,OAAO,GAAG,CAAC;AACZ,CAAC"}
@@ -0,0 +1,18 @@
1
+ import type { Comparator } from "@ac-kit/core";
2
+ /**
3
+ * The positions of `values` in the order `compare` sorts them — an argsort:
4
+ * `values[result[0]]` is the first value, `values[result[1]]` the second, and
5
+ * so on. Stable: positions of equivalent values keep their input order.
6
+ *
7
+ * Reach for it to sort one array and carry the permutation over to others, or
8
+ * to write a result back at each value's original position (ranks, adjusted
9
+ * p-values) without sorting `[value, index]` pairs.
10
+ *
11
+ * O(n log n) comparisons; one array of `n` positions.
12
+ *
13
+ * @param values The values to order. Not modified.
14
+ * @param compare A strict weak ordering of the values.
15
+ * @returns A new array of the positions `0 … n − 1`, in sorted order.
16
+ */
17
+ export declare function sortedIndices<T>(values: ArrayLike<T>, compare: Comparator<T>): number[];
18
+ //# sourceMappingURL=sorted-indices.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sorted-indices.d.ts","sourceRoot":"","sources":["../../src/sort/sorted-indices.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAC9B,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,EACpB,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,GACpB,MAAM,EAAE,CAMV"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * The positions of `values` in the order `compare` sorts them — an argsort:
3
+ * `values[result[0]]` is the first value, `values[result[1]]` the second, and
4
+ * so on. Stable: positions of equivalent values keep their input order.
5
+ *
6
+ * Reach for it to sort one array and carry the permutation over to others, or
7
+ * to write a result back at each value's original position (ranks, adjusted
8
+ * p-values) without sorting `[value, index]` pairs.
9
+ *
10
+ * O(n log n) comparisons; one array of `n` positions.
11
+ *
12
+ * @param values The values to order. Not modified.
13
+ * @param compare A strict weak ordering of the values.
14
+ * @returns A new array of the positions `0 … n − 1`, in sorted order.
15
+ */
16
+ export function sortedIndices(values, compare) {
17
+ const indices = Array.from({ length: values.length }, (_, index) => index);
18
+ // Array.prototype.sort is stable (ES2019), and the input positions start in
19
+ // ascending order, so equivalent values keep their order.
20
+ return indices.sort((left, right) => compare(values[left], values[right]));
21
+ }
22
+ //# sourceMappingURL=sorted-indices.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sorted-indices.js","sourceRoot":"","sources":["../../src/sort/sorted-indices.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,aAAa,CAC5B,MAAoB,EACpB,OAAsB;IAEtB,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC;IAE3E,4EAA4E;IAC5E,0DAA0D;IAC1D,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAE,EAAE,MAAM,CAAC,KAAK,CAAE,CAAC,CAAC,CAAC;AAC9E,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ac-kit/algo",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Classic algorithms that work on your own types rather than on numbers: searching, sorting, grouping, graph traversal and string distance.",
5
5
  "keywords": [
6
6
  "algorithms",
@@ -45,15 +45,15 @@
45
45
  "prepack": "yarn build"
46
46
  },
47
47
  "dependencies": {
48
- "@ac-kit/core": "^0.10.0",
49
- "@ac-kit/data": "^0.3.2",
48
+ "@ac-kit/core": "^0.12.0",
49
+ "@ac-kit/data": "^0.4.0",
50
50
  "@types/web": "^0.0.367"
51
51
  },
52
52
  "devDependencies": {
53
- "@ac-kit/math-random": "0.2.0",
53
+ "@ac-kit/math-random": "0.4.0",
54
54
  "@ac-kit/tsconfig": "0.1.0",
55
55
  "@ac-kit/typedoc-config": "0.1.0",
56
- "@ac-kit/vitest-config": "0.1.1",
56
+ "@ac-kit/vitest-config": "0.2.0",
57
57
  "@typescript/native": "typescript@7.0.2",
58
58
  "rimraf": "6.1.4",
59
59
  "typedoc": "0.28.20",
@@ -1,23 +0,0 @@
1
- import type { RandomFn } from "@ac-kit/core";
2
- /**
3
- * Answers `true` with the given probability.
4
- *
5
- * Calls `random` exactly once, whatever `probability` is (also `0` and `1`), so
6
- * a seeded source is consumed at the same rate whichever branch a test takes.
7
- * `0` never answers `true` and `1` always does, since `random` is in `[0, 1)`.
8
- *
9
- * Complexity: O(1), one call to `random`.
10
- *
11
- * @example
12
- * ```ts
13
- * if (randomBoolean(0.3, Math.random)) {
14
- * // taken three times out of ten
15
- * }
16
- * ```;
17
- *
18
- * @param probability The chance of `true`, in `[0, 1]`.
19
- * @param random A source in `[0, 1)`.
20
- * @throws {RangeError} When `probability` is `NaN` or outside `[0, 1]`.
21
- */
22
- export declare function randomBoolean(probability: number, random: RandomFn): boolean;
23
- //# sourceMappingURL=random-boolean.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"random-boolean.d.ts","sourceRoot":"","sources":["../../src/sample/random-boolean.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,aAAa,CAAC,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,GAAG,OAAO,CAM5E"}
@@ -1,27 +0,0 @@
1
- /**
2
- * Answers `true` with the given probability.
3
- *
4
- * Calls `random` exactly once, whatever `probability` is (also `0` and `1`), so
5
- * a seeded source is consumed at the same rate whichever branch a test takes.
6
- * `0` never answers `true` and `1` always does, since `random` is in `[0, 1)`.
7
- *
8
- * Complexity: O(1), one call to `random`.
9
- *
10
- * @example
11
- * ```ts
12
- * if (randomBoolean(0.3, Math.random)) {
13
- * // taken three times out of ten
14
- * }
15
- * ```;
16
- *
17
- * @param probability The chance of `true`, in `[0, 1]`.
18
- * @param random A source in `[0, 1)`.
19
- * @throws {RangeError} When `probability` is `NaN` or outside `[0, 1]`.
20
- */
21
- export function randomBoolean(probability, random) {
22
- if (!(probability >= 0 && probability <= 1)) {
23
- throw new RangeError("probability must be within [0, 1]");
24
- }
25
- return random() < probability;
26
- }
27
- //# sourceMappingURL=random-boolean.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"random-boolean.js","sourceRoot":"","sources":["../../src/sample/random-boolean.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,aAAa,CAAC,WAAmB,EAAE,MAAgB;IAClE,IAAI,CAAC,CAAC,WAAW,IAAI,CAAC,IAAI,WAAW,IAAI,CAAC,CAAC,EAAE,CAAC;QAC7C,MAAM,IAAI,UAAU,CAAC,mCAAmC,CAAC,CAAC;IAC3D,CAAC;IAED,OAAO,MAAM,EAAE,GAAG,WAAW,CAAC;AAC/B,CAAC"}
@@ -1,27 +0,0 @@
1
- import type { RandomFn } from "@ac-kit/core";
2
- /**
3
- * Draws an integer uniformly from `[minInclusive, maxExclusive)`.
4
- *
5
- * Computed as `floor(random() * range)`, so the result is as uniform as
6
- * `random` is: a generator with `k` bits of output gives each value a
7
- * probability within `range / 2^k` (relative) of `1 / range`, about `2^-20` for
8
- * `range = 2^32` over `Math.random`'s 52 to 53 bits, depending on the engine. A
9
- * range wider than the generator's resolution cannot be covered uniformly by
10
- * any method.
11
- *
12
- * Complexity: O(1), one call to `random`.
13
- *
14
- * @example
15
- * ```ts
16
- * const roll = randomInteger(1, 7, Math.random); // 1..6
17
- * ```;
18
- *
19
- * @param minInclusive The smallest result; a safe integer.
20
- * @param maxExclusive The bound, never returned; a safe integer greater than
21
- * `minInclusive`, with `maxExclusive - minInclusive` at most `2^53`.
22
- * @param random A source in `[0, 1)`.
23
- * @throws {RangeError} When a bound is not a safe integer, the range is empty
24
- * or wider than `2^53`.
25
- */
26
- export declare function randomInteger(minInclusive: number, maxExclusive: number, random: RandomFn): number;
27
- //# sourceMappingURL=random-integer.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"random-integer.d.ts","sourceRoot":"","sources":["../../src/sample/random-integer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,aAAa,CAC5B,YAAY,EAAE,MAAM,EACpB,YAAY,EAAE,MAAM,EACpB,MAAM,EAAE,QAAQ,GACd,MAAM,CAkBR"}
@@ -1,40 +0,0 @@
1
- /**
2
- * Draws an integer uniformly from `[minInclusive, maxExclusive)`.
3
- *
4
- * Computed as `floor(random() * range)`, so the result is as uniform as
5
- * `random` is: a generator with `k` bits of output gives each value a
6
- * probability within `range / 2^k` (relative) of `1 / range`, about `2^-20` for
7
- * `range = 2^32` over `Math.random`'s 52 to 53 bits, depending on the engine. A
8
- * range wider than the generator's resolution cannot be covered uniformly by
9
- * any method.
10
- *
11
- * Complexity: O(1), one call to `random`.
12
- *
13
- * @example
14
- * ```ts
15
- * const roll = randomInteger(1, 7, Math.random); // 1..6
16
- * ```;
17
- *
18
- * @param minInclusive The smallest result; a safe integer.
19
- * @param maxExclusive The bound, never returned; a safe integer greater than
20
- * `minInclusive`, with `maxExclusive - minInclusive` at most `2^53`.
21
- * @param random A source in `[0, 1)`.
22
- * @throws {RangeError} When a bound is not a safe integer, the range is empty
23
- * or wider than `2^53`.
24
- */
25
- export function randomInteger(minInclusive, maxExclusive, random) {
26
- if (!Number.isSafeInteger(minInclusive) ||
27
- !Number.isSafeInteger(maxExclusive)) {
28
- throw new RangeError("bounds must be safe integers");
29
- }
30
- // Compared before subtracting: the difference of two safe integers can
31
- // round to 2^53 when the true range is 2^53 + 1.
32
- if (!(maxExclusive > minInclusive) || maxExclusive > minInclusive + 2 ** 53) {
33
- throw new RangeError("maxExclusive must exceed minInclusive by at most 2^53");
34
- }
35
- const range = maxExclusive - minInclusive;
36
- const offset = Math.floor(random() * range);
37
- // random() * range can round up to range itself for a huge range.
38
- return minInclusive + Math.min(offset, range - 1);
39
- }
40
- //# sourceMappingURL=random-integer.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"random-integer.js","sourceRoot":"","sources":["../../src/sample/random-integer.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,aAAa,CAC5B,YAAoB,EACpB,YAAoB,EACpB,MAAgB;IAEhB,IACC,CAAC,MAAM,CAAC,aAAa,CAAC,YAAY,CAAC;QACnC,CAAC,MAAM,CAAC,aAAa,CAAC,YAAY,CAAC,EAClC,CAAC;QACF,MAAM,IAAI,UAAU,CAAC,8BAA8B,CAAC,CAAC;IACtD,CAAC;IACD,uEAAuE;IACvE,iDAAiD;IACjD,IAAI,CAAC,CAAC,YAAY,GAAG,YAAY,CAAC,IAAI,YAAY,GAAG,YAAY,GAAG,CAAC,IAAI,EAAE,EAAE,CAAC;QAC7E,MAAM,IAAI,UAAU,CACnB,uDAAuD,CACvD,CAAC;IACH,CAAC;IACD,MAAM,KAAK,GAAG,YAAY,GAAG,YAAY,CAAC;IAC1C,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC;IAC5C,kEAAkE;IAClE,OAAO,YAAY,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;AACnD,CAAC"}