@ac-kit/data 0.5.0 → 0.6.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 +28 -0
- package/README.md +3 -2
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/probabilistic/{top-k.d.ts → heavy-hitters.d.ts} +7 -5
- package/dist/probabilistic/heavy-hitters.d.ts.map +1 -0
- package/dist/probabilistic/{top-k.js → heavy-hitters.js} +5 -3
- package/dist/probabilistic/heavy-hitters.js.map +1 -0
- package/dist/range/_range-checks.d.ts +9 -0
- package/dist/range/_range-checks.d.ts.map +1 -0
- package/dist/range/_range-checks.js +28 -0
- package/dist/range/_range-checks.js.map +1 -0
- package/dist/range/fenwick-tree.d.ts +82 -0
- package/dist/range/fenwick-tree.d.ts.map +1 -0
- package/dist/range/fenwick-tree.js +173 -0
- package/dist/range/fenwick-tree.js.map +1 -0
- package/dist/range/irange-aggregate.d.ts +22 -0
- package/dist/range/irange-aggregate.d.ts.map +1 -0
- package/dist/range/irange-aggregate.js +2 -0
- package/dist/range/irange-aggregate.js.map +1 -0
- package/dist/range/monoid.d.ts +38 -0
- package/dist/range/monoid.d.ts.map +1 -0
- package/dist/range/monoid.js +2 -0
- package/dist/range/monoid.js.map +1 -0
- package/dist/range/segment-tree.d.ts +91 -0
- package/dist/range/segment-tree.d.ts.map +1 -0
- package/dist/range/segment-tree.js +219 -0
- package/dist/range/segment-tree.js.map +1 -0
- package/package.json +4 -4
- package/dist/probabilistic/top-k.d.ts.map +0 -1
- package/dist/probabilistic/top-k.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
# @ac-kit/data
|
|
2
2
|
|
|
3
|
+
## 0.6.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 5c203e7: Range queries. `FenwickTree` answers prefix and range aggregates under point
|
|
8
|
+
updates in O(log n) over an `AbelianGroup` (`{ identity, combine, invert }`:
|
|
9
|
+
sum, XOR, `bigint` sum, …), with `FenwickTree.from(group, values)` building in
|
|
10
|
+
O(n) and `maxPrefix(predicate)` finding the longest prefix a monotone predicate
|
|
11
|
+
accepts. `SegmentTree` does the same over any `Monoid` (`{ identity, combine }`:
|
|
12
|
+
minimum, maximum, gcd, or a non-commutative operation combined in index order),
|
|
13
|
+
with an O(1) `get` and `all()`, and `maxRight(start, predicate)` /
|
|
14
|
+
`minLeft(end, predicate)` binary searches such as "the first element below a
|
|
15
|
+
threshold". Both implement `IRangeAggregate` (`length`, `get`, `set`,
|
|
16
|
+
`combineAt`, `query(start, end)` over half-open ranges, `toArray`) and throw a
|
|
17
|
+
`RangeError` on an index or range out of bounds.
|
|
18
|
+
|
|
19
|
+
`TopK` is renamed `HeavyHitters`, and `TopKOptions` `HeavyHittersOptions`, the
|
|
20
|
+
streaming literature's name for the problem; the old names are removed.
|
|
21
|
+
Migration: rename the imports.
|
|
22
|
+
|
|
23
|
+
### Patch Changes
|
|
24
|
+
|
|
25
|
+
- Updated dependencies [5c203e7]
|
|
26
|
+
- Updated dependencies [5c203e7]
|
|
27
|
+
- Updated dependencies [5c203e7]
|
|
28
|
+
- Updated dependencies [5c203e7]
|
|
29
|
+
- @ac-kit/core@0.14.0
|
|
30
|
+
|
|
3
31
|
## 0.5.0
|
|
4
32
|
|
|
5
33
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -12,8 +12,9 @@ indexes and probabilistic sketches.
|
|
|
12
12
|
| Trees | `BinarySearchTree`, `AvlTree`, `Trie` |
|
|
13
13
|
| Graphs | `Graph`, `DirectedGraph`, `StaticDisjointSet` |
|
|
14
14
|
| Indexes | `InvertedIndex`, `SuffixArray`, `GeneralizedSuffixArray`, `VpTree`, `StaticIntervalTree` |
|
|
15
|
+
| Range queries | `FenwickTree` (over an `AbelianGroup`), `SegmentTree` (over a `Monoid`) |
|
|
15
16
|
| Bit-packed | `BitSet`, `BitVector` |
|
|
16
|
-
| Probabilistic | `BloomFilter`, `CuckooFilter`, `CountMinSketch`, `HyperLogLog`, `
|
|
17
|
+
| Probabilistic | `BloomFilter`, `CuckooFilter`, `CountMinSketch`, `HyperLogLog`, `HeavyHitters` |
|
|
17
18
|
| Storage backings | `RingVector`, `TypedVector`, `TypedColumnRing` |
|
|
18
19
|
|
|
19
20
|
Each sequence-like abstract data type comes as a family of classes, one per
|
|
@@ -86,7 +87,7 @@ repeatedly.
|
|
|
86
87
|
|
|
87
88
|
## Approximate structures need a hash
|
|
88
89
|
|
|
89
|
-
`BloomFilter`, `CuckooFilter`, `CountMinSketch`, `HyperLogLog` and `
|
|
90
|
+
`BloomFilter`, `CuckooFilter`, `CountMinSketch`, `HyperLogLog` and `HeavyHitters` each
|
|
90
91
|
take a required `hash: Hash32<T>` as their first constructor argument. There is
|
|
91
92
|
no default, because
|
|
92
93
|
`@ac-kit/noncrypto-hash`'s `murmur3_32`, `xxhash_32`, `fnv_32` and `djb2` all
|
package/dist/index.d.ts
CHANGED
|
@@ -92,8 +92,8 @@ export * from "./probabilistic/bloom-filter.js";
|
|
|
92
92
|
export * from "./probabilistic/count-min-sketch.js";
|
|
93
93
|
export * from "./probabilistic/cuckoo-filter.js";
|
|
94
94
|
export * from "./probabilistic/hash32.js";
|
|
95
|
+
export * from "./probabilistic/heavy-hitters.js";
|
|
95
96
|
export * from "./probabilistic/hyper-log-log.js";
|
|
96
|
-
export * from "./probabilistic/top-k.js";
|
|
97
97
|
export * from "./queue/blocking-queue.js";
|
|
98
98
|
export * from "./queue/bounded-queue.js";
|
|
99
99
|
export * from "./queue/iasync-lossy-queue.js";
|
|
@@ -106,6 +106,10 @@ export * from "./queue/iweight-bounded-queue.js";
|
|
|
106
106
|
export * from "./queue/lossy-queue.js";
|
|
107
107
|
export * from "./queue/queue.js";
|
|
108
108
|
export * from "./queue/weight-bounded-queue.js";
|
|
109
|
+
export * from "./range/fenwick-tree.js";
|
|
110
|
+
export * from "./range/irange-aggregate.js";
|
|
111
|
+
export * from "./range/monoid.js";
|
|
112
|
+
export * from "./range/segment-tree.js";
|
|
109
113
|
export * from "./set/enhanced-set.js";
|
|
110
114
|
export * from "./set/iasync-replacing-set.js";
|
|
111
115
|
export * from "./set/iasync-set.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,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,+CAA+C,CAAC;AAC9D,cAAc,mCAAmC,CAAC;AAClD,cAAc,mCAAmC,CAAC;AAClD,cAAc,0BAA0B,CAAC;AACzC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,wBAAwB,CAAC;AACvC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,4CAA4C,CAAC;AAC3D,cAAc,mCAAmC,CAAC;AAClD,cAAc,8BAA8B,CAAC;AAC7C,cAAc,qBAAqB,CAAC;AACpC,cAAc,mCAAmC,CAAC;AAClD,cAAc,mCAAmC,CAAC;AAClD,cAAc,2BAA2B,CAAC;AAC1C,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,yBAAyB,CAAC;AACxC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,mBAAmB,CAAC;AAClC,cAAc,yBAAyB,CAAC;AACxC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,uCAAuC,CAAC;AACtD,cAAc,iCAAiC,CAAC;AAChD,cAAc,uCAAuC,CAAC;AACtD,cAAc,sBAAsB,CAAC;AACrC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,2BAA2B,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;AACrC,cAAc,yCAAyC,CAAC;AACxD,cAAc,mCAAmC,CAAC;AAClD,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,0BAA0B,CAAC;AACzC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,yBAAyB,CAAC;AACxC,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,mBAAmB,CAAC;AAClC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,kBAAkB,CAAC;AACjC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,iCAAiC,CAAC;AAChD,cAAc,iCAAiC,CAAC;AAChD,cAAc,kCAAkC,CAAC;AACjD,cAAc,uBAAuB,CAAC;AACtC,cAAc,0BAA0B,CAAC;AACzC,cAAc,oCAAoC,CAAC;AACnD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,yCAAyC,CAAC;AACxD,cAAc,uCAAuC,CAAC;AACtD,cAAc,iCAAiC,CAAC;AAChD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oBAAoB,CAAC;AACnC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,0BAA0B,CAAC;AACzC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -92,8 +92,8 @@ export * from "./probabilistic/bloom-filter.js";
|
|
|
92
92
|
export * from "./probabilistic/count-min-sketch.js";
|
|
93
93
|
export * from "./probabilistic/cuckoo-filter.js";
|
|
94
94
|
export * from "./probabilistic/hash32.js";
|
|
95
|
+
export * from "./probabilistic/heavy-hitters.js";
|
|
95
96
|
export * from "./probabilistic/hyper-log-log.js";
|
|
96
|
-
export * from "./probabilistic/top-k.js";
|
|
97
97
|
export * from "./queue/blocking-queue.js";
|
|
98
98
|
export * from "./queue/bounded-queue.js";
|
|
99
99
|
export * from "./queue/iasync-lossy-queue.js";
|
|
@@ -106,6 +106,10 @@ export * from "./queue/iweight-bounded-queue.js";
|
|
|
106
106
|
export * from "./queue/lossy-queue.js";
|
|
107
107
|
export * from "./queue/queue.js";
|
|
108
108
|
export * from "./queue/weight-bounded-queue.js";
|
|
109
|
+
export * from "./range/fenwick-tree.js";
|
|
110
|
+
export * from "./range/irange-aggregate.js";
|
|
111
|
+
export * from "./range/monoid.js";
|
|
112
|
+
export * from "./range/segment-tree.js";
|
|
109
113
|
export * from "./set/enhanced-set.js";
|
|
110
114
|
export * from "./set/iasync-replacing-set.js";
|
|
111
115
|
export * from "./set/iasync-set.js";
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,+CAA+C,CAAC;AAC9D,cAAc,mCAAmC,CAAC;AAClD,cAAc,mCAAmC,CAAC;AAClD,cAAc,0BAA0B,CAAC;AACzC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,wBAAwB,CAAC;AACvC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,4CAA4C,CAAC;AAC3D,cAAc,mCAAmC,CAAC;AAClD,cAAc,8BAA8B,CAAC;AAC7C,cAAc,qBAAqB,CAAC;AACpC,cAAc,mCAAmC,CAAC;AAClD,cAAc,mCAAmC,CAAC;AAClD,cAAc,2BAA2B,CAAC;AAC1C,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,yBAAyB,CAAC;AACxC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,m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CAAC;AAClC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,qBAAqB,CAAC;AACpC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,yBAAyB,CAAC;AACxC,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAC;AACrC,cAAc,yCAAyC,CAAC;AACxD,cAAc,mCAAmC,CAAC;AAClD,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,0BAA0B,CAAC;AACzC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,yBAAyB,CAAC;AACxC,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,mBAAmB,CAAC;AAClC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,kBAAkB,CAAC;AACjC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,iCAAiC,CAAC;AAChD,cAAc,iCAAiC,CAAC;AAChD,cAAc,kCAAkC,CAAC;AACjD,cAAc,uBAAuB,CAAC;AACtC,cAAc,0BAA0B,CAAC;AACzC,cAAc,oCAAoC,CAAC;AACnD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,yCAAyC,CAAC;AACxD,cAAc,uCAAuC,CAAC;AACtD,cAAc,iCAAiC,CAAC;AAChD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oBAAoB,CAAC;AACnC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,0BAA0B,CAAC;AACzC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC"}
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { type CountMinSketchOptions } from "./count-min-sketch.js";
|
|
2
2
|
import type { Hash32 } from "./hash32.js";
|
|
3
|
-
export type
|
|
3
|
+
export type HeavyHittersOptions = CountMinSketchOptions;
|
|
4
4
|
/** A tracked item and the frequency estimated for it. */
|
|
5
5
|
export type HeavyHitter<T> = {
|
|
6
6
|
readonly item: T;
|
|
7
7
|
readonly count: number;
|
|
8
8
|
};
|
|
9
9
|
/**
|
|
10
|
-
* Approximate heavy hitters: the `k` most frequent items in a stream
|
|
10
|
+
* Approximate heavy hitters: the `k` most frequent items in a stream — what the
|
|
11
|
+
* streaming literature calls the heavy-hitters problem. For the exact `k` best
|
|
12
|
+
* items under a comparator, see `@ac-kit/algo` instead.
|
|
11
13
|
*
|
|
12
14
|
* A {@link CountMinSketch} estimates every item's frequency in fixed space; on
|
|
13
15
|
* its own it cannot say _which_ items are frequent, because it never stores
|
|
@@ -25,7 +27,7 @@ export type HeavyHitter<T> = {
|
|
|
25
27
|
*
|
|
26
28
|
* @template T The item type.
|
|
27
29
|
*/
|
|
28
|
-
export declare class
|
|
30
|
+
export declare class HeavyHitters<T> {
|
|
29
31
|
private readonly sketch;
|
|
30
32
|
/** Candidates by item, holding the estimate at the time of last update. */
|
|
31
33
|
private readonly candidates;
|
|
@@ -36,7 +38,7 @@ export declare class TopK<T> {
|
|
|
36
38
|
* @param k How many heavy hitters to track.
|
|
37
39
|
* @param options Sizing of the underlying {@link CountMinSketch}.
|
|
38
40
|
*/
|
|
39
|
-
constructor(hash: Hash32<T>, k: number, options?:
|
|
41
|
+
constructor(hash: Hash32<T>, k: number, options?: HeavyHittersOptions);
|
|
40
42
|
/** The total of every count added, exact. */
|
|
41
43
|
total(): number;
|
|
42
44
|
/** How many candidates are currently tracked, at most `k`. */
|
|
@@ -59,4 +61,4 @@ export declare class TopK<T> {
|
|
|
59
61
|
heavyHitters(): HeavyHitter<T>[];
|
|
60
62
|
private weakestCandidate;
|
|
61
63
|
}
|
|
62
|
-
//# sourceMappingURL=
|
|
64
|
+
//# sourceMappingURL=heavy-hitters.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3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Y,IAAI,WAAW,CAAC,CAAC,CAAC,EAAE,CAU/B;IAED,OAAO,CAAC,gBAAgB;CAWxB"}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { describeValue } from "@ac-kit/core";
|
|
2
2
|
import { CountMinSketch, } from "./count-min-sketch.js";
|
|
3
3
|
/**
|
|
4
|
-
* Approximate heavy hitters: the `k` most frequent items in a stream
|
|
4
|
+
* Approximate heavy hitters: the `k` most frequent items in a stream — what the
|
|
5
|
+
* streaming literature calls the heavy-hitters problem. For the exact `k` best
|
|
6
|
+
* items under a comparator, see `@ac-kit/algo` instead.
|
|
5
7
|
*
|
|
6
8
|
* A {@link CountMinSketch} estimates every item's frequency in fixed space; on
|
|
7
9
|
* its own it cannot say _which_ items are frequent, because it never stores
|
|
@@ -19,7 +21,7 @@ import { CountMinSketch, } from "./count-min-sketch.js";
|
|
|
19
21
|
*
|
|
20
22
|
* @template T The item type.
|
|
21
23
|
*/
|
|
22
|
-
export class
|
|
24
|
+
export class HeavyHitters {
|
|
23
25
|
sketch;
|
|
24
26
|
/** Candidates by item, holding the estimate at the time of last update. */
|
|
25
27
|
candidates = new Map();
|
|
@@ -107,4 +109,4 @@ export class TopK {
|
|
|
107
109
|
return weakest;
|
|
108
110
|
}
|
|
109
111
|
}
|
|
110
|
-
//# sourceMappingURL=
|
|
112
|
+
//# sourceMappingURL=heavy-hitters.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"heavy-hitters.js","sourceRoot":"","sources":["../../src/probabilistic/heavy-hitters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,OAAO,EACN,cAAc,GAEd,MAAM,uBAAuB,CAAC;AAW/B;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,OAAO,YAAY;IACP,MAAM,CAAoB;IAC3C,2EAA2E;IAC1D,UAAU,GAAG,IAAI,GAAG,EAAa,CAAC;IAEnD,0CAA0C;IACj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AEO,gBAAgB;QACvB,IAAI,OAAyC,CAAC;QAE9C,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;YAC7C,IAAI,OAAO,KAAK,SAAS,IAAI,KAAK,GAAG,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;gBACjD,OAAO,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;YACzB,CAAC;QACF,CAAC;QAED,OAAO,OAAO,CAAC;IAChB,CAAC;CACD"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** @internal */
|
|
2
|
+
export declare function checkLength_(length: number, maximum: number): void;
|
|
3
|
+
/** @internal */
|
|
4
|
+
export declare function checkIndex_(index: number, length: number): void;
|
|
5
|
+
/** @internal */
|
|
6
|
+
export declare function checkBound_(bound: number, length: number): void;
|
|
7
|
+
/** @internal */
|
|
8
|
+
export declare function checkRange_(start: number, end: number, length: number): void;
|
|
9
|
+
//# sourceMappingURL=_range-checks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_range-checks.d.ts","sourceRoot":"","sources":["../../src/range/_range-checks.ts"],"names":[],"mappings":"AAEA,gBAAgB;AAChB,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAMlE;AAED,gBAAgB;AAChB,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAM/D;AAED,gBAAgB;AAChB,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAM/D;AAED,gBAAgB;AAChB,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAO5E"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { describeValue } from "@ac-kit/core";
|
|
2
|
+
/** @internal */
|
|
3
|
+
export function checkLength_(length, maximum) {
|
|
4
|
+
if (!Number.isInteger(length) || length < 0 || length > maximum) {
|
|
5
|
+
throw new RangeError(`length must be an integer in [0, ${maximum}], got ${describeValue(length)}`);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
/** @internal */
|
|
9
|
+
export function checkIndex_(index, length) {
|
|
10
|
+
if (!Number.isInteger(index) || index < 0 || index >= length) {
|
|
11
|
+
throw new RangeError(`index must be an integer in [0, ${length}), got ${describeValue(index)}`);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/** @internal */
|
|
15
|
+
export function checkBound_(bound, length) {
|
|
16
|
+
if (!Number.isInteger(bound) || bound < 0 || bound > length) {
|
|
17
|
+
throw new RangeError(`range bound must be an integer in [0, ${length}], got ${describeValue(bound)}`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** @internal */
|
|
21
|
+
export function checkRange_(start, end, length) {
|
|
22
|
+
checkBound_(start, length);
|
|
23
|
+
checkBound_(end, length);
|
|
24
|
+
if (start > end) {
|
|
25
|
+
throw new RangeError(`range start ${start} is after its end ${end}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=_range-checks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_range-checks.js","sourceRoot":"","sources":["../../src/range/_range-checks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,gBAAgB;AAChB,MAAM,UAAU,YAAY,CAAC,MAAc,EAAE,OAAe;IAC3D,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,MAAM,GAAG,CAAC,IAAI,MAAM,GAAG,OAAO,EAAE,CAAC;QACjE,MAAM,IAAI,UAAU,CACnB,oCAAoC,OAAO,UAAU,aAAa,CAAC,MAAM,CAAC,EAAE,CAC5E,CAAC;IACH,CAAC;AACF,CAAC;AAED,gBAAgB;AAChB,MAAM,UAAU,WAAW,CAAC,KAAa,EAAE,MAAc;IACxD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,IAAI,KAAK,IAAI,MAAM,EAAE,CAAC;QAC9D,MAAM,IAAI,UAAU,CACnB,mCAAmC,MAAM,UAAU,aAAa,CAAC,KAAK,CAAC,EAAE,CACzE,CAAC;IACH,CAAC;AACF,CAAC;AAED,gBAAgB;AAChB,MAAM,UAAU,WAAW,CAAC,KAAa,EAAE,MAAc;IACxD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,IAAI,KAAK,GAAG,MAAM,EAAE,CAAC;QAC7D,MAAM,IAAI,UAAU,CACnB,yCAAyC,MAAM,UAAU,aAAa,CAAC,KAAK,CAAC,EAAE,CAC/E,CAAC;IACH,CAAC;AACF,CAAC;AAED,gBAAgB;AAChB,MAAM,UAAU,WAAW,CAAC,KAAa,EAAE,GAAW,EAAE,MAAc;IACrE,WAAW,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAC3B,WAAW,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IAEzB,IAAI,KAAK,GAAG,GAAG,EAAE,CAAC;QACjB,MAAM,IAAI,UAAU,CAAC,eAAe,KAAK,qBAAqB,GAAG,EAAE,CAAC,CAAC;IACtE,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { type Predicate } from "@ac-kit/core";
|
|
2
|
+
import type { IRangeAggregate } from "./irange-aggregate.js";
|
|
3
|
+
import type { AbelianGroup } from "./monoid.js";
|
|
4
|
+
/**
|
|
5
|
+
* A Fenwick tree (binary indexed tree): prefix and range aggregates under point
|
|
6
|
+
* updates, O(log n) each, over an {@link AbelianGroup} — sum, XOR, sum over
|
|
7
|
+
* `bigint`, or any operation with an inverse. It needs only n + 1 slots and a
|
|
8
|
+
* few additions per operation, half the memory of a {@link SegmentTree}, which
|
|
9
|
+
* is the one to use for an operation without an inverse (minimum, maximum, gcd)
|
|
10
|
+
* or a non-commutative one.
|
|
11
|
+
*
|
|
12
|
+
* A range is answered as `prefix(end)` with `prefix(start)` removed, so over
|
|
13
|
+
* floating-point numbers it carries the rounding of both prefixes.
|
|
14
|
+
*
|
|
15
|
+
* At most 2³¹ − 1 elements.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const sums = FenwickTree.from(
|
|
19
|
+
* { identity: 0, combine: (a, b) => a + b, invert: (a) => -a },
|
|
20
|
+
* [3, 1, 4, 1, 5],
|
|
21
|
+
* );
|
|
22
|
+
* sums.query(1, 4); // 6
|
|
23
|
+
* sums.combineAt(2, 10); // the element at 2 is now 14
|
|
24
|
+
* sums.maxPrefix((sum) => sum <= 18); // 3: prefixes 0, 3, 4, 18, 19, 24
|
|
25
|
+
*
|
|
26
|
+
* @template T The element type.
|
|
27
|
+
*/
|
|
28
|
+
export declare class FenwickTree<T> implements IRangeAggregate<T> {
|
|
29
|
+
private readonly group;
|
|
30
|
+
private readonly tree;
|
|
31
|
+
readonly length: number;
|
|
32
|
+
/**
|
|
33
|
+
* A tree of `length` elements, each the group's identity. O(n).
|
|
34
|
+
*
|
|
35
|
+
* @throws {RangeError} If `length` is not an integer in [0, 2³¹ − 1].
|
|
36
|
+
*/
|
|
37
|
+
constructor(group: AbelianGroup<T>, length: number);
|
|
38
|
+
/**
|
|
39
|
+
* A tree over `values`, in order. O(n), not O(n log n): each slot is pushed
|
|
40
|
+
* once into its parent.
|
|
41
|
+
*
|
|
42
|
+
* @throws {RangeError} If there are more than 2³¹ − 1 values.
|
|
43
|
+
*/
|
|
44
|
+
static from<T>(group: AbelianGroup<T>, values: readonly T[]): FenwickTree<T>;
|
|
45
|
+
/** The element at `index`. O(log n). */
|
|
46
|
+
get(index: number): T;
|
|
47
|
+
/** Replaces the element at `index`. O(log n). */
|
|
48
|
+
set(index: number, value: T): void;
|
|
49
|
+
/**
|
|
50
|
+
* Combines `value` into the element at `index` — adds it, for a sum. O(log
|
|
51
|
+
* n).
|
|
52
|
+
*/
|
|
53
|
+
combineAt(index: number, value: T): void;
|
|
54
|
+
/** The combination of the elements of `[0, end)`. O(log n). */
|
|
55
|
+
prefix(end: number): T;
|
|
56
|
+
/**
|
|
57
|
+
* The combination of the elements of `[start, end)`: `prefix(end)` with
|
|
58
|
+
* `prefix(start)` removed. O(log n).
|
|
59
|
+
*
|
|
60
|
+
* @throws {RangeError} Unless `0 ≤ start ≤ end ≤ length`, integers.
|
|
61
|
+
*/
|
|
62
|
+
query(start: number, end: number): T;
|
|
63
|
+
/**
|
|
64
|
+
* The largest `end` in `[0, length]` for which `predicate(prefix(end))`
|
|
65
|
+
* holds, by binary lifting over the tree in O(log n) calls — for a sum of
|
|
66
|
+
* non-negative weights, the number of leading elements that fit a budget, or
|
|
67
|
+
* the index a cumulative-weight sample lands on.
|
|
68
|
+
*
|
|
69
|
+
* `predicate` must be monotone along the prefixes: once false, false for
|
|
70
|
+
* every longer prefix. Otherwise the answer is some `end` where it holds and
|
|
71
|
+
* fails at `end + 1`, not necessarily the largest.
|
|
72
|
+
*
|
|
73
|
+
* @throws {RangeError} If `predicate` rejects the identity, the empty prefix:
|
|
74
|
+
* no `end` satisfies it.
|
|
75
|
+
*/
|
|
76
|
+
maxPrefix(predicate: Predicate<[aggregate: T]>): number;
|
|
77
|
+
/** Every element, in index order. O(n): the construction run backwards. */
|
|
78
|
+
toArray(): T[];
|
|
79
|
+
private addAt_;
|
|
80
|
+
private prefix_;
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=fenwick-tree.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fenwick-tree.d.ts","sourceRoot":"","sources":["../../src/range/fenwick-tree.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,SAAS,EAAsB,MAAM,cAAc,CAAC;AAQlE,OAAO,KAA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|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { previousPowerOfTwo } from "@ac-kit/core";
|
|
2
|
+
import { checkBound_, checkIndex_, checkLength_, checkRange_, } from "./_range-checks.js";
|
|
3
|
+
// The lowest set bit, `i & -i`, is exact only on int32.
|
|
4
|
+
const MAX_LENGTH_ = 2 ** 31 - 1;
|
|
5
|
+
/**
|
|
6
|
+
* A Fenwick tree (binary indexed tree): prefix and range aggregates under point
|
|
7
|
+
* updates, O(log n) each, over an {@link AbelianGroup} — sum, XOR, sum over
|
|
8
|
+
* `bigint`, or any operation with an inverse. It needs only n + 1 slots and a
|
|
9
|
+
* few additions per operation, half the memory of a {@link SegmentTree}, which
|
|
10
|
+
* is the one to use for an operation without an inverse (minimum, maximum, gcd)
|
|
11
|
+
* or a non-commutative one.
|
|
12
|
+
*
|
|
13
|
+
* A range is answered as `prefix(end)` with `prefix(start)` removed, so over
|
|
14
|
+
* floating-point numbers it carries the rounding of both prefixes.
|
|
15
|
+
*
|
|
16
|
+
* At most 2³¹ − 1 elements.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* const sums = FenwickTree.from(
|
|
20
|
+
* { identity: 0, combine: (a, b) => a + b, invert: (a) => -a },
|
|
21
|
+
* [3, 1, 4, 1, 5],
|
|
22
|
+
* );
|
|
23
|
+
* sums.query(1, 4); // 6
|
|
24
|
+
* sums.combineAt(2, 10); // the element at 2 is now 14
|
|
25
|
+
* sums.maxPrefix((sum) => sum <= 18); // 3: prefixes 0, 3, 4, 18, 19, 24
|
|
26
|
+
*
|
|
27
|
+
* @template T The element type.
|
|
28
|
+
*/
|
|
29
|
+
export class FenwickTree {
|
|
30
|
+
group;
|
|
31
|
+
// 1-based: slot i holds the combination of the `i & -i` elements ending at
|
|
32
|
+
// element i - 1; slot 0 is unused.
|
|
33
|
+
tree;
|
|
34
|
+
length;
|
|
35
|
+
/**
|
|
36
|
+
* A tree of `length` elements, each the group's identity. O(n).
|
|
37
|
+
*
|
|
38
|
+
* @throws {RangeError} If `length` is not an integer in [0, 2³¹ − 1].
|
|
39
|
+
*/
|
|
40
|
+
constructor(group, length) {
|
|
41
|
+
this.group = group;
|
|
42
|
+
checkLength_(length, MAX_LENGTH_);
|
|
43
|
+
this.length = length;
|
|
44
|
+
this.tree = [];
|
|
45
|
+
for (let slot = 0; slot <= length; slot++) {
|
|
46
|
+
this.tree.push(group.identity);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* A tree over `values`, in order. O(n), not O(n log n): each slot is pushed
|
|
51
|
+
* once into its parent.
|
|
52
|
+
*
|
|
53
|
+
* @throws {RangeError} If there are more than 2³¹ − 1 values.
|
|
54
|
+
*/
|
|
55
|
+
static from(group, values) {
|
|
56
|
+
const length = values.length;
|
|
57
|
+
const fenwick = new FenwickTree(group, length);
|
|
58
|
+
const tree = fenwick.tree;
|
|
59
|
+
for (let index = 0; index < length; index++) {
|
|
60
|
+
tree[index + 1] = values[index];
|
|
61
|
+
}
|
|
62
|
+
for (let slot = 1; slot <= length; slot++) {
|
|
63
|
+
const parent = slot + (slot & -slot);
|
|
64
|
+
if (parent <= length) {
|
|
65
|
+
tree[parent] = group.combine(tree[parent], tree[slot]);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return fenwick;
|
|
69
|
+
}
|
|
70
|
+
/** The element at `index`. O(log n). */
|
|
71
|
+
get(index) {
|
|
72
|
+
checkIndex_(index, this.length);
|
|
73
|
+
const group = this.group;
|
|
74
|
+
const slot = index + 1;
|
|
75
|
+
const stop = slot - (slot & -slot);
|
|
76
|
+
let value = this.tree[slot];
|
|
77
|
+
for (let child = index; child > stop; child -= child & -child) {
|
|
78
|
+
value = group.combine(value, group.invert(this.tree[child]));
|
|
79
|
+
}
|
|
80
|
+
return value;
|
|
81
|
+
}
|
|
82
|
+
/** Replaces the element at `index`. O(log n). */
|
|
83
|
+
set(index, value) {
|
|
84
|
+
const current = this.get(index);
|
|
85
|
+
this.addAt_(index, this.group.combine(value, this.group.invert(current)));
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Combines `value` into the element at `index` — adds it, for a sum. O(log
|
|
89
|
+
* n).
|
|
90
|
+
*/
|
|
91
|
+
combineAt(index, value) {
|
|
92
|
+
checkIndex_(index, this.length);
|
|
93
|
+
this.addAt_(index, value);
|
|
94
|
+
}
|
|
95
|
+
/** The combination of the elements of `[0, end)`. O(log n). */
|
|
96
|
+
prefix(end) {
|
|
97
|
+
checkBound_(end, this.length);
|
|
98
|
+
return this.prefix_(end);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The combination of the elements of `[start, end)`: `prefix(end)` with
|
|
102
|
+
* `prefix(start)` removed. O(log n).
|
|
103
|
+
*
|
|
104
|
+
* @throws {RangeError} Unless `0 ≤ start ≤ end ≤ length`, integers.
|
|
105
|
+
*/
|
|
106
|
+
query(start, end) {
|
|
107
|
+
checkRange_(start, end, this.length);
|
|
108
|
+
const group = this.group;
|
|
109
|
+
return group.combine(this.prefix_(end), group.invert(this.prefix_(start)));
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The largest `end` in `[0, length]` for which `predicate(prefix(end))`
|
|
113
|
+
* holds, by binary lifting over the tree in O(log n) calls — for a sum of
|
|
114
|
+
* non-negative weights, the number of leading elements that fit a budget, or
|
|
115
|
+
* the index a cumulative-weight sample lands on.
|
|
116
|
+
*
|
|
117
|
+
* `predicate` must be monotone along the prefixes: once false, false for
|
|
118
|
+
* every longer prefix. Otherwise the answer is some `end` where it holds and
|
|
119
|
+
* fails at `end + 1`, not necessarily the largest.
|
|
120
|
+
*
|
|
121
|
+
* @throws {RangeError} If `predicate` rejects the identity, the empty prefix:
|
|
122
|
+
* no `end` satisfies it.
|
|
123
|
+
*/
|
|
124
|
+
maxPrefix(predicate) {
|
|
125
|
+
const group = this.group;
|
|
126
|
+
if (!predicate(group.identity)) {
|
|
127
|
+
throw new RangeError("predicate must accept the identity");
|
|
128
|
+
}
|
|
129
|
+
if (this.length === 0) {
|
|
130
|
+
return 0;
|
|
131
|
+
}
|
|
132
|
+
let end = 0;
|
|
133
|
+
let aggregate = group.identity;
|
|
134
|
+
for (let step = previousPowerOfTwo(this.length); step > 0; step >>>= 1) {
|
|
135
|
+
const next = end + step;
|
|
136
|
+
if (next <= this.length) {
|
|
137
|
+
const candidate = group.combine(aggregate, this.tree[next]);
|
|
138
|
+
if (predicate(candidate)) {
|
|
139
|
+
end = next;
|
|
140
|
+
aggregate = candidate;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return end;
|
|
145
|
+
}
|
|
146
|
+
/** Every element, in index order. O(n): the construction run backwards. */
|
|
147
|
+
toArray() {
|
|
148
|
+
const group = this.group;
|
|
149
|
+
const values = this.tree.slice(1);
|
|
150
|
+
for (let slot = this.length; slot >= 1; slot--) {
|
|
151
|
+
const parent = slot + (slot & -slot);
|
|
152
|
+
if (parent <= this.length) {
|
|
153
|
+
values[parent - 1] = group.combine(values[parent - 1], group.invert(values[slot - 1]));
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return values;
|
|
157
|
+
}
|
|
158
|
+
addAt_(index, value) {
|
|
159
|
+
const group = this.group;
|
|
160
|
+
for (let slot = index + 1; slot <= this.length; slot += slot & -slot) {
|
|
161
|
+
this.tree[slot] = group.combine(this.tree[slot], value);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
prefix_(end) {
|
|
165
|
+
const group = this.group;
|
|
166
|
+
let aggregate = group.identity;
|
|
167
|
+
for (let slot = end; slot > 0; slot -= slot & -slot) {
|
|
168
|
+
aggregate = group.combine(aggregate, this.tree[slot]);
|
|
169
|
+
}
|
|
170
|
+
return aggregate;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
//# sourceMappingURL=fenwick-tree.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fenwick-tree.js","sourceRoot":"","sources":["../../src/range/f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AAE,IAAI,IAAI,IAAI,GAAG,CAAC,IAAI,EAAE,CAAC;YACtE,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAE,EAAE,KAAK,CAAC,CAAC;QAC1D,CAAC;IACF,CAAC;IAEO,OAAO,CAAC,GAAW;QAC1B,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;QACzB,IAAI,SAAS,GAAG,KAAK,CAAC,QAAQ,CAAC;QAE/B,KAAK,IAAI,IAAI,GAAG,GAAG,EAAE,IAAI,GAAG,CAAC,EAAE,IAAI,IAAI,IAAI,GAAG,CAAC,IAAI,EAAE,CAAC;YACrD,SAAS,GAAG,KAAK,CAAC,OAAO,CAAC,SAAS,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,CAAE,CAAC,CAAC;QACxD,CAAC;QAED,OAAO,SAAS,CAAC;IAClB,CAAC;CACD"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A fixed-length sequence answering the aggregate of any contiguous range under
|
|
3
|
+
* point updates — what {@link FenwickTree} and {@link SegmentTree} share. Ranges
|
|
4
|
+
* are half-open, `[start, end)`, with `0 ≤ start ≤ end ≤ length`.
|
|
5
|
+
*
|
|
6
|
+
* @template T The element type.
|
|
7
|
+
*/
|
|
8
|
+
export interface IRangeAggregate<T> {
|
|
9
|
+
/** The number of elements, fixed at construction. */
|
|
10
|
+
readonly length: number;
|
|
11
|
+
/** The element at `index`. */
|
|
12
|
+
get(index: number): T;
|
|
13
|
+
/** Replaces the element at `index`. */
|
|
14
|
+
set(index: number, value: T): void;
|
|
15
|
+
/** Replaces the element at `index` with `combine(element, value)`. */
|
|
16
|
+
combineAt(index: number, value: T): void;
|
|
17
|
+
/** The combination of the elements of `[start, end)`, in index order. */
|
|
18
|
+
query(start: number, end: number): T;
|
|
19
|
+
/** Every element, in index order, in a new array. O(n). */
|
|
20
|
+
toArray(): T[];
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=irange-aggregate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"irange-aggregate.d.ts","sourceRoot":"","sources":["../../src/range/irange-aggregate.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC;IACjC,qDAAqD;IACrD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAExB,8BAA8B;IAC9B,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,CAAC,CAAC;IAEtB,uCAAuC;IACvC,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;IAEnC,sEAAsE;IACtE,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;IAEzC,yEAAyE;IACzE,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,CAAC;IAErC,2DAA2D;IAC3D,OAAO,IAAI,CAAC,EAAE,CAAC;CACf"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"irange-aggregate.js","sourceRoot":"","sources":["../../src/range/irange-aggregate.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A monoid over `T`: an associative `combine` with an `identity` element — the
|
|
3
|
+
* evidence a {@link SegmentTree} needs. Sum (`+`, `0`), minimum (`Math.min`,
|
|
4
|
+
* `Infinity`), maximum, gcd (`gcd`, `0`), string concatenation (`+`, `""`) and
|
|
5
|
+
* matrix product are all monoids; `combine` need not be commutative.
|
|
6
|
+
*
|
|
7
|
+
* TypeScript cannot check the laws: `combine(combine(a, b), c)` must equal
|
|
8
|
+
* `combine(a, combine(b, c))`, and `combine(identity, a)` and `combine(a,
|
|
9
|
+
* identity)` must equal `a`. A structure built over an operation breaking them
|
|
10
|
+
* answers wrongly, without an error.
|
|
11
|
+
*
|
|
12
|
+
* @template T The element type.
|
|
13
|
+
*/
|
|
14
|
+
export type Monoid<T> = {
|
|
15
|
+
/** The identity: what an empty range aggregates to. */
|
|
16
|
+
readonly identity: T;
|
|
17
|
+
/** The associative operation, `left` before `right` in index order. */
|
|
18
|
+
combine(left: T, right: T): T;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* An abelian group over `T`: a commutative {@link Monoid} where every element
|
|
22
|
+
* has an inverse — the evidence a {@link FenwickTree} needs, which answers a
|
|
23
|
+
* range by removing one prefix from another. Sum over numbers (`+`, `0`,
|
|
24
|
+
* negation), sum over `bigint`, and XOR (`^`, `0`, the value itself) are
|
|
25
|
+
* groups; minimum and maximum are not.
|
|
26
|
+
*
|
|
27
|
+
* Beyond the monoid laws, `combine(a, b)` must equal `combine(b, a)` and
|
|
28
|
+
* `combine(a, invert(a))` must equal `identity`. Floating-point addition only
|
|
29
|
+
* approximates them: a range answered by subtraction carries the rounding of
|
|
30
|
+
* both prefixes.
|
|
31
|
+
*
|
|
32
|
+
* @template T The element type.
|
|
33
|
+
*/
|
|
34
|
+
export type AbelianGroup<T> = Monoid<T> & {
|
|
35
|
+
/** The inverse of `value`: `combine(value, invert(value))` is `identity`. */
|
|
36
|
+
invert(value: T): T;
|
|
37
|
+
};
|
|
38
|
+
//# sourceMappingURL=monoid.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"monoid.d.ts","sourceRoot":"","sources":["../../src/range/monoid.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,IAAI;IACvB,uDAAuD;IACvD,QAAQ,CAAC,QAAQ,EAAE,CAAC,CAAC;IAErB,uEAAuE;IACvE,OAAO,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;CAC9B,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,GAAG;IACzC,6EAA6E;IAC7E,MAAM,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;CACpB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"monoid.js","sourceRoot":"","sources":["../../src/range/monoid.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { type Predicate } from "@ac-kit/core";
|
|
2
|
+
import type { IRangeAggregate } from "./irange-aggregate.js";
|
|
3
|
+
import type { Monoid } from "./monoid.js";
|
|
4
|
+
/**
|
|
5
|
+
* A segment tree: range aggregates under point updates, O(log n) each, over any
|
|
6
|
+
* {@link Monoid} — minimum, maximum, sum, gcd, or a non-commutative operation
|
|
7
|
+
* such as string concatenation or matrix product, combined in index order.
|
|
8
|
+
* Requiring only associativity is what makes it more general than a
|
|
9
|
+
* {@link FenwickTree}, which needs an inverse and answers in half the memory.
|
|
10
|
+
*
|
|
11
|
+
* Beyond `query`, `maxRight` and `minLeft` binary-search over the tree in O(log
|
|
12
|
+
* n): the leftmost element at or after a position whose running aggregate
|
|
13
|
+
* breaks a condition — the first element above a threshold, the longest run
|
|
14
|
+
* fitting a budget.
|
|
15
|
+
*
|
|
16
|
+
* Iterative and bottom-up, over 2·2^⌈log₂ n⌉ slots. At most 2³⁰ elements.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* const minimum = SegmentTree.from(
|
|
20
|
+
* { identity: Infinity, combine: Math.min },
|
|
21
|
+
* [5, 3, 8, 6, 1, 9],
|
|
22
|
+
* );
|
|
23
|
+
* minimum.query(0, 4); // 3
|
|
24
|
+
* minimum.set(1, 7); // [5, 7, 8, 6, 1, 9]
|
|
25
|
+
* // The first element from index 0 that takes the minimum below 5:
|
|
26
|
+
* minimum.maxRight(0, (value) => value >= 5); // 4
|
|
27
|
+
*
|
|
28
|
+
* @template T The element type.
|
|
29
|
+
*/
|
|
30
|
+
export declare class SegmentTree<T> implements IRangeAggregate<T> {
|
|
31
|
+
private readonly monoid;
|
|
32
|
+
private readonly tree;
|
|
33
|
+
private readonly size;
|
|
34
|
+
readonly length: number;
|
|
35
|
+
/**
|
|
36
|
+
* A tree of `length` elements, each the monoid's identity. O(n).
|
|
37
|
+
*
|
|
38
|
+
* @throws {RangeError} If `length` is not an integer in [0, 2³⁰].
|
|
39
|
+
*/
|
|
40
|
+
constructor(monoid: Monoid<T>, length: number);
|
|
41
|
+
/**
|
|
42
|
+
* A tree over `values`, in order. O(n).
|
|
43
|
+
*
|
|
44
|
+
* @throws {RangeError} If there are more than 2³⁰ values.
|
|
45
|
+
*/
|
|
46
|
+
static from<T>(monoid: Monoid<T>, values: readonly T[]): SegmentTree<T>;
|
|
47
|
+
/** The element at `index`. O(1). */
|
|
48
|
+
get(index: number): T;
|
|
49
|
+
/** Replaces the element at `index`. O(log n). */
|
|
50
|
+
set(index: number, value: T): void;
|
|
51
|
+
/** Replaces the element at `index` with `combine(element, value)`. O(log n). */
|
|
52
|
+
combineAt(index: number, value: T): void;
|
|
53
|
+
/**
|
|
54
|
+
* The combination of the elements of `[start, end)`, in index order; the
|
|
55
|
+
* identity for an empty range. O(log n).
|
|
56
|
+
*
|
|
57
|
+
* @throws {RangeError} Unless `0 ≤ start ≤ end ≤ length`, integers.
|
|
58
|
+
*/
|
|
59
|
+
query(start: number, end: number): T;
|
|
60
|
+
/** The combination of every element, in index order. O(1). */
|
|
61
|
+
all(): T;
|
|
62
|
+
/**
|
|
63
|
+
* The largest `end` in `[start, length]` for which `predicate(query(start,
|
|
64
|
+
* end))` holds, in O(log n) calls — so `end` is the index of the first
|
|
65
|
+
* element whose inclusion breaks the predicate, or `length` if none does.
|
|
66
|
+
*
|
|
67
|
+
* `predicate` must be monotone as the range grows: once false, false for
|
|
68
|
+
* every longer range from `start`. Otherwise the answer is some `end` where
|
|
69
|
+
* it holds and fails at `end + 1`, not necessarily the largest.
|
|
70
|
+
*
|
|
71
|
+
* @throws {RangeError} If `start` is not an integer in `[0, length]`, or if
|
|
72
|
+
* `predicate` rejects the identity, the empty range.
|
|
73
|
+
*/
|
|
74
|
+
maxRight(start: number, predicate: Predicate<[aggregate: T]>): number;
|
|
75
|
+
/**
|
|
76
|
+
* The smallest `start` in `[0, end]` for which `predicate(query(start, end))`
|
|
77
|
+
* holds, in O(log n) calls — the mirror of {@link maxRight}, growing the range
|
|
78
|
+
* leftwards from `end`.
|
|
79
|
+
*
|
|
80
|
+
* `predicate` must be monotone as the range grows: once false, false for
|
|
81
|
+
* every longer range ending at `end`.
|
|
82
|
+
*
|
|
83
|
+
* @throws {RangeError} If `end` is not an integer in `[0, length]`, or if
|
|
84
|
+
* `predicate` rejects the identity, the empty range.
|
|
85
|
+
*/
|
|
86
|
+
minLeft(end: number, predicate: Predicate<[aggregate: T]>): number;
|
|
87
|
+
/** Every element, in index order. O(n). */
|
|
88
|
+
toArray(): T[];
|
|
89
|
+
private pull_;
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=segment-tree.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"segment-tree.d.ts","sourceRoot":"","sources":["../../src/range/segment-tree.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,SAAS,EAAkB,MAAM,cAAc,CAAC;AAQ9D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAO1C;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,qBAAa,WAAW,CAAC,CAAC,CAAE,YAAW,eAAe,CAAC,CAAC,CAAC;IAcvD,OAAO,CAAC,QAAQ,CAAC,MAAM;IAXxB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAM;IAC3B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAE9B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAExB;;;;OAIG;IACH,YACkB,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,EAClC,MAAM,EAAE,MAAM,EAWd;IAED;;;;OAIG;IACH,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,GAAG,WAAW,CAAC,CAAC,CAAC,CAatE;IAED,oCAAoC;IACpC,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,CAAC,CAIpB;IAED,iDAAiD;IACjD,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,CASjC;IAED,gFAAgF;IAChF,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,CAEvC;IAED;;;;;OAKG;IACH,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,CAyBnC;IAED,8DAA8D;IAC9D,GAAG,IAAI,CAAC,CAEP;IAED;;;;;;;;;;;OAWG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,GAAG,MAAM,CA4CpE;IAED;;;;;;;;;;OAUG;IACH,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,GAAG,MAAM,CA2CjE;IAED,2CAA2C;IAC3C,OAAO,IAAI,CAAC,EAAE,CAEb;IAED,OAAO,CAAC,KAAK;CAMb"}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { nextPowerOfTwo } from "@ac-kit/core";
|
|
2
|
+
import { checkBound_, checkIndex_, checkLength_, checkRange_, } from "./_range-checks.js";
|
|
3
|
+
// The tree holds twice the leaf count rounded up to a power of two: at most
|
|
4
|
+
// 2³¹ slots, within an array's maximum length, and node indices the int32 bit
|
|
5
|
+
// operations read right.
|
|
6
|
+
const MAX_LENGTH_ = 2 ** 30;
|
|
7
|
+
/**
|
|
8
|
+
* A segment tree: range aggregates under point updates, O(log n) each, over any
|
|
9
|
+
* {@link Monoid} — minimum, maximum, sum, gcd, or a non-commutative operation
|
|
10
|
+
* such as string concatenation or matrix product, combined in index order.
|
|
11
|
+
* Requiring only associativity is what makes it more general than a
|
|
12
|
+
* {@link FenwickTree}, which needs an inverse and answers in half the memory.
|
|
13
|
+
*
|
|
14
|
+
* Beyond `query`, `maxRight` and `minLeft` binary-search over the tree in O(log
|
|
15
|
+
* n): the leftmost element at or after a position whose running aggregate
|
|
16
|
+
* breaks a condition — the first element above a threshold, the longest run
|
|
17
|
+
* fitting a budget.
|
|
18
|
+
*
|
|
19
|
+
* Iterative and bottom-up, over 2·2^⌈log₂ n⌉ slots. At most 2³⁰ elements.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* const minimum = SegmentTree.from(
|
|
23
|
+
* { identity: Infinity, combine: Math.min },
|
|
24
|
+
* [5, 3, 8, 6, 1, 9],
|
|
25
|
+
* );
|
|
26
|
+
* minimum.query(0, 4); // 3
|
|
27
|
+
* minimum.set(1, 7); // [5, 7, 8, 6, 1, 9]
|
|
28
|
+
* // The first element from index 0 that takes the minimum below 5:
|
|
29
|
+
* minimum.maxRight(0, (value) => value >= 5); // 4
|
|
30
|
+
*
|
|
31
|
+
* @template T The element type.
|
|
32
|
+
*/
|
|
33
|
+
export class SegmentTree {
|
|
34
|
+
monoid;
|
|
35
|
+
// Leaves at [size, size + length), padded with the identity up to 2·size;
|
|
36
|
+
// node i combines nodes 2i and 2i + 1; node 0 is unused.
|
|
37
|
+
tree;
|
|
38
|
+
size;
|
|
39
|
+
length;
|
|
40
|
+
/**
|
|
41
|
+
* A tree of `length` elements, each the monoid's identity. O(n).
|
|
42
|
+
*
|
|
43
|
+
* @throws {RangeError} If `length` is not an integer in [0, 2³⁰].
|
|
44
|
+
*/
|
|
45
|
+
constructor(monoid, length) {
|
|
46
|
+
this.monoid = monoid;
|
|
47
|
+
checkLength_(length, MAX_LENGTH_);
|
|
48
|
+
this.length = length;
|
|
49
|
+
this.size = nextPowerOfTwo(length);
|
|
50
|
+
this.tree = [];
|
|
51
|
+
for (let node = 0; node < 2 * this.size; node++) {
|
|
52
|
+
this.tree.push(monoid.identity);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* A tree over `values`, in order. O(n).
|
|
57
|
+
*
|
|
58
|
+
* @throws {RangeError} If there are more than 2³⁰ values.
|
|
59
|
+
*/
|
|
60
|
+
static from(monoid, values) {
|
|
61
|
+
const segment = new SegmentTree(monoid, values.length);
|
|
62
|
+
const tree = segment.tree;
|
|
63
|
+
for (let index = 0; index < values.length; index++) {
|
|
64
|
+
tree[segment.size + index] = values[index];
|
|
65
|
+
}
|
|
66
|
+
for (let node = segment.size - 1; node >= 1; node--) {
|
|
67
|
+
segment.pull_(node);
|
|
68
|
+
}
|
|
69
|
+
return segment;
|
|
70
|
+
}
|
|
71
|
+
/** The element at `index`. O(1). */
|
|
72
|
+
get(index) {
|
|
73
|
+
checkIndex_(index, this.length);
|
|
74
|
+
return this.tree[this.size + index];
|
|
75
|
+
}
|
|
76
|
+
/** Replaces the element at `index`. O(log n). */
|
|
77
|
+
set(index, value) {
|
|
78
|
+
checkIndex_(index, this.length);
|
|
79
|
+
let node = this.size + index;
|
|
80
|
+
this.tree[node] = value;
|
|
81
|
+
for (node >>>= 1; node >= 1; node >>>= 1) {
|
|
82
|
+
this.pull_(node);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** Replaces the element at `index` with `combine(element, value)`. O(log n). */
|
|
86
|
+
combineAt(index, value) {
|
|
87
|
+
this.set(index, this.monoid.combine(this.get(index), value));
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The combination of the elements of `[start, end)`, in index order; the
|
|
91
|
+
* identity for an empty range. O(log n).
|
|
92
|
+
*
|
|
93
|
+
* @throws {RangeError} Unless `0 ≤ start ≤ end ≤ length`, integers.
|
|
94
|
+
*/
|
|
95
|
+
query(start, end) {
|
|
96
|
+
checkRange_(start, end, this.length);
|
|
97
|
+
const monoid = this.monoid;
|
|
98
|
+
let left = monoid.identity;
|
|
99
|
+
let right = monoid.identity;
|
|
100
|
+
let low = start + this.size;
|
|
101
|
+
let high = end + this.size;
|
|
102
|
+
while (low < high) {
|
|
103
|
+
if ((low & 1) === 1) {
|
|
104
|
+
left = monoid.combine(left, this.tree[low]);
|
|
105
|
+
low++;
|
|
106
|
+
}
|
|
107
|
+
if ((high & 1) === 1) {
|
|
108
|
+
high--;
|
|
109
|
+
right = monoid.combine(this.tree[high], right);
|
|
110
|
+
}
|
|
111
|
+
low >>>= 1;
|
|
112
|
+
high >>>= 1;
|
|
113
|
+
}
|
|
114
|
+
return monoid.combine(left, right);
|
|
115
|
+
}
|
|
116
|
+
/** The combination of every element, in index order. O(1). */
|
|
117
|
+
all() {
|
|
118
|
+
return this.tree[1];
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* The largest `end` in `[start, length]` for which `predicate(query(start,
|
|
122
|
+
* end))` holds, in O(log n) calls — so `end` is the index of the first
|
|
123
|
+
* element whose inclusion breaks the predicate, or `length` if none does.
|
|
124
|
+
*
|
|
125
|
+
* `predicate` must be monotone as the range grows: once false, false for
|
|
126
|
+
* every longer range from `start`. Otherwise the answer is some `end` where
|
|
127
|
+
* it holds and fails at `end + 1`, not necessarily the largest.
|
|
128
|
+
*
|
|
129
|
+
* @throws {RangeError} If `start` is not an integer in `[0, length]`, or if
|
|
130
|
+
* `predicate` rejects the identity, the empty range.
|
|
131
|
+
*/
|
|
132
|
+
maxRight(start, predicate) {
|
|
133
|
+
checkBound_(start, this.length);
|
|
134
|
+
const monoid = this.monoid;
|
|
135
|
+
if (!predicate(monoid.identity)) {
|
|
136
|
+
throw new RangeError("predicate must accept the identity");
|
|
137
|
+
}
|
|
138
|
+
if (start === this.length) {
|
|
139
|
+
return this.length;
|
|
140
|
+
}
|
|
141
|
+
let node = start + this.size;
|
|
142
|
+
let aggregate = monoid.identity;
|
|
143
|
+
do {
|
|
144
|
+
while ((node & 1) === 0) {
|
|
145
|
+
node >>>= 1;
|
|
146
|
+
}
|
|
147
|
+
const candidate = monoid.combine(aggregate, this.tree[node]);
|
|
148
|
+
if (!predicate(candidate)) {
|
|
149
|
+
while (node < this.size) {
|
|
150
|
+
node *= 2;
|
|
151
|
+
const left = monoid.combine(aggregate, this.tree[node]);
|
|
152
|
+
if (predicate(left)) {
|
|
153
|
+
aggregate = left;
|
|
154
|
+
node++;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return node - this.size;
|
|
158
|
+
}
|
|
159
|
+
aggregate = candidate;
|
|
160
|
+
node++;
|
|
161
|
+
// Stop once `node` is a power of two: the range reached the right
|
|
162
|
+
// edge of the tree. `node` reaches 2³¹ at most, where the int32
|
|
163
|
+
// `&` still reads it right.
|
|
164
|
+
} while ((node & (node - 1)) !== 0);
|
|
165
|
+
return this.length;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* The smallest `start` in `[0, end]` for which `predicate(query(start, end))`
|
|
169
|
+
* holds, in O(log n) calls — the mirror of {@link maxRight}, growing the range
|
|
170
|
+
* leftwards from `end`.
|
|
171
|
+
*
|
|
172
|
+
* `predicate` must be monotone as the range grows: once false, false for
|
|
173
|
+
* every longer range ending at `end`.
|
|
174
|
+
*
|
|
175
|
+
* @throws {RangeError} If `end` is not an integer in `[0, length]`, or if
|
|
176
|
+
* `predicate` rejects the identity, the empty range.
|
|
177
|
+
*/
|
|
178
|
+
minLeft(end, predicate) {
|
|
179
|
+
checkBound_(end, this.length);
|
|
180
|
+
const monoid = this.monoid;
|
|
181
|
+
if (!predicate(monoid.identity)) {
|
|
182
|
+
throw new RangeError("predicate must accept the identity");
|
|
183
|
+
}
|
|
184
|
+
if (end === 0) {
|
|
185
|
+
return 0;
|
|
186
|
+
}
|
|
187
|
+
let node = end + this.size;
|
|
188
|
+
let aggregate = monoid.identity;
|
|
189
|
+
do {
|
|
190
|
+
node--;
|
|
191
|
+
while (node > 1 && (node & 1) === 1) {
|
|
192
|
+
node >>>= 1;
|
|
193
|
+
}
|
|
194
|
+
const candidate = monoid.combine(this.tree[node], aggregate);
|
|
195
|
+
if (!predicate(candidate)) {
|
|
196
|
+
while (node < this.size) {
|
|
197
|
+
node = node * 2 + 1;
|
|
198
|
+
const right = monoid.combine(this.tree[node], aggregate);
|
|
199
|
+
if (predicate(right)) {
|
|
200
|
+
aggregate = right;
|
|
201
|
+
node--;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return node + 1 - this.size;
|
|
205
|
+
}
|
|
206
|
+
aggregate = candidate;
|
|
207
|
+
// A power of two: the range reached the left edge of the tree.
|
|
208
|
+
} while ((node & (node - 1)) !== 0);
|
|
209
|
+
return 0;
|
|
210
|
+
}
|
|
211
|
+
/** Every element, in index order. O(n). */
|
|
212
|
+
toArray() {
|
|
213
|
+
return this.tree.slice(this.size, this.size + this.length);
|
|
214
|
+
}
|
|
215
|
+
pull_(node) {
|
|
216
|
+
this.tree[node] = this.monoid.combine(this.tree[2 * node], this.tree[2 * node + 1]);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
//# sourceMappingURL=segment-tree.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"segment-tree.js","sourceRoot":"","sources":["../../src/range/segment-tree.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9D,OAAO,EACN,WAAW,EACX,WAAW,EACX,YAAY,EACZ,WAAW,GACX,MAAM,oBAAoB,CAAC;AAI5B,4EAA4E;AAC5E,8EAA8E;AAC9E,yBAAyB;AACzB,MAAM,WAAW,GAAG,CAAC,IAAI,EAAE,CAAC;AAE5B;;;;;;;;;;;;;;;;;;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I,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,OAAO,CACpC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,IAAI,CAAE,EACpB,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,IAAI,GAAG,CAAC,CAAE,CACxB,CAAC;IACH,CAAC;CACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ac-kit/data",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Generic data structures: sequences, associative collections, trees, graphs, indexes and probabilistic sketches.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"bloom-filter",
|
|
@@ -45,14 +45,14 @@
|
|
|
45
45
|
"prepack": "yarn build"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@ac-kit/core": "^0.
|
|
48
|
+
"@ac-kit/core": "^0.14.0",
|
|
49
49
|
"@types/web": "^0.0.367"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@ac-kit/math-random": "0.4.
|
|
52
|
+
"@ac-kit/math-random": "0.4.2",
|
|
53
53
|
"@ac-kit/tsconfig": "0.1.0",
|
|
54
54
|
"@ac-kit/typedoc-config": "0.1.0",
|
|
55
|
-
"@ac-kit/vitest-config": "0.
|
|
55
|
+
"@ac-kit/vitest-config": "0.3.0",
|
|
56
56
|
"@typescript/native": "typescript@7.0.2",
|
|
57
57
|
"rimraf": "6.1.4",
|
|
58
58
|
"typedoc": "0.28.20",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"top-k.d.ts","sourceRoot":"","sources":["../../src/probabilistic/top-k.ts"],"names":[],"mappings":"AAEA,OAAO,EAEN,KAAK,qBAAqB,EAC1B,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C,MAAM,MAAM,WAAW,GAAG,qBAAqB,C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