summaryrefslogtreecommitdiff
path: root/src/app/utils/set_utils.js
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2021-10-17 02:52:05 +0200
committerJules Laplace <julescarbon@gmail.com>2021-10-17 02:52:05 +0200
commit06ecdf2af182034496e2123852deee4a58de1043 (patch)
treec8d4eb9664dd368bee5a4bf73dd1e02015ecaf39 /src/app/utils/set_utils.js
making a shoebox
Diffstat (limited to 'src/app/utils/set_utils.js')
-rw-r--r--src/app/utils/set_utils.js81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/app/utils/set_utils.js b/src/app/utils/set_utils.js
new file mode 100644
index 0000000..88e8fea
--- /dev/null
+++ b/src/app/utils/set_utils.js
@@ -0,0 +1,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;
+}