473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Condensing nested switch statements

On a note related to my question about logical XOR, I'm trying to
think of a non-obfuscated way to condense the following code block
(which, as usual, I did not write):

switch( int_val_1 ) {
case 0:
switch( int_val_2 ) {
case enum_val_1: return( num1 == num2 );
case enum_val_2: return( num1 <= num2 );
case enum_val_3: return( num1 >= num2 );
default: return( true );
}
case 1:
switch( int_val_2 ) {
case enum_val_1: return( num1 != num2 );
case enum_val_2: return( num1 > num2 );
case enum_val_3: return( num1 < num2 );
default: return( true );
}
default:
return true;
}

Any suggestions?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #1
19 15166
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
switch( int_val_1 ) {
case 0:
switch( int_val_2 ) {
case enum_val_1: return( num1 == num2 );
case enum_val_2: return( num1 <= num2 );
case enum_val_3: return( num1 >= num2 );
default: return( true );
}
case 1:
switch( int_val_2 ) {
case enum_val_1: return( num1 != num2 );
case enum_val_2: return( num1 > num2 );
case enum_val_3: return( num1 < num2 );
default: return( true );
}
default:
return true;
}

Any suggestions?


Yes: why? This code isn't really obfuscated. Ok, the _design_ probably
isn't all that hot, with the two different control variables, but
there's little you can do about that without changing the semantics.

Richard
Nov 14 '05 #2
nrk
Christopher Benson-Manica wrote:
On a note related to my question about logical XOR, I'm trying to
think of a non-obfuscated way to condense the following code block
(which, as usual, I did not write):

switch( int_val_1 ) {
case 0:
switch( int_val_2 ) {
case enum_val_1: return( num1 == num2 );
case enum_val_2: return( num1 <= num2 );
case enum_val_3: return( num1 >= num2 );
default: return( true );
}
case 1:
switch( int_val_2 ) {
case enum_val_1: return( num1 != num2 );
case enum_val_2: return( num1 > num2 );
case enum_val_3: return( num1 < num2 );
default: return( true );
}
default:
return true;
}

Any suggestions?


You can condense it, but I don't consider the following to be an improvement
(hopefully, I haven't changed the semantics either):

int ret;

if ( int_val_1 != !!int_val_1 ) return true;
switch ( int_val_2 ) {
case enum_val_1: ret = (num1 == num2); break;
case enum_val_2: ret = (num1 <= num2); break;
case enum_val_3: ret = (num1 >= num2); break;
default: return true;
}
return ret ^ int_val_1;

Less readable, less flexible, more error-prone. Wow!! If job security was a
priority, I wouldn't hesitate to incorporate this "improvement" :-)

-nrk.

--
Remove devnull for email
Nov 14 '05 #3
Christopher Benson-Manica wrote:

On a note related to my question about logical XOR, I'm trying to
think of a non-obfuscated way to condense the following code block
(which, as usual, I did not write):

switch( int_val_1 ) {
case 0:
switch( int_val_2 ) {
case enum_val_1: return( num1 == num2 );
case enum_val_2: return( num1 <= num2 );
case enum_val_3: return( num1 >= num2 );
default: return( true );
}
case 1:
switch( int_val_2 ) {
case enum_val_1: return( num1 != num2 );
case enum_val_2: return( num1 > num2 );
case enum_val_3: return( num1 < num2 );
default: return( true );
}
default:
return true;
}

Any suggestions?


int result;

switch (int_val_2) {
case enum_val_1: result = num1 == num2;
case enum_val_2: result = num1 <= num2;
case enum_val_3: result = num1 >= num2;
default: return true; /* sounds ridiculous */
}
if (1 == int_val1) result = !result;
else if (0 != int_val1) result = true;
/* else use_result for 0 */
return result;

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #4
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in
news:bv**********@chessie.cirr.com:
On a note related to my question about logical XOR, I'm trying to
think of a non-obfuscated way to condense the following code block
(which, as usual, I did not write):

switch( int_val_1 ) {
case 0:
switch( int_val_2 ) {
case enum_val_1: return( num1 == num2 );
case enum_val_2: return( num1 <= num2 );
case enum_val_3: return( num1 >= num2 );
default: return( true );
}
case 1:
switch( int_val_2 ) {
case enum_val_1: return( num1 != num2 );
case enum_val_2: return( num1 > num2 );
case enum_val_3: return( num1 < num2 );
default: return( true );
}
default:
return true;
}

Any suggestions?


Get rid of the default cases?

switch (int_val_1)
{

case 0:
switch (int_val_2)
{
case enum_val_1: return num1 == num2;
case enum_val_2: return num1 <= num2;
case enum_val_3: return num1 >= num2;
}
break;

case 1:
switch (int_val_2)
{
case enum_val_1: return num1 != num2;
case enum_val_2: return num1 > num2;
case enum_val_3: return num1 < num2;
}
break;
}

return true; /* factor this out */

It's not much, admittedly.

--
- Mark ->
--
Nov 14 '05 #5
nrk
CBFalconer wrote:
Christopher Benson-Manica wrote:

On a note related to my question about logical XOR, I'm trying to
think of a non-obfuscated way to condense the following code block
(which, as usual, I did not write):

switch( int_val_1 ) {
case 0:
switch( int_val_2 ) {
case enum_val_1: return( num1 == num2 );
case enum_val_2: return( num1 <= num2 );
case enum_val_3: return( num1 >= num2 );
default: return( true );
}
case 1:
switch( int_val_2 ) {
case enum_val_1: return( num1 != num2 );
case enum_val_2: return( num1 > num2 );
case enum_val_3: return( num1 < num2 );
default: return( true );
}
default:
return true;
}

Any suggestions?


int result;

switch (int_val_2) {
case enum_val_1: result = num1 == num2;
case enum_val_2: result = num1 <= num2;
case enum_val_3: result = num1 >= num2;
default: return true; /* sounds ridiculous */
}
if (1 == int_val1) result = !result;
else if (0 != int_val1) result = true;
/* else use_result for 0 */
return result;


Your incantations have invoked the demons that reside in fall-thru hell :-)
I believe this is also known as the devil's device.

-nrk.
--
Remove devnull for email
Nov 14 '05 #6
On Tue, 3 Feb 2004 15:42:07 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:
On a note related to my question about logical XOR, I'm trying to
think of a non-obfuscated way to condense the following code block
(which, as usual, I did not write):

switch( int_val_1 ) {
case 0:
switch( int_val_2 ) {
case enum_val_1: return( num1 == num2 );
case enum_val_2: return( num1 <= num2 );
case enum_val_3: return( num1 >= num2 );
default: return( true );
}
case 1:
switch( int_val_2 ) {
case enum_val_1: return( num1 != num2 );
case enum_val_2: return( num1 > num2 );
case enum_val_3: return( num1 < num2 );
default: return( true );
}
default:
return true;
}

Any suggestions?


Not counting the default cases you have 6 = 2*3 distinct results. So
you will definitely need 6 distinct cases. What's wrong with the code?

--
Horst

Nov 14 '05 #7
CBFalconer wrote:

Christopher Benson-Manica wrote:

On a note related to my question about logical XOR, I'm trying to
think of a non-obfuscated way to condense the following code block
(which, as usual, I did not write):

switch( int_val_1 ) {
case 0:
switch( int_val_2 ) {
case enum_val_1: return( num1 == num2 );
case enum_val_2: return( num1 <= num2 );
case enum_val_3: return( num1 >= num2 );
default: return( true );
}
case 1:
switch( int_val_2 ) {
case enum_val_1: return( num1 != num2 );
case enum_val_2: return( num1 > num2 );
case enum_val_3: return( num1 < num2 );
default: return( true );
}
default:
return true;
}

Any suggestions?


int result;

switch (int_val_2) {
case enum_val_1: result = num1 == num2;
case enum_val_2: result = num1 <= num2;
case enum_val_3: result = num1 >= num2;
default: return true; /* sounds ridiculous */
}
if (1 == int_val1) result = !result;
else if (0 != int_val1) result = true;
/* else use_result for 0 */
return result;


Woops! - Each case is missing a "break;" statement.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #8
begin followup to Christopher Benson-Manica:
switch( int_val_1 ) {
case 0:
switch( int_val_2 ) {
case enum_val_1: return( num1 == num2 );
case enum_val_2: return( num1 <= num2 );
case enum_val_3: return( num1 >= num2 );
default: return( true );
}
case 1:
switch( int_val_2 ) {
case enum_val_1: return( num1 != num2 );
case enum_val_2: return( num1 > num2 );
case enum_val_3: return( num1 < num2 );
default: return( true );
}
default:
return true;
}


Well, if the values of int_val_2 are contiguous then there is one
obvious solution. Replace "- 0" with the actual minimum value.

switch(int_val_1 * 3 + int_val_2 - 0)
{
case 0: return( num1 == num2 );
case 1: return( num1 <= num2 );
case 2: return( num1 >= num2 );
case 3: return( num1 != num2 );
case 4: return( num1 > num2 );
case 5: return( num1 < num2 );
default: return true;
}

--
Für Google, Tux und GPL!
Nov 14 '05 #9
Horst Kraemer <ho***********@epost.de> spoke thus:
switch( int_val_1 ) {
case 0:
switch( int_val_2 ) {
case enum_val_1: return( num1 == num2 );
case enum_val_2: return( num1 <= num2 );
case enum_val_3: return( num1 >= num2 );
default: return( true );
}
case 1:
switch( int_val_2 ) {
case enum_val_1: return( num1 != num2 );
case enum_val_2: return( num1 > num2 );
case enum_val_3: return( num1 < num2 );
default: return( true );
}
default:
return true;
}
Not counting the default cases you have 6 = 2*3 distinct results. So
you will definitely need 6 distinct cases. What's wrong with the code?


6 distinct results, yes, but notice that they are related. After
reading replies to my other question, I gather that something like

switch( int_val_1 ) {
case 0:
case 1:
switch( int_val_2 ) {
case enum_val_1: return( (num1 == num2) ^ int_val_1 );
case enum_val_2: return( (num1 <= num2) ^ int_val_1 );
case enum_val_3: return( (num1 >= num2) ^ int_val_1 );
}
}
return 1;

would work, at the cost of some clarity.

(Sorry for the true's - I forgot to un-C++ my code...)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #10
CBFalconer <cb********@yahoo.com> spoke thus:
default: return true; /* sounds ridiculous */


Indeed, for which I apologize - as you might have guessed, this code
actually lives in C++ world, and I forgot to make the transistion back
to this dimension.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #11
Richard Bos <rl*@hoekstra-uitgeverij.nl> spoke thus:
Yes: why? This code isn't really obfuscated. Ok, the _design_ probably
isn't all that hot, with the two different control variables, but
there's little you can do about that without changing the semantics.


Well, the code as written isn't obfuscated, but I would like to shrink
it without turning it into a job-security device. I posted a solution
rather akin to nrk's solution in my reply to Mr. Kraemer, but I'm
afraid I could be stuck here awhile if I incorporate it into the real
code.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #12
Christopher Benson-Manica wrote:

Richard Bos <rl*@hoekstra-uitgeverij.nl> spoke thus:
Yes: why? This code isn't really obfuscated. Ok, the _design_ probably
isn't all that hot, with the two different control variables, but
there's little you can do about that without changing the semantics.


Well, the code as written isn't obfuscated, but I would like to shrink
it without turning it into a job-security device. I posted a solution
rather akin to nrk's solution in my reply to Mr. Kraemer, but I'm
afraid I could be stuck here awhile if I incorporate it into the real
code.


<topicality degree="tenuous">

If you're interested not just in rearranging the posted
code but in restructuring more general groups of multi-level
switch constructs and/or if ladders, you might want to read
up on the technique of "decision tables" (also called "decision
matrices" or sometimes "decision trees"). I haven't seen much
written about them in recent years -- heck, in recent decades --
but there was at one time an extensive literature about how
to construct them, how to optimize them for size or for speed,
how to check for completeness and other good qualities, and
so on.

</topicality>

--
Er*********@sun.com
Nov 14 '05 #13
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:bv**********@chessie.cirr.com...
CBFalconer <cb********@yahoo.com> spoke thus:
default: return true; /* sounds ridiculous */


Indeed, for which I apologize - as you might have guessed, this code
actually lives in C++ world, and I forgot to make the transistion back
to this dimension.


I think CBF was refering the whole default case rather than to such a petty
crime like using true.
Nov 14 '05 #14
Peter Pichler <pi*****@pobox.sk> spoke thus:
I think CBF was refering the whole default case rather than to such a petty
crime like using true.


No crime is too petty for c.l.c! ;) No, I appreciate petty nitpicks.
What, specifically, is wrong with the default case, besides the fact
that it is superfluous?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #15
nrk
Christopher Benson-Manica wrote:
Peter Pichler <pi*****@pobox.sk> spoke thus:
I think CBF was refering the whole default case rather than to such a
petty crime like using true.


No crime is too petty for c.l.c! ;) No, I appreciate petty nitpicks.
What, specifically, is wrong with the default case, besides the fact
that it is superfluous?


It's an enumeration you're switching on. default should lead to
self-destruct :-) IOW, the mere thought that you would get a value that
wasn't one of the enumerated constants shouldn't even begin to speculate
about the merest possibility of crossing your mind.

-nrk.
--
Remove devnull for email
Nov 14 '05 #16
nrk <ra*********@devnull.verizon.net> spoke thus:
It's an enumeration you're switching on. default should lead to
self-destruct :-) IOW, the mere thought that you would get a value that
wasn't one of the enumerated constants shouldn't even begin to speculate
about the merest possibility of crossing your mind.


Oh wow... good call. As you might surmise, the thought hadn't
crossed my mind, and the fact that I didn't write the code is a poor
excuse for not seeing the error. Thanks for a needed heads-up.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #17
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Richard Bos <rl*@hoekstra-uitgeverij.nl> spoke thus:
Yes: why? This code isn't really obfuscated. Ok, the _design_ probably
isn't all that hot, with the two different control variables, but
there's little you can do about that without changing the semantics.


Well, the code as written isn't obfuscated, but I would like to shrink
it without turning it into a job-security device.


Again: why? It isn't all that long. Does it occur a couple of dozen
times in that program? If so, make it a function.

Richard
Nov 14 '05 #18
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in message news:<bv**********@chessie.cirr.com>...
nrk <ra*********@devnull.verizon.net> spoke thus:
It's an enumeration you're switching on. default should lead to
self-destruct :-) IOW, the mere thought that you would get a value that
wasn't one of the enumerated constants shouldn't even begin to speculate
about the merest possibility of crossing your mind.


Oh wow... good call. As you might surmise, the thought hadn't
crossed my mind, and the fact that I didn't write the code is a poor
excuse for not seeing the error. Thanks for a needed heads-up.


A related solution to CBFalconer's is to split it into 2 functions.
I like his better, but YMMV. This doesn't preserve the returning
of "true" or 1 on failure to get an expected input, but...

int cmp(int int_val2, int num1, int num2)
{
switch (int_val2) {
case enum_val_1: return num1 == num2;
case enum_val_2: return num1 <= num2;
case enum_val_3: return num1 >= num2;
}
assert(0); /* Or otherwise deal with failure to be receive
an enumerated case */
}

int func1(int int_val_1, int int_val_2, int num1, int num2)
{
switch (int_val_1) {
case 0: return cmp(int_val_2, num1, num2);
case 1: return !cmp(int_val_2, num1, num2);
default: assert(0); /* or otherwise deal with failure to receive
an expected value */
}
}

-David
Nov 14 '05 #19
> > No crime is too petty for c.l.c! ;) No, I appreciate petty nitpicks.
What, specifically, is wrong with the default case, besides the fact
that it is superfluous?


It's an enumeration you're switching on. default should lead to
self-destruct :-) IOW, the mere thought that you would get a value that
wasn't one of the enumerated constants shouldn't even begin to speculate
about the merest possibility of crossing your mind.


Well, we all know that means it is going to happen as soon as we
stop looking :)
Nov 14 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jeff Maestas | last post by:
I want to publish a patch CD. The CD will contain my files and an HTML page which will autorun, and tell the user which patch to install. (The Microsoft MSI installer v2 is comes in two different...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
10
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our...
13
by: PeterZ | last post by:
Hi, Back to basics! My understanding is that the only way to exit a For-Next loop prematurely is with the 'break' keyword. How are you supposed to do that if you're inside a Switch...
3
by: G Patel | last post by:
Compiler won't let me nest switch statements with common labels inside them (in the cases). Why is this a problem (either in terms of standard or in terms of compiler's ability to make sense of...
9
by: paulyche | last post by:
I'm writing a program which contains an awful lot of nested if statements. I don't know how efficient this is but it definetely makes the code less readable. Does anyone have any advice on how I can...
18
by: desktop | last post by:
I have 3 types of objects: bob1, bob2 and bob3. Each object is identified by a unique ID which gets returned by the function getId(). All bobs are descendants from class BaseBob which is an...
18
by: MZaza | last post by:
Hello, When I compile it I get an error in line 16, which got case 'c'. #include <cstdlib> #include <iostream> #include <math.h> using namespace std; int main()
13
by: JRough | last post by:
I got lost on formatting the nested if/else's on the bottom of the file right up the last else/if there seems to be a stray bracket. I'm trying to line up the brackets so I can read it. Is it...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.