473,289 Members | 1,940 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,289 software developers and data experts.

Mapping Function

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 [restriction])
#define CHANNEL_0 0
#define CHANNEL_1 1
#define CHANNEL_2 2
#define CHANNEL_3 3
#define CHANNEL_4 4
#define CHANNEL_5 5

However, in another file B.cxx, I have a fucntion called:

registerChannel(int channelNumber_)
{
// perform registration via channel number (valid range 0 - 5)
register(channelNumber_);
}

It works fine. However, now there is a new requirements: Channel
0,2,4 must maps to 0, 1, 2. Other channel maps to an invalid value.

I was wondering what is the best way to implements the mapping
function. My idea is as follows:

// 255 is an invalid value
#define INVALID_CHANNEL 255
int ChannelMapArray[] =
{0, INVALID_CHANNEL, 1, INVALID_CHANNEL, 2, INVALID_VALUE, };
So I could just use it in registerChannel() as follows:
registerChannel(int channelNumber_)
{
// perform registration via channel number (valid range 0 - 5)
register(ChannelMapArray[channelNumber_]);
}
Is there a better way to implement a mapping function. The array
implementation seems unclear. Also, is it possible to implement it as
a 2 ways mapping function (i.e given 1-6 -> 0,1,2,invalid or vice
versa)? Is there a commonly known technique for this? Is it possible
to reference the mapping to the #define in A.cxx?

Thanks All,
Naruto
Jul 22 '05 #1
6 6472
naruto wrote:

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 [restriction])
A header file does not necessarily mean the outside world. The
restriction is superficial.

#define CHANNEL_0 0
#define CHANNEL_1 1
#define CHANNEL_2 2
#define CHANNEL_3 3
#define CHANNEL_4 4
#define CHANNEL_5 5

You might benefit from using enums. In addition, you have two
different systems of channel values, they could be represented by
two different enum types to eliminate the confusion as to which
channel system int channelNumber_ belongs.

However, in another file B.cxx, I have a fucntion called:

registerChannel(int channelNumber_)
{
// perform registration via channel number (valid range 0 - 5)
register(channelNumber_);
}

It works fine. However, now there is a new requirements: Channel
0,2,4 must maps to 0, 1, 2. Other channel maps to an invalid value.

I was wondering what is the best way to implements the mapping
function. My idea is as follows:

// 255 is an invalid value
#define INVALID_CHANNEL 255
int ChannelMapArray[] =
{0, INVALID_CHANNEL, 1, INVALID_CHANNEL, 2, INVALID_VALUE, };

So I could just use it in registerChannel() as follows:
registerChannel(int channelNumber_)
{
// perform registration via channel number (valid range 0 - 5)
register(ChannelMapArray[channelNumber_]);
}

Is there a better way to implement a mapping function.
You've said it: implement a mapping function.

The array
implementation seems unclear. Also, is it possible to implement it as
a 2 ways mapping function (i.e given 1-6 -> 0,1,2,invalid or vice
versa)? Is there a commonly known technique for this?

Something to the effect:

//I contrived the names; imagined that you have, e.g., "device" and
//"logical" channels

class ChannelMapping {
struct MappingInfo {
channel_type_log channel_log;
channel_type_dev channel_dev;
};

static MappingInfo mapping_info_[];

public:
static channel_type_log devToLog(channel_type_dev channel) {...}
static channel_type_dev logToSrc(channel_type_log channel) {...}
};

Depending on your design you may need not to make these functions static.
Otherwise you can use a namespace instead of a class. Define mapping_info_
(give it a better name). Use it directly or write an init() function to
create an optimised storage for search by the mapping functions for a
sufficiently large number of values.

Is it possible
to reference the mapping to the #define in A.cxx?
Move them to file A_internal_whatever.h

Denis

Thanks All,
Naruto

Jul 22 '05 #2
naruto wrote:

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 [restriction])
A header file does not necessarily mean the outside world. The
restriction is superficial.

#define CHANNEL_0 0
#define CHANNEL_1 1
#define CHANNEL_2 2
#define CHANNEL_3 3
#define CHANNEL_4 4
#define CHANNEL_5 5

You might benefit from using enums. In addition, you have two
different systems of channel values, they could be represented by
two different enum types to eliminate the confusion as to which
channel system int channelNumber_ belongs.

However, in another file B.cxx, I have a fucntion called:

registerChannel(int channelNumber_)
{
// perform registration via channel number (valid range 0 - 5)
register(channelNumber_);
}

It works fine. However, now there is a new requirements: Channel
0,2,4 must maps to 0, 1, 2. Other channel maps to an invalid value.

I was wondering what is the best way to implements the mapping
function. My idea is as follows:

// 255 is an invalid value
#define INVALID_CHANNEL 255
int ChannelMapArray[] =
{0, INVALID_CHANNEL, 1, INVALID_CHANNEL, 2, INVALID_VALUE, };

So I could just use it in registerChannel() as follows:
registerChannel(int channelNumber_)
{
// perform registration via channel number (valid range 0 - 5)
register(ChannelMapArray[channelNumber_]);
}

Is there a better way to implement a mapping function.
You've said it: implement a mapping function.

The array
implementation seems unclear. Also, is it possible to implement it as
a 2 ways mapping function (i.e given 1-6 -> 0,1,2,invalid or vice
versa)? Is there a commonly known technique for this?

Something to the effect:

//I contrived the names; imagined that you have, e.g., "device" and
//"logical" channels

class ChannelMapping {
struct MappingInfo {
channel_type_log channel_log;
channel_type_dev channel_dev;
};

static MappingInfo mapping_info_[];

public:
static channel_type_log devToLog(channel_type_dev channel) {...}
static channel_type_dev logToSrc(channel_type_log channel) {...}
};

Depending on your design you may need not to make these functions static.
Otherwise you can use a namespace instead of a class. Define mapping_info_
(give it a better name). Use it directly or write an init() function to
create an optimised storage for search by the mapping functions for a
sufficiently large number of values.

Is it possible
to reference the mapping to the #define in A.cxx?
Move them to file A_internal_whatever.h

Denis

Thanks All,
Naruto

Jul 22 '05 #3
naruto wrote:
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 [restriction])
#define CHANNEL_0 0
#define CHANNEL_1 1
#define CHANNEL_2 2
#define CHANNEL_3 3
#define CHANNEL_4 4
#define CHANNEL_5 5

However, in another file B.cxx, I have a fucntion called:

registerChannel(int channelNumber_)
{
// perform registration via channel number (valid range 0 - 5)
register(channelNumber_);
}

It works fine. However, now there is a new requirements: Channel
0,2,4 must maps to 0, 1, 2. Other channel maps to an invalid value.

I was wondering what is the best way to implements the mapping
function. My idea is as follows:

// 255 is an invalid value
#define INVALID_CHANNEL 255
int ChannelMapArray[] =
{0, INVALID_CHANNEL, 1, INVALID_CHANNEL, 2, INVALID_VALUE, };
So I could just use it in registerChannel() as follows:
registerChannel(int channelNumber_)
{
// perform registration via channel number (valid range 0 - 5)
register(ChannelMapArray[channelNumber_]);
}
Is there a better way to implement a mapping function. The array
implementation seems unclear. Also, is it possible to implement it as
a 2 ways mapping function (i.e given 1-6 -> 0,1,2,invalid or vice
versa)? Is there a commonly known technique for this? Is it possible
to reference the mapping to the #define in A.cxx?

Thanks All,
Naruto


One could always use the std::map container:
#include <map>
using std::map;
typedef map<unsigned int, unsigned int> Mapping_Container;

The mapping would be:
Mapping_Container channel_map;
//...
unsigned int original_value;
unsigned int new_value;

//...
new_value = channel_map[original_value];
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #4
naruto wrote:
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 [restriction])
#define CHANNEL_0 0
#define CHANNEL_1 1
#define CHANNEL_2 2
#define CHANNEL_3 3
#define CHANNEL_4 4
#define CHANNEL_5 5

However, in another file B.cxx, I have a fucntion called:

registerChannel(int channelNumber_)
{
// perform registration via channel number (valid range 0 - 5)
register(channelNumber_);
}

It works fine. However, now there is a new requirements: Channel
0,2,4 must maps to 0, 1, 2. Other channel maps to an invalid value.

I was wondering what is the best way to implements the mapping
function. My idea is as follows:

// 255 is an invalid value
#define INVALID_CHANNEL 255
int ChannelMapArray[] =
{0, INVALID_CHANNEL, 1, INVALID_CHANNEL, 2, INVALID_VALUE, };
So I could just use it in registerChannel() as follows:
registerChannel(int channelNumber_)
{
// perform registration via channel number (valid range 0 - 5)
register(ChannelMapArray[channelNumber_]);
}
Is there a better way to implement a mapping function. The array
implementation seems unclear. Also, is it possible to implement it as
a 2 ways mapping function (i.e given 1-6 -> 0,1,2,invalid or vice
versa)? Is there a commonly known technique for this? Is it possible
to reference the mapping to the #define in A.cxx?

Thanks All,
Naruto


One could always use the std::map container:
#include <map>
using std::map;
typedef map<unsigned int, unsigned int> Mapping_Container;

The mapping would be:
Mapping_Container channel_map;
//...
unsigned int original_value;
unsigned int new_value;

//...
new_value = channel_map[original_value];
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #5
> //I contrived the names; imagined that you have, e.g., "device" and
//"logical" channels

class ChannelMapping {
struct MappingInfo {
channel_type_log channel_log;
channel_type_dev channel_dev;
};

static MappingInfo mapping_info_[];

public:
static channel_type_log devToLog(channel_type_dev channel) {...}
static channel_type_dev logToSrc(channel_type_log channel) {...}
};

Depending on your design you may need not to make these functions static.
Otherwise you can use a namespace instead of a class. Define mapping_info_
(give it a better name). Use it directly or write an init() function to
create an optimised storage for search by the mapping functions for a
sufficiently large number of values.

Is it possible
to reference the mapping to the #define in A.cxx?


Move them to file A_internal_whatever.h

Denis


Thanks Denis. I didn't thought of using a class or namespace to
perform a mapping fuction. Thanks!!

Naruto
Jul 22 '05 #6
> //I contrived the names; imagined that you have, e.g., "device" and
//"logical" channels

class ChannelMapping {
struct MappingInfo {
channel_type_log channel_log;
channel_type_dev channel_dev;
};

static MappingInfo mapping_info_[];

public:
static channel_type_log devToLog(channel_type_dev channel) {...}
static channel_type_dev logToSrc(channel_type_log channel) {...}
};

Depending on your design you may need not to make these functions static.
Otherwise you can use a namespace instead of a class. Define mapping_info_
(give it a better name). Use it directly or write an init() function to
create an optimised storage for search by the mapping functions for a
sufficiently large number of values.

Is it possible
to reference the mapping to the #define in A.cxx?


Move them to file A_internal_whatever.h

Denis


Thanks Denis. I didn't thought of using a class or namespace to
perform a mapping fuction. Thanks!!

Naruto
Jul 22 '05 #7

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

Similar topics

1
by: Ben | last post by:
I realize that GIS is a whole other realm but was wondering if any of you have done any PHP apps that incorporate a mapping function? Specifically what I need is to give an address, street, city...
3
by: jahurt | last post by:
I need to map a function to several variables. I'm trying to use map and lambda to do this. Here's my attempt... #!/usr/bin/env python from random import * = map(lambda n: random(),...
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...
1
by: jiatiejun | last post by:
Can .Net use File Mapping function without Win32 API ? thanks!
0
by: Joel | last post by:
Hello: I have some question about some data type mapping for some data type of my c++ dll Data Type Mapping ----------------------------------- type are time_t tm char *message
4
by: BentleyInc | last post by:
I'm trying to find a way to add a whildcard application mapping to aspnet_isapi.dll in IIS programmatically.... been looking into IIS administrator reference but didn't find the right function to...
4
by: Tony Lownds | last post by:
(Note: PEPs in the 3xxx number range are intended for Python 3000) PEP: 3107 Title: Function Annotations Version: $Revision: 53169 $ Last-Modified: $Date: 2006-12-27 20:59:16 -0800 (Wed, 27 Dec...
3
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
5
by: alan | last post by:
Hello world, I'm wondering if it's possible to implement some sort of class/object that can perform mapping from class types to strings? I will know the class type at compile time, like so:...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.