UNPKG

@math-x-ts/generix

Version:

This library tasks is to generate combinations and permutations

54 lines (53 loc) 3.36 kB
/** * Class to generate combinations with & without repetition. */ export default class 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. */ private static generateWithoutRepetition; /** * 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. */ static withoutRepetition<S>(elements: S[], n?: number, m?: number): S[][]; /** * 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. */ static generateWithRepetition<S>(elements: S[], currentIndexes: number[], n: number, index: number, startIndex: number): S[][]; /** * 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. */ static withRepetition<S>(elements: S[], n?: number, m?: number): S[][]; }