@@ -162,53 +162,51 @@ export class LinkedMap<K, V> implements Map<K, V> {
162162 }
163163 }
164164
165- public keys ( ) : IterableIterator < K > {
165+ public keys ( ) : MapIterator < K > {
166+ return this . _keys ( ) ;
167+ }
168+
169+ private * _keys ( ) : Generator < K , BuiltinIteratorReturn , unknown > {
166170 const state = this . _state ;
167171 let current = this . _head ;
168- const iterator : IterableIterator < K > = {
169- [ Symbol . iterator ] : ( ) => {
170- return iterator ;
171- } ,
172- next : ( ) : IteratorResult < K > => {
173- if ( this . _state !== state ) {
174- throw new Error ( `LinkedMap got modified during iteration.` ) ;
175- }
176- if ( current ) {
177- const result = { value : current . key , done : false } ;
178- current = current . next ;
179- return result ;
180- } else {
181- return { value : undefined , done : true } ;
182- }
172+
173+ while ( true ) {
174+ if ( this . _state !== state ) {
175+ throw new Error ( `LinkedMap got modified during iteration.` ) ;
183176 }
184- } ;
185- return iterator ;
177+ if ( ! current ) {
178+ return ;
179+ }
180+
181+ const yieldResult = current . key ;
182+ current = current . next ;
183+ yield yieldResult ;
184+ }
186185 }
187186
188- public values ( ) : IterableIterator < V > {
187+ public values ( ) : MapIterator < V > {
188+ return this . _values ( ) ;
189+ }
190+
191+ private * _values ( ) : Generator < V , BuiltinIteratorReturn , unknown > {
189192 const state = this . _state ;
190193 let current = this . _head ;
191- const iterator : IterableIterator < V > = {
192- [ Symbol . iterator ] : ( ) => {
193- return iterator ;
194- } ,
195- next : ( ) : IteratorResult < V > => {
196- if ( this . _state !== state ) {
197- throw new Error ( `LinkedMap got modified during iteration.` ) ;
198- }
199- if ( current ) {
200- const result = { value : current . value , done : false } ;
201- current = current . next ;
202- return result ;
203- } else {
204- return { value : undefined , done : true } ;
205- }
194+
195+ while ( true ) {
196+ if ( this . _state !== state ) {
197+ throw new Error ( `LinkedMap got modified during iteration.` ) ;
206198 }
207- } ;
208- return iterator ;
199+ if ( ! current ) {
200+ return ;
201+ }
202+
203+ const yieldResult = current . value ;
204+ current = current . next ;
205+ yield yieldResult ;
206+ }
209207 }
210208
211- public entries ( ) : IterableIterator < [ K , V ] > {
209+ public entries ( ) : MapIterator < [ K , V ] > {
212210 const state = this . _state ;
213211 let current = this . _head ;
214212 const iterator : IterableIterator < [ K , V ] > = {
@@ -231,8 +229,26 @@ export class LinkedMap<K, V> implements Map<K, V> {
231229 return iterator ;
232230 }
233231
234- public [ Symbol . iterator ] ( ) : IterableIterator < [ K , V ] > {
235- return this . entries ( ) ;
232+ private * _entries ( ) : Generator < [ K , V ] , BuiltinIteratorReturn , unknown > {
233+ const state = this . _state ;
234+ let current = this . _head ;
235+
236+ while ( true ) {
237+ if ( this . _state !== state ) {
238+ throw new Error ( `LinkedMap got modified during iteration.` ) ;
239+ }
240+ if ( ! current ) {
241+ return ;
242+ }
243+
244+ const yieldResult : [ K , V ] = [ current . key , current . value ] ;
245+ current = current . next ;
246+ yield yieldResult ;
247+ }
248+ }
249+
250+ public [ Symbol . iterator ] ( ) : MapIterator < [ K , V ] > {
251+ return this . _entries ( ) ;
236252 }
237253
238254 protected trimOld ( newSize : number ) : void {
0 commit comments