main
Last change
on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago |
F4 Finalna Verzija
|
-
Property mode
set to
100644
|
File size:
978 bytes
|
Line | |
---|
1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * @template T
|
---|
10 | */
|
---|
11 | class Queue {
|
---|
12 | /**
|
---|
13 | * @param {Iterable<T>=} items The initial elements.
|
---|
14 | */
|
---|
15 | constructor(items) {
|
---|
16 | /**
|
---|
17 | * @private
|
---|
18 | * @type {Set<T>}
|
---|
19 | */
|
---|
20 | this._set = new Set(items);
|
---|
21 | }
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Returns the number of elements in this queue.
|
---|
25 | * @returns {number} The number of elements in this queue.
|
---|
26 | */
|
---|
27 | get length() {
|
---|
28 | return this._set.size;
|
---|
29 | }
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Appends the specified element to this queue.
|
---|
33 | * @param {T} item The element to add.
|
---|
34 | * @returns {void}
|
---|
35 | */
|
---|
36 | enqueue(item) {
|
---|
37 | this._set.add(item);
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Retrieves and removes the head of this queue.
|
---|
42 | * @returns {T | undefined} The head of the queue of `undefined` if this queue is empty.
|
---|
43 | */
|
---|
44 | dequeue() {
|
---|
45 | const result = this._set[Symbol.iterator]().next();
|
---|
46 | if (result.done) return;
|
---|
47 | this._set.delete(result.value);
|
---|
48 | return result.value;
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | module.exports = Queue;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.