| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2020-08-22 17:12:08 +08:00
										 |  |  |  * @param {Array<Iterable<T> | LazySet<T>>} toDeepMerge iterables or lazy set to be flattened | 
					
						
							| 
									
										
										
										
											2019-10-28 20:59:14 +08:00
										 |  |  |  * @returns {void} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const flatten = (targetSet, toDeepMerge) => { | 
					
						
							|  |  |  | 	for (const set of toDeepMerge) { | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | 		if (set instanceof LazySet) { | 
					
						
							| 
									
										
										
										
											2020-08-22 17:12:08 +08:00
										 |  |  | 			if (set._set.size > 0) 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 { | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-03-13 00:51:26 +08:00
										 |  |  | 	 * @param {Iterable<T>=} iterable init iterable | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	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(); | 
					
						
							| 
									
										
										
										
											2020-08-22 17:12:08 +08:00
										 |  |  | 		/** @type {Array<Iterable<T> | LazySet<T>>} */ | 
					
						
							|  |  |  | 		this._toDeepMerge = []; | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											2020-08-22 17:12:08 +08:00
										 |  |  | 		this._toDeepMerge.length = 0; | 
					
						
							| 
									
										
										
										
											2019-10-28 20:59:14 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							| 
									
										
										
										
											2020-08-22 17:12:08 +08:00
										 |  |  | 			this._toDeepMerge.push(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
 | 
					
						
							| 
									
										
										
										
											2020-08-22 17:12:08 +08:00
										 |  |  | 			if (this._toDeepMerge.length > 100000) { | 
					
						
							| 
									
										
										
										
											2019-10-28 20:59:14 +08:00
										 |  |  | 				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(); | 
					
						
							| 
									
										
										
										
											2020-08-22 17:12:08 +08:00
										 |  |  | 		this._toDeepMerge.length = 0; | 
					
						
							| 
									
										
										
										
											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](); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-17 15:42:09 +08:00
										 |  |  | 	/* istanbul ignore next */ | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | 	get [Symbol.toStringTag]() { | 
					
						
							|  |  |  | 		return "LazySet"; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	serialize({ write }) { | 
					
						
							|  |  |  | 		if (this._needMerge) this._merge(); | 
					
						
							| 
									
										
										
										
											2020-01-28 17:47:00 +08:00
										 |  |  | 		write(this._set.size); | 
					
						
							|  |  |  | 		for (const item of this._set) write(item); | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:15 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	static deserialize({ read }) { | 
					
						
							| 
									
										
										
										
											2020-01-28 17:47:00 +08:00
										 |  |  | 		const count = read(); | 
					
						
							|  |  |  | 		const items = []; | 
					
						
							|  |  |  | 		for (let i = 0; i < count; i++) { | 
					
						
							|  |  |  | 			items.push(read()); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return new LazySet(items); | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:15 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											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; |