@procedures/collections 0.47.1 → 0.48.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -8
- package/README.md +3 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +1 -59
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -95
- package/dist/index.js.map +1 -1
- package/dist/storage/index.d.ts +2 -2
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/index.js +1 -1
- package/dist/storage/index.js.map +1 -1
- package/dist/storage/memory.d.ts +1 -1
- package/dist/storage/memory.d.ts.map +1 -1
- package/dist/storage/memory.js +1 -1
- package/dist/storage/memory.js.map +1 -1
- package/dist/testing/index.d.ts +1 -1
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/index.js +1 -1
- package/dist/testing/index.js.map +1 -1
- package/dist/testing/storage-contract.js +1 -1
- package/dist/testing/storage-contract.js.map +1 -1
- package/dist/undo/index.d.ts +1 -1
- package/dist/undo/index.js +1 -1
- package/dist/undo/undo-log.d.ts +1 -1
- package/dist/undo/undo-log.js +1 -1
- package/package.json +5 -5
- package/src/index.ts +338 -214
- package/src/storage/index.ts +2 -2
- package/src/storage/memory.ts +1 -1
- package/src/testing/index.ts +1 -1
- package/src/testing/storage-contract.ts +1 -1
- package/src/undo/index.ts +1 -1
- package/src/undo/undo-log.ts +1 -1
- package/dist/async/async-queue.d.ts +0 -170
- package/dist/async/async-queue.d.ts.map +0 -1
- package/dist/async/async-queue.js +0 -404
- package/dist/async/async-queue.js.map +0 -1
- package/dist/async/channels.d.ts +0 -211
- package/dist/async/channels.d.ts.map +0 -1
- package/dist/async/channels.js +0 -393
- package/dist/async/channels.js.map +0 -1
- package/dist/async/settled.d.ts +0 -23
- package/dist/async/settled.d.ts.map +0 -1
- package/dist/async/settled.js +0 -32
- package/dist/async/settled.js.map +0 -1
- package/dist/behaviors/bounded.d.ts +0 -79
- package/dist/behaviors/bounded.d.ts.map +0 -1
- package/dist/behaviors/bounded.js +0 -393
- package/dist/behaviors/bounded.js.map +0 -1
- package/dist/behaviors/composite.d.ts +0 -17
- package/dist/behaviors/composite.d.ts.map +0 -1
- package/dist/behaviors/composite.js +0 -93
- package/dist/behaviors/composite.js.map +0 -1
- package/dist/behaviors/evented.d.ts +0 -154
- package/dist/behaviors/evented.d.ts.map +0 -1
- package/dist/behaviors/evented.js +0 -299
- package/dist/behaviors/evented.js.map +0 -1
- package/dist/behaviors/lru.d.ts +0 -84
- package/dist/behaviors/lru.d.ts.map +0 -1
- package/dist/behaviors/lru.js +0 -376
- package/dist/behaviors/lru.js.map +0 -1
- package/dist/behaviors/readonly.d.ts +0 -73
- package/dist/behaviors/readonly.d.ts.map +0 -1
- package/dist/behaviors/readonly.js +0 -189
- package/dist/behaviors/readonly.js.map +0 -1
- package/dist/behaviors/safe.d.ts +0 -163
- package/dist/behaviors/safe.d.ts.map +0 -1
- package/dist/behaviors/safe.js +0 -176
- package/dist/behaviors/safe.js.map +0 -1
- package/dist/behaviors/synchronized.d.ts +0 -88
- package/dist/behaviors/synchronized.d.ts.map +0 -1
- package/dist/behaviors/synchronized.js +0 -285
- package/dist/behaviors/synchronized.js.map +0 -1
- package/dist/behaviors/ttl.d.ts +0 -118
- package/dist/behaviors/ttl.d.ts.map +0 -1
- package/dist/behaviors/ttl.js +0 -354
- package/dist/behaviors/ttl.js.map +0 -1
- package/dist/contracts/model.d.ts +0 -29
- package/dist/contracts/model.d.ts.map +0 -1
- package/dist/contracts/model.js +0 -26
- package/dist/contracts/model.js.map +0 -1
- package/dist/core/effects.d.ts +0 -176
- package/dist/core/effects.d.ts.map +0 -1
- package/dist/core/effects.js +0 -180
- package/dist/core/effects.js.map +0 -1
- package/dist/core/events.d.ts +0 -192
- package/dist/core/events.d.ts.map +0 -1
- package/dist/core/events.js +0 -291
- package/dist/core/events.js.map +0 -1
- package/dist/core/middleware.d.ts +0 -231
- package/dist/core/middleware.d.ts.map +0 -1
- package/dist/core/middleware.js +0 -161
- package/dist/core/middleware.js.map +0 -1
- package/dist/core/policies.d.ts +0 -220
- package/dist/core/policies.d.ts.map +0 -1
- package/dist/core/policies.js +0 -48
- package/dist/core/policies.js.map +0 -1
- package/dist/core/traits.d.ts +0 -142
- package/dist/core/traits.d.ts.map +0 -1
- package/dist/core/traits.js +0 -8
- package/dist/core/traits.js.map +0 -1
- package/dist/fx/collectors.d.ts +0 -193
- package/dist/fx/collectors.d.ts.map +0 -1
- package/dist/fx/collectors.js +0 -375
- package/dist/fx/collectors.js.map +0 -1
- package/dist/fx/iter.d.ts +0 -166
- package/dist/fx/iter.d.ts.map +0 -1
- package/dist/fx/iter.js +0 -421
- package/dist/fx/iter.js.map +0 -1
- package/dist/impl/array-deque.d.ts +0 -112
- package/dist/impl/array-deque.d.ts.map +0 -1
- package/dist/impl/array-deque.js +0 -380
- package/dist/impl/array-deque.js.map +0 -1
- package/dist/impl/array-list.d.ts +0 -101
- package/dist/impl/array-list.d.ts.map +0 -1
- package/dist/impl/array-list.js +0 -387
- package/dist/impl/array-list.js.map +0 -1
- package/dist/impl/hash-map.d.ts +0 -149
- package/dist/impl/hash-map.d.ts.map +0 -1
- package/dist/impl/hash-map.js +0 -473
- package/dist/impl/hash-map.js.map +0 -1
- package/dist/impl/hash-set.d.ts +0 -125
- package/dist/impl/hash-set.d.ts.map +0 -1
- package/dist/impl/hash-set.js +0 -264
- package/dist/impl/hash-set.js.map +0 -1
- package/dist/impl/linked-hash-map.d.ts +0 -141
- package/dist/impl/linked-hash-map.d.ts.map +0 -1
- package/dist/impl/linked-hash-map.js +0 -442
- package/dist/impl/linked-hash-map.js.map +0 -1
- package/dist/impl/linked-list.d.ts +0 -109
- package/dist/impl/linked-list.d.ts.map +0 -1
- package/dist/impl/linked-list.js +0 -494
- package/dist/impl/linked-list.js.map +0 -1
- package/dist/impl/priority-queue.d.ts +0 -139
- package/dist/impl/priority-queue.d.ts.map +0 -1
- package/dist/impl/priority-queue.js +0 -343
- package/dist/impl/priority-queue.js.map +0 -1
- package/dist/impl/summary-tree.d.ts +0 -146
- package/dist/impl/summary-tree.d.ts.map +0 -1
- package/dist/impl/summary-tree.js +0 -524
- package/dist/impl/summary-tree.js.map +0 -1
- package/dist/impl/tree-map.d.ts +0 -153
- package/dist/impl/tree-map.d.ts.map +0 -1
- package/dist/impl/tree-map.js +0 -788
- package/dist/impl/tree-map.js.map +0 -1
- package/dist/impl/tree-set.d.ts +0 -112
- package/dist/impl/tree-set.d.ts.map +0 -1
- package/dist/impl/tree-set.js +0 -374
- package/dist/impl/tree-set.js.map +0 -1
- package/dist/interfaces/collection.d.ts +0 -119
- package/dist/interfaces/collection.d.ts.map +0 -1
- package/dist/interfaces/collection.js +0 -8
- package/dist/interfaces/collection.js.map +0 -1
- package/dist/interfaces/list.d.ts +0 -247
- package/dist/interfaces/list.d.ts.map +0 -1
- package/dist/interfaces/list.js +0 -8
- package/dist/interfaces/list.js.map +0 -1
- package/dist/interfaces/map.d.ts +0 -344
- package/dist/interfaces/map.d.ts.map +0 -1
- package/dist/interfaces/map.js +0 -8
- package/dist/interfaces/map.js.map +0 -1
- package/dist/interfaces/queue.d.ts +0 -304
- package/dist/interfaces/queue.d.ts.map +0 -1
- package/dist/interfaces/queue.js +0 -8
- package/dist/interfaces/queue.js.map +0 -1
- package/dist/interfaces/set.d.ts +0 -137
- package/dist/interfaces/set.d.ts.map +0 -1
- package/dist/interfaces/set.js +0 -8
- package/dist/interfaces/set.js.map +0 -1
- package/dist/order/index.d.ts +0 -7
- package/dist/order/index.d.ts.map +0 -1
- package/dist/order/index.js +0 -7
- package/dist/order/index.js.map +0 -1
- package/dist/order/rank.d.ts +0 -100
- package/dist/order/rank.d.ts.map +0 -1
- package/dist/order/rank.js +0 -286
- package/dist/order/rank.js.map +0 -1
- package/dist/storage/listeners.d.ts +0 -39
- package/dist/storage/listeners.d.ts.map +0 -1
- package/dist/storage/listeners.js +0 -65
- package/dist/storage/listeners.js.map +0 -1
- package/dist/testing/check.d.ts +0 -25
- package/dist/testing/check.d.ts.map +0 -1
- package/dist/testing/check.js +0 -85
- package/dist/testing/check.js.map +0 -1
- package/dist/utils/defaults.d.ts +0 -135
- package/dist/utils/defaults.d.ts.map +0 -1
- package/dist/utils/defaults.js +0 -322
- package/dist/utils/defaults.js.map +0 -1
- package/dist/utils/factories.d.ts +0 -144
- package/dist/utils/factories.d.ts.map +0 -1
- package/dist/utils/factories.js +0 -293
- package/dist/utils/factories.js.map +0 -1
- package/dist/utils/helpers.d.ts +0 -137
- package/dist/utils/helpers.d.ts.map +0 -1
- package/dist/utils/helpers.js +0 -221
- package/dist/utils/helpers.js.map +0 -1
- package/src/async/async-queue.ts +0 -481
- package/src/async/channels.ts +0 -450
- package/src/async/settled.ts +0 -41
- package/src/behaviors/bounded.ts +0 -485
- package/src/behaviors/composite.ts +0 -99
- package/src/behaviors/evented.ts +0 -464
- package/src/behaviors/lru.ts +0 -447
- package/src/behaviors/readonly.ts +0 -236
- package/src/behaviors/safe.ts +0 -372
- package/src/behaviors/synchronized.ts +0 -316
- package/src/behaviors/ttl.ts +0 -462
- package/src/contracts/model.ts +0 -44
- package/src/core/effects.ts +0 -323
- package/src/core/events.ts +0 -356
- package/src/core/middleware.ts +0 -415
- package/src/core/policies.ts +0 -316
- package/src/core/traits.ts +0 -164
- package/src/fx/collectors.ts +0 -500
- package/src/fx/iter.ts +0 -536
- package/src/impl/array-deque.ts +0 -463
- package/src/impl/array-list.ts +0 -490
- package/src/impl/hash-map.ts +0 -623
- package/src/impl/hash-set.ts +0 -342
- package/src/impl/linked-hash-map.ts +0 -572
- package/src/impl/linked-list.ts +0 -606
- package/src/impl/priority-queue.ts +0 -448
- package/src/impl/summary-tree.ts +0 -586
- package/src/impl/tree-map.ts +0 -912
- package/src/impl/tree-set.ts +0 -469
- package/src/interfaces/collection.ts +0 -141
- package/src/interfaces/list.ts +0 -281
- package/src/interfaces/map.ts +0 -405
- package/src/interfaces/queue.ts +0 -353
- package/src/interfaces/set.ts +0 -158
- package/src/order/index.ts +0 -6
- package/src/order/rank.ts +0 -271
- package/src/storage/listeners.ts +0 -72
- package/src/testing/check.ts +0 -81
- package/src/utils/defaults.ts +0 -345
- package/src/utils/factories.ts +0 -357
- package/src/utils/helpers.ts +0 -248
|
@@ -1,343 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* PriorityQueue implementation - A binary heap-based priority queue.
|
|
3
|
-
*
|
|
4
|
-
* Elements are ordered according to their natural ordering (via comparator).
|
|
5
|
-
* The head of the queue is the least element according to the comparator.
|
|
6
|
-
* Provides O(log n) enqueue and O(log n) dequeue operations.
|
|
7
|
-
* Mirrors java.util.PriorityQueue.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* const pq = priorityQueue<number>((a, b) => a - b) // Min heap
|
|
11
|
-
* pq.offer(5)
|
|
12
|
-
* pq.offer(2)
|
|
13
|
-
* pq.offer(8)
|
|
14
|
-
* pq.poll() // 2 (smallest element)
|
|
15
|
-
*/
|
|
16
|
-
import { defaultEq, defaultCompare } from "../utils/defaults.js";
|
|
17
|
-
/**
|
|
18
|
-
* PriorityQueue<T> - Binary heap implementation of priority queue.
|
|
19
|
-
*
|
|
20
|
-
* Elements are always retrieved in priority order (according to comparator).
|
|
21
|
-
* Not a FIFO queue - order depends on priority, not insertion order.
|
|
22
|
-
*
|
|
23
|
-
* The heap is a min heap by default (smallest element has highest priority).
|
|
24
|
-
* For max heap, use reversed comparator: (a, b) => compare(b, a)
|
|
25
|
-
*
|
|
26
|
-
* Not thread-safe. Use synchronized() behavior for concurrent access.
|
|
27
|
-
*/
|
|
28
|
-
export class PriorityQueue {
|
|
29
|
-
_heap;
|
|
30
|
-
_compare;
|
|
31
|
-
_eq;
|
|
32
|
-
constructor(options = {}) {
|
|
33
|
-
const { compare = defaultCompare, eq = defaultEq, } = options;
|
|
34
|
-
// The heap starts empty: a JavaScript array grows by itself, so initialCapacity is only a
|
|
35
|
-
// hint. (Before, `new Array(initialCapacity)` made 11 holes: an empty queue had size 11,
|
|
36
|
-
// and the heap compared holes. BUGS-2026-07 C9.)
|
|
37
|
-
this._heap = [];
|
|
38
|
-
this._compare = compare;
|
|
39
|
-
this._eq = eq;
|
|
40
|
-
}
|
|
41
|
-
// ========================================================================
|
|
42
|
-
// Size
|
|
43
|
-
// ========================================================================
|
|
44
|
-
get size() {
|
|
45
|
-
return this._heap.length;
|
|
46
|
-
}
|
|
47
|
-
get isEmpty() {
|
|
48
|
-
return this._heap.length === 0;
|
|
49
|
-
}
|
|
50
|
-
get eq() {
|
|
51
|
-
return this._eq;
|
|
52
|
-
}
|
|
53
|
-
get compare() {
|
|
54
|
-
return this._compare;
|
|
55
|
-
}
|
|
56
|
-
get comparator() {
|
|
57
|
-
return this._compare;
|
|
58
|
-
}
|
|
59
|
-
// ========================================================================
|
|
60
|
-
// Priority queue operations
|
|
61
|
-
// ========================================================================
|
|
62
|
-
offer(element) {
|
|
63
|
-
this._heap.push(element);
|
|
64
|
-
this.bubbleUp(this._heap.length - 1);
|
|
65
|
-
return true;
|
|
66
|
-
}
|
|
67
|
-
poll() {
|
|
68
|
-
if (this.isEmpty) {
|
|
69
|
-
throw new Error("Priority queue is empty");
|
|
70
|
-
}
|
|
71
|
-
return this.pollOrUndefined();
|
|
72
|
-
}
|
|
73
|
-
pollOrUndefined() {
|
|
74
|
-
if (this.isEmpty) {
|
|
75
|
-
return undefined;
|
|
76
|
-
}
|
|
77
|
-
const result = this._heap[0];
|
|
78
|
-
if (this._heap.length === 1) {
|
|
79
|
-
this._heap.pop();
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
this._heap[0] = this._heap.pop();
|
|
83
|
-
this.bubbleDown(0);
|
|
84
|
-
}
|
|
85
|
-
return result;
|
|
86
|
-
}
|
|
87
|
-
peek() {
|
|
88
|
-
if (this.isEmpty) {
|
|
89
|
-
throw new Error("Priority queue is empty");
|
|
90
|
-
}
|
|
91
|
-
return this._heap[0];
|
|
92
|
-
}
|
|
93
|
-
peekOrUndefined() {
|
|
94
|
-
return this.isEmpty ? undefined : this._heap[0];
|
|
95
|
-
}
|
|
96
|
-
// ========================================================================
|
|
97
|
-
// Queue interface compatibility
|
|
98
|
-
// ========================================================================
|
|
99
|
-
enqueue(element) {
|
|
100
|
-
return this.offer(element);
|
|
101
|
-
}
|
|
102
|
-
dequeue() {
|
|
103
|
-
return this.poll();
|
|
104
|
-
}
|
|
105
|
-
// ========================================================================
|
|
106
|
-
// Collection operations
|
|
107
|
-
// ========================================================================
|
|
108
|
-
add(element) {
|
|
109
|
-
return this.offer(element);
|
|
110
|
-
}
|
|
111
|
-
addAll(other) {
|
|
112
|
-
let modified = false;
|
|
113
|
-
for (const element of other) {
|
|
114
|
-
this.offer(element);
|
|
115
|
-
modified = true;
|
|
116
|
-
}
|
|
117
|
-
return modified;
|
|
118
|
-
}
|
|
119
|
-
contains(element) {
|
|
120
|
-
for (const item of this._heap) {
|
|
121
|
-
if (this._eq(item, element)) {
|
|
122
|
-
return true;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
return false;
|
|
126
|
-
}
|
|
127
|
-
containsAll(other) {
|
|
128
|
-
for (const element of other) {
|
|
129
|
-
if (!this.contains(element)) {
|
|
130
|
-
return false;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
return true;
|
|
134
|
-
}
|
|
135
|
-
remove(element) {
|
|
136
|
-
const index = this._heap.findIndex((item) => this._eq(item, element));
|
|
137
|
-
if (index === -1) {
|
|
138
|
-
return false;
|
|
139
|
-
}
|
|
140
|
-
if (index === this._heap.length - 1) {
|
|
141
|
-
this._heap.pop();
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
this._heap[index] = this._heap.pop();
|
|
145
|
-
this.bubbleDown(index);
|
|
146
|
-
this.bubbleUp(index);
|
|
147
|
-
}
|
|
148
|
-
return true;
|
|
149
|
-
}
|
|
150
|
-
removeAll(other) {
|
|
151
|
-
let modified = false;
|
|
152
|
-
for (const element of other) {
|
|
153
|
-
while (this.remove(element)) {
|
|
154
|
-
modified = true;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
return modified;
|
|
158
|
-
}
|
|
159
|
-
retainAll(other) {
|
|
160
|
-
const toRetain = new Set(other);
|
|
161
|
-
const newHeap = [];
|
|
162
|
-
for (const element of this._heap) {
|
|
163
|
-
let shouldRetain = false;
|
|
164
|
-
for (const retainElement of toRetain) {
|
|
165
|
-
if (this._eq(element, retainElement)) {
|
|
166
|
-
shouldRetain = true;
|
|
167
|
-
break;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
if (shouldRetain) {
|
|
171
|
-
newHeap.push(element);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
const modified = newHeap.length !== this._heap.length;
|
|
175
|
-
this._heap = newHeap;
|
|
176
|
-
this.heapify();
|
|
177
|
-
return modified;
|
|
178
|
-
}
|
|
179
|
-
clear() {
|
|
180
|
-
this._heap = [];
|
|
181
|
-
}
|
|
182
|
-
// ========================================================================
|
|
183
|
-
// Iteration
|
|
184
|
-
// ========================================================================
|
|
185
|
-
/**
|
|
186
|
-
* Iterator returns elements in arbitrary order (not priority order).
|
|
187
|
-
* To get elements in priority order, repeatedly call poll().
|
|
188
|
-
*/
|
|
189
|
-
*[Symbol.iterator]() {
|
|
190
|
-
yield* this._heap;
|
|
191
|
-
}
|
|
192
|
-
forEach(action) {
|
|
193
|
-
this._heap.forEach((element, index) => action(element, index));
|
|
194
|
-
}
|
|
195
|
-
/**
|
|
196
|
-
* Returns elements in arbitrary heap order (not sorted).
|
|
197
|
-
*/
|
|
198
|
-
toArray() {
|
|
199
|
-
return this._heap.slice();
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Returns elements in priority order (sorted).
|
|
203
|
-
* This is a destructive operation that drains the queue.
|
|
204
|
-
*/
|
|
205
|
-
toSortedArray() {
|
|
206
|
-
const result = [];
|
|
207
|
-
const clone = this.clone();
|
|
208
|
-
while (!clone.isEmpty) {
|
|
209
|
-
result.push(clone.poll());
|
|
210
|
-
}
|
|
211
|
-
return result;
|
|
212
|
-
}
|
|
213
|
-
// ========================================================================
|
|
214
|
-
// Heap operations
|
|
215
|
-
// ========================================================================
|
|
216
|
-
/**
|
|
217
|
-
* Bubbles an element up the heap to maintain heap property.
|
|
218
|
-
*/
|
|
219
|
-
bubbleUp(index) {
|
|
220
|
-
const element = this._heap[index];
|
|
221
|
-
while (index > 0) {
|
|
222
|
-
const parentIndex = Math.floor((index - 1) / 2);
|
|
223
|
-
const parent = this._heap[parentIndex];
|
|
224
|
-
if (this._compare(element, parent) >= 0) {
|
|
225
|
-
break;
|
|
226
|
-
}
|
|
227
|
-
this._heap[index] = parent;
|
|
228
|
-
index = parentIndex;
|
|
229
|
-
}
|
|
230
|
-
this._heap[index] = element;
|
|
231
|
-
}
|
|
232
|
-
/**
|
|
233
|
-
* Bubbles an element down the heap to maintain heap property.
|
|
234
|
-
*
|
|
235
|
-
* The element moves down through a hole: a smaller child moves up into the hole, and the
|
|
236
|
-
* element goes into the last hole. The children are compared with the element. (Before, they
|
|
237
|
-
* were compared with `_heap[index]`, which after the first step is a copy of the child that
|
|
238
|
-
* moved up: a poll could leave a larger element above a smaller one.)
|
|
239
|
-
*/
|
|
240
|
-
bubbleDown(index) {
|
|
241
|
-
const length = this._heap.length;
|
|
242
|
-
const element = this._heap[index];
|
|
243
|
-
while (true) {
|
|
244
|
-
const leftChildIndex = 2 * index + 1;
|
|
245
|
-
const rightChildIndex = 2 * index + 2;
|
|
246
|
-
if (leftChildIndex >= length) {
|
|
247
|
-
break;
|
|
248
|
-
}
|
|
249
|
-
let childIndex = leftChildIndex;
|
|
250
|
-
if (rightChildIndex < length &&
|
|
251
|
-
this._compare(this._heap[rightChildIndex], this._heap[leftChildIndex]) < 0) {
|
|
252
|
-
childIndex = rightChildIndex;
|
|
253
|
-
}
|
|
254
|
-
if (this._compare(this._heap[childIndex], element) >= 0) {
|
|
255
|
-
break;
|
|
256
|
-
}
|
|
257
|
-
this._heap[index] = this._heap[childIndex];
|
|
258
|
-
index = childIndex;
|
|
259
|
-
}
|
|
260
|
-
this._heap[index] = element;
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* Rebuilds the heap from an arbitrary array.
|
|
264
|
-
* Uses Floyd's algorithm (O(n) time).
|
|
265
|
-
*/
|
|
266
|
-
heapify() {
|
|
267
|
-
// Start from last non-leaf node and bubble down
|
|
268
|
-
for (let i = Math.floor(this._heap.length / 2) - 1; i >= 0; i--) {
|
|
269
|
-
this.bubbleDown(i);
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
// ========================================================================
|
|
273
|
-
// Utility methods
|
|
274
|
-
// ========================================================================
|
|
275
|
-
/**
|
|
276
|
-
* Creates a shallow copy of this priority queue.
|
|
277
|
-
*/
|
|
278
|
-
clone() {
|
|
279
|
-
const clone = new PriorityQueue({
|
|
280
|
-
compare: this._compare,
|
|
281
|
-
eq: this._eq,
|
|
282
|
-
});
|
|
283
|
-
clone._heap = this._heap.slice();
|
|
284
|
-
return clone;
|
|
285
|
-
}
|
|
286
|
-
/**
|
|
287
|
-
* Validates the heap property (for debugging).
|
|
288
|
-
* Returns true if the heap is valid.
|
|
289
|
-
*/
|
|
290
|
-
validate() {
|
|
291
|
-
for (let i = 0; i < this._heap.length; i++) {
|
|
292
|
-
const leftChildIndex = 2 * i + 1;
|
|
293
|
-
const rightChildIndex = 2 * i + 2;
|
|
294
|
-
if (leftChildIndex < this._heap.length &&
|
|
295
|
-
this._compare(this._heap[i], this._heap[leftChildIndex]) > 0) {
|
|
296
|
-
return false;
|
|
297
|
-
}
|
|
298
|
-
if (rightChildIndex < this._heap.length &&
|
|
299
|
-
this._compare(this._heap[i], this._heap[rightChildIndex]) > 0) {
|
|
300
|
-
return false;
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
return true;
|
|
304
|
-
}
|
|
305
|
-
/**
|
|
306
|
-
* Returns a string representation for debugging.
|
|
307
|
-
*/
|
|
308
|
-
toString() {
|
|
309
|
-
return `PriorityQueue[${this._heap.join(", ")}]`;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
/**
|
|
313
|
-
* Factory function to create a PriorityQueue.
|
|
314
|
-
*
|
|
315
|
-
* @example
|
|
316
|
-
* // Min heap (default)
|
|
317
|
-
* const minHeap = priorityQueue<number>()
|
|
318
|
-
*
|
|
319
|
-
* // Max heap
|
|
320
|
-
* const maxHeap = priorityQueue<number>((a, b) => b - a)
|
|
321
|
-
*
|
|
322
|
-
* // From array
|
|
323
|
-
* const pq = priorityQueue([5, 2, 8, 1, 9])
|
|
324
|
-
*
|
|
325
|
-
* // With custom comparator
|
|
326
|
-
* const pq = priorityQueue<Task>((a, b) => a.priority - b.priority)
|
|
327
|
-
*/
|
|
328
|
-
export function priorityQueue(optionsOrElementsOrComparator) {
|
|
329
|
-
// If it's a function, treat it as a comparator
|
|
330
|
-
if (typeof optionsOrElementsOrComparator === "function") {
|
|
331
|
-
return new PriorityQueue({
|
|
332
|
-
compare: optionsOrElementsOrComparator,
|
|
333
|
-
});
|
|
334
|
-
}
|
|
335
|
-
// Check if it's an iterable (not options)
|
|
336
|
-
if (optionsOrElementsOrComparator && Symbol.iterator in optionsOrElementsOrComparator) {
|
|
337
|
-
const pq = new PriorityQueue();
|
|
338
|
-
pq.addAll(optionsOrElementsOrComparator);
|
|
339
|
-
return pq;
|
|
340
|
-
}
|
|
341
|
-
return new PriorityQueue(optionsOrElementsOrComparator);
|
|
342
|
-
}
|
|
343
|
-
//# sourceMappingURL=priority-queue.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"priority-queue.js","sourceRoot":"","sources":["../../src/impl/priority-queue.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAIH,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AA0BjE;;;;;;;;;;GAUG;AACH,MAAM,OAAO,aAA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a,CAAI;YAC1B,OAAO,EAAE,6BAA2C;SACrD,CAAC,CAAC;IACL,CAAC;IAED,0CAA0C;IAC1C,IAAI,6BAA6B,IAAI,MAAM,CAAC,QAAQ,IAAI,6BAA6B,EAAE,CAAC;QACtF,MAAM,EAAE,GAAG,IAAI,aAAa,EAAK,CAAC;QAClC,EAAE,CAAC,MAAM,CAAC,6BAA4C,CAAC,CAAC;QACxD,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,OAAO,IAAI,aAAa,CACtB,6BAAwD,CACzD,CAAC;AACJ,CAAC"}
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SummaryTree: a persistent sequence of leaves with a summary of each part.
|
|
3
|
-
*
|
|
4
|
-
* The tree is a balanced binary tree (AVL) with one leaf in each node. Each node keeps the summary of its subtree.
|
|
5
|
-
* A summary is a value of a monoid: the caller gives the monoid as data, with `empty` and `combine`. The order of the
|
|
6
|
-
* leaves is kept, so `combine` must be associative, but it can be non-commutative.
|
|
7
|
-
*
|
|
8
|
-
* Each operation returns a new tree and keeps the old tree. The two trees share the nodes that did not change.
|
|
9
|
-
* Concatenation, a split at an index or at a summary search, an edit and a lookup are O(log n).
|
|
10
|
-
*
|
|
11
|
-
* The algorithms are the "join" algorithms of Blelloch, Ferizovic and Sun, "Just Join for Parallel Ordered Sets"
|
|
12
|
-
* (SPAA 2016).
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* // The number of leaves and the total length of the strings
|
|
16
|
-
* const spec = {
|
|
17
|
-
* monoid: { empty: { count: 0, length: 0 }, combine: (a, b) => ({ count: a.count + b.count, length: a.length + b.length }) },
|
|
18
|
-
* measure: (leaf: string) => ({ count: 1, length: leaf.length }),
|
|
19
|
-
* };
|
|
20
|
-
* const tree = summaryTree(spec, ["ab", "cde", "f"]);
|
|
21
|
-
* tree.summary; // { count: 3, length: 6 }
|
|
22
|
-
* const [left, right] = tree.split((prefix) => prefix.length > 2); // left: ["ab"], right: ["cde", "f"]
|
|
23
|
-
*/
|
|
24
|
-
/** A monoid as data: the identity value `empty` and an associative operation `combine`. */
|
|
25
|
-
export interface SummaryMonoid<Summary> {
|
|
26
|
-
/** The summary of a sequence with no leaf. `combine(empty, s)` and `combine(s, empty)` are equal to `s`. */
|
|
27
|
-
readonly empty: Summary;
|
|
28
|
-
/** This function gives the summary of a sequence from the summaries of its two parts, left first. */
|
|
29
|
-
readonly combine: (left: Summary, right: Summary) => Summary;
|
|
30
|
-
}
|
|
31
|
-
/** The data that a tree needs: the monoid of the summaries and the summary of one leaf. */
|
|
32
|
-
export interface SummarySpec<Leaf, Summary> {
|
|
33
|
-
readonly monoid: SummaryMonoid<Summary>;
|
|
34
|
-
/** This function gives the summary of one leaf. */
|
|
35
|
-
readonly measure: (leaf: Leaf) => Summary;
|
|
36
|
-
/**
|
|
37
|
-
* This optional function joins two neighbor leaves at a seam into one leaf, or gives `undefined` to keep both. A seam
|
|
38
|
-
* is the place where `concat`, `append`, `prepend`, `splice`, `insert` or `remove` puts two leaves next to each other.
|
|
39
|
-
* The tree calls it once at each seam, and not between the leaves that one call inserts. `set` does not call it.
|
|
40
|
-
*/
|
|
41
|
-
readonly merge?: (left: Leaf, right: Leaf) => Leaf | undefined;
|
|
42
|
-
}
|
|
43
|
-
/** The result of a summary search: the first leaf whose prefix summary satisfies the predicate. */
|
|
44
|
-
export interface SummarySearch<Leaf, Summary> {
|
|
45
|
-
/** The index of the leaf. */
|
|
46
|
-
readonly index: number;
|
|
47
|
-
readonly leaf: Leaf;
|
|
48
|
-
/** The summary of the leaves before the leaf. */
|
|
49
|
-
readonly before: Summary;
|
|
50
|
-
/** The summary of the leaves before the leaf, and of the leaf. */
|
|
51
|
-
readonly through: Summary;
|
|
52
|
-
}
|
|
53
|
-
/** The result of `locate`: the leaf that holds a position of an additive key, and the position in that leaf. */
|
|
54
|
-
export interface SummaryLocation<Leaf> {
|
|
55
|
-
/** The index of the leaf. */
|
|
56
|
-
readonly index: number;
|
|
57
|
-
readonly leaf: Leaf;
|
|
58
|
-
/** The position in the leaf: the target minus the key of the leaves before the leaf. */
|
|
59
|
-
readonly offset: number;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* A persistent sequence of leaves with a summary of each part. Make one with `summaryTree`. Each method that changes
|
|
63
|
-
* the sequence returns a new tree.
|
|
64
|
-
*/
|
|
65
|
-
export declare class SummaryTree<Leaf, Summary> implements Iterable<Leaf> {
|
|
66
|
-
private readonly builder;
|
|
67
|
-
private readonly root;
|
|
68
|
-
private constructor();
|
|
69
|
-
/** This function makes a tree of a spec from leaves, as `summaryTree` does. */
|
|
70
|
-
static of<Leaf, Summary>(spec: SummarySpec<Leaf, Summary>, leaves?: Iterable<Leaf>): SummaryTree<Leaf, Summary>;
|
|
71
|
-
private wrap;
|
|
72
|
-
private treeOf;
|
|
73
|
-
/** The spec of the tree: its monoid and its measure. */
|
|
74
|
-
get spec(): SummarySpec<Leaf, Summary>;
|
|
75
|
-
/** The number of leaves. */
|
|
76
|
-
get size(): number;
|
|
77
|
-
get isEmpty(): boolean;
|
|
78
|
-
/** The summary of all the leaves, in their order. An empty tree has the summary `monoid.empty`. */
|
|
79
|
-
get summary(): Summary;
|
|
80
|
-
/** The height of the tree: 0 for an empty tree. The height is at most 1.45 log2(size + 2). */
|
|
81
|
-
get height(): number;
|
|
82
|
-
/**
|
|
83
|
-
* This function gives the leaf at an index, or `undefined` when the index is not in the tree. A negative index counts
|
|
84
|
-
* from the end, as `Array.prototype.at` does.
|
|
85
|
-
*/
|
|
86
|
-
at(index: number): Leaf | undefined;
|
|
87
|
-
/** This function gives the summary of the leaves from `start` to `end`, with `end` not included. */
|
|
88
|
-
summaryOf(start?: number, end?: number): Summary;
|
|
89
|
-
/** This function gives the tree of the leaves of this tree, then the leaves of `other`. The two trees must have the same spec. */
|
|
90
|
-
concat(other: SummaryTree<Leaf, Summary> | Iterable<Leaf>): SummaryTree<Leaf, Summary>;
|
|
91
|
-
/** This function splits the tree into its first `index` leaves and the other leaves. */
|
|
92
|
-
splitAt(index: number): [SummaryTree<Leaf, Summary>, SummaryTree<Leaf, Summary>];
|
|
93
|
-
/**
|
|
94
|
-
* This function splits the tree before the first leaf whose prefix summary satisfies the predicate. The prefix of a
|
|
95
|
-
* leaf includes the leaf. The predicate must be monotone: after it is true for a prefix, it is true for each longer
|
|
96
|
-
* prefix. When no prefix satisfies it, the right tree is empty.
|
|
97
|
-
*/
|
|
98
|
-
split(predicate: (prefix: Summary) => boolean): [SummaryTree<Leaf, Summary>, SummaryTree<Leaf, Summary>];
|
|
99
|
-
/**
|
|
100
|
-
* This function finds the first leaf whose prefix summary satisfies a monotone predicate, as `split` does. It changes
|
|
101
|
-
* nothing. It gives `undefined` when no prefix satisfies the predicate.
|
|
102
|
-
*/
|
|
103
|
-
search(predicate: (prefix: Summary) => boolean): SummarySearch<Leaf, Summary> | undefined;
|
|
104
|
-
/**
|
|
105
|
-
* This function finds the leaf that holds a position of an additive key, and it calls no `combine`. The key must be
|
|
106
|
-
* additive and not negative: `key(combine(a, b))` is `key(a) + key(b)`. The function descends by the key of each left
|
|
107
|
-
* subtree and of each leaf, and subtracts it from the target. It gives the first leaf whose prefix key is more than
|
|
108
|
-
* the target, or `undefined` when the target is not in `[0, key(summary))`.
|
|
109
|
-
*
|
|
110
|
-
* @example
|
|
111
|
-
* // The leaf and the offset in it of the code unit at position 1000 of a rope
|
|
112
|
-
* const at = rope.locate((s) => s.length, 1000);
|
|
113
|
-
*/
|
|
114
|
-
locate(key: (summary: Summary) => number, target: number): SummaryLocation<Leaf> | undefined;
|
|
115
|
-
/** This function gives the tree of the leaves from `start` to `end`, with `end` not included. A negative index counts from the end. */
|
|
116
|
-
slice(start?: number, end?: number): SummaryTree<Leaf, Summary>;
|
|
117
|
-
/**
|
|
118
|
-
* This function removes `deleteCount` leaves at `start` and puts the leaves of `inserted` there, as
|
|
119
|
-
* `Array.prototype.splice` does. It returns the new tree, not the removed leaves.
|
|
120
|
-
*/
|
|
121
|
-
splice(start: number, deleteCount?: number, inserted?: SummaryTree<Leaf, Summary> | Iterable<Leaf>): SummaryTree<Leaf, Summary>;
|
|
122
|
-
/** This function puts the leaves of `inserted` before the leaf at `index`. */
|
|
123
|
-
insert(index: number, inserted: SummaryTree<Leaf, Summary> | Iterable<Leaf>): SummaryTree<Leaf, Summary>;
|
|
124
|
-
/** This function removes the leaves from `start` to `end`, with `end` not included. */
|
|
125
|
-
remove(start: number, end?: number): SummaryTree<Leaf, Summary>;
|
|
126
|
-
/**
|
|
127
|
-
* This function replaces the leaf at an index. A negative index counts from the end, as `Array.prototype.with` does.
|
|
128
|
-
* An index that is not in the tree changes nothing. It copies only the path to the leaf, and it does not call
|
|
129
|
-
* `spec.merge`.
|
|
130
|
-
*/
|
|
131
|
-
set(index: number, leaf: Leaf): SummaryTree<Leaf, Summary>;
|
|
132
|
-
/** This function adds leaves after the last leaf. */
|
|
133
|
-
append(...leaves: Leaf[]): SummaryTree<Leaf, Summary>;
|
|
134
|
-
/** This function adds leaves before the first leaf. */
|
|
135
|
-
prepend(...leaves: Leaf[]): SummaryTree<Leaf, Summary>;
|
|
136
|
-
/** This function gives the leaves in their order. */
|
|
137
|
-
[Symbol.iterator](): Iterator<Leaf>;
|
|
138
|
-
/** This function gives the leaves in an array. */
|
|
139
|
-
toArray(): Leaf[];
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* This function makes a persistent tree of leaves with a summary of each part. Use the same `spec` object for each
|
|
143
|
-
* tree that `concat` or `splice` joins.
|
|
144
|
-
*/
|
|
145
|
-
export declare function summaryTree<Leaf, Summary>(spec: SummarySpec<Leaf, Summary>, leaves?: Iterable<Leaf>): SummaryTree<Leaf, Summary>;
|
|
146
|
-
//# sourceMappingURL=summary-tree.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"summary-tree.d.ts","sourceRoot":"","sources":["../../src/impl/summary-tree.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;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