setIsSubset.js 2.3 KB

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