-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExtensions.Subsets.cs
226 lines (201 loc) · 7.12 KB
/
Extensions.Subsets.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
namespace Open.Collections;
public static partial class Extensions
{
/// <param name="source">The source list to derive from.</param>
/// <param name="count">The maximum number of items in the result sets.</param>
/// <param name="buffer">
/// A buffer to use instead of returning new arrays for each iteration.
/// It must be at least the length of the count.
/// </param>
/// <inheritdoc cref="Subsets{T}(IReadOnlyList{T}, int)"/>
public static IEnumerable<Memory<T>> Subsets<T>(this IReadOnlyList<T> source, int count, Memory<T> buffer)
{
if (source is null)
throw new ArgumentNullException(nameof(source));
if (count < 1)
throw new ArgumentOutOfRangeException(nameof(count), count, "Must greater than zero.");
if (count > source.Count)
throw new ArgumentOutOfRangeException(nameof(count), count, "Must be less than or equal to the length of the source set.");
if (buffer.Length < count)
throw new ArgumentOutOfRangeException(nameof(buffer), buffer, "Length must be greater than or equal to the provided count.");
Contract.EndContractBlock();
return SubsetsCore(source, count, buffer);
static IEnumerable<Memory<T>> SubsetsCore(IReadOnlyList<T> source, int count, Memory<T> buffer)
{
if (count == 1)
{
foreach (T? e in source)
{
buffer.Span[0] = e;
yield return buffer;
}
yield break;
}
// Using an ArrayPool in this manner instead of a MemoryPool does use a few more bytes but is also slightly faster.
// The result is faster enough to justify using this method.
int diff = source.Count - count;
ArrayPool<int>? pool = count > 128 ? ArrayPool<int>.Shared : null;
int[]? indices = pool?.Rent(count) ?? new int[count];
try
{
int pos = 0;
int index = 0;
loop:
var span = buffer.Span;
while (pos < count)
{
indices[pos] = index;
span[pos] = source[index];
++pos;
++index;
}
yield return buffer;
do
{
if (pos == 0) yield break;
index = indices[--pos] + 1;
}
while (index > diff + pos);
goto loop;
}
finally
{
pool?.Return(indices);
}
}
}
/// <inheritdoc cref="Subsets{T}(IReadOnlyList{T}, int)"/>
/// <remarks>Values are yielded as read only memory buffer that should not be retained as its array is returned to pool afterwards.</remarks>
/// <returns>An enumerable containing the resultant subsets as a memory buffer.</returns>
public static IEnumerable<ReadOnlyMemory<T>> SubsetsBuffered<T>(this IReadOnlyList<T> source, int count)
{
using var lease = MemoryPool<T>.Shared.Rent(count);
Memory<T> buffer = lease.Memory.Slice(0, count);
ReadOnlyMemory<T> readBuffer = buffer;
foreach (Memory<T> _ in Subsets(source, count, buffer))
yield return readBuffer;
}
/// <summary>
/// Enumerates the possible (ordered) subsets of the list, limited by the provided count.
/// </summary>
/// <param name="source">The source list to derive from.</param>
/// <param name="count">The maximum number of items in the result sets.</param>
/// <returns>An enumerable containing the resultant subsets.</returns>
public static IEnumerable<T[]> Subsets<T>(this IReadOnlyList<T> source, int count)
{
using var lease = MemoryPool<T>.Shared.Rent(count);
Memory<T> buffer = lease.Memory.Slice(0, count);
foreach (Memory<T> _ in Subsets(source, count, buffer))
{
var a = new T[count];
buffer.CopyTo(a);
yield return a;
}
}
/// <param name="source">The source list to derive from.</param>
/// <param name="count">The maximum number of items in the result sets.</param>
/// <param name="pool">The array pool to get result arrays from.</param>
/// <param name="clearArray"><see langword="true"/>, clears the pooled array when finished; otherwise is left alone.</param>
/// <inheritdoc cref="Subsets{T}(IReadOnlyList{T}, int)" />
public static IEnumerable<ArrayPoolSegment<T>> Subsets<T>(this IReadOnlyList<T> source, int count, ArrayPool<T>? pool, bool clearArray = false)
{
foreach (ReadOnlyMemory<T> subset in SubsetsBuffered(source, count))
{
var a = new ArrayPoolSegment<T>(count, pool, clearArray);
subset.CopyTo(a);
yield return a;
}
}
/// <inheritdoc cref="Subsets{T}(IReadOnlyList{T}, int, Memory{T})" />
public static IEnumerable<Memory<T>> Subsets<T>(this ReadOnlyMemory<T> source, int count, Memory<T> buffer)
{
if (count < 1)
throw new ArgumentOutOfRangeException(nameof(count), count, "Must greater than zero.");
if (count > source.Length)
throw new ArgumentOutOfRangeException(nameof(count), count, "Must be less than or equal to the length of the source set.");
if (buffer.Length < count)
throw new ArgumentOutOfRangeException(nameof(buffer), buffer, "Length must be greater than or equal to the provided count.");
Contract.EndContractBlock();
return SubsetsCore(source, count, buffer);
static IEnumerable<Memory<T>> SubsetsCore(ReadOnlyMemory<T> source, int count, Memory<T> buffer)
{
if (count == 1)
{
int len = source.Length;
for (int i = 0; i < len; ++i)
{
buffer.Span[0] = source.Span[i];
yield return buffer;
}
yield break;
}
// Using an ArrayPool in this manner instead of a MemoryPool does use a few more bytes but is also slightly faster.
// The result is faster enough to justify using this method.
int diff = source.Length - count;
ArrayPool<int>? pool = count > 128 ? ArrayPool<int>.Shared : null;
int[]? indices = pool?.Rent(count) ?? new int[count];
try
{
int pos = 0;
int index = 0;
loop:
var span = buffer.Span;
var sourceSpan = source.Span;
while (pos < count)
{
indices[pos] = index;
span[pos] = sourceSpan[index];
++pos;
++index;
}
yield return buffer;
do
{
if (pos == 0) yield break;
index = indices[--pos] + 1;
}
while (index > diff + pos);
goto loop;
}
finally
{
pool?.Return(indices);
}
}
}
/// <inheritdoc cref="Subsets{T}(IReadOnlyList{T}, int)"/>
public static IEnumerable<ReadOnlyMemory<T>> SubsetsBuffered<T>(this ReadOnlyMemory<T> source, int count)
{
using var lease = MemoryPool<T>.Shared.Rent(count);
Memory<T> buffer = lease.Memory.Slice(0, count);
ReadOnlyMemory<T> readBuffer = buffer;
foreach (Memory<T> _ in Subsets(source, count, buffer))
yield return readBuffer;
}
/// <inheritdoc cref="Subsets{T}(IReadOnlyList{T}, int)"/>
public static IEnumerable<T[]> Subsets<T>(this ReadOnlyMemory<T> source, int count)
{
using var lease = MemoryPool<T>.Shared.Rent(count);
Memory<T> buffer = lease.Memory.Slice(0, count);
foreach (Memory<T> _ in Subsets(source, count, buffer))
{
var a = new T[count];
buffer.CopyTo(a);
yield return a;
}
}
/// <inheritdoc cref="Subsets{T}(IReadOnlyList{T}, int, ArrayPool{T}?, bool)"/>
public static IEnumerable<ArrayPoolSegment<T>> Subsets<T>(this ReadOnlyMemory<T> source, int count, ArrayPool<T>? pool, bool clearArray = false)
{
foreach (ReadOnlyMemory<T> subset in SubsetsBuffered(source, count))
{
var a = new ArrayPoolSegment<T>(count, pool, clearArray);
subset.CopyTo(a);
yield return a;
}
}
}