| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:15 +08:00
										 |  |  | const makeSerializable = require("./makeSerializable.js"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @template T | 
					
						
							|  |  |  |  * @param {Set<T>} targetSet set where items should be added | 
					
						
							| 
									
										
										
										
											2019-10-28 20:59:14 +08:00
										 |  |  |  * @param {Set<Iterable<T>>} toMerge iterables to be merged | 
					
						
							|  |  |  |  * @returns {void} | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | const merge = (targetSet, toMerge) => { | 
					
						
							|  |  |  | 	for (const set of toMerge) { | 
					
						
							| 
									
										
										
										
											2019-10-28 20:59:14 +08:00
										 |  |  | 		for (const item of set) { | 
					
						
							|  |  |  | 			targetSet.add(item); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @template T | 
					
						
							|  |  |  |  * @param {Set<Iterable<T>>} targetSet set where iterables should be added | 
					
						
							|  |  |  |  * @param {Set<Iterable<T> | LazySet<T>>} toDeepMerge iterables or lazy set to be flattened | 
					
						
							|  |  |  |  * @returns {void} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const flatten = (targetSet, toDeepMerge) => { | 
					
						
							|  |  |  | 	for (const set of toDeepMerge) { | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | 		if (set instanceof LazySet) { | 
					
						
							| 
									
										
										
										
											2019-10-28 20:59:14 +08:00
										 |  |  | 			targetSet.add(set._set); | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | 			if (set._needMerge) { | 
					
						
							| 
									
										
										
										
											2019-10-28 20:59:14 +08:00
										 |  |  | 				for (const mergedSet of set._toMerge) { | 
					
						
							|  |  |  | 					targetSet.add(mergedSet); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				flatten(targetSet, set._toDeepMerge); | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2019-10-28 20:59:14 +08:00
										 |  |  | 			targetSet.add(set); | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Like Set but with an addAll method to eventually add items from another iterable. | 
					
						
							|  |  |  |  * Access methods make sure that all delayed operations are executed. | 
					
						
							|  |  |  |  * Iteration methods deopts to normal Set performance until clear is called again (because of the chance of modifications during iteration). | 
					
						
							|  |  |  |  * @template T | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class LazySet { | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Iterable<T>=} iterable init interable | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	constructor(iterable) { | 
					
						
							|  |  |  | 		/** @type {Set<T>} */ | 
					
						
							|  |  |  | 		this._set = new Set(iterable); | 
					
						
							| 
									
										
										
										
											2019-10-28 20:59:14 +08:00
										 |  |  | 		/** @type {Set<Iterable<T>>} */ | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | 		this._toMerge = new Set(); | 
					
						
							| 
									
										
										
										
											2019-10-28 20:59:14 +08:00
										 |  |  | 		/** @type {Set<Iterable<T> | LazySet<T>>} */ | 
					
						
							|  |  |  | 		this._toDeepMerge = new Set(); | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | 		this._needMerge = false; | 
					
						
							|  |  |  | 		this._deopt = false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-28 20:59:14 +08:00
										 |  |  | 	_flatten() { | 
					
						
							|  |  |  | 		flatten(this._toMerge, this._toDeepMerge); | 
					
						
							|  |  |  | 		this._toDeepMerge.clear(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | 	_merge() { | 
					
						
							| 
									
										
										
										
											2019-10-28 20:59:14 +08:00
										 |  |  | 		this._flatten(); | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | 		merge(this._set, this._toMerge); | 
					
						
							|  |  |  | 		this._toMerge.clear(); | 
					
						
							|  |  |  | 		this._needMerge = false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	get size() { | 
					
						
							|  |  |  | 		if (this._needMerge) this._merge(); | 
					
						
							|  |  |  | 		return this._set.size; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {T} item an item | 
					
						
							|  |  |  | 	 * @returns {this} itself | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	add(item) { | 
					
						
							|  |  |  | 		this._set.add(item); | 
					
						
							|  |  |  | 		return this; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Iterable<T> | LazySet<T>} iterable a immutable iterable or another immutable LazySet which will eventually be merged into the Set | 
					
						
							|  |  |  | 	 * @returns {this} itself | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	addAll(iterable) { | 
					
						
							|  |  |  | 		if (this._deopt) { | 
					
						
							|  |  |  | 			const _set = this._set; | 
					
						
							|  |  |  | 			for (const item of iterable) { | 
					
						
							|  |  |  | 				_set.add(item); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2019-10-28 20:59:14 +08:00
										 |  |  | 			this._toDeepMerge.add(iterable); | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | 			this._needMerge = true; | 
					
						
							| 
									
										
										
										
											2019-10-28 20:59:14 +08:00
										 |  |  | 			// Avoid being too memory hungry
 | 
					
						
							|  |  |  | 			if (this._toDeepMerge.size > 100000) { | 
					
						
							|  |  |  | 				this._flatten(); | 
					
						
							|  |  |  | 				if (this._toMerge.size > 100000) this._merge(); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		return this; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	clear() { | 
					
						
							|  |  |  | 		this._set.clear(); | 
					
						
							|  |  |  | 		this._toMerge.clear(); | 
					
						
							| 
									
										
										
										
											2019-10-28 20:59:14 +08:00
										 |  |  | 		this._toDeepMerge.clear(); | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | 		this._needMerge = false; | 
					
						
							|  |  |  | 		this._deopt = false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {T} value an item | 
					
						
							|  |  |  | 	 * @returns {boolean} true, if the value was in the Set before | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	delete(value) { | 
					
						
							|  |  |  | 		if (this._needMerge) this._merge(); | 
					
						
							|  |  |  | 		return this._set.delete(value); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	entries() { | 
					
						
							|  |  |  | 		this._deopt = true; | 
					
						
							|  |  |  | 		if (this._needMerge) this._merge(); | 
					
						
							|  |  |  | 		return this._set.entries(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {function(T, T, Set<T>): void} callbackFn function called for each entry | 
					
						
							|  |  |  | 	 * @param {any} thisArg this argument for the callbackFn | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	forEach(callbackFn, thisArg) { | 
					
						
							|  |  |  | 		this._deopt = true; | 
					
						
							|  |  |  | 		if (this._needMerge) this._merge(); | 
					
						
							|  |  |  | 		this._set.forEach(callbackFn, thisArg); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {T} item an item | 
					
						
							|  |  |  | 	 * @returns {boolean} true, when the item is in the Set | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	has(item) { | 
					
						
							|  |  |  | 		if (this._needMerge) this._merge(); | 
					
						
							|  |  |  | 		return this._set.has(item); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	keys() { | 
					
						
							|  |  |  | 		this._deopt = true; | 
					
						
							|  |  |  | 		if (this._needMerge) this._merge(); | 
					
						
							|  |  |  | 		return this._set.keys(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	values() { | 
					
						
							|  |  |  | 		this._deopt = true; | 
					
						
							|  |  |  | 		if (this._needMerge) this._merge(); | 
					
						
							|  |  |  | 		return this._set.values(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	[Symbol.iterator]() { | 
					
						
							|  |  |  | 		this._deopt = true; | 
					
						
							|  |  |  | 		if (this._needMerge) this._merge(); | 
					
						
							|  |  |  | 		return this._set[Symbol.iterator](); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	get [Symbol.toStringTag]() { | 
					
						
							|  |  |  | 		return "LazySet"; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	serialize({ write }) { | 
					
						
							|  |  |  | 		if (this._needMerge) this._merge(); | 
					
						
							|  |  |  | 		write(this._set); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	static deserialize({ read }) { | 
					
						
							|  |  |  | 		return new LazySet(read()); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:15 +08:00
										 |  |  | makeSerializable(LazySet, "webpack/lib/util/LazySet"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | module.exports = LazySet; |