| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-26 04:12:28 +08:00
										 |  |  | const TOMBSTONE = Symbol("tombstone"); | 
					
						
							|  |  |  | const UNDEFINED_MARKER = Symbol("undefined"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @template T | 
					
						
							| 
									
										
										
										
											2019-09-05 17:47:18 +08:00
										 |  |  |  * @typedef {T | undefined} Cell<T> | 
					
						
							| 
									
										
										
										
											2018-11-26 04:12:28 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @template T | 
					
						
							| 
									
										
										
										
											2019-09-05 17:47:18 +08:00
										 |  |  |  * @typedef {T | typeof TOMBSTONE | typeof UNDEFINED_MARKER} InternalCell<T> | 
					
						
							| 
									
										
										
										
											2018-11-26 04:12:28 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @template K | 
					
						
							|  |  |  |  * @template V | 
					
						
							|  |  |  |  * @param {[K, InternalCell<V>]} pair the internal cell | 
					
						
							|  |  |  |  * @returns {[K, Cell<V>]} its “safe” representation | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const extractPair = pair => { | 
					
						
							|  |  |  | 	const key = pair[0]; | 
					
						
							|  |  |  | 	const val = pair[1]; | 
					
						
							|  |  |  | 	if (val === UNDEFINED_MARKER || val === TOMBSTONE) { | 
					
						
							|  |  |  | 		return [key, undefined]; | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2018-11-26 23:26:18 +08:00
										 |  |  | 		return /** @type {[K, Cell<V>]} */ (pair); | 
					
						
							| 
									
										
										
										
											2018-11-26 04:12:28 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-08-11 20:11:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-26 04:12:28 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @template K | 
					
						
							|  |  |  |  * @template V | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-09-05 17:47:18 +08:00
										 |  |  | class StackedMap { | 
					
						
							| 
									
										
										
										
											2018-11-26 04:12:28 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2019-08-29 21:28:19 +08:00
										 |  |  | 	 * @param {Map<K, InternalCell<V>>[]=} parentStack an optional parent | 
					
						
							| 
									
										
										
										
											2018-11-26 04:12:28 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-08-11 20:11:58 +08:00
										 |  |  | 	constructor(parentStack) { | 
					
						
							| 
									
										
										
										
											2018-11-26 04:12:28 +08:00
										 |  |  | 		/** @type {Map<K, InternalCell<V>>} */ | 
					
						
							| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | 		this.map = new Map(); | 
					
						
							| 
									
										
										
										
											2018-11-26 04:12:28 +08:00
										 |  |  | 		/** @type {Map<K, InternalCell<V>>[]} */ | 
					
						
							|  |  |  | 		this.stack = parentStack === undefined ? [] : parentStack.slice(); | 
					
						
							| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | 		this.stack.push(this.map); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-26 04:12:28 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {K} item the key of the element to add | 
					
						
							|  |  |  | 	 * @param {V} value the value of the element to add | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | 	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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-26 04:12:28 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {K} item the item to delete | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-26 04:12:28 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {K} item the item to test | 
					
						
							|  |  |  | 	 * @returns {boolean} true if the item exists in this set | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											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-11-26 04:12:28 +08:00
										 |  |  | 		if (topValue !== undefined) { | 
					
						
							|  |  |  | 			return topValue !== TOMBSTONE; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (this.stack.length > 1) { | 
					
						
							| 
									
										
										
										
											2018-11-26 04:12:28 +08:00
										 |  |  | 			for (let 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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-26 04:12:28 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {K} item the key of the element to return | 
					
						
							|  |  |  | 	 * @returns {Cell<V>} the value of the element | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2018-11-26 04:12:28 +08:00
										 |  |  | 			for (let 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(); | 
					
						
							| 
									
										
										
										
											2018-11-26 04:12:28 +08:00
										 |  |  | 		return Array.from(this.map.keys()); | 
					
						
							| 
									
										
										
										
											2017-08-11 20:11:58 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 16:06:40 +08:00
										 |  |  | 	asSet() { | 
					
						
							| 
									
										
										
										
											2018-11-26 23:26:18 +08:00
										 |  |  | 		this._compress(); | 
					
						
							|  |  |  | 		return new Set(this.map.keys()); | 
					
						
							| 
									
										
										
										
											2017-11-19 16:06:40 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	asPairArray() { | 
					
						
							| 
									
										
										
										
											2017-11-19 07:50:10 +08:00
										 |  |  | 		this._compress(); | 
					
						
							| 
									
										
										
										
											2018-11-26 04:12:28 +08:00
										 |  |  | 		return Array.from(this.map.entries(), extractPair); | 
					
						
							| 
									
										
										
										
											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() { | 
					
						
							| 
									
										
										
										
											2019-09-05 17:47:18 +08:00
										 |  |  | 		return new StackedMap(this.stack); | 
					
						
							| 
									
										
										
										
											2017-08-11 18:30:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 17:47:18 +08:00
										 |  |  | module.exports = StackedMap; |