forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
25 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,46 @@ | ||
"use strict"; | ||
|
||
module.exports = class Queue { | ||
/** | ||
* @template T | ||
*/ | ||
class Queue { | ||
/** | ||
* @param {IterableIterator<T>=} items The initial elements. | ||
*/ | ||
constructor(items) { | ||
/** @private @type {Set<T>} */ | ||
this.set = new Set(items); | ||
/** @private @type {Iterator<T>} */ | ||
this.iterator = this.set[Symbol.iterator](); | ||
} | ||
|
||
/** | ||
* Returns the number of elements in this queue. | ||
* @return {number} The number of elements in this queue. | ||
*/ | ||
get length() { | ||
return this.set.size; | ||
} | ||
|
||
/** | ||
* Appends the specified element to this queue. | ||
* @param {T} item The element to add. | ||
* @return {void} | ||
*/ | ||
enqueue(item) { | ||
this.set.add(item); | ||
} | ||
|
||
/** | ||
* Retrieves and removes the head of this queue. | ||
* @return {T | undefined} The head of the queue of `undefined` if this queue is empty. | ||
*/ | ||
dequeue() { | ||
const result = this.iterator.next(); | ||
if (result.done) return undefined; | ||
this.set.delete(result.value); | ||
return result.value; | ||
} | ||
}; | ||
} | ||
|
||
module.exports = Queue; |