summaryrefslogtreecommitdiff
path: root/src/app/utils/set_utils.js
blob: 88e8feaf8f65711d6cfeecf11dcbc6975d59f4ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
 * Operations on sets.
 * @module app/utils/set_utils
 */

/**
 * Determine if `set` contains `subset`
 * @param  {Set}  set       the superset
 * @param  {Set}  subset    the subset
 * @return {Boolean}        true if set contains subset
 */
export function isSuperset(set, subset) {
  for (let elem of subset) {
    if (!set.has(elem)) {
      return false;
    }
  }
  return true;
}

/**
 * Return the union (A or B) of two sets
 * @param  {Set}  setA      a set
 * @param  {Set}  setB      a set
 * @return {Boolean}        the union of the sets
 */
export function union(setA, setB) {
  let _union = new Set(setA);
  for (let elem of setB) {
    _union.add(elem);
  }
  return _union;
}

/**
 * Return the intersection (A and B) of two sets
 * @param  {Set}  setA      a set
 * @param  {Set}  setB      a set
 * @return {Boolean}        the intersection of the sets
 */
export function intersection(setA, setB) {
  let _intersection = new Set();
  for (let elem of setB) {
    if (setA.has(elem)) {
      _intersection.add(elem);
    }
  }
  return _intersection;
}

/**
 * Return the symmetric difference (A xor B) of two sets
 * @param  {Set}  setA      a set
 * @param  {Set}  setB      a set
 * @return {Boolean}        the symmetric difference of the sets
 */
export function symmetricDifference(setA, setB) {
  let _difference = new Set(setA);
  for (let elem of setB) {
    if (_difference.has(elem)) {
      _difference.delete(elem);
    } else {
      _difference.add(elem);
    }
  }
  return _difference;
}

/**
 * Return the difference (A not B) of two sets
 * @param  {Set}  setA      a set
 * @param  {Set}  setB      a set
 * @return {Boolean}        the difference of the sets
 */
export function difference(setA, setB) {
  let _difference = new Set(setA);
  for (let elem of setB) {
    _difference.delete(elem);
  }
  return _difference;
}