"Multiplex" 4 variables into a single number?

C

carl

In a program I have a nested switch over 3 variables a, b and c. Now instead
of having this nasty switch (which might grow) I was thinking about creating
a single unique value for each combination of the 3 variables and then
switch on this single varible instead. Something like:


// a, b and c can each range from 0 to n.
int selection = multiplex(a,b,c);

// Single level switch
switch (selection) {
case: 0
....

}

Has anyone done this kind of "multiplexing before" and can recommend an
approach to implement it?
 
J

Jonathan Lee

In a program I have a nested switch over 3 variables a, b and c. Now instead
of having this nasty switch (which might grow) I was thinking about creating
a single unique value for each combination of the 3 variables and then
switch on this single varible instead. Something like:

Has anyone done this kind of "multiplexing before" and can recommend an
approach to implement it?

Any injective function from (a,b,c) into the range of "int" would
do the trick. Like

int multiplex(int a, int b, int c) {
return a*(n+1)*(n+1) + b*(n+1) + c;
}

Assuming the range you gave, 0 to n, is inclusive. If n is small
enough this won't overflow. If n + 1 happens to be a power of 2
this is just bit shifting and packing.

Of course, this will probably obfuscate your code... but that's
your choice.

--Jonathan
 
G

Gert-Jan de Vos

Any injective function from (a,b,c) into the range of "int" would
do the trick. Like

  int multiplex(int a, int b, int c) {
    return a*(n+1)*(n+1) + b*(n+1) + c;
  }

Assuming the range you gave, 0 to n, is inclusive. If n is small
enough this won't overflow. If n + 1 happens to be a power of 2
this is just bit shifting and packing.

If you use a macro or C++0x constexpr function, you can use it in the
case statements too.

Gert-Jan
 
Ö

Öö Tiib

In a program I have a nested switch over 3 variables a, b and c. Now instead
of having this nasty switch (which might grow) I was thinking about creating
a single unique value for each combination of the 3 variables and then
switch on this single varible instead. Something like:

// a, b and c can each range from 0 to n.
int selection = multiplex(a,b,c);

// Single level switch
switch (selection) {
  case: 0
 ....

}

Has anyone done this kind of "multiplexing before" and can recommend an
approach to implement it?

Using lots of nested switches can be considered preliminary
optimization when it makes code hard to read.

Long switch is as terrible as nested switches. Nested switches you can
at least move into separate procedures to reduce cyclomatic complexity
of single procedure.

When there are lot of such cases then i use lookup tables.

If it is used in innermost loop and hitting performance then i may
sort it and use binary search in it. Finally i may use simple code
generator to generate nested switch code from a lookup table. Then at
least i need not to read that switch.
 
A

Alf P. Steinbach

* carl, on 25.05.2010 00:30:
In a program I have a nested switch over 3 variables a, b and c. Now
instead of having this nasty switch (which might grow) I was thinking
about creating a single unique value for each combination of the 3
variables and then switch on this single varible instead. Something like:


// a, b and c can each range from 0 to n.
int selection = multiplex(a,b,c);

// Single level switch
switch (selection) {
case: 0
....

}

Has anyone done this kind of "multiplexing before" and can recommend an
approach to implement it?

Jonathan Lee mentioned arithmetic coding, <url:
http://en.wikipedia.org/wiki/Arithmetic_coding>.

Öö Tiib mentioned that lots of nested switches can make the code hard to read.

Anyway, it appears that you're trying to map a range of value combinations to
distinct pieces of code. Often that indicates a design issue. Instead consider
parameterizing the code so that it can deal more generally with the data.

Most likely you could get better help if you described what it's /for/.

The nested switching is a solution to some problem: most likely there are other
solutions (like, parameterization) that solve the problem in a way that's more
easy to maintain.


Cheers & hth.,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top