More efficient alternative to compare booleans?
#1 Members - Reputation: 379
Posted 08 October 2012 - 11:21 PM
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?
#2 Members - Reputation: 4605
Posted 08 October 2012 - 11:46 PM
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.
My game: Gnoblins
Developer journal about Gnoblins
Small goodies: Simple alpha transparency in deferred shader
#3 Members - Reputation: 425
Posted 08 October 2012 - 11:53 PM
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.
#4 Members - Reputation: 683
Posted 08 October 2012 - 11:59 PM
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.
http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.
#5 Members - Reputation: 1951
Posted 09 October 2012 - 12:05 AM
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;
};
Edited by iMalc, 09 October 2012 - 12:07 AM.
My website dedicated to sorting algorithms
#6 Members - Reputation: 379
Posted 09 October 2012 - 03:16 AM
I think I like the sound of
as this seems like the best way for what I'm doing.you can convert the booleans to bit-flags:
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.
Edited by Rybo5001, 09 October 2012 - 03:19 AM.
#7 Moderators - Reputation: 4636
Posted 09 October 2012 - 03:29 AM
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 24-1=15.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.
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.
#8 Members - Reputation: 5815
Posted 09 October 2012 - 03:54 AM
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])();
}
Edited by alvaro, 09 October 2012 - 03:54 AM.
#9 Members - Reputation: 568
Posted 09 October 2012 - 07:30 AM
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?
#10 Members - Reputation: 5815
Posted 09 October 2012 - 07:46 AM
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?
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.
#12 Moderators - Reputation: 4636
Posted 09 October 2012 - 10:30 AM
There's no reason why any range compact enough could be implemented as a jump table. Any offset from zero can be accounted for by subtraction before lookup, and missing cases can be accounted for with an entry jumping to the end of the case statement, or to wherever the no-case route goes.Don't most [all?] compilers convert switches into jump tables anyway?
Not all switches are converted to jump tables in C++. Only switch statements with contiguous integer cases starting from 0 AFAIK.
Edited by Brother Bob, 09 October 2012 - 10:31 AM.
#13 Members - Reputation: 190
Posted 09 October 2012 - 10:37 AM
If you can't do this and you still want to have clean code, use a hash map as stated above because this will be the most efficient and expandable in the long term. Also rather than using individual booleans for A, B, C, and D, consider having a bit mask because it will make it easier to use as an index. For instance,
[source lang="cpp"]int ABCD = 0; // Flags variableABCD |= 1; // Set AABCD |= 2; // Set Bstatic Function functions[FUNC_NUM] = { doSomething, doSomethingElse, ...}int index = hash(ABCD);function[index]();[/source]






