max-priority-queue-typed 2.4.4 → 2.5.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 (85) hide show
  1. package/README.md +63 -0
  2. package/dist/cjs/index.cjs +403 -98
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +402 -97
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +403 -99
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +402 -98
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/common/error.d.ts +23 -0
  11. package/dist/types/common/index.d.ts +1 -0
  12. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  13. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  14. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  15. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
  16. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  17. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
  18. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  19. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  20. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  21. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  22. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  23. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  24. package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
  25. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  26. package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
  27. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  28. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  29. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  30. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  31. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  32. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  33. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  34. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  35. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  36. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  37. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  38. package/dist/types/data-structures/queue/deque.d.ts +313 -66
  39. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  40. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  41. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  42. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  43. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  44. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  45. package/dist/umd/max-priority-queue-typed.js +400 -95
  46. package/dist/umd/max-priority-queue-typed.js.map +1 -1
  47. package/dist/umd/max-priority-queue-typed.min.js +1 -1
  48. package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
  49. package/package.json +2 -2
  50. package/src/common/error.ts +60 -0
  51. package/src/common/index.ts +2 -0
  52. package/src/data-structures/base/iterable-element-base.ts +2 -2
  53. package/src/data-structures/binary-tree/avl-tree.ts +134 -51
  54. package/src/data-structures/binary-tree/binary-indexed-tree.ts +303 -247
  55. package/src/data-structures/binary-tree/binary-tree.ts +542 -121
  56. package/src/data-structures/binary-tree/bst.ts +346 -37
  57. package/src/data-structures/binary-tree/red-black-tree.ts +309 -96
  58. package/src/data-structures/binary-tree/segment-tree.ts +372 -248
  59. package/src/data-structures/binary-tree/tree-map.ts +1292 -13
  60. package/src/data-structures/binary-tree/tree-multi-map.ts +1098 -215
  61. package/src/data-structures/binary-tree/tree-multi-set.ts +863 -69
  62. package/src/data-structures/binary-tree/tree-set.ts +1143 -15
  63. package/src/data-structures/graph/abstract-graph.ts +106 -1
  64. package/src/data-structures/graph/directed-graph.ts +223 -47
  65. package/src/data-structures/graph/map-graph.ts +59 -1
  66. package/src/data-structures/graph/undirected-graph.ts +299 -59
  67. package/src/data-structures/hash/hash-map.ts +243 -79
  68. package/src/data-structures/heap/heap.ts +291 -102
  69. package/src/data-structures/heap/max-heap.ts +48 -3
  70. package/src/data-structures/heap/min-heap.ts +59 -0
  71. package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
  72. package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
  73. package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
  74. package/src/data-structures/matrix/matrix.ts +425 -22
  75. package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
  76. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  77. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  78. package/src/data-structures/queue/deque.ts +343 -68
  79. package/src/data-structures/queue/queue.ts +211 -42
  80. package/src/data-structures/stack/stack.ts +174 -32
  81. package/src/data-structures/trie/trie.ts +215 -44
  82. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  83. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  84. package/src/types/data-structures/queue/deque.ts +7 -0
  85. package/src/utils/utils.ts +4 -2
@@ -1,27 +1,430 @@
1
- import type { SkipLinkedListOptions } from '../../types';
1
+ /**
2
+ * data-structure-typed
3
+ *
4
+ * @author Pablo Zeng
5
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
7
+ */
8
+ import type { Comparator, EntryCallback } from '../../types';
9
+ import { IterableEntryBase } from '../base';
2
10
  export declare class SkipListNode<K, V> {
3
11
  key: K;
4
12
  value: V;
5
- forward: SkipListNode<K, V>[];
13
+ forward: (SkipListNode<K, V> | undefined)[];
6
14
  constructor(key: K, value: V, level: number);
7
15
  }
8
- export declare class SkipList<K, V> {
9
- constructor(elements?: Iterable<[K, V]>, options?: SkipLinkedListOptions);
16
+ export type SkipListOptions<K, V, R = [K, V]> = {
17
+ comparator?: Comparator<K>;
18
+ toEntryFn?: (rawElement: R) => [K, V];
19
+ maxLevel?: number;
20
+ probability?: number;
21
+ };
22
+ export type SkipListRangeOptions = {
23
+ lowInclusive?: boolean;
24
+ highInclusive?: boolean;
25
+ };
26
+ /**
27
+ * SkipList — a probabilistic sorted key-value container.
28
+ *
29
+ * API mirrors TreeMap: users can swap `TreeMap` ↔ `SkipList` with zero code changes.
30
+ * Reference: Java ConcurrentSkipListMap (NavigableMap interface).
31
+ *
32
+ * @example
33
+ * // Display skip list
34
+ * const sl = new SkipList<number, string>([[1, 'a']]);
35
+ * expect(() => sl.print()).not.toThrow();
36
+ */
37
+ export declare class SkipList<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V | undefined> {
38
+ #private;
39
+ constructor(entries?: Iterable<R> | Iterable<[K, V | undefined]>, options?: SkipListOptions<K, V, R>);
40
+ /**
41
+ * Creates a default comparator supporting number, string, Date, and bigint.
42
+ */
43
+ static createDefaultComparator<K>(): Comparator<K>;
10
44
  protected _head: SkipListNode<K, V>;
11
- get head(): SkipListNode<K, V>;
12
45
  protected _level: number;
13
- get level(): number;
46
+ protected _size: number;
14
47
  protected _maxLevel: number;
15
- get maxLevel(): number;
16
48
  protected _probability: number;
49
+ get size(): number;
50
+ get maxLevel(): number;
17
51
  get probability(): number;
18
- get first(): V | undefined;
19
- get last(): V | undefined;
20
- add(key: K, value: V): void;
52
+ get comparator(): Comparator<K>;
53
+ /**
54
+ * Check if empty
55
+
56
+
57
+
58
+
59
+
60
+
61
+
62
+
63
+ * @example
64
+ * // Check if empty
65
+ * const sl = new SkipList<number, string>();
66
+ * console.log(sl.isEmpty()); // true;
67
+ */
68
+ isEmpty(): boolean;
69
+ /**
70
+ * Remove all entries
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+ * @example
80
+ * // Remove all entries
81
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b']]);
82
+ * sl.clear();
83
+ * console.log(sl.isEmpty()); // true;
84
+ */
85
+ clear(): void;
86
+ /**
87
+ * Create independent copy
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+ * @example
97
+ * // Create independent copy
98
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b']]);
99
+ * const copy = sl.clone();
100
+ * copy.delete(1);
101
+ * console.log(sl.has(1)); // true;
102
+ */
103
+ clone(): this;
104
+ /**
105
+ * Insert or update a key-value pair. Returns `this` for chaining.
106
+ * Unique keys only — if key exists, value is updated in place.
107
+
108
+
109
+
110
+
111
+
112
+
113
+
114
+
115
+
116
+
117
+
118
+ * @example
119
+ * // In-memory sorted key-value store
120
+ * const store = new SkipList<number, string>();
121
+ *
122
+ * store.set(3, 'three');
123
+ * store.set(1, 'one');
124
+ * store.set(5, 'five');
125
+ * store.set(2, 'two');
126
+ *
127
+ * console.log(store.get(3)); // 'three';
128
+ * console.log(store.get(1)); // 'one';
129
+ * console.log(store.get(5)); // 'five';
130
+ *
131
+ * // Update existing key
132
+ * store.set(3, 'THREE');
133
+ * console.log(store.get(3)); // 'THREE';
134
+ */
135
+ set(key: K, value: V): this;
136
+ /**
137
+ * Get the value for a key, or `undefined` if not found.
138
+ * Overrides base O(n) with O(log n) skip-list search.
139
+
140
+
141
+
142
+
143
+
144
+
145
+
146
+
147
+
148
+
149
+
150
+ * @example
151
+ * // Building a sorted index
152
+ * type Product = { id: number; name: string; price: number };
153
+ * const products: Product[] = [
154
+ * { id: 1, name: 'Widget', price: 25 },
155
+ * { id: 2, name: 'Gadget', price: 50 },
156
+ * { id: 3, name: 'Doohickey', price: 15 }
157
+ * ];
158
+ *
159
+ * const index = new SkipList<number, Product>(products as any, {
160
+ * toEntryFn: (p: any) => [p.price, p]
161
+ * });
162
+ *
163
+ * // Iterate in sorted order by price
164
+ * const names = [...index.values()].map(p => p!.name);
165
+ * console.log(names); // ['Doohickey', 'Widget', 'Gadget'];
166
+ *
167
+ * // Range search: products between $20 and $60
168
+ * const range = index.rangeSearch([20, 60]);
169
+ * console.log(range.map(([, p]) => p!.name)); // ['Widget', 'Gadget'];
170
+ */
21
171
  get(key: K): V | undefined;
172
+ /**
173
+ * Check if a key exists.
174
+ * Overrides base O(n) with O(log n) skip-list search.
175
+
176
+
177
+
178
+
179
+
180
+
181
+
182
+
183
+
184
+
185
+
186
+ * @example
187
+ * // Check key existence
188
+ * const sl = new SkipList<number, string>([[1, 'a'], [3, 'c'], [5, 'e']]);
189
+ * console.log(sl.has(3)); // true;
190
+ * console.log(sl.has(4)); // false;
191
+ */
22
192
  has(key: K): boolean;
193
+ /**
194
+ * Delete a key. Returns `true` if the key was found and removed.
195
+
196
+
197
+
198
+
199
+
200
+
201
+
202
+
203
+
204
+
205
+
206
+ * @example
207
+ * // Fast lookup with deletion
208
+ * const cache = new SkipList<string, number>();
209
+ *
210
+ * cache.set('alpha', 1);
211
+ * cache.set('beta', 2);
212
+ * cache.set('gamma', 3);
213
+ *
214
+ * console.log(cache.has('beta')); // true;
215
+ * cache.delete('beta');
216
+ * console.log(cache.has('beta')); // false;
217
+ * console.log(cache.size); // 2;
218
+ */
23
219
  delete(key: K): boolean;
24
- higher(key: K): V | undefined;
25
- lower(key: K): V | undefined;
220
+ /**
221
+ * Returns the first (smallest key) entry, or `undefined` if empty.
222
+
223
+
224
+
225
+
226
+
227
+
228
+
229
+
230
+
231
+
232
+
233
+ * @example
234
+ * // Access the minimum entry
235
+ * const sl = new SkipList<number, string>([[5, 'e'], [1, 'a'], [3, 'c']]);
236
+ * console.log(sl.first()); // [1, 'a'];
237
+ */
238
+ first(): [K, V | undefined] | undefined;
239
+ /**
240
+ * Returns the last (largest key) entry, or `undefined` if empty.
241
+
242
+
243
+
244
+
245
+
246
+
247
+
248
+
249
+
250
+
251
+
252
+ * @example
253
+ * // Access the maximum entry
254
+ * const sl = new SkipList<number, string>([[5, 'e'], [1, 'a'], [3, 'c']]);
255
+ * console.log(sl.last()); // [5, 'e'];
256
+ */
257
+ last(): [K, V | undefined] | undefined;
258
+ /**
259
+ * Remove and return the first (smallest key) entry.
260
+
261
+
262
+
263
+
264
+
265
+
266
+
267
+
268
+ * @example
269
+ * // Remove and return smallest
270
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c']]);
271
+ * console.log(sl.pollFirst()); // [1, 'a'];
272
+ * console.log(sl.size); // 2;
273
+ */
274
+ pollFirst(): [K, V | undefined] | undefined;
275
+ /**
276
+ * Remove and return the last (largest key) entry.
277
+
278
+
279
+
280
+
281
+
282
+
283
+
284
+
285
+ * @example
286
+ * // Remove and return largest
287
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c']]);
288
+ * console.log(sl.pollLast()); // [3, 'c'];
289
+ * console.log(sl.size); // 2;
290
+ */
291
+ pollLast(): [K, V | undefined] | undefined;
292
+ /**
293
+ * Least entry ≥ key, or `undefined`.
294
+
295
+
296
+
297
+
298
+
299
+
300
+
301
+
302
+
303
+
304
+
305
+ * @example
306
+ * // Least entry ≥ key
307
+ * const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
308
+ * console.log(sl.ceiling(15)); // [20, 'b'];
309
+ * console.log(sl.ceiling(20)); // [20, 'b'];
310
+ */
311
+ ceiling(key: K): [K, V | undefined] | undefined;
312
+ /**
313
+ * Greatest entry ≤ key, or `undefined`.
314
+
315
+
316
+
317
+
318
+
319
+
320
+
321
+
322
+
323
+
324
+
325
+ * @example
326
+ * // Greatest entry ≤ key
327
+ * const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
328
+ * console.log(sl.floor(25)); // [20, 'b'];
329
+ * console.log(sl.floor(5)); // undefined;
330
+ */
331
+ floor(key: K): [K, V | undefined] | undefined;
332
+ /**
333
+ * Least entry strictly > key, or `undefined`.
334
+
335
+
336
+
337
+
338
+
339
+
340
+
341
+
342
+ * @example
343
+ * // Strictly greater entry
344
+ * const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
345
+ * console.log(sl.higher(15)); // [20, 'b'];
346
+ * console.log(sl.higher(30)); // undefined;
347
+ */
348
+ higher(key: K): [K, V | undefined] | undefined;
349
+ /**
350
+ * Greatest entry strictly < key, or `undefined`.
351
+
352
+
353
+
354
+
355
+
356
+
357
+
358
+
359
+ * @example
360
+ * // Strictly less entry
361
+ * const sl = new SkipList<number, string>([[10, 'a'], [20, 'b'], [30, 'c']]);
362
+ * console.log(sl.lower(25)); // [20, 'b'];
363
+ * console.log(sl.lower(10)); // undefined;
364
+ */
365
+ lower(key: K): [K, V | undefined] | undefined;
366
+ /**
367
+ * Returns entries within the given key range.
368
+
369
+
370
+
371
+
372
+
373
+
374
+
375
+
376
+
377
+
378
+
379
+ * @example
380
+ * // Find entries in a range
381
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd'], [5, 'e']]);
382
+ * const result = sl.rangeSearch([2, 4]);
383
+ * console.log(result); // [[2, 'b'], [3, 'c'], [4, 'd']];
384
+ */
385
+ rangeSearch(range: [K, K], options?: SkipListRangeOptions): Array<[K, V | undefined]>;
386
+ /**
387
+ * Creates a new SkipList with entries transformed by callback.
388
+
389
+
390
+
391
+
392
+
393
+
394
+
395
+
396
+ * @example
397
+ * // Transform entries
398
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b']]);
399
+ * const mapped = sl.map((v, k) => [k, v?.toUpperCase()] as [number, string]);
400
+ * console.log([...mapped.values()]); // ['A', 'B'];
401
+ */
402
+ map<MK, MV>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: SkipListOptions<MK, MV>): SkipList<MK, MV>;
403
+ /**
404
+ * Creates a new SkipList with entries that pass the predicate.
405
+
406
+
407
+
408
+
409
+
410
+
411
+
412
+
413
+ * @example
414
+ * // Filter entries
415
+ * const sl = new SkipList<number, string>([[1, 'a'], [2, 'b'], [3, 'c']]);
416
+ * const result = sl.filter((v, k) => k > 1);
417
+ * console.log(result.size); // 2;
418
+ */
419
+ filter(callbackfn: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): this;
420
+ protected _getIterator(): IterableIterator<[K, V | undefined]>;
421
+ /**
422
+ * Finds the update array (predecessors at each level) for a given key.
423
+ */
424
+ protected _findUpdate(key: K): SkipListNode<K, V>[];
425
+ /**
426
+ * Finds the node for a given key, or undefined.
427
+ */
428
+ protected _findNode(key: K): SkipListNode<K, V> | undefined;
26
429
  protected _randomLevel(): number;
27
430
  }