| 
									
										
										
										
											2020-07-29 04:05:13 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const TupleSet = require("./TupleSet"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2025-04-03 00:02:22 +08:00
										 |  |  |  * @template T | 
					
						
							|  |  |  |  * @template V | 
					
						
							| 
									
										
										
										
											2020-07-29 04:05:13 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | class TupleQueue { | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2025-04-16 22:04:11 +08:00
										 |  |  | 	 * @param {Iterable<[T, V, ...EXPECTED_ANY]>=} items The initial elements. | 
					
						
							| 
									
										
										
										
											2020-07-29 04:05:13 +08:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	constructor(items) { | 
					
						
							| 
									
										
										
										
											2024-06-11 20:32:02 +08:00
										 |  |  | 		/** | 
					
						
							|  |  |  | 		 * @private | 
					
						
							| 
									
										
										
										
											2025-04-16 22:04:11 +08:00
										 |  |  | 		 * @type {TupleSet<T, V>} | 
					
						
							| 
									
										
										
										
											2024-06-11 20:32:02 +08:00
										 |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2020-07-29 04:05:13 +08:00
										 |  |  | 		this._set = new TupleSet(items); | 
					
						
							| 
									
										
										
										
											2024-06-11 20:32:02 +08:00
										 |  |  | 		/** | 
					
						
							|  |  |  | 		 * @private | 
					
						
							| 
									
										
										
										
											2025-04-16 22:04:11 +08:00
										 |  |  | 		 * @type {Iterator<[T, V, ...EXPECTED_ANY]>} | 
					
						
							| 
									
										
										
										
											2024-06-11 20:32:02 +08:00
										 |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2020-07-29 04:05:13 +08:00
										 |  |  | 		this._iterator = this._set[Symbol.iterator](); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Returns the number of elements in this queue. | 
					
						
							|  |  |  | 	 * @returns {number} The number of elements in this queue. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	get length() { | 
					
						
							|  |  |  | 		return this._set.size; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Appends the specified element to this queue. | 
					
						
							| 
									
										
										
										
											2025-04-16 22:04:11 +08:00
										 |  |  | 	 * @param {[T, V, ...EXPECTED_ANY]} item The element to add. | 
					
						
							| 
									
										
										
										
											2020-07-29 04:05:13 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	enqueue(...item) { | 
					
						
							|  |  |  | 		this._set.add(...item); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Retrieves and removes the head of this queue. | 
					
						
							| 
									
										
										
										
											2025-04-16 22:04:11 +08:00
										 |  |  | 	 * @returns {[T, V, ...EXPECTED_ANY] | undefined} The head of the queue of `undefined` if this queue is empty. | 
					
						
							| 
									
										
										
										
											2020-07-29 04:05:13 +08:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	dequeue() { | 
					
						
							|  |  |  | 		const result = this._iterator.next(); | 
					
						
							|  |  |  | 		if (result.done) { | 
					
						
							|  |  |  | 			if (this._set.size > 0) { | 
					
						
							|  |  |  | 				this._iterator = this._set[Symbol.iterator](); | 
					
						
							| 
									
										
										
										
											2025-04-16 22:04:11 +08:00
										 |  |  | 				const value = | 
					
						
							|  |  |  | 					/** @type {[T, V, ...EXPECTED_ANY]} */ | 
					
						
							|  |  |  | 					(this._iterator.next().value); | 
					
						
							| 
									
										
										
										
											2020-07-29 04:05:13 +08:00
										 |  |  | 				this._set.delete(...value); | 
					
						
							|  |  |  | 				return value; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2020-07-29 04:05:13 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2025-04-16 22:04:11 +08:00
										 |  |  | 		this._set.delete(.../** @type {[T, V, ...EXPECTED_ANY]} */ (result.value)); | 
					
						
							| 
									
										
										
										
											2020-07-29 04:05:13 +08:00
										 |  |  | 		return result.value; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = TupleQueue; |