setUnion.js (1855B)
1 "use strict"; 2 3 Object.defineProperty(exports, "__esModule", { 4 value: true 5 }); 6 exports.createSetUnion = void 0; 7 8 var _array = require("../../utils/array.js"); 9 10 var _factory = require("../../utils/factory.js"); 11 12 var name = 'setUnion'; 13 var dependencies = ['typed', 'size', 'concat', 'subset', 'setIntersect', 'setSymDifference', 'Index']; 14 var createSetUnion = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) { 15 var typed = _ref.typed, 16 size = _ref.size, 17 concat = _ref.concat, 18 subset = _ref.subset, 19 setIntersect = _ref.setIntersect, 20 setSymDifference = _ref.setSymDifference, 21 Index = _ref.Index; 22 23 /** 24 * Create the union of two (multi)sets. 25 * Multi-dimension arrays will be converted to single-dimension arrays before the operation. 26 * 27 * Syntax: 28 * 29 * math.setUnion(set1, set2) 30 * 31 * Examples: 32 * 33 * math.setUnion([1, 2, 3, 4], [3, 4, 5, 6]) // returns [1, 2, 3, 4, 5, 6] 34 * math.setUnion([[1, 2], [3, 4]], [[3, 4], [5, 6]]) // returns [1, 2, 3, 4, 5, 6] 35 * 36 * See also: 37 * 38 * setIntersect, setDifference 39 * 40 * @param {Array | Matrix} a1 A (multi)set 41 * @param {Array | Matrix} a2 A (multi)set 42 * @return {Array | Matrix} The union of two (multi)sets 43 */ 44 return typed(name, { 45 'Array | Matrix, Array | Matrix': function ArrayMatrixArrayMatrix(a1, a2) { 46 if (subset(size(a1), new Index(0)) === 0) { 47 // if any of them is empty, return the other one 48 return (0, _array.flatten)(a2); 49 } else if (subset(size(a2), new Index(0)) === 0) { 50 return (0, _array.flatten)(a1); 51 } 52 53 var b1 = (0, _array.flatten)(a1); 54 var b2 = (0, _array.flatten)(a2); 55 return concat(setSymDifference(b1, b2), setIntersect(b1, b2)); 56 } 57 }); 58 }); 59 exports.createSetUnion = createSetUnion;