@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.
Files changed (238) hide show
  1. package/CHANGELOG.md +32 -8
  2. package/README.md +3 -1
  3. package/dist/.tsbuildinfo +1 -1
  4. package/dist/index.d.ts +1 -59
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +3 -95
  7. package/dist/index.js.map +1 -1
  8. package/dist/storage/index.d.ts +2 -2
  9. package/dist/storage/index.d.ts.map +1 -1
  10. package/dist/storage/index.js +1 -1
  11. package/dist/storage/index.js.map +1 -1
  12. package/dist/storage/memory.d.ts +1 -1
  13. package/dist/storage/memory.d.ts.map +1 -1
  14. package/dist/storage/memory.js +1 -1
  15. package/dist/storage/memory.js.map +1 -1
  16. package/dist/testing/index.d.ts +1 -1
  17. package/dist/testing/index.d.ts.map +1 -1
  18. package/dist/testing/index.js +1 -1
  19. package/dist/testing/index.js.map +1 -1
  20. package/dist/testing/storage-contract.js +1 -1
  21. package/dist/testing/storage-contract.js.map +1 -1
  22. package/dist/undo/index.d.ts +1 -1
  23. package/dist/undo/index.js +1 -1
  24. package/dist/undo/undo-log.d.ts +1 -1
  25. package/dist/undo/undo-log.js +1 -1
  26. package/package.json +5 -5
  27. package/src/index.ts +338 -214
  28. package/src/storage/index.ts +2 -2
  29. package/src/storage/memory.ts +1 -1
  30. package/src/testing/index.ts +1 -1
  31. package/src/testing/storage-contract.ts +1 -1
  32. package/src/undo/index.ts +1 -1
  33. package/src/undo/undo-log.ts +1 -1
  34. package/dist/async/async-queue.d.ts +0 -170
  35. package/dist/async/async-queue.d.ts.map +0 -1
  36. package/dist/async/async-queue.js +0 -404
  37. package/dist/async/async-queue.js.map +0 -1
  38. package/dist/async/channels.d.ts +0 -211
  39. package/dist/async/channels.d.ts.map +0 -1
  40. package/dist/async/channels.js +0 -393
  41. package/dist/async/channels.js.map +0 -1
  42. package/dist/async/settled.d.ts +0 -23
  43. package/dist/async/settled.d.ts.map +0 -1
  44. package/dist/async/settled.js +0 -32
  45. package/dist/async/settled.js.map +0 -1
  46. package/dist/behaviors/bounded.d.ts +0 -79
  47. package/dist/behaviors/bounded.d.ts.map +0 -1
  48. package/dist/behaviors/bounded.js +0 -393
  49. package/dist/behaviors/bounded.js.map +0 -1
  50. package/dist/behaviors/composite.d.ts +0 -17
  51. package/dist/behaviors/composite.d.ts.map +0 -1
  52. package/dist/behaviors/composite.js +0 -93
  53. package/dist/behaviors/composite.js.map +0 -1
  54. package/dist/behaviors/evented.d.ts +0 -154
  55. package/dist/behaviors/evented.d.ts.map +0 -1
  56. package/dist/behaviors/evented.js +0 -299
  57. package/dist/behaviors/evented.js.map +0 -1
  58. package/dist/behaviors/lru.d.ts +0 -84
  59. package/dist/behaviors/lru.d.ts.map +0 -1
  60. package/dist/behaviors/lru.js +0 -376
  61. package/dist/behaviors/lru.js.map +0 -1
  62. package/dist/behaviors/readonly.d.ts +0 -73
  63. package/dist/behaviors/readonly.d.ts.map +0 -1
  64. package/dist/behaviors/readonly.js +0 -189
  65. package/dist/behaviors/readonly.js.map +0 -1
  66. package/dist/behaviors/safe.d.ts +0 -163
  67. package/dist/behaviors/safe.d.ts.map +0 -1
  68. package/dist/behaviors/safe.js +0 -176
  69. package/dist/behaviors/safe.js.map +0 -1
  70. package/dist/behaviors/synchronized.d.ts +0 -88
  71. package/dist/behaviors/synchronized.d.ts.map +0 -1
  72. package/dist/behaviors/synchronized.js +0 -285
  73. package/dist/behaviors/synchronized.js.map +0 -1
  74. package/dist/behaviors/ttl.d.ts +0 -118
  75. package/dist/behaviors/ttl.d.ts.map +0 -1
  76. package/dist/behaviors/ttl.js +0 -354
  77. package/dist/behaviors/ttl.js.map +0 -1
  78. package/dist/contracts/model.d.ts +0 -29
  79. package/dist/contracts/model.d.ts.map +0 -1
  80. package/dist/contracts/model.js +0 -26
  81. package/dist/contracts/model.js.map +0 -1
  82. package/dist/core/effects.d.ts +0 -176
  83. package/dist/core/effects.d.ts.map +0 -1
  84. package/dist/core/effects.js +0 -180
  85. package/dist/core/effects.js.map +0 -1
  86. package/dist/core/events.d.ts +0 -192
  87. package/dist/core/events.d.ts.map +0 -1
  88. package/dist/core/events.js +0 -291
  89. package/dist/core/events.js.map +0 -1
  90. package/dist/core/middleware.d.ts +0 -231
  91. package/dist/core/middleware.d.ts.map +0 -1
  92. package/dist/core/middleware.js +0 -161
  93. package/dist/core/middleware.js.map +0 -1
  94. package/dist/core/policies.d.ts +0 -220
  95. package/dist/core/policies.d.ts.map +0 -1
  96. package/dist/core/policies.js +0 -48
  97. package/dist/core/policies.js.map +0 -1
  98. package/dist/core/traits.d.ts +0 -142
  99. package/dist/core/traits.d.ts.map +0 -1
  100. package/dist/core/traits.js +0 -8
  101. package/dist/core/traits.js.map +0 -1
  102. package/dist/fx/collectors.d.ts +0 -193
  103. package/dist/fx/collectors.d.ts.map +0 -1
  104. package/dist/fx/collectors.js +0 -375
  105. package/dist/fx/collectors.js.map +0 -1
  106. package/dist/fx/iter.d.ts +0 -166
  107. package/dist/fx/iter.d.ts.map +0 -1
  108. package/dist/fx/iter.js +0 -421
  109. package/dist/fx/iter.js.map +0 -1
  110. package/dist/impl/array-deque.d.ts +0 -112
  111. package/dist/impl/array-deque.d.ts.map +0 -1
  112. package/dist/impl/array-deque.js +0 -380
  113. package/dist/impl/array-deque.js.map +0 -1
  114. package/dist/impl/array-list.d.ts +0 -101
  115. package/dist/impl/array-list.d.ts.map +0 -1
  116. package/dist/impl/array-list.js +0 -387
  117. package/dist/impl/array-list.js.map +0 -1
  118. package/dist/impl/hash-map.d.ts +0 -149
  119. package/dist/impl/hash-map.d.ts.map +0 -1
  120. package/dist/impl/hash-map.js +0 -473
  121. package/dist/impl/hash-map.js.map +0 -1
  122. package/dist/impl/hash-set.d.ts +0 -125
  123. package/dist/impl/hash-set.d.ts.map +0 -1
  124. package/dist/impl/hash-set.js +0 -264
  125. package/dist/impl/hash-set.js.map +0 -1
  126. package/dist/impl/linked-hash-map.d.ts +0 -141
  127. package/dist/impl/linked-hash-map.d.ts.map +0 -1
  128. package/dist/impl/linked-hash-map.js +0 -442
  129. package/dist/impl/linked-hash-map.js.map +0 -1
  130. package/dist/impl/linked-list.d.ts +0 -109
  131. package/dist/impl/linked-list.d.ts.map +0 -1
  132. package/dist/impl/linked-list.js +0 -494
  133. package/dist/impl/linked-list.js.map +0 -1
  134. package/dist/impl/priority-queue.d.ts +0 -139
  135. package/dist/impl/priority-queue.d.ts.map +0 -1
  136. package/dist/impl/priority-queue.js +0 -343
  137. package/dist/impl/priority-queue.js.map +0 -1
  138. package/dist/impl/summary-tree.d.ts +0 -146
  139. package/dist/impl/summary-tree.d.ts.map +0 -1
  140. package/dist/impl/summary-tree.js +0 -524
  141. package/dist/impl/summary-tree.js.map +0 -1
  142. package/dist/impl/tree-map.d.ts +0 -153
  143. package/dist/impl/tree-map.d.ts.map +0 -1
  144. package/dist/impl/tree-map.js +0 -788
  145. package/dist/impl/tree-map.js.map +0 -1
  146. package/dist/impl/tree-set.d.ts +0 -112
  147. package/dist/impl/tree-set.d.ts.map +0 -1
  148. package/dist/impl/tree-set.js +0 -374
  149. package/dist/impl/tree-set.js.map +0 -1
  150. package/dist/interfaces/collection.d.ts +0 -119
  151. package/dist/interfaces/collection.d.ts.map +0 -1
  152. package/dist/interfaces/collection.js +0 -8
  153. package/dist/interfaces/collection.js.map +0 -1
  154. package/dist/interfaces/list.d.ts +0 -247
  155. package/dist/interfaces/list.d.ts.map +0 -1
  156. package/dist/interfaces/list.js +0 -8
  157. package/dist/interfaces/list.js.map +0 -1
  158. package/dist/interfaces/map.d.ts +0 -344
  159. package/dist/interfaces/map.d.ts.map +0 -1
  160. package/dist/interfaces/map.js +0 -8
  161. package/dist/interfaces/map.js.map +0 -1
  162. package/dist/interfaces/queue.d.ts +0 -304
  163. package/dist/interfaces/queue.d.ts.map +0 -1
  164. package/dist/interfaces/queue.js +0 -8
  165. package/dist/interfaces/queue.js.map +0 -1
  166. package/dist/interfaces/set.d.ts +0 -137
  167. package/dist/interfaces/set.d.ts.map +0 -1
  168. package/dist/interfaces/set.js +0 -8
  169. package/dist/interfaces/set.js.map +0 -1
  170. package/dist/order/index.d.ts +0 -7
  171. package/dist/order/index.d.ts.map +0 -1
  172. package/dist/order/index.js +0 -7
  173. package/dist/order/index.js.map +0 -1
  174. package/dist/order/rank.d.ts +0 -100
  175. package/dist/order/rank.d.ts.map +0 -1
  176. package/dist/order/rank.js +0 -286
  177. package/dist/order/rank.js.map +0 -1
  178. package/dist/storage/listeners.d.ts +0 -39
  179. package/dist/storage/listeners.d.ts.map +0 -1
  180. package/dist/storage/listeners.js +0 -65
  181. package/dist/storage/listeners.js.map +0 -1
  182. package/dist/testing/check.d.ts +0 -25
  183. package/dist/testing/check.d.ts.map +0 -1
  184. package/dist/testing/check.js +0 -85
  185. package/dist/testing/check.js.map +0 -1
  186. package/dist/utils/defaults.d.ts +0 -135
  187. package/dist/utils/defaults.d.ts.map +0 -1
  188. package/dist/utils/defaults.js +0 -322
  189. package/dist/utils/defaults.js.map +0 -1
  190. package/dist/utils/factories.d.ts +0 -144
  191. package/dist/utils/factories.d.ts.map +0 -1
  192. package/dist/utils/factories.js +0 -293
  193. package/dist/utils/factories.js.map +0 -1
  194. package/dist/utils/helpers.d.ts +0 -137
  195. package/dist/utils/helpers.d.ts.map +0 -1
  196. package/dist/utils/helpers.js +0 -221
  197. package/dist/utils/helpers.js.map +0 -1
  198. package/src/async/async-queue.ts +0 -481
  199. package/src/async/channels.ts +0 -450
  200. package/src/async/settled.ts +0 -41
  201. package/src/behaviors/bounded.ts +0 -485
  202. package/src/behaviors/composite.ts +0 -99
  203. package/src/behaviors/evented.ts +0 -464
  204. package/src/behaviors/lru.ts +0 -447
  205. package/src/behaviors/readonly.ts +0 -236
  206. package/src/behaviors/safe.ts +0 -372
  207. package/src/behaviors/synchronized.ts +0 -316
  208. package/src/behaviors/ttl.ts +0 -462
  209. package/src/contracts/model.ts +0 -44
  210. package/src/core/effects.ts +0 -323
  211. package/src/core/events.ts +0 -356
  212. package/src/core/middleware.ts +0 -415
  213. package/src/core/policies.ts +0 -316
  214. package/src/core/traits.ts +0 -164
  215. package/src/fx/collectors.ts +0 -500
  216. package/src/fx/iter.ts +0 -536
  217. package/src/impl/array-deque.ts +0 -463
  218. package/src/impl/array-list.ts +0 -490
  219. package/src/impl/hash-map.ts +0 -623
  220. package/src/impl/hash-set.ts +0 -342
  221. package/src/impl/linked-hash-map.ts +0 -572
  222. package/src/impl/linked-list.ts +0 -606
  223. package/src/impl/priority-queue.ts +0 -448
  224. package/src/impl/summary-tree.ts +0 -586
  225. package/src/impl/tree-map.ts +0 -912
  226. package/src/impl/tree-set.ts +0 -469
  227. package/src/interfaces/collection.ts +0 -141
  228. package/src/interfaces/list.ts +0 -281
  229. package/src/interfaces/map.ts +0 -405
  230. package/src/interfaces/queue.ts +0 -353
  231. package/src/interfaces/set.ts +0 -158
  232. package/src/order/index.ts +0 -6
  233. package/src/order/rank.ts +0 -271
  234. package/src/storage/listeners.ts +0 -72
  235. package/src/testing/check.ts +0 -81
  236. package/src/utils/defaults.ts +0 -345
  237. package/src/utils/factories.ts +0 -357
  238. package/src/utils/helpers.ts +0 -248
@@ -1,448 +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
-
17
- import type { PriorityQueue as IPriorityQueue } from "../interfaces/queue.js";
18
- import type { Eq, Compare } from "../core/traits.js";
19
- import { defaultEq, defaultCompare } from "../utils/defaults.js";
20
-
21
- /**
22
- * Options for creating a PriorityQueue.
23
- */
24
- export interface PriorityQueueOptions<T> {
25
- /**
26
- * Initial capacity of the heap.
27
- * @default 11
28
- */
29
- initialCapacity?: number;
30
-
31
- /**
32
- * Comparator for ordering elements.
33
- * Lower values have higher priority (min heap).
34
- * @default defaultCompare
35
- */
36
- compare?: Compare<T>;
37
-
38
- /**
39
- * Equality function for elements.
40
- * @default defaultEq
41
- */
42
- eq?: Eq<T>;
43
- }
44
-
45
- /**
46
- * PriorityQueue<T> - Binary heap implementation of priority queue.
47
- *
48
- * Elements are always retrieved in priority order (according to comparator).
49
- * Not a FIFO queue - order depends on priority, not insertion order.
50
- *
51
- * The heap is a min heap by default (smallest element has highest priority).
52
- * For max heap, use reversed comparator: (a, b) => compare(b, a)
53
- *
54
- * Not thread-safe. Use synchronized() behavior for concurrent access.
55
- */
56
- export class PriorityQueue<T> implements IPriorityQueue<T> {
57
- private _heap: T[];
58
- private readonly _compare: Compare<T>;
59
- private readonly _eq: Eq<T>;
60
-
61
- constructor(options: PriorityQueueOptions<T> = {}) {
62
- const {
63
- compare = defaultCompare,
64
- eq = defaultEq,
65
- } = options;
66
-
67
- // The heap starts empty: a JavaScript array grows by itself, so initialCapacity is only a
68
- // hint. (Before, `new Array(initialCapacity)` made 11 holes: an empty queue had size 11,
69
- // and the heap compared holes. BUGS-2026-07 C9.)
70
- this._heap = [];
71
- this._compare = compare;
72
- this._eq = eq;
73
- }
74
-
75
- // ========================================================================
76
- // Size
77
- // ========================================================================
78
-
79
- get size(): number {
80
- return this._heap.length;
81
- }
82
-
83
- get isEmpty(): boolean {
84
- return this._heap.length === 0;
85
- }
86
-
87
- get eq(): Eq<T> {
88
- return this._eq;
89
- }
90
-
91
- get compare(): Compare<T> {
92
- return this._compare;
93
- }
94
-
95
- get comparator(): Compare<T> {
96
- return this._compare;
97
- }
98
-
99
- // ========================================================================
100
- // Priority queue operations
101
- // ========================================================================
102
-
103
- offer(element: T): boolean {
104
- this._heap.push(element);
105
- this.bubbleUp(this._heap.length - 1);
106
- return true;
107
- }
108
-
109
- poll(): T {
110
- if (this.isEmpty) {
111
- throw new Error("Priority queue is empty");
112
- }
113
- return this.pollOrUndefined()!;
114
- }
115
-
116
- pollOrUndefined(): T | undefined {
117
- if (this.isEmpty) {
118
- return undefined;
119
- }
120
-
121
- const result = this._heap[0];
122
-
123
- if (this._heap.length === 1) {
124
- this._heap.pop();
125
- } else {
126
- this._heap[0] = this._heap.pop()!;
127
- this.bubbleDown(0);
128
- }
129
-
130
- return result;
131
- }
132
-
133
- peek(): T {
134
- if (this.isEmpty) {
135
- throw new Error("Priority queue is empty");
136
- }
137
- return this._heap[0]!;
138
- }
139
-
140
- peekOrUndefined(): T | undefined {
141
- return this.isEmpty ? undefined : this._heap[0];
142
- }
143
-
144
- // ========================================================================
145
- // Queue interface compatibility
146
- // ========================================================================
147
-
148
- enqueue(element: T): boolean {
149
- return this.offer(element);
150
- }
151
-
152
- dequeue(): T {
153
- return this.poll();
154
- }
155
-
156
- // ========================================================================
157
- // Collection operations
158
- // ========================================================================
159
-
160
- add(element: T): boolean {
161
- return this.offer(element);
162
- }
163
-
164
- addAll(other: Iterable<T>): boolean {
165
- let modified = false;
166
- for (const element of other) {
167
- this.offer(element);
168
- modified = true;
169
- }
170
- return modified;
171
- }
172
-
173
- contains(element: T): boolean {
174
- for (const item of this._heap) {
175
- if (this._eq(item, element)) {
176
- return true;
177
- }
178
- }
179
- return false;
180
- }
181
-
182
- containsAll(other: Iterable<T>): boolean {
183
- for (const element of other) {
184
- if (!this.contains(element)) {
185
- return false;
186
- }
187
- }
188
- return true;
189
- }
190
-
191
- remove(element: T): boolean {
192
- const index = this._heap.findIndex((item) => this._eq(item, element));
193
- if (index === -1) {
194
- return false;
195
- }
196
-
197
- if (index === this._heap.length - 1) {
198
- this._heap.pop();
199
- } else {
200
- this._heap[index] = this._heap.pop()!;
201
- this.bubbleDown(index);
202
- this.bubbleUp(index);
203
- }
204
-
205
- return true;
206
- }
207
-
208
- removeAll(other: Iterable<T>): boolean {
209
- let modified = false;
210
- for (const element of other) {
211
- while (this.remove(element)) {
212
- modified = true;
213
- }
214
- }
215
- return modified;
216
- }
217
-
218
- retainAll(other: Iterable<T>): boolean {
219
- const toRetain = new Set(other);
220
- const newHeap: T[] = [];
221
-
222
- for (const element of this._heap) {
223
- let shouldRetain = false;
224
- for (const retainElement of toRetain) {
225
- if (this._eq(element, retainElement)) {
226
- shouldRetain = true;
227
- break;
228
- }
229
- }
230
- if (shouldRetain) {
231
- newHeap.push(element);
232
- }
233
- }
234
-
235
- const modified = newHeap.length !== this._heap.length;
236
- this._heap = newHeap;
237
- this.heapify();
238
-
239
- return modified;
240
- }
241
-
242
- clear(): void {
243
- this._heap = [];
244
- }
245
-
246
- // ========================================================================
247
- // Iteration
248
- // ========================================================================
249
-
250
- /**
251
- * Iterator returns elements in arbitrary order (not priority order).
252
- * To get elements in priority order, repeatedly call poll().
253
- */
254
- *[Symbol.iterator](): Iterator<T> {
255
- yield* this._heap;
256
- }
257
-
258
- forEach(action: (element: T, index: number) => void): void {
259
- this._heap.forEach((element, index) => action(element, index));
260
- }
261
-
262
- /**
263
- * Returns elements in arbitrary heap order (not sorted).
264
- */
265
- toArray(): T[] {
266
- return this._heap.slice();
267
- }
268
-
269
- /**
270
- * Returns elements in priority order (sorted).
271
- * This is a destructive operation that drains the queue.
272
- */
273
- toSortedArray(): T[] {
274
- const result: T[] = [];
275
- const clone = this.clone();
276
-
277
- while (!clone.isEmpty) {
278
- result.push(clone.poll());
279
- }
280
-
281
- return result;
282
- }
283
-
284
- // ========================================================================
285
- // Heap operations
286
- // ========================================================================
287
-
288
- /**
289
- * Bubbles an element up the heap to maintain heap property.
290
- */
291
- private bubbleUp(index: number): void {
292
- const element = this._heap[index]!;
293
-
294
- while (index > 0) {
295
- const parentIndex = Math.floor((index - 1) / 2);
296
- const parent = this._heap[parentIndex]!;
297
-
298
- if (this._compare(element, parent) >= 0) {
299
- break;
300
- }
301
-
302
- this._heap[index] = parent;
303
- index = parentIndex;
304
- }
305
-
306
- this._heap[index] = element;
307
- }
308
-
309
- /**
310
- * Bubbles an element down the heap to maintain heap property.
311
- *
312
- * The element moves down through a hole: a smaller child moves up into the hole, and the
313
- * element goes into the last hole. The children are compared with the element. (Before, they
314
- * were compared with `_heap[index]`, which after the first step is a copy of the child that
315
- * moved up: a poll could leave a larger element above a smaller one.)
316
- */
317
- private bubbleDown(index: number): void {
318
- const length = this._heap.length;
319
- const element = this._heap[index]!;
320
-
321
- while (true) {
322
- const leftChildIndex = 2 * index + 1;
323
- const rightChildIndex = 2 * index + 2;
324
- if (leftChildIndex >= length) {
325
- break;
326
- }
327
-
328
- let childIndex = leftChildIndex;
329
- if (
330
- rightChildIndex < length &&
331
- this._compare(this._heap[rightChildIndex]!, this._heap[leftChildIndex]!) < 0
332
- ) {
333
- childIndex = rightChildIndex;
334
- }
335
-
336
- if (this._compare(this._heap[childIndex]!, element) >= 0) {
337
- break;
338
- }
339
-
340
- this._heap[index] = this._heap[childIndex]!;
341
- index = childIndex;
342
- }
343
-
344
- this._heap[index] = element;
345
- }
346
-
347
- /**
348
- * Rebuilds the heap from an arbitrary array.
349
- * Uses Floyd's algorithm (O(n) time).
350
- */
351
- private heapify(): void {
352
- // Start from last non-leaf node and bubble down
353
- for (let i = Math.floor(this._heap.length / 2) - 1; i >= 0; i--) {
354
- this.bubbleDown(i);
355
- }
356
- }
357
-
358
- // ========================================================================
359
- // Utility methods
360
- // ========================================================================
361
-
362
- /**
363
- * Creates a shallow copy of this priority queue.
364
- */
365
- clone(): PriorityQueue<T> {
366
- const clone = new PriorityQueue<T>({
367
- compare: this._compare,
368
- eq: this._eq,
369
- });
370
- clone._heap = this._heap.slice();
371
- return clone;
372
- }
373
-
374
- /**
375
- * Validates the heap property (for debugging).
376
- * Returns true if the heap is valid.
377
- */
378
- validate(): boolean {
379
- for (let i = 0; i < this._heap.length; i++) {
380
- const leftChildIndex = 2 * i + 1;
381
- const rightChildIndex = 2 * i + 2;
382
-
383
- if (
384
- leftChildIndex < this._heap.length &&
385
- this._compare(this._heap[i]!, this._heap[leftChildIndex]!) > 0
386
- ) {
387
- return false;
388
- }
389
-
390
- if (
391
- rightChildIndex < this._heap.length &&
392
- this._compare(this._heap[i]!, this._heap[rightChildIndex]!) > 0
393
- ) {
394
- return false;
395
- }
396
- }
397
-
398
- return true;
399
- }
400
-
401
- /**
402
- * Returns a string representation for debugging.
403
- */
404
- toString(): string {
405
- return `PriorityQueue[${this._heap.join(", ")}]`;
406
- }
407
- }
408
-
409
- /**
410
- * Factory function to create a PriorityQueue.
411
- *
412
- * @example
413
- * // Min heap (default)
414
- * const minHeap = priorityQueue<number>()
415
- *
416
- * // Max heap
417
- * const maxHeap = priorityQueue<number>((a, b) => b - a)
418
- *
419
- * // From array
420
- * const pq = priorityQueue([5, 2, 8, 1, 9])
421
- *
422
- * // With custom comparator
423
- * const pq = priorityQueue<Task>((a, b) => a.priority - b.priority)
424
- */
425
- export function priorityQueue<T>(
426
- optionsOrElementsOrComparator?:
427
- | PriorityQueueOptions<T>
428
- | Iterable<T>
429
- | Compare<T>
430
- ): PriorityQueue<T> {
431
- // If it's a function, treat it as a comparator
432
- if (typeof optionsOrElementsOrComparator === "function") {
433
- return new PriorityQueue<T>({
434
- compare: optionsOrElementsOrComparator as Compare<T>,
435
- });
436
- }
437
-
438
- // Check if it's an iterable (not options)
439
- if (optionsOrElementsOrComparator && Symbol.iterator in optionsOrElementsOrComparator) {
440
- const pq = new PriorityQueue<T>();
441
- pq.addAll(optionsOrElementsOrComparator as Iterable<T>);
442
- return pq;
443
- }
444
-
445
- return new PriorityQueue<T>(
446
- optionsOrElementsOrComparator as PriorityQueueOptions<T>
447
- );
448
- }