Ejemplo n.º 1
0
        /// <summary> Returns all combinations of the specified elements of a given size. </summary>
        /// <typeparam name="T"> The elements of the sequence. </typeparam>
        /// <param name="elements"> The elements to combine. Each element is assumed to be unique. </param>
        /// <param name="combinationSize"> The number of elements to choose per combination. </param>
        public static IEnumerable <T[]> AllCombinations <T>(this IReadOnlyList <T> elements, int combinationSize)
        {
            Contract.Requires(elements != null);
            Contract.Requires(combinationSize > 0);

            if (elements.Count < combinationSize)
            {
                return(EmptyCollection <T[]> .Enumerable);
            }

            var ranges = EnumerableExtensions.AllCombinationsInRange(elements.Count, combinationSize);

            EnsureSingleEnumerationDEBUG(ref ranges);
            return(ranges.Select(indices => indices.Map(i => elements[i])));
        }