| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const util = require("util"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-11 20:11:58 +08:00
										 |  |  | const TOMBSTONE = {}; | 
					
						
							|  |  |  | const UNDEFINED_MARKER = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | class StackedSetMap { | 
					
						
							| 
									
										
										
										
											2017-08-11 20:11:58 +08:00
										 |  |  | 	constructor(parentStack) { | 
					
						
							| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | 		this.stack = parentStack === undefined ? [] : parentStack.slice(); | 
					
						
							|  |  |  | 		this.map = new Map(); | 
					
						
							|  |  |  | 		this.stack.push(this.map); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	add(item) { | 
					
						
							|  |  |  | 		this.map.set(item, true); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	set(item, value) { | 
					
						
							| 
									
										
										
										
											2017-08-11 20:11:58 +08:00
										 |  |  | 		this.map.set(item, value === undefined ? UNDEFINED_MARKER : value); | 
					
						
							| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	delete(item) { | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 		if (this.stack.length > 1) { | 
					
						
							|  |  |  | 			this.map.set(item, TOMBSTONE); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			this.map.delete(item); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	has(item) { | 
					
						
							| 
									
										
										
										
											2017-08-11 20:11:58 +08:00
										 |  |  | 		const topValue = this.map.get(item); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (topValue !== undefined) return topValue !== TOMBSTONE; | 
					
						
							|  |  |  | 		if (this.stack.length > 1) { | 
					
						
							|  |  |  | 			for (var i = this.stack.length - 2; i >= 0; i--) { | 
					
						
							| 
									
										
										
										
											2017-08-11 20:11:58 +08:00
										 |  |  | 				const value = this.stack[i].get(item); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				if (value !== undefined) { | 
					
						
							| 
									
										
										
										
											2017-08-11 20:11:58 +08:00
										 |  |  | 					this.map.set(item, value); | 
					
						
							|  |  |  | 					return value !== TOMBSTONE; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			this.map.set(item, TOMBSTONE); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	get(item) { | 
					
						
							|  |  |  | 		const topValue = this.map.get(item); | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 		if (topValue !== undefined) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			return topValue === TOMBSTONE || topValue === UNDEFINED_MARKER | 
					
						
							|  |  |  | 				? undefined | 
					
						
							|  |  |  | 				: topValue; | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (this.stack.length > 1) { | 
					
						
							|  |  |  | 			for (var i = this.stack.length - 2; i >= 0; i--) { | 
					
						
							| 
									
										
										
										
											2017-08-11 20:11:58 +08:00
										 |  |  | 				const value = this.stack[i].get(item); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				if (value !== undefined) { | 
					
						
							| 
									
										
										
										
											2017-08-11 20:11:58 +08:00
										 |  |  | 					this.map.set(item, value); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					return value === TOMBSTONE || value === UNDEFINED_MARKER | 
					
						
							|  |  |  | 						? undefined | 
					
						
							|  |  |  | 						: value; | 
					
						
							| 
									
										
										
										
											2017-08-11 20:11:58 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-08-11 20:11:58 +08:00
										 |  |  | 			this.map.set(item, TOMBSTONE); | 
					
						
							| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-08-11 20:11:58 +08:00
										 |  |  | 		return undefined; | 
					
						
							| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_compress() { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (this.stack.length === 1) return; | 
					
						
							| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | 		this.map = new Map(); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		for (const data of this.stack) { | 
					
						
							|  |  |  | 			for (const pair of data) { | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 				if (pair[1] === TOMBSTONE) { | 
					
						
							|  |  |  | 					this.map.delete(pair[0]); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					this.map.set(pair[0], pair[1]); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		this.stack = [this.map]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 16:06:40 +08:00
										 |  |  | 	asArray() { | 
					
						
							| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | 		this._compress(); | 
					
						
							| 
									
										
										
										
											2017-11-19 16:06:40 +08:00
										 |  |  | 		return Array.from(this.map.entries(), pair => pair[0]); | 
					
						
							| 
									
										
										
										
											2017-08-11 20:11:58 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 16:06:40 +08:00
										 |  |  | 	asSet() { | 
					
						
							|  |  |  | 		return new Set(this.asArray()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	asPairArray() { | 
					
						
							| 
									
										
										
										
											2017-11-19 07:50:10 +08:00
										 |  |  | 		this._compress(); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		return Array.from( | 
					
						
							|  |  |  | 			this.map.entries(), | 
					
						
							| 
									
										
										
										
											2018-04-06 01:44:15 +08:00
										 |  |  | 			pair => | 
					
						
							| 
									
										
										
										
											2018-05-08 20:31:51 +08:00
										 |  |  | 				/** @type {[TODO, TODO]} */ (pair[1] === UNDEFINED_MARKER | 
					
						
							| 
									
										
										
										
											2018-04-06 01:44:15 +08:00
										 |  |  | 					? [pair[0], undefined] | 
					
						
							|  |  |  | 					: pair) | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-11-19 07:50:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-11 20:11:58 +08:00
										 |  |  | 	asMap() { | 
					
						
							| 
									
										
										
										
											2017-11-19 16:06:40 +08:00
										 |  |  | 		return new Map(this.asPairArray()); | 
					
						
							| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 16:06:40 +08:00
										 |  |  | 	get size() { | 
					
						
							| 
									
										
										
										
											2017-11-19 07:50:10 +08:00
										 |  |  | 		this._compress(); | 
					
						
							| 
									
										
										
										
											2017-11-19 16:06:40 +08:00
										 |  |  | 		return this.map.size; | 
					
						
							| 
									
										
										
										
											2017-11-19 07:50:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | 	createChild() { | 
					
						
							| 
									
										
										
										
											2017-08-11 20:11:58 +08:00
										 |  |  | 		return new StackedSetMap(this.stack); | 
					
						
							| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	get length() { | 
					
						
							|  |  |  | 		throw new Error("This is no longer an Array"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	set length(value) { | 
					
						
							|  |  |  | 		throw new Error("This is no longer an Array"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-24 05:28:23 +08:00
										 |  |  | // TODO remove in webpack 5
 | 
					
						
							| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | StackedSetMap.prototype.push = util.deprecate(function(item) { | 
					
						
							|  |  |  | 	this.add(item); | 
					
						
							|  |  |  | }, "This is no longer an Array: Use add instead."); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = StackedSetMap; |