setDistinct.js (1866B)
1 "use strict"; 2 3 Object.defineProperty(exports, "__esModule", { 4 value: true 5 }); 6 exports.createSetDistinct = void 0; 7 8 var _array = require("../../utils/array.js"); 9 10 var _factory = require("../../utils/factory.js"); 11 12 var name = 'setDistinct'; 13 var dependencies = ['typed', 'size', 'subset', 'compareNatural', 'Index', 'DenseMatrix']; 14 var createSetDistinct = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) { 15 var typed = _ref.typed, 16 size = _ref.size, 17 subset = _ref.subset, 18 compareNatural = _ref.compareNatural, 19 Index = _ref.Index, 20 DenseMatrix = _ref.DenseMatrix; 21 22 /** 23 * Collect the distinct elements of a multiset. 24 * A multi-dimension array will be converted to a single-dimension array before the operation. 25 * 26 * Syntax: 27 * 28 * math.setDistinct(set) 29 * 30 * Examples: 31 * 32 * math.setDistinct([1, 1, 1, 2, 2, 3]) // returns [1, 2, 3] 33 * 34 * See also: 35 * 36 * setMultiplicity 37 * 38 * @param {Array | Matrix} a A multiset 39 * @return {Array | Matrix} A set containing the distinc elements of the multiset 40 */ 41 return typed(name, { 42 'Array | Matrix': function ArrayMatrix(a) { 43 var result; 44 45 if (subset(size(a), new Index(0)) === 0) { 46 // if empty, return empty 47 result = []; 48 } else { 49 var b = (0, _array.flatten)(Array.isArray(a) ? a : a.toArray()).sort(compareNatural); 50 result = []; 51 result.push(b[0]); 52 53 for (var i = 1; i < b.length; i++) { 54 if (compareNatural(b[i], b[i - 1]) !== 0) { 55 result.push(b[i]); 56 } 57 } 58 } // return an array, if the input was an array 59 60 61 if (Array.isArray(a)) { 62 return result; 63 } // return a matrix otherwise 64 65 66 return new DenseMatrix(result); 67 } 68 }); 69 }); 70 exports.createSetDistinct = createSetDistinct;