setSize.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createSetSize = void 0;
  6. var _array = require("../../utils/array.js");
  7. var _factory = require("../../utils/factory.js");
  8. var name = 'setSize';
  9. var dependencies = ['typed', 'compareNatural'];
  10. var createSetSize = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  11. var typed = _ref.typed,
  12. compareNatural = _ref.compareNatural;
  13. /**
  14. * Count the number of elements of a (multi)set. When a second parameter is 'true', count only the unique values.
  15. * A multi-dimension array will be converted to a single-dimension array before the operation.
  16. *
  17. * Syntax:
  18. *
  19. * math.setSize(set)
  20. * math.setSize(set, unique)
  21. *
  22. * Examples:
  23. *
  24. * math.setSize([1, 2, 2, 4]) // returns 4
  25. * math.setSize([1, 2, 2, 4], true) // returns 3
  26. *
  27. * See also:
  28. *
  29. * setUnion, setIntersect, setDifference
  30. *
  31. * @param {Array | Matrix} a A multiset
  32. * @return {number} The number of elements of the (multi)set
  33. */
  34. return typed(name, {
  35. 'Array | Matrix': function ArrayMatrix(a) {
  36. return Array.isArray(a) ? (0, _array.flatten)(a).length : (0, _array.flatten)(a.toArray()).length;
  37. },
  38. 'Array | Matrix, boolean': function ArrayMatrixBoolean(a, unique) {
  39. if (unique === false || a.length === 0) {
  40. return Array.isArray(a) ? (0, _array.flatten)(a).length : (0, _array.flatten)(a.toArray()).length;
  41. } else {
  42. var b = (0, _array.flatten)(Array.isArray(a) ? a : a.toArray()).sort(compareNatural);
  43. var count = 1;
  44. for (var i = 1; i < b.length; i++) {
  45. if (compareNatural(b[i], b[i - 1]) !== 0) {
  46. count++;
  47. }
  48. }
  49. return count;
  50. }
  51. }
  52. });
  53. });
  54. exports.createSetSize = createSetSize;