Refactor `EnumerableExtensions` to simplify combination generation by removing `Range` method and streamlining index logic
This commit is contained in:
parent
ee061757a8
commit
56521d39d7
|
|
@ -12,29 +12,24 @@ public static class EnumerableExtensions
|
||||||
var poolLength = pool.Length;
|
var poolLength = pool.Length;
|
||||||
if (count > poolLength)
|
if (count > poolLength)
|
||||||
yield break;
|
yield break;
|
||||||
var indices = Range(0, count).ToArray();
|
var indices = Enumerable.Range(0, count).ToArray();
|
||||||
yield return GetCombination(indices, pool);
|
yield return GetCombination(indices, pool);
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
var validIndex = false;
|
int idx;
|
||||||
var currentIndex = 0;
|
for(idx = count - 1; idx >= 0; idx--)
|
||||||
for(var i = count - 1; i >= 0; i--)
|
|
||||||
{
|
{
|
||||||
currentIndex = i;
|
var isIndexBelowMax = indices[idx] != idx + poolLength - count;
|
||||||
if (indices[i] != i + poolLength - count)
|
if (isIndexBelowMax)
|
||||||
{
|
|
||||||
validIndex = true;
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if(!validIndex)
|
if(idx < 0)
|
||||||
yield break;
|
yield break;
|
||||||
|
|
||||||
indices[currentIndex] += 1;
|
indices[idx] += 1;
|
||||||
foreach (var j in Range(currentIndex + 1, count))
|
for (var j = idx + 1; j < count; j++)
|
||||||
{
|
{
|
||||||
Index ix = j - 1;
|
indices[j] = indices[j - 1] + 1;
|
||||||
indices[j] = indices[ix] + 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
yield return GetCombination(indices, pool);
|
yield return GetCombination(indices, pool);
|
||||||
|
|
@ -42,11 +37,5 @@ public static class EnumerableExtensions
|
||||||
|
|
||||||
static TValue[] GetCombination(int[] innerIndices, TValue[] innerPool) =>
|
static TValue[] GetCombination(int[] innerIndices, TValue[] innerPool) =>
|
||||||
innerIndices.Select(i => innerPool[i]).ToArray();
|
innerIndices.Select(i => innerPool[i]).ToArray();
|
||||||
|
|
||||||
static IEnumerable<int> Range(int start, int end)
|
|
||||||
{
|
|
||||||
for (var i = start; i < end; i++)
|
|
||||||
yield return i;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue