simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

setIsSubset.js (2327B)


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