truthTable

Generates a truth table from a list of runtime numbers.

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

Can be used during compile-time. Produces a dynamic array unless a highestValueOverride is provided, in which case it will be a static array sized to accommodate that value.

If the list of numbers is known at compile-time, there is also an overload that, given the numbers as template arguments, also produces a static array.

Note: This is not a sparse array and will be as large as requested.

  1. auto truthTable(Numbers numbers)
    truthTable
    (
    int highestValueOverride = 0
    Numbers...
    )
    (
    Numbers numbers
    )
    if (
    Numbers.length &&
    allSatisfy!(isImplicitlyConvertibleToSize_t, Numbers)
    )
  2. auto truthTable(Enums values)
  3. auto truthTable()
  4. auto truthTable()

Parameters

highestValueOverride

(Optional) The highest value the truth table should size itself to accommodate for. If not set, the highest value in the input list is used.

numbers Numbers

The numbers to generate a truth table from.

Return Value

Type: auto

A truth table as an array of booleans.

Throws

ArrayException if a negative number is passed, or if a highestValueOverride is set and a number is out of bounds.

Examples

const table = truthTable(1, 3, 5);
assert(table.length == 6);
assert(table == [ false, true, false, true, false, true ]);
assert(table == [ 0, 1, 0, 1, 0, 1 ]);

assert(!table[0]);
assert( table[1]);
assert(!table[2]);
assert( table[3]);
assert(!table[4]);
assert( table[5]);

const staticTable = truthTable!5(1, 2, 3);
assert(staticTable.length == 6);
assert(staticTable == [ true, true, true, false, false, false ]);
assert(staticTable == [ 1, 1, 1, 0, 0, 0 ]);
import lu.traits : UnqualArray;
import std.conv : to;

{
    static immutable table = truthTable(1, 3, 5);
    alias T = UnqualArray!(typeof(table));
    static assert(is(T == bool[]), T.stringof);
    static assert((table.length == 6), table.length.to!string);
    static assert((table == [ false, true, false, true, false, true ]), table.to!string);
    static assert((table == [ 0, 1, 0, 1, 0, 1 ]), table.to!string);

    static assert(!table[0]);
    static assert( table[1]);
    static assert(!table[2]);
    static assert( table[3]);
    static assert(!table[4]);
    static assert( table[5]);
}
{
    static immutable table = truthTable!5(1, 2, 3);
    static assert(is(typeof(table) : bool[6]), typeof(table).stringof);
    static assert((table == [ false, true, true, true, false, false ]), table.to!string);
    static assert((table == [ 0, 1, 1, 1, 0, 0 ]), table.to!string);

    static assert(!table[0]);
    static assert( table[1]);
    static assert( table[2]);
    static assert( table[3]);
    static assert(!table[4]);
    static assert(!table[5]);
}