|
| 1 | +using System; |
| 2 | +using System.Buffers; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Collections.Immutable; |
| 5 | + |
| 6 | +namespace Open.Collections |
| 7 | +{ |
| 8 | + public static class Subsets |
| 9 | + { |
| 10 | + internal static IEnumerable<int[]> IndexesInternal(int sourceLength, int subsetLength, int[] buffer) |
| 11 | + { |
| 12 | + if (subsetLength == 1) |
| 13 | + { |
| 14 | + for (var i = 0; i < sourceLength; ++i) |
| 15 | + { |
| 16 | + buffer[0] = i; |
| 17 | + yield return buffer; |
| 18 | + } |
| 19 | + yield break; |
| 20 | + } |
| 21 | + |
| 22 | + var diff = sourceLength - subsetLength; |
| 23 | + |
| 24 | + var pos = 0; |
| 25 | + var index = 0; |
| 26 | + |
| 27 | + loop: |
| 28 | + while (pos < subsetLength) |
| 29 | + { |
| 30 | + buffer[pos] = index; |
| 31 | + ++pos; |
| 32 | + ++index; |
| 33 | + } |
| 34 | + |
| 35 | + yield return buffer; |
| 36 | + |
| 37 | + do |
| 38 | + { |
| 39 | + if (pos == 0) yield break; |
| 40 | + index = buffer[--pos] + 1; |
| 41 | + } |
| 42 | + while (index > diff + pos); |
| 43 | + |
| 44 | + goto loop; |
| 45 | + } |
| 46 | + |
| 47 | + /// <inheritdoc cref="Indexes(int, int)"/> |
| 48 | + /// <param name="buffer"> |
| 49 | + /// A buffer to use instead of returning new arrays for each iteration. |
| 50 | + /// It must be at least the length of the count. |
| 51 | + /// </param> |
| 52 | + public static IEnumerable<int[]> Indexes(int sourceLength, int subsetLength, int[] buffer) |
| 53 | + { |
| 54 | + |
| 55 | + if (subsetLength < 1) |
| 56 | + throw new ArgumentOutOfRangeException(nameof(subsetLength), subsetLength, "Must greater than zero."); |
| 57 | + if (subsetLength > sourceLength) |
| 58 | + throw new ArgumentOutOfRangeException(nameof(subsetLength), subsetLength, "Must be less than or equal to the length of the source set."); |
| 59 | + if (buffer is null) |
| 60 | + throw new ArgumentNullException(nameof(buffer)); |
| 61 | + if (buffer.Length < subsetLength) |
| 62 | + throw new ArgumentOutOfRangeException(nameof(buffer), buffer, "Length must be greater than or equal to the provided count."); |
| 63 | + |
| 64 | + return IndexesInternal(sourceLength, subsetLength, buffer); |
| 65 | + } |
| 66 | + |
| 67 | + /// <inheritdoc cref="Indexes(int, int)"/> |
| 68 | + /// <remarks>Values are yielded as read only memory buffer that should not be retained as its array is returned to pool afterwards.</remarks> |
| 69 | + public static IEnumerable<ReadOnlyMemory<int>> IndexesBuffered(int sourceLength, int subsetLength) |
| 70 | + { |
| 71 | + var pool = ArrayPool<int>.Shared; |
| 72 | + var buffer = pool.Rent(subsetLength); |
| 73 | + var readBuffer = new ReadOnlyMemory<int>(buffer, 0, subsetLength); |
| 74 | + try |
| 75 | + { |
| 76 | + foreach (var _ in Indexes(sourceLength, subsetLength, buffer)) |
| 77 | + yield return readBuffer; |
| 78 | + } |
| 79 | + finally |
| 80 | + { |
| 81 | + pool.Return(buffer, true); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Enumerates all the possible subset indexes for a given source set length and subset length. |
| 87 | + /// </summary> |
| 88 | + /// <param name="sourceLength">The length of the source set.</param> |
| 89 | + /// <param name="subsetLength">The size of the desired subsets.</param>s |
| 90 | + public static IEnumerable<int[]> Indexes(int sourceLength, int subsetLength) |
| 91 | + { |
| 92 | + foreach (var i in IndexesBuffered(sourceLength, subsetLength)) |
| 93 | + yield return i.ToArray(); |
| 94 | + } |
| 95 | + |
| 96 | + /// <inheritdoc cref="Indexes(int, int)"/> |
| 97 | + public static IEnumerable<ImmutableArray<int>> IndexesImmutable(int sourceLength, int subsetLength) |
| 98 | + { |
| 99 | + foreach (var i in IndexesBuffered(sourceLength, subsetLength)) |
| 100 | + yield return i.ToImmutableArray(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments