General pattern for implementing truth tables

Is there a good general template for implementing truth tables?

I recycle some inherited code (C ++) and just realized that the function I'm working with comes down to a truth table with three binary inputs and 8 possible outputs. The following is an example of two of eight tests and their respective outputs:

// - + + if ( (prevdst5 < 0.0) && (dst5 > 0.0) && (nextdst5 > 0.0) ){ thawpct = (dst5 / (dst5 - prevdst5)); } // - - + if ( (prevdst5 < 0.0) && (dst5 < 0.0) && (nextdst5 > 0.0) ){ thawpct = (nextdst5 / (nextdst5 - dst5)); } // other cases... return thawpct; 

Basically, I wonder if there is a cleaner, more convenient, or extensible installation method.

  • What if another input is added? Then the number of required if statements should be 16, which, in my opinion, would be too cumbersome to manage the current template.
  • What if several input combos should be displayed on one output?

* The code base is an ecosystem model used in academia, so maintaining and expanding is equivalent to similar things depending on the perspective of the encoder.

+7
source share
3 answers
 int condition = ( prev >= 0 ? ( 1<<0 ) : 0 ) + ( cur >= 0 ? ( 1<<1 ) : 0 ) + ( next >= 0 ? ( 1<<2 ) : 0 ); switch (condition) { case 0: // - - - case 1: // + - - case 2: // - + - case 3: // + + - case 4: // - - + case 5: // + - + case 6: // - + + case 7: // + + + } 
+8
source

An array is a table.

The sequence of Boolean truth values ​​is a binary number.

Arrays are indexed by number.

Sooooo .....

You can define a function for each calculation:

 // - + + == 0 1 1 == 3 inline double f3(double prev, double curr, double next) { return curr / (curr - prev); } // - - + == 0 0 1 == 1 inline double f1(double prev, double curr, double next) { return next / (next - curr); } // ... 

Then declare an array of function pointers:

 typedef double (*func_type)(double, double, double); func_type funcs[8] = { f1, f2, // ... }; 

Then index into the array using logical conditions in the form of binary digits:

 func_type f = funcs[ (int(prevdst5 < 0.0)<<2) | (int(dst5 < 0.0)<<1) | int(nextdst5 > 0.0) ]; thawpct = f(prevdst5, dst5, nextdst5); 
  • What if another input is added? (Then the number of tests will be 16, which, in my opinion, will be cumbersome to manage with the current template)

You double the size of the array and add f8 , f9 , etc., if you need different calculations, but you only add a new test once to the array index:

 func_type f = funcs[ (cond<<3) | (int(prevdst5 < 0.0)<<2) | (int(dst5 < 0.0)<<1) | int(nextdst5 > 0.0) ]; 
  • What if several input combos should be displayed on one output?

Keep the same pointer to several elements of the array:

 func_type funcs[8] = { f1, f2, f3, f1, f1, // ... }; 
+4
source

You can save a mapping of 3 (or n ) to the corresponding function that needs to be executed. Then loop the function on this map depending on the state of the true / false states and do it.

Thus, you will have a 3-dimensional map (with size 2 in all dimensions)

0
source

All Articles