|
| 1 | +using System; |
| 2 | +using System.Buffers; |
| 3 | +using System.Collections.Generic; |
| 4 | + |
| 5 | +namespace Open.Collections |
| 6 | +{ |
| 7 | + public static partial class Extensions |
| 8 | + { |
| 9 | + /// <summary> |
| 10 | + /// Progressively enumerates the possible (ordered) subsets of the list, limited by the provided count. |
| 11 | + /// The buffer is filled with the values and returned as the yielded value. |
| 12 | + /// Note: Works especially well when the source is a LazyList. |
| 13 | + /// </summary> |
| 14 | + /// <param name="source">The source list to derive from.</param> |
| 15 | + /// <param name="count">The maximum number of items in the result sets.</param> |
| 16 | + /// <param name="buffer"> |
| 17 | + /// A buffer to use instead of returning new arrays for each iteration. |
| 18 | + /// It must be at least the length of the count. |
| 19 | + /// </param> |
| 20 | + /// <returns>An enumerable containing the resultant subsets.</returns> |
| 21 | + public static IEnumerable<T[]> SubsetsProgressive<T>(this IReadOnlyList<T> source, int count, T[] buffer) |
| 22 | + { |
| 23 | + if (count < 1) |
| 24 | + throw new ArgumentOutOfRangeException(nameof(count), count, "Must greater than zero."); |
| 25 | + if (buffer is null) |
| 26 | + throw new ArgumentNullException(nameof(buffer)); |
| 27 | + if (buffer.Length < count) |
| 28 | + throw new ArgumentOutOfRangeException(nameof(buffer), buffer, "Length must be greater than or equal to the provided count."); |
| 29 | + |
| 30 | + if (count == 1) |
| 31 | + { |
| 32 | + foreach (var e in source) |
| 33 | + { |
| 34 | + buffer[0] = e; |
| 35 | + yield return buffer; |
| 36 | + } |
| 37 | + yield break; |
| 38 | + } |
| 39 | + |
| 40 | + var pool = ArrayPool<int>.Shared; |
| 41 | + var indices = pool.Rent(count); |
| 42 | + try |
| 43 | + { |
| 44 | + using var e = source.GetEnumerator(); |
| 45 | + |
| 46 | + // Setup the first result and make sure there's enough for the count. |
| 47 | + var n = 0; |
| 48 | + for (; n < count; ++n) |
| 49 | + { |
| 50 | + if (!e.MoveNext()) throw new ArgumentOutOfRangeException(nameof(count), count, "Is greater than the length of the source."); |
| 51 | + buffer[n] = e.Current; |
| 52 | + indices[n] = n; |
| 53 | + } |
| 54 | + |
| 55 | + // First result. |
| 56 | + yield return buffer; |
| 57 | + |
| 58 | + if (!e.MoveNext()) yield break; // Only one set. |
| 59 | + |
| 60 | + var lastSlot = count - 1; |
| 61 | + |
| 62 | + // Second result. |
| 63 | + buffer[lastSlot] = e.Current; |
| 64 | + yield return buffer; |
| 65 | + indices[lastSlot] = n; |
| 66 | + |
| 67 | + var nextToLastSlot = lastSlot - 1; |
| 68 | + |
| 69 | + loop: |
| 70 | + var prevIndex = n; |
| 71 | + var pos = nextToLastSlot; |
| 72 | + |
| 73 | + while (pos >= 0) |
| 74 | + { |
| 75 | + var firstRun = true; |
| 76 | + var index = indices[pos]; |
| 77 | + while (index + 1 < prevIndex) |
| 78 | + { |
| 79 | + // Subsequent results. |
| 80 | + buffer[pos] = source[++index]; |
| 81 | + indices[pos] = index; |
| 82 | + if (firstRun) |
| 83 | + { |
| 84 | + while (pos < nextToLastSlot && index + 1 < prevIndex) |
| 85 | + { |
| 86 | + buffer[++pos] = source[++index]; |
| 87 | + indices[pos] = index; |
| 88 | + } |
| 89 | + prevIndex = indices[pos + 1]; |
| 90 | + firstRun = false; |
| 91 | + } |
| 92 | + yield return buffer; |
| 93 | + } |
| 94 | + --pos; |
| 95 | + prevIndex = index; |
| 96 | + |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + if (!e.MoveNext()) yield break; |
| 101 | + |
| 102 | + // Update the last one. |
| 103 | + buffer[lastSlot] = e.Current; |
| 104 | + for (var i = 0; i < lastSlot; ++i) |
| 105 | + { |
| 106 | + buffer[i] = source[i]; |
| 107 | + indices[i] = i; |
| 108 | + } |
| 109 | + yield return buffer; |
| 110 | + indices[lastSlot] = ++n; |
| 111 | + |
| 112 | + goto loop; |
| 113 | + } |
| 114 | + finally |
| 115 | + { |
| 116 | + pool.Return(indices); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Enumerates the possible (ordered) subsets of the list, limited by the provided count. |
| 122 | + /// The yielded results are a buffer (array) that is at least the length of the provided count. |
| 123 | + /// NOTE: Do not retain the result array as it is returned to an array pool when complete. |
| 124 | + /// </summary> |
| 125 | + /// <param name="source">The source list to derive from.</param> |
| 126 | + /// <param name="count">The maximum number of items in the result sets.</param> |
| 127 | + /// <returns>An enumerable containing the resultant subsets as an buffer array.</returns> |
| 128 | + public static IEnumerable<ReadOnlyMemory<T>> SubsetsProgressiveBuffered<T>(this IReadOnlyList<T> source, int count) |
| 129 | + { |
| 130 | + var pool = ArrayPool<T>.Shared; |
| 131 | + var buffer = pool.Rent(count); |
| 132 | + var readBuffer = new ReadOnlyMemory<T>(buffer, 0, count); |
| 133 | + try |
| 134 | + { |
| 135 | + foreach (var _ in SubsetsProgressive(source, count, buffer)) |
| 136 | + yield return readBuffer; |
| 137 | + } |
| 138 | + finally |
| 139 | + { |
| 140 | + pool.Return(buffer, true); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Enumerates the possible (ordered) subsets of the list, limited by the provided count. |
| 147 | + /// A new array is created for each subset. |
| 148 | + /// </summary> |
| 149 | + /// <param name="source">The source list to derive from.</param> |
| 150 | + /// <param name="count">The maximum number of items in the result sets.</param> |
| 151 | + /// <returns>An enumerable containing the resultant subsets.</returns> |
| 152 | + public static IEnumerable<T[]> SubsetsProgressive<T>(this IReadOnlyList<T> source, int count) |
| 153 | + { |
| 154 | + foreach (var subset in SubsetsProgressiveBuffered(source, count)) |
| 155 | + yield return subset.ToArray(); |
| 156 | + } |
| 157 | + |
| 158 | + } |
| 159 | +} |
0 commit comments