data-structure-typed 1.47.9 → 1.48.1
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 +1 -1
- package/README.md +49 -48
- package/benchmark/report.html +16 -16
- package/benchmark/report.json +172 -292
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +6 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +8 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +52 -0
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +106 -28
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +13 -0
- package/dist/cjs/data-structures/binary-tree/bst.js +41 -21
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +16 -0
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +54 -31
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +28 -0
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +60 -21
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +173 -16
- package/dist/cjs/data-structures/hash/hash-map.js +373 -37
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/cjs/data-structures/matrix/navigator.js.map +1 -1
- package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/tree/tree.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -0
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +6 -0
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +8 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +52 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +106 -28
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +13 -0
- package/dist/mjs/data-structures/binary-tree/bst.js +41 -21
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +16 -0
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +54 -31
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +28 -0
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +60 -21
- package/dist/mjs/data-structures/hash/hash-map.d.ts +173 -16
- package/dist/mjs/data-structures/hash/hash-map.js +369 -35
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -0
- package/dist/umd/data-structure-typed.js +615 -120
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +9 -0
- package/src/data-structures/binary-tree/binary-tree.ts +109 -24
- package/src/data-structures/binary-tree/bst.ts +38 -19
- package/src/data-structures/binary-tree/rb-tree.ts +55 -31
- package/src/data-structures/binary-tree/tree-multimap.ts +60 -17
- package/src/data-structures/hash/hash-map.ts +400 -40
- package/src/types/data-structures/hash/hash-map.ts +2 -0
- package/test/integration/avl-tree.test.ts +2 -2
- package/test/integration/bst.test.ts +21 -25
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +17 -12
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +16 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +16 -1
- package/test/unit/data-structures/binary-tree/bst.test.ts +16 -0
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +82 -1
- package/test/unit/data-structures/hash/hash-map.test.ts +312 -18
|
@@ -127,6 +127,7 @@ var dataStructureTyped = (() => {
|
|
|
127
127
|
HashTableNode: () => HashTableNode,
|
|
128
128
|
Heap: () => Heap,
|
|
129
129
|
IterationType: () => IterationType,
|
|
130
|
+
LinkedHashMap: () => LinkedHashMap,
|
|
130
131
|
LinkedListQueue: () => LinkedListQueue,
|
|
131
132
|
MapEdge: () => MapEdge,
|
|
132
133
|
MapGraph: () => MapGraph,
|
|
@@ -517,6 +518,299 @@ var dataStructureTyped = (() => {
|
|
|
517
518
|
|
|
518
519
|
// src/data-structures/hash/hash-map.ts
|
|
519
520
|
var HashMap = class _HashMap {
|
|
521
|
+
/**
|
|
522
|
+
* The constructor function initializes a new instance of a class with optional elements and options.
|
|
523
|
+
* @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It
|
|
524
|
+
* is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with
|
|
525
|
+
* key-value pairs.
|
|
526
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
527
|
+
* configuration options for the constructor. In this case, it has one property:
|
|
528
|
+
*/
|
|
529
|
+
constructor(elements = [], options) {
|
|
530
|
+
__publicField(this, "_store", {});
|
|
531
|
+
__publicField(this, "_objMap", /* @__PURE__ */ new Map());
|
|
532
|
+
__publicField(this, "_size", 0);
|
|
533
|
+
__publicField(this, "_hashFn", (key) => String(key));
|
|
534
|
+
if (options) {
|
|
535
|
+
const { hashFn } = options;
|
|
536
|
+
if (hashFn) {
|
|
537
|
+
this._hashFn = hashFn;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
if (elements) {
|
|
541
|
+
this.setMany(elements);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
get size() {
|
|
545
|
+
return this._size;
|
|
546
|
+
}
|
|
547
|
+
isEmpty() {
|
|
548
|
+
return this.size === 0;
|
|
549
|
+
}
|
|
550
|
+
clear() {
|
|
551
|
+
this._store = {};
|
|
552
|
+
this._objMap.clear();
|
|
553
|
+
this._size = 0;
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* The `set` function adds a key-value pair to a map-like data structure, incrementing the size if
|
|
557
|
+
* the key is not already present.
|
|
558
|
+
* @param {K} key - The key parameter is the key used to identify the value in the data structure. It
|
|
559
|
+
* can be of any type, but if it is an object, it will be stored in a Map, otherwise it will be
|
|
560
|
+
* stored in a regular JavaScript object.
|
|
561
|
+
* @param {V} value - The value parameter represents the value that you want to associate with the
|
|
562
|
+
* key in the data structure.
|
|
563
|
+
*/
|
|
564
|
+
set(key, value) {
|
|
565
|
+
if (this._isObjKey(key)) {
|
|
566
|
+
if (!this._objMap.has(key)) {
|
|
567
|
+
this._size++;
|
|
568
|
+
}
|
|
569
|
+
this._objMap.set(key, value);
|
|
570
|
+
} else {
|
|
571
|
+
const strKey = this._getNoObjKey(key);
|
|
572
|
+
if (this._store[strKey] === void 0) {
|
|
573
|
+
this._size++;
|
|
574
|
+
}
|
|
575
|
+
this._store[strKey] = { key, value };
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* The function "setMany" sets multiple key-value pairs in a map.
|
|
580
|
+
* @param elements - The `elements` parameter is an iterable containing key-value pairs. Each
|
|
581
|
+
* key-value pair is represented as an array with two elements: the key and the value.
|
|
582
|
+
*/
|
|
583
|
+
setMany(elements) {
|
|
584
|
+
for (const [key, value] of elements)
|
|
585
|
+
this.set(key, value);
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* The `get` function retrieves a value from a map based on a given key, either from an object map or
|
|
589
|
+
* a string map.
|
|
590
|
+
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
|
|
591
|
+
* of any type, but it should be compatible with the key type used when the map was created.
|
|
592
|
+
* @returns The method `get(key: K)` returns a value of type `V` if the key exists in the `_objMap`
|
|
593
|
+
* or `_store`, otherwise it returns `undefined`.
|
|
594
|
+
*/
|
|
595
|
+
get(key) {
|
|
596
|
+
var _a;
|
|
597
|
+
if (this._isObjKey(key)) {
|
|
598
|
+
return this._objMap.get(key);
|
|
599
|
+
} else {
|
|
600
|
+
const strKey = this._getNoObjKey(key);
|
|
601
|
+
return (_a = this._store[strKey]) == null ? void 0 : _a.value;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* The `has` function checks if a given key exists in the `_objMap` or `_store` based on whether it
|
|
606
|
+
* is an object key or not.
|
|
607
|
+
* @param {K} key - The parameter "key" is of type K, which means it can be any type.
|
|
608
|
+
* @returns The `has` method is returning a boolean value.
|
|
609
|
+
*/
|
|
610
|
+
has(key) {
|
|
611
|
+
if (this._isObjKey(key)) {
|
|
612
|
+
return this._objMap.has(key);
|
|
613
|
+
} else {
|
|
614
|
+
const strKey = this._getNoObjKey(key);
|
|
615
|
+
return strKey in this._store;
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* The `delete` function removes an element from a map-like data structure based on the provided key.
|
|
620
|
+
* @param {K} key - The `key` parameter is the key of the element that you want to delete from the
|
|
621
|
+
* data structure.
|
|
622
|
+
* @returns The `delete` method returns a boolean value. It returns `true` if the key was
|
|
623
|
+
* successfully deleted from the map, and `false` if the key was not found in the map.
|
|
624
|
+
*/
|
|
625
|
+
delete(key) {
|
|
626
|
+
if (this._isObjKey(key)) {
|
|
627
|
+
if (this._objMap.has(key)) {
|
|
628
|
+
this._size--;
|
|
629
|
+
}
|
|
630
|
+
return this._objMap.delete(key);
|
|
631
|
+
} else {
|
|
632
|
+
const strKey = this._getNoObjKey(key);
|
|
633
|
+
if (strKey in this._store) {
|
|
634
|
+
delete this._store[strKey];
|
|
635
|
+
this._size--;
|
|
636
|
+
return true;
|
|
637
|
+
}
|
|
638
|
+
return false;
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
643
|
+
* object map.
|
|
644
|
+
*/
|
|
645
|
+
*[Symbol.iterator]() {
|
|
646
|
+
for (const node of Object.values(this._store)) {
|
|
647
|
+
yield [node.key, node.value];
|
|
648
|
+
}
|
|
649
|
+
for (const node of this._objMap) {
|
|
650
|
+
yield node;
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* The function returns an iterator that yields key-value pairs from the object.
|
|
655
|
+
*/
|
|
656
|
+
*entries() {
|
|
657
|
+
for (const item of this) {
|
|
658
|
+
yield item;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* The function `keys()` returns an iterator that yields all the keys of the object.
|
|
663
|
+
*/
|
|
664
|
+
*keys() {
|
|
665
|
+
for (const [key] of this) {
|
|
666
|
+
yield key;
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
*values() {
|
|
670
|
+
for (const [, value] of this) {
|
|
671
|
+
yield value;
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* The `every` function checks if every element in a HashMap satisfies a given predicate function.
|
|
676
|
+
* @param predicate - The predicate parameter is a function that takes four arguments: value, key,
|
|
677
|
+
* index, and map. It is used to test each element in the map against a condition. If the predicate
|
|
678
|
+
* function returns false for any element, the every() method will return false. If the predicate
|
|
679
|
+
* function returns true for all
|
|
680
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
681
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
682
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
683
|
+
* @returns The method is returning a boolean value. It returns true if the predicate function
|
|
684
|
+
* returns true for every element in the map, and false otherwise.
|
|
685
|
+
*/
|
|
686
|
+
every(predicate, thisArg) {
|
|
687
|
+
let index = 0;
|
|
688
|
+
for (const [key, value] of this) {
|
|
689
|
+
if (!predicate.call(thisArg, value, key, index++, this)) {
|
|
690
|
+
return false;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
return true;
|
|
694
|
+
}
|
|
695
|
+
/**
|
|
696
|
+
* The "some" function checks if at least one element in a HashMap satisfies a given predicate.
|
|
697
|
+
* @param predicate - The `predicate` parameter is a function that takes four arguments: `value`,
|
|
698
|
+
* `key`, `index`, and `map`. It is used to determine whether a specific condition is met for a given
|
|
699
|
+
* key-value pair in the `HashMap`.
|
|
700
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
701
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
702
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
703
|
+
* @returns a boolean value. It returns true if the predicate function returns true for any element
|
|
704
|
+
* in the map, and false otherwise.
|
|
705
|
+
*/
|
|
706
|
+
some(predicate, thisArg) {
|
|
707
|
+
let index = 0;
|
|
708
|
+
for (const [key, value] of this) {
|
|
709
|
+
if (predicate.call(thisArg, value, key, index++, this)) {
|
|
710
|
+
return true;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
return false;
|
|
714
|
+
}
|
|
715
|
+
/**
|
|
716
|
+
* The `forEach` function iterates over the elements of a HashMap and applies a callback function to
|
|
717
|
+
* each element.
|
|
718
|
+
* @param callbackfn - A function that will be called for each key-value pair in the HashMap. It
|
|
719
|
+
* takes four parameters:
|
|
720
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
721
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
722
|
+
* be passed as the `this` value inside the `callbackfn` function. If `thisArg
|
|
723
|
+
*/
|
|
724
|
+
forEach(callbackfn, thisArg) {
|
|
725
|
+
let index = 0;
|
|
726
|
+
for (const [key, value] of this) {
|
|
727
|
+
callbackfn.call(thisArg, value, key, index++, this);
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
/**
|
|
731
|
+
* The `map` function in TypeScript creates a new HashMap by applying a callback function to each
|
|
732
|
+
* key-value pair in the original HashMap.
|
|
733
|
+
* @param callbackfn - The callback function that will be called for each key-value pair in the
|
|
734
|
+
* HashMap. It takes four parameters:
|
|
735
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
736
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
737
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
738
|
+
* @returns The `map` method is returning a new `HashMap` object with the transformed values based on
|
|
739
|
+
* the provided callback function.
|
|
740
|
+
*/
|
|
741
|
+
map(callbackfn, thisArg) {
|
|
742
|
+
const resultMap = new _HashMap();
|
|
743
|
+
let index = 0;
|
|
744
|
+
for (const [key, value] of this) {
|
|
745
|
+
resultMap.set(key, callbackfn.call(thisArg, value, key, index++, this));
|
|
746
|
+
}
|
|
747
|
+
return resultMap;
|
|
748
|
+
}
|
|
749
|
+
/**
|
|
750
|
+
* The `filter` function creates a new HashMap containing key-value pairs from the original HashMap
|
|
751
|
+
* that satisfy a given predicate function.
|
|
752
|
+
* @param predicate - The predicate parameter is a function that takes four arguments: value, key,
|
|
753
|
+
* index, and map. It is used to determine whether an element should be included in the filtered map
|
|
754
|
+
* or not. The function should return a boolean value - true if the element should be included, and
|
|
755
|
+
* false otherwise.
|
|
756
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
757
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
758
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
759
|
+
* @returns The `filter` method is returning a new `HashMap` object that contains the key-value pairs
|
|
760
|
+
* from the original `HashMap` that pass the provided `predicate` function.
|
|
761
|
+
*/
|
|
762
|
+
filter(predicate, thisArg) {
|
|
763
|
+
const filteredMap = new _HashMap();
|
|
764
|
+
let index = 0;
|
|
765
|
+
for (const [key, value] of this) {
|
|
766
|
+
if (predicate.call(thisArg, value, key, index++, this)) {
|
|
767
|
+
filteredMap.set(key, value);
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
return filteredMap;
|
|
771
|
+
}
|
|
772
|
+
/**
|
|
773
|
+
* The `reduce` function iterates over the elements of a HashMap and applies a callback function to
|
|
774
|
+
* each element, accumulating a single value.
|
|
775
|
+
* @param callbackfn - The callback function that will be called for each element in the HashMap. It
|
|
776
|
+
* takes five parameters:
|
|
777
|
+
* @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
|
778
|
+
* is the value that will be used as the first argument of the callback function when reducing the
|
|
779
|
+
* elements of the map.
|
|
780
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
781
|
+
* all the elements in the `HashMap`.
|
|
782
|
+
*/
|
|
783
|
+
reduce(callbackfn, initialValue) {
|
|
784
|
+
let accumulator = initialValue;
|
|
785
|
+
let index = 0;
|
|
786
|
+
for (const [key, value] of this) {
|
|
787
|
+
accumulator = callbackfn(accumulator, value, key, index++, this);
|
|
788
|
+
}
|
|
789
|
+
return accumulator;
|
|
790
|
+
}
|
|
791
|
+
print() {
|
|
792
|
+
console.log([...this.entries()]);
|
|
793
|
+
}
|
|
794
|
+
_isObjKey(key) {
|
|
795
|
+
const keyType = typeof key;
|
|
796
|
+
return (keyType === "object" || keyType === "function") && key !== null;
|
|
797
|
+
}
|
|
798
|
+
_getNoObjKey(key) {
|
|
799
|
+
const keyType = typeof key;
|
|
800
|
+
let strKey;
|
|
801
|
+
if (keyType !== "string" && keyType !== "number" && keyType !== "symbol") {
|
|
802
|
+
strKey = this._hashFn(key);
|
|
803
|
+
} else {
|
|
804
|
+
if (keyType === "number") {
|
|
805
|
+
strKey = key;
|
|
806
|
+
} else {
|
|
807
|
+
strKey = key;
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
return strKey;
|
|
811
|
+
}
|
|
812
|
+
};
|
|
813
|
+
var LinkedHashMap = class _LinkedHashMap {
|
|
520
814
|
constructor(elements, options = {
|
|
521
815
|
hashFn: (key) => String(key),
|
|
522
816
|
objHashFn: (key) => key
|
|
@@ -604,39 +898,65 @@ var dataStructureTyped = (() => {
|
|
|
604
898
|
*/
|
|
605
899
|
set(key, value) {
|
|
606
900
|
let node;
|
|
901
|
+
const isNewKey = !this.has(key);
|
|
607
902
|
if (isWeakKey(key)) {
|
|
608
903
|
const hash = this._objHashFn(key);
|
|
609
904
|
node = this._objMap.get(hash);
|
|
610
|
-
if (node) {
|
|
611
|
-
node.value = value;
|
|
612
|
-
} else {
|
|
905
|
+
if (!node && isNewKey) {
|
|
613
906
|
node = { key: hash, value, prev: this._tail, next: this._sentinel };
|
|
614
907
|
this._objMap.set(hash, node);
|
|
908
|
+
} else if (node) {
|
|
909
|
+
node.value = value;
|
|
615
910
|
}
|
|
616
911
|
} else {
|
|
617
912
|
const hash = this._hashFn(key);
|
|
618
913
|
node = this._noObjMap[hash];
|
|
619
|
-
if (node) {
|
|
914
|
+
if (!node && isNewKey) {
|
|
915
|
+
this._noObjMap[hash] = node = { key, value, prev: this._tail, next: this._sentinel };
|
|
916
|
+
} else if (node) {
|
|
620
917
|
node.value = value;
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
if (node && isNewKey) {
|
|
921
|
+
if (this._size === 0) {
|
|
922
|
+
this._head = node;
|
|
923
|
+
this._sentinel.next = node;
|
|
621
924
|
} else {
|
|
622
|
-
this.
|
|
623
|
-
|
|
624
|
-
value,
|
|
625
|
-
prev: this._tail,
|
|
626
|
-
next: this._sentinel
|
|
627
|
-
};
|
|
925
|
+
this._tail.next = node;
|
|
926
|
+
node.prev = this._tail;
|
|
628
927
|
}
|
|
928
|
+
this._tail = node;
|
|
929
|
+
this._sentinel.prev = node;
|
|
930
|
+
this._size++;
|
|
629
931
|
}
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
932
|
+
return this._size;
|
|
933
|
+
}
|
|
934
|
+
has(key) {
|
|
935
|
+
if (isWeakKey(key)) {
|
|
936
|
+
const hash = this._objHashFn(key);
|
|
937
|
+
return this._objMap.has(hash);
|
|
633
938
|
} else {
|
|
634
|
-
|
|
939
|
+
const hash = this._hashFn(key);
|
|
940
|
+
return hash in this._noObjMap;
|
|
635
941
|
}
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
942
|
+
}
|
|
943
|
+
setMany(entries) {
|
|
944
|
+
for (const entry of entries) {
|
|
945
|
+
const [key, value] = entry;
|
|
946
|
+
this.set(key, value);
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
keys() {
|
|
950
|
+
const keys = [];
|
|
951
|
+
for (const [key] of this)
|
|
952
|
+
keys.push(key);
|
|
953
|
+
return keys;
|
|
954
|
+
}
|
|
955
|
+
values() {
|
|
956
|
+
const values = [];
|
|
957
|
+
for (const [, value] of this)
|
|
958
|
+
values.push(value);
|
|
959
|
+
return values;
|
|
640
960
|
}
|
|
641
961
|
/**
|
|
642
962
|
* Time Complexity: O(1)
|
|
@@ -751,14 +1071,22 @@ var dataStructureTyped = (() => {
|
|
|
751
1071
|
this._size = 0;
|
|
752
1072
|
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
753
1073
|
}
|
|
1074
|
+
clone() {
|
|
1075
|
+
const cloned = new _LinkedHashMap([], { hashFn: this._hashFn, objHashFn: this._objHashFn });
|
|
1076
|
+
for (const entry of this) {
|
|
1077
|
+
const [key, value] = entry;
|
|
1078
|
+
cloned.set(key, value);
|
|
1079
|
+
}
|
|
1080
|
+
return cloned;
|
|
1081
|
+
}
|
|
754
1082
|
/**
|
|
755
|
-
* Time Complexity: O(n), where n is the number of elements in the
|
|
1083
|
+
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
|
|
756
1084
|
* Space Complexity: O(1)
|
|
757
1085
|
*
|
|
758
|
-
* The `forEach` function iterates over each element in a
|
|
1086
|
+
* The `forEach` function iterates over each element in a LinkedHashMap and executes a callback function on
|
|
759
1087
|
* each element.
|
|
760
1088
|
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
761
|
-
*
|
|
1089
|
+
* LinkedHashMap. It takes three arguments:
|
|
762
1090
|
*/
|
|
763
1091
|
forEach(callback) {
|
|
764
1092
|
let index = 0;
|
|
@@ -769,15 +1097,15 @@ var dataStructureTyped = (() => {
|
|
|
769
1097
|
}
|
|
770
1098
|
}
|
|
771
1099
|
/**
|
|
772
|
-
* The `filter` function takes a predicate function and returns a new
|
|
1100
|
+
* The `filter` function takes a predicate function and returns a new LinkedHashMap containing only the
|
|
773
1101
|
* key-value pairs that satisfy the predicate.
|
|
774
1102
|
* @param predicate - The `predicate` parameter is a function that takes two arguments: `element` and
|
|
775
1103
|
* `map`.
|
|
776
|
-
* @returns a new
|
|
1104
|
+
* @returns a new LinkedHashMap object that contains the key-value pairs from the original LinkedHashMap that
|
|
777
1105
|
* satisfy the given predicate function.
|
|
778
1106
|
*/
|
|
779
1107
|
filter(predicate) {
|
|
780
|
-
const filteredMap = new
|
|
1108
|
+
const filteredMap = new _LinkedHashMap();
|
|
781
1109
|
let index = 0;
|
|
782
1110
|
for (const [key, value] of this) {
|
|
783
1111
|
if (predicate([key, value], index, this)) {
|
|
@@ -788,14 +1116,14 @@ var dataStructureTyped = (() => {
|
|
|
788
1116
|
return filteredMap;
|
|
789
1117
|
}
|
|
790
1118
|
/**
|
|
791
|
-
* The `map` function takes a callback function and returns a new
|
|
1119
|
+
* The `map` function takes a callback function and returns a new LinkedHashMap with the values transformed
|
|
792
1120
|
* by the callback.
|
|
793
1121
|
* @param callback - The `callback` parameter is a function that takes two arguments: `element` and
|
|
794
1122
|
* `map`.
|
|
795
|
-
* @returns a new
|
|
1123
|
+
* @returns a new LinkedHashMap object with the values mapped according to the provided callback function.
|
|
796
1124
|
*/
|
|
797
1125
|
map(callback) {
|
|
798
|
-
const mappedMap = new
|
|
1126
|
+
const mappedMap = new _LinkedHashMap();
|
|
799
1127
|
let index = 0;
|
|
800
1128
|
for (const [key, value] of this) {
|
|
801
1129
|
const newValue = callback([key, value], index, this);
|
|
@@ -805,16 +1133,16 @@ var dataStructureTyped = (() => {
|
|
|
805
1133
|
return mappedMap;
|
|
806
1134
|
}
|
|
807
1135
|
/**
|
|
808
|
-
* The `reduce` function iterates over the elements of a
|
|
1136
|
+
* The `reduce` function iterates over the elements of a LinkedHashMap and applies a callback function to
|
|
809
1137
|
* each element, accumulating a single value.
|
|
810
1138
|
* @param callback - The callback parameter is a function that takes three arguments: accumulator,
|
|
811
|
-
* element, and map. It is called for each element in the
|
|
1139
|
+
* element, and map. It is called for each element in the LinkedHashMap and is used to accumulate a single
|
|
812
1140
|
* result.
|
|
813
1141
|
* @param {A} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
|
|
814
1142
|
* is the value that will be passed as the first argument to the `callback` function when reducing
|
|
815
1143
|
* the elements of the map.
|
|
816
1144
|
* @returns The `reduce` function is returning the final value of the accumulator after iterating
|
|
817
|
-
* over all the elements in the
|
|
1145
|
+
* over all the elements in the LinkedHashMap and applying the callback function to each element.
|
|
818
1146
|
*/
|
|
819
1147
|
reduce(callback, initialValue) {
|
|
820
1148
|
let accumulator = initialValue;
|
|
@@ -826,7 +1154,7 @@ var dataStructureTyped = (() => {
|
|
|
826
1154
|
return accumulator;
|
|
827
1155
|
}
|
|
828
1156
|
/**
|
|
829
|
-
* Time Complexity: O(n), where n is the number of elements in the
|
|
1157
|
+
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
|
|
830
1158
|
* Space Complexity: O(1)
|
|
831
1159
|
*
|
|
832
1160
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
@@ -6981,6 +7309,45 @@ var dataStructureTyped = (() => {
|
|
|
6981
7309
|
createTree(options) {
|
|
6982
7310
|
return new _BinaryTree([], __spreadValues({ iterationType: this.iterationType }, options));
|
|
6983
7311
|
}
|
|
7312
|
+
/**
|
|
7313
|
+
* The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class.
|
|
7314
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
|
|
7315
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the class N.
|
|
7316
|
+
*/
|
|
7317
|
+
isNode(exemplar) {
|
|
7318
|
+
return exemplar instanceof BinaryTreeNode;
|
|
7319
|
+
}
|
|
7320
|
+
/**
|
|
7321
|
+
* The function `exemplarToNode` converts an exemplar of a binary tree node into an actual node
|
|
7322
|
+
* object.
|
|
7323
|
+
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing the exemplar parameter of the
|
|
7324
|
+
* function. It can be any type.
|
|
7325
|
+
* @returns a value of type `N` (which represents a node), or `null`, or `undefined`.
|
|
7326
|
+
*/
|
|
7327
|
+
exemplarToNode(exemplar) {
|
|
7328
|
+
if (exemplar === void 0)
|
|
7329
|
+
return;
|
|
7330
|
+
let node;
|
|
7331
|
+
if (exemplar === null) {
|
|
7332
|
+
node = null;
|
|
7333
|
+
} else if (this.isEntry(exemplar)) {
|
|
7334
|
+
const [key, value] = exemplar;
|
|
7335
|
+
if (key === void 0) {
|
|
7336
|
+
return;
|
|
7337
|
+
} else if (key === null) {
|
|
7338
|
+
node = null;
|
|
7339
|
+
} else {
|
|
7340
|
+
node = this.createNode(key, value);
|
|
7341
|
+
}
|
|
7342
|
+
} else if (this.isNode(exemplar)) {
|
|
7343
|
+
node = exemplar;
|
|
7344
|
+
} else if (this.isNodeKey(exemplar)) {
|
|
7345
|
+
node = this.createNode(exemplar);
|
|
7346
|
+
} else {
|
|
7347
|
+
return;
|
|
7348
|
+
}
|
|
7349
|
+
return node;
|
|
7350
|
+
}
|
|
6984
7351
|
/**
|
|
6985
7352
|
* The function checks if a given value is an entry in a binary tree node.
|
|
6986
7353
|
* @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
|
|
@@ -7003,16 +7370,19 @@ var dataStructureTyped = (() => {
|
|
|
7003
7370
|
* @returns The function `add` returns the inserted node (`N`), `null`, or `undefined`.
|
|
7004
7371
|
*/
|
|
7005
7372
|
add(keyOrNodeOrEntry) {
|
|
7006
|
-
let inserted
|
|
7007
|
-
const
|
|
7373
|
+
let inserted;
|
|
7374
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
7375
|
+
if (newNode === void 0)
|
|
7376
|
+
return;
|
|
7377
|
+
const _bfs = (root, newNode2) => {
|
|
7008
7378
|
const queue = new Queue([root]);
|
|
7009
7379
|
while (queue.size > 0) {
|
|
7010
7380
|
const cur = queue.shift();
|
|
7011
|
-
if (
|
|
7012
|
-
this._replaceNode(cur,
|
|
7013
|
-
return
|
|
7381
|
+
if (newNode2 && cur.key === newNode2.key) {
|
|
7382
|
+
this._replaceNode(cur, newNode2);
|
|
7383
|
+
return newNode2;
|
|
7014
7384
|
}
|
|
7015
|
-
const inserted2 = this._addTo(
|
|
7385
|
+
const inserted2 = this._addTo(newNode2, cur);
|
|
7016
7386
|
if (inserted2 !== void 0)
|
|
7017
7387
|
return inserted2;
|
|
7018
7388
|
if (cur.left)
|
|
@@ -7021,29 +7391,11 @@ var dataStructureTyped = (() => {
|
|
|
7021
7391
|
queue.push(cur.right);
|
|
7022
7392
|
}
|
|
7023
7393
|
};
|
|
7024
|
-
if (keyOrNodeOrEntry === null) {
|
|
7025
|
-
needInsert = null;
|
|
7026
|
-
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
7027
|
-
needInsert = this.createNode(keyOrNodeOrEntry);
|
|
7028
|
-
} else if (keyOrNodeOrEntry instanceof BinaryTreeNode) {
|
|
7029
|
-
needInsert = keyOrNodeOrEntry;
|
|
7030
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
7031
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
7032
|
-
if (key === void 0) {
|
|
7033
|
-
return;
|
|
7034
|
-
} else if (key === null) {
|
|
7035
|
-
needInsert = null;
|
|
7036
|
-
} else {
|
|
7037
|
-
needInsert = this.createNode(key, value);
|
|
7038
|
-
}
|
|
7039
|
-
} else {
|
|
7040
|
-
return;
|
|
7041
|
-
}
|
|
7042
7394
|
if (this.root) {
|
|
7043
|
-
inserted = _bfs(this.root,
|
|
7395
|
+
inserted = _bfs(this.root, newNode);
|
|
7044
7396
|
} else {
|
|
7045
|
-
this._setRoot(
|
|
7046
|
-
if (
|
|
7397
|
+
this._setRoot(newNode);
|
|
7398
|
+
if (newNode) {
|
|
7047
7399
|
this._size = 1;
|
|
7048
7400
|
} else {
|
|
7049
7401
|
this._size = 0;
|
|
@@ -8194,6 +8546,60 @@ var dataStructureTyped = (() => {
|
|
|
8194
8546
|
}
|
|
8195
8547
|
return ans;
|
|
8196
8548
|
}
|
|
8549
|
+
/**
|
|
8550
|
+
* Time complexity: O(n)
|
|
8551
|
+
* Space complexity: O(n)
|
|
8552
|
+
*/
|
|
8553
|
+
/**
|
|
8554
|
+
* Time complexity: O(n)
|
|
8555
|
+
* Space complexity: O(n)
|
|
8556
|
+
*
|
|
8557
|
+
* The function "keys" returns an array of keys from a given object.
|
|
8558
|
+
* @returns an array of BTNKey objects.
|
|
8559
|
+
*/
|
|
8560
|
+
keys() {
|
|
8561
|
+
const keys = [];
|
|
8562
|
+
for (const entry of this) {
|
|
8563
|
+
keys.push(entry[0]);
|
|
8564
|
+
}
|
|
8565
|
+
return keys;
|
|
8566
|
+
}
|
|
8567
|
+
/**
|
|
8568
|
+
* Time complexity: O(n)
|
|
8569
|
+
* Space complexity: O(n)
|
|
8570
|
+
*/
|
|
8571
|
+
/**
|
|
8572
|
+
* Time complexity: O(n)
|
|
8573
|
+
* Space complexity: O(n)
|
|
8574
|
+
*
|
|
8575
|
+
* The function "values" returns an array of values from a map-like object.
|
|
8576
|
+
* @returns The `values()` method is returning an array of values (`V`) from the entries in the
|
|
8577
|
+
* object.
|
|
8578
|
+
*/
|
|
8579
|
+
values() {
|
|
8580
|
+
const values = [];
|
|
8581
|
+
for (const entry of this) {
|
|
8582
|
+
values.push(entry[1]);
|
|
8583
|
+
}
|
|
8584
|
+
return values;
|
|
8585
|
+
}
|
|
8586
|
+
/**
|
|
8587
|
+
* Time complexity: O(n)
|
|
8588
|
+
* Space complexity: O(n)
|
|
8589
|
+
*/
|
|
8590
|
+
/**
|
|
8591
|
+
* Time complexity: O(n)
|
|
8592
|
+
* Space complexity: O(n)
|
|
8593
|
+
*
|
|
8594
|
+
* The `clone` function creates a new tree object and copies all the nodes from the original tree to
|
|
8595
|
+
* the new tree.
|
|
8596
|
+
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
8597
|
+
*/
|
|
8598
|
+
clone() {
|
|
8599
|
+
const cloned = this.createTree();
|
|
8600
|
+
this.bfs((node) => cloned.add([node.key, node.value]));
|
|
8601
|
+
return cloned;
|
|
8602
|
+
}
|
|
8197
8603
|
/**
|
|
8198
8604
|
* Time complexity: O(n)
|
|
8199
8605
|
* Space complexity: O(1)
|
|
@@ -8549,6 +8955,40 @@ var dataStructureTyped = (() => {
|
|
|
8549
8955
|
comparator: this.comparator
|
|
8550
8956
|
}, options));
|
|
8551
8957
|
}
|
|
8958
|
+
/**
|
|
8959
|
+
* The function checks if an exemplar is an instance of BSTNode.
|
|
8960
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
|
|
8961
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
8962
|
+
*/
|
|
8963
|
+
isNode(exemplar) {
|
|
8964
|
+
return exemplar instanceof BSTNode;
|
|
8965
|
+
}
|
|
8966
|
+
/**
|
|
8967
|
+
* The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar
|
|
8968
|
+
* is valid, otherwise it returns undefined.
|
|
8969
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
8970
|
+
* @returns a variable `node` which is of type `N` or `undefined`.
|
|
8971
|
+
*/
|
|
8972
|
+
exemplarToNode(exemplar) {
|
|
8973
|
+
let node;
|
|
8974
|
+
if (exemplar === null || exemplar === void 0) {
|
|
8975
|
+
return;
|
|
8976
|
+
} else if (this.isNode(exemplar)) {
|
|
8977
|
+
node = exemplar;
|
|
8978
|
+
} else if (this.isEntry(exemplar)) {
|
|
8979
|
+
const [key, value] = exemplar;
|
|
8980
|
+
if (key === void 0 || key === null) {
|
|
8981
|
+
return;
|
|
8982
|
+
} else {
|
|
8983
|
+
node = this.createNode(key, value);
|
|
8984
|
+
}
|
|
8985
|
+
} else if (this.isNodeKey(exemplar)) {
|
|
8986
|
+
node = this.createNode(exemplar);
|
|
8987
|
+
} else {
|
|
8988
|
+
return;
|
|
8989
|
+
}
|
|
8990
|
+
return node;
|
|
8991
|
+
}
|
|
8552
8992
|
/**
|
|
8553
8993
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
8554
8994
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -8564,24 +9004,9 @@ var dataStructureTyped = (() => {
|
|
|
8564
9004
|
* (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
|
|
8565
9005
|
*/
|
|
8566
9006
|
add(keyOrNodeOrEntry) {
|
|
8567
|
-
|
|
8568
|
-
|
|
8569
|
-
}
|
|
8570
|
-
let newNode;
|
|
8571
|
-
if (keyOrNodeOrEntry instanceof BSTNode) {
|
|
8572
|
-
newNode = keyOrNodeOrEntry;
|
|
8573
|
-
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
8574
|
-
newNode = this.createNode(keyOrNodeOrEntry);
|
|
8575
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
8576
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
8577
|
-
if (key === void 0 || key === null) {
|
|
8578
|
-
return;
|
|
8579
|
-
} else {
|
|
8580
|
-
newNode = this.createNode(key, value);
|
|
8581
|
-
}
|
|
8582
|
-
} else {
|
|
9007
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
9008
|
+
if (newNode === void 0)
|
|
8583
9009
|
return;
|
|
8584
|
-
}
|
|
8585
9010
|
if (this.root === void 0) {
|
|
8586
9011
|
this._setRoot(newNode);
|
|
8587
9012
|
this._size++;
|
|
@@ -9545,6 +9970,14 @@ var dataStructureTyped = (() => {
|
|
|
9545
9970
|
comparator: this.comparator
|
|
9546
9971
|
}, options));
|
|
9547
9972
|
}
|
|
9973
|
+
/**
|
|
9974
|
+
* The function checks if an exemplar is an instance of AVLTreeNode.
|
|
9975
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
9976
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class.
|
|
9977
|
+
*/
|
|
9978
|
+
isNode(exemplar) {
|
|
9979
|
+
return exemplar instanceof AVLTreeNode;
|
|
9980
|
+
}
|
|
9548
9981
|
/**
|
|
9549
9982
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
9550
9983
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -9966,70 +10399,93 @@ var dataStructureTyped = (() => {
|
|
|
9966
10399
|
}, options));
|
|
9967
10400
|
}
|
|
9968
10401
|
/**
|
|
9969
|
-
*
|
|
9970
|
-
*
|
|
10402
|
+
* The function checks if an exemplar is an instance of the RedBlackTreeNode class.
|
|
10403
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
10404
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
10405
|
+
* class.
|
|
9971
10406
|
*/
|
|
10407
|
+
isNode(exemplar) {
|
|
10408
|
+
return exemplar instanceof RedBlackTreeNode;
|
|
10409
|
+
}
|
|
9972
10410
|
/**
|
|
9973
|
-
* The function
|
|
9974
|
-
*
|
|
9975
|
-
* @
|
|
9976
|
-
*
|
|
10411
|
+
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
10412
|
+
* otherwise it returns undefined.
|
|
10413
|
+
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree
|
|
10414
|
+
* node. It can be either a node itself, an entry (key-value pair), a node key, or any other value
|
|
10415
|
+
* that is not a valid exemplar.
|
|
10416
|
+
* @returns a variable `node` which is of type `N | undefined`.
|
|
9977
10417
|
*/
|
|
9978
|
-
|
|
10418
|
+
exemplarToNode(exemplar) {
|
|
9979
10419
|
let node;
|
|
9980
|
-
if (
|
|
9981
|
-
node = this.createNode(keyOrNodeOrEntry, void 0, 1 /* RED */);
|
|
9982
|
-
} else if (keyOrNodeOrEntry instanceof RedBlackTreeNode) {
|
|
9983
|
-
node = keyOrNodeOrEntry;
|
|
9984
|
-
} else if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0) {
|
|
10420
|
+
if (exemplar === null || exemplar === void 0) {
|
|
9985
10421
|
return;
|
|
9986
|
-
} else if (this.
|
|
9987
|
-
|
|
10422
|
+
} else if (this.isNode(exemplar)) {
|
|
10423
|
+
node = exemplar;
|
|
10424
|
+
} else if (this.isEntry(exemplar)) {
|
|
10425
|
+
const [key, value] = exemplar;
|
|
9988
10426
|
if (key === void 0 || key === null) {
|
|
9989
10427
|
return;
|
|
9990
10428
|
} else {
|
|
9991
10429
|
node = this.createNode(key, value, 1 /* RED */);
|
|
9992
10430
|
}
|
|
10431
|
+
} else if (this.isNodeKey(exemplar)) {
|
|
10432
|
+
node = this.createNode(exemplar, void 0, 1 /* RED */);
|
|
9993
10433
|
} else {
|
|
9994
10434
|
return;
|
|
9995
10435
|
}
|
|
9996
|
-
node
|
|
9997
|
-
|
|
10436
|
+
return node;
|
|
10437
|
+
}
|
|
10438
|
+
/**
|
|
10439
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
10440
|
+
* Space Complexity: O(1)
|
|
10441
|
+
*/
|
|
10442
|
+
/**
|
|
10443
|
+
* The function adds a node to a Red-Black Tree data structure.
|
|
10444
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
10445
|
+
* @returns The method `add` returns either an instance of `N` (the node that was added) or
|
|
10446
|
+
* `undefined`.
|
|
10447
|
+
*/
|
|
10448
|
+
add(keyOrNodeOrEntry) {
|
|
10449
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
10450
|
+
if (newNode === void 0)
|
|
10451
|
+
return;
|
|
10452
|
+
newNode.left = this.Sentinel;
|
|
10453
|
+
newNode.right = this.Sentinel;
|
|
9998
10454
|
let y = void 0;
|
|
9999
10455
|
let x = this.root;
|
|
10000
10456
|
while (x !== this.Sentinel) {
|
|
10001
10457
|
y = x;
|
|
10002
10458
|
if (x) {
|
|
10003
|
-
if (
|
|
10459
|
+
if (newNode.key < x.key) {
|
|
10004
10460
|
x = x.left;
|
|
10005
|
-
} else if (
|
|
10461
|
+
} else if (newNode.key > x.key) {
|
|
10006
10462
|
x = x == null ? void 0 : x.right;
|
|
10007
10463
|
} else {
|
|
10008
|
-
if (
|
|
10009
|
-
this._replaceNode(x,
|
|
10464
|
+
if (newNode !== x) {
|
|
10465
|
+
this._replaceNode(x, newNode);
|
|
10010
10466
|
}
|
|
10011
10467
|
return;
|
|
10012
10468
|
}
|
|
10013
10469
|
}
|
|
10014
10470
|
}
|
|
10015
|
-
|
|
10471
|
+
newNode.parent = y;
|
|
10016
10472
|
if (y === void 0) {
|
|
10017
|
-
this._setRoot(
|
|
10018
|
-
} else if (
|
|
10019
|
-
y.left =
|
|
10473
|
+
this._setRoot(newNode);
|
|
10474
|
+
} else if (newNode.key < y.key) {
|
|
10475
|
+
y.left = newNode;
|
|
10020
10476
|
} else {
|
|
10021
|
-
y.right =
|
|
10477
|
+
y.right = newNode;
|
|
10022
10478
|
}
|
|
10023
|
-
if (
|
|
10024
|
-
|
|
10479
|
+
if (newNode.parent === void 0) {
|
|
10480
|
+
newNode.color = 0 /* BLACK */;
|
|
10025
10481
|
this._size++;
|
|
10026
10482
|
return;
|
|
10027
10483
|
}
|
|
10028
|
-
if (
|
|
10484
|
+
if (newNode.parent.parent === void 0) {
|
|
10029
10485
|
this._size++;
|
|
10030
10486
|
return;
|
|
10031
10487
|
}
|
|
10032
|
-
this._fixInsert(
|
|
10488
|
+
this._fixInsert(newNode);
|
|
10033
10489
|
this._size++;
|
|
10034
10490
|
}
|
|
10035
10491
|
/**
|
|
@@ -10480,6 +10936,43 @@ var dataStructureTyped = (() => {
|
|
|
10480
10936
|
comparator: this.comparator
|
|
10481
10937
|
}, options));
|
|
10482
10938
|
}
|
|
10939
|
+
/**
|
|
10940
|
+
* The function checks if an exemplar is an instance of the TreeMultimapNode class.
|
|
10941
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
10942
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
10943
|
+
* class.
|
|
10944
|
+
*/
|
|
10945
|
+
isNode(exemplar) {
|
|
10946
|
+
return exemplar instanceof TreeMultimapNode;
|
|
10947
|
+
}
|
|
10948
|
+
/**
|
|
10949
|
+
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
10950
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`, where `V` represents
|
|
10951
|
+
* the value type and `N` represents the node type.
|
|
10952
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
10953
|
+
* times the node should be created. If not provided, it defaults to 1.
|
|
10954
|
+
* @returns a value of type `N` (the generic type parameter) or `undefined`.
|
|
10955
|
+
*/
|
|
10956
|
+
exemplarToNode(exemplar, count = 1) {
|
|
10957
|
+
let node;
|
|
10958
|
+
if (exemplar === void 0 || exemplar === null) {
|
|
10959
|
+
return;
|
|
10960
|
+
} else if (this.isNode(exemplar)) {
|
|
10961
|
+
node = exemplar;
|
|
10962
|
+
} else if (this.isEntry(exemplar)) {
|
|
10963
|
+
const [key, value] = exemplar;
|
|
10964
|
+
if (key === void 0 || key === null) {
|
|
10965
|
+
return;
|
|
10966
|
+
} else {
|
|
10967
|
+
node = this.createNode(key, value, count);
|
|
10968
|
+
}
|
|
10969
|
+
} else if (this.isNodeKey(exemplar)) {
|
|
10970
|
+
node = this.createNode(exemplar, void 0, count);
|
|
10971
|
+
} else {
|
|
10972
|
+
return;
|
|
10973
|
+
}
|
|
10974
|
+
return node;
|
|
10975
|
+
}
|
|
10483
10976
|
/**
|
|
10484
10977
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
10485
10978
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -10497,23 +10990,9 @@ var dataStructureTyped = (() => {
|
|
|
10497
10990
|
* @returns either a node (`N`) or `undefined`.
|
|
10498
10991
|
*/
|
|
10499
10992
|
add(keyOrNodeOrEntry, count = 1) {
|
|
10500
|
-
|
|
10501
|
-
if (
|
|
10502
|
-
return;
|
|
10503
|
-
} else if (keyOrNodeOrEntry instanceof TreeMultimapNode) {
|
|
10504
|
-
newNode = keyOrNodeOrEntry;
|
|
10505
|
-
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
10506
|
-
newNode = this.createNode(keyOrNodeOrEntry, void 0, count);
|
|
10507
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
10508
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
10509
|
-
if (key === void 0 || key === null) {
|
|
10510
|
-
return;
|
|
10511
|
-
} else {
|
|
10512
|
-
newNode = this.createNode(key, value, count);
|
|
10513
|
-
}
|
|
10514
|
-
} else {
|
|
10993
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry, count);
|
|
10994
|
+
if (newNode === void 0)
|
|
10515
10995
|
return;
|
|
10516
|
-
}
|
|
10517
10996
|
const orgNodeCount = (newNode == null ? void 0 : newNode.count) || 0;
|
|
10518
10997
|
const inserted = super.add(newNode);
|
|
10519
10998
|
if (inserted) {
|
|
@@ -10674,6 +11153,22 @@ var dataStructureTyped = (() => {
|
|
|
10674
11153
|
super.clear();
|
|
10675
11154
|
this._count = 0;
|
|
10676
11155
|
}
|
|
11156
|
+
/**
|
|
11157
|
+
* Time complexity: O(n)
|
|
11158
|
+
* Space complexity: O(n)
|
|
11159
|
+
*/
|
|
11160
|
+
/**
|
|
11161
|
+
* Time complexity: O(n)
|
|
11162
|
+
* Space complexity: O(n)
|
|
11163
|
+
*
|
|
11164
|
+
* The `clone` function creates a deep copy of a tree object.
|
|
11165
|
+
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
11166
|
+
*/
|
|
11167
|
+
clone() {
|
|
11168
|
+
const cloned = this.createTree();
|
|
11169
|
+
this.bfs((node) => cloned.add([node.key, node.value], node.count));
|
|
11170
|
+
return cloned;
|
|
11171
|
+
}
|
|
10677
11172
|
/**
|
|
10678
11173
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
10679
11174
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|