@ac-kit/algo 0.1.1 → 0.2.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 +45 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/dist/interval/complement-intervals.d.ts +29 -0
- package/dist/interval/complement-intervals.d.ts.map +1 -0
- package/dist/interval/complement-intervals.js +62 -0
- package/dist/interval/complement-intervals.js.map +1 -0
- package/dist/interval/intersect-intervals.d.ts +21 -0
- package/dist/interval/intersect-intervals.d.ts.map +1 -0
- package/dist/interval/intersect-intervals.js +48 -0
- package/dist/interval/intersect-intervals.js.map +1 -0
- package/dist/interval/interval-step.d.ts +29 -0
- package/dist/interval/interval-step.d.ts.map +1 -0
- package/dist/interval/interval-step.js +11 -0
- package/dist/interval/interval-step.js.map +1 -0
- package/dist/interval/merge-intervals.d.ts +47 -0
- package/dist/interval/merge-intervals.d.ts.map +1 -0
- package/dist/interval/merge-intervals.js +78 -0
- package/dist/interval/merge-intervals.js.map +1 -0
- package/dist/interval/subtract-intervals.d.ts +28 -0
- package/dist/interval/subtract-intervals.d.ts.map +1 -0
- package/dist/interval/subtract-intervals.js +66 -0
- package/dist/interval/subtract-intervals.js.map +1 -0
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,50 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- a5c4d66: New `mergeIntervals`, `intersectIntervals`, `complementIntervals` and
|
|
8
|
+
`subtractIntervals`, a complete set algebra over the flat, stride-2
|
|
9
|
+
`[start, end, …]` form `isInSortedIntervals` already speaks. Input and output
|
|
10
|
+
are both that form, so one feeds the next and no interval object is allocated on
|
|
11
|
+
the way through.
|
|
12
|
+
|
|
13
|
+
`mergeIntervals(intervals, compare, isAdjacent?)` sorts and coalesces a set into
|
|
14
|
+
the minimal disjoint set covering the same points — the preprocessing step
|
|
15
|
+
`isInSortedIntervals` assumed somebody else had done and had no companion that
|
|
16
|
+
produced. Touching and adjacent are kept as separate questions: `[1, 5]` and
|
|
17
|
+
`[5, 9]` share the point 5 and always merge; `[1, 2]` and `[3, 4]` share
|
|
18
|
+
nothing, and whether they should still become `[1, 4]` depends on whether the
|
|
19
|
+
bound type has anything between 2 and 3 — the integers do not, the reals do.
|
|
20
|
+
`isAdjacent` is how a caller says which it holds, and omitting it merges
|
|
21
|
+
overlaps only.
|
|
22
|
+
|
|
23
|
+
`intersectIntervals(a, b, compare)` is one merge walk, O(n + m), and needs
|
|
24
|
+
nothing beyond the comparator: every bound it emits is a bound one of the inputs
|
|
25
|
+
already named.
|
|
26
|
+
|
|
27
|
+
`complementIntervals(intervals, bounds, compare, step)` and
|
|
28
|
+
`subtractIntervals(a, b, compare, step)` take the new `IntervalStep<T>`, a
|
|
29
|
+
`{ next, previous }` pair, because they are the two that have to name a point
|
|
30
|
+
just outside an interval — removing `[3, 5]` from `[0, 10]` leaves `[0, 2]` and
|
|
31
|
+
`[6, 10]`, and neither 2 nor 6 can be derived from a comparator. That also marks
|
|
32
|
+
them as discrete-domain operations: over the reals the result is genuinely not
|
|
33
|
+
expressible as closed intervals, and no `IntervalStep` can be written.
|
|
34
|
+
`INTEGER_INTERVAL_STEP` and `BIG_INT_INTERVAL_STEP` ship for the two common
|
|
35
|
+
cases.
|
|
36
|
+
|
|
37
|
+
Those three require ascending, disjoint input, as `isInSortedIntervals` does,
|
|
38
|
+
and all four preserve that property in their output — including non-adjacency,
|
|
39
|
+
so a caller never has to re-run `mergeIntervals` on a result.
|
|
40
|
+
|
|
41
|
+
### Patch Changes
|
|
42
|
+
|
|
43
|
+
- Updated dependencies [a5c4d66]
|
|
44
|
+
- Updated dependencies [a5c4d66]
|
|
45
|
+
- @ac-kit/core@0.3.0
|
|
46
|
+
- @ac-kit/data@0.1.2
|
|
47
|
+
|
|
3
48
|
## 0.1.1
|
|
4
49
|
|
|
5
50
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,12 @@ export * from "./graph/dfs.js";
|
|
|
5
5
|
export * from "./group/group-by.js";
|
|
6
6
|
export * from "./group/partition-indices.js";
|
|
7
7
|
export * from "./group/partition.js";
|
|
8
|
+
export * from "./interval/complement-intervals.js";
|
|
9
|
+
export * from "./interval/intersect-intervals.js";
|
|
10
|
+
export * from "./interval/interval-step.js";
|
|
8
11
|
export * from "./interval/is-in-sorted-intervals.js";
|
|
12
|
+
export * from "./interval/merge-intervals.js";
|
|
13
|
+
export * from "./interval/subtract-intervals.js";
|
|
9
14
|
export * from "./optimize/backtrack.js";
|
|
10
15
|
export * from "./optimize/beam-search.js";
|
|
11
16
|
export * from "./ordered/bisect-center.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +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,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Ac,sBAAsB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,12 @@ export * from "./graph/dfs.js";
|
|
|
5
5
|
export * from "./group/group-by.js";
|
|
6
6
|
export * from "./group/partition-indices.js";
|
|
7
7
|
export * from "./group/partition.js";
|
|
8
|
+
export * from "./interval/complement-intervals.js";
|
|
9
|
+
export * from "./interval/intersect-intervals.js";
|
|
10
|
+
export * from "./interval/interval-step.js";
|
|
8
11
|
export * from "./interval/is-in-sorted-intervals.js";
|
|
12
|
+
export * from "./interval/merge-intervals.js";
|
|
13
|
+
export * from "./interval/subtract-intervals.js";
|
|
9
14
|
export * from "./optimize/backtrack.js";
|
|
10
15
|
export * from "./optimize/beam-search.js";
|
|
11
16
|
export * from "./ordered/bisect-center.js";
|
package/dist/index.js.map
CHANGED
|
@@ -1 +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,CAA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kit/core";
|
|
2
|
+
import type { IntervalStep } from "./interval-step.js";
|
|
3
|
+
/**
|
|
4
|
+
* The gaps: every point of `bounds` that none of `intervals` covers, as the
|
|
5
|
+
* same flat, stride-2 `[start, end, …]` form.
|
|
6
|
+
*
|
|
7
|
+
* Free slots between the meetings in a calendar, the lines a coverage report
|
|
8
|
+
* never reached, the holes in an allocation map. Anything outside `bounds` is
|
|
9
|
+
* ignored rather than reported, since "everything not covered" has no answer
|
|
10
|
+
* over an unbounded domain.
|
|
11
|
+
*
|
|
12
|
+
* `intervals` must already be ascending and disjoint — run `mergeIntervals`
|
|
13
|
+
* first if it is not. Its bounds may lie outside `bounds`; the parts that do
|
|
14
|
+
* are clipped.
|
|
15
|
+
*
|
|
16
|
+
* Complexity: O(n) in the interval count, O(n) extra space.
|
|
17
|
+
*
|
|
18
|
+
* @param intervals - Flat `[start, end, …]` pairs, ascending and disjoint.
|
|
19
|
+
* @param bounds - The inclusive window to report gaps within.
|
|
20
|
+
* @param compare - Total order over the bound type.
|
|
21
|
+
* @param step - Successor and predecessor; the gap beside `[3, 5]` ends at 2
|
|
22
|
+
* and resumes at 6, and only `step` knows that.
|
|
23
|
+
* @returns A fresh flat array, ascending, disjoint, and covering exactly
|
|
24
|
+
* `bounds` minus `intervals`. Empty when `intervals` covers all of `bounds`
|
|
25
|
+
* or when `bounds` is itself empty.
|
|
26
|
+
* @throws {RangeError} When `intervals` holds an odd number of bounds.
|
|
27
|
+
*/
|
|
28
|
+
export declare function complementIntervals<T>(intervals: readonly T[], bounds: readonly [T, T], compare: Comparator<T>, step: IntervalStep<T>): T[];
|
|
29
|
+
//# sourceMappingURL=complement-intervals.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"complement-intervals.d.ts","sourceRoot":"","sources":["../../src/interval/complement-intervals.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EACpC,SAAS,EAAE,SAAS,CAAC,EAAE,EACvB,MAAM,EAAE,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,EACvB,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,EACtB,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,GACnB,CAAC,EAAE,CAgDL"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The gaps: every point of `bounds` that none of `intervals` covers, as the
|
|
3
|
+
* same flat, stride-2 `[start, end, …]` form.
|
|
4
|
+
*
|
|
5
|
+
* Free slots between the meetings in a calendar, the lines a coverage report
|
|
6
|
+
* never reached, the holes in an allocation map. Anything outside `bounds` is
|
|
7
|
+
* ignored rather than reported, since "everything not covered" has no answer
|
|
8
|
+
* over an unbounded domain.
|
|
9
|
+
*
|
|
10
|
+
* `intervals` must already be ascending and disjoint — run `mergeIntervals`
|
|
11
|
+
* first if it is not. Its bounds may lie outside `bounds`; the parts that do
|
|
12
|
+
* are clipped.
|
|
13
|
+
*
|
|
14
|
+
* Complexity: O(n) in the interval count, O(n) extra space.
|
|
15
|
+
*
|
|
16
|
+
* @param intervals - Flat `[start, end, …]` pairs, ascending and disjoint.
|
|
17
|
+
* @param bounds - The inclusive window to report gaps within.
|
|
18
|
+
* @param compare - Total order over the bound type.
|
|
19
|
+
* @param step - Successor and predecessor; the gap beside `[3, 5]` ends at 2
|
|
20
|
+
* and resumes at 6, and only `step` knows that.
|
|
21
|
+
* @returns A fresh flat array, ascending, disjoint, and covering exactly
|
|
22
|
+
* `bounds` minus `intervals`. Empty when `intervals` covers all of `bounds`
|
|
23
|
+
* or when `bounds` is itself empty.
|
|
24
|
+
* @throws {RangeError} When `intervals` holds an odd number of bounds.
|
|
25
|
+
*/
|
|
26
|
+
export function complementIntervals(intervals, bounds, compare, step) {
|
|
27
|
+
if (intervals.length % 2 !== 0) {
|
|
28
|
+
throw new RangeError(`interval bounds come in pairs, got ${intervals.length}`);
|
|
29
|
+
}
|
|
30
|
+
const [low, high] = bounds;
|
|
31
|
+
if (compare(low, high) > 0) {
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
const gaps = [];
|
|
35
|
+
// The first point not yet known to be covered.
|
|
36
|
+
let cursor = low;
|
|
37
|
+
for (let index = 0; index < intervals.length; index += 2) {
|
|
38
|
+
// Non-null: the length is even, so both bounds of the pair exist.
|
|
39
|
+
const start = intervals[index];
|
|
40
|
+
const end = intervals[index + 1];
|
|
41
|
+
if (compare(end, cursor) < 0) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (compare(start, high) > 0) {
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
if (compare(start, cursor) > 0) {
|
|
48
|
+
gaps.push(cursor, step.previous(start));
|
|
49
|
+
}
|
|
50
|
+
// `step.next` is only ever reached below `high`, so it cannot be asked for
|
|
51
|
+
// a successor the bound type has no room for.
|
|
52
|
+
if (compare(end, high) >= 0) {
|
|
53
|
+
return gaps;
|
|
54
|
+
}
|
|
55
|
+
cursor = step.next(end);
|
|
56
|
+
}
|
|
57
|
+
if (compare(cursor, high) <= 0) {
|
|
58
|
+
gaps.push(cursor, high);
|
|
59
|
+
}
|
|
60
|
+
return gaps;
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=complement-intervals.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"complement-intervals.js","sourceRoot":"","sources":["../../src/interval/complement-intervals.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,mBAAmB,CAClC,SAAuB,EACvB,MAAuB,EACvB,OAAsB,EACtB,IAAqB;IAErB,IAAI,SAAS,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;QAChC,MAAM,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,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACzB,CAAC;IAED,OAAO,IAAI,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Comparator } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Every point both sets cover, as the same flat, stride-2 `[start, end, …]`
|
|
4
|
+
* form.
|
|
5
|
+
*
|
|
6
|
+
* Both inputs must already be ascending and disjoint — run `mergeIntervals`
|
|
7
|
+
* first if they are not. Needs no `IntervalStep`, unlike `complementIntervals`
|
|
8
|
+
* and `subtractIntervals`: an intersection's bounds are always bounds one of
|
|
9
|
+
* the inputs already named, so nothing has to be stepped past.
|
|
10
|
+
*
|
|
11
|
+
* Complexity: O(n + m) in the two interval counts, by a single merge walk.
|
|
12
|
+
*
|
|
13
|
+
* @param a - Flat `[start, end, …]` pairs, ascending and disjoint.
|
|
14
|
+
* @param b - The same, and independent of `a`'s bounds.
|
|
15
|
+
* @param compare - Total order over the bound type.
|
|
16
|
+
* @returns A fresh flat array, ascending and disjoint. Inherits both inputs'
|
|
17
|
+
* non-adjacency: it can only narrow intervals, never bring two closer.
|
|
18
|
+
* @throws {RangeError} When either input holds an odd number of bounds.
|
|
19
|
+
*/
|
|
20
|
+
export declare function intersectIntervals<T>(a: readonly T[], b: readonly T[], compare: Comparator<T>): T[];
|
|
21
|
+
//# sourceMappingURL=intersect-intervals.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"intersect-intervals.d.ts","sourceRoot":"","sources":["../../src/interval/intersect-intervals.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EACnC,CAAC,EAAE,SAAS,CAAC,EAAE,EACf,CAAC,EAAE,SAAS,CAAC,EAAE,EACf,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,GACpB,CAAC,EAAE,CAmCL"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every point both sets cover, as the same flat, stride-2 `[start, end, …]`
|
|
3
|
+
* form.
|
|
4
|
+
*
|
|
5
|
+
* Both inputs must already be ascending and disjoint — run `mergeIntervals`
|
|
6
|
+
* first if they are not. Needs no `IntervalStep`, unlike `complementIntervals`
|
|
7
|
+
* and `subtractIntervals`: an intersection's bounds are always bounds one of
|
|
8
|
+
* the inputs already named, so nothing has to be stepped past.
|
|
9
|
+
*
|
|
10
|
+
* Complexity: O(n + m) in the two interval counts, by a single merge walk.
|
|
11
|
+
*
|
|
12
|
+
* @param a - Flat `[start, end, …]` pairs, ascending and disjoint.
|
|
13
|
+
* @param b - The same, and independent of `a`'s bounds.
|
|
14
|
+
* @param compare - Total order over the bound type.
|
|
15
|
+
* @returns A fresh flat array, ascending and disjoint. Inherits both inputs'
|
|
16
|
+
* non-adjacency: it can only narrow intervals, never bring two closer.
|
|
17
|
+
* @throws {RangeError} When either input holds an odd number of bounds.
|
|
18
|
+
*/
|
|
19
|
+
export function intersectIntervals(a, b, compare) {
|
|
20
|
+
if (a.length % 2 !== 0 || b.length % 2 !== 0) {
|
|
21
|
+
throw new RangeError(`interval bounds come in pairs, got ${a.length} and ${b.length}`);
|
|
22
|
+
}
|
|
23
|
+
const result = [];
|
|
24
|
+
let indexA = 0;
|
|
25
|
+
let indexB = 0;
|
|
26
|
+
while (indexA < a.length && indexB < b.length) {
|
|
27
|
+
// Non-null: both indices are below their array's even length.
|
|
28
|
+
const endA = a[indexA + 1];
|
|
29
|
+
const endB = b[indexB + 1];
|
|
30
|
+
const startA = a[indexA];
|
|
31
|
+
const startB = b[indexB];
|
|
32
|
+
const start = compare(startA, startB) > 0 ? startA : startB;
|
|
33
|
+
const end = compare(endA, endB) < 0 ? endA : endB;
|
|
34
|
+
if (compare(start, end) <= 0) {
|
|
35
|
+
result.push(start, end);
|
|
36
|
+
}
|
|
37
|
+
// Retire whichever interval ends first; the other may still meet the next
|
|
38
|
+
// one on the opposite side.
|
|
39
|
+
if (compare(endA, endB) < 0) {
|
|
40
|
+
indexA += 2;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
indexB += 2;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=intersect-intervals.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"intersect-intervals.js","sourceRoot":"","sources":["../../src/interval/intersect-intervals.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,kBAAkB,CACjC,CAAe,EACf,CAAe,EACf,OAAsB;IAEtB,IAAI,CAAC,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;QAC9C,MAAM,IAAI,UAAU,CACnB,sCAAsC,CAAC,CAAC,MAAM,QAAQ,CAAC,CAAC,MAAM,EAAE,CAChE,CAAC;IACH,CAAC;IAED,MAAM,MAAM,GAAQ,EAAE,CAAC;IACvB,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,IAAI,MAAM,GAAG,CAAC,CAAC;IAEf,OAAO,MAAM,GAAG,CAAC,CAAC,MAAM,IAAI,MAAM,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC;QAC/C,8DAA8D;QAC9D,MAAM,IAAI,GAAG,CAAC,CAAC,MAAM,GAAG,CAAC,CAAE,CAAC;QAC5B,MAAM,IAAI,GAAG,CAAC,CAAC,MAAM,GAAG,CAAC,CAAE,CAAC;QAC5B,MAAM,MAAM,GAAG,CAAC,CAAC,MAAM,CAAE,CAAC;QAC1B,MAAM,MAAM,GAAG,CAAC,CAAC,MAAM,CAAE,CAAC;QAE1B,MAAM,KAAK,GAAG,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC;QAC5D,MAAM,GAAG,GAAG,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;QAElD,IAAI,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YAC9B,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QACzB,CAAC;QAED,0EAA0E;QAC1E,4BAA4B;QAC5B,IAAI,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;YAC7B,MAAM,IAAI,CAAC,CAAC;QACb,CAAC;aAAM,CAAC;YACP,MAAM,IAAI,CAAC,CAAC;QACb,CAAC;IACF,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The successor and predecessor of a bound, for the operations that have to
|
|
3
|
+
* name a point just outside an interval.
|
|
4
|
+
*
|
|
5
|
+
* Complementing or subtracting closed intervals cannot be done without one.
|
|
6
|
+
* Removing `[3, 5]` from `[0, 10]` leaves everything up to but excluding 3 and
|
|
7
|
+
* everything after but excluding 5, and a closed interval can only say that as
|
|
8
|
+
* `[0, 2]` and `[6, 10]` — so the algorithm needs 2 from 3 and 6 from 5, which
|
|
9
|
+
* no comparator can supply.
|
|
10
|
+
*
|
|
11
|
+
* That makes these operations meaningful **only over a discrete bound type**.
|
|
12
|
+
* Over the reals the answer is genuinely not expressible as closed intervals,
|
|
13
|
+
* and no `IntervalStep` can be written for them.
|
|
14
|
+
*
|
|
15
|
+
* A type with a step is also adjacency-aware, so a caller passing one here
|
|
16
|
+
* usually wants `(end, next) => compare(step.next(end), next) === 0` as
|
|
17
|
+
* `mergeIntervals`' `isAdjacent`.
|
|
18
|
+
*/
|
|
19
|
+
export type IntervalStep<T> = {
|
|
20
|
+
/** The smallest value strictly greater than `value`. */
|
|
21
|
+
next(value: T): T;
|
|
22
|
+
/** The largest value strictly less than `value`. */
|
|
23
|
+
previous(value: T): T;
|
|
24
|
+
};
|
|
25
|
+
/** `IntervalStep<number>` over the integers. */
|
|
26
|
+
export declare const INTEGER_INTERVAL_STEP: IntervalStep<number>;
|
|
27
|
+
/** `IntervalStep<bigint>` over the integers. */
|
|
28
|
+
export declare const BIG_INT_INTERVAL_STEP: IntervalStep<bigint>;
|
|
29
|
+
//# sourceMappingURL=interval-step.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interval-step.d.ts","sourceRoot":"","sources":["../../src/interval/interval-step.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI;IAC7B,wDAAwD;IACxD,IAAI,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;IAClB,oDAAoD;IACpD,QAAQ,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;CACtB,CAAC;AAEF,gDAAgD;AAChD,eAAO,MAAM,qBAAqB,EAAE,YAAY,CAAC,MAAM,CAGtD,CAAC;AAEF,gDAAgD;AAChD,eAAO,MAAM,qBAAqB,EAAE,YAAY,CAAC,MAAM,CAGtD,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** `IntervalStep<number>` over the integers. */
|
|
2
|
+
export const INTEGER_INTERVAL_STEP = {
|
|
3
|
+
next: (value) => value + 1,
|
|
4
|
+
previous: (value) => value - 1,
|
|
5
|
+
};
|
|
6
|
+
/** `IntervalStep<bigint>` over the integers. */
|
|
7
|
+
export const BIG_INT_INTERVAL_STEP = {
|
|
8
|
+
next: (value) => value + 1n,
|
|
9
|
+
previous: (value) => value - 1n,
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=interval-step.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interval-step.js","sourceRoot":"","sources":["../../src/interval/interval-step.ts"],"names":[],"mappings":"AAyBA,gDAAgD;AAChD,MAAM,CAAC,MAAM,qBAAqB,GAAyB;IAC1D,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,GAAG,CAAC;IAC1B,QAAQ,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,GAAG,CAAC;CAC9B,CAAC;AAEF,gDAAgD;AAChD,MAAM,CAAC,MAAM,qBAAqB,GAAyB;IAC1D,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,GAAG,EAAE;IAC3B,QAAQ,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,GAAG,EAAE;CAC/B,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Comparator } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Whether an interval ending at `end` and one starting at `nextStart` leave no
|
|
4
|
+
* point between them, so the two cover a contiguous stretch despite sharing no
|
|
5
|
+
* point.
|
|
6
|
+
*
|
|
7
|
+
* Only meaningful over a discrete bound type: `(end, next) => next === end + 1`
|
|
8
|
+
* for integers, `(end, next) => next === end + 1n` for `bigint`.
|
|
9
|
+
*/
|
|
10
|
+
export type IntervalAdjacency<T> = (end: T, nextStart: T) => boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Sorts and coalesces intervals into the minimal set of disjoint, ascending,
|
|
13
|
+
* inclusive ranges covering exactly the points the input covered.
|
|
14
|
+
*
|
|
15
|
+
* Intervals arrive and leave in the flat, stride-2 `[start, end, start, end,
|
|
16
|
+
* …]` form the rest of `interval/` uses, so the result can be handed straight
|
|
17
|
+
* to {@link isInSortedIntervals}.
|
|
18
|
+
*
|
|
19
|
+
* Two intervals coalesce when they overlap or touch at a point: `[1, 5]` and
|
|
20
|
+
* `[5, 9]` become `[1, 9]`, because 5 is in both. **Touching and adjacent are
|
|
21
|
+
* not the same question, and only the first is answered by default.** `[1, 2]`
|
|
22
|
+
* and `[3, 4]` share no point, so whether they should still become `[1, 4]`
|
|
23
|
+
* turns on whether anything lies strictly between 2 and 3 — over the integers
|
|
24
|
+
* nothing does, over the reals 2.5 does, and over a `Date` it depends on
|
|
25
|
+
* whether the caller is counting milliseconds or instants. The bound type alone
|
|
26
|
+
* cannot settle it, so `isAdjacent` is how the caller says what the successor
|
|
27
|
+
* of a bound is. Omit it and only overlaps merge.
|
|
28
|
+
*
|
|
29
|
+
* An interval whose start compares after its end covers no point and is
|
|
30
|
+
* dropped: the result is the union of the inputs, and an empty interval
|
|
31
|
+
* contributes nothing to a union.
|
|
32
|
+
*
|
|
33
|
+
* Complexity: O(n log n) in the interval count, dominated by the sort; O(n)
|
|
34
|
+
* extra space. `isAdjacent`, when given, is called at most once per interval,
|
|
35
|
+
* on already-sorted neighbours that do not overlap.
|
|
36
|
+
*
|
|
37
|
+
* @param intervals - Flat `[start, end, …]` pairs, in any order, possibly
|
|
38
|
+
* overlapping.
|
|
39
|
+
* @param compare - Total order over the bound type.
|
|
40
|
+
* @param isAdjacent - Successor test for a discrete bound type; omit for a
|
|
41
|
+
* dense one.
|
|
42
|
+
* @returns A fresh flat array, ascending by start, holding no two intervals
|
|
43
|
+
* that overlap, touch, or satisfy `isAdjacent`.
|
|
44
|
+
* @throws {RangeError} When `intervals` holds an odd number of bounds.
|
|
45
|
+
*/
|
|
46
|
+
export declare function mergeIntervals<T>(intervals: readonly T[], compare: Comparator<T>, isAdjacent?: IntervalAdjacency<T>): T[];
|
|
47
|
+
//# sourceMappingURL=merge-intervals.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merge-intervals.d.ts","sourceRoot":"","sources":["../../src/interval/merge-intervals.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C;;;;;;;GAOG;AACH,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,SAAS,EAAE,CAAC,KAAK,OAAO,CAAC;AAErE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAC/B,SAAS,EAAE,SAAS,CAAC,EAAE,EACvB,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,EACtB,UAAU,CAAC,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAC/B,CAAC,EAAE,CAwDL"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sorts and coalesces intervals into the minimal set of disjoint, ascending,
|
|
3
|
+
* inclusive ranges covering exactly the points the input covered.
|
|
4
|
+
*
|
|
5
|
+
* Intervals arrive and leave in the flat, stride-2 `[start, end, start, end,
|
|
6
|
+
* …]` form the rest of `interval/` uses, so the result can be handed straight
|
|
7
|
+
* to {@link isInSortedIntervals}.
|
|
8
|
+
*
|
|
9
|
+
* Two intervals coalesce when they overlap or touch at a point: `[1, 5]` and
|
|
10
|
+
* `[5, 9]` become `[1, 9]`, because 5 is in both. **Touching and adjacent are
|
|
11
|
+
* not the same question, and only the first is answered by default.** `[1, 2]`
|
|
12
|
+
* and `[3, 4]` share no point, so whether they should still become `[1, 4]`
|
|
13
|
+
* turns on whether anything lies strictly between 2 and 3 — over the integers
|
|
14
|
+
* nothing does, over the reals 2.5 does, and over a `Date` it depends on
|
|
15
|
+
* whether the caller is counting milliseconds or instants. The bound type alone
|
|
16
|
+
* cannot settle it, so `isAdjacent` is how the caller says what the successor
|
|
17
|
+
* of a bound is. Omit it and only overlaps merge.
|
|
18
|
+
*
|
|
19
|
+
* An interval whose start compares after its end covers no point and is
|
|
20
|
+
* dropped: the result is the union of the inputs, and an empty interval
|
|
21
|
+
* contributes nothing to a union.
|
|
22
|
+
*
|
|
23
|
+
* Complexity: O(n log n) in the interval count, dominated by the sort; O(n)
|
|
24
|
+
* extra space. `isAdjacent`, when given, is called at most once per interval,
|
|
25
|
+
* on already-sorted neighbours that do not overlap.
|
|
26
|
+
*
|
|
27
|
+
* @param intervals - Flat `[start, end, …]` pairs, in any order, possibly
|
|
28
|
+
* overlapping.
|
|
29
|
+
* @param compare - Total order over the bound type.
|
|
30
|
+
* @param isAdjacent - Successor test for a discrete bound type; omit for a
|
|
31
|
+
* dense one.
|
|
32
|
+
* @returns A fresh flat array, ascending by start, holding no two intervals
|
|
33
|
+
* that overlap, touch, or satisfy `isAdjacent`.
|
|
34
|
+
* @throws {RangeError} When `intervals` holds an odd number of bounds.
|
|
35
|
+
*/
|
|
36
|
+
export function mergeIntervals(intervals, compare, isAdjacent) {
|
|
37
|
+
if (intervals.length % 2 !== 0) {
|
|
38
|
+
throw new RangeError(`interval bounds come in pairs, got ${intervals.length}`);
|
|
39
|
+
}
|
|
40
|
+
// Sort pair indices rather than the pairs themselves: no interval object is
|
|
41
|
+
// ever materialised, which is the point of the flat form.
|
|
42
|
+
const order = [];
|
|
43
|
+
for (let pair = 0; pair * 2 < intervals.length; pair++) {
|
|
44
|
+
// Non-null: the length is even, so both bounds of `pair` exist.
|
|
45
|
+
if (compare(intervals[pair * 2], intervals[pair * 2 + 1]) <= 0) {
|
|
46
|
+
order.push(pair);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
order.sort((left, right) => {
|
|
50
|
+
// Non-null: `order` holds pair indices that were bounds-checked above.
|
|
51
|
+
const byStart = compare(intervals[left * 2], intervals[right * 2]);
|
|
52
|
+
return byStart !== 0
|
|
53
|
+
? byStart
|
|
54
|
+
: compare(intervals[left * 2 + 1], intervals[right * 2 + 1]);
|
|
55
|
+
});
|
|
56
|
+
const merged = [];
|
|
57
|
+
for (const pair of order) {
|
|
58
|
+
// Non-null: as above.
|
|
59
|
+
const start = intervals[pair * 2];
|
|
60
|
+
const end = intervals[pair * 2 + 1];
|
|
61
|
+
if (merged.length > 0) {
|
|
62
|
+
// Non-null: a non-empty `merged` always ends on an interval's end bound.
|
|
63
|
+
const previousEnd = merged[merged.length - 1];
|
|
64
|
+
// Sorted by start, so `start` is at or after the previous start and the
|
|
65
|
+
// only way the two can miss each other is by a gap after `previousEnd`.
|
|
66
|
+
if (compare(start, previousEnd) <= 0 ||
|
|
67
|
+
isAdjacent?.(previousEnd, start) === true) {
|
|
68
|
+
if (compare(end, previousEnd) > 0) {
|
|
69
|
+
merged[merged.length - 1] = end;
|
|
70
|
+
}
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
merged.push(start, end);
|
|
75
|
+
}
|
|
76
|
+
return merged;
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=merge-intervals.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merge-intervals.js","sourceRoot":"","sources":["../../src/interval/merge-intervals.ts"],"names":[],"mappings":"AAYA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,UAAU,cAAc,CAC7B,SAAuB,EACvB,OAAsB,EACtB,UAAiC;IAEjC,IAAI,SAAS,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;QAChC,MAAM,IAAI,UAAU,CACnB,sCAAsC,SAAS,CAAC,MAAM,EAAE,CACxD,CAAC;IACH,CAAC;IAED,4EAA4E;IAC5E,0DAA0D;IAC1D,MAAM,KAAK,GAAa,EAAE,CAAC;IAE3B,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,GAAG,CAAC,GAAG,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC;QACxD,gEAAgE;QAChE,IAAI,OAAO,CAAC,SAAS,CAAC,IAAI,GAAG,CAAC,CAAE,EAAE,SAAS,CAAC,IAAI,GAAG,CAAC,GAAG,CAAC,CAAE,CAAC,IAAI,CAAC,EAAE,CAAC;YAClE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAClB,CAAC;IACF,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,CAAC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AAC;AACf,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Comparator } from "@ac-kit/core";
|
|
2
|
+
import type { IntervalStep } from "./interval-step.js";
|
|
3
|
+
/**
|
|
4
|
+
* What remains of `a` once every point of `b` is removed, as the same flat,
|
|
5
|
+
* stride-2 `[start, end, …]` form.
|
|
6
|
+
*
|
|
7
|
+
* How a firewall carves an exception out of an allowlist: exact in both
|
|
8
|
+
* directions, so no point of `b` survives and no point of `a` is lost.
|
|
9
|
+
*
|
|
10
|
+
* Both inputs must already be ascending and disjoint — run `mergeIntervals`
|
|
11
|
+
* first if they are not.
|
|
12
|
+
*
|
|
13
|
+
* Complexity: O(n + m) amortised in the two interval counts. Each hole is
|
|
14
|
+
* revisited only while it still overlaps the interval under consideration, and
|
|
15
|
+
* a hole that ends before the current interval starts is retired for good.
|
|
16
|
+
*
|
|
17
|
+
* @param a - Flat `[start, end, …]` pairs, ascending and disjoint.
|
|
18
|
+
* @param b - The holes to remove, ascending and disjoint.
|
|
19
|
+
* @param compare - Total order over the bound type.
|
|
20
|
+
* @param step - Successor and predecessor; removing `[3, 5]` leaves a piece
|
|
21
|
+
* ending at 2 and one resuming at 6, and only `step` knows those.
|
|
22
|
+
* @returns A fresh flat array, ascending and disjoint. Inherits `a`'s
|
|
23
|
+
* non-adjacency: two surviving pieces are always separated by a removed
|
|
24
|
+
* point.
|
|
25
|
+
* @throws {RangeError} When either input holds an odd number of bounds.
|
|
26
|
+
*/
|
|
27
|
+
export declare function subtractIntervals<T>(a: readonly T[], b: readonly T[], compare: Comparator<T>, step: IntervalStep<T>): T[];
|
|
28
|
+
//# sourceMappingURL=subtract-intervals.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subtract-intervals.d.ts","sourceRoot":"","sources":["../../src/interval/subtract-intervals.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAClC,CAAC,EAAE,SAAS,CAAC,EAAE,EACf,CAAC,EAAE,SAAS,CAAC,EAAE,EACf,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,EACtB,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,GACnB,CAAC,EAAE,CAsDL"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What remains of `a` once every point of `b` is removed, as the same flat,
|
|
3
|
+
* stride-2 `[start, end, …]` form.
|
|
4
|
+
*
|
|
5
|
+
* How a firewall carves an exception out of an allowlist: exact in both
|
|
6
|
+
* directions, so no point of `b` survives and no point of `a` is lost.
|
|
7
|
+
*
|
|
8
|
+
* Both inputs must already be ascending and disjoint — run `mergeIntervals`
|
|
9
|
+
* first if they are not.
|
|
10
|
+
*
|
|
11
|
+
* Complexity: O(n + m) amortised in the two interval counts. Each hole is
|
|
12
|
+
* revisited only while it still overlaps the interval under consideration, and
|
|
13
|
+
* a hole that ends before the current interval starts is retired for good.
|
|
14
|
+
*
|
|
15
|
+
* @param a - Flat `[start, end, …]` pairs, ascending and disjoint.
|
|
16
|
+
* @param b - The holes to remove, ascending and disjoint.
|
|
17
|
+
* @param compare - Total order over the bound type.
|
|
18
|
+
* @param step - Successor and predecessor; removing `[3, 5]` leaves a piece
|
|
19
|
+
* ending at 2 and one resuming at 6, and only `step` knows those.
|
|
20
|
+
* @returns A fresh flat array, ascending and disjoint. Inherits `a`'s
|
|
21
|
+
* non-adjacency: two surviving pieces are always separated by a removed
|
|
22
|
+
* point.
|
|
23
|
+
* @throws {RangeError} When either input holds an odd number of bounds.
|
|
24
|
+
*/
|
|
25
|
+
export function subtractIntervals(a, b, compare, step) {
|
|
26
|
+
if (a.length % 2 !== 0 || b.length % 2 !== 0) {
|
|
27
|
+
throw new RangeError(`interval bounds come in pairs, got ${a.length} and ${b.length}`);
|
|
28
|
+
}
|
|
29
|
+
const result = [];
|
|
30
|
+
let firstLiveHole = 0;
|
|
31
|
+
for (let index = 0; index < a.length; index += 2) {
|
|
32
|
+
// Non-null: the length is even, so both bounds of the pair exist.
|
|
33
|
+
const end = a[index + 1];
|
|
34
|
+
let cursor = a[index];
|
|
35
|
+
let consumed = false;
|
|
36
|
+
// `a` ascends, so a hole ending before this interval starts cannot reach
|
|
37
|
+
// any later one either.
|
|
38
|
+
while (firstLiveHole < b.length &&
|
|
39
|
+
compare(b[firstLiveHole + 1], cursor) < 0) {
|
|
40
|
+
firstLiveHole += 2;
|
|
41
|
+
}
|
|
42
|
+
for (let hole = firstLiveHole; hole < b.length; hole += 2) {
|
|
43
|
+
// Non-null: as above.
|
|
44
|
+
const holeStart = b[hole];
|
|
45
|
+
const holeEnd = b[hole + 1];
|
|
46
|
+
if (compare(holeStart, end) > 0) {
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
if (compare(holeStart, cursor) > 0) {
|
|
50
|
+
result.push(cursor, step.previous(holeStart));
|
|
51
|
+
}
|
|
52
|
+
// `step.next` is only ever reached below `end`, so it cannot be asked
|
|
53
|
+
// for a successor the bound type has no room for.
|
|
54
|
+
if (compare(holeEnd, end) >= 0) {
|
|
55
|
+
consumed = true;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
cursor = step.next(holeEnd);
|
|
59
|
+
}
|
|
60
|
+
if (!consumed && compare(cursor, end) <= 0) {
|
|
61
|
+
result.push(cursor, end);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=subtract-intervals.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subtract-intervals.js","sourceRoot":"","sources":["../../src/interval/subtract-intervals.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,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M,CAAC,GAAG,CAAC,EAAE,CAAC;gBACpC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC;YAC/C,CAAC;YAED,sEAAsE;YACtE,kDAAkD;YAClD,IAAI,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;gBAChC,QAAQ,GAAG,IAAI,CAAC;gBAChB,MAAM;YACP,CAAC;YAED,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC7B,CAAC;QAED,IAAI,CAAC,QAAQ,IAAI,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YAC5C,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;QAC1B,CAAC;IACF,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ac-kit/algo",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.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,8 +45,8 @@
|
|
|
45
45
|
"prepack": "yarn build"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@ac-kit/core": "^0.
|
|
49
|
-
"@ac-kit/data": "^0.1.
|
|
48
|
+
"@ac-kit/core": "^0.3.0",
|
|
49
|
+
"@ac-kit/data": "^0.1.2",
|
|
50
50
|
"@types/web": "^0.0.354"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|