queue-typed 1.49.1 → 1.49.3

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 (40) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +11 -0
  2. package/dist/data-structures/base/iterable-base.js +21 -0
  3. package/dist/data-structures/graph/abstract-graph.d.ts +7 -7
  4. package/dist/data-structures/graph/abstract-graph.js +43 -12
  5. package/dist/data-structures/graph/directed-graph.d.ts +2 -2
  6. package/dist/data-structures/graph/directed-graph.js +2 -2
  7. package/dist/data-structures/graph/undirected-graph.d.ts +1 -1
  8. package/dist/data-structures/graph/undirected-graph.js +1 -1
  9. package/dist/data-structures/hash/hash-map.d.ts +9 -9
  10. package/dist/data-structures/hash/hash-map.js +16 -15
  11. package/dist/data-structures/heap/heap.d.ts +6 -35
  12. package/dist/data-structures/heap/heap.js +10 -42
  13. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +99 -105
  14. package/dist/data-structures/linked-list/doubly-linked-list.js +143 -146
  15. package/dist/data-structures/linked-list/singly-linked-list.d.ts +16 -21
  16. package/dist/data-structures/linked-list/singly-linked-list.js +42 -42
  17. package/dist/data-structures/linked-list/skip-linked-list.d.ts +25 -25
  18. package/dist/data-structures/linked-list/skip-linked-list.js +36 -36
  19. package/dist/data-structures/queue/deque.d.ts +70 -75
  20. package/dist/data-structures/queue/deque.js +100 -110
  21. package/dist/data-structures/queue/queue.d.ts +37 -38
  22. package/dist/data-structures/queue/queue.js +46 -49
  23. package/dist/data-structures/stack/stack.d.ts +2 -3
  24. package/dist/data-structures/stack/stack.js +2 -5
  25. package/dist/data-structures/trie/trie.d.ts +1 -2
  26. package/dist/data-structures/trie/trie.js +2 -5
  27. package/package.json +2 -2
  28. package/src/data-structures/base/iterable-base.ts +24 -0
  29. package/src/data-structures/graph/abstract-graph.ts +55 -14
  30. package/src/data-structures/graph/directed-graph.ts +3 -2
  31. package/src/data-structures/graph/undirected-graph.ts +1 -1
  32. package/src/data-structures/hash/hash-map.ts +27 -28
  33. package/src/data-structures/heap/heap.ts +19 -57
  34. package/src/data-structures/linked-list/doubly-linked-list.ts +157 -161
  35. package/src/data-structures/linked-list/singly-linked-list.ts +49 -49
  36. package/src/data-structures/linked-list/skip-linked-list.ts +40 -40
  37. package/src/data-structures/queue/deque.ts +122 -135
  38. package/src/data-structures/queue/queue.ts +54 -58
  39. package/src/data-structures/stack/stack.ts +4 -8
  40. package/src/data-structures/trie/trie.ts +5 -9
@@ -35,7 +35,6 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
35
35
  const { hashFn } = options;
36
36
  if (hashFn) {
37
37
  this._hashFn = hashFn;
38
-
39
38
  }
40
39
  }
41
40
  if (elements) {
@@ -68,8 +67,7 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
68
67
  * @param {V} value - The value parameter represents the value that you want to associate with the
69
68
  * key in the data structure.
70
69
  */
71
- set(key: K, value: V) {
72
-
70
+ set(key: K, value: V): boolean {
73
71
  if (this._isObjKey(key)) {
74
72
  if (!this._objMap.has(key)) {
75
73
  this._size++;
@@ -83,6 +81,7 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
83
81
  }
84
82
  this._store[strKey] = { key, value };
85
83
  }
84
+ return true;
86
85
  }
87
86
 
88
87
  /**
@@ -90,8 +89,10 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
90
89
  * @param elements - The `elements` parameter is an iterable containing key-value pairs. Each
91
90
  * key-value pair is represented as an array with two elements: the key and the value.
92
91
  */
93
- setMany(elements: Iterable<[K, V]>) {
94
- for (const [key, value] of elements) this.set(key, value);
92
+ setMany(elements: Iterable<[K, V]>): boolean[] {
93
+ const results: boolean[] = [];
94
+ for (const [key, value] of elements) results.push(this.set(key, value));
95
+ return results;
95
96
  }
96
97
 
97
98
  /**
@@ -215,6 +216,10 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
215
216
  console.log([...this.entries()]);
216
217
  }
217
218
 
219
+ put(key: K, value: V): boolean {
220
+ return this.set(key, value);
221
+ }
222
+
218
223
  /**
219
224
  * The function returns an iterator that yields key-value pairs from both an object store and an
220
225
  * object map.
@@ -286,7 +291,6 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
286
291
  this.set(el[0], el[1]);
287
292
  }
288
293
  }
289
-
290
294
  }
291
295
 
292
296
  protected _size = 0;
@@ -356,7 +360,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
356
360
  * value associated with the key being set in the data structure.
357
361
  * @returns the size of the data structure after the key-value pair has been set.
358
362
  */
359
- set(key: K, value?: V) {
363
+ set(key: K, value?: V): boolean {
360
364
  let node;
361
365
  const isNewKey = !this.has(key); // Check if the key is new
362
366
 
@@ -398,7 +402,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
398
402
  this._size++;
399
403
  }
400
404
 
401
- return this._size;
405
+ return true;
402
406
  }
403
407
 
404
408
  has(key: K): boolean {
@@ -411,18 +415,13 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
411
415
  }
412
416
  }
413
417
 
414
- hasValue(value: V): boolean {
415
- for (const [, elementValue] of this) {
416
- if (elementValue === value) return true;
417
- }
418
- return false;
419
- }
420
-
421
- setMany(entries: Iterable<[K, V]>): void {
418
+ setMany(entries: Iterable<[K, V]>): boolean[] {
419
+ const results: boolean[] = [];
422
420
  for (const entry of entries) {
423
421
  const [key, value] = entry;
424
- this.set(key, value);
422
+ results.push(this.set(key, value));
425
423
  }
424
+ return results;
426
425
  }
427
426
 
428
427
  /**
@@ -461,13 +460,13 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
461
460
  * the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`,
462
461
  * where `K` is the key and `V` is the value.
463
462
  */
464
- getAt(index: number) {
463
+ getAt(index: number): V | undefined {
465
464
  rangeCheck(index, 0, this._size - 1);
466
465
  let node = this._head;
467
466
  while (index--) {
468
467
  node = node.next;
469
468
  }
470
- return <[K, V]>[node.key, node.value];
469
+ return node.value;
471
470
  }
472
471
 
473
472
  /**
@@ -480,7 +479,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
480
479
  * @returns a boolean value. It returns `true` if the deletion was successful, and `false` if the key
481
480
  * was not found.
482
481
  */
483
- delete(key: K) {
482
+ delete(key: K): boolean {
484
483
  let node;
485
484
 
486
485
  if (isWeakKey(key)) {
@@ -521,14 +520,13 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
521
520
  * deleted in the linked list.
522
521
  * @returns The size of the list after deleting the element at the specified index.
523
522
  */
524
- deleteAt(index: number) {
523
+ deleteAt(index: number): boolean {
525
524
  rangeCheck(index, 0, this._size - 1);
526
525
  let node = this._head;
527
526
  while (index--) {
528
527
  node = node.next;
529
528
  }
530
- this._deleteNode(node);
531
- return this._size;
529
+ return this._deleteNode(node);
532
530
  }
533
531
 
534
532
  /**
@@ -539,7 +537,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
539
537
  * @returns The method is returning a boolean value indicating whether the size of the object is 0 or
540
538
  * not.
541
539
  */
542
- isEmpty() {
540
+ isEmpty(): boolean {
543
541
  return this._size === 0;
544
542
  }
545
543
 
@@ -549,7 +547,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
549
547
  *
550
548
  * The `clear` function clears all the elements in a data structure and resets its properties.
551
549
  */
552
- clear() {
550
+ clear(): void {
553
551
  this._noObjMap = {};
554
552
  this._size = 0;
555
553
  this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
@@ -629,8 +627,8 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
629
627
  return mappedMap;
630
628
  }
631
629
 
632
- print() {
633
- console.log([...this]);
630
+ put(key: K, value: V): boolean {
631
+ return this.set(key, value);
634
632
  }
635
633
 
636
634
  /**
@@ -657,7 +655,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
657
655
  * represents a node in a linked list. It contains a key-value pair and references to the previous
658
656
  * and next nodes in the list.
659
657
  */
660
- protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>) {
658
+ protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>): boolean {
661
659
  const { prev, next } = node;
662
660
  prev.next = next;
663
661
  next.prev = prev;
@@ -671,5 +669,6 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
671
669
  }
672
670
 
673
671
  this._size -= 1;
672
+ return true;
674
673
  }
675
674
  }
@@ -42,7 +42,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
42
42
 
43
43
  if (elements) {
44
44
  for (const el of elements) {
45
- this.push(el);
45
+ this.add(el);
46
46
  }
47
47
  // this.fix();
48
48
  }
@@ -91,26 +91,9 @@ export class Heap<E = any> extends IterableElementBase<E> {
91
91
  * Insert an element into the heap and maintain the heap properties.
92
92
  * @param element - The element to be inserted.
93
93
  */
94
- add(element: E): Heap<E> {
95
- return this.push(element);
96
- }
97
-
98
- /**
99
- * Time Complexity: O(log n), where n is the number of elements in the heap.
100
- * Space Complexity: O(1)
101
- */
102
-
103
- /**
104
- * Time Complexity: O(log n), where n is the number of elements in the heap.
105
- * Space Complexity: O(1)
106
- *
107
- * Insert an element into the heap and maintain the heap properties.
108
- * @param element - The element to be inserted.
109
- */
110
- push(element: E): Heap<E> {
94
+ add(element: E): boolean {
111
95
  this._elements.push(element);
112
- this._bubbleUp(this.elements.length - 1);
113
- return this;
96
+ return this._bubbleUp(this.elements.length - 1);
114
97
  }
115
98
 
116
99
  /**
@@ -136,22 +119,6 @@ export class Heap<E = any> extends IterableElementBase<E> {
136
119
  return value;
137
120
  }
138
121
 
139
- /**
140
- * Time Complexity: O(log n), where n is the number of elements in the heap.
141
- * Space Complexity: O(1)
142
- */
143
-
144
- /**
145
- * Time Complexity: O(log n), where n is the number of elements in the heap.
146
- * Space Complexity: O(1)
147
- *
148
- * Remove and return the top element (smallest or largest element) from the heap.
149
- * @returns The top element or undefined if the heap is empty.
150
- */
151
- pop(): E | undefined {
152
- return this.poll();
153
- }
154
-
155
122
  /**
156
123
  * Peek at the top element of the heap without removing it.
157
124
  * @returns The top element or undefined if the heap is empty.
@@ -164,14 +131,14 @@ export class Heap<E = any> extends IterableElementBase<E> {
164
131
  * Check if the heap is empty.
165
132
  * @returns True if the heap is empty, otherwise false.
166
133
  */
167
- isEmpty() {
134
+ isEmpty(): boolean {
168
135
  return this.size === 0;
169
136
  }
170
137
 
171
138
  /**
172
139
  * Reset the elements of the heap. Make the elements empty.
173
140
  */
174
- clear() {
141
+ clear(): void {
175
142
  this._elements = [];
176
143
  }
177
144
 
@@ -187,9 +154,9 @@ export class Heap<E = any> extends IterableElementBase<E> {
187
154
  * Clear and add elements of the heap
188
155
  * @param elements
189
156
  */
190
- refill(elements: E[]) {
157
+ refill(elements: E[]): boolean[] {
191
158
  this._elements = elements;
192
- this.fix();
159
+ return this.fix();
193
160
  }
194
161
 
195
162
  /**
@@ -225,11 +192,11 @@ export class Heap<E = any> extends IterableElementBase<E> {
225
192
  * @returns The `delete` function is returning a boolean value. It returns `true` if the element was
226
193
  * successfully deleted from the array, and `false` if the element was not found in the array.
227
194
  */
228
- delete(element: E) {
195
+ delete(element: E): boolean {
229
196
  const index = this.elements.indexOf(element);
230
197
  if (index < 0) return false;
231
198
  if (index === 0) {
232
- this.pop();
199
+ this.poll();
233
200
  } else if (index === this.elements.length - 1) {
234
201
  this.elements.pop();
235
202
  } else {
@@ -348,8 +315,10 @@ export class Heap<E = any> extends IterableElementBase<E> {
348
315
  *
349
316
  * Fix the entire heap to maintain heap properties.
350
317
  */
351
- fix() {
352
- for (let i = Math.floor(this.size / 2); i >= 0; i--) this._sinkDown(i, this.elements.length >> 1);
318
+ fix(): boolean[] {
319
+ const results: boolean[] = [];
320
+ for (let i = Math.floor(this.size / 2); i >= 0; i--) results.push(this._sinkDown(i, this.elements.length >> 1));
321
+ return results;
353
322
  }
354
323
 
355
324
  /**
@@ -378,7 +347,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
378
347
  let index = 0;
379
348
  for (const current of this) {
380
349
  if (callback.call(thisArg, current, index, this)) {
381
- filteredList.push(current);
350
+ filteredList.add(current);
382
351
  }
383
352
  index++;
384
353
  }
@@ -421,16 +390,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
421
390
  return mappedHeap;
422
391
  }
423
392
 
424
- /**
425
- * Time Complexity: O(log n)
426
- * Space Complexity: O(1)
427
- */
428
-
429
- print(): void {
430
- console.log([...this]);
431
- }
432
-
433
- protected* _getIterator() {
393
+ protected* _getIterator(): IterableIterator<E> {
434
394
  for (const element of this.elements) {
435
395
  yield element;
436
396
  }
@@ -448,7 +408,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
448
408
  * Float operation to maintain heap properties after adding an element.
449
409
  * @param index - The index of the newly added element.
450
410
  */
451
- protected _bubbleUp(index: number) {
411
+ protected _bubbleUp(index: number): boolean {
452
412
  const element = this.elements[index];
453
413
  while (index > 0) {
454
414
  const parent = (index - 1) >> 1;
@@ -458,6 +418,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
458
418
  index = parent;
459
419
  }
460
420
  this.elements[index] = element;
421
+ return true;
461
422
  }
462
423
 
463
424
  /**
@@ -468,7 +429,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
468
429
  * @param index - The index from which to start sinking.
469
430
  * @param halfLength
470
431
  */
471
- protected _sinkDown(index: number, halfLength: number) {
432
+ protected _sinkDown(index: number, halfLength: number): boolean {
472
433
  const element = this.elements[index];
473
434
  while (index < halfLength) {
474
435
  let left = index << 1 | 1;
@@ -486,6 +447,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
486
447
  index = left;
487
448
  }
488
449
  this.elements[index] = element;
450
+ return true;
489
451
  }
490
452
  }
491
453