2022-08-30 16:13:11 +05:00
|
|
|
/* This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2022 Streetwriters (Private) Limited
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-01-31 00:24:21 +05:00
|
|
|
// SetManipulator MIT Licence © 2016 Edwin Monk-Fromont http://github.com/edmofro
|
|
|
|
|
// Based on setOps.js MIT License © 2014 James Abney http://github.com/jabney
|
|
|
|
|
|
|
|
|
|
// Set operations union, intersection, symmetric difference,
|
|
|
|
|
// relative complement, equals. Set operations are fast.
|
2021-10-29 14:05:11 +05:00
|
|
|
export class SetManipulator {
|
|
|
|
|
constructor() {}
|
2020-01-31 00:24:21 +05:00
|
|
|
|
|
|
|
|
// Processes a histogram consructed from two arrays, 'a' and 'b'.
|
|
|
|
|
// This function is used generically by the below set operation
|
|
|
|
|
// methods, a.k.a, 'evaluators', to return some subset of
|
|
|
|
|
// a set union, based on frequencies in the histogram.
|
2021-10-29 14:05:11 +05:00
|
|
|
process(a, b, getKey = (k) => k, evaluator) {
|
2020-01-31 00:24:21 +05:00
|
|
|
// If identity extractor passed in, push it on the stack
|
2020-04-07 19:05:10 +05:00
|
|
|
//if (identityExtractor) this.pushIdentityExtractor(identityExtractor);
|
2020-01-31 00:24:21 +05:00
|
|
|
// Create a histogram of 'a'.
|
|
|
|
|
const hist = {};
|
|
|
|
|
const out = [];
|
|
|
|
|
let ukey;
|
2020-04-07 19:05:10 +05:00
|
|
|
a.forEach((value) => {
|
2021-10-29 14:05:11 +05:00
|
|
|
ukey = getKey(value);
|
2020-01-31 00:24:21 +05:00
|
|
|
if (!hist[ukey]) {
|
|
|
|
|
hist[ukey] = { value: value, freq: 1 };
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// Merge 'b' into the histogram.
|
2020-04-07 19:05:10 +05:00
|
|
|
b.forEach((value) => {
|
2021-10-29 14:05:11 +05:00
|
|
|
ukey = getKey(value);
|
2020-01-31 00:24:21 +05:00
|
|
|
if (hist[ukey]) {
|
|
|
|
|
if (hist[ukey].freq === 1) hist[ukey].freq = 3;
|
|
|
|
|
} else hist[ukey] = { value: value, freq: 2 };
|
|
|
|
|
});
|
|
|
|
|
// Pop any new identity extractor
|
2020-04-07 19:05:10 +05:00
|
|
|
//if (identityExtractor) this.popIdentityExtractor(identityExtractor);
|
2020-01-31 00:24:21 +05:00
|
|
|
// Call the given evaluator.
|
|
|
|
|
if (evaluator) {
|
|
|
|
|
for (const key in hist) {
|
2020-04-07 19:05:10 +05:00
|
|
|
//if (!hist.hasOwnProperty(key)) continue; // Property from object prototype, skip
|
2020-01-31 00:24:21 +05:00
|
|
|
if (evaluator(hist[key].freq)) out.push(hist[key].value);
|
|
|
|
|
}
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
return hist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Join two sets together.
|
|
|
|
|
// Set.union([1, 2, 2], [2, 3]) => [1, 2, 3]
|
2021-10-29 14:05:11 +05:00
|
|
|
union(a, b, getKey) {
|
|
|
|
|
return this.process(a, b, getKey, () => true);
|
2020-01-31 00:24:21 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return items common to both sets.
|
|
|
|
|
// Set.intersection([1, 1, 2], [2, 2, 3]) => [2]
|
2020-04-07 19:05:10 +05:00
|
|
|
intersection(a, b) {
|
2021-10-30 13:48:36 +05:00
|
|
|
return this.process(a, b, undefined, (freq) => freq === 3);
|
2020-01-31 00:24:21 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Symmetric difference. Items from either set that
|
|
|
|
|
// are not in both sets.
|
|
|
|
|
// Set.difference([1, 1, 2], [2, 3, 3]) => [1, 3]
|
2020-04-07 19:05:10 +05:00
|
|
|
difference(a, b) {
|
2021-10-30 13:48:36 +05:00
|
|
|
return this.process(a, b, undefined, (freq) => freq < 3);
|
2020-01-31 00:24:21 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Relative complement. Items from 'a' which are
|
|
|
|
|
// not also in 'b'.
|
|
|
|
|
// Set.complement([1, 2, 2], [2, 2, 3]) => [3]
|
2020-04-07 19:05:10 +05:00
|
|
|
complement(a, b) {
|
2021-10-30 13:48:36 +05:00
|
|
|
return this.process(a, b, undefined, (freq) => freq === 1);
|
2020-01-31 00:24:21 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns true if both sets are equivalent, false otherwise.
|
|
|
|
|
// Set.equals([1, 1, 2], [1, 2, 2]) => true
|
|
|
|
|
// Set.equals([1, 1, 2], [1, 2, 3]) => false
|
2020-04-07 19:05:10 +05:00
|
|
|
equals(a, b) {
|
2020-01-31 00:24:21 +05:00
|
|
|
let max = 0;
|
|
|
|
|
let min = Math.pow(2, 53);
|
2020-04-07 19:05:10 +05:00
|
|
|
const hist = this.process(a, b);
|
2020-01-31 00:24:21 +05:00
|
|
|
for (const key in hist) {
|
2020-04-07 19:05:10 +05:00
|
|
|
// if (!hist.hasOwnProperty(key)) continue; // Property from object prototype, skip
|
2020-01-31 00:24:21 +05:00
|
|
|
max = Math.max(max, hist[key].freq);
|
|
|
|
|
min = Math.min(min, hist[key].freq);
|
|
|
|
|
}
|
|
|
|
|
return min === 3 && max === 3;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const setManipulator = new SetManipulator();
|
|
|
|
|
export default setManipulator;
|