@math-x-ts/generix
Version:
This library tasks is to generate combinations and permutations
106 lines • 5.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Class to generate combinations with & without repetition.
*/
var Combination = /** @class */ (function () {
function 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 (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++) {
currentCombination.push(elements[i]);
var combinations = Combination.generateWithoutRepetition(elements, i + 1, currentCombination, n, m);
result.push.apply(result, 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 (elements, n, m) {
if (n === void 0) { n = elements.length; }
if (m === void 0) { m = 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 (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 i = startIndex; i < elements.length; i++) {
currentIndexes[index] = i;
result.push.apply(result, Combination.generateWithRepetition(elements, currentIndexes, n, index + 1, i));
}
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 (elements, n, m) {
if (n === void 0) { n = elements.length; }
if (m === void 0) { m = n; }
var result = [];
for (var i = n; i <= m; i++) {
var currentIndexes = [];
for (var j = 0; j < i; j++) {
currentIndexes.push(0);
}
result.push.apply(result, Combination.generateWithRepetition(elements, currentIndexes, i, 0, 0));
}
return result;
};
return Combination;
}());
exports.default = Combination;
//# sourceMappingURL=Combination.js.map