|
| 1 | +using System; |
| 2 | +using System.Buffers; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Diagnostics.Contracts; |
| 6 | +using System.Linq; |
| 7 | + |
| 8 | +namespace Open.Collections |
| 9 | +{ |
| 10 | + public static partial class Extensions |
| 11 | + { |
| 12 | + static IEnumerable<T[]> CombinationsCore<T>(IReadOnlyList<T> source, int length, bool distinctSet, T[]? buffer = null) |
| 13 | + { |
| 14 | + Debug.Assert(length != 0); |
| 15 | + var count = source.Count; |
| 16 | + Debug.Assert(count != 0); |
| 17 | + |
| 18 | + { |
| 19 | + var value = source[0]; |
| 20 | + var result = buffer ?? new T[length]; |
| 21 | + for (var i = 0; i < length; i++) result[i] = value; |
| 22 | + yield return result; |
| 23 | + if (count == 1) yield break; |
| 24 | + } |
| 25 | + |
| 26 | + var pool = ArrayPool<int>.Shared; |
| 27 | + var indexes = pool.Rent(length); |
| 28 | + try |
| 29 | + { |
| 30 | + |
| 31 | + for (var i = 0; i < length; i++) indexes[i] = 0; |
| 32 | + |
| 33 | + var lastIndex = length - 1; |
| 34 | + bool GetNext() |
| 35 | + { |
| 36 | + int i; |
| 37 | + for (i = lastIndex; i >= 0; --i) |
| 38 | + { |
| 39 | + var e = ++indexes[i]; |
| 40 | + if (count == e) |
| 41 | + { |
| 42 | + if (i == 0) return false; |
| 43 | + } |
| 44 | + else |
| 45 | + { |
| 46 | + if (i == lastIndex) return true; |
| 47 | + else break; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + for (++i; i < length; ++i) |
| 52 | + { |
| 53 | + if (indexes[i] == count) |
| 54 | + indexes[i] = distinctSet ? indexes[i - 1] : 0; |
| 55 | + } |
| 56 | + |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + while (GetNext()) |
| 61 | + { |
| 62 | + var result = buffer ?? new T[length]; |
| 63 | + for (var i = 0; i < length; i++) |
| 64 | + { |
| 65 | + result[i] = source[indexes[i]]; |
| 66 | + } |
| 67 | + yield return result; |
| 68 | + } |
| 69 | + } |
| 70 | + finally |
| 71 | + { |
| 72 | + pool.Return(indexes); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Enumerates all possible combinations of values. |
| 78 | + /// Results can be different permutations of another set. |
| 79 | + /// Examples: |
| 80 | + /// [0, 0], [0, 1], [1, 0], [1, 1] where [0, 1] and [1, 0] are a different permutatation of the same set. |
| 81 | + /// </summary> |
| 82 | + /// <param name="elements">The elements to draw from.</param> |
| 83 | + /// <param name="length">The length of each result.</param> |
| 84 | + public static IEnumerable<T[]> Combinations<T>(this IEnumerable<T> elements, int length, T[]? buffer = null) |
| 85 | + { |
| 86 | + if (elements is null) |
| 87 | + throw new ArgumentNullException(nameof(elements)); |
| 88 | + if (length < 0) |
| 89 | + throw new ArgumentOutOfRangeException(nameof(length), length, "Cannot be less than zero."); |
| 90 | + Contract.EndContractBlock(); |
| 91 | + |
| 92 | + if (length == 0) return Enumerable.Empty<T[]>(); |
| 93 | + var source = elements as IReadOnlyList<T> ?? elements.ToArray(); |
| 94 | + return source.Count == 0 ? Enumerable.Empty<T[]>() : CombinationsCore(source, length, false, buffer); |
| 95 | + } |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Enumerates all possible distinct set combinations. |
| 99 | + /// A set that has its items reordered is not distinct from the original. |
| 100 | + /// Examples: |
| 101 | + /// [0, 0], [0, 1], [1, 1] where [1, 0] is not included as it is not a disticnt set from [0, 1]. |
| 102 | + /// |
| 103 | + /// </summary> |
| 104 | + /// <param name="elements">The elements to draw from.</param> |
| 105 | + /// <param name="length">The length of each result.</param> |
| 106 | + public static IEnumerable<T[]> CombinationsDistinct<T>(this IEnumerable<T> elements, int length, T[]? buffer = null) |
| 107 | + { |
| 108 | + if (elements is null) |
| 109 | + throw new ArgumentNullException(nameof(elements)); |
| 110 | + if (length < 0) |
| 111 | + throw new ArgumentOutOfRangeException(nameof(length), length, "Cannot be less than zero."); |
| 112 | + Contract.EndContractBlock(); |
| 113 | + |
| 114 | + if (length == 0) return Enumerable.Empty<T[]>(); |
| 115 | + var source = elements as IReadOnlyList<T> ?? elements.ToArray(); |
| 116 | + return source.Count == 0 ? Enumerable.Empty<T[]>() : CombinationsCore(source, length, true, buffer); |
| 117 | + } |
| 118 | + |
| 119 | + [Obsolete("Deprecated in favor of using .Subsets(length) or .Combinations(length) depending on intent.")] |
| 120 | + public static IEnumerable<T[]> Combinations<T>(this IEnumerable<T> elements, int length, bool uniqueOnly) |
| 121 | + { |
| 122 | + if (elements is null) |
| 123 | + throw new ArgumentNullException(nameof(elements)); |
| 124 | + if (length < 0) |
| 125 | + throw new ArgumentOutOfRangeException(nameof(length), length, "Cannot be less than zero."); |
| 126 | + Contract.EndContractBlock(); |
| 127 | + |
| 128 | + if (length == 0) return Enumerable.Empty<T[]>(); |
| 129 | + var source = elements as IReadOnlyList<T> ?? elements.ToArray(); |
| 130 | + var count = source.Count; |
| 131 | + if (count == 0) return Enumerable.Empty<T[]>(); |
| 132 | + |
| 133 | + return uniqueOnly ? source.Subsets(length) : CombinationsCore(source, length, true); |
| 134 | + } |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// Enumerates all possible (order retained) combinations of the source elements up to the length. |
| 138 | + /// </summary> |
| 139 | + /// <param name="elements">The elements to draw from.</param> |
| 140 | + /// <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> |
| 142 | + public static IEnumerable<T[]> CombinationsBuffered<T>(this IEnumerable<T> elements, int length) |
| 143 | + { |
| 144 | + if (elements is null) |
| 145 | + throw new ArgumentNullException(nameof(elements)); |
| 146 | + if (length < 0) |
| 147 | + throw new ArgumentOutOfRangeException(nameof(length), length, "Cannot be less than zero."); |
| 148 | + Contract.EndContractBlock(); |
| 149 | + |
| 150 | + if (length == 0) |
| 151 | + { |
| 152 | + return Enumerable.Empty<T[]>(); |
| 153 | + } |
| 154 | + |
| 155 | + var arrayPool = ArrayPool<T>.Shared; |
| 156 | + var buffer = arrayPool.Rent(length); |
| 157 | + try |
| 158 | + { |
| 159 | + return Combinations(elements, length, buffer); |
| 160 | + } |
| 161 | + finally |
| 162 | + { |
| 163 | + arrayPool.Return(buffer, true); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + } |
| 168 | +} |
0 commit comments