| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-27 10:30:00 +08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2018-04-30 15:18:49 +08:00
										 |  |  |  * intersect creates Set containing the intersection of elements between all sets | 
					
						
							| 
									
										
										
										
											2018-09-06 22:58:05 +08:00
										 |  |  |  * @template T | 
					
						
							|  |  |  |  * @param {Set<T>[]} sets an array of sets being checked for shared elements | 
					
						
							|  |  |  |  * @returns {Set<T>} returns a new Set containing the intersecting items | 
					
						
							| 
									
										
										
										
											2018-04-27 10:30:00 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-06-27 19:48:13 +08:00
										 |  |  | const intersect = sets => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	if (sets.length === 0) return new Set(); | 
					
						
							|  |  |  | 	if (sets.length === 1) return new Set(sets[0]); | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 	let minSize = Infinity; | 
					
						
							|  |  |  | 	let minIndex = -1; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	for (let i = 0; i < sets.length; i++) { | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 		const size = sets[i].size; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (size < minSize) { | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 			minIndex = i; | 
					
						
							|  |  |  | 			minSize = size; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	const current = new Set(sets[minIndex]); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	for (let i = 0; i < sets.length; i++) { | 
					
						
							|  |  |  | 		if (i === minIndex) continue; | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 		const set = sets[i]; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		for (const item of current) { | 
					
						
							|  |  |  | 			if (!set.has(item)) { | 
					
						
							| 
									
										
										
										
											2018-01-20 00:06:59 +08:00
										 |  |  | 				current.delete(item); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return current; | 
					
						
							| 
									
										
										
										
											2018-06-27 19:48:13 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2018-02-17 16:48:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-27 10:30:00 +08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2018-04-30 15:18:49 +08:00
										 |  |  |  * Checks if a set is the subset of another set | 
					
						
							| 
									
										
										
										
											2018-09-06 22:58:05 +08:00
										 |  |  |  * @template T | 
					
						
							|  |  |  |  * @param {Set<T>} bigSet a Set which contains the original elements to compare against | 
					
						
							|  |  |  |  * @param {Set<T>} smallSet the set whos elements might be contained inside of bigSet | 
					
						
							| 
									
										
										
										
											2018-04-27 10:30:00 +08:00
										 |  |  |  * @returns {boolean} returns true if smallSet contains all elements inside of the bigSet | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-06-27 19:48:13 +08:00
										 |  |  | const isSubset = (bigSet, smallSet) => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	if (bigSet.size < smallSet.size) return false; | 
					
						
							|  |  |  | 	for (const item of smallSet) { | 
					
						
							|  |  |  | 		if (!bigSet.has(item)) return false; | 
					
						
							| 
									
										
										
										
											2018-02-17 16:48:40 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return true; | 
					
						
							| 
									
										
										
										
											2018-06-27 19:48:13 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2018-04-27 10:30:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 22:59:11 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @template T | 
					
						
							|  |  |  |  * @param {Set<T>} set a set | 
					
						
							|  |  |  |  * @param {function(T): boolean} fn selector function | 
					
						
							|  |  |  |  * @returns {T | undefined} found item | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const find = (set, fn) => { | 
					
						
							|  |  |  | 	for (const item of set) { | 
					
						
							|  |  |  | 		if (fn(item)) return item; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-27 10:30:00 +08:00
										 |  |  | exports.intersect = intersect; | 
					
						
							|  |  |  | exports.isSubset = isSubset; | 
					
						
							| 
									
										
										
										
											2018-09-06 22:59:11 +08:00
										 |  |  | exports.find = find; |