Followers 0

# More efficient alternative to compare booleans?

## 12 posts in this topic

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?
0

##### Share on other sites
[quote name='Rybo5001' timestamp='1349760106' post='4988223']
Is there a better way of doing this?
[/quote]

Yes =>

[quote name='Rybo5001' timestamp='1349760106' post='4988223']
I know a switch statement could be used
[/quote]

or, depending on the language you are using, you can use a hashmap (int->function), where int is the binary representation of your conditions.
0

##### Share on other sites
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:
[CODE]
switch (convert_to_enum(A, B, C, D))
{
case <case1>: ...;
case <case2>: ...;
...
}
[/CODE]

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

##### Share on other sites
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.
0

##### Share on other sites
If you have 16 [i]distinct[/i] pieces of code similar to [i]doSomething[/i], 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.[code]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;
};[/code] Edited by iMalc
0

##### Share on other sites
Thank you for all these excellent suggestions, looks like I'm not getting away with shorter coding! [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]

I think I like the sound of [quote name='iMalc' timestamp='1349762749' post='4988231']
you can convert the booleans to bit-flags:
[/quote] as this seems like the best way for what I'm doing.

[b]EDIT: [/b]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
0

##### Share on other sites
[quote name='Rybo5001' timestamp='1349774203' post='4988268']
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.
[/quote]
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.
[code]
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;
}
[/code]
Extend to as many bits as you want to use.
0

##### Share on other sites
[quote]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?
0

##### Share on other sites
[quote name='turch' timestamp='1349789410' post='4988334']
[quote]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?
[/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.
0

##### Share on other sites
[quote]Don't most [all?] compilers convert switches into jump tables anyway?[/quote]

Not all switches are converted to jump tables in C++. Only switch statements with contiguous integer cases starting from 0 AFAIK.
0

##### Share on other sites
[quote name='greenvertex' timestamp='1349798616' post='4988394']
[quote]Don't most [all?] compilers convert switches into jump tables anyway?[/quote]

Not all switches are converted to jump tables in C++. Only switch statements with contiguous integer cases starting from 0 AFAIK.
[/quote]
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. Edited by Brother Bob
1

##### Share on other sites
This type of structure with a massive "if else if" is typically not the best style. The only way I see reducing this large block is to take a step back and see if you can group functionality in a different way. The way to tell if this will work is to look through all the body's of the if statements and see if they are unique. If they are not you can try and regroup the logic based on the statement bodies.

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 variable

ABCD |= 1; // Set A
ABCD |= 2; // Set B

static Function functions[FUNC_NUM] = {
doSomething,
doSomethingElse,
...
}

int index = hash(ABCD);

function[index]();[/source]
0

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account