Post by Nimmi SrivastavKindly suggest the most elegant and efficient implementation of a
multi-column truth table.
Thanks,
Nimmi
One way to code truth tables is to use a tabular approach. I will
describe the implementation of "non-boolean" truth tables (if there is
such a thing), since the traditional truth tables are degenerate cases
of the "non-boolean" truth tables
The simplest implementation is to use a multi-dimensional array.
Suppose the truth table has 4 input columns. In this case you could
use a four-dimensional array. Of course this assumes that in each
column the indices are zero-based and contiguous. If that is not the
case, you can still use a multi-dimensional array, but instead of
using the input column entries directly as indices, you can create a
mapping function that maps the input column values to a zero-based
contiguous range and use the mapped values instead. Kindly note that
there is no restriction on the output column of the truth table.
As an alternative, you could use a single dimension array and compute
the correct index to use "manually". Let us assume that the truth
table has four columns: A, B, C and D (arranged most significant to
least significant). Further, let us assume that the range of values
in these columns is as follows:
A 0..a-1 (i.e. number of values equals 'a')
B 0..b-1 (i.e. number of values equals 'b')
C 0..c-1 (i.e. number of values equals 'c')
D 0..d-1 (i.e. number of values equals 'd')
A multi-dimensional array index of the type Table[i][j][k][l] can be
translated into an index into a one-dimensional array using the
following formula:
b*c*d*i + c*d*j + d*k + l
This looks somewhat complicated, but once you get a hang of it, it is
really quite trivial.
Hope that helps
--ZF