Solved

Lookup Tables

Posted on 2000-04-14
14
393 Views
Last Modified: 2010-04-15
I have been hearing the term lookuptables for a long time. What exactly is a lookup table, and how do you implement one in C? The only way I can think of is with if statments or using the switch block. I have looked through some of my old C books but there is no mention of lookup tables. Any info will be helpfull code would be great too.
0
Comment
Question by:Sasho
  • 7
  • 6
14 Comments
 
LVL 1

Expert Comment

by:mournblade
ID: 2718379
a lookup table is something really simple: it is a simple table implemented as an array or a linked list (for a variable number of possible answers). each of the elements in the array contains the answer related to a question that has the index as a parameter.

for example, if you want to know the number of bits set to one in a byte, you could have a lookup table (an array) of 256 int (int table[256]) that has the answer for each of the values a byte can have. e.g. if the byte you want to know the number of set bits has value 131 (10000011 in binary, 3 bits are set (i.e. have value '1'), you would look in the array at index 131 and search for your answer, which would be 3.

the table would have been filled previously with the following values:
....
table[127] = 7; /* (01111111) */
table[128] = 1; /* (10000000) */
table[129] = 2; /* (10000001) */
table[130] = 2; /* (10000010) */
table[131] = 3; /* (10000011) */
table[132] = 2; /* (10000100) */
....

so if you do a simple assignment like this:
int set_bits_131 = table[131];

set_bits would be equal to 3 and there's your answer.
simple, no? :)
0
 
LVL 3

Author Comment

by:Sasho
ID: 2718397
I understand your answer completely mornblade. But my perticular situation is a bit more complicated. I have to use a command line option to assgin two values.

Example:

C:\vref v1_2

so based on the v1_2 i have to assign to values. I can just do:

if(strcmp(argv[1],"v1_2)==0)
 {
  device=1;
  WCR=2;
 }

But with many different options it is a nightmqre. So someone suggested to use a lookup table. I will not reject your answer but I hope you can help with my issue.
0
 
LVL 1

Expert Comment

by:mournblade
ID: 2718462
if the format of your argument is always 'vX_Y', why don't you parse the string to extract X and Y, convert them to integers and assign them to device and WCR? i.e. :

char *deviceStr;
char *WCRStr;
char *arg = (char*) malloc (sizeof(char) * (strlen(argv[1]) + 1));
int pos;

strcpy(arg, argv[1]);

/* starting at 1, not 0 to discard letter v */
/* search for '_'*/
for (pos = 1; arg[pos] != '_'; pos++);

arg[pos] = '\0'; /* separate the string in 2 */
deviceStr = (char*) malloc (sizeof(char) * pos);
strcpy(deviceStr, (arg+1)); /* will copy from X to '_'*/

/* here strlen() gives length from character after '_' to the end of arg */
WCRStr = (char*) malloc (sizeof(char) * (strlen(arg + pos + 1) + 1));
strcpy(WCRStr, (arg+pos+1)); /* will copy from Y to end of arg */

device = atoi(deviceStr);
WCR = atoi(WCRStr);

free(deviceStr);
free(WCRStr);
free(arg);

that works, i've tested it. and you don't need a lookup table.
0
 
LVL 3

Author Comment

by:Sasho
ID: 2720832
Well the idea is good. But in my case
the argument vX_Y is not the way you see it X and Y are not device and WCR. Basically the argument will be a string and based on the string I will decide on what device and which WCR to use. I thought about your original idea and I guess I can implement what I need with a 3xN array where N is the number of possible arguments. So that each row will have the string, a device and a WCR. I am not really sure how to get mixed types in an array. I'll try to play around with it. Let me lnow what you think.
0
 
LVL 1

Expert Comment

by:mournblade
ID: 2720934
oops, my mistake. i shouldn't have jumped to conclusion that the 1s and 2s in the string and the values you want were the same things.

instead of an array 3xN use and array of N structures that contains your 3 types, i.e.

typedef struct
{
  char *arg;
  int device;
  int WCR;
} myStruct_t;

myStruct_t array[N];

and fill the array, the search by strcmp array[ii].arg with argv[1]

but then you have kinda the same problem that you had at the beginning because you have to actually fill the array, each element 1 by 1, and you needed a loop with strcmp to serach the array. it's not possible to do a lookup table with strings as a search parameter, you needed something that can be casted as an integer to be used as the index.
0
 
LVL 3

Author Comment

by:Sasho
ID: 2720968
Yeah I just finished the implementation using an array of structs and I am filling the array one by one and having a loop using strcmp. Thats the best I could think of. I was thinking of enumerating the arguments but don't know how that will work.

example:

enum args {v1_2,v1_8,v1_5}

to acess:

device =table[v1_2][0]
wiper  =table[v1_2][1]

the problem is converiting the string in argv[] to the enum type I am not really sure of how to do it.
0
 
LVL 84

Expert Comment

by:ozo
ID: 2721007
#define table \
x(v1_2) \
x(v1_8) \
x(v1_5) \
/* end table */
#define x(arg)  arg,
enum args { table n_args };
#undef x
#define x(arg) # arg ,
char *strings[]={ table "undefined" };
#undef x
int convert(char *s){
  #define x(arg) if( strcmp( # arg , s ) == 0 ){ return arg; }
  table
  #undef x
  return n_args;
}

main(){
  printf("%s\n",strings[convert("v1_8")]);
}
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Author Comment

by:Sasho
ID: 2721043
ozo,

would you mind disecting your code with some comments. I am trying to figure out what exactly it is doing It seems that it converts a string to a enum type? But I am not really sure. Is this the whole table implemenattion? I am lost
0
 
LVL 1

Expert Comment

by:mournblade
ID: 2721274
ozo's code actually works! i don't understand all of it but it actually converts a string to an int and then uses the int as an index to search the table and return the string to be printed out. i've never seen preprocessing used to such extent before.
0
 
LVL 1

Expert Comment

by:mournblade
ID: 2722843
hmm, i dissected ozo's code and i does exactly what you're doing with your code: it does a strcmp() on every first element of the array. the only difference is that the code is more compact and much more difficult to read.
0
 
LVL 3

Author Comment

by:Sasho
ID: 2723522
Hmm...where would the device and wiper columns of the lookup-table be stored? And you are right the code is difficult to read. I think for code managment it will be a lot better to code it using what we mentioned earlier, after all I will have to move on to other projects and I don't want people who inherit ownership of my code to come to me every so often so that I can translate it to them. So far the implementation I have seems to work but I am not sure if it can be called a lookup table :)

struct lookup{
      char arg[5];
      unsigned char device;
      unsigned char pot;
};

struct lookup table[16];

void initialize_lookup_table()
{

      strcpy(table[0].arg,"v1_2");
      table[0].device=0;table[0].pot=0;


      strcpy(table[1].arg,"v1_8");
      table[1].device=0;table[1].pot=1;


      strcpy(table[2].arg,"v1_5");
      table[2].device=0;table[2].pot=2;


      strcpy(table[3].arg,"v3_3");
      table[3].device=0;table[3].pot=3;

}


unsigned char check_argument(char * arg,unsigned char * device,unsigned char * pot)
{
  int j;

  for(j=0;j<16;j++)
      if(strcmp(table[j].arg,arg)==0)
      {
            *device = table[j].device;
            *pot  = table[j].pot;
            return TRUE;
      }

 return FALSE;
}



mornblade let me know what you think. Is there anything you could add?
0
 
LVL 1

Accepted Solution

by:
mournblade earned 150 total points
ID: 2723981
seems good. and you don't waste time finding what enum goes with what string. code is more efficient and much more readable. and no, i don't think it can be called a lookup table anymore :)

personnally, i would have make the fcn check_argument return the index and copy the values for pot and device in the place that fcn is called, and return -1 if not found. i.e.

#define NB_ENTRIES 16
....
int index;
if ((index = check_argument(argv[1])) != -1)
{
  pot = table[index].pot;
  device = table[index].device;
}
else
{
  /* ERROR! */
}
....

char check_argument(char *arg)
{
  for (int ii=0; ii < NB_ENTRIES; ii++)
  {
    if (strcmp(table[ii].arg, arg) == 0)
      return ii;
  }

  return -1;
}

more efficient, since you pass less parameters (smaller stack, less overhead) and, the table being a global var, you have access to its components everywhere. this is only personal taste however. do it the way you want :)
0
 
LVL 3

Author Comment

by:Sasho
ID: 2724004
I like your last comment as the answer for this whole issue. I'll try to accept it as the answer.
0
 
LVL 3

Author Comment

by:Sasho
ID: 2724011
Well it seems the specs for this program of mine keep changing from day to day. This lookup table thing is getting out of hand. Now they want an otion to use a file as a lookup table initialization, I guess it is easier for the user to change the values. To me its just one more thing the user can screw up. Oh well...thanks for the help guys.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now