473,408 Members | 2,477 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,408 software developers and data experts.

switch is not optimized for mapping between enums

Hello,
I have used some library where is defined enum like:

typedef enum Enum1 { ENUM1_1, ENUM1_2, ENUM1_3, ENUM1_4, ENUM1_5 };

In my code I have defined:

typedef enum Enum2 { ENUM2_1, ENUM2_2, ENUM2_3, ENUM2_4, ENUM2_5 };

My map function looks like:

Enum2 mapEnum1toEnum2( Enum1 enum )
{
switch( enum )
{
case ENUM1_1: return ENUM2_1;
case ENUM1_2: return ENUM2_2;
case ENUM1_3: return ENUM2_3;
case ENUM1_4: return ENUM2_4;
case ENUM1_5: return ENUM2_5;
default: throw "UNKNOWN ENUM1!!!";
}
}

I have analised asm code generated by compiler, and the results is not
optimal as it can be. Because the enums can map 1:1 I have expected that
compiler creates code similar to:

Enum2 mapEnum1toEnum2( Enum1 enum )
{
if ( enum < ENUM1_1 || enum ENUM1_5 )
throw "UNKNOWN ENUM1!!!";
return reinterpret_cast<Enum2>( enum );
}

In example above both enums are continuous, but in case if one of enums
is not continuous there is another possibility - create hash table e.g.
typedef enum Enum2 { ENUM2_1 = -1, ENUM2_2 = 33, ENUM2_3 = 55, ENUM2_4 =
1234, ENUM2_5 = 5533 };

Enum2 mapEnum1toEnum2( Enum1 enum )
{
static const Enum2 hash[] = { ENUM2_1, ENUM2_2, ENUM2_3, ENUM2_4,
ENUM2_5 };
if ( enum < ENUM1_1 || enum ENUM1_5 )
throw "UNKNOWN ENUM1!!!";
return hash[enum];
}
Do you know any implementation to create mapEnum1toEnum2 function which
is better optimized than switch construction. Maybe there is possible
to use metaprogramming for this issue.

Best Regards
MariuszC

Nov 11 '06 #1
3 2202
MariuszC wrote:

You don't have to ask twice.
Hello,
I have used some library where is defined enum like:

typedef enum Enum1 { ENUM1_1, ENUM1_2, ENUM1_3, ENUM1_4, ENUM1_5 };

In my code I have defined:

typedef enum Enum2 { ENUM2_1, ENUM2_2, ENUM2_3, ENUM2_4, ENUM2_5 };

My map function looks like:

Enum2 mapEnum1toEnum2( Enum1 enum )
{
switch( enum )
{
case ENUM1_1: return ENUM2_1;
case ENUM1_2: return ENUM2_2;
case ENUM1_3: return ENUM2_3;
case ENUM1_4: return ENUM2_4;
case ENUM1_5: return ENUM2_5;
default: throw "UNKNOWN ENUM1!!!";
}
}

I have analised asm code generated by compiler, and the results is not
optimal as it can be. Because the enums can map 1:1 I have expected that
compiler creates code similar to:
Is this a problem to your application?

If the list is quite long and a switch is inefficient, you could try a
std::map<Enum1,Enum2>.

The only way to tell which is better is to profile your application.

--
Ian Collins.
Nov 11 '06 #2
Ian Collins wrote:
MariuszC wrote:
....
>I have analised asm code generated by compiler, and the results is not
optimal as it can be. Because the enums can map 1:1 I have expected that
compiler creates code similar to:
Is this a problem to your application?

If the list is quite long and a switch is inefficient, you could try a
std::map<Enum1,Enum2>.

The only way to tell which is better is to profile your application.

std::map is way more expensive than a switch.

Perhaps you could do something like this.
enum OldEnum
{
OA=1,
OB,
OC
};

enum {
g_smallest_old = OA,
g_largest_old = OC
};

enum NewEnum
{
NA=10,
NB,
NC,
BAD=-1
};
template <int w_enum>
struct OldEnumMap
{
static const NewEnum m_value = BAD;
};
template <NewEnum w_enum>
struct NewEnumMap
{
static const NewEnum m_value = w_enum;
};

// define the mapping between old and new enums
template <struct OldEnumMap<OA: NewEnumMap<NA{};
template <struct OldEnumMap<OB: NewEnumMap<NB{};
template <struct OldEnumMap<OC: NewEnumMap<NC{};

NewEnum enummap[] = {
OldEnumMap<0>::m_value,
OldEnumMap<1>::m_value,
OldEnumMap<2>::m_value,
OldEnumMap<3>::m_value,
OldEnumMap<4>::m_value,
// ... fill in to your heart's content
};

NewEnum Convert( OldEnum i_val )
{
if ( i_val < int(g_smallest_old) )
{
return BAD;
}

if ( i_val int(g_largest_old) )
{
return BAD;
}

return enummap[ i_val ];
}

#include <iostream>
int main()
{
// dynamic conversion
std::cout << Convert(OB) << "\n";

// static (compile time) conversion
std::cout << OldEnumMap<OB>::m_value << "\n";

}
Nov 11 '06 #3


Ian Collins wrote:
MariuszC wrote:

You don't have to ask twice.
In first version I made mistake in condition, there was && instead of
||. I was deleting first post before sending second corrected one.

Mariusz
Nov 11 '06 #4

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

Similar topics

14
by: Rudi Hansen | last post by:
I dont seem to be able to find the switch statement in Python. I would like to be able to do switch(var) case 1 : print "var = 1" case 2: print "var = 2"
6
by: naruto | last post by:
Hi all, I have the following being defined in a A.cxx file. // define in source file. Not exported to the outside world (this cannot be // moved to the header file ) #define CHANNEL_0 0...
24
by: | last post by:
Hi, I need to read a big CSV file, where different fields should be converted to different types, such as int, double, datetime, SqlMoney, etc. I have an array, which describes the fields and...
65
by: He Shiming | last post by:
Hi, I just wrote a function that has over 200 "cases" wrapped in a "switch" statement. I'm wondering if there are performance issues in such implementation. Do I need to optimize it some way? ...
10
by: cody | last post by:
Why can't we use switches with singleton objects? I know that the compiler cannot optimize them like constant values but the same is true for strings and they are allowed in switches. The reason...
9
by: cody | last post by:
Why can't switch used for objects, at least for static readonly fields it wold be nice. The Jit could certainly optimize this case because the addresses of static readonly variables are known at...
1
by: MariuszC | last post by:
Hello, I have used some library where is defined enum like: typedef enum Enum1 { ENUM1_1, ENUM1_2, ENUM1_3, ENUM1_4, ENUM1_5 }; In my code I have defined: typedef enum Enum2 { ENUM2_1,...
11
by: Peter Kirk | last post by:
Hi i have a string variable which can take one of a set of many values. Depending on the value I need to take different (but related) actions. So I have a big if/else-if construct - but what is...
12
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
7
by: Kurt | last post by:
Hi. I have a c++ project that is basically a set of implementation hiding classes, so that we can convert a static lib with a lot of dependancies into a dynamic lib with less dependancies, so that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...

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.