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

Function lookup tables?

Hi,

In assembly language you can use a lookup table to call functions.

1. Lookup function address in table
2. Call the function

Like:

CALL FUNCTION[INDEX]

FUNCTION DD FUNC1, FUNC2, FUNC3

FUNC1:
FUNC2:
FUNC3:
RET

How can I do this in C++ I would like it to be reasonably fast also?
The way I am doing it now is with a big switch statement. The switch
uses the index number for lookup. This seems like a bad way to do
what I'd would like. The preferred way would be with the lookup tables.
If anyone could help I would appreciate it.

Thanks

John Collyer


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 19 '05 #1
11 18950
WW
John Collyer wrote:
Hi,

In assembly language you can use a lookup table to call functions.

1. Lookup function address in table
2. Call the function

Like:

CALL FUNCTION[INDEX]

FUNCTION DD FUNC1, FUNC2, FUNC3

FUNC1:
FUNC2:
FUNC3:
RET

How can I do this in C++ I would like it to be reasonably fast also?
The way I am doing it now is with a big switch statement. The switch
uses the index number for lookup. This seems like a bad way to do
what I'd would like. The preferred way would be with the lookup
tables. If anyone could help I would appreciate it.


How about a static (initialized) array of function pointers? Or - depending
on what you do - OO and inheritance (vtable)?

--
WW aka Attila
Jul 19 '05 #2
"John Collyer" <jo*********@zoominternet.net> wrote...
In assembly language you can use a lookup table to call functions.

1. Lookup function address in table
2. Call the function

Like:

CALL FUNCTION[INDEX]

FUNCTION DD FUNC1, FUNC2, FUNC3

FUNC1:
FUNC2:
FUNC3:
RET

How can I do this in C++ I would like it to be reasonably fast also?
You would use an array of pointers to functions. If all of them
have the same type (number, order, and types of arguments and the
return value types), of course.
The way I am doing it now is with a big switch statement. The switch
uses the index number for lookup. This seems like a bad way to do
what I'd would like. The preferred way would be with the lookup tables.
If anyone could help I would appreciate it.


#include <iostream>

void print3(int i) {
std::cout << " " << i << std::endl;
}

void print5(int i) {
std::cout << " " << i << std::endl;
}

void print7(int i) {
std::cout << " " << i << std::endl;
}

int main() {
void (*FUNCTION[])(int) = { print3, print5, print7 };
for (int i = 0; i < 20; ++i)
FUNCTION[i % 3](i); // call from a table
return 0;
}

Victor
Jul 19 '05 #3
"John Collyer" <jo*********@zoominternet.net> wrote in message
news:3f**********@corp.newsgroups.com...
Hi,

In assembly language you can use a lookup table to call functions.

1. Lookup function address in table
2. Call the function

Like:

CALL FUNCTION[INDEX]

FUNCTION DD FUNC1, FUNC2, FUNC3

FUNC1:
FUNC2:
FUNC3:
RET

How can I do this in C++ I would like it to be reasonably fast also?
The way I am doing it now is with a big switch statement. The switch
uses the index number for lookup. This seems like a bad way to do
what I'd would like. The preferred way would be with the lookup tables.
If anyone could help I would appreciate it.


Try this:

#include <iostream>
using std::cout;

typedef void(*func_ptr)();

void func1()
{
cout << "func1()\n";
}

void func2()
{
cout << "func2()\n";
}

void func3()
{
cout << "func3()\n";
}

int main()
{
func_ptr func[3] = {func1, func2, func3};
for(int i = 0; i < 3; ++i)
{
func[i]();
}

return 0;
}

Depending on the optimization capabilities of your compiler the resulting
code may be as fast as the assembler equivalent.

Note that in C++ polymorphism might be a more appropriate solution than
using function pointers or switches.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 19 '05 #4
John Collyer wrote:
Hi,

In assembly language you can use a lookup table to call functions.

1. Lookup function address in table
2. Call the function

Like:

CALL FUNCTION[INDEX]

FUNCTION DD FUNC1, FUNC2, FUNC3

FUNC1:
FUNC2:
FUNC3:
RET

How can I do this in C++ I would like it to be reasonably fast also?
The way I am doing it now is with a big switch statement. The switch
uses the index number for lookup. This seems like a bad way to do
what I'd would like. The preferred way would be with the lookup tables.
If anyone could help I would appreciate it.


There are posts that indicate how to do what you're looking for and
they're fast.

However, consider that C++ has another mechanism that is more extensible
and this may solve you problem more effectively ( I don't know, for sure
- it's just an idea spawned by problems I had with function tables.)

C++ allows you to have polymorphic objects - i.e.
struct Poly
{
virtual void WantACracker( Cracker & food ) = 0;

virtual void Say( std::string & words ) = 0;
};
struct Poicephalus : Poly
{
virtual void WantACracker( Cracker & food )
{
if ( food != SunflowerSeed )
{
Say( "YUCK" );
}
}
};
struct Psittacus : Poly
{

Poly()
: fed_count(0)
{
}

int fed_count; // state associated with this Poly

virtual void WantACracker( Cracker & food )
{
if ( food != Ritz )
{
Say( "YUCK" );
}
fed_couint ++;
}
};
Poly * Parrots[] = { new Poicephalus, new Psittacus, new Psittacus };
int main()
{
Parrots[ 1 ]->WantACracker( Ritz );

Parrots[ 0 ]->WantACracker( SunflowerSeed );
}
The cost of the virtual overhead depends greatly on how you use it but
it is usually no more than 2 indirections and a call on most compilers.
(compared to one indirection and call with a function table). However
the flexibilty of having state associated with the function is usually
far more of a performance advantage than you're giving up.
Jul 19 '05 #5

"Peter van Merkerk" <me*****@deadspam.com> wrote in message news:bl************@ID-133164.news.uni-berlin.de...
<snip>
Note that in C++ polymorphism might be a more appropriate solution than
using function pointers or switches.


Or might not. This kind of thing is particularly useful when the
decision about which function should be used isn't made at
compile time.
Jul 19 '05 #6
Ron Ruble wrote:
"Peter van Merkerk" <me*****@deadspam.com> wrote in message news:bl************@ID-133164.news.uni-berlin.de...
<snip>
Note that in C++ polymorphism might be a more appropriate solution than
using function pointers or switches.

Or might not. This kind of thing is particularly useful when the
decision about which function should be used isn't made at
compile time.

How does the "decision not at compile time" change the polymorphic
suggestion ?

Jul 19 '05 #7

"Gianni Mariani" <gi*******@mariani.ws> wrote in message news:bl********@dispatch.concentric.net...
Ron Ruble wrote:
Or might not. This kind of thing is particularly useful when the
decision about which function should be used isn't made at
compile time.


How does the "decision not at compile time" change the polymorphic
suggestion ?


Forgive me, I realize now that I didn't state my thoughts
clearly.

I was thinking of several apps I've worked on where
specific operations were performed based on interpreting
commands in a text file, effectively a user scripted
operation, using functions in a module loaded at
run-time.

In these cases, the -logical- operation was based on
the index into a table of function pointers, where the
index had a meaning known to the programmer
(startup code, load file, shutdown, etc.).

The actual function the pointer was set to was provided
by the dynamic module at run time, based on values
in a text file. Certainly, there are other ways to do this,
including some methods using polymorphism.

But in the specific cases I was thinking of, using a table
of function pointers was clean and simple.

Not that a polymorphic solution would be bad, just
that the table of function pointers was perfectly
adequate, and in the cases I was thinking of , it
eliminated additional design effort.

Jul 19 '05 #8
Ron Ruble wrote:
"Gianni Mariani" <gi*******@mariani.ws> wrote in message news:bl********@dispatch.concentric.net...
Ron Ruble wrote:
....
Not that a polymorphic solution would be bad, just
that the table of function pointers was perfectly
adequate, and in the cases I was thinking of , it
eliminated additional design effort.


I've implemented both function table and polymorphic approaches in a
scenario like this one and I have found the polymorphic system to be
more easily extensible and hence in general less design effort.

I suppose we can chalk this off to different experience.

G

Jul 19 '05 #9
The c code for function lookup table works great, but?
I have the lookup table in a class as a member of that class.
I want to initialize the function lookup table array to my member
function calls of the class inside of the constructor for the class.
What is the correct code to initialize the function pointer array
in the constructor? I have:

inside the class header file

typedef void(*func_ptr)(); // declare pointer to function

class myclass
{
public:
myclass();
virtual ~myclass();

func_ptr Functions[5]; // declare the function pointer array

char F0(size_t address); // member functions
char F1(size_t address);
char F2(size_t address);
char F3(size_t address);
char F4(size_t address);

};

Inside the class source cpp file

myclass:myclass()
{
Functions[] = { F0,F1,F2,F3,F4}
}
myclass::~myclass()
{
}

char myclass::F0(size_t address)
{
}

char myclass::F1(size_t address)
{
}

char myclass::F2(size_t address)
{
}

char myclass::F3(size_t address)
{
}

char myclass::F4(size_t address)
{
}

The actual function pointer lookup table array is 256 functions or pointers
to functions, so the initialization list inside the constructor is quite
long and uses more then one line. Everything works except I get errors with
the array initialization list in the constructor; errors dealing with the
initialation of the array.

syntax error : ']'
error C2143: syntax error : missing ';' before '{'
error C2143: syntax error : missing ';' before '}'
error C2059: syntax error : ']'
syntax error : missing ';' before '{'
error C2143: syntax error : missing ';' before '}'

Any help?

John Collyer

"Peter van Merkerk" <me*****@deadspam.com> wrote in message
news:bl************@ID-133164.news.uni-berlin.de...
"John Collyer" <jo*********@zoominternet.net> wrote in message
news:3f**********@corp.newsgroups.com...
Hi,

In assembly language you can use a lookup table to call functions.

1. Lookup function address in table
2. Call the function

Like:

CALL FUNCTION[INDEX]

FUNCTION DD FUNC1, FUNC2, FUNC3

FUNC1:
FUNC2:
FUNC3:
RET

How can I do this in C++ I would like it to be reasonably fast also?
The way I am doing it now is with a big switch statement. The switch
uses the index number for lookup. This seems like a bad way to do
what I'd would like. The preferred way would be with the lookup tables.
If anyone could help I would appreciate it.


Try this:

#include <iostream>
using std::cout;

typedef void(*func_ptr)();

void func1()
{
cout << "func1()\n";
}

void func2()
{
cout << "func2()\n";
}

void func3()
{
cout << "func3()\n";
}

int main()
{
func_ptr func[3] = {func1, func2, func3};
for(int i = 0; i < 3; ++i)
{
func[i]();
}

return 0;
}

Depending on the optimization capabilities of your compiler the resulting
code may be as fast as the assembler equivalent.

Note that in C++ polymorphism might be a more appropriate solution than
using function pointers or switches.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl



-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 19 '05 #10
Ignore any typos the source is good except howto initialize the function
pointer array inside the constructor

"John Collyer" <jo*********@zoominternet.net> wrote in message
news:3f**********@corp.newsgroups.com...
The c code for function lookup table works great, but?
I have the lookup table in a class as a member of that class.
I want to initialize the function lookup table array to my member
function calls of the class inside of the constructor for the class.
What is the correct code to initialize the function pointer array
in the constructor? I have:

inside the class header file

typedef void(*func_ptr)(); // declare pointer to function

class myclass
{
public:
myclass();
virtual ~myclass();

func_ptr Functions[5]; // declare the function pointer array

char F0(size_t address); // member functions
char F1(size_t address);
char F2(size_t address);
char F3(size_t address);
char F4(size_t address);

};

Inside the class source cpp file

myclass:myclass()
{
Functions[] = { F0,F1,F2,F3,F4}; // semi colen is there typo
}
myclass::~myclass()
{
}

char myclass::F0(size_t address)
{
}

char myclass::F1(size_t address)
{
}

char myclass::F2(size_t address)
{
}

char myclass::F3(size_t address)
{
}

char myclass::F4(size_t address)
{
}

The actual function pointer lookup table array is 256 functions or pointers to functions, so the initialization list inside the constructor is quite
long and uses more then one line. Everything works except I get errors with the array initialization list in the constructor; errors dealing with the
initialation of the array.

syntax error : ']'
error C2143: syntax error : missing ';' before '{'
error C2143: syntax error : missing ';' before '}'
error C2059: syntax error : ']'
syntax error : missing ';' before '{'
error C2143: syntax error : missing ';' before '}'

Any help?

John Collyer

"Peter van Merkerk" <me*****@deadspam.com> wrote in message
news:bl************@ID-133164.news.uni-berlin.de...
"John Collyer" <jo*********@zoominternet.net> wrote in message
news:3f**********@corp.newsgroups.com...
Hi,

In assembly language you can use a lookup table to call functions.

1. Lookup function address in table
2. Call the function

Like:

CALL FUNCTION[INDEX]

FUNCTION DD FUNC1, FUNC2, FUNC3

FUNC1:
FUNC2:
FUNC3:
RET

How can I do this in C++ I would like it to be reasonably fast also?
The way I am doing it now is with a big switch statement. The switch
uses the index number for lookup. This seems like a bad way to do
what I'd would like. The preferred way would be with the lookup tables. If anyone could help I would appreciate it.


Try this:

#include <iostream>
using std::cout;

typedef void(*func_ptr)();

void func1()
{
cout << "func1()\n";
}

void func2()
{
cout << "func2()\n";
}

void func3()
{
cout << "func3()\n";
}

int main()
{
func_ptr func[3] = {func1, func2, func3};
for(int i = 0; i < 3; ++i)
{
func[i]();
}

return 0;
}

Depending on the optimization capabilities of your compiler the resulting code may be as fast as the assembler equivalent.

Note that in C++ polymorphism might be a more appropriate solution than
using function pointers or switches.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl



-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----



-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 19 '05 #11
John Collyer escribió:
typedef void(*func_ptr)(); // declare pointer to function

class myclass
{
public:
myclass();
virtual ~myclass();

func_ptr Functions[5]; // declare the function pointer array


Make Functions static.

static func_ptr Functions [5];

And intialize it in the .cpp file outside any function as:

func_ptr myclass::Functions []= { F0, F1, F2, F3, F4 };

Regards.
Jul 19 '05 #12

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

Similar topics

4
by: Adrian Charteris | last post by:
Hi I'm currently trying to use a lookup table for converting one xml doc to another using a XSLT transformation. Ideally I would like my first xml doc to be converted to my second xml doc below. ...
2
by: DKode | last post by:
Ok, My staff has determined that we will be using custom business objects for a new app we are creating. we have an object that is called Employee. The employee object has about 8 lookup...
2
by: CoreyWhite | last post by:
The future of computer architecture will use lookup tables. Currently computer processor speed outweighs the benefits of using computer memory for lookup tables, except in some cases. As computer...
1
by: Mark Lees | last post by:
Do I need to create a relationship between fields in a table I created for a lookup(combo box) and the actual table that contains the combo box? Or do these lookup tables exist independently of the...
9
by: Koen | last post by:
Hi all, My application uses a lot of lookup tables. I've splitted the frontend (forms, reports, etc) from the backend (data). The database has around 10 different users. The values in the...
10
by: junky_fellow | last post by:
what are lookup tables ? How can they be used to optimise the code ?
0
by: mjsterz | last post by:
I've been working with VB .NET for less than a year and this is the first time I've posted on one of these groups, so let me apologize beforehand if I'm being unclear, not posting my issue...
2
by: Ronald Verbeek | last post by:
I have to deal with several lookup tables like for example: Quality SpecificWeight Hardness TearStrength So I would create a Quality Class to store the values for a single record. I know it...
0
by: larry | last post by:
I'm wondering if there is something already out there for parsing lookup tables, You know, like tax tables, the columns are marital status, and the lookup is income within the rane and marital...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
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)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.