mathjs
Version:
Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with dif
51 lines • 1.71 kB
JavaScript
import { flatten } from '../../utils/array.js';
import { factory } from '../../utils/factory.js';
var name = 'setSize';
var dependencies = ['typed', 'compareNatural'];
export var createSetSize = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
compareNatural
} = _ref;
/**
* Count the number of elements of a (multi)set. When a second parameter is 'true', count only the unique values.
* A multi-dimension array will be converted to a single-dimension array before the operation.
*
* Syntax:
*
* math.setSize(set)
* math.setSize(set, unique)
*
* Examples:
*
* math.setSize([1, 2, 2, 4]) // returns 4
* math.setSize([1, 2, 2, 4], true) // returns 3
*
* See also:
*
* setUnion, setIntersect, setDifference
*
* @param {Array | Matrix} a A multiset
* @param {boolean} [unique] If true, only the unique values are counted. False by default
* @return {number} The number of elements of the (multi)set
*/
return typed(name, {
'Array | Matrix': function Array__Matrix(a) {
return Array.isArray(a) ? flatten(a).length : flatten(a.toArray()).length;
},
'Array | Matrix, boolean': function Array__Matrix_boolean(a, unique) {
if (unique === false || a.length === 0) {
return Array.isArray(a) ? flatten(a).length : flatten(a.toArray()).length;
} else {
var b = flatten(Array.isArray(a) ? a : a.toArray()).sort(compareNatural);
var count = 1;
for (var i = 1; i < b.length; i++) {
if (compareNatural(b[i], b[i - 1]) !== 0) {
count++;
}
}
return count;
}
}
});
});