More efficient alternative to compare booleans?

Started by
11 comments, last by slicksk8te 11 years, 6 months ago
Say I have many booleans, for simplicity sake I will called them A, B, C and D.

so my code if:
[source lang="cpp"]if(A && B && C && D)
{
doSomething();
}
else if(A && B && C)//but NOT D
{
doSomethingElse();
}
else if(A && B && D)//but NOT C
{
doSomethingElseElse();
}
else if(B && C)//but NOT D or A[/source]
and this will continue onward for all possiblites (what if only A is true, none are true etc etc etc).


I know a switch statement could be used but that'd be just as long.

Is there a better way of doing this?
Advertisement

Is there a better way of doing this?


Yes =>


I know a switch statement could be used


or, depending on the language you are using, you can use a hashmap (int->function), where int is the binary representation of your conditions.
If you have 16 possibilities you can't get away with less than 16 "ifs" or switch cases, if that's what you mean. Sometimes it's appropriate to define an enum that lists all possibilities, and use the switch statement:

switch (convert_to_enum(A, B, C, D))
{
case <case1>: ...;
case <case2>: ...;
...
}


Or, as Ashaman73 says, adopt the functional approach and define a function (or hash map) that gives you the function to be called.

openwar - the real-time tactical war-game platform

Or you can use bit flags.
const unsigned int BitA = 1 << 0;
const unsigned int BitB = 1 << 1;
...
if(value == (BitA | BitB)) ...
if(value == (BitA | BitB | BitC)) ...
Also you can make some hash map to dispatch the function invoke based on the bit value.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

If you have 16 distinct pieces of code similar to doSomething, which for the sake of this thread I assume is several lines of code and have hardly anything in common, then you cant exactly shorten it much. Most times though, I would expect that there is a lot of commonality between cases that can be taken advantage of.

Other than that, you can convert the booleans to bit-flags:
i.e.static const int AIsSet = 1;
static const int BIsSet = 2;
static const int CIsSet = 4;
static const int DIsSet = 8;

int choice = 0;
if (A) choice += AIsSet;
if (B) choice += BIsSet;
if (C) choice += CIsSet;
if (D) choice += DIsSet;

switch (choice) {
case AIsSet + BIsSet + CIsSet + DIsSet:
doSomething();
break;
case AIsSet + BIsSet + CIsSet:
doSomethingElse();
break;
case AIsSet + BIsSet + DIsSet:
doSomethingElseElse();
break;
case BIsSet + CIsSet:
doAnotherThing();
break;
};
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Thank you for all these excellent suggestions, looks like I'm not getting away with shorter coding! tongue.png

I think I like the sound of

you can convert the booleans to bit-flags:
as this seems like the best way for what I'm doing.

EDIT: another thought, is there an easy way to work out all the possibilities such as A+B+C+D, A+B+C? Because at the moment my program may end up having to compare many many variables like this.

another thought, is there an easy way to work out all the possibilities such as A+B+C+D, A+B+C? Because at the moment my program may end up having to compare many many variables like this.

If you have four boolean states, and each state corresponds to a bit in an integer, then all possible combinations are just the bit patterns of the numbers from 0 to 2[sup]4[/sup]-1=15.

for(int i=0; i<16; ++i) {
std::cout << "pattern " << i << " = " << ((i&8) ? "D" : "") << ((i&4) ? "C" : "") << ((i&2) ? "B" : "") << ((i&1) ? "A" : "") << std::endl;
}

Extend to as many bits as you want to use.
People have suggested using a hash map from int to functions, but since we have indices that are small consecutive integers starting at 0, an array is much more reasonable.

In C++:
typedef void Function(void);

void call_appropriate_function(bool a, bool b, bool c, bool d) {
static Function *function_array[16] = {
some_function, some_other_function, ...
};

(function_array[a + 2*b + 4*c + 8*d])();
}
People have suggested using a hash map from int to functions, but since we have indices that are small consecutive integers starting at 0, an array is much more reasonable.[/quote]

Don't most [all?] compilers convert switches into jump tables anyway?

People have suggested using a hash map from int to functions, but since we have indices that are small consecutive integers starting at 0, an array is much more reasonable.


Don't most [all?] compilers convert switches into jump tables anyway?
[/quote]

Yes, that's probably what a switch would become. But the OP was complaining about the code being long, and using a container of function pointers is shorter, because you save yourself the `case' labels and the `break's.

Anyway, my comment was objecting to using a hash map: Using a switch statement is perfectly acceptable in my opinion.

This topic is closed to new replies.

Advertisement