| 
									
										
										
										
											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-04-27 10:30:00 +08:00
										 |  |  |  * @param {Set[]} sets an array of sets being checked for shared elements | 
					
						
							| 
									
										
										
										
											2018-05-08 20:31:51 +08:00
										 |  |  |  * @returns {Set<TODO>} 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-05-08 20:31:51 +08:00
										 |  |  |  * @param {Set<TODO>} bigSet a Set which contains the original elements to compare against | 
					
						
							|  |  |  |  * @param {Set<TODO>} 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
										 |  |  | 
 | 
					
						
							|  |  |  | exports.intersect = intersect; | 
					
						
							|  |  |  | exports.isSubset = isSubset; |