1616/**
1717 * Stores tuples of arbitrary length while preserving efficient prefix lookups
1818 * through a tree of maps that ends in a set of final values.
19- * @template T
19+ * @template {EXPECTED_ANY[]} K
2020 * @template V
2121 */
2222class TupleSet {
2323 /**
2424 * Seeds the tuple set with an optional iterable of tuples.
25- * @param {Iterable<[T, V, ...EXPECTED_ANY ]>= } init init
25+ * @param {Iterable<[...K, V ]>= } init init
2626 */
2727 constructor ( init ) {
28- /** @type {InnerMap<T , V> } */
28+ /** @type {InnerMap<K[number] , V> } */
2929 this . _map = new Map ( ) ;
3030 /** @type {number } */
3131 this . size = 0 ;
@@ -38,80 +38,80 @@ class TupleSet {
3838
3939 /**
4040 * Adds a tuple to the set, creating any missing prefix maps along the way.
41- * @param {[T, V, ...EXPECTED_ANY ] } args tuple
41+ * @param {[...K, V ] } args tuple
4242 * @returns {void }
4343 */
4444 add ( ...args ) {
4545 let map = this . _map ;
4646 for ( let i = 0 ; i < args . length - 2 ; i ++ ) {
47- const arg = args [ i ] ;
47+ const arg = /** @type { K[number] } */ ( args [ i ] ) ;
4848 const innerMap = map . get ( arg ) ;
4949 if ( innerMap === undefined ) {
5050 map . set ( arg , ( map = new Map ( ) ) ) ;
5151 } else {
52- map = /** @type {InnerMap<T , V> } */ ( innerMap ) ;
52+ map = /** @type {InnerMap<K[number] , V> } */ ( innerMap ) ;
5353 }
5454 }
5555
56- const beforeLast = args [ args . length - 2 ] ;
56+ const beforeLast = /** @type { K[number] } */ ( args [ args . length - 2 ] ) ;
5757 let set = /** @type {Set<V> } */ ( map . get ( beforeLast ) ) ;
5858 if ( set === undefined ) {
5959 map . set ( beforeLast , ( set = new Set ( ) ) ) ;
6060 }
6161
62- const last = args [ args . length - 1 ] ;
62+ const last = /** @type { V } */ ( args [ args . length - 1 ] ) ;
6363 this . size -= set . size ;
6464 set . add ( last ) ;
6565 this . size += set . size ;
6666 }
6767
6868 /**
6969 * Checks whether the exact tuple is already present in the set.
70- * @param {[T, V, ...EXPECTED_ANY ] } args tuple
70+ * @param {[...K, V ] } args tuple
7171 * @returns {boolean } true, if the tuple is in the Set
7272 */
7373 has ( ...args ) {
7474 let map = this . _map ;
7575 for ( let i = 0 ; i < args . length - 2 ; i ++ ) {
76- const arg = args [ i ] ;
77- map = /** @type {InnerMap<T , V> } */ ( map . get ( arg ) ) ;
76+ const arg = /** @type { K[number] } */ ( args [ i ] ) ;
77+ map = /** @type {InnerMap<K[number] , V> } */ ( map . get ( arg ) ) ;
7878 if ( map === undefined ) {
7979 return false ;
8080 }
8181 }
8282
83- const beforeLast = args [ args . length - 2 ] ;
84- const set = map . get ( beforeLast ) ;
83+ const beforeLast = /** @type { K[number] } */ ( args [ args . length - 2 ] ) ;
84+ const set = /** @type { Set<V> | undefined } */ ( map . get ( beforeLast ) ) ;
8585 if ( set === undefined ) {
8686 return false ;
8787 }
8888
89- const last = args [ args . length - 1 ] ;
89+ const last = /** @type { V } */ ( args [ args . length - 1 ] ) ;
9090 return set . has ( last ) ;
9191 }
9292
9393 /**
9494 * Removes a tuple from the set when it is present.
95- * @param {[T, V, ...EXPECTED_ANY ] } args tuple
95+ * @param {[...K, V ] } args tuple
9696 * @returns {void }
9797 */
9898 delete ( ...args ) {
9999 let map = this . _map ;
100100 for ( let i = 0 ; i < args . length - 2 ; i ++ ) {
101- const arg = args [ i ] ;
102- map = /** @type {InnerMap<T , V> } */ ( map . get ( arg ) ) ;
101+ const arg = /** @type { K[number] } */ ( args [ i ] ) ;
102+ map = /** @type {InnerMap<K[number] , V> } */ ( map . get ( arg ) ) ;
103103 if ( map === undefined ) {
104104 return ;
105105 }
106106 }
107107
108- const beforeLast = args [ args . length - 2 ] ;
109- const set = map . get ( beforeLast ) ;
108+ const beforeLast = /** @type { K[number] } */ ( args [ args . length - 2 ] ) ;
109+ const set = /** @type { Set<V> | undefined } */ ( map . get ( beforeLast ) ) ;
110110 if ( set === undefined ) {
111111 return ;
112112 }
113113
114- const last = args [ args . length - 1 ] ;
114+ const last = /** @type { V } */ ( args [ args . length - 1 ] ) ;
115115 this . size -= set . size ;
116116 set . delete ( last ) ;
117117 this . size += set . size ;
@@ -120,7 +120,7 @@ class TupleSet {
120120 /**
121121 * Iterates over every stored tuple by walking the nested map structure and
122122 * yielding each complete prefix plus its terminal set value.
123- * @returns {Iterator<[T, V, ...EXPECTED_ANY ]> } iterator
123+ * @returns {Iterator<[...K, V ]> } iterator
124124 */
125125 [ Symbol . iterator ] ( ) {
126126 /**
@@ -131,17 +131,17 @@ class TupleSet {
131131
132132 // This is difficult to type because we can have a map inside a map inside a map, etc. where the end is a set (each key is an argument)
133133 // But in basic use we only have 2 arguments in our methods, so we have `Map<K, Set<V>>`
134- /** @type {IteratorStack<T , V>[] } */
134+ /** @type {IteratorStack<K[number] , V>[] } */
135135 const iteratorStack = [ ] ;
136- /** @type {[T?, V?, ...EXPECTED_ANY ] } */
136+ /** @type {(K[number] | V)[ ] } */
137137 const tuple = [ ] ;
138138 /** @type {SetIterator<V> | undefined } */
139139 let currentSetIterator ;
140140
141141 /**
142142 * Advances through nested maps until a terminal value set is reached or
143143 * every remaining branch has been exhausted.
144- * @param {IteratorStack<T , V> } it iterator
144+ * @param {IteratorStack<K[number] , V> } it iterator
145145 * @returns {boolean } result
146146 */
147147 const next = ( it ) => {
@@ -150,7 +150,7 @@ class TupleSet {
150150 if ( iteratorStack . length === 0 ) return false ;
151151 tuple . pop ( ) ;
152152 return next (
153- /** @type {IteratorStack<T , V> } */
153+ /** @type {IteratorStack<K[number] , V> } */
154154 ( iteratorStack . pop ( ) )
155155 ) ;
156156 }
@@ -174,7 +174,7 @@ class TupleSet {
174174 tuple . pop ( ) ;
175175 if (
176176 ! next (
177- /** @type {IteratorStack<T , V> } */
177+ /** @type {IteratorStack<K[number] , V> } */
178178 ( iteratorStack . pop ( ) )
179179 )
180180 ) {
@@ -185,7 +185,7 @@ class TupleSet {
185185 done : false ,
186186 value :
187187 /* eslint-disable unicorn/prefer-spread */
188- /** @type {[T, V, ...EXPECTED_ANY ] } */
188+ /** @type {[...K, V ] } */
189189 ( tuple . concat ( result . value ) )
190190 } ;
191191 }
0 commit comments