This commit is contained in:
Muthukumar M 2025-10-06 11:28:31 +05:30 committed by GitHub
commit 8fbce14d5b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 3 deletions

View File

@ -18,6 +18,11 @@ class Queue {
* @type {Set<T>} * @type {Set<T>}
*/ */
this._set = new Set(items); this._set = new Set(items);
/**
* @private
* @type {Iterator<T>}
*/
this._iterator = this._set[Symbol.iterator]();
} }
/** /**
@ -39,11 +44,16 @@ class Queue {
/** /**
* Retrieves and removes the head of this queue. * Retrieves and removes the head of this queue.
* @returns {T | undefined} The head of the queue of `undefined` if this queue is empty. * @returns {T | undefined} The head of the queue or `undefined` if this queue is empty.
*/ */
dequeue() { dequeue() {
const result = this._set[Symbol.iterator]().next(); let result = this._iterator.next();
if (result.done) return; const isInvalidIterator = result.done && this._set.size > 0;
if (isInvalidIterator) {
this._iterator = this._set[Symbol.iterator]();
result = this._iterator.next();
}
if (result.done) return undefined;
this._set.delete(result.value); this._set.delete(result.value);
return result.value; return result.value;
} }