@@ -76,11 +76,13 @@ bool GetNext()
7676 /// <summary>
7777 /// Enumerates all possible combinations of values.
7878 /// Results can be different permutations of another set.
79- /// Examples:
79+ ///
80+ /// Example:
8081 /// [0, 0], [0, 1], [1, 0], [1, 1] where [0, 1] and [1, 0] are a different permutatation of the same set.
8182 /// </summary>
8283 /// <param name="elements">The elements to draw from.</param>
8384 /// <param name="length">The length of each result.</param>
85+ /// <param name="buffer">An optional buffer that is filled with the values and returned as the yielded value instead of a new array</param>
8486 public static IEnumerable < T [ ] > Combinations < T > ( this IEnumerable < T > elements , int length , T [ ] ? buffer = null )
8587 {
8688 if ( elements is null )
@@ -97,12 +99,13 @@ public static IEnumerable<T[]> Combinations<T>(this IEnumerable<T> elements, int
9799 /// <summary>
98100 /// Enumerates all possible distinct set combinations.
99101 /// A set that has its items reordered is not distinct from the original.
100- /// Examples:
102+ ///
103+ /// Example:
101104 /// [0, 0], [0, 1], [1, 1] where [1, 0] is not included as it is not a disticnt set from [0, 1].
102- ///
103105 /// </summary>
104106 /// <param name="elements">The elements to draw from.</param>
105107 /// <param name="length">The length of each result.</param>
108+ /// <param name="buffer">An optional buffer that is filled with the values and returned as the yielded value instead of a new array</param>
106109 public static IEnumerable < T [ ] > CombinationsDistinct < T > ( this IEnumerable < T > elements , int length , T [ ] ? buffer = null )
107110 {
108111 if ( elements is null )
@@ -138,7 +141,6 @@ public static IEnumerable<T[]> Combinations<T>(this IEnumerable<T> elements, int
138141 /// </summary>
139142 /// <param name="elements">The elements to draw from.</param>
140143 /// <param name="length">The length of each result.</param>
141- /// <param name="uniqueOnly">Finds all possible subsets instead of all possible combinations of values.</param>
142144 public static IEnumerable < T [ ] > CombinationsBuffered < T > ( this IEnumerable < T > elements , int length )
143145 {
144146 if ( elements is null )
@@ -149,20 +151,54 @@ public static IEnumerable<T[]> CombinationsBuffered<T>(this IEnumerable<T> eleme
149151
150152 if ( length == 0 )
151153 {
152- return Enumerable . Empty < T [ ] > ( ) ;
154+ yield break ;
153155 }
154156
155- var arrayPool = ArrayPool < T > . Shared ;
156- var buffer = arrayPool . Rent ( length ) ;
157+ var pool = ArrayPool < T > . Shared ;
158+ var buffer = pool . Rent ( length ) ;
157159 try
158160 {
159- return Combinations ( elements , length , buffer ) ;
161+ foreach ( var c in Combinations ( elements , length , buffer ) )
162+ yield return c ;
160163 }
161164 finally
162165 {
163- arrayPool . Return ( buffer , true ) ;
166+ pool . Return ( buffer , true ) ;
164167 }
165168 }
166169
170+
171+ /// <summary>
172+ /// Enumerates all possible combinations of values.
173+ /// Results can be different permutations of another set.
174+ /// Examples:
175+ /// [0, 0], [0, 1], [1, 0], [1, 1] where [0, 1] and [1, 0] are a different permutatation of the same set.
176+ /// </summary>
177+ /// <param name="elements">The elements to draw from.</param>\
178+ public static IEnumerable < T [ ] > Combinations < T > ( this IEnumerable < T > elements )
179+ {
180+ if ( elements is null )
181+ throw new ArgumentNullException ( nameof ( elements ) ) ;
182+ Contract . EndContractBlock ( ) ;
183+ var source = elements as IReadOnlyList < T > ?? elements . ToArray ( ) ;
184+ return source . Count == 0 ? Enumerable . Empty < T [ ] > ( ) : Combinations ( source , source . Count ) ;
185+ }
186+
187+ /// <summary>
188+ /// Enumerates all possible distinct set combinations.
189+ /// A set that has its items reordered is not distinct from the original.
190+ /// Examples:
191+ /// [0, 0], [0, 1], [1, 1] where [1, 0] is not included as it is not a disticnt set from [0, 1].
192+ ///
193+ /// </summary>
194+ /// <param name="elements">The elements to draw from.</param>
195+ public static IEnumerable < T [ ] > CombinationsDistinct < T > ( this IEnumerable < T > elements )
196+ {
197+ if ( elements is null )
198+ throw new ArgumentNullException ( nameof ( elements ) ) ;
199+ Contract . EndContractBlock ( ) ;
200+ var source = elements as IReadOnlyList < T > ?? elements . ToArray ( ) ;
201+ return source . Count == 0 ? Enumerable . Empty < T [ ] > ( ) : CombinationsDistinct ( source , source . Count ) ;
202+ }
167203 }
168204}
0 commit comments