Skip to content

Commit 1a1ea21

Browse files
committed
fix: Adjust type safety for the map method of all binary trees, ensuring it no longer returns the TREE type to its parent class and limiting Node type nesting to a maximum of 10 levels. Fix the issue with RedBlackTree objects being incorrectly compared when used as keys. Rename extractComparable to specificComparable. Correct the order of key and value in callback functions for the map and filter methods.
1 parent 1494ffa commit 1a1ea21

32 files changed

+842
-491
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file.
88
- [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
99
- [`auto-changelog`](https://github.com/CookPete/auto-changelog)
1010

11-
## [v1.53.7](https://github.com/zrwusa/data-structure-typed/compare/v1.51.5...main) (upcoming)
11+
## [v1.53.8](https://github.com/zrwusa/data-structure-typed/compare/v1.51.5...main) (upcoming)
1212

1313
### Changes
1414

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686

8787
- Make your pull requests to be **specific** and **focused**. Instead of
8888
contributing "several data structures" all at once contribute them all
89-
one by one separately (i.e. one pull request for "RBTree", another one
89+
one by one separately (i.e. one pull request for "RedBlackTree", another one
9090
for "AATree" and so on).
9191
- Modify **README.md** for each of the data structure **with explanations** of
9292
the algorithm and **with links** to further readings.

package-lock.json

Lines changed: 26 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "data-structure-typed",
3-
"version": "1.53.8",
3+
"version": "1.53.9",
44
"description": "Javascript Data Structure. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
55
"main": "dist/cjs/index.js",
66
"module": "dist/mjs/index.js",
@@ -70,11 +70,11 @@
7070
"@typescript-eslint/eslint-plugin": "^8.12.1",
7171
"@typescript-eslint/parser": "^8.12.1",
7272
"auto-changelog": "^2.5.0",
73-
"avl-tree-typed": "^1.53.6",
73+
"avl-tree-typed": "^1.53.7",
7474
"benchmark": "^2.1.4",
75-
"binary-tree-typed": "^1.53.6",
76-
"bst-typed": "^1.53.6",
77-
"data-structure-typed": "^1.53.6",
75+
"binary-tree-typed": "^1.53.7",
76+
"bst-typed": "^1.53.7",
77+
"data-structure-typed": "^1.53.7",
7878
"dependency-cruiser": "^16.5.0",
7979
"doctoc": "^2.2.1",
8080
"eslint": "^9.13.0",
@@ -83,7 +83,7 @@
8383
"eslint-import-resolver-typescript": "^3.6.3",
8484
"eslint-plugin-import": "^2.31.0",
8585
"fast-glob": "^3.3.2",
86-
"heap-typed": "^1.53.6",
86+
"heap-typed": "^1.53.7",
8787
"istanbul-badges-readme": "^1.9.0",
8888
"jest": "^29.7.0",
8989
"js-sdsl": "^4.4.2",

src/data-structures/binary-tree/avl-tree-multi-map.ts

Lines changed: 81 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
* @license MIT License
77
*/
88
import type {
9-
AVLTreeMultiMapNested,
109
AVLTreeMultiMapNodeNested,
1110
AVLTreeMultiMapOptions,
1211
BinaryTreeDeleteResult,
1312
BSTNOptKeyOrNode,
1413
BTNRep,
14+
EntryCallback,
1515
IterationType
1616
} from '../../types';
1717
import { IBinaryTree } from '../../interfaces';
@@ -64,17 +64,10 @@ export class AVLTreeMultiMap<
6464
K = any,
6565
V = any,
6666
R = object,
67-
NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>,
68-
TREE extends AVLTreeMultiMap<K, V, R, NODE, TREE> = AVLTreeMultiMap<
69-
K,
70-
V,
71-
R,
72-
NODE,
73-
AVLTreeMultiMapNested<K, V, R, NODE>
74-
>
67+
NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>
7568
>
76-
extends AVLTree<K, V, R, NODE, TREE>
77-
implements IBinaryTree<K, V, R, NODE, TREE>
69+
extends AVLTree<K, V, R, NODE>
70+
implements IBinaryTree<K, V, R, NODE>
7871
{
7972
/**
8073
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
@@ -139,15 +132,16 @@ export class AVLTreeMultiMap<
139132
* @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE
140133
* object.
141134
*/
142-
override createTree(options?: AVLTreeMultiMapOptions<K, V, R>): TREE {
143-
return new AVLTreeMultiMap<K, V, R, NODE, TREE>([], {
135+
// @ts-ignore
136+
override createTree(options?: AVLTreeMultiMapOptions<K, V, R>) {
137+
return new AVLTreeMultiMap<K, V, R, NODE>([], {
144138
iterationType: this.iterationType,
145139
isMapMode: this._isMapMode,
146-
extractComparable: this._extractComparable,
140+
specifyComparable: this._specifyComparable,
147141
toEntryFn: this._toEntryFn,
148142
isReverse: this._isReverse,
149143
...options
150-
}) as TREE;
144+
});
151145
}
152146

153147
/**
@@ -161,44 +155,6 @@ export class AVLTreeMultiMap<
161155
return keyNodeEntryOrRaw instanceof AVLTreeMultiMapNode;
162156
}
163157

164-
/**
165-
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
166-
* a node object.
167-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
168-
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
169-
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
170-
* `override` function. It represents the value associated with the key in the data structure. If no
171-
* value is provided, it will default to `undefined`.
172-
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
173-
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
174-
* @returns either a NODE object or undefined.
175-
*/
176-
protected override _keyValueNodeEntryRawToNodeAndValue(
177-
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
178-
value?: V,
179-
count = 1
180-
): [NODE | undefined, V | undefined] {
181-
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined];
182-
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
183-
184-
if (this.isEntry(keyNodeEntryOrRaw)) {
185-
const [key, entryValue] = keyNodeEntryOrRaw;
186-
if (key === undefined || key === null) return [undefined, undefined];
187-
const finalValue = value ?? entryValue;
188-
return [this.createNode(key, finalValue, count), finalValue];
189-
}
190-
191-
if (this.isRaw(keyNodeEntryOrRaw)) {
192-
const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
193-
const finalValue = value ?? entryValue;
194-
if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
195-
}
196-
197-
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
198-
199-
return [undefined, undefined];
200-
}
201-
202158
/**
203159
* Time Complexity: O(log n)
204160
* Space Complexity: O(1)
@@ -374,14 +330,85 @@ export class AVLTreeMultiMap<
374330
* The function overrides the clone method to create a deep copy of a tree object.
375331
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
376332
*/
377-
override clone(): TREE {
333+
// @ts-ignore
334+
override clone() {
378335
const cloned = this.createTree();
379336
if (this._isMapMode) this.bfs(node => cloned.add(node.key, undefined, node.count));
380337
else this.bfs(node => cloned.add(node.key, node.value, node.count));
381338
if (this._isMapMode) cloned._store = this._store;
382339
return cloned;
383340
}
384341

342+
/**
343+
* The `map` function in TypeScript overrides the default behavior to create a new AVLTreeMultiMap
344+
* with modified entries based on a provided callback.
345+
* @param callback - The `callback` parameter is a function that will be called for each entry in the
346+
* AVLTreeMultiMap. It takes four arguments:
347+
* @param [options] - The `options` parameter in the `override map` function is of type
348+
* `AVLTreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional
349+
* configuration options when creating a new `AVLTreeMultiMap` instance within the `map` function.
350+
* These options
351+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
352+
* the value of `this` when executing the `callback` function. It allows you to set the context
353+
* (value of `this`) for the callback function. This can be useful when you want to access properties
354+
* or
355+
* @returns The `map` method is returning a new `AVLTreeMultiMap` instance with the entries
356+
* transformed by the provided `callback` function. Each entry in the original tree is passed to the
357+
* `callback` function along with the index and the original tree itself. The transformed entries are
358+
* then added to the new `AVLTreeMultiMap` instance, which is returned at the end.
359+
*/
360+
// @ts-ignore
361+
override map<MK, MV, MR>(
362+
callback: EntryCallback<K, V | undefined, [MK, MV]>,
363+
options?: AVLTreeMultiMapOptions<MK, MV, MR>,
364+
thisArg?: any
365+
) {
366+
const newTree = new AVLTreeMultiMap<MK, MV, MR>([], options);
367+
let index = 0;
368+
for (const [key, value] of this) {
369+
newTree.add(callback.call(thisArg, key, value, index++, this));
370+
}
371+
return newTree;
372+
}
373+
374+
/**
375+
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
376+
* a node object.
377+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
378+
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
379+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
380+
* `override` function. It represents the value associated with the key in the data structure. If no
381+
* value is provided, it will default to `undefined`.
382+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
383+
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
384+
* @returns either a NODE object or undefined.
385+
*/
386+
protected override _keyValueNodeEntryRawToNodeAndValue(
387+
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
388+
value?: V,
389+
count = 1
390+
): [NODE | undefined, V | undefined] {
391+
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined];
392+
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
393+
394+
if (this.isEntry(keyNodeEntryOrRaw)) {
395+
const [key, entryValue] = keyNodeEntryOrRaw;
396+
if (key === undefined || key === null) return [undefined, undefined];
397+
const finalValue = value ?? entryValue;
398+
return [this.createNode(key, finalValue, count), finalValue];
399+
}
400+
401+
if (this.isRaw(keyNodeEntryOrRaw)) {
402+
const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
403+
const finalValue = value ?? entryValue;
404+
if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
405+
}
406+
407+
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
408+
409+
return [undefined, undefined];
410+
}
411+
385412
/**
386413
* Time Complexity: O(1)
387414
* Space Complexity: O(1)

0 commit comments

Comments
 (0)