source: frontend/node_modules/webpack/lib/util/SetHelpers.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.4 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8/**
9 * intersect creates Set containing the intersection of elements between all sets
10 * @template T
11 * @param {Set<T>[]} sets an array of sets being checked for shared elements
12 * @returns {Set<T>} returns a new Set containing the intersecting items
13 */
14const intersect = (sets) => {
15 if (sets.length === 0) return new Set();
16 if (sets.length === 1) return new Set(sets[0]);
17 let minSize = Infinity;
18 let minIndex = -1;
19 for (let i = 0; i < sets.length; i++) {
20 const size = sets[i].size;
21 if (size < minSize) {
22 minIndex = i;
23 minSize = size;
24 }
25 }
26 const current = new Set(sets[minIndex]);
27 for (let i = 0; i < sets.length; i++) {
28 if (i === minIndex) continue;
29 const set = sets[i];
30 for (const item of current) {
31 if (!set.has(item)) {
32 current.delete(item);
33 }
34 }
35 }
36 return current;
37};
38
39/**
40 * Checks if a set is the subset of another set
41 * @template T
42 * @param {Set<T>} bigSet a Set which contains the original elements to compare against
43 * @param {Set<T>} smallSet the set whose elements might be contained inside of bigSet
44 * @returns {boolean} returns true if smallSet contains all elements inside of the bigSet
45 */
46const isSubset = (bigSet, smallSet) => {
47 if (bigSet.size < smallSet.size) return false;
48 for (const item of smallSet) {
49 if (!bigSet.has(item)) return false;
50 }
51 return true;
52};
53
54/**
55 * Returns found item.
56 * @template T
57 * @param {Set<T>} set a set
58 * @param {(set: T) => boolean} fn selector function
59 * @returns {T | undefined} found item
60 */
61const find = (set, fn) => {
62 for (const item of set) {
63 if (fn(item)) return item;
64 }
65};
66
67/**
68 * Returns first item.
69 * @template T
70 * @param {Set<T> | ReadonlySet<T>} set a set
71 * @returns {T | undefined} first item
72 */
73const first = (set) => {
74 const entry = set.values().next();
75 return entry.done ? undefined : entry.value;
76};
77
78/**
79 * Returns combined set, may be identical to a or b.
80 * @template T
81 * @param {Set<T>} a first
82 * @param {Set<T>} b second
83 * @returns {Set<T>} combined set, may be identical to a or b
84 */
85const combine = (a, b) => {
86 if (b.size === 0) return a;
87 if (a.size === 0) return b;
88 const set = new Set(a);
89 for (const item of b) set.add(item);
90 return set;
91};
92
93module.exports.combine = combine;
94module.exports.find = find;
95module.exports.first = first;
96module.exports.intersect = intersect;
97module.exports.isSubset = isSubset;
Note: See TracBrowser for help on using the repository browser.