Sha256: 4f9dfaa207b5fb02f4a334bf5b8cf3cf2cb4ab66dc0a880182dfa7b344436564

Contents?: true

Size: 1.34 KB

Versions: 27

Compression:

Stored size: 1.34 KB

Contents

"use strict";

/**
 * intersect creates Set containing the intersection of elements between all sets
 * @param {Set[]} sets an array of sets being checked for shared elements
 * @returns {Set<TODO>} returns a new Set containing the intersecting items
 */
const intersect = sets => {
	if (sets.length === 0) return new Set();
	if (sets.length === 1) return new Set(sets[0]);
	let minSize = Infinity;
	let minIndex = -1;
	for (let i = 0; i < sets.length; i++) {
		const size = sets[i].size;
		if (size < minSize) {
			minIndex = i;
			minSize = size;
		}
	}
	const current = new Set(sets[minIndex]);
	for (let i = 0; i < sets.length; i++) {
		if (i === minIndex) continue;
		const set = sets[i];
		for (const item of current) {
			if (!set.has(item)) {
				current.delete(item);
			}
		}
	}
	return current;
};

/**
 * Checks if a set is the subset of another set
 * @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
 * @returns {boolean} returns true if smallSet contains all elements inside of the bigSet
 */
const isSubset = (bigSet, smallSet) => {
	if (bigSet.size < smallSet.size) return false;
	for (const item of smallSet) {
		if (!bigSet.has(item)) return false;
	}
	return true;
};

exports.intersect = intersect;
exports.isSubset = isSubset;

Version data entries

27 entries across 26 versions & 9 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/webpack/lib/util/SetHelpers.js
disco_app-0.18.0 test/dummy/node_modules/webpack/lib/util/SetHelpers.js
disco_app-0.18.2 test/dummy/node_modules/webpack/lib/util/SetHelpers.js
disco_app-0.16.1 test/dummy/node_modules/webpack/lib/util/SetHelpers.js
disco_app-0.15.2 test/dummy/node_modules/webpack/lib/util/SetHelpers.js
disco_app-0.18.4 test/dummy/node_modules/webpack/lib/util/SetHelpers.js
disco_app-0.18.1 test/dummy/node_modules/webpack/lib/util/SetHelpers.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/webpack/lib/util/SetHelpers.js
disco_app-0.14.0 test/dummy/node_modules/webpack/lib/util/SetHelpers.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/webpack/lib/util/SetHelpers.js
tang-0.2.1 spec/tang_app/node_modules/webpack/lib/util/SetHelpers.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/webpack/lib/util/SetHelpers.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/webpack/lib/util/SetHelpers.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/webpack/lib/util/SetHelpers.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/webpack/lib/util/SetHelpers.js
tang-0.2.0 spec/tang_app/node_modules/webpack/lib/util/SetHelpers.js
tang-0.1.0 spec/tang_app/node_modules/webpack/lib/util/SetHelpers.js
tang-0.0.9 spec/tang_app/node_modules/webpack/lib/util/SetHelpers.js
enju_library-0.3.8 spec/dummy/node_modules/webpack/lib/util/SetHelpers.js
jester-data-8.0.0 node_modules/webpack/lib/util/SetHelpers.js