typescript-dsa-stl 2.4.0 → 2.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/README.md +204 -4
- package/dist/collections/Deque.d.ts +44 -0
- package/dist/collections/Deque.d.ts.map +1 -0
- package/dist/collections/Deque.js +178 -0
- package/dist/collections/Deque.js.map +1 -0
- package/dist/collections/SegmentTree.d.ts +108 -0
- package/dist/collections/SegmentTree.d.ts.map +1 -0
- package/dist/collections/SegmentTree.js +234 -0
- package/dist/collections/SegmentTree.js.map +1 -0
- package/dist/collections/index.d.ts +3 -0
- package/dist/collections/index.d.ts.map +1 -1
- package/dist/collections/index.js +2 -0
- package/dist/collections/index.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
**This is the GitHub repository** for the npm package **[typescript-dsa-stl](https://www.npmjs.com/package/typescript-dsa-stl)**.
|
|
4
4
|
|
|
5
|
-
STL-style data structures and algorithms for TypeScript: **Vector**, **Stack**, **Queue**, **List**, **PriorityQueue**, **OrderedMap** (Map), **UnorderedMap**, **OrderedSet** (Set), **UnorderedSet**, **OrderedMultiMap**, **OrderedMultiSet**, and algorithms (`sort`, `binarySearch`, `lowerBound`, `min`, `max`, **KnuthMorrisPratt**, **RabinKarp**, **StringRollingHash**, etc.). Install from npm to use in your project; this repo holds the source code.
|
|
5
|
+
STL-style data structures and algorithms for TypeScript: **Vector**, **Stack**, **Queue**, **Deque**, **List**, **PriorityQueue**, **OrderedMap** (Map), **UnorderedMap**, **OrderedSet** (Set), **UnorderedSet**, **OrderedMultiMap**, **OrderedMultiSet**, **segment trees** (`SegmentTreeSum`, `SegmentTreeMin`, `SegmentTreeMax`, `SegmentTree`, `GeneralSegmentTree`, `LazySegmentTreeSum`), and algorithms (`sort`, `binarySearch`, `lowerBound`, `min`, `max`, **KnuthMorrisPratt**, **RabinKarp**, **StringRollingHash**, etc.). Install from npm to use in your project; this repo holds the source code.
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
Vector,
|
|
22
22
|
Stack,
|
|
23
23
|
Queue,
|
|
24
|
+
Deque,
|
|
24
25
|
List,
|
|
25
26
|
PriorityQueue,
|
|
26
27
|
OrderedMap,
|
|
@@ -53,6 +54,15 @@ queue.enqueue(1);
|
|
|
53
54
|
queue.enqueue(2);
|
|
54
55
|
console.log(queue.front()); // 1
|
|
55
56
|
|
|
57
|
+
// Deque — double-ended queue (like C++ std::deque): O(1) both ends + O(1) index access
|
|
58
|
+
const deque = new Deque<number>();
|
|
59
|
+
deque.pushBack(2);
|
|
60
|
+
deque.pushFront(1);
|
|
61
|
+
deque.pushBack(3);
|
|
62
|
+
console.log(deque.front()); // 1
|
|
63
|
+
console.log(deque.back()); // 3
|
|
64
|
+
console.log(deque.at(1)); // 2
|
|
65
|
+
|
|
56
66
|
const list = new List<number>();
|
|
57
67
|
list.pushBack(10);
|
|
58
68
|
const node = list.pushBack(20);
|
|
@@ -102,6 +112,30 @@ clamp(42, 0, 10); // 10
|
|
|
102
112
|
range(0, 5); // [0, 1, 2, 3, 4]
|
|
103
113
|
```
|
|
104
114
|
|
|
115
|
+
### Deque (like C++ `std::deque`)
|
|
116
|
+
|
|
117
|
+
A **double-ended queue**: amortized **O(1)** `pushFront` / `pushBack` / `popFront` / `popBack`, and **O(1)** random access via `at` / `set`. Implemented as a growable circular buffer (same asymptotics as a typical `std::deque` for these operations).
|
|
118
|
+
|
|
119
|
+
| C++ | TypeScript |
|
|
120
|
+
|-----|------------|
|
|
121
|
+
| `push_front` | `pushFront` |
|
|
122
|
+
| `push_back` | `pushBack` |
|
|
123
|
+
| `pop_front` | `popFront` |
|
|
124
|
+
| `pop_back` | `popBack` |
|
|
125
|
+
| `front` / `back` | `front()` / `back()` |
|
|
126
|
+
| `operator[]` / `at` | `at(i)` / `set(i, value)` |
|
|
127
|
+
| `size` / `empty` | `size` / `empty` |
|
|
128
|
+
| — | `capacity`, `reserve`, `shrinkToFit`, `toArray()`, iterator |
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
import { Deque } from 'typescript-dsa-stl';
|
|
132
|
+
|
|
133
|
+
const d = new Deque<number>([1, 2, 3]); // copy initial elements, or `new Deque()` / `new Deque(64)` for capacity hint
|
|
134
|
+
d.pushFront(0);
|
|
135
|
+
d.popBack();
|
|
136
|
+
console.log(d.toArray()); // [0, 1, 2]
|
|
137
|
+
```
|
|
138
|
+
|
|
105
139
|
### 2D and 3D vectors (like C++ `vector<vector<int>>`)
|
|
106
140
|
|
|
107
141
|
`Vector<T>` is generic, so you can nest it for 2D/3D grids:
|
|
@@ -521,19 +555,177 @@ console.log(a.substringHash(2, 2) === b.fullHash()); // true — both are "na"
|
|
|
521
555
|
|
|
522
556
|
---
|
|
523
557
|
|
|
558
|
+
## Segment trees
|
|
559
|
+
|
|
560
|
+
Segment trees support **range queries** and **point updates** in **O(log n)**. Range endpoints are **inclusive**: `query(l, r)` covers indices `l` through `r`.
|
|
561
|
+
|
|
562
|
+
### Ready-made variants (`SegmentTreeSum`, `SegmentTreeMin`, `SegmentTreeMax`)
|
|
563
|
+
|
|
564
|
+
```ts
|
|
565
|
+
import {
|
|
566
|
+
SegmentTreeSum,
|
|
567
|
+
SegmentTreeMin,
|
|
568
|
+
SegmentTreeMax,
|
|
569
|
+
} from 'typescript-dsa-stl';
|
|
570
|
+
|
|
571
|
+
const sum = new SegmentTreeSum([1, 2, 3, 4]);
|
|
572
|
+
console.log(sum.query(0, 3)); // 10
|
|
573
|
+
sum.update(1, 10);
|
|
574
|
+
console.log(sum.query(0, 3)); // 1 + 10 + 3 + 4 = 18
|
|
575
|
+
|
|
576
|
+
const mn = new SegmentTreeMin([5, 2, 8, 1]);
|
|
577
|
+
console.log(mn.query(0, 3)); // 1
|
|
578
|
+
|
|
579
|
+
const mx = new SegmentTreeMax([5, 2, 8, 1]);
|
|
580
|
+
console.log(mx.query(0, 3)); // 8
|
|
581
|
+
```
|
|
582
|
+
|
|
583
|
+
### Generic `SegmentTree<T>` (custom combine + neutral)
|
|
584
|
+
|
|
585
|
+
Use the same type for array elements and aggregates. Pass an **associative** `combine` and a **neutral** value for query ranges that miss a segment (e.g. `0` for sum, `Infinity` for min).
|
|
586
|
+
|
|
587
|
+
```ts
|
|
588
|
+
import { SegmentTree } from 'typescript-dsa-stl';
|
|
589
|
+
|
|
590
|
+
const gcdTree = new SegmentTree<number>(
|
|
591
|
+
[12, 18, 24],
|
|
592
|
+
(a, b) => {
|
|
593
|
+
let x = a;
|
|
594
|
+
let y = b;
|
|
595
|
+
while (y !== 0) {
|
|
596
|
+
const t = y;
|
|
597
|
+
y = x % y;
|
|
598
|
+
x = t;
|
|
599
|
+
}
|
|
600
|
+
return x;
|
|
601
|
+
},
|
|
602
|
+
0
|
|
603
|
+
);
|
|
604
|
+
console.log(gcdTree.query(0, 2)); // gcd(12, 18, 24) === 6
|
|
605
|
+
|
|
606
|
+
// Non-numeric example: concatenate strings
|
|
607
|
+
const strTree = new SegmentTree<string>(
|
|
608
|
+
['a', 'b', 'c'],
|
|
609
|
+
(a, b) => a + b,
|
|
610
|
+
''
|
|
611
|
+
);
|
|
612
|
+
console.log(strTree.query(0, 2)); // 'abc'
|
|
613
|
+
```
|
|
614
|
+
|
|
615
|
+
### `GeneralSegmentTree<T, V>` (custom merge + buildLeaf)
|
|
616
|
+
|
|
617
|
+
Use when **raw** values `V` differ from the **aggregate** type `T`:
|
|
618
|
+
|
|
619
|
+
- **`merge(left, right)`** — combine two child aggregates (internal nodes).
|
|
620
|
+
- **`neutral`** — identity for `merge` when a query does not overlap a segment.
|
|
621
|
+
- **`buildLeaf(value, index)`** — build the leaf from the raw array on initial construction and on every `update`.
|
|
622
|
+
|
|
623
|
+
```ts
|
|
624
|
+
import { GeneralSegmentTree } from 'typescript-dsa-stl';
|
|
625
|
+
|
|
626
|
+
// Store sum of squares; raw array is plain numbers
|
|
627
|
+
const st = new GeneralSegmentTree<number, number>([1, 2, 3], {
|
|
628
|
+
merge: (a, b) => a + b,
|
|
629
|
+
neutral: 0,
|
|
630
|
+
buildLeaf: (v, i) => v * v + i,
|
|
631
|
+
});
|
|
632
|
+
console.log(st.query(0, 2)); // (1+0) + (4+1) + (9+2) = 17
|
|
633
|
+
st.update(1, 4);
|
|
634
|
+
console.log(st.rawAt(1)); // 4 — current raw value at index 1
|
|
635
|
+
```
|
|
636
|
+
|
|
637
|
+
### `LazySegmentTreeSum` (range add + range sum)
|
|
638
|
+
|
|
639
|
+
**`rangeAdd(l, r, delta)`** adds `delta` to every element in the inclusive range. **`rangeSum(l, r)`** returns the sum. **`set(i, value)`** assigns one position (lazy tags are applied along the path). All are **O(log n)**.
|
|
640
|
+
|
|
641
|
+
```ts
|
|
642
|
+
import { LazySegmentTreeSum } from 'typescript-dsa-stl';
|
|
643
|
+
|
|
644
|
+
const lazy = new LazySegmentTreeSum([0, 0, 0, 0]);
|
|
645
|
+
lazy.rangeAdd(1, 2, 5); // indices 1 and 2 get +5
|
|
646
|
+
console.log(lazy.rangeSum(0, 3)); // 10
|
|
647
|
+
lazy.set(0, 100);
|
|
648
|
+
console.log(lazy.rangeSum(0, 3)); // 100 + 5 + 5 + 0
|
|
649
|
+
```
|
|
650
|
+
|
|
651
|
+
### Real-world use cases
|
|
652
|
+
|
|
653
|
+
These patterns show up in backends and internal tools when you need **many** range queries and updates on a fixed sequence (length known up front), without scanning the whole array each time.
|
|
654
|
+
|
|
655
|
+
#### 1. Analytics or reporting: totals over a time window (with corrections)
|
|
656
|
+
|
|
657
|
+
Each index is a **fixed bucket** (hour of day, day of month, version slot, etc.). You repeatedly ask “what is the **sum** from bucket `a` through `b`?” and sometimes **fix one bucket** after late data or a reconciliation.
|
|
658
|
+
|
|
659
|
+
```ts
|
|
660
|
+
import { SegmentTreeSum } from 'typescript-dsa-stl';
|
|
661
|
+
|
|
662
|
+
/** Revenue (or events, page views, API calls) per calendar day; index 0 = first day of period. */
|
|
663
|
+
class PeriodMetrics {
|
|
664
|
+
private readonly tree: SegmentTreeSum;
|
|
665
|
+
|
|
666
|
+
constructor(dailyValues: readonly number[]) {
|
|
667
|
+
this.tree = new SegmentTreeSum(dailyValues);
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/** Total for an inclusive day range — e.g. chart drill-down or export row. */
|
|
671
|
+
totalBetweenDay(firstDayIndex: number, lastDayIndex: number): number {
|
|
672
|
+
return this.tree.query(firstDayIndex, lastDayIndex);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/** Backfill or correct one day without rebuilding the whole series. */
|
|
676
|
+
setDay(dayIndex: number, amount: number): void {
|
|
677
|
+
this.tree.update(dayIndex, amount);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
const january = new PeriodMetrics([1200, 980, 1100, 1050, 1300]);
|
|
682
|
+
console.log(january.totalBetweenDay(0, 4)); // full period
|
|
683
|
+
january.setDay(2, 1150); // corrected day 2
|
|
684
|
+
console.log(january.totalBetweenDay(1, 3)); // sum over days 1..3
|
|
685
|
+
```
|
|
686
|
+
|
|
687
|
+
In production you would usually **persist** the underlying series in a database and **rebuild** the tree when the period reloads; the tree stays useful in memory for dashboards, simulations, or request handlers that see heavy read/update traffic on the same window.
|
|
688
|
+
|
|
689
|
+
#### 2. Operations or finance: bulk adjustment on a slice, then aggregate
|
|
690
|
+
|
|
691
|
+
You apply the **same delta** to **every** element in an index range (tiered bonuses, prorated credits, simulation shocks), then need **range sums** for reporting. A lazy sum tree avoids touching each cell one by one.
|
|
692
|
+
|
|
693
|
+
```ts
|
|
694
|
+
import { LazySegmentTreeSum } from 'typescript-dsa-stl';
|
|
695
|
+
|
|
696
|
+
/** Example: per-seat or per-row amounts; apply a flat bonus to ranks 10–50 (0-based 9..49), then sum a sub-range for a sub-team. */
|
|
697
|
+
function simulateBulkBonusAndSubtotal(seatCount: number): void {
|
|
698
|
+
// Initial per-seat values (e.g. base commission), built once
|
|
699
|
+
const base = Array.from({ length: seatCount }, (_, i) => 100 + i);
|
|
700
|
+
const amounts = new LazySegmentTreeSum(base);
|
|
701
|
+
|
|
702
|
+
// HR: +250 to everyone in seats 10–50 inclusive (indices 9..49)
|
|
703
|
+
amounts.rangeAdd(9, 49, 250);
|
|
704
|
+
|
|
705
|
+
// Finance: subtotal for seats 20–30 only
|
|
706
|
+
console.log(amounts.rangeSum(19, 29));
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
simulateBulkBonusAndSubtotal(100);
|
|
710
|
+
```
|
|
711
|
+
|
|
712
|
+
The same idea applies to **inventory deltas** across bin ranges, **loyalty points** batch credits by user-ID band (when IDs map to contiguous indices), or **game/simulation** state where many cells gain the same buff and you query partial totals.
|
|
713
|
+
|
|
714
|
+
---
|
|
715
|
+
|
|
524
716
|
## API overview
|
|
525
717
|
|
|
526
718
|
| Module | Exports |
|
|
527
719
|
|--------|--------|
|
|
528
|
-
| **Collections** | `Vector`, `Stack`, `Queue`, `List`, `ListNode`, `PriorityQueue`, `OrderedMap`, `UnorderedMap`, `OrderedSet`, `UnorderedSet`, `OrderedMultiMap`, `OrderedMultiSet`, `WeightedEdge`, `AdjacencyList`, `WeightedAdjacencyList`, `createAdjacencyList`, `createWeightedAdjacencyList`, `addEdge`, `deleteEdge` |
|
|
720
|
+
| **Collections** | `Vector`, `Stack`, `Queue`, `Deque`, `List`, `ListNode`, `PriorityQueue`, `OrderedMap`, `UnorderedMap`, `OrderedSet`, `UnorderedSet`, `OrderedMultiMap`, `OrderedMultiSet`, `GeneralSegmentTree`, `SegmentTree`, `SegmentTreeSum`, `SegmentTreeMin`, `SegmentTreeMax`, `LazySegmentTreeSum`, `WeightedEdge`, `AdjacencyList`, `WeightedAdjacencyList`, `createAdjacencyList`, `createWeightedAdjacencyList`, `addEdge`, `deleteEdge` |
|
|
529
721
|
| **Algorithms** | `sort`, `find`, `findIndex`, `transform`, `filter`, `reduce`, `reverse`, `unique`, `binarySearch`, `lowerBound`, `upperBound`, `min`, `max`, `partition`, `DisjointSetUnion`, `KnuthMorrisPratt`, `RabinKarp`, `RABIN_KARP_DEFAULT_MODS`, `StringRollingHash`, `breadthFirstSearch`, `depthFirstSearch`, `connectedComponents`, `kruskalMST` |
|
|
530
722
|
| **Utils** | `clamp`, `range`, `noop`, `identity`, `swap` |
|
|
531
|
-
| **Types** | `Comparator`, `Predicate`, `UnaryFn`, `Reducer`, `IterableLike`, `toArray`, `RabinKarpTripleMods` |
|
|
723
|
+
| **Types** | `Comparator`, `Predicate`, `UnaryFn`, `Reducer`, `IterableLike`, `toArray`, `RabinKarpTripleMods`, `GeneralSegmentTreeConfig`, `SegmentCombine`, `SegmentMerge`, `SegmentLeafBuild` |
|
|
532
724
|
|
|
533
725
|
### Subpath imports (tree-shaking)
|
|
534
726
|
|
|
535
727
|
```ts
|
|
536
|
-
import { Vector, Stack } from 'typescript-dsa-stl/collections';
|
|
728
|
+
import { Vector, Stack, Queue, Deque } from 'typescript-dsa-stl/collections';
|
|
537
729
|
import { sort, binarySearch, breadthFirstSearch, depthFirstSearch, KnuthMorrisPratt, RabinKarp, StringRollingHash } from 'typescript-dsa-stl/algorithms';
|
|
538
730
|
import { clamp, range } from 'typescript-dsa-stl/utils';
|
|
539
731
|
import type { Comparator } from 'typescript-dsa-stl/types';
|
|
@@ -548,6 +740,7 @@ import type { Comparator } from 'typescript-dsa-stl/types';
|
|
|
548
740
|
| **Vector** | O(1) | O(1)* | O(n) | O(1) | O(n) |
|
|
549
741
|
| **Stack** | — | O(1) | — | O(1) | — |
|
|
550
742
|
| **Queue** | — | O(1)* | — | O(1)* | — |
|
|
743
|
+
| **Deque** | O(1) | O(1)* (front/back) | — | O(1)* (front/back) | — |
|
|
551
744
|
| **List** | O(n) | O(1) | O(1)** | O(1) | O(1)** |
|
|
552
745
|
| **PriorityQueue** | — | O(log n) | — | O(log n) | — |
|
|
553
746
|
| **OrderedMap** (Map) | O(log n) get | O(log n) set | — | O(log n) delete | — |
|
|
@@ -560,6 +753,13 @@ import type { Comparator } from 'typescript-dsa-stl/types';
|
|
|
560
753
|
\* Amortized (hash).
|
|
561
754
|
\** At a known node.
|
|
562
755
|
|
|
756
|
+
### Segment trees (range queries)
|
|
757
|
+
|
|
758
|
+
| Structure | Build | Point update | Range query | Extra |
|
|
759
|
+
|-----------|-------|--------------|-------------|--------|
|
|
760
|
+
| **GeneralSegmentTree**, **SegmentTree**, **SegmentTreeSum** / **Min** / **Max** | O(n) | O(log n) | O(log n) | Inclusive `[l, r]`; **GeneralSegmentTree** keeps raw `V` and uses `merge` + `buildLeaf` |
|
|
761
|
+
| **LazySegmentTreeSum** | O(n) | `set`: O(log n) | `rangeSum`: O(log n) | `rangeAdd` on a range: O(log n) |
|
|
762
|
+
|
|
563
763
|
---
|
|
564
764
|
|
|
565
765
|
## OrderedMultiMap and OrderedMultiSet — use cases
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Double-ended queue (STL `std::deque`-style API in camelCase).
|
|
3
|
+
*
|
|
4
|
+
* Backed by a growable circular buffer: amortized **O(1)** `pushFront` / `pushBack` /
|
|
5
|
+
* `popFront` / `popBack`, and **O(1)** indexed `at` / `set` (unlike a linked list).
|
|
6
|
+
*/
|
|
7
|
+
export declare class Deque<T> {
|
|
8
|
+
private _buf;
|
|
9
|
+
private _head;
|
|
10
|
+
private _size;
|
|
11
|
+
/**
|
|
12
|
+
* @param initial — initial capacity (number), or copy elements in order from an array.
|
|
13
|
+
*/
|
|
14
|
+
constructor(initial?: readonly T[] | number);
|
|
15
|
+
get size(): number;
|
|
16
|
+
get empty(): boolean;
|
|
17
|
+
/** Current backing slot count (≥ size). */
|
|
18
|
+
get capacity(): number;
|
|
19
|
+
private grow;
|
|
20
|
+
private ensureSpace;
|
|
21
|
+
/** Ensure buffer has room for at least `n` elements total (may shrink logical size only via pop/clear). */
|
|
22
|
+
reserve(minCapacity: number): void;
|
|
23
|
+
/** Drop unused buffer capacity (keeps current elements). */
|
|
24
|
+
shrinkToFit(): void;
|
|
25
|
+
private indexPhysical;
|
|
26
|
+
at(index: number): T;
|
|
27
|
+
set(index: number, value: T): void;
|
|
28
|
+
/** Same as C++ `push_front`. */
|
|
29
|
+
pushFront(value: T): void;
|
|
30
|
+
/** Same as C++ `push_back`. */
|
|
31
|
+
pushBack(value: T): void;
|
|
32
|
+
/** Same as C++ `pop_front`. */
|
|
33
|
+
popFront(): T | undefined;
|
|
34
|
+
/** Same as C++ `pop_back`. */
|
|
35
|
+
popBack(): T | undefined;
|
|
36
|
+
/** Same as C++ `front`. */
|
|
37
|
+
front(): T;
|
|
38
|
+
/** Same as C++ `back`. */
|
|
39
|
+
back(): T;
|
|
40
|
+
clear(): void;
|
|
41
|
+
[Symbol.iterator](): Iterator<T>;
|
|
42
|
+
toArray(): T[];
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=Deque.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Deque.d.ts","sourceRoot":"","sources":["../../src/collections/Deque.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,qBAAa,KAAK,CAAC,CAAC;IAClB,OAAO,CAAC,IAAI,CAAoB;IAChC,OAAO,CAAC,KAAK,CAAK;IAClB,OAAO,CAAC,KAAK,CAAK;IAElB;;OAEG;gBACS,OAAO,CAAC,EAAE,SAAS,CAAC,EAAE,GAAG,MAAM;IAkB3C,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,IAAI,KAAK,IAAI,OAAO,CAEnB;IAED,2CAA2C;IAC3C,IAAI,QAAQ,IAAI,MAAM,CAErB;IAED,OAAO,CAAC,IAAI;IAUZ,OAAO,CAAC,WAAW;IAKnB,2GAA2G;IAC3G,OAAO,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI;IAWlC,4DAA4D;IAC5D,WAAW,IAAI,IAAI;IAgBnB,OAAO,CAAC,aAAa;IAIrB,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,CAAC;IAOpB,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI;IAOlC,gCAAgC;IAChC,SAAS,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI;IAQzB,+BAA+B;IAC/B,QAAQ,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI;IAOxB,+BAA+B;IAC/B,QAAQ,IAAI,CAAC,GAAG,SAAS;IAUzB,8BAA8B;IAC9B,OAAO,IAAI,CAAC,GAAG,SAAS;IAUxB,2BAA2B;IAC3B,KAAK,IAAI,CAAC;IAKV,0BAA0B;IAC1B,IAAI,IAAI,CAAC;IAMT,KAAK,IAAI,IAAI;IAMb,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,QAAQ,CAAC,CAAC,CAAC;IAgBhC,OAAO,IAAI,CAAC,EAAE;CAQf"}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Double-ended queue (STL `std::deque`-style API in camelCase).
|
|
3
|
+
*
|
|
4
|
+
* Backed by a growable circular buffer: amortized **O(1)** `pushFront` / `pushBack` /
|
|
5
|
+
* `popFront` / `popBack`, and **O(1)** indexed `at` / `set` (unlike a linked list).
|
|
6
|
+
*/
|
|
7
|
+
export class Deque {
|
|
8
|
+
/**
|
|
9
|
+
* @param initial — initial capacity (number), or copy elements in order from an array.
|
|
10
|
+
*/
|
|
11
|
+
constructor(initial) {
|
|
12
|
+
this._head = 0;
|
|
13
|
+
this._size = 0;
|
|
14
|
+
if (initial === undefined) {
|
|
15
|
+
this._buf = new Array(16);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (typeof initial === 'number') {
|
|
19
|
+
this._buf = new Array(Math.max(4, initial));
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const n = initial.length;
|
|
23
|
+
const cap = Math.max(16, n <= 4 ? 4 : 2 ** Math.ceil(Math.log2(n)));
|
|
24
|
+
this._buf = new Array(cap);
|
|
25
|
+
for (let i = 0; i < n; i++) {
|
|
26
|
+
this._buf[i] = initial[i];
|
|
27
|
+
}
|
|
28
|
+
this._size = n;
|
|
29
|
+
}
|
|
30
|
+
get size() {
|
|
31
|
+
return this._size;
|
|
32
|
+
}
|
|
33
|
+
get empty() {
|
|
34
|
+
return this._size === 0;
|
|
35
|
+
}
|
|
36
|
+
/** Current backing slot count (≥ size). */
|
|
37
|
+
get capacity() {
|
|
38
|
+
return this._buf.length;
|
|
39
|
+
}
|
|
40
|
+
grow() {
|
|
41
|
+
const cap = this._buf.length;
|
|
42
|
+
const next = new Array(Math.max(4, cap * 2));
|
|
43
|
+
for (let i = 0; i < this._size; i++) {
|
|
44
|
+
next[i] = this._buf[(this._head + i) % cap];
|
|
45
|
+
}
|
|
46
|
+
this._buf = next;
|
|
47
|
+
this._head = 0;
|
|
48
|
+
}
|
|
49
|
+
ensureSpace() {
|
|
50
|
+
if (this._size < this._buf.length)
|
|
51
|
+
return;
|
|
52
|
+
this.grow();
|
|
53
|
+
}
|
|
54
|
+
/** Ensure buffer has room for at least `n` elements total (may shrink logical size only via pop/clear). */
|
|
55
|
+
reserve(minCapacity) {
|
|
56
|
+
if (minCapacity <= this._buf.length)
|
|
57
|
+
return;
|
|
58
|
+
const next = new Array(minCapacity);
|
|
59
|
+
const cap = this._buf.length;
|
|
60
|
+
for (let i = 0; i < this._size; i++) {
|
|
61
|
+
next[i] = this._buf[(this._head + i) % cap];
|
|
62
|
+
}
|
|
63
|
+
this._buf = next;
|
|
64
|
+
this._head = 0;
|
|
65
|
+
}
|
|
66
|
+
/** Drop unused buffer capacity (keeps current elements). */
|
|
67
|
+
shrinkToFit() {
|
|
68
|
+
if (this._size === this._buf.length)
|
|
69
|
+
return;
|
|
70
|
+
if (this._size === 0) {
|
|
71
|
+
this._buf = new Array(4);
|
|
72
|
+
this._head = 0;
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const next = new Array(this._size);
|
|
76
|
+
const cap = this._buf.length;
|
|
77
|
+
for (let i = 0; i < this._size; i++) {
|
|
78
|
+
next[i] = this._buf[(this._head + i) % cap];
|
|
79
|
+
}
|
|
80
|
+
this._buf = next;
|
|
81
|
+
this._head = 0;
|
|
82
|
+
}
|
|
83
|
+
indexPhysical(i) {
|
|
84
|
+
return (this._head + i) % this._buf.length;
|
|
85
|
+
}
|
|
86
|
+
at(index) {
|
|
87
|
+
if (index < 0 || index >= this._size) {
|
|
88
|
+
throw new RangeError(`Deque index ${index} out of range (size ${this._size})`);
|
|
89
|
+
}
|
|
90
|
+
return this._buf[this.indexPhysical(index)];
|
|
91
|
+
}
|
|
92
|
+
set(index, value) {
|
|
93
|
+
if (index < 0 || index >= this._size) {
|
|
94
|
+
throw new RangeError(`Deque index ${index} out of range (size ${this._size})`);
|
|
95
|
+
}
|
|
96
|
+
this._buf[this.indexPhysical(index)] = value;
|
|
97
|
+
}
|
|
98
|
+
/** Same as C++ `push_front`. */
|
|
99
|
+
pushFront(value) {
|
|
100
|
+
this.ensureSpace();
|
|
101
|
+
const cap = this._buf.length;
|
|
102
|
+
this._head = (this._head - 1 + cap) % cap;
|
|
103
|
+
this._buf[this._head] = value;
|
|
104
|
+
this._size++;
|
|
105
|
+
}
|
|
106
|
+
/** Same as C++ `push_back`. */
|
|
107
|
+
pushBack(value) {
|
|
108
|
+
this.ensureSpace();
|
|
109
|
+
const cap = this._buf.length;
|
|
110
|
+
this._buf[(this._head + this._size) % cap] = value;
|
|
111
|
+
this._size++;
|
|
112
|
+
}
|
|
113
|
+
/** Same as C++ `pop_front`. */
|
|
114
|
+
popFront() {
|
|
115
|
+
if (this._size === 0)
|
|
116
|
+
return undefined;
|
|
117
|
+
const cap = this._buf.length;
|
|
118
|
+
const value = this._buf[this._head];
|
|
119
|
+
this._buf[this._head] = undefined;
|
|
120
|
+
this._head = (this._head + 1) % cap;
|
|
121
|
+
this._size--;
|
|
122
|
+
return value;
|
|
123
|
+
}
|
|
124
|
+
/** Same as C++ `pop_back`. */
|
|
125
|
+
popBack() {
|
|
126
|
+
if (this._size === 0)
|
|
127
|
+
return undefined;
|
|
128
|
+
const cap = this._buf.length;
|
|
129
|
+
const idx = (this._head + this._size - 1 + cap) % cap;
|
|
130
|
+
const value = this._buf[idx];
|
|
131
|
+
this._buf[idx] = undefined;
|
|
132
|
+
this._size--;
|
|
133
|
+
return value;
|
|
134
|
+
}
|
|
135
|
+
/** Same as C++ `front`. */
|
|
136
|
+
front() {
|
|
137
|
+
if (this._size === 0)
|
|
138
|
+
throw new RangeError('Deque is empty');
|
|
139
|
+
return this._buf[this._head];
|
|
140
|
+
}
|
|
141
|
+
/** Same as C++ `back`. */
|
|
142
|
+
back() {
|
|
143
|
+
if (this._size === 0)
|
|
144
|
+
throw new RangeError('Deque is empty');
|
|
145
|
+
const cap = this._buf.length;
|
|
146
|
+
return this._buf[(this._head + this._size - 1 + cap) % cap];
|
|
147
|
+
}
|
|
148
|
+
clear() {
|
|
149
|
+
this._buf = new Array(this._buf.length);
|
|
150
|
+
this._head = 0;
|
|
151
|
+
this._size = 0;
|
|
152
|
+
}
|
|
153
|
+
[Symbol.iterator]() {
|
|
154
|
+
const buf = this._buf;
|
|
155
|
+
const cap = buf.length;
|
|
156
|
+
const head = this._head;
|
|
157
|
+
const size = this._size;
|
|
158
|
+
let i = 0;
|
|
159
|
+
return {
|
|
160
|
+
next() {
|
|
161
|
+
if (i >= size)
|
|
162
|
+
return { done: true, value: undefined };
|
|
163
|
+
const value = buf[(head + i) % cap];
|
|
164
|
+
i++;
|
|
165
|
+
return { done: false, value };
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
toArray() {
|
|
170
|
+
const out = [];
|
|
171
|
+
const cap = this._buf.length;
|
|
172
|
+
for (let i = 0; i < this._size; i++) {
|
|
173
|
+
out.push(this._buf[(this._head + i) % cap]);
|
|
174
|
+
}
|
|
175
|
+
return out;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
//# sourceMappingURL=Deque.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Deque.js","sourceRoot":"","sources":["../../src/collections/Deque.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,OAAO,KAAK;IAKhB;;OAEG;IACH,YAAY,OAA+B;QANnC,UAAK,GAAG,CAAC,CAAC;QACV,UAAK,GAAG,CAAC,CAAC;QAMhB,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YAC1B,IAAI,CAAC,IAAI,GAAG,IAAI,KAAK,CAAC,EAAE,CAAC,CAAC;YAC1B,OAAO;QACT,CAAC;QACD,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;YAChC,IAAI,CAAC,IAAI,GAAG,IAAI,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC;YAC5C,OAAO;QACT,CAAC;QACD,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC;QACzB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACpE,IAAI,CAAC,IAAI,GAAG,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC;QAC3B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YAC3B,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;QAC5B,CAAC;QACD,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC;IACjB,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KA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GAAG,GAAG,CAAE,CAAC,CAAC;QAC/C,CAAC;QACD,OAAO,GAAG,CAAC;IACb,CAAC;CACF"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Segment trees: range queries and point updates in O(log n).
|
|
3
|
+
*
|
|
4
|
+
* - {@link GeneralSegmentTree} — full control: custom **merge** (internal nodes) and **buildLeaf** (initial + point updates).
|
|
5
|
+
* - {@link SegmentTree} — same stored type as array; custom combine + neutral only.
|
|
6
|
+
* - {@link SegmentTreeSum} — range sum, point assign.
|
|
7
|
+
* - {@link SegmentTreeMin} — range minimum, point assign.
|
|
8
|
+
* - {@link SegmentTreeMax} — range maximum, point assign.
|
|
9
|
+
* - {@link LazySegmentTreeSum} — range add + range sum (lazy propagation).
|
|
10
|
+
*
|
|
11
|
+
* All range endpoints are **inclusive**: `query(l, r)` uses indices `l` through `r`.
|
|
12
|
+
*/
|
|
13
|
+
export type SegmentCombine<T> = (a: T, b: T) => T;
|
|
14
|
+
/** Combine two child segment aggregates into the parent (internal nodes). */
|
|
15
|
+
export type SegmentMerge<T> = (left: T, right: T) => T;
|
|
16
|
+
/**
|
|
17
|
+
* Turn each raw array element into the leaf value stored in the tree.
|
|
18
|
+
* Used on initial build and on every `update`.
|
|
19
|
+
*/
|
|
20
|
+
export type SegmentLeafBuild<T, V> = (value: V, index: number) => T;
|
|
21
|
+
export interface GeneralSegmentTreeConfig<T, V> {
|
|
22
|
+
/** Combine left and right child aggregates (must be associative with `neutral`). */
|
|
23
|
+
merge: SegmentMerge<T>;
|
|
24
|
+
/** Result of `merge` when a query misses the segment (identity for `merge`). */
|
|
25
|
+
neutral: T;
|
|
26
|
+
/** Build the leaf at `index` from the current raw value `value`. */
|
|
27
|
+
buildLeaf: SegmentLeafBuild<T, V>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* General-purpose segment tree: raw array type `V`, stored aggregate type `T`.
|
|
31
|
+
* You supply **merge** for internal nodes and **buildLeaf** for leaves (build + point updates).
|
|
32
|
+
*/
|
|
33
|
+
export declare class GeneralSegmentTree<T, V> {
|
|
34
|
+
private readonly _n;
|
|
35
|
+
private readonly _tree;
|
|
36
|
+
private readonly _raw;
|
|
37
|
+
private readonly _merge;
|
|
38
|
+
private readonly _neutral;
|
|
39
|
+
private readonly _buildLeaf;
|
|
40
|
+
constructor(values: readonly V[], config: GeneralSegmentTreeConfig<T, V>);
|
|
41
|
+
get size(): number;
|
|
42
|
+
/** Current raw value at `index` (the same `V` you pass to `update`). */
|
|
43
|
+
rawAt(index: number): V;
|
|
44
|
+
/**
|
|
45
|
+
* Set raw value at `index`, rebuild leaf with `buildLeaf`, refresh ancestors. O(log n).
|
|
46
|
+
*/
|
|
47
|
+
update(index: number, rawValue: V): void;
|
|
48
|
+
/** Aggregate over inclusive range `[left, right]`. O(log n). */
|
|
49
|
+
query(left: number, right: number): T;
|
|
50
|
+
private _bounds;
|
|
51
|
+
private _build;
|
|
52
|
+
private _update;
|
|
53
|
+
private _query;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Generic segment tree: stored type equals array element type; leaves are the raw values.
|
|
57
|
+
* Equivalent to {@link GeneralSegmentTree} with `buildLeaf: (v) => v`.
|
|
58
|
+
*/
|
|
59
|
+
export declare class SegmentTree<T> {
|
|
60
|
+
private readonly _inner;
|
|
61
|
+
/**
|
|
62
|
+
* @param values Initial leaf values (copied).
|
|
63
|
+
* @param combine Associative operation (e.g. addition, min, max).
|
|
64
|
+
* @param neutral Identity for `combine` (e.g. 0 for sum, Infinity for min).
|
|
65
|
+
*/
|
|
66
|
+
constructor(values: readonly T[], combine: SegmentCombine<T>, neutral: T);
|
|
67
|
+
get size(): number;
|
|
68
|
+
/** Set index `index` to `value` and refresh ancestors. O(log n). */
|
|
69
|
+
update(index: number, value: T): void;
|
|
70
|
+
/** Aggregate over inclusive range `[left, right]`. O(log n). */
|
|
71
|
+
query(left: number, right: number): T;
|
|
72
|
+
}
|
|
73
|
+
/** Range sum and point assign. Neutral element is `0`. */
|
|
74
|
+
export declare class SegmentTreeSum extends SegmentTree<number> {
|
|
75
|
+
constructor(values: readonly number[]);
|
|
76
|
+
}
|
|
77
|
+
/** Range minimum and point assign. Neutral for out-of-range is `Infinity`. */
|
|
78
|
+
export declare class SegmentTreeMin extends SegmentTree<number> {
|
|
79
|
+
constructor(values: readonly number[]);
|
|
80
|
+
}
|
|
81
|
+
/** Range maximum and point assign. Neutral for out-of-range is `-Infinity`. */
|
|
82
|
+
export declare class SegmentTreeMax extends SegmentTree<number> {
|
|
83
|
+
constructor(values: readonly number[]);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Lazy segment tree: add a constant to every element in `[left, right]` and query range sums.
|
|
87
|
+
* Both operations are O(log n).
|
|
88
|
+
*/
|
|
89
|
+
export declare class LazySegmentTreeSum {
|
|
90
|
+
private readonly _n;
|
|
91
|
+
private readonly _tree;
|
|
92
|
+
private readonly _lazy;
|
|
93
|
+
constructor(values: readonly number[]);
|
|
94
|
+
get size(): number;
|
|
95
|
+
/** Add `delta` to each element in inclusive `[left, right]`. O(log n). */
|
|
96
|
+
rangeAdd(left: number, right: number, delta: number): void;
|
|
97
|
+
/** Sum of elements in inclusive `[left, right]`. O(log n). */
|
|
98
|
+
rangeSum(left: number, right: number): number;
|
|
99
|
+
/** Point assign (sets one index; clears lazy along the path). O(log n). */
|
|
100
|
+
set(index: number, value: number): void;
|
|
101
|
+
private _bounds;
|
|
102
|
+
private _build;
|
|
103
|
+
private _push;
|
|
104
|
+
private _rangeAdd;
|
|
105
|
+
private _rangeSum;
|
|
106
|
+
private _set;
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=SegmentTree.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SegmentTree.d.ts","sourceRoot":"","sources":["../../src/collections/SegmentTree.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,cAAc,CAAC,CA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,MAAM,GAAG,IAAI;IAO1D,8DAA8D;IAC9D,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM;IAO7C,2EAA2E;IAC3E,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;IAKvC,OAAO,CAAC,OAAO;IAOf,OAAO,CAAC,MAAM;IAWd,OAAO,CAAC,KAAK;IAab,OAAO,CAAC,SAAS;IAcjB,OAAO,CAAC,SAAS;IAUjB,OAAO,CAAC,IAAI;CAYb"}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* General-purpose segment tree: raw array type `V`, stored aggregate type `T`.
|
|
3
|
+
* You supply **merge** for internal nodes and **buildLeaf** for leaves (build + point updates).
|
|
4
|
+
*/
|
|
5
|
+
export class GeneralSegmentTree {
|
|
6
|
+
constructor(values, config) {
|
|
7
|
+
this._n = values.length;
|
|
8
|
+
this._merge = config.merge;
|
|
9
|
+
this._neutral = config.neutral;
|
|
10
|
+
this._buildLeaf = config.buildLeaf;
|
|
11
|
+
this._raw = this._n > 0 ? Array.from(values) : [];
|
|
12
|
+
this._tree = new Array(Math.max(1, 4 * this._n));
|
|
13
|
+
if (this._n > 0) {
|
|
14
|
+
this._build(1, 0, this._n - 1);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
get size() {
|
|
18
|
+
return this._n;
|
|
19
|
+
}
|
|
20
|
+
/** Current raw value at `index` (the same `V` you pass to `update`). */
|
|
21
|
+
rawAt(index) {
|
|
22
|
+
this._bounds(index);
|
|
23
|
+
return this._raw[index];
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Set raw value at `index`, rebuild leaf with `buildLeaf`, refresh ancestors. O(log n).
|
|
27
|
+
*/
|
|
28
|
+
update(index, rawValue) {
|
|
29
|
+
this._bounds(index);
|
|
30
|
+
this._raw[index] = rawValue;
|
|
31
|
+
this._update(1, 0, this._n - 1, index);
|
|
32
|
+
}
|
|
33
|
+
/** Aggregate over inclusive range `[left, right]`. O(log n). */
|
|
34
|
+
query(left, right) {
|
|
35
|
+
this._bounds(left);
|
|
36
|
+
this._bounds(right);
|
|
37
|
+
if (left > right)
|
|
38
|
+
throw new RangeError('left must be <= right');
|
|
39
|
+
return this._query(1, 0, this._n - 1, left, right);
|
|
40
|
+
}
|
|
41
|
+
_bounds(i) {
|
|
42
|
+
if (this._n === 0)
|
|
43
|
+
throw new RangeError('GeneralSegmentTree is empty');
|
|
44
|
+
if (i < 0 || i >= this._n) {
|
|
45
|
+
throw new RangeError(`Index ${i} out of range [0, ${this._n - 1}]`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
_build(node, l, r) {
|
|
49
|
+
if (l === r) {
|
|
50
|
+
this._tree[node] = this._buildLeaf(this._raw[l], l);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const mid = (l + r) >>> 1;
|
|
54
|
+
this._build(node * 2, l, mid);
|
|
55
|
+
this._build(node * 2 + 1, mid + 1, r);
|
|
56
|
+
this._tree[node] = this._merge(this._tree[node * 2], this._tree[node * 2 + 1]);
|
|
57
|
+
}
|
|
58
|
+
_update(node, l, r, idx) {
|
|
59
|
+
if (l === r) {
|
|
60
|
+
this._tree[node] = this._buildLeaf(this._raw[idx], idx);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const mid = (l + r) >>> 1;
|
|
64
|
+
if (idx <= mid)
|
|
65
|
+
this._update(node * 2, l, mid, idx);
|
|
66
|
+
else
|
|
67
|
+
this._update(node * 2 + 1, mid + 1, r, idx);
|
|
68
|
+
this._tree[node] = this._merge(this._tree[node * 2], this._tree[node * 2 + 1]);
|
|
69
|
+
}
|
|
70
|
+
_query(node, l, r, ql, qr) {
|
|
71
|
+
if (qr < l || r < ql)
|
|
72
|
+
return this._neutral;
|
|
73
|
+
if (ql <= l && r <= qr)
|
|
74
|
+
return this._tree[node];
|
|
75
|
+
const mid = (l + r) >>> 1;
|
|
76
|
+
const leftQ = this._query(node * 2, l, mid, ql, qr);
|
|
77
|
+
const rightQ = this._query(node * 2 + 1, mid + 1, r, ql, qr);
|
|
78
|
+
return this._merge(leftQ, rightQ);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Generic segment tree: stored type equals array element type; leaves are the raw values.
|
|
83
|
+
* Equivalent to {@link GeneralSegmentTree} with `buildLeaf: (v) => v`.
|
|
84
|
+
*/
|
|
85
|
+
export class SegmentTree {
|
|
86
|
+
/**
|
|
87
|
+
* @param values Initial leaf values (copied).
|
|
88
|
+
* @param combine Associative operation (e.g. addition, min, max).
|
|
89
|
+
* @param neutral Identity for `combine` (e.g. 0 for sum, Infinity for min).
|
|
90
|
+
*/
|
|
91
|
+
constructor(values, combine, neutral) {
|
|
92
|
+
this._inner = new GeneralSegmentTree(values, {
|
|
93
|
+
merge: combine,
|
|
94
|
+
neutral,
|
|
95
|
+
buildLeaf: (v) => v,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
get size() {
|
|
99
|
+
return this._inner.size;
|
|
100
|
+
}
|
|
101
|
+
/** Set index `index` to `value` and refresh ancestors. O(log n). */
|
|
102
|
+
update(index, value) {
|
|
103
|
+
this._inner.update(index, value);
|
|
104
|
+
}
|
|
105
|
+
/** Aggregate over inclusive range `[left, right]`. O(log n). */
|
|
106
|
+
query(left, right) {
|
|
107
|
+
return this._inner.query(left, right);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/** Range sum and point assign. Neutral element is `0`. */
|
|
111
|
+
export class SegmentTreeSum extends SegmentTree {
|
|
112
|
+
constructor(values) {
|
|
113
|
+
super(values, (a, b) => a + b, 0);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/** Range minimum and point assign. Neutral for out-of-range is `Infinity`. */
|
|
117
|
+
export class SegmentTreeMin extends SegmentTree {
|
|
118
|
+
constructor(values) {
|
|
119
|
+
super(values, (a, b) => Math.min(a, b), Infinity);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/** Range maximum and point assign. Neutral for out-of-range is `-Infinity`. */
|
|
123
|
+
export class SegmentTreeMax extends SegmentTree {
|
|
124
|
+
constructor(values) {
|
|
125
|
+
super(values, (a, b) => Math.max(a, b), -Infinity);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Lazy segment tree: add a constant to every element in `[left, right]` and query range sums.
|
|
130
|
+
* Both operations are O(log n).
|
|
131
|
+
*/
|
|
132
|
+
export class LazySegmentTreeSum {
|
|
133
|
+
constructor(values) {
|
|
134
|
+
this._n = values.length;
|
|
135
|
+
const cap = Math.max(1, 4 * this._n);
|
|
136
|
+
this._tree = new Array(cap).fill(0);
|
|
137
|
+
this._lazy = new Array(cap).fill(0);
|
|
138
|
+
if (this._n > 0) {
|
|
139
|
+
this._build(1, 0, this._n - 1, values);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
get size() {
|
|
143
|
+
return this._n;
|
|
144
|
+
}
|
|
145
|
+
/** Add `delta` to each element in inclusive `[left, right]`. O(log n). */
|
|
146
|
+
rangeAdd(left, right, delta) {
|
|
147
|
+
this._bounds(left);
|
|
148
|
+
this._bounds(right);
|
|
149
|
+
if (left > right)
|
|
150
|
+
throw new RangeError('left must be <= right');
|
|
151
|
+
this._rangeAdd(1, 0, this._n - 1, left, right, delta);
|
|
152
|
+
}
|
|
153
|
+
/** Sum of elements in inclusive `[left, right]`. O(log n). */
|
|
154
|
+
rangeSum(left, right) {
|
|
155
|
+
this._bounds(left);
|
|
156
|
+
this._bounds(right);
|
|
157
|
+
if (left > right)
|
|
158
|
+
throw new RangeError('left must be <= right');
|
|
159
|
+
return this._rangeSum(1, 0, this._n - 1, left, right);
|
|
160
|
+
}
|
|
161
|
+
/** Point assign (sets one index; clears lazy along the path). O(log n). */
|
|
162
|
+
set(index, value) {
|
|
163
|
+
this._bounds(index);
|
|
164
|
+
this._set(1, 0, this._n - 1, index, value);
|
|
165
|
+
}
|
|
166
|
+
_bounds(i) {
|
|
167
|
+
if (this._n === 0)
|
|
168
|
+
throw new RangeError('LazySegmentTreeSum is empty');
|
|
169
|
+
if (i < 0 || i >= this._n) {
|
|
170
|
+
throw new RangeError(`Index ${i} out of range [0, ${this._n - 1}]`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
_build(node, l, r, arr) {
|
|
174
|
+
if (l === r) {
|
|
175
|
+
this._tree[node] = arr[l];
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const mid = (l + r) >>> 1;
|
|
179
|
+
this._build(node * 2, l, mid, arr);
|
|
180
|
+
this._build(node * 2 + 1, mid + 1, r, arr);
|
|
181
|
+
this._tree[node] = this._tree[node * 2] + this._tree[node * 2 + 1];
|
|
182
|
+
}
|
|
183
|
+
_push(node, l, r) {
|
|
184
|
+
const lz = this._lazy[node];
|
|
185
|
+
if (lz === 0 || l === r)
|
|
186
|
+
return;
|
|
187
|
+
const mid = (l + r) >>> 1;
|
|
188
|
+
const leftLen = mid - l + 1;
|
|
189
|
+
const rightLen = r - mid;
|
|
190
|
+
this._lazy[node * 2] += lz;
|
|
191
|
+
this._lazy[node * 2 + 1] += lz;
|
|
192
|
+
this._tree[node * 2] += lz * leftLen;
|
|
193
|
+
this._tree[node * 2 + 1] += lz * rightLen;
|
|
194
|
+
this._lazy[node] = 0;
|
|
195
|
+
}
|
|
196
|
+
_rangeAdd(node, l, r, ql, qr, delta) {
|
|
197
|
+
if (qr < l || r < ql)
|
|
198
|
+
return;
|
|
199
|
+
if (ql <= l && r <= qr) {
|
|
200
|
+
this._tree[node] += delta * (r - l + 1);
|
|
201
|
+
this._lazy[node] += delta;
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
this._push(node, l, r);
|
|
205
|
+
const mid = (l + r) >>> 1;
|
|
206
|
+
this._rangeAdd(node * 2, l, mid, ql, qr, delta);
|
|
207
|
+
this._rangeAdd(node * 2 + 1, mid + 1, r, ql, qr, delta);
|
|
208
|
+
this._tree[node] = this._tree[node * 2] + this._tree[node * 2 + 1];
|
|
209
|
+
}
|
|
210
|
+
_rangeSum(node, l, r, ql, qr) {
|
|
211
|
+
if (qr < l || r < ql)
|
|
212
|
+
return 0;
|
|
213
|
+
if (ql <= l && r <= qr)
|
|
214
|
+
return this._tree[node];
|
|
215
|
+
this._push(node, l, r);
|
|
216
|
+
const mid = (l + r) >>> 1;
|
|
217
|
+
return (this._rangeSum(node * 2, l, mid, ql, qr) + this._rangeSum(node * 2 + 1, mid + 1, r, ql, qr));
|
|
218
|
+
}
|
|
219
|
+
_set(node, l, r, idx, value) {
|
|
220
|
+
if (l === r) {
|
|
221
|
+
this._tree[node] = value;
|
|
222
|
+
this._lazy[node] = 0;
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
this._push(node, l, r);
|
|
226
|
+
const mid = (l + r) >>> 1;
|
|
227
|
+
if (idx <= mid)
|
|
228
|
+
this._set(node * 2, l, mid, idx, value);
|
|
229
|
+
else
|
|
230
|
+
this._set(node * 2 + 1, mid + 1, r, idx, value);
|
|
231
|
+
this._tree[node] = this._tree[node * 2] + this._tree[node * 2 + 1];
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
//# sourceMappingURL=SegmentTree.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SegmentTree.js","sourceRoot":"","sources":["../../src/collections/SegmentTree.ts"],"names":[],"mappings":"AAgCA;;;GAGG;AACH,MAAM,OAAO,kBAAkB;IAQ7B,YAAY,MAAoB,EAAE,MAAsC;QACtE,IAAI,CAAC,EAAE,GAAG,MAAM,CAAC,MAAM,CAAC;QACxB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC;QAC3B,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,OAAO,CAAC;QAC/B,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,SAAS,CAAC;QACnC,IAA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E,CAAC;CACF"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { Vector } from './Vector.js';
|
|
2
2
|
export { Stack } from './Stack.js';
|
|
3
3
|
export { Queue } from './Queue.js';
|
|
4
|
+
export { Deque } from './Deque.js';
|
|
4
5
|
export { List, ListNode } from './List.js';
|
|
5
6
|
export { PriorityQueue } from './PriorityQueue.js';
|
|
6
7
|
export { OrderedMap } from './OrderedMap.js';
|
|
@@ -10,6 +11,8 @@ export { UnorderedSet } from './UnorderedSet.js';
|
|
|
10
11
|
export { OrderedMultiSet } from './OrderedMultiSet.js';
|
|
11
12
|
export { OrderedMultiMap } from './OrderedMultiMap.js';
|
|
12
13
|
export { RedBlackTree, RBNode } from './RedBlackTree.js';
|
|
14
|
+
export { GeneralSegmentTree, SegmentTree, SegmentTreeSum, SegmentTreeMin, SegmentTreeMax, LazySegmentTreeSum, } from './SegmentTree.js';
|
|
15
|
+
export type { GeneralSegmentTreeConfig, SegmentCombine, SegmentLeafBuild, SegmentMerge, } from './SegmentTree.js';
|
|
13
16
|
export type { WeightedEdge, AdjacencyList, WeightedAdjacencyList } from './Graph.js';
|
|
14
17
|
export { createAdjacencyList, createWeightedAdjacencyList, addEdge, deleteEdge } from './Graph.js';
|
|
15
18
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/collections/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,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kBAAkB,EAClB,WAAW,EACX,cAAc,EACd,cAAc,EACd,cAAc,EACd,kBAAkB,GACnB,MAAM,kBAAkB,CAAC;AAC1B,YAAY,EACV,wBAAwB,EACxB,cAAc,EACd,gBAAgB,EAChB,YAAY,GACb,MAAM,kBAAkB,CAAC;AAC1B,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AACrF,OAAO,EAAE,mBAAmB,EAAE,2BAA2B,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { Vector } from './Vector.js';
|
|
2
2
|
export { Stack } from './Stack.js';
|
|
3
3
|
export { Queue } from './Queue.js';
|
|
4
|
+
export { Deque } from './Deque.js';
|
|
4
5
|
export { List, ListNode } from './List.js';
|
|
5
6
|
export { PriorityQueue } from './PriorityQueue.js';
|
|
6
7
|
export { OrderedMap } from './OrderedMap.js';
|
|
@@ -10,5 +11,6 @@ export { UnorderedSet } from './UnorderedSet.js';
|
|
|
10
11
|
export { OrderedMultiSet } from './OrderedMultiSet.js';
|
|
11
12
|
export { OrderedMultiMap } from './OrderedMultiMap.js';
|
|
12
13
|
export { RedBlackTree, RBNode } from './RedBlackTree.js';
|
|
14
|
+
export { GeneralSegmentTree, SegmentTree, SegmentTreeSum, SegmentTreeMin, SegmentTreeMax, LazySegmentTreeSum, } from './SegmentTree.js';
|
|
13
15
|
export { createAdjacencyList, createWeightedAdjacencyList, addEdge, deleteEdge } from './Graph.js';
|
|
14
16
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/collections/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC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ACzD,OAAO,EACL,kBAAkB,EAClB,WAAW,EACX,cAAc,EACd,cAAc,EACd,cAAc,EACd,kBAAkB,GACnB,MAAM,kBAAkB,CAAC;AAQ1B,OAAO,EAAE,mBAAmB,EAAE,2BAA2B,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* @example
|
|
6
6
|
* import { Vector, Stack, Queue, List, sort, binarySearch, clamp } from 'typescript-dsa-stl';
|
|
7
7
|
*/
|
|
8
|
-
export { Vector, Stack, Queue, List, ListNode, PriorityQueue, OrderedMap, UnorderedMap, OrderedSet, UnorderedSet, OrderedMultiSet, OrderedMultiMap, addEdge, deleteEdge, createAdjacencyList, createWeightedAdjacencyList, } from './collections/index.js';
|
|
9
|
-
export type { WeightedEdge, AdjacencyList, WeightedAdjacencyList } from './collections/index.js';
|
|
8
|
+
export { Vector, Stack, Queue, Deque, List, ListNode, PriorityQueue, OrderedMap, UnorderedMap, OrderedSet, UnorderedSet, OrderedMultiSet, OrderedMultiMap, addEdge, deleteEdge, createAdjacencyList, createWeightedAdjacencyList, GeneralSegmentTree, SegmentTree, SegmentTreeSum, SegmentTreeMin, SegmentTreeMax, LazySegmentTreeSum, } from './collections/index.js';
|
|
9
|
+
export type { WeightedEdge, AdjacencyList, WeightedAdjacencyList, GeneralSegmentTreeConfig, SegmentCombine, SegmentLeafBuild, SegmentMerge, } from './collections/index.js';
|
|
10
10
|
export { sort, find, findIndex, transform, filter, reduce, reverse, unique, binarySearch, lowerBound, upperBound, min, max, partition, DisjointSetUnion, KnuthMorrisPratt, StringRollingHash, RabinKarp, RABIN_KARP_DEFAULT_MODS, breadthFirstSearch, depthFirstSearch, connectedComponents, dijkstra, reconstructPath, kruskalMST, } from './algorithms/index.js';
|
|
11
11
|
export type { WeightedUndirectedEdge, RabinKarpTripleMods } from './algorithms/index.js';
|
|
12
12
|
export { clamp, range, noop, identity, swap } from './utils/index.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;;;;;;GAMG;AAEH,OAAO,EACL,MAAM,EACN,KAAK,EACL,KAAK,EACL,IAAI,EACJ,QAAQ,EACR,aAAa,EACb,UAAU,EACV,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,eAAe,EACf,eAAe,EACf,OAAO,EACP,UAAU,EACV,mBAAmB,EACnB,2BAA2B,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,MAAM,EACN,KAAK,EACL,KAAK,EACL,KAAK,EACL,IAAI,EACJ,QAAQ,EACR,aAAa,EACb,UAAU,EACV,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,eAAe,EACf,eAAe,EACf,OAAO,EACP,UAAU,EACV,mBAAmB,EACnB,2BAA2B,EAC3B,kBAAkB,EAClB,WAAW,EACX,cAAc,EACd,cAAc,EACd,cAAc,EACd,kBAAkB,GACnB,MAAM,wBAAwB,CAAC;AAChC,YAAY,EACV,YAAY,EACZ,aAAa,EACb,qBAAqB,EACrB,wBAAwB,EACxB,cAAc,EACd,gBAAgB,EAChB,YAAY,GACb,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACL,IAAI,EACJ,IAAI,EACJ,SAAS,EACT,SAAS,EACT,MAAM,EACN,MAAM,EACN,OAAO,EACP,MAAM,EACN,YAAY,EACZ,UAAU,EACV,UAAU,EACV,GAAG,EACH,GAAG,EACH,SAAS,EACT,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,SAAS,EACT,uBAAuB,EACvB,kBAAkB,EAClB,gBAAgB,EAChB,mBAAmB,EACnB,QAAQ,EACR,eAAe,EACf,UAAU,GACX,MAAM,uBAAuB,CAAC;AAC/B,YAAY,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AACzF,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACtE,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAC9F,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @example
|
|
6
6
|
* import { Vector, Stack, Queue, List, sort, binarySearch, clamp } from 'typescript-dsa-stl';
|
|
7
7
|
*/
|
|
8
|
-
export { Vector, Stack, Queue, List, ListNode, PriorityQueue, OrderedMap, UnorderedMap, OrderedSet, UnorderedSet, OrderedMultiSet, OrderedMultiMap, addEdge, deleteEdge, createAdjacencyList, createWeightedAdjacencyList, } from './collections/index.js';
|
|
8
|
+
export { Vector, Stack, Queue, Deque, List, ListNode, PriorityQueue, OrderedMap, UnorderedMap, OrderedSet, UnorderedSet, OrderedMultiSet, OrderedMultiMap, addEdge, deleteEdge, createAdjacencyList, createWeightedAdjacencyList, GeneralSegmentTree, SegmentTree, SegmentTreeSum, SegmentTreeMin, SegmentTreeMax, LazySegmentTreeSum, } from './collections/index.js';
|
|
9
9
|
export { sort, find, findIndex, transform, filter, reduce, reverse, unique, binarySearch, lowerBound, upperBound, min, max, partition, DisjointSetUnion, KnuthMorrisPratt, StringRollingHash, RabinKarp, RABIN_KARP_DEFAULT_MODS, breadthFirstSearch, depthFirstSearch, connectedComponents, dijkstra, reconstructPath, kruskalMST, } from './algorithms/index.js';
|
|
10
10
|
export { clamp, range, noop, identity, swap } from './utils/index.js';
|
|
11
11
|
export { toArray } from './types/index.js';
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,MAAM,EACN,KAAK,EACL,KAAK,EACL,IAAI,EACJ,QAAQ,EACR,aAAa,EACb,UAAU,EACV,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,eAAe,EACf,eAAe,EACf,OAAO,EACP,UAAU,EACV,mBAAmB,EACnB,2BAA2B,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,MAAM,EACN,KAAK,EACL,KAAK,EACL,KAAK,EACL,IAAI,EACJ,QAAQ,EACR,aAAa,EACb,UAAU,EACV,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,eAAe,EACf,eAAe,EACf,OAAO,EACP,UAAU,EACV,mBAAmB,EACnB,2BAA2B,EAC3B,kBAAkB,EAClB,WAAW,EACX,cAAc,EACd,cAAc,EACd,cAAc,EACd,kBAAkB,GACnB,MAAM,wBAAwB,CAAC;AAUhC,OAAO,EACL,IAAI,EACJ,IAAI,EACJ,SAAS,EACT,SAAS,EACT,MAAM,EACN,MAAM,EACN,OAAO,EACP,MAAM,EACN,YAAY,EACZ,UAAU,EACV,UAAU,EACV,GAAG,EACH,GAAG,EACH,SAAS,EACT,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,SAAS,EACT,uBAAuB,EACvB,kBAAkB,EAClB,gBAAgB,EAChB,mBAAmB,EACnB,QAAQ,EACR,eAAe,EACf,UAAU,GACX,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAEtE,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typescript-dsa-stl",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"description": "STL-style data structures and algorithms for TypeScript: Vector, Stack, Queue, List, PriorityQueue, Map, Set, sort, binarySearch, graph utilities. Use like C++ STL.",
|
|
3
|
+
"version": "2.6.0",
|
|
4
|
+
"description": "STL-style data structures and algorithms for TypeScript: Vector, Stack, Queue, Deque (double-ended queue), List, PriorityQueue, Map, Set, sort, binarySearch, graph utilities. Use like C++ STL.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"module": "dist/index.js",
|
|
@@ -52,6 +52,8 @@
|
|
|
52
52
|
"vector",
|
|
53
53
|
"stack",
|
|
54
54
|
"queue",
|
|
55
|
+
"deque",
|
|
56
|
+
"double-ended-queue",
|
|
55
57
|
"list",
|
|
56
58
|
"priority-queue",
|
|
57
59
|
"map",
|