@zengrid/shared 1.0.3 → 1.0.4
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/dist/README.md +44 -0
- package/dist/index.cjs.default.js +1 -0
- package/dist/index.cjs.js +7394 -0
- package/dist/index.cjs.mjs +2 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.esm.js +7343 -0
- package/dist/package.json +54 -0
- package/dist/src/algorithms/filter/bloom-filter.d.ts +120 -0
- package/dist/src/algorithms/filter/bloom-filter.d.ts.map +1 -0
- package/dist/src/algorithms/filter/index.d.ts +7 -0
- package/dist/src/algorithms/filter/index.d.ts.map +1 -0
- package/dist/src/algorithms/graph/bfs.d.ts +71 -0
- package/dist/src/algorithms/graph/bfs.d.ts.map +1 -0
- package/dist/src/algorithms/graph/dfs.d.ts +62 -0
- package/dist/src/algorithms/graph/dfs.d.ts.map +1 -0
- package/dist/src/algorithms/graph/graph.interface.d.ts +63 -0
- package/dist/src/algorithms/graph/graph.interface.d.ts.map +1 -0
- package/dist/src/algorithms/graph/index.d.ts +9 -0
- package/dist/src/algorithms/graph/index.d.ts.map +1 -0
- package/dist/src/algorithms/index.d.ts +11 -0
- package/dist/src/algorithms/index.d.ts.map +1 -0
- package/dist/src/algorithms/pattern/index.d.ts +8 -0
- package/dist/src/algorithms/pattern/index.d.ts.map +1 -0
- package/dist/src/algorithms/pattern/sequence-detector.d.ts +62 -0
- package/dist/src/algorithms/pattern/sequence-detector.d.ts.map +1 -0
- package/dist/src/algorithms/search/binary-search.d.ts +86 -0
- package/dist/src/algorithms/search/binary-search.d.ts.map +1 -0
- package/dist/src/algorithms/search/index.d.ts +6 -0
- package/dist/src/algorithms/search/index.d.ts.map +1 -0
- package/dist/src/algorithms/sorting/comparators.d.ts +126 -0
- package/dist/src/algorithms/sorting/comparators.d.ts.map +1 -0
- package/dist/src/algorithms/sorting/index.d.ts +9 -0
- package/dist/src/algorithms/sorting/index.d.ts.map +1 -0
- package/dist/src/algorithms/sorting/sorting.interface.d.ts +38 -0
- package/dist/src/algorithms/sorting/sorting.interface.d.ts.map +1 -0
- package/dist/src/algorithms/sorting/timsort.d.ts +55 -0
- package/dist/src/algorithms/sorting/timsort.d.ts.map +1 -0
- package/dist/src/data-structures/column-store/column-store.d.ts +52 -0
- package/dist/src/data-structures/column-store/column-store.d.ts.map +1 -0
- package/dist/src/data-structures/column-store/column-store.interface.d.ts +155 -0
- package/dist/src/data-structures/column-store/column-store.interface.d.ts.map +1 -0
- package/dist/src/data-structures/column-store/index.d.ts +6 -0
- package/dist/src/data-structures/column-store/index.d.ts.map +1 -0
- package/dist/src/data-structures/command-stack/command-stack.d.ts +61 -0
- package/dist/src/data-structures/command-stack/command-stack.d.ts.map +1 -0
- package/dist/src/data-structures/command-stack/command-stack.interface.d.ts +80 -0
- package/dist/src/data-structures/command-stack/command-stack.interface.d.ts.map +1 -0
- package/dist/src/data-structures/command-stack/index.d.ts +3 -0
- package/dist/src/data-structures/command-stack/index.d.ts.map +1 -0
- package/dist/src/data-structures/dependency-graph/dependency-graph.d.ts +147 -0
- package/dist/src/data-structures/dependency-graph/dependency-graph.d.ts.map +1 -0
- package/dist/src/data-structures/dependency-graph/dependency-graph.interface.d.ts +161 -0
- package/dist/src/data-structures/dependency-graph/dependency-graph.interface.d.ts.map +1 -0
- package/dist/src/data-structures/dependency-graph/index.d.ts +8 -0
- package/dist/src/data-structures/dependency-graph/index.d.ts.map +1 -0
- package/dist/src/data-structures/disjoint-set/disjoint-set.d.ts +84 -0
- package/dist/src/data-structures/disjoint-set/disjoint-set.d.ts.map +1 -0
- package/dist/src/data-structures/disjoint-set/disjoint-set.interface.d.ts +185 -0
- package/dist/src/data-structures/disjoint-set/disjoint-set.interface.d.ts.map +1 -0
- package/dist/src/data-structures/disjoint-set/index.d.ts +7 -0
- package/dist/src/data-structures/disjoint-set/index.d.ts.map +1 -0
- package/dist/src/data-structures/index-map/index-map.d.ts +150 -0
- package/dist/src/data-structures/index-map/index-map.d.ts.map +1 -0
- package/dist/src/data-structures/index-map/index-map.interface.d.ts +186 -0
- package/dist/src/data-structures/index-map/index-map.interface.d.ts.map +1 -0
- package/dist/src/data-structures/index-map/index.d.ts +6 -0
- package/dist/src/data-structures/index-map/index.d.ts.map +1 -0
- package/dist/src/data-structures/index.d.ts +36 -0
- package/dist/src/data-structures/index.d.ts.map +1 -0
- package/dist/src/data-structures/interval-tree/index.d.ts +7 -0
- package/dist/src/data-structures/interval-tree/index.d.ts.map +1 -0
- package/dist/src/data-structures/interval-tree/interval-tree.d.ts +141 -0
- package/dist/src/data-structures/interval-tree/interval-tree.d.ts.map +1 -0
- package/dist/src/data-structures/interval-tree/interval-tree.interface.d.ts +212 -0
- package/dist/src/data-structures/interval-tree/interval-tree.interface.d.ts.map +1 -0
- package/dist/src/data-structures/lru-cache/index.d.ts +3 -0
- package/dist/src/data-structures/lru-cache/index.d.ts.map +1 -0
- package/dist/src/data-structures/lru-cache/lru-cache.d.ts +134 -0
- package/dist/src/data-structures/lru-cache/lru-cache.d.ts.map +1 -0
- package/dist/src/data-structures/lru-cache/lru-cache.interface.d.ts +173 -0
- package/dist/src/data-structures/lru-cache/lru-cache.interface.d.ts.map +1 -0
- package/dist/src/data-structures/prefix-sum-array/index.d.ts +6 -0
- package/dist/src/data-structures/prefix-sum-array/index.d.ts.map +1 -0
- package/dist/src/data-structures/prefix-sum-array/prefix-sum-array.d.ts +63 -0
- package/dist/src/data-structures/prefix-sum-array/prefix-sum-array.d.ts.map +1 -0
- package/dist/src/data-structures/prefix-sum-array/prefix-sum-array.interface.d.ts +80 -0
- package/dist/src/data-structures/prefix-sum-array/prefix-sum-array.interface.d.ts.map +1 -0
- package/dist/src/data-structures/rtree/index.d.ts +8 -0
- package/dist/src/data-structures/rtree/index.d.ts.map +1 -0
- package/dist/src/data-structures/rtree/rtree.d.ts +81 -0
- package/dist/src/data-structures/rtree/rtree.d.ts.map +1 -0
- package/dist/src/data-structures/rtree/rtree.interface.d.ts +143 -0
- package/dist/src/data-structures/rtree/rtree.interface.d.ts.map +1 -0
- package/dist/src/data-structures/segment-tree/index.d.ts +4 -0
- package/dist/src/data-structures/segment-tree/index.d.ts.map +1 -0
- package/dist/src/data-structures/segment-tree/segment-tree.d.ts +134 -0
- package/dist/src/data-structures/segment-tree/segment-tree.d.ts.map +1 -0
- package/dist/src/data-structures/segment-tree/segment-tree.interface.d.ts +202 -0
- package/dist/src/data-structures/segment-tree/segment-tree.interface.d.ts.map +1 -0
- package/dist/src/data-structures/skip-list/index.d.ts +4 -0
- package/dist/src/data-structures/skip-list/index.d.ts.map +1 -0
- package/dist/src/data-structures/skip-list/skip-list.d.ts +137 -0
- package/dist/src/data-structures/skip-list/skip-list.d.ts.map +1 -0
- package/dist/src/data-structures/skip-list/skip-list.interface.d.ts +223 -0
- package/dist/src/data-structures/skip-list/skip-list.interface.d.ts.map +1 -0
- package/dist/src/data-structures/sparse-matrix/index.d.ts +6 -0
- package/dist/src/data-structures/sparse-matrix/index.d.ts.map +1 -0
- package/dist/src/data-structures/sparse-matrix/sparse-matrix.d.ts +44 -0
- package/dist/src/data-structures/sparse-matrix/sparse-matrix.d.ts.map +1 -0
- package/dist/src/data-structures/sparse-matrix/sparse-matrix.interface.d.ts +105 -0
- package/dist/src/data-structures/sparse-matrix/sparse-matrix.interface.d.ts.map +1 -0
- package/dist/src/data-structures/suffix-array/index.d.ts +3 -0
- package/dist/src/data-structures/suffix-array/index.d.ts.map +1 -0
- package/dist/src/data-structures/suffix-array/suffix-array.d.ts +102 -0
- package/dist/src/data-structures/suffix-array/suffix-array.d.ts.map +1 -0
- package/dist/src/data-structures/suffix-array/suffix-array.interface.d.ts +117 -0
- package/dist/src/data-structures/suffix-array/suffix-array.interface.d.ts.map +1 -0
- package/dist/src/data-structures/trie/index.d.ts +8 -0
- package/dist/src/data-structures/trie/index.d.ts.map +1 -0
- package/dist/src/data-structures/trie/trie.d.ts +205 -0
- package/dist/src/data-structures/trie/trie.d.ts.map +1 -0
- package/dist/src/data-structures/trie/trie.interface.d.ts +117 -0
- package/dist/src/data-structures/trie/trie.interface.d.ts.map +1 -0
- package/dist/src/index.d.ts +51 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/patterns/coordinator.d.ts +90 -0
- package/dist/src/patterns/coordinator.d.ts.map +1 -0
- package/dist/src/patterns/event-emitter.d.ts +106 -0
- package/dist/src/patterns/event-emitter.d.ts.map +1 -0
- package/dist/src/patterns/factory.d.ts +120 -0
- package/dist/src/patterns/factory.d.ts.map +1 -0
- package/dist/src/patterns/index.d.ts +21 -0
- package/dist/src/patterns/index.d.ts.map +1 -0
- package/dist/src/patterns/mediator.d.ts +116 -0
- package/dist/src/patterns/mediator.d.ts.map +1 -0
- package/dist/src/patterns/object-pool.d.ts +178 -0
- package/dist/src/patterns/object-pool.d.ts.map +1 -0
- package/dist/src/patterns/observer.d.ts +89 -0
- package/dist/src/patterns/observer.d.ts.map +1 -0
- package/dist/src/patterns/operation-mode.d.ts +75 -0
- package/dist/src/patterns/operation-mode.d.ts.map +1 -0
- package/dist/src/patterns/state-machine.d.ts +122 -0
- package/dist/src/patterns/state-machine.d.ts.map +1 -0
- package/dist/src/types/common.d.ts +56 -0
- package/dist/src/types/common.d.ts.map +1 -0
- package/dist/src/types/index.d.ts +5 -0
- package/dist/src/types/index.d.ts.map +1 -0
- package/dist/src/utils/index.d.ts +7 -0
- package/dist/src/utils/index.d.ts.map +1 -0
- package/dist/src/utils/timing.d.ts +104 -0
- package/dist/src/utils/timing.d.ts.map +1 -0
- package/dist/src/utils/timing.interface.d.ts +96 -0
- package/dist/src/utils/timing.interface.d.ts.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prefix Sum Array interfaces for ZenGrid
|
|
3
|
+
* Used for efficient range queries and offset calculations
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Prefix Sum Array for cumulative calculations
|
|
7
|
+
*
|
|
8
|
+
* Useful for:
|
|
9
|
+
* - Variable row heights in virtual scrolling
|
|
10
|
+
* - Finding element at specific offset (O(log n) with binary search)
|
|
11
|
+
* - Range sum queries
|
|
12
|
+
*/
|
|
13
|
+
export interface PrefixSumArray {
|
|
14
|
+
/**
|
|
15
|
+
* Get cumulative sum up to (but not including) index
|
|
16
|
+
* @param index - Index to query
|
|
17
|
+
* @returns Sum of values[0] through values[index-1]
|
|
18
|
+
* @complexity O(1)
|
|
19
|
+
*/
|
|
20
|
+
getOffset(index: number): number;
|
|
21
|
+
/**
|
|
22
|
+
* Get cumulative sum up to and including index
|
|
23
|
+
* @param index - Index to query
|
|
24
|
+
* @returns Sum of values[0] through values[index]
|
|
25
|
+
* @complexity O(1)
|
|
26
|
+
*/
|
|
27
|
+
getSumUpTo(index: number): number;
|
|
28
|
+
/**
|
|
29
|
+
* Get sum of range [start, end)
|
|
30
|
+
* @param start - Start index (inclusive)
|
|
31
|
+
* @param end - End index (exclusive)
|
|
32
|
+
* @returns Sum of values[start] through values[end-1]
|
|
33
|
+
* @complexity O(1)
|
|
34
|
+
*/
|
|
35
|
+
getRangeSum(start: number, end: number): number;
|
|
36
|
+
/**
|
|
37
|
+
* Find index where cumulative sum >= target offset
|
|
38
|
+
* Uses binary search internally
|
|
39
|
+
* @param offset - Target cumulative value
|
|
40
|
+
* @returns Index where cumulative sum >= offset
|
|
41
|
+
* @complexity O(log n)
|
|
42
|
+
*/
|
|
43
|
+
findIndexAtOffset(offset: number): number;
|
|
44
|
+
/**
|
|
45
|
+
* Update value at index and rebuild prefix sums
|
|
46
|
+
* @param index - Index to update
|
|
47
|
+
* @param newValue - New value
|
|
48
|
+
* @complexity O(n) - requires rebuilding from index onward
|
|
49
|
+
*/
|
|
50
|
+
update(index: number, newValue: number): void;
|
|
51
|
+
/**
|
|
52
|
+
* Get original value at index (not cumulative)
|
|
53
|
+
* @param index - Index to query
|
|
54
|
+
* @returns Original value
|
|
55
|
+
* @complexity O(1)
|
|
56
|
+
*/
|
|
57
|
+
getValue(index: number): number;
|
|
58
|
+
/**
|
|
59
|
+
* Total number of elements
|
|
60
|
+
*/
|
|
61
|
+
readonly length: number;
|
|
62
|
+
/**
|
|
63
|
+
* Total sum of all values
|
|
64
|
+
*/
|
|
65
|
+
readonly total: number;
|
|
66
|
+
/**
|
|
67
|
+
* Get underlying prefix sum array (for debugging)
|
|
68
|
+
*/
|
|
69
|
+
readonly prefixSums: ReadonlyArray<number>;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Options for creating a PrefixSumArray
|
|
73
|
+
*/
|
|
74
|
+
export interface PrefixSumArrayOptions {
|
|
75
|
+
/**
|
|
76
|
+
* Initial values to build prefix sums from
|
|
77
|
+
*/
|
|
78
|
+
values?: number[];
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=prefix-sum-array.interface.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prefix-sum-array.interface.d.ts","sourceRoot":"","sources":["../../../../src/data-structures/prefix-sum-array/prefix-sum-array.interface.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;GAOG;AACH,MAAM,WAAW,cAAc;IAC7B;;;;;OAKG;IACH,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;IAEjC;;;;;OAKG;IACH,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;IAElC;;;;;;OAMG;IACH,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;IAEhD;;;;;;OAMG;IACH,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC;IAE1C;;;;;OAKG;IACH,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IAE9C;;;;;OAKG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;IAEhC;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAExB;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB;;OAEG;IACH,QAAQ,CAAC,UAAU,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;CAC5C;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;CACnB"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* R-Tree (Spatial Index) data structure
|
|
3
|
+
*
|
|
4
|
+
* Used for efficient spatial queries, hit testing, and range searching.
|
|
5
|
+
*/
|
|
6
|
+
export { RTree } from './rtree';
|
|
7
|
+
export type { IRTree, Rectangle, RTreeData, RTreeOptions, RTreeSearchResult, RTreeStats, } from './rtree.interface';
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/data-structures/rtree/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,YAAY,EACV,MAAM,EACN,SAAS,EACT,SAAS,EACT,YAAY,EACZ,iBAAiB,EACjB,UAAU,GACX,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { IRTree, Rectangle, RTreeOptions, RTreeSearchResult, RTreeStats } from './rtree.interface';
|
|
2
|
+
/**
|
|
3
|
+
* R-Tree - Spatial Index Data Structure
|
|
4
|
+
*
|
|
5
|
+
* An R-Tree is a tree data structure for indexing spatial data.
|
|
6
|
+
* It groups nearby objects and represents them with their minimum bounding rectangle (MBR).
|
|
7
|
+
*
|
|
8
|
+
* **Time Complexity:**
|
|
9
|
+
* - Insert: O(log n) average, O(n) worst case
|
|
10
|
+
* - Search: O(log n + k) where k = number of results
|
|
11
|
+
* - Delete: O(log n) average
|
|
12
|
+
*
|
|
13
|
+
* **Space Complexity:** O(n)
|
|
14
|
+
*
|
|
15
|
+
* **Use Cases in Grid:**
|
|
16
|
+
* - Hit testing: Find cell at mouse coordinates - O(log n)
|
|
17
|
+
* - Range queries: Find all cells in viewport - O(log n + k)
|
|
18
|
+
* - Merged cells: Detect overlapping cell ranges
|
|
19
|
+
* - Selection optimization: Quickly find cells in selection rect
|
|
20
|
+
*/
|
|
21
|
+
export declare class RTree<T = any> implements IRTree<T> {
|
|
22
|
+
private root;
|
|
23
|
+
private options;
|
|
24
|
+
private itemCount;
|
|
25
|
+
constructor(options?: RTreeOptions);
|
|
26
|
+
/**
|
|
27
|
+
* Insert an item into the R-Tree
|
|
28
|
+
*/
|
|
29
|
+
insert(rect: Rectangle, data: T): void;
|
|
30
|
+
/**
|
|
31
|
+
* Search for items intersecting a rectangle
|
|
32
|
+
*/
|
|
33
|
+
search(rect: Rectangle): RTreeSearchResult<T>[];
|
|
34
|
+
/**
|
|
35
|
+
* Search for items at a specific point
|
|
36
|
+
*/
|
|
37
|
+
searchAtPoint(x: number, y: number): RTreeSearchResult<T>[];
|
|
38
|
+
/**
|
|
39
|
+
* Find nearest neighbors to a point
|
|
40
|
+
*/
|
|
41
|
+
nearest(x: number, y: number, maxResults?: number, maxDistance?: number): RTreeSearchResult<T>[];
|
|
42
|
+
/**
|
|
43
|
+
* Remove an item from the tree
|
|
44
|
+
*/
|
|
45
|
+
remove(rect: Rectangle, data?: T): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Clear all items from the tree
|
|
48
|
+
*/
|
|
49
|
+
clear(): void;
|
|
50
|
+
/**
|
|
51
|
+
* Get all items in the tree
|
|
52
|
+
*/
|
|
53
|
+
all(): RTreeSearchResult<T>[];
|
|
54
|
+
/**
|
|
55
|
+
* Check if the tree is empty
|
|
56
|
+
*/
|
|
57
|
+
isEmpty(): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Get statistics about the tree
|
|
60
|
+
*/
|
|
61
|
+
getStats(): RTreeStats;
|
|
62
|
+
/**
|
|
63
|
+
* Rebuild the tree for better performance
|
|
64
|
+
*/
|
|
65
|
+
rebuild(): void;
|
|
66
|
+
private createNode;
|
|
67
|
+
private isNode;
|
|
68
|
+
private calcBBox;
|
|
69
|
+
private extendBBox;
|
|
70
|
+
private intersects;
|
|
71
|
+
private rectangleDistanceTo;
|
|
72
|
+
private searchNode;
|
|
73
|
+
private insertItem;
|
|
74
|
+
private enlargementNeeded;
|
|
75
|
+
private area;
|
|
76
|
+
private split;
|
|
77
|
+
private findItem;
|
|
78
|
+
private rectanglesEqual;
|
|
79
|
+
private getAllItems;
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=rtree.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rtree.d.ts","sourceRoot":"","sources":["../../../../src/data-structures/rtree/rtree.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,MAAM,EACN,SAAS,EAET,YAAY,EACZ,iBAAiB,EACjB,UAAU,EACX,MAAM,mBAAmB,CAAC;AAmB3B;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,KAAK,CAAC,CAAC,GAAG,GAAG,CAAE,YAAW,MAAM,CAAC,CAAC,CAAC;IAC9C,OAAO,CAAC,IAAI,CAAe;IAC3B,OAAO,CAAC,OAAO,CAAyB;IACxC,OAAO,CAAC,SAAS,CAAa;gBAElB,OAAO,GAAE,YAAiB;IAStC;;OAEG;IACH,MAAM,CAAC,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI;IAMtC;;OAEG;IACH,MAAM,CAAC,IAAI,EAAE,SAAS,GAAG,iBAAiB,CAAC,CAAC,CAAC,EAAE;IAQ/C;;OAEG;IACH,aAAa,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,iBAAiB,CAAC,CAAC,CAAC,EAAE;IAS3D;;OAEG;IACH,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,UAAU,GAAE,MAAU,EAAE,WAAW,GAAE,MAAiB,GAAG,iBAAiB,CAAC,CAAC,CAAC,EAAE;IAuD7G;;OAEG;IACH,MAAM,CAAC,IAAI,EAAE,SAAS,EAAE,IAAI,CAAC,EAAE,CAAC,GAAG,OAAO;IAmC1C;;OAEG;IACH,KAAK,IAAI,IAAI;IAKb;;OAEG;IACH,GAAG,IAAI,iBAAiB,CAAC,CAAC,CAAC,EAAE;IAQ7B;;OAEG;IACH,OAAO,IAAI,OAAO;IAIlB;;OAEG;IACH,QAAQ,IAAI,UAAU;IAuBtB;;OAEG;IACH,OAAO,IAAI,IAAI;IAWf,OAAO,CAAC,UAAU;IAelB,OAAO,CAAC,MAAM;IAId,OAAO,CAAC,QAAQ;IAuBhB,OAAO,CAAC,UAAU;IAOlB,OAAO,CAAC,UAAU;IAIlB,OAAO,CAAC,mBAAmB;IAM3B,OAAO,CAAC,UAAU;IAsBlB,OAAO,CAAC,UAAU;IA6DlB,OAAO,CAAC,iBAAiB;IAazB,OAAO,CAAC,IAAI;IAIZ,OAAO,CAAC,KAAK;IA8Bb,OAAO,CAAC,QAAQ;IAgChB,OAAO,CAAC,eAAe;IAIvB,OAAO,CAAC,WAAW;CAcpB"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* R-Tree (Spatial Index) Interface
|
|
3
|
+
*
|
|
4
|
+
* Used for:
|
|
5
|
+
* - Efficient spatial queries (hit testing, range queries)
|
|
6
|
+
* - Finding cells at point (x, y)
|
|
7
|
+
* - Finding cells in rectangular region
|
|
8
|
+
* - Merged cell detection
|
|
9
|
+
* - Selection range optimization
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* 2D Rectangle (Axis-Aligned Bounding Box)
|
|
13
|
+
*/
|
|
14
|
+
export interface Rectangle {
|
|
15
|
+
/**
|
|
16
|
+
* Minimum X coordinate (left edge)
|
|
17
|
+
*/
|
|
18
|
+
minX: number;
|
|
19
|
+
/**
|
|
20
|
+
* Minimum Y coordinate (top edge)
|
|
21
|
+
*/
|
|
22
|
+
minY: number;
|
|
23
|
+
/**
|
|
24
|
+
* Maximum X coordinate (right edge)
|
|
25
|
+
*/
|
|
26
|
+
maxX: number;
|
|
27
|
+
/**
|
|
28
|
+
* Maximum Y coordinate (bottom edge)
|
|
29
|
+
*/
|
|
30
|
+
maxY: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* R-Tree node data
|
|
34
|
+
*/
|
|
35
|
+
export interface RTreeData<T = any> {
|
|
36
|
+
/**
|
|
37
|
+
* The bounding rectangle
|
|
38
|
+
*/
|
|
39
|
+
rect: Rectangle;
|
|
40
|
+
/**
|
|
41
|
+
* Associated data
|
|
42
|
+
*/
|
|
43
|
+
data: T;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* R-Tree search result
|
|
47
|
+
*/
|
|
48
|
+
export interface RTreeSearchResult<T = any> {
|
|
49
|
+
/**
|
|
50
|
+
* The bounding rectangle
|
|
51
|
+
*/
|
|
52
|
+
rect: Rectangle;
|
|
53
|
+
/**
|
|
54
|
+
* Associated data
|
|
55
|
+
*/
|
|
56
|
+
data: T;
|
|
57
|
+
/**
|
|
58
|
+
* Distance from query point (if applicable)
|
|
59
|
+
*/
|
|
60
|
+
distance?: number;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* R-Tree configuration options
|
|
64
|
+
*/
|
|
65
|
+
export interface RTreeOptions {
|
|
66
|
+
/**
|
|
67
|
+
* Maximum entries per node
|
|
68
|
+
* @default 9
|
|
69
|
+
*/
|
|
70
|
+
maxEntries?: number;
|
|
71
|
+
/**
|
|
72
|
+
* Minimum entries per node
|
|
73
|
+
* @default Math.ceil(maxEntries * 0.4)
|
|
74
|
+
*/
|
|
75
|
+
minEntries?: number;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* R-Tree statistics
|
|
79
|
+
*/
|
|
80
|
+
export interface RTreeStats {
|
|
81
|
+
/**
|
|
82
|
+
* Total number of items in the tree
|
|
83
|
+
*/
|
|
84
|
+
size: number;
|
|
85
|
+
/**
|
|
86
|
+
* Height of the tree
|
|
87
|
+
*/
|
|
88
|
+
height: number;
|
|
89
|
+
/**
|
|
90
|
+
* Total number of nodes
|
|
91
|
+
*/
|
|
92
|
+
nodeCount: number;
|
|
93
|
+
/**
|
|
94
|
+
* Bounding box of all items
|
|
95
|
+
*/
|
|
96
|
+
bounds: Rectangle | null;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* R-Tree interface
|
|
100
|
+
*/
|
|
101
|
+
export interface IRTree<T = any> {
|
|
102
|
+
/**
|
|
103
|
+
* Insert an item into the tree
|
|
104
|
+
*/
|
|
105
|
+
insert(rect: Rectangle, data: T): void;
|
|
106
|
+
/**
|
|
107
|
+
* Search for items intersecting a rectangle
|
|
108
|
+
*/
|
|
109
|
+
search(rect: Rectangle): RTreeSearchResult<T>[];
|
|
110
|
+
/**
|
|
111
|
+
* Search for items at a specific point
|
|
112
|
+
*/
|
|
113
|
+
searchAtPoint(x: number, y: number): RTreeSearchResult<T>[];
|
|
114
|
+
/**
|
|
115
|
+
* Find nearest neighbors to a point
|
|
116
|
+
*/
|
|
117
|
+
nearest(x: number, y: number, maxResults?: number, maxDistance?: number): RTreeSearchResult<T>[];
|
|
118
|
+
/**
|
|
119
|
+
* Remove an item from the tree
|
|
120
|
+
*/
|
|
121
|
+
remove(rect: Rectangle, data?: T): boolean;
|
|
122
|
+
/**
|
|
123
|
+
* Remove all items from the tree
|
|
124
|
+
*/
|
|
125
|
+
clear(): void;
|
|
126
|
+
/**
|
|
127
|
+
* Get all items in the tree
|
|
128
|
+
*/
|
|
129
|
+
all(): RTreeSearchResult<T>[];
|
|
130
|
+
/**
|
|
131
|
+
* Check if the tree is empty
|
|
132
|
+
*/
|
|
133
|
+
isEmpty(): boolean;
|
|
134
|
+
/**
|
|
135
|
+
* Get statistics about the tree
|
|
136
|
+
*/
|
|
137
|
+
getStats(): RTreeStats;
|
|
138
|
+
/**
|
|
139
|
+
* Rebuild the tree (optimize structure)
|
|
140
|
+
*/
|
|
141
|
+
rebuild(): void;
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=rtree.interface.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rtree.interface.d.ts","sourceRoot":"","sources":["../../../../src/data-structures/rtree/rtree.interface.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;CACd;AAED;;GAEG;AACH,MAAM,WAAW,SAAS,CAAC,CAAC,GAAG,GAAG;IAChC;;OAEG;IACH,IAAI,EAAE,SAAS,CAAC;IAEhB;;OAEG;IACH,IAAI,EAAE,CAAC,CAAC;CACT;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB,CAAC,CAAC,GAAG,GAAG;IACxC;;OAEG;IACH,IAAI,EAAE,SAAS,CAAC;IAEhB;;OAEG;IACH,IAAI,EAAE,CAAC,CAAC;IAER;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IAEf;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,MAAM,EAAE,SAAS,GAAG,IAAI,CAAC;CAC1B;AAED;;GAEG;AACH,MAAM,WAAW,MAAM,CAAC,CAAC,GAAG,GAAG;IAC7B;;OAEG;IACH,MAAM,CAAC,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC;IAEvC;;OAEG;IACH,MAAM,CAAC,IAAI,EAAE,SAAS,GAAG,iBAAiB,CAAC,CAAC,CAAC,EAAE,CAAC;IAEhD;;OAEG;IACH,aAAa,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,iBAAiB,CAAC,CAAC,CAAC,EAAE,CAAC;IAE5D;;OAEG;IACH,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,GAAG,iBAAiB,CAAC,CAAC,CAAC,EAAE,CAAC;IAEjG;;OAEG;IACH,MAAM,CAAC,IAAI,EAAE,SAAS,EAAE,IAAI,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC;IAE3C;;OAEG;IACH,KAAK,IAAI,IAAI,CAAC;IAEd;;OAEG;IACH,GAAG,IAAI,iBAAiB,CAAC,CAAC,CAAC,EAAE,CAAC;IAE9B;;OAEG;IACH,OAAO,IAAI,OAAO,CAAC;IAEnB;;OAEG;IACH,QAAQ,IAAI,UAAU,CAAC;IAEvB;;OAEG;IACH,OAAO,IAAI,IAAI,CAAC;CACjB"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { SegmentTree } from './segment-tree';
|
|
2
|
+
export type { ISegmentTree, SegmentTreeOptions, AggregateFunction, } from './segment-tree.interface';
|
|
3
|
+
export { Aggregations, SegmentTreeUtils, AggregationType } from './segment-tree.interface';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/data-structures/segment-tree/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,YAAY,EACV,YAAY,EACZ,kBAAkB,EAClB,iBAAiB,GAClB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Segment Tree implementation
|
|
3
|
+
* Efficient range query and update data structure
|
|
4
|
+
*/
|
|
5
|
+
import type { ISegmentTree, SegmentTreeOptions, AggregateFunction } from './segment-tree.interface';
|
|
6
|
+
import { AggregationType } from './segment-tree.interface';
|
|
7
|
+
/**
|
|
8
|
+
* Segment Tree - Efficient range queries and updates
|
|
9
|
+
*
|
|
10
|
+
* Implementation:
|
|
11
|
+
* - Array-based complete binary tree
|
|
12
|
+
* - Each node stores the aggregate of its range
|
|
13
|
+
* - Supports lazy propagation for efficient range updates
|
|
14
|
+
* - Generic type T for flexibility (numbers, objects, etc.)
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* // Sum queries
|
|
19
|
+
* const tree = new SegmentTree({ values: [1, 3, 5, 7, 9, 11], type: 'sum' });
|
|
20
|
+
* tree.query(1, 3); // Returns 15 (3 + 5 + 7)
|
|
21
|
+
* tree.update(1, 10); // Change 3 to 10
|
|
22
|
+
* tree.query(1, 3); // Returns 22 (10 + 5 + 7)
|
|
23
|
+
*
|
|
24
|
+
* // Min queries
|
|
25
|
+
* const minTree = new SegmentTree({ values: [5, 2, 8, 1, 9], type: 'min' });
|
|
26
|
+
* minTree.query(0, 4); // Returns 1
|
|
27
|
+
* minTree.update(3, 10);
|
|
28
|
+
* minTree.query(0, 4); // Returns 2
|
|
29
|
+
*
|
|
30
|
+
* // Status bar aggregation example
|
|
31
|
+
* const cellValues = [100, 200, 150, 300, 250];
|
|
32
|
+
* const sumTree = new SegmentTree({ values: cellValues, type: 'sum' });
|
|
33
|
+
* sumTree.query(1, 3); // Sum of cells 1-3: 650
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare class SegmentTree<T> implements ISegmentTree<T> {
|
|
37
|
+
private tree;
|
|
38
|
+
private lazy;
|
|
39
|
+
private arr;
|
|
40
|
+
private n;
|
|
41
|
+
private aggregate;
|
|
42
|
+
private identityValue;
|
|
43
|
+
private useLazy;
|
|
44
|
+
private _type;
|
|
45
|
+
constructor(options: SegmentTreeOptions<T>);
|
|
46
|
+
/**
|
|
47
|
+
* Get aggregation configuration
|
|
48
|
+
*/
|
|
49
|
+
private getAggregationConfig;
|
|
50
|
+
/**
|
|
51
|
+
* Build the segment tree recursively
|
|
52
|
+
*/
|
|
53
|
+
private buildTree;
|
|
54
|
+
/**
|
|
55
|
+
* Push lazy updates down to children
|
|
56
|
+
*/
|
|
57
|
+
private pushDown;
|
|
58
|
+
/**
|
|
59
|
+
* Apply update to a node value
|
|
60
|
+
* For SUM: multiply update by range size
|
|
61
|
+
* For MIN/MAX: just use the update value
|
|
62
|
+
*/
|
|
63
|
+
private applyUpdate;
|
|
64
|
+
/**
|
|
65
|
+
* Query range [left, right]
|
|
66
|
+
*/
|
|
67
|
+
query(left: number, right: number): T;
|
|
68
|
+
/**
|
|
69
|
+
* Recursive query helper
|
|
70
|
+
*/
|
|
71
|
+
private queryTree;
|
|
72
|
+
/**
|
|
73
|
+
* Update a single element
|
|
74
|
+
*/
|
|
75
|
+
update(index: number, value: T): void;
|
|
76
|
+
/**
|
|
77
|
+
* Recursive update helper
|
|
78
|
+
*/
|
|
79
|
+
private updateTree;
|
|
80
|
+
/**
|
|
81
|
+
* Update a range [left, right]
|
|
82
|
+
*/
|
|
83
|
+
rangeUpdate(left: number, right: number, value: T): void;
|
|
84
|
+
/**
|
|
85
|
+
* Lazy range update helper
|
|
86
|
+
*/
|
|
87
|
+
private rangeUpdateLazy;
|
|
88
|
+
/**
|
|
89
|
+
* Get value at index
|
|
90
|
+
*/
|
|
91
|
+
get(index: number): T;
|
|
92
|
+
/**
|
|
93
|
+
* Build tree from new array
|
|
94
|
+
*/
|
|
95
|
+
build(arr: T[]): void;
|
|
96
|
+
/**
|
|
97
|
+
* Convert tree to array
|
|
98
|
+
*/
|
|
99
|
+
toArray(): T[];
|
|
100
|
+
/**
|
|
101
|
+
* Find first index where predicate is true
|
|
102
|
+
*/
|
|
103
|
+
findFirst(left: number, predicate: (value: T) => boolean): number;
|
|
104
|
+
/**
|
|
105
|
+
* Get size of array
|
|
106
|
+
*/
|
|
107
|
+
get size(): number;
|
|
108
|
+
/**
|
|
109
|
+
* Get total aggregated value
|
|
110
|
+
*/
|
|
111
|
+
get total(): T;
|
|
112
|
+
/**
|
|
113
|
+
* Get aggregation type
|
|
114
|
+
*/
|
|
115
|
+
get aggregationType(): AggregationType;
|
|
116
|
+
/**
|
|
117
|
+
* Create SegmentTree from array
|
|
118
|
+
*/
|
|
119
|
+
static from<T>(values: T[], type?: AggregationType): SegmentTree<T>;
|
|
120
|
+
/**
|
|
121
|
+
* Create custom SegmentTree
|
|
122
|
+
*/
|
|
123
|
+
static custom<T>(values: T[], aggregate: AggregateFunction<T>, identity: T): SegmentTree<T>;
|
|
124
|
+
/**
|
|
125
|
+
* Debug: Print tree structure
|
|
126
|
+
*/
|
|
127
|
+
toString(): string;
|
|
128
|
+
/**
|
|
129
|
+
* Debug: Validate tree structure
|
|
130
|
+
*/
|
|
131
|
+
validate(): boolean;
|
|
132
|
+
private validateNode;
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=segment-tree.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"segment-tree.d.ts","sourceRoot":"","sources":["../../../../src/data-structures/segment-tree/segment-tree.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EACV,YAAY,EACZ,kBAAkB,EAClB,iBAAiB,EAClB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAkC,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAE3F;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,qBAAa,WAAW,CAAC,CAAC,CAAE,YAAW,YAAY,CAAC,CAAC,CAAC;IACpD,OAAO,CAAC,IAAI,CAAM;IAClB,OAAO,CAAC,IAAI,CAAM;IAClB,OAAO,CAAC,GAAG,CAAM;IACjB,OAAO,CAAC,CAAC,CAAS;IAClB,OAAO,CAAC,SAAS,CAAuB;IACxC,OAAO,CAAC,aAAa,CAAI;IACzB,OAAO,CAAC,OAAO,CAAU;IACzB,OAAO,CAAC,KAAK,CAAkB;gBAEnB,OAAO,EAAE,kBAAkB,CAAC,CAAC,CAAC;IAgC1C;;OAEG;IACH,OAAO,CAAC,oBAAoB;IAsB5B;;OAEG;IACH,OAAO,CAAC,SAAS;IAiBjB;;OAEG;IACH,OAAO,CAAC,QAAQ;IA8BhB;;;;OAIG;IACH,OAAO,CAAC,WAAW;IAYnB;;OAEG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,CAAC;IAcrC;;OAEG;IACH,OAAO,CAAC,SAAS;IAiCjB;;OAEG;IACH,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI;IASrC;;OAEG;IACH,OAAO,CAAC,UAAU;IA+BlB;;OAEG;IACH,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI;IAkBxD;;OAEG;IACH,OAAO,CAAC,eAAe;IAqCvB;;OAEG;IACH,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,CAAC;IAQrB;;OAEG;IACH,KAAK,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,IAAI;IAarB;;OAEG;IACH,OAAO,IAAI,CAAC,EAAE;IAQd;;OAEG;IACH,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,GAAG,MAAM;IAUjE;;OAEG;IACH,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;OAEG;IACH,IAAI,KAAK,IAAI,CAAC,CAKb;IAED;;OAEG;IACH,IAAI,eAAe,IAAI,eAAe,CAErC;IAED;;OAEG;IACH,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,EAAE,IAAI,GAAE,eAAqC,GAAG,WAAW,CAAC,CAAC,CAAC;IAIxF;;OAEG;IACH,MAAM,CAAC,MAAM,CAAC,CAAC,EACb,MAAM,EAAE,CAAC,EAAE,EACX,SAAS,EAAE,iBAAiB,CAAC,CAAC,CAAC,EAC/B,QAAQ,EAAE,CAAC,GACV,WAAW,CAAC,CAAC,CAAC;IASjB;;OAEG;IACH,QAAQ,IAAI,MAAM;IAIlB;;OAEG;IACH,QAAQ,IAAI,OAAO;IAInB,OAAO,CAAC,YAAY;CA2BrB"}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Segment Tree interfaces for ZenGrid
|
|
3
|
+
* Efficiently handles range queries and updates for aggregation operations
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Aggregation function type
|
|
7
|
+
* Combines two values into one
|
|
8
|
+
*/
|
|
9
|
+
export type AggregateFunction<T> = (a: T, b: T) => T;
|
|
10
|
+
/**
|
|
11
|
+
* Common aggregation operations
|
|
12
|
+
*/
|
|
13
|
+
export declare enum AggregationType {
|
|
14
|
+
SUM = "sum",
|
|
15
|
+
MIN = "min",
|
|
16
|
+
MAX = "max",
|
|
17
|
+
GCD = "gcd",
|
|
18
|
+
LCM = "lcm",
|
|
19
|
+
PRODUCT = "product",
|
|
20
|
+
CUSTOM = "custom"
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Segment Tree interface
|
|
24
|
+
*
|
|
25
|
+
* A tree data structure for storing intervals or segments, allowing for efficient
|
|
26
|
+
* querying and updating of array elements over ranges.
|
|
27
|
+
*
|
|
28
|
+
* Use cases:
|
|
29
|
+
* - Status bar aggregations (sum, min, max of selected cells)
|
|
30
|
+
* - Range statistics (average, count, sum over a range)
|
|
31
|
+
* - Dynamic cumulative calculations
|
|
32
|
+
* - Range minimum/maximum queries
|
|
33
|
+
* - Frequent range updates with queries
|
|
34
|
+
*
|
|
35
|
+
* Performance:
|
|
36
|
+
* - Build: O(n)
|
|
37
|
+
* - Query: O(log n)
|
|
38
|
+
* - Update (point): O(log n)
|
|
39
|
+
* - Update (range): O(log n) with lazy propagation
|
|
40
|
+
* - Space: O(4n) = O(n)
|
|
41
|
+
*/
|
|
42
|
+
export interface ISegmentTree<T> {
|
|
43
|
+
/**
|
|
44
|
+
* Query the aggregated value over a range [left, right]
|
|
45
|
+
* @param left - Start index (inclusive)
|
|
46
|
+
* @param right - End index (inclusive)
|
|
47
|
+
* @returns Aggregated value over the range
|
|
48
|
+
* @complexity O(log n)
|
|
49
|
+
*/
|
|
50
|
+
query(left: number, right: number): T;
|
|
51
|
+
/**
|
|
52
|
+
* Update a single element at index
|
|
53
|
+
* @param index - Index to update
|
|
54
|
+
* @param value - New value
|
|
55
|
+
* @complexity O(log n)
|
|
56
|
+
*/
|
|
57
|
+
update(index: number, value: T): void;
|
|
58
|
+
/**
|
|
59
|
+
* Update a range of elements [left, right]
|
|
60
|
+
* @param left - Start index (inclusive)
|
|
61
|
+
* @param right - End index (inclusive)
|
|
62
|
+
* @param value - Value to apply to the range
|
|
63
|
+
* @complexity O(log n) with lazy propagation
|
|
64
|
+
*/
|
|
65
|
+
rangeUpdate(left: number, right: number, value: T): void;
|
|
66
|
+
/**
|
|
67
|
+
* Get the value at a specific index
|
|
68
|
+
* @param index - Index to query
|
|
69
|
+
* @returns Value at index
|
|
70
|
+
* @complexity O(log n) or O(1) depending on implementation
|
|
71
|
+
*/
|
|
72
|
+
get(index: number): T;
|
|
73
|
+
/**
|
|
74
|
+
* Build or rebuild the tree from new array
|
|
75
|
+
* @param arr - New array of values
|
|
76
|
+
* @complexity O(n)
|
|
77
|
+
*/
|
|
78
|
+
build(arr: T[]): void;
|
|
79
|
+
/**
|
|
80
|
+
* Get the entire array
|
|
81
|
+
* @returns Current array values
|
|
82
|
+
* @complexity O(n)
|
|
83
|
+
*/
|
|
84
|
+
toArray(): T[];
|
|
85
|
+
/**
|
|
86
|
+
* Find the first index where the aggregate from start meets a condition
|
|
87
|
+
* Useful for binary search on aggregates
|
|
88
|
+
* @param left - Start index
|
|
89
|
+
* @param predicate - Condition to test
|
|
90
|
+
* @returns Index or -1 if not found
|
|
91
|
+
* @complexity O(log^2 n)
|
|
92
|
+
*/
|
|
93
|
+
findFirst(left: number, predicate: (value: T) => boolean): number;
|
|
94
|
+
/**
|
|
95
|
+
* Get the size of the underlying array
|
|
96
|
+
*/
|
|
97
|
+
readonly size: number;
|
|
98
|
+
/**
|
|
99
|
+
* Get the total aggregated value (entire array)
|
|
100
|
+
*/
|
|
101
|
+
readonly total: T;
|
|
102
|
+
/**
|
|
103
|
+
* Get the aggregation type
|
|
104
|
+
*/
|
|
105
|
+
readonly aggregationType: AggregationType;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Options for creating a Segment Tree
|
|
109
|
+
*/
|
|
110
|
+
export interface SegmentTreeOptions<T> {
|
|
111
|
+
/**
|
|
112
|
+
* Initial array of values
|
|
113
|
+
*/
|
|
114
|
+
values?: T[];
|
|
115
|
+
/**
|
|
116
|
+
* Aggregation type (for common operations)
|
|
117
|
+
* If CUSTOM is specified, must provide customAggregate function
|
|
118
|
+
*/
|
|
119
|
+
type?: AggregationType;
|
|
120
|
+
/**
|
|
121
|
+
* Custom aggregation function
|
|
122
|
+
* Required if type is CUSTOM
|
|
123
|
+
*/
|
|
124
|
+
customAggregate?: AggregateFunction<T>;
|
|
125
|
+
/**
|
|
126
|
+
* Identity value for the aggregation
|
|
127
|
+
* - For SUM: 0
|
|
128
|
+
* - For MIN: Infinity
|
|
129
|
+
* - For MAX: -Infinity
|
|
130
|
+
* - For PRODUCT: 1
|
|
131
|
+
* Must be provided for CUSTOM type
|
|
132
|
+
*/
|
|
133
|
+
identity?: T;
|
|
134
|
+
/**
|
|
135
|
+
* Whether to use lazy propagation for range updates
|
|
136
|
+
* @default true
|
|
137
|
+
*/
|
|
138
|
+
lazy?: boolean;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Predefined aggregation functions and identities
|
|
142
|
+
*/
|
|
143
|
+
export declare const Aggregations: {
|
|
144
|
+
sum: {
|
|
145
|
+
fn: (a: number, b: number) => number;
|
|
146
|
+
identity: number;
|
|
147
|
+
};
|
|
148
|
+
min: {
|
|
149
|
+
fn: (a: number, b: number) => number;
|
|
150
|
+
identity: number;
|
|
151
|
+
};
|
|
152
|
+
max: {
|
|
153
|
+
fn: (a: number, b: number) => number;
|
|
154
|
+
identity: number;
|
|
155
|
+
};
|
|
156
|
+
gcd: {
|
|
157
|
+
fn: (a: number, b: number) => number;
|
|
158
|
+
identity: number;
|
|
159
|
+
};
|
|
160
|
+
lcm: {
|
|
161
|
+
fn: (a: number, b: number) => number;
|
|
162
|
+
identity: number;
|
|
163
|
+
};
|
|
164
|
+
product: {
|
|
165
|
+
fn: (a: number, b: number) => number;
|
|
166
|
+
identity: number;
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
/**
|
|
170
|
+
* Helper functions for segment tree operations
|
|
171
|
+
*/
|
|
172
|
+
export declare const SegmentTreeUtils: {
|
|
173
|
+
/**
|
|
174
|
+
* Calculate the tree size needed for array of size n
|
|
175
|
+
*/
|
|
176
|
+
getTreeSize(n: number): number;
|
|
177
|
+
/**
|
|
178
|
+
* Check if index is valid
|
|
179
|
+
*/
|
|
180
|
+
isValidIndex(index: number, size: number): boolean;
|
|
181
|
+
/**
|
|
182
|
+
* Check if range is valid
|
|
183
|
+
*/
|
|
184
|
+
isValidRange(left: number, right: number, size: number): boolean;
|
|
185
|
+
/**
|
|
186
|
+
* Get parent index in tree
|
|
187
|
+
*/
|
|
188
|
+
parent(index: number): number;
|
|
189
|
+
/**
|
|
190
|
+
* Get left child index in tree
|
|
191
|
+
*/
|
|
192
|
+
leftChild(index: number): number;
|
|
193
|
+
/**
|
|
194
|
+
* Get right child index in tree
|
|
195
|
+
*/
|
|
196
|
+
rightChild(index: number): number;
|
|
197
|
+
/**
|
|
198
|
+
* Get the middle point of a range
|
|
199
|
+
*/
|
|
200
|
+
mid(left: number, right: number): number;
|
|
201
|
+
};
|
|
202
|
+
//# sourceMappingURL=segment-tree.interface.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"segment-tree.interface.d.ts","sourceRoot":"","sources":["../../../../src/data-structures/segment-tree/segment-tree.interface.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;GAGG;AACH,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC;AAErD;;GAEG;AACH,oBAAY,eAAe;IACzB,GAAG,QAAQ;IACX,GAAG,QAAQ;IACX,GAAG,QAAQ;IACX,GAAG,QAAQ;IACX,GAAG,QAAQ;IACX,OAAO,YAAY;IACnB,MAAM,WAAW;CAClB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,WAAW,YAAY,CAAC,CAAC;IAC7B;;;;;;OAMG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,CAAC,CAAC;IAEtC;;;;;OAKG;IACH,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;IAEtC;;;;;;OAMG;IACH,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;IAEzD;;;;;OAKG;IACH,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,CAAC,CAAC;IAEtB;;;;OAIG;IACH,KAAK,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,IAAI,CAAC;IAEtB;;;;OAIG;IACH,OAAO,IAAI,CAAC,EAAE,CAAC;IAEf;;;;;;;OAOG;IACH,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,GAAG,MAAM,CAAC;IAElE;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;IAElB;;OAEG;IACH,QAAQ,CAAC,eAAe,EAAE,eAAe,CAAC;CAC3C;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB,CAAC,CAAC;IACnC;;OAEG;IACH,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC;IAEb;;;OAGG;IACH,IAAI,CAAC,EAAE,eAAe,CAAC;IAEvB;;;OAGG;IACH,eAAe,CAAC,EAAE,iBAAiB,CAAC,CAAC,CAAC,CAAC;IAEvC;;;;;;;OAOG;IACH,QAAQ,CAAC,EAAE,CAAC,CAAC;IAEb;;;OAGG;IACH,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED;;GAEG;AACH,eAAO,MAAM,YAAY;;gBAEb,MAAM,KAAK,MAAM;;;;gBAIjB,MAAM,KAAK,MAAM;;;;gBAIjB,MAAM,KAAK,MAAM;;;;gBAIjB,MAAM,KAAK,MAAM;;;;gBAWjB,MAAM,KAAK,MAAM;;;;gBAOjB,MAAM,KAAK,MAAM;;;CAG5B,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,gBAAgB;IAC3B;;OAEG;mBACY,MAAM,GAAG,MAAM;IAK9B;;OAEG;wBACiB,MAAM,QAAQ,MAAM,GAAG,OAAO;IAIlD;;OAEG;uBACgB,MAAM,SAAS,MAAM,QAAQ,MAAM,GAAG,OAAO;IAQhE;;OAEG;kBACW,MAAM,GAAG,MAAM;IAI7B;;OAEG;qBACc,MAAM,GAAG,MAAM;IAIhC;;OAEG;sBACe,MAAM,GAAG,MAAM;IAIjC;;OAEG;cACO,MAAM,SAAS,MAAM,GAAG,MAAM;CAGzC,CAAC"}
|