GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Apr-2004, 09:59
incognito54 incognito54 is offline
New Member
 
Join Date: Apr 2004
Posts: 1
incognito54 is on a distinguished road

customizing preprocessor and hash table


i have this implementation of an hash table that i want to use for customizing a preprocessor, but i don't understand quite right how it is implemented and used
CPP / C++ / C Code:
#include <string.h>
#include <stdlib.h>

#ifndef HASH_H
#define HASH_H
typedef struct witem {
  char *word;
  struct witem *next;
} WORDITEM;
extern WORDITEM *lookup(char *);
extern WORDITEM *insert(char *word);
#endif 


#define TABDIM 501


static WORDITEM *symtab[TABDIM];

static unsigned hash_value(char *word) {
  unsigned hval;
  for (hval = 0; *word; )
    hval = *(word++) + 31*hval;
  
  return hval%TABDIM;
}


WORDITEM *lookup(char *word) {
  int hval = hash_value(word), comp = 1;
  WORDITEM *px = symtab[hval];

  for (; px && (comp = strcmp(px->word, word)) < 0; px = px->next);

  return comp ? NULL : px;
}


WORDITEM *insert(char *word) {
  int hval = hash_value(word), comp;
  WORDITEM *px = symtab[hval], *py = px, *pw;

  for (; px && (comp = strcmp(px->word, word)) < 0; py = px, px = px->next)
    ;
  
  if (!px || comp) {
    pw = (WORDITEM*)malloc(sizeof(WORDITEM));
    pw->word = (char*)malloc(strlen(word) + 1);
    strcpy(pw->word, word);
    pw->next = px;
    if (px == py) 
      symtab[hval] = pw;
    else
      py->next = pw;
  } else
    return px; 

  return pw;
}
what i wanted was to use this hash table in making #define but with another name like #definir...
EX: #definir AAA 2
what it would was to replace AAA with 2.. i have already made my version of include...

usage of the program:

preprocess test.c out.c

can someone help me??
Last edited by dsmith : 21-Apr-2004 at 07:37. Reason: Use [c] & [/c] for syntax highlighting
  #2  
Old 21-Apr-2004, 10:48
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi incognito54. Sorry for the delay in responding. I was going to see if anyone else understood what you needed, because I am bit confused.

Quote:
what i wanted was to use this hash table in making #define but with another name like #definir...
EX: #definir AAA 2
what it would was to replace AAA with 2.. i have already made my version of include...

usage of the program:

preprocess test.c out.c

It sounds like you want to make some sort of preprocessor to run your source files through? I am not sure what exactly you are trying to accomplish. Sorry, if you could give some more explanation, perhaps someone here could offer some help.

Cheers,
d
  #3  
Old 21-Apr-2004, 18:25
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by dsmith
Hi incognito54. Sorry for the delay in responding. I was going to see if anyone else understood what you needed, because I am bit confused.
People on the CProgramming board are just as confused, so don't feel bad. He's trying to learn C by rewriting it.
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:51.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.