UNPKG

xen-dev-utils

Version:

Utility functions used by the Scale Workshop ecosystem

80 lines (79 loc) 3.27 kB
/** * https://gist.github.com/axelpale/3118596 * * Copyright 2012 Akseli Palén. * Created 2012-07-15. * Types added by Lumi Pakkanen on 2022-05-04 * Licensed under the MIT license. * * <license> * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files * (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * </license> * * Implements functions to calculate combinations of elements in JS Arrays. * * Functions: * kCombinations(set, k) -- Return all non-empty k-sized combinations in a set * combinations(set) -- Return all non-empty combinations of the set */ /** * K-combinations * * Examples: * ```ts * kCombinations([1, 2, 3], 1) // [[1], [2], [3]] * kCombinations([1, 2, 3], 2) // [[1, 2], [1, 3], [2, 3]] * kCombinations([1, 2, 3], 3) // [[1, 2, 3]] * kCombinations([1, 2, 3], 4) // [] * kCombinations([1, 2, 3], 0) // [] * kCombinations([1, 2, 3], -1) // [] * kCombinations([], 0) // [] * ``` * @param set Array of objects of any type. They are treated as unique. * @param k Size of combinations to search for. * @returns Array of found non-empty combinations, size of a combination is k. */ export declare function kCombinations<T>(set: T[], k: number): T[][]; /** * Get all possible combinations of elements in a set. * * Examples: * ```ts * combinations([1, 2, 3]) // [[1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]] * combinations([1]) // [[1]] * ``` * @param set Array of objects of any type. They are treated as unique. * @returns Array of arrays representing all possible non-empty combinations of elements in a set. */ export declare function combinations<T>(set: T[]): T[][]; /** * K-combinations * @param set Array of objects of any type. They are treated as unique. * @param k Size of combinations to search for. * @returns Generator of found combinations, size of a combination is k. */ export declare function iterKCombinations<T>(set: T[], k: number): Generator<T[], number, undefined>; /** * Get all possible combinations of elements in a set. * @param set Array of objects of any type. They are treated as unique. * @returns Generator of arrays representing all possible non-empty combinations of elements in a set. */ export declare function iterCombinations<T>(set: T[]): Generator<T[], number, undefined>;