UNPKG

2.05 kBJavaScriptView Raw
1import { flatten, identify } from '../../utils/array.js';
2import { factory } from '../../utils/factory.js';
3var name = 'setIsSubset';
4var dependencies = ['typed', 'size', 'subset', 'compareNatural', 'Index'];
5export var createSetIsSubset = /* #__PURE__ */factory(name, dependencies, (_ref) => {
6 var {
7 typed,
8 size,
9 subset,
10 compareNatural,
11 Index
12 } = _ref;
13
14 /**
15 * Check whether a (multi)set is a subset of another (multi)set. (Every element of set1 is the element of set2.)
16 * Multi-dimension arrays will be converted to single-dimension arrays before the operation.
17 *
18 * Syntax:
19 *
20 * math.setIsSubset(set1, set2)
21 *
22 * Examples:
23 *
24 * math.setIsSubset([1, 2], [3, 4, 5, 6]) // returns false
25 * math.setIsSubset([3, 4], [3, 4, 5, 6]) // returns true
26 *
27 * See also:
28 *
29 * setUnion, setIntersect, setDifference
30 *
31 * @param {Array | Matrix} a1 A (multi)set
32 * @param {Array | Matrix} a2 A (multi)set
33 * @return {boolean} true | false
34 */
35 return typed(name, {
36 'Array | Matrix, Array | Matrix': function ArrayMatrixArrayMatrix(a1, a2) {
37 if (subset(size(a1), new Index(0)) === 0) {
38 // empty is a subset of anything
39 return true;
40 } else if (subset(size(a2), new Index(0)) === 0) {
41 // anything is not a subset of empty
42 return false;
43 }
44
45 var b1 = identify(flatten(Array.isArray(a1) ? a1 : a1.toArray()).sort(compareNatural));
46 var b2 = identify(flatten(Array.isArray(a2) ? a2 : a2.toArray()).sort(compareNatural));
47 var inb2;
48
49 for (var i = 0; i < b1.length; i++) {
50 inb2 = false;
51
52 for (var j = 0; j < b2.length; j++) {
53 if (compareNatural(b1[i].value, b2[j].value) === 0 && b1[i].identifier === b2[j].identifier) {
54 // the identifier is always a decimal int
55 inb2 = true;
56 break;
57 }
58 }
59
60 if (inb2 === false) {
61 return false;
62 }
63 }
64
65 return true;
66 }
67 });
68});
\No newline at end of file