data-structure-typed 1.49.6 → 1.49.8
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 +16 -13
- package/README_zh-CN.md +2 -0
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +148 -148
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +5 -5
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +3 -3
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +11 -13
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/cjs/data-structures/binary-tree/bst.js +6 -7
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +6 -6
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +3 -3
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +24 -27
- package/dist/cjs/data-structures/hash/hash-map.js +35 -35
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/index.d.ts +0 -1
- package/dist/cjs/data-structures/hash/index.js +0 -1
- package/dist/cjs/data-structures/hash/index.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +2 -1
- package/dist/cjs/data-structures/heap/heap.js +13 -13
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js +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 +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 +1 -3
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +2 -8
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +15 -18
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +2 -7
- package/dist/cjs/data-structures/matrix/matrix.js +0 -7
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +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 +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +2 -11
- package/dist/cjs/data-structures/queue/deque.js +9 -13
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/cjs/data-structures/queue/queue.js +29 -25
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.js +2 -3
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +2 -2
- package/dist/cjs/data-structures/trie/trie.js +9 -5
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +5 -2
- package/dist/cjs/types/data-structures/hash/index.d.ts +0 -1
- package/dist/cjs/types/data-structures/hash/index.js +0 -1
- package/dist/cjs/types/data-structures/hash/index.js.map +1 -1
- package/dist/cjs/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/cjs/types/data-structures/linked-list/index.d.ts +1 -0
- package/dist/cjs/types/data-structures/linked-list/index.js +1 -0
- package/dist/cjs/types/data-structures/linked-list/index.js.map +1 -1
- package/dist/cjs/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
- package/dist/cjs/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/cjs/types/data-structures/matrix/index.js +1 -0
- package/dist/cjs/types/data-structures/matrix/index.js.map +1 -1
- package/dist/cjs/types/data-structures/matrix/matrix.d.ts +7 -1
- package/dist/cjs/types/data-structures/queue/deque.d.ts +3 -1
- package/dist/cjs/types/data-structures/trie/trie.d.ts +3 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +5 -5
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +11 -13
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/bst.js +6 -7
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +5 -6
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +3 -3
- package/dist/mjs/data-structures/hash/hash-map.d.ts +24 -27
- package/dist/mjs/data-structures/hash/hash-map.js +35 -37
- package/dist/mjs/data-structures/hash/index.d.ts +0 -1
- package/dist/mjs/data-structures/hash/index.js +0 -1
- package/dist/mjs/data-structures/heap/heap.d.ts +2 -1
- package/dist/mjs/data-structures/heap/heap.js +19 -20
- package/dist/mjs/data-structures/heap/max-heap.js +1 -1
- package/dist/mjs/data-structures/heap/min-heap.js +1 -1
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +1 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +2 -5
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -8
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +16 -23
- package/dist/mjs/data-structures/matrix/matrix.d.ts +2 -7
- package/dist/mjs/data-structures/matrix/matrix.js +0 -7
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +1 -1
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +1 -1
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +1 -1
- package/dist/mjs/data-structures/queue/deque.d.ts +2 -11
- package/dist/mjs/data-structures/queue/deque.js +9 -14
- package/dist/mjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/mjs/data-structures/queue/queue.js +30 -28
- package/dist/mjs/data-structures/stack/stack.js +3 -5
- package/dist/mjs/data-structures/trie/trie.d.ts +2 -2
- package/dist/mjs/data-structures/trie/trie.js +10 -9
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +5 -2
- package/dist/mjs/types/data-structures/hash/index.d.ts +0 -1
- package/dist/mjs/types/data-structures/hash/index.js +0 -1
- package/dist/mjs/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/mjs/types/data-structures/linked-list/index.d.ts +1 -0
- package/dist/mjs/types/data-structures/linked-list/index.js +1 -0
- package/dist/mjs/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
- package/dist/mjs/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/mjs/types/data-structures/matrix/index.js +1 -0
- package/dist/mjs/types/data-structures/matrix/matrix.d.ts +7 -1
- package/dist/mjs/types/data-structures/queue/deque.d.ts +3 -1
- package/dist/mjs/types/data-structures/trie/trie.d.ts +3 -1
- package/dist/umd/data-structure-typed.js +151 -432
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +4 -4
- package/src/data-structures/binary-tree/binary-tree.ts +10 -14
- package/src/data-structures/binary-tree/bst.ts +5 -7
- package/src/data-structures/binary-tree/rb-tree.ts +4 -5
- package/src/data-structures/binary-tree/tree-multimap.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +46 -50
- package/src/data-structures/hash/index.ts +0 -1
- package/src/data-structures/heap/heap.ts +20 -19
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -5
- package/src/data-structures/linked-list/skip-linked-list.ts +15 -16
- package/src/data-structures/matrix/matrix.ts +2 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +11 -15
- package/src/data-structures/queue/queue.ts +29 -28
- package/src/data-structures/stack/stack.ts +3 -6
- package/src/data-structures/trie/trie.ts +10 -11
- package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +6 -2
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/heap/heap.ts +1 -1
- package/src/types/data-structures/linked-list/index.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -1
- package/src/types/data-structures/matrix/index.ts +1 -0
- package/src/types/data-structures/matrix/matrix.ts +7 -1
- package/src/types/data-structures/queue/deque.ts +1 -1
- package/src/types/data-structures/trie/trie.ts +1 -1
- package/test/integration/index.html +4 -4
- package/test/unit/data-structures/binary-tree/overall.test.ts +180 -1
- package/test/unit/data-structures/hash/hash-map.test.ts +19 -6
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +5 -5
- package/test/unit/data-structures/trie/trie.test.ts +1 -1
- package/dist/cjs/data-structures/hash/hash-table.d.ts +0 -108
- package/dist/cjs/data-structures/hash/hash-table.js +0 -282
- package/dist/cjs/data-structures/hash/hash-table.js.map +0 -1
- package/dist/cjs/types/data-structures/hash/hash-table.d.ts +0 -1
- package/dist/cjs/types/data-structures/hash/hash-table.js +0 -3
- package/dist/cjs/types/data-structures/hash/hash-table.js.map +0 -1
- package/dist/cjs/types/data-structures/matrix/matrix2d.d.ts +0 -1
- package/dist/cjs/types/data-structures/matrix/matrix2d.js +0 -3
- package/dist/cjs/types/data-structures/matrix/matrix2d.js.map +0 -1
- package/dist/cjs/types/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/cjs/types/data-structures/matrix/vector2d.js +0 -3
- package/dist/cjs/types/data-structures/matrix/vector2d.js.map +0 -1
- package/dist/mjs/data-structures/hash/hash-table.d.ts +0 -108
- package/dist/mjs/data-structures/hash/hash-table.js +0 -283
- package/dist/mjs/types/data-structures/hash/hash-table.d.ts +0 -1
- package/dist/mjs/types/data-structures/hash/hash-table.js +0 -1
- package/dist/mjs/types/data-structures/matrix/matrix2d.d.ts +0 -1
- package/dist/mjs/types/data-structures/matrix/matrix2d.js +0 -1
- package/dist/mjs/types/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/mjs/types/data-structures/matrix/vector2d.js +0 -1
- package/src/data-structures/hash/hash-table.ts +0 -318
- package/src/types/data-structures/hash/hash-table.ts +0 -1
- package/src/types/data-structures/matrix/matrix2d.ts +0 -1
- package/src/types/data-structures/matrix/vector2d.ts +0 -1
- package/test/unit/data-structures/hash/hash-table.test.ts +0 -238
|
@@ -56,23 +56,22 @@ export class BSTNode extends BinaryTreeNode {
|
|
|
56
56
|
export class BST extends BinaryTree {
|
|
57
57
|
/**
|
|
58
58
|
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
59
|
-
* the tree with optional
|
|
60
|
-
* @param [
|
|
59
|
+
* the tree with optional keysOrNodesOrEntries and options.
|
|
60
|
+
* @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the
|
|
61
61
|
* binary search tree.
|
|
62
62
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
63
63
|
* configuration options for the binary search tree. It can have the following properties:
|
|
64
64
|
*/
|
|
65
|
-
constructor(
|
|
65
|
+
constructor(keysOrNodesOrEntries = [], options) {
|
|
66
66
|
super([], options);
|
|
67
67
|
if (options) {
|
|
68
68
|
const { variant } = options;
|
|
69
|
-
if (variant)
|
|
69
|
+
if (variant)
|
|
70
70
|
this._variant = variant;
|
|
71
|
-
}
|
|
72
71
|
}
|
|
73
72
|
this._root = undefined;
|
|
74
|
-
if (
|
|
75
|
-
this.addMany(
|
|
73
|
+
if (keysOrNodesOrEntries)
|
|
74
|
+
this.addMany(keysOrNodesOrEntries);
|
|
76
75
|
}
|
|
77
76
|
_root;
|
|
78
77
|
get root() {
|
|
@@ -24,7 +24,7 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
24
24
|
/**
|
|
25
25
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
26
26
|
* initializes the tree with optional nodes and options.
|
|
27
|
-
* @param [
|
|
27
|
+
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
|
|
28
28
|
* objects. It represents the initial nodes that will be added to the RBTree during its
|
|
29
29
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
30
30
|
* nodes to the
|
|
@@ -32,7 +32,7 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
32
32
|
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
33
33
|
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
34
34
|
*/
|
|
35
|
-
constructor(
|
|
35
|
+
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: RBTreeOptions<K>);
|
|
36
36
|
protected _root: N;
|
|
37
37
|
get root(): N;
|
|
38
38
|
protected _size: number;
|
|
@@ -26,7 +26,7 @@ export class RedBlackTree extends BST {
|
|
|
26
26
|
/**
|
|
27
27
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
28
28
|
* initializes the tree with optional nodes and options.
|
|
29
|
-
* @param [
|
|
29
|
+
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
|
|
30
30
|
* objects. It represents the initial nodes that will be added to the RBTree during its
|
|
31
31
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
32
32
|
* nodes to the
|
|
@@ -34,11 +34,11 @@ export class RedBlackTree extends BST {
|
|
|
34
34
|
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
35
35
|
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
36
36
|
*/
|
|
37
|
-
constructor(
|
|
37
|
+
constructor(keysOrNodesOrEntries = [], options) {
|
|
38
38
|
super([], options);
|
|
39
39
|
this._root = this.Sentinel;
|
|
40
|
-
if (
|
|
41
|
-
super.addMany(
|
|
40
|
+
if (keysOrNodesOrEntries)
|
|
41
|
+
super.addMany(keysOrNodesOrEntries);
|
|
42
42
|
}
|
|
43
43
|
_root;
|
|
44
44
|
get root() {
|
|
@@ -73,7 +73,6 @@ export class RedBlackTree extends BST {
|
|
|
73
73
|
createTree(options) {
|
|
74
74
|
return new RedBlackTree([], {
|
|
75
75
|
iterationType: this.iterationType,
|
|
76
|
-
variant: this.variant,
|
|
77
76
|
...options
|
|
78
77
|
});
|
|
79
78
|
}
|
|
@@ -275,9 +274,9 @@ export class RedBlackTree extends BST {
|
|
|
275
274
|
this._fixDelete(x);
|
|
276
275
|
}
|
|
277
276
|
this._size--;
|
|
277
|
+
ans.push({ deleted: z, needBalanced: undefined });
|
|
278
278
|
};
|
|
279
279
|
helper(this.root);
|
|
280
|
-
// TODO
|
|
281
280
|
return ans;
|
|
282
281
|
}
|
|
283
282
|
/**
|
|
@@ -27,7 +27,7 @@ export declare class TreeMultimapNode<K = any, V = any, N extends TreeMultimapNo
|
|
|
27
27
|
* The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
28
28
|
*/
|
|
29
29
|
export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>, TREE extends TreeMultimap<K, V, N, TREE> = TreeMultimap<K, V, N, TreeMultimapNested<K, V, N>>> extends AVLTree<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
|
|
30
|
-
constructor(
|
|
30
|
+
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: TreeMultimapOptions<K>);
|
|
31
31
|
private _count;
|
|
32
32
|
get count(): number;
|
|
33
33
|
/**
|
|
@@ -21,10 +21,10 @@ export class TreeMultimapNode extends AVLTreeNode {
|
|
|
21
21
|
* The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
22
22
|
*/
|
|
23
23
|
export class TreeMultimap extends AVLTree {
|
|
24
|
-
constructor(
|
|
24
|
+
constructor(keysOrNodesOrEntries = [], options) {
|
|
25
25
|
super([], options);
|
|
26
|
-
if (
|
|
27
|
-
this.addMany(
|
|
26
|
+
if (keysOrNodesOrEntries)
|
|
27
|
+
this.addMany(keysOrNodesOrEntries);
|
|
28
28
|
}
|
|
29
29
|
_count = 0;
|
|
30
30
|
// TODO the _count is not accurate after nodes count modified
|
|
@@ -5,13 +5,13 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
|
|
8
|
+
import type { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem, LinkedHashMapOptions } from '../../types';
|
|
9
9
|
import { IterableEntryBase } from '../base';
|
|
10
10
|
/**
|
|
11
11
|
* 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value.
|
|
12
|
-
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete
|
|
12
|
+
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete entries based on a key.
|
|
13
13
|
* 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one.
|
|
14
|
-
* 4. Unordered Collection: HashMap does not guarantee the order of
|
|
14
|
+
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
|
|
15
15
|
*/
|
|
16
16
|
export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
17
17
|
protected _store: {
|
|
@@ -19,16 +19,14 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
19
19
|
};
|
|
20
20
|
protected _objMap: Map<object, V>;
|
|
21
21
|
/**
|
|
22
|
-
* The constructor function initializes a new instance of a class with optional
|
|
23
|
-
* @param
|
|
22
|
+
* The constructor function initializes a new instance of a class with optional entries and options.
|
|
23
|
+
* @param entries - The `entries` parameter is an iterable containing key-value pairs `[K, V]`. It
|
|
24
24
|
* is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with
|
|
25
25
|
* key-value pairs.
|
|
26
26
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
27
27
|
* configuration options for the constructor. In this case, it has one property:
|
|
28
28
|
*/
|
|
29
|
-
constructor(
|
|
30
|
-
hashFn: (key: K) => string;
|
|
31
|
-
});
|
|
29
|
+
constructor(entries?: Iterable<[K, V]>, options?: HashMapOptions<K>);
|
|
32
30
|
protected _size: number;
|
|
33
31
|
get size(): number;
|
|
34
32
|
isEmpty(): boolean;
|
|
@@ -45,10 +43,10 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
45
43
|
set(key: K, value: V): boolean;
|
|
46
44
|
/**
|
|
47
45
|
* The function "setMany" sets multiple key-value pairs in a map.
|
|
48
|
-
* @param
|
|
49
|
-
* key-value pair is represented as an array with two
|
|
46
|
+
* @param entries - The `entries` parameter is an iterable containing key-value pairs. Each
|
|
47
|
+
* key-value pair is represented as an array with two entries: the key and the value.
|
|
50
48
|
*/
|
|
51
|
-
setMany(
|
|
49
|
+
setMany(entries: Iterable<[K, V]>): boolean[];
|
|
52
50
|
/**
|
|
53
51
|
* The `get` function retrieves a value from a map based on a given key, either from an object map or
|
|
54
52
|
* a string map.
|
|
@@ -113,7 +111,6 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
113
111
|
* from the original `HashMap` that pass the provided `predicate` function.
|
|
114
112
|
*/
|
|
115
113
|
filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): HashMap<K, V>;
|
|
116
|
-
print(): void;
|
|
117
114
|
put(key: K, value: V): boolean;
|
|
118
115
|
/**
|
|
119
116
|
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
@@ -125,8 +122,8 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
125
122
|
protected _getNoObjKey(key: K): string;
|
|
126
123
|
}
|
|
127
124
|
/**
|
|
128
|
-
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which
|
|
129
|
-
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of
|
|
125
|
+
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which entries are inserted. Therefore, when you traverse it, entries will be returned in the order they were inserted into the map.
|
|
126
|
+
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of entries through the linked list.
|
|
130
127
|
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
|
|
131
128
|
*/
|
|
132
129
|
export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
@@ -135,9 +132,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
135
132
|
protected _head: HashMapLinkedNode<K, V | undefined>;
|
|
136
133
|
protected _tail: HashMapLinkedNode<K, V | undefined>;
|
|
137
134
|
protected readonly _sentinel: HashMapLinkedNode<K, V | undefined>;
|
|
138
|
-
|
|
139
|
-
protected _objHashFn: (key: K) => object;
|
|
140
|
-
constructor(elements?: Iterable<[K, V]>, options?: HashMapOptions<K>);
|
|
135
|
+
constructor(entries?: Iterable<[K, V]>, options?: LinkedHashMapOptions<K>);
|
|
141
136
|
protected _size: number;
|
|
142
137
|
get size(): number;
|
|
143
138
|
/**
|
|
@@ -242,14 +237,10 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
242
237
|
* Time Complexity: O(1)
|
|
243
238
|
* Space Complexity: O(1)
|
|
244
239
|
*
|
|
245
|
-
* The `clear` function clears all the
|
|
240
|
+
* The `clear` function clears all the entries in a data structure and resets its properties.
|
|
246
241
|
*/
|
|
247
242
|
clear(): void;
|
|
248
243
|
clone(): LinkedHashMap<K, V>;
|
|
249
|
-
/**
|
|
250
|
-
* Time Complexity: O(n)
|
|
251
|
-
* Space Complexity: O(n)
|
|
252
|
-
*/
|
|
253
244
|
/**
|
|
254
245
|
* Time Complexity: O(n)
|
|
255
246
|
* Space Complexity: O(n)
|
|
@@ -266,10 +257,6 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
266
257
|
* `LinkedHashMap` object that satisfy the given predicate function.
|
|
267
258
|
*/
|
|
268
259
|
filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): LinkedHashMap<K, V>;
|
|
269
|
-
/**
|
|
270
|
-
* Time Complexity: O(n)
|
|
271
|
-
* Space Complexity: O(n)
|
|
272
|
-
*/
|
|
273
260
|
/**
|
|
274
261
|
* Time Complexity: O(n)
|
|
275
262
|
* Space Complexity: O(n)
|
|
@@ -288,9 +275,19 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
288
275
|
* function.
|
|
289
276
|
*/
|
|
290
277
|
map<NV>(callback: EntryCallback<K, V, NV>, thisArg?: any): LinkedHashMap<K, NV>;
|
|
278
|
+
/**
|
|
279
|
+
* Time Complexity: O(n)
|
|
280
|
+
* Space Complexity: O(n)
|
|
281
|
+
*/
|
|
291
282
|
put(key: K, value: V): boolean;
|
|
292
283
|
/**
|
|
293
|
-
* Time Complexity: O(n)
|
|
284
|
+
* Time Complexity: O(n)
|
|
285
|
+
* Space Complexity: O(n)
|
|
286
|
+
*/
|
|
287
|
+
protected _hashFn: (key: K) => string;
|
|
288
|
+
protected _objHashFn: (key: K) => object;
|
|
289
|
+
/**
|
|
290
|
+
* Time Complexity: O(n), where n is the number of entries in the LinkedHashMap.
|
|
294
291
|
* Space Complexity: O(1)
|
|
295
292
|
*
|
|
296
293
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
@@ -2,22 +2,22 @@ import { IterableEntryBase } from '../base';
|
|
|
2
2
|
import { isWeakKey, rangeCheck } from '../../utils';
|
|
3
3
|
/**
|
|
4
4
|
* 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value.
|
|
5
|
-
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete
|
|
5
|
+
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete entries based on a key.
|
|
6
6
|
* 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one.
|
|
7
|
-
* 4. Unordered Collection: HashMap does not guarantee the order of
|
|
7
|
+
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
|
|
8
8
|
*/
|
|
9
9
|
export class HashMap extends IterableEntryBase {
|
|
10
10
|
_store = {};
|
|
11
11
|
_objMap = new Map();
|
|
12
12
|
/**
|
|
13
|
-
* The constructor function initializes a new instance of a class with optional
|
|
14
|
-
* @param
|
|
13
|
+
* The constructor function initializes a new instance of a class with optional entries and options.
|
|
14
|
+
* @param entries - The `entries` parameter is an iterable containing key-value pairs `[K, V]`. It
|
|
15
15
|
* is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with
|
|
16
16
|
* key-value pairs.
|
|
17
17
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
18
18
|
* configuration options for the constructor. In this case, it has one property:
|
|
19
19
|
*/
|
|
20
|
-
constructor(
|
|
20
|
+
constructor(entries = [], options) {
|
|
21
21
|
super();
|
|
22
22
|
if (options) {
|
|
23
23
|
const { hashFn } = options;
|
|
@@ -25,8 +25,8 @@ export class HashMap extends IterableEntryBase {
|
|
|
25
25
|
this._hashFn = hashFn;
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
|
-
if (
|
|
29
|
-
this.setMany(
|
|
28
|
+
if (entries) {
|
|
29
|
+
this.setMany(entries);
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
_size = 0;
|
|
@@ -68,12 +68,12 @@ export class HashMap extends IterableEntryBase {
|
|
|
68
68
|
}
|
|
69
69
|
/**
|
|
70
70
|
* The function "setMany" sets multiple key-value pairs in a map.
|
|
71
|
-
* @param
|
|
72
|
-
* key-value pair is represented as an array with two
|
|
71
|
+
* @param entries - The `entries` parameter is an iterable containing key-value pairs. Each
|
|
72
|
+
* key-value pair is represented as an array with two entries: the key and the value.
|
|
73
73
|
*/
|
|
74
|
-
setMany(
|
|
74
|
+
setMany(entries) {
|
|
75
75
|
const results = [];
|
|
76
|
-
for (const [key, value] of
|
|
76
|
+
for (const [key, value] of entries)
|
|
77
77
|
results.push(this.set(key, value));
|
|
78
78
|
return results;
|
|
79
79
|
}
|
|
@@ -189,9 +189,6 @@ export class HashMap extends IterableEntryBase {
|
|
|
189
189
|
}
|
|
190
190
|
return filteredMap;
|
|
191
191
|
}
|
|
192
|
-
print() {
|
|
193
|
-
console.log([...this.entries()]);
|
|
194
|
-
}
|
|
195
192
|
put(key, value) {
|
|
196
193
|
return this.set(key, value);
|
|
197
194
|
}
|
|
@@ -231,8 +228,8 @@ export class HashMap extends IterableEntryBase {
|
|
|
231
228
|
}
|
|
232
229
|
}
|
|
233
230
|
/**
|
|
234
|
-
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which
|
|
235
|
-
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of
|
|
231
|
+
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which entries are inserted. Therefore, when you traverse it, entries will be returned in the order they were inserted into the map.
|
|
232
|
+
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of entries through the linked list.
|
|
236
233
|
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
|
|
237
234
|
*/
|
|
238
235
|
export class LinkedHashMap extends IterableEntryBase {
|
|
@@ -241,20 +238,19 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
241
238
|
_head;
|
|
242
239
|
_tail;
|
|
243
240
|
_sentinel;
|
|
244
|
-
|
|
245
|
-
_objHashFn;
|
|
246
|
-
constructor(elements, options = {
|
|
247
|
-
hashFn: (key) => String(key),
|
|
248
|
-
objHashFn: (key) => key
|
|
249
|
-
}) {
|
|
241
|
+
constructor(entries, options) {
|
|
250
242
|
super();
|
|
251
243
|
this._sentinel = {};
|
|
252
244
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
245
|
+
if (options) {
|
|
246
|
+
const { hashFn, objHashFn } = options;
|
|
247
|
+
if (hashFn)
|
|
248
|
+
this._hashFn = hashFn;
|
|
249
|
+
if (objHashFn)
|
|
250
|
+
this._objHashFn = objHashFn;
|
|
251
|
+
}
|
|
252
|
+
if (entries) {
|
|
253
|
+
for (const el of entries) {
|
|
258
254
|
this.set(el[0], el[1]);
|
|
259
255
|
}
|
|
260
256
|
}
|
|
@@ -495,7 +491,7 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
495
491
|
* Time Complexity: O(1)
|
|
496
492
|
* Space Complexity: O(1)
|
|
497
493
|
*
|
|
498
|
-
* The `clear` function clears all the
|
|
494
|
+
* The `clear` function clears all the entries in a data structure and resets its properties.
|
|
499
495
|
*/
|
|
500
496
|
clear() {
|
|
501
497
|
this._noObjMap = {};
|
|
@@ -510,10 +506,6 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
510
506
|
}
|
|
511
507
|
return cloned;
|
|
512
508
|
}
|
|
513
|
-
/**
|
|
514
|
-
* Time Complexity: O(n)
|
|
515
|
-
* Space Complexity: O(n)
|
|
516
|
-
*/
|
|
517
509
|
/**
|
|
518
510
|
* Time Complexity: O(n)
|
|
519
511
|
* Space Complexity: O(n)
|
|
@@ -540,10 +532,6 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
540
532
|
}
|
|
541
533
|
return filteredMap;
|
|
542
534
|
}
|
|
543
|
-
/**
|
|
544
|
-
* Time Complexity: O(n)
|
|
545
|
-
* Space Complexity: O(n)
|
|
546
|
-
*/
|
|
547
535
|
/**
|
|
548
536
|
* Time Complexity: O(n)
|
|
549
537
|
* Space Complexity: O(n)
|
|
@@ -571,11 +559,21 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
571
559
|
}
|
|
572
560
|
return mappedMap;
|
|
573
561
|
}
|
|
562
|
+
/**
|
|
563
|
+
* Time Complexity: O(n)
|
|
564
|
+
* Space Complexity: O(n)
|
|
565
|
+
*/
|
|
574
566
|
put(key, value) {
|
|
575
567
|
return this.set(key, value);
|
|
576
568
|
}
|
|
577
569
|
/**
|
|
578
|
-
* Time Complexity: O(n)
|
|
570
|
+
* Time Complexity: O(n)
|
|
571
|
+
* Space Complexity: O(n)
|
|
572
|
+
*/
|
|
573
|
+
_hashFn = (key) => String(key);
|
|
574
|
+
_objHashFn = (key) => key;
|
|
575
|
+
/**
|
|
576
|
+
* Time Complexity: O(n), where n is the number of entries in the LinkedHashMap.
|
|
579
577
|
* Space Complexity: O(1)
|
|
580
578
|
*
|
|
581
579
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
@@ -19,8 +19,9 @@ import { IterableElementBase } from '../base';
|
|
|
19
19
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
20
20
|
*/
|
|
21
21
|
export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
22
|
-
options: HeapOptions<E>;
|
|
23
22
|
constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
|
|
23
|
+
protected _comparator: (a: E, b: E) => number;
|
|
24
|
+
get comparator(): (a: E, b: E) => number;
|
|
24
25
|
protected _elements: E[];
|
|
25
26
|
get elements(): E[];
|
|
26
27
|
/**
|
|
@@ -18,24 +18,12 @@ import { IterableElementBase } from '../base';
|
|
|
18
18
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
19
19
|
*/
|
|
20
20
|
export class Heap extends IterableElementBase {
|
|
21
|
-
options
|
|
22
|
-
constructor(elements, options) {
|
|
21
|
+
constructor(elements = [], options) {
|
|
23
22
|
super();
|
|
24
|
-
const defaultComparator = (a, b) => {
|
|
25
|
-
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
26
|
-
throw new Error('The a, b params of compare function must be number');
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
return a - b;
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
23
|
if (options) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
this.options = {
|
|
37
|
-
comparator: defaultComparator
|
|
38
|
-
};
|
|
24
|
+
const { comparator } = options;
|
|
25
|
+
if (comparator)
|
|
26
|
+
this._comparator = comparator;
|
|
39
27
|
}
|
|
40
28
|
if (elements) {
|
|
41
29
|
for (const el of elements) {
|
|
@@ -44,6 +32,17 @@ export class Heap extends IterableElementBase {
|
|
|
44
32
|
// this.fix();
|
|
45
33
|
}
|
|
46
34
|
}
|
|
35
|
+
_comparator = (a, b) => {
|
|
36
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
37
|
+
throw new Error('The a, b params of compare function must be number');
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
return a - b;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
get comparator() {
|
|
44
|
+
return this._comparator;
|
|
45
|
+
}
|
|
47
46
|
_elements = [];
|
|
48
47
|
get elements() {
|
|
49
48
|
return this._elements;
|
|
@@ -253,7 +252,7 @@ export class Heap extends IterableElementBase {
|
|
|
253
252
|
* @returns A new Heap instance containing the same elements.
|
|
254
253
|
*/
|
|
255
254
|
clone() {
|
|
256
|
-
const clonedHeap = new Heap([], this.
|
|
255
|
+
const clonedHeap = new Heap([], { comparator: this.comparator });
|
|
257
256
|
clonedHeap._elements = [...this.elements];
|
|
258
257
|
return clonedHeap;
|
|
259
258
|
}
|
|
@@ -379,7 +378,7 @@ export class Heap extends IterableElementBase {
|
|
|
379
378
|
while (index > 0) {
|
|
380
379
|
const parent = (index - 1) >> 1;
|
|
381
380
|
const parentItem = this.elements[parent];
|
|
382
|
-
if (this.
|
|
381
|
+
if (this.comparator(parentItem, element) <= 0)
|
|
383
382
|
break;
|
|
384
383
|
this.elements[index] = parentItem;
|
|
385
384
|
index = parent;
|
|
@@ -401,11 +400,11 @@ export class Heap extends IterableElementBase {
|
|
|
401
400
|
let left = (index << 1) | 1;
|
|
402
401
|
const right = left + 1;
|
|
403
402
|
let minItem = this.elements[left];
|
|
404
|
-
if (right < this.elements.length && this.
|
|
403
|
+
if (right < this.elements.length && this.comparator(minItem, this.elements[right]) > 0) {
|
|
405
404
|
left = right;
|
|
406
405
|
minItem = this.elements[right];
|
|
407
406
|
}
|
|
408
|
-
if (this.
|
|
407
|
+
if (this.comparator(minItem, element) >= 0)
|
|
409
408
|
break;
|
|
410
409
|
this.elements[index] = minItem;
|
|
411
410
|
index = left;
|
|
@@ -10,7 +10,7 @@ import { Heap } from './heap';
|
|
|
10
10
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
11
11
|
*/
|
|
12
12
|
export class MaxHeap extends Heap {
|
|
13
|
-
constructor(elements, options = {
|
|
13
|
+
constructor(elements = [], options = {
|
|
14
14
|
comparator: (a, b) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -10,7 +10,7 @@ import { Heap } from './heap';
|
|
|
10
10
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
11
11
|
*/
|
|
12
12
|
export class MinHeap extends Heap {
|
|
13
|
-
constructor(elements, options = {
|
|
13
|
+
constructor(elements = [], options = {
|
|
14
14
|
comparator: (a, b) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -24,7 +24,7 @@ export class DoublyLinkedList extends IterableElementBase {
|
|
|
24
24
|
/**
|
|
25
25
|
* The constructor initializes the linked list with an empty head, tail, and size.
|
|
26
26
|
*/
|
|
27
|
-
constructor(elements) {
|
|
27
|
+
constructor(elements = []) {
|
|
28
28
|
super();
|
|
29
29
|
this._head = undefined;
|
|
30
30
|
this._tail = undefined;
|
|
@@ -16,11 +16,8 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
16
16
|
/**
|
|
17
17
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
18
18
|
*/
|
|
19
|
-
constructor(elements) {
|
|
19
|
+
constructor(elements = []) {
|
|
20
20
|
super();
|
|
21
|
-
this._head = undefined;
|
|
22
|
-
this._tail = undefined;
|
|
23
|
-
this._size = 0;
|
|
24
21
|
if (elements) {
|
|
25
22
|
for (const el of elements)
|
|
26
23
|
this.push(el);
|
|
@@ -34,7 +31,7 @@ export class SinglyLinkedList extends IterableElementBase {
|
|
|
34
31
|
get tail() {
|
|
35
32
|
return this._tail;
|
|
36
33
|
}
|
|
37
|
-
_size;
|
|
34
|
+
_size = 0;
|
|
38
35
|
get size() {
|
|
39
36
|
return this._size;
|
|
40
37
|
}
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
+
import type { SkipLinkedListOptions } from '../../types';
|
|
8
9
|
export declare class SkipListNode<K, V> {
|
|
9
10
|
key: K;
|
|
10
11
|
value: V;
|
|
@@ -12,14 +13,7 @@ export declare class SkipListNode<K, V> {
|
|
|
12
13
|
constructor(key: K, value: V, level: number);
|
|
13
14
|
}
|
|
14
15
|
export declare class SkipList<K, V> {
|
|
15
|
-
|
|
16
|
-
* The constructor initializes a SkipList with a specified maximum level and probability.
|
|
17
|
-
* @param [maxLevel=16] - The `maxLevel` parameter represents the maximum level that a skip list can have. It determines
|
|
18
|
-
* the maximum number of levels that can be created in the skip list.
|
|
19
|
-
* @param [probability=0.5] - The probability parameter represents the probability of a node being promoted to a higher
|
|
20
|
-
* level in the skip list. It is used to determine the height of each node in the skip list.
|
|
21
|
-
*/
|
|
22
|
-
constructor(maxLevel?: number, probability?: number);
|
|
16
|
+
constructor(elements?: Iterable<[K, V]>, options?: SkipLinkedListOptions);
|
|
23
17
|
protected _head: SkipListNode<K, V>;
|
|
24
18
|
get head(): SkipListNode<K, V>;
|
|
25
19
|
protected _level: number;
|
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Tyler Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
1
|
export class SkipListNode {
|
|
9
2
|
key;
|
|
10
3
|
value;
|
|
@@ -16,32 +9,32 @@ export class SkipListNode {
|
|
|
16
9
|
}
|
|
17
10
|
}
|
|
18
11
|
export class SkipList {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
12
|
+
constructor(elements = [], options) {
|
|
13
|
+
if (options) {
|
|
14
|
+
const { maxLevel, probability } = options;
|
|
15
|
+
if (typeof maxLevel === 'number')
|
|
16
|
+
this._maxLevel = maxLevel;
|
|
17
|
+
if (typeof probability === 'number')
|
|
18
|
+
this._probability = probability;
|
|
19
|
+
}
|
|
20
|
+
if (elements) {
|
|
21
|
+
for (const [key, value] of elements)
|
|
22
|
+
this.add(key, value);
|
|
23
|
+
}
|
|
31
24
|
}
|
|
32
|
-
_head;
|
|
25
|
+
_head = new SkipListNode(undefined, undefined, this.maxLevel);
|
|
33
26
|
get head() {
|
|
34
27
|
return this._head;
|
|
35
28
|
}
|
|
36
|
-
_level;
|
|
29
|
+
_level = 0;
|
|
37
30
|
get level() {
|
|
38
31
|
return this._level;
|
|
39
32
|
}
|
|
40
|
-
_maxLevel;
|
|
33
|
+
_maxLevel = 16;
|
|
41
34
|
get maxLevel() {
|
|
42
35
|
return this._maxLevel;
|
|
43
36
|
}
|
|
44
|
-
_probability;
|
|
37
|
+
_probability = 0.5;
|
|
45
38
|
get probability() {
|
|
46
39
|
return this._probability;
|
|
47
40
|
}
|