6
6
* @license MIT License
7
7
*/
8
8
import type {
9
- AVLTreeMultiMapNested ,
10
9
AVLTreeMultiMapNodeNested ,
11
10
AVLTreeMultiMapOptions ,
12
11
BinaryTreeDeleteResult ,
13
12
BSTNOptKeyOrNode ,
14
13
BTNRep ,
14
+ EntryCallback ,
15
15
IterationType
16
16
} from '../../types' ;
17
17
import { IBinaryTree } from '../../interfaces' ;
@@ -64,17 +64,10 @@ export class AVLTreeMultiMap<
64
64
K = any ,
65
65
V = any ,
66
66
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 > >
75
68
>
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 >
78
71
{
79
72
/**
80
73
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
@@ -139,15 +132,16 @@ export class AVLTreeMultiMap<
139
132
* @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE
140
133
* object.
141
134
*/
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 > ( [ ] , {
144
138
iterationType : this . iterationType ,
145
139
isMapMode : this . _isMapMode ,
146
- extractComparable : this . _extractComparable ,
140
+ specifyComparable : this . _specifyComparable ,
147
141
toEntryFn : this . _toEntryFn ,
148
142
isReverse : this . _isReverse ,
149
143
...options
150
- } ) as TREE ;
144
+ } ) ;
151
145
}
152
146
153
147
/**
@@ -161,44 +155,6 @@ export class AVLTreeMultiMap<
161
155
return keyNodeEntryOrRaw instanceof AVLTreeMultiMapNode ;
162
156
}
163
157
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
-
202
158
/**
203
159
* Time Complexity: O(log n)
204
160
* Space Complexity: O(1)
@@ -374,14 +330,85 @@ export class AVLTreeMultiMap<
374
330
* The function overrides the clone method to create a deep copy of a tree object.
375
331
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
376
332
*/
377
- override clone ( ) : TREE {
333
+ // @ts -ignore
334
+ override clone ( ) {
378
335
const cloned = this . createTree ( ) ;
379
336
if ( this . _isMapMode ) this . bfs ( node => cloned . add ( node . key , undefined , node . count ) ) ;
380
337
else this . bfs ( node => cloned . add ( node . key , node . value , node . count ) ) ;
381
338
if ( this . _isMapMode ) cloned . _store = this . _store ;
382
339
return cloned ;
383
340
}
384
341
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
+
385
412
/**
386
413
* Time Complexity: O(1)
387
414
* Space Complexity: O(1)
0 commit comments