次のように宣言された 2 次元配列を考えてみましょう。
#include <stdbool.h>
bool array[N1][N2];
この配列の各行がtrue
同じ位置に正確に 1 つの値を持っているかどうかを知る必要があります。
たとえば、次は問題ありません。
{
{ 1, 0, 1, 0 },
{ 1, 0, 0, 1 },
{ 0, 0, 1, 1 }
}
これは正しくありません:
{
{ 1, 0, 1, 0 },
{ 1, 0, 1, 0 },
{ 0, 0, 1, 1 }
}
私はこれを試しました:
static uintmax_t hash(const bool *t, size_t n)
{
uintmax_t retv = 0U;
for (size_t i = 0; i < n; ++i)
if (t[i] == true)
retv |= 1 << i;
return retv;
}
static int is_valid(bool n)
{
return n != 0 && (n & (n - 1)) == 0;
}
bool check(bool t[N1][N2])
{
uintmax_t thash[N1];
for (size_t i = 0; i < N1; ++i)
thash[i] = hash(t[i], N2);
for (size_t i = 0; i < N1; ++i)
for (size_t j = 0; j < N1; ++j)
if (i != j && !is_valid(thash[i] & thash[j]))
return 0;
return 1;
}
しかし、それはでのみ機能しN1 <= sizeof(uintmax_t) * CHAR_BIT
ます。それを解決するための最良の方法を知っていますか?