UNPKG

@math-x-ts/generix

Version:

This library tasks is to generate combinations and permutations

360 lines (350 loc) 17.7 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(); else if(typeof define === 'function' && define.amd) define([], factory); else if(typeof exports === 'object') exports["MathXGenerix"] = factory(); else root["MathXGenerix"] = factory(); })(this, () => { return /******/ (() => { // webpackBootstrap /******/ "use strict"; /******/ // The require scope /******/ var __webpack_require__ = {}; /******/ /************************************************************************/ /******/ /* webpack/runtime/define property getters */ /******/ (() => { /******/ // define getter functions for harmony exports /******/ __webpack_require__.d = (exports, definition) => { /******/ for(var key in definition) { /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); /******/ } /******/ } /******/ }; /******/ })(); /******/ /******/ /* webpack/runtime/hasOwnProperty shorthand */ /******/ (() => { /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) /******/ })(); /******/ /******/ /* webpack/runtime/make namespace object */ /******/ (() => { /******/ // define __esModule on exports /******/ __webpack_require__.r = (exports) => { /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); /******/ } /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ })(); /******/ /************************************************************************/ var __webpack_exports__ = {}; // ESM COMPAT FLAG __webpack_require__.r(__webpack_exports__); // EXPORTS __webpack_require__.d(__webpack_exports__, { "Combination": () => (/* reexport */ Combination), "Permutation": () => (/* reexport */ Permutation) }); ;// CONCATENATED MODULE: ./src/Combination.ts /** * Class to generate combinations with & without repetition. */ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i]; return arr2; } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } var Combination = /*#__PURE__*/ function() { "use strict"; function Combination() { _classCallCheck(this, Combination); } /** * The concrete implementation of the combination without repetition algorithm. * * @template S - The type of the elements in the input array. * @param {Array<S>} elements - The input array containing the elements to generate combination for. * @param {number} startIndex - The starting index for. * @param {Array<Array<S>>} currentCombination - The accumulated combination during the recursion. * @param {number} n - The minimum length of the generated combination (inclusive). * @param {number} m - The maximum length of the generated combination (inclusive). * * @returns {Array<Array<S>>} - An array of arrays, where each inner array is a combination of the input elements. */ Combination.generateWithoutRepetition = function generateWithoutRepetition(elements, startIndex, currentCombination, n, m) { var result = []; if (currentCombination.length >= n && currentCombination.length <= m) { result.push(currentCombination.slice()); } if (currentCombination.length >= m) { return result; } for(var i = startIndex; i < elements.length; i++){ var _result; currentCombination.push(elements[i]); var combinations = Combination.generateWithoutRepetition(elements, i + 1, currentCombination, n, m); (_result = result).push.apply(_result, _toConsumableArray(combinations)); currentCombination.pop(); } return result; }; /** * Generates all possible combinations without repetition of a given array of elements, with the option to specify * the minimum (n) and maximum (m) lengths of the generated permutations. * * @template S - The type of the elements in the input array. * @param {Array<S>} elements - The input array containing the elements to generate combination for. * @param {number} [n=elements.length] - The minimum length of the generated combination (inclusive). * @param {number} [m=n] - The maximum length of the generated combination (inclusive). * @returns {Array<Array<S>>} - An array of arrays, where each inner array is a combination of the input elements. */ Combination.withoutRepetition = function withoutRepetition(elements) { var n = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : elements.length, m = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : n; var result = Combination.generateWithoutRepetition(elements, 0, [], n, m); return result.sort(function(a, b) { return a.length - b.length; }); }; /** * The concrete implementation of the combination with repetition algorithm. * * @template S - The type of the elements in the input array. * @param {Array<S>} elements - The input array containing the elements to generate combination for. * @param {Array<number>} currentIndexes - The combination indexes accumulated during recursion. * @param {number} n - The minimum length of the generated combination (inclusive). * @param {number} index - The current index, initially this should be 0. * @param {number} startIndex - The starting index from where the combination should begin. * * @returns {Array<Array<S>>} - An array of arrays, where each inner array is a combination of the input elements. */ Combination.generateWithRepetition = function generateWithRepetition(elements, currentIndexes, n, index, startIndex) { var result = []; if (index === n) { var combo = []; for(var i = 0; i < n; i++){ combo.push(elements[currentIndexes[i]]); } return [ combo ]; } for(var i1 = startIndex; i1 < elements.length; i1++){ var _result; currentIndexes[index] = i1; (_result = result).push.apply(_result, _toConsumableArray(Combination.generateWithRepetition(elements, currentIndexes, n, index + 1, i1))); } return result; }; /** * Generates all possible combinations with repetition of a given array of elements, with the option to specify * the minimum (n) and maximum (m) lengths of the generated permutations. * * @template S - The type of the elements in the input array. * @param {Array<S>} elements - The input array containing the elements to generate combination for. * @param {number} [n=elements.length] - The minimum length of the generated combination (inclusive). * @param {number} [m=n] - The maximum length of the generated combination (inclusive). * @returns {Array<Array<S>>} - An array of arrays, where each inner array is a combination of the input elements. */ Combination.withRepetition = function withRepetition(elements) { var n = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : elements.length, m = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : n; var result = []; for(var i = n; i <= m; i++){ var _result; var currentIndexes = []; for(var j = 0; j < i; j++){ currentIndexes.push(0); } (_result = result).push.apply(_result, _toConsumableArray(Combination.generateWithRepetition(elements, currentIndexes, i, 0, 0))); } return result; }; return Combination; }(); ;// CONCATENATED MODULE: ./src/Permutation.ts /** * Class to generate permutations with & without repetition. */ function Permutation_arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i]; return arr2; } function Permutation_arrayWithoutHoles(arr) { if (Array.isArray(arr)) return Permutation_arrayLikeToArray(arr); } function Permutation_classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function Permutation_iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function Permutation_nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function Permutation_toConsumableArray(arr) { return Permutation_arrayWithoutHoles(arr) || Permutation_iterableToArray(arr) || Permutation_unsupportedIterableToArray(arr) || Permutation_nonIterableSpread(); } function Permutation_unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return Permutation_arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return Permutation_arrayLikeToArray(o, minLen); } var Permutation = /*#__PURE__*/ function() { "use strict"; function Permutation() { Permutation_classCallCheck(this, Permutation); } /** * Generates all possible permutations without repetition of a given array of elements, * with the option to specify the minimum (n) and maximum (m) lengths of the generated permutations. * The maximum must be less or equal to the number of elements. * * @template S - The type of the elements in the input array. * @param {Array<S>} elements - The input array containing the elements to generate permutations for. * @param {number} [n=elements.length] - The minimum number of elements in the generated permutations (inclusive). * @param {number} [m=n] - The maximum number of elements in the generated permutations (inclusive). * @param {Array<S>} [combo=[]] - An optional array to store the current combination during recursion. * Should not be provided when calling the function initially. * * @returns {Array<Array<S>>} - An array of arrays, where each inner array is a permutation of the input elements. */ Permutation.generateWithoutRepetition = function generateWithoutRepetition(elements) { var n = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : elements.length, m = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : n, combo = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : []; var result = []; for(var i = 0; i < elements.length; i++){ var element = elements[i]; var rest = Permutation_toConsumableArray(elements.slice(0, i)).concat(Permutation_toConsumableArray(elements.slice(i + 1, elements.length))); combo.push(element); if (combo.length >= n && combo.length <= m) { result.push(Permutation_toConsumableArray(combo)); } if (combo.length <= m) { var _result; (_result = result).push.apply(_result, Permutation_toConsumableArray(Permutation.generateWithoutRepetition(rest, n, m, combo))); } combo.pop(); } return result; }; /** * Generates all possible permutations without repetition of a given array of elements, with the option to specify * the minimum (n) and maximum (m) lengths of the generated permutations. * * @template S - The type of the elements in the input array. * @param {Array<S>} elements - The input array containing the elements to generate permutations for. * @param {number} [n=elements.length] - The minimum length of the generated permutations (inclusive). * @param {number} [m=n] - The maximum length of the generated permutations (inclusive). * @returns {Array<Array<S>>} - An array of arrays, where each inner array is a permutation of the input elements. */ Permutation.withoutRepetition = function withoutRepetition(elements) { var n = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : elements.length, m = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : n; var result = Permutation.generateWithoutRepetition(elements, n, m, []); return result.sort(function(a, b) { return a.length - b.length; }); }; /** * Generates all possible permutations with repetition of a given array of elements, * with the option to specify the minimum (n) and maximum (m) lengths of the generated permutations. * * @template S The type of elements in the input array and resulting permutations. * @param {Array<S>} elements A set of elements used during the generation process. * @param {number} [n=elements.length] The minimum number of elements in the generated permutations (inclusive). * @param {number} [m=n] The maximum number of elements in the generated permutations (inclusive). * * @returns {Array<Array<S>>} An array of arrays containing all the possible permutations. * * @example * // Generate permutations with repetition for the elements 'a', 'b', and 'c', with a size between 2 and 3 * const result = Permutations.withRepetition(['a', 'b', 'c'], 2, 3); * [['a', 'a'], ['a', 'b'], ['a', 'c'], ['b', 'b'], ['b', 'c'], ['c', 'c'], ['a', 'a', 'a'], ['a', 'a', 'b'], ...] */ Permutation.withRepetition = function withRepetition(elements) { var n = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : elements.length, m = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : n; var combinations = []; var result = []; var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; try { for(var _iterator = elements[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){ var s = _step.value; combinations.push([ s ]); if (n <= 1) { result.push([ s ]); } } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally{ try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally{ if (_didIteratorError) { throw _iteratorError; } } } var startIndex = 0; var endIndex = combinations.length; for(var i = 0; i < m - 1; i++){ for(var j = startIndex; j < endIndex; j++){ var _combinations; var c = combinations[j]; var newCombos = []; for(var k = 0; k < elements.length; k++){ newCombos.push(Permutation_toConsumableArray(c).concat([ elements[k] ])); if (c.length + 1 >= n && c.length - 1 <= m) { result.push(Permutation_toConsumableArray(c).concat([ elements[k] ])); } } (_combinations = combinations).push.apply(_combinations, Permutation_toConsumableArray(newCombos)); } startIndex = endIndex; endIndex = combinations.length; } return result; }; return Permutation; }(); ;// CONCATENATED MODULE: ./src/index.ts /******/ return __webpack_exports__; /******/ })() ; });