@@ -6,26 +6,26 @@ namespace Open.Collections
66{
77 public static partial class Extensions
88 {
9+
910 /// <summary>
10- /// Iteratively updates the buffer with the possible subsets.
11+ /// Enumerates the possible (ordered) subsets.
12+ /// If a buffer is supplied, the buffer is filled with the values and returned as the yielded value.
13+ /// If no buffer is supplised, a new array is created for each subset.
1114 /// </summary>
12- /// <returns>An enumerable that yields the buffer.</returns>
13- public static IEnumerable < T [ ] > SubsetsBuffered < T > ( this IReadOnlyList < T > source , int count , T [ ] buffer )
15+ public static IEnumerable < T [ ] > Subsets < T > ( this IReadOnlyList < T > source , int count , T [ ] ? buffer = null )
1416 {
1517 if ( count < 1 )
1618 throw new ArgumentOutOfRangeException ( nameof ( count ) , count , "Must greater than zero." ) ;
1719 if ( count > source . Count )
1820 throw new ArgumentOutOfRangeException ( nameof ( count ) , count , "Must be less than or equal to the length of the source set." ) ;
19- if ( buffer is null )
20- throw new ArgumentNullException ( nameof ( buffer ) ) ;
21-
2221
2322 if ( count == 1 )
2423 {
24+ var result = buffer ?? new T [ count ] ;
2525 foreach ( var e in source )
2626 {
27- buffer [ 0 ] = e ;
28- yield return buffer ;
27+ result [ 0 ] = e ;
28+ yield return result ;
2929 }
3030 yield break ;
3131 }
@@ -35,12 +35,13 @@ public static IEnumerable<T[]> SubsetsBuffered<T>(this IReadOnlyList<T> source,
3535 {
3636 for ( int pos = 0 , index = 0 ; ; )
3737 {
38+ var result = buffer ?? new T [ count ] ;
3839 for ( ; pos < count ; pos ++ , index ++ )
3940 {
4041 indices [ pos ] = index ;
41- buffer [ pos ] = source [ index ] ;
42+ result [ pos ] = source [ index ] ;
4243 }
43- yield return buffer ;
44+ yield return result ;
4445 do
4546 {
4647 if ( pos == 0 ) yield break ;
@@ -66,33 +67,12 @@ public static IEnumerable<T[]> SubsetsBuffered<T>(this IReadOnlyList<T> source,
6667
6768 try
6869 {
69- return SubsetsBuffered ( source , count , buffer ) ;
70+ return Subsets ( source , count , buffer ) ;
7071 }
7172 finally
7273 {
73- pool . Return ( buffer ) ;
74+ pool . Return ( buffer , true ) ;
7475 }
7576 }
76-
77- /// <summary>
78- /// Iteratively updates the provided buffer with the possible subsets of the length of the buffer.
79- /// </summary>
80- /// <returns>An enumerable that yields a buffer that is at least the length of the count provided.</returns>
81- public static IEnumerable < T [ ] > SubsetsBuffered < T > ( this IReadOnlyList < T > source , T [ ] buffer )
82- => SubsetsBuffered ( source , buffer . Length , buffer ) ;
83-
84-
85- /// <summary>
86- /// Iteratively returns the possible subsets of the source.
87- /// </summary>
88- /// <returns>An enumerable that yields each subset.</returns>
89- public static IEnumerable < T [ ] > Subsets < T > ( this IReadOnlyList < T > source , int count )
90- {
91- foreach ( var s in SubsetsBuffered ( source , count ) )
92- {
93- yield return s . AsCopy ( count ) ;
94- }
95- }
96-
9777 }
9878}
0 commit comments