truthTable

Generates a truth table from a list of runtime enum values.

The table is an array of booleans, where each index corresponds to an enum value in the input list. The boolean at each index is true if the value is in the input list, and false otherwise.

Can be used during compile-time. If Yes.fullEnumRange is passed, the returned table will be a static array sized to accommodate the highest value in the enum. If No.fullEnumRange is passed, the returned table will be a dynamic one sized to accommodate the highest value in the input list.

If no fullEnumRange argument is passed, the function call resolves to the overload that takes compile-time numbers instead.

Note: This is not a sparse array and will be as large as requested. Additionally it is stack-allocated if Yes.fullEnumRange is passed, so be mindful of the size of the enum.

  1. auto truthTable(Numbers numbers)
  2. auto truthTable(Enums values)
    truthTable
    (
    Flag!"fullEnumRange" fullEnumRange
    Enums...
    )
    (
    Enums values
    )
    if (
    Enums.length &&
    allSameType!Enums
    &&
    is(Enums[0] == enum)
    &&
    is(Enums[0] : size_t)
    )
  3. auto truthTable()
  4. auto truthTable()

Parameters

fullEnumRange

Whether to generate a truth table for the full enum range.

values Enums

The enum values to generate a truth table from.

Return Value

Type: auto

A truth table as an array of booleans.

Examples

enum E { a, b, c, d, e, f }

const table = truthTable(E.b, E.c, E.d);
assert(table.length == 4);
assert(table == [ false, true, true, true]);
assert(table == [ 0, 1, 1, 1 ]);

assert(!table[E.a]);
assert( table[E.b]);
assert( table[E.c]);
assert( table[E.d]);

const staticTable = truthTable!(Yes.fullEnumRange)(E.b, E.c);
assert(staticTable.length == 6);
assert(staticTable == [ false, true, true, false, false, false ]);
assert(staticTable == [ 0, 1, 1, 0, 0, 0 ]);
import lu.traits : UnqualArray;
import std.conv : to;

enum E { a, b, c, d, e }

{
    static immutable table = truthTable!(Yes.fullEnumRange)(E.b, E.d, E.c, E.b);
    static assert(is(typeof(table) : bool[5]), typeof(table).stringof);
    static assert((table == [ false, true, true, true, false ]), table.to!string);
    static assert((table == [ 0, 1, 1, 1, 0 ]), table.to!string);

    static assert(!table[E.a]);
    static assert( table[E.b]);
    static assert( table[E.c]);
    static assert( table[E.d]);
    static assert(!table[E.e]);
}
{
    static immutable table = truthTable!(No.fullEnumRange)(E.a, E.b);
    alias T = UnqualArray!(typeof(table));
    static assert(is(T == bool[]), T.stringof);
    static assert((table.length == 2), table.length.to!string);
    static assert((table == [ true, true ]), table.to!string);
    static assert((table == [ 1, 1 ]), table.to!string);

    static assert(table[E.a]);
    static assert(table[E.b]);
}