パワーセットについて説明しているように思えます
私の個人的なライブラリの周りに横たわっていた実装は次のとおりです。
// Helper method to count set bits in an integer
public static int CountBits(int n)
{
int count = 0;
while (n != 0)
{
count++;
n &= (n - 1);
}
return count;
}
public static IEnumerable<IEnumerable<T>> PowerSet<T>(
IEnumerable<T> src,
int minSetSize = 0,
int maxSetSize = int.MaxValue)
{
// we want fast random access to the source, so we'll
// need to ToArray() it
var cached = src.ToArray();
var setSize = Math.Pow(2, cached.Length);
for(int i=0; i < setSize; i++)
{
var subSetSize = CountBits(i);
if(subSetSize < minSetSize ||
subSetSize > maxSetSize)
{
continue;
}
T[] set = new T[subSetSize];
var temp = i;
var srcIdx = 0;
var dstIdx = 0;
while(temp > 0)
{
if((temp & 0x01) == 1)
{
set[dstIdx++] = cached[srcIdx];
}
temp >>= 1;
srcIdx++;
}
yield return set;
}
yield break;
}
そして、簡単なテスト リグ:
void Main()
{
var src = "ABCDEF";
var combos = PowerSet(src, 3, 6);
// hairy joins for great prettiness
Console.WriteLine(
string.Join(" , ",
combos.Select(subset =>
string.Concat("[",
string.Join(",", subset) , "]")))
);
}
出力:
[A,B,C] , [A,B,D] , [A,C,D] , [B,C,D] , [A,B,C,D] , [A,B,E] , [A,C,E] , [B,C,E] , [A,B,C,E] ,
[A,D,E] , [B,D,E] , [A,B,D,E] , [C,D,E] , [A,C,D,E] , [B,C,D,E] , [A,B,C,D,E] , [A,B,F] ,
[A,C,F] , [B,C,F] , [A,B,C,F] , [A,D,F] , [B,D,F] , [A,B,D,F] , [C,D,F] , [A,C,D,F] ,
[B,C,D,F] , [A,B,C,D,F] , [A,E,F] , [B,E,F] , [A,B,E,F] , [C,E,F] , [A,C,E,F] , [B,C,E,F] ,
[A,B,C,E,F] , [D,E,F] , [A,D,E,F] , [B,D,E,F] , [A,B,D,E,F] , [C,D,E,F] , [A,C,D,E,F] ,
[B,C,D,E,F] , [A,B,C,D,E,F]