| 
									
										
										
										
											2017-10-13 01:12:48 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-15 04:19:36 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @template T | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class Queue { | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2018-05-12 00:12:30 +08:00
										 |  |  | 	 * @param {Iterable<T>=} items The initial elements. | 
					
						
							| 
									
										
										
										
											2018-04-15 04:19:36 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-10-13 01:12:48 +08:00
										 |  |  | 	constructor(items) { | 
					
						
							| 
									
										
										
										
											2018-04-15 04:19:36 +08:00
										 |  |  | 		/** @private @type {Set<T>} */ | 
					
						
							| 
									
										
										
										
											2018-01-11 20:25:26 +08:00
										 |  |  | 		this.set = new Set(items); | 
					
						
							| 
									
										
										
										
											2018-04-15 04:19:36 +08:00
										 |  |  | 		/** @private @type {Iterator<T>} */ | 
					
						
							| 
									
										
										
										
											2018-01-11 20:25:26 +08:00
										 |  |  | 		this.iterator = this.set[Symbol.iterator](); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-15 04:19:36 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Returns the number of elements in this queue. | 
					
						
							| 
									
										
										
										
											2018-05-08 20:31:51 +08:00
										 |  |  | 	 * @returns {number} The number of elements in this queue. | 
					
						
							| 
									
										
										
										
											2018-04-15 04:19:36 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-01-11 20:25:26 +08:00
										 |  |  | 	get length() { | 
					
						
							|  |  |  | 		return this.set.size; | 
					
						
							| 
									
										
										
										
											2017-10-13 01:12:48 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-15 04:19:36 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Appends the specified element to this queue. | 
					
						
							|  |  |  | 	 * @param {T} item The element to add. | 
					
						
							| 
									
										
										
										
											2018-05-08 20:31:51 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							| 
									
										
										
										
											2018-04-15 04:19:36 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-10-13 01:12:48 +08:00
										 |  |  | 	enqueue(item) { | 
					
						
							| 
									
										
										
										
											2018-01-11 20:25:26 +08:00
										 |  |  | 		this.set.add(item); | 
					
						
							| 
									
										
										
										
											2017-10-13 01:12:48 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-15 04:19:36 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Retrieves and removes the head of this queue. | 
					
						
							| 
									
										
										
										
											2018-05-08 20:31:51 +08:00
										 |  |  | 	 * @returns {T | undefined} The head of the queue of `undefined` if this queue is empty. | 
					
						
							| 
									
										
										
										
											2018-04-15 04:19:36 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-10-13 01:12:48 +08:00
										 |  |  | 	dequeue() { | 
					
						
							| 
									
										
										
										
											2018-01-11 20:25:26 +08:00
										 |  |  | 		const result = this.iterator.next(); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (result.done) return undefined; | 
					
						
							| 
									
										
										
										
											2018-01-11 20:25:26 +08:00
										 |  |  | 		this.set.delete(result.value); | 
					
						
							|  |  |  | 		return result.value; | 
					
						
							| 
									
										
										
										
											2017-10-13 01:12:48 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-15 04:19:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = Queue; |