| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2020-03-13 00:51:26 +08:00
										 |  |  |  * @param {Set<T>} smallSet the set whose 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 | 
					
						
							| 
									
										
										
										
											2025-03-12 09:56:14 +08:00
										 |  |  |  * @param {(set: T) => boolean} fn selector function | 
					
						
							| 
									
										
										
										
											2018-09-06 22:59:11 +08:00
										 |  |  |  * @returns {T | undefined} found item | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const find = (set, fn) => { | 
					
						
							|  |  |  | 	for (const item of set) { | 
					
						
							|  |  |  | 		if (fn(item)) return item; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 21:21:27 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @template T | 
					
						
							| 
									
										
										
										
											2024-11-01 04:19:07 +08:00
										 |  |  |  * @param {Set<T> | ReadonlySet<T>} set a set | 
					
						
							| 
									
										
										
										
											2021-02-04 21:21:27 +08:00
										 |  |  |  * @returns {T | undefined} first item | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const first = set => { | 
					
						
							|  |  |  | 	const entry = set.values().next(); | 
					
						
							|  |  |  | 	return entry.done ? undefined : entry.value; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-23 04:55:35 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @template T | 
					
						
							|  |  |  |  * @param {Set<T>} a first | 
					
						
							|  |  |  |  * @param {Set<T>} b second | 
					
						
							|  |  |  |  * @returns {Set<T>} combined set, may be identical to a or b | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const combine = (a, b) => { | 
					
						
							|  |  |  | 	if (b.size === 0) return a; | 
					
						
							|  |  |  | 	if (a.size === 0) return b; | 
					
						
							|  |  |  | 	const set = new Set(a); | 
					
						
							|  |  |  | 	for (const item of b) set.add(item); | 
					
						
							|  |  |  | 	return set; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-31 04:54:55 +08:00
										 |  |  | module.exports.intersect = intersect; | 
					
						
							|  |  |  | module.exports.isSubset = isSubset; | 
					
						
							|  |  |  | module.exports.find = find; | 
					
						
							|  |  |  | module.exports.first = first; | 
					
						
							|  |  |  | module.exports.combine = combine; |