simple-squiggle

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

setSize.js (1860B)


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