Wild Life  2.30
 All Data Structures Files Functions Variables Typedefs Macros
Functions | Variables
hash_table.c File Reference

keyword hash tables More...

Go to the source code of this file.

Functions

ptr_hash_table hash_create (int size)
 HASH_CREATE. More...
 
void hash_expand (ptr_hash_table table, int new_size)
 HASH_EXPAND. More...
 
int hash_code (ptr_hash_table table, char *symbol)
 HASH_CODE. More...
 
int hash_find (ptr_hash_table table, char *symbol)
 hash_find More...
 
ptr_keyword hash_lookup (ptr_hash_table table, char *symbol)
 HASH_LOOKUP. More...
 
void hash_insert (ptr_hash_table table, char *symbol, ptr_keyword keyword)
 HASH_INSERT. More...
 
void hash_display (ptr_hash_table table)
 HASH_DISPLAY. More...
 

Variables

ptr_definition first_definition =NULL
 
long rand_array [256]
 

Detailed Description

keyword hash tables

KEYWORDS RM: Feb 3 1993 New version of the keyword table and related routines. The keyword table will not NOT be sorted, however, access will be hashed. Each module has its own hash table of symbols. All definition are stores in a linked list starting at first_definition.

Definition in file hash_table.c.

Function Documentation

int hash_code ( ptr_hash_table  table,
char *  symbol 
)

HASH_CODE.

Parameters
table- ptr_hash_table table
symbol- char *symbol

Return the hash code for a symbol

Definition at line 79 of file hash_table.c.

References rand_array, and wl_hash_table::size.

80 {
81  int n=0;
82 
83  /* printf("code of %s ",symbol); */
84 
85  while(*symbol) {
86  n ^= rand_array[*symbol]+rand_array[n&255];
87  n++;
88  symbol++;
89  }
90 
91  n &= (table->size-1);
92 
93 
94  /* printf("=%d\n",n); */
95 
96  return n;
97 }
long rand_array[256]
Definition: hash_table.c:16
ptr_hash_table hash_create ( int  size)

HASH_CREATE.

Parameters
size- int size

Create a hash-table for max size keywords.

Definition at line 25 of file hash_table.c.

References NULL, and wl_hash_table::size.

26 {
27  ptr_hash_table new;
28  int i;
29 
30  new=(ptr_hash_table)malloc(sizeof(struct wl_hash_table));
31  new->size=size;
32  new->used=0;
33  new->data=(ptr_keyword *)malloc(size*sizeof(ptr_keyword));
34  for(i=0;i<size;i++)
35  new->data[i]=NULL;
36  return new;
37 }
#define NULL
Definition: def_const.h:533
struct wl_hash_table * ptr_hash_table
Definition: def_struct.h:100
void hash_display ( ptr_hash_table  table)

HASH_DISPLAY.

Parameters
table- ptr_hash_table table

Display a symbol table (for debugging).

Definition at line 174 of file hash_table.c.

References hash_code().

175 {
176  int i;
177  int n;
178  char *s;
179  int c=0;
180  int t=0;
181 
182  printf("*** Hash table %lx:\n",(long)table);
183  printf("Size: %d\n",table->size);
184  printf("Used: %d\n",table->used);
185 
186  for(i=0;i<table->size;i++)
187  if(table->data[i]) {
188  t++;
189  s=table->data[i]->symbol;
190  n=hash_code(table,s);
191 
192  printf("%4d %4d %s %s\n",
193  i,
194  n,
195  i==n?"ok ":"*bad*",
196  s);
197 
198  if(i!=n)
199  c++;
200  }
201 
202  printf("Really used: %d\n",t);
203  printf("Collisions: %d = %1.3f%%\n",
204  c,
205  100.0*c/(double)t);
206 }
ptr_keyword * data
Definition: def_struct.h:139
char * symbol
Definition: def_struct.h:118
int hash_code(ptr_hash_table table, char *symbol)
HASH_CODE.
Definition: hash_table.c:79
void hash_expand ( ptr_hash_table  table,
int  new_size 
)

HASH_EXPAND.

Parameters
table- ptr_hash_table table
new_size- int new_size

Allocate a bigger hash table.

Definition at line 47 of file hash_table.c.

References wl_hash_table::data, hash_insert(), NULL, wl_hash_table::size, and wl_hash_table::used.

48 {
49  ptr_keyword *old_data;
50  int old_size;
51  int i;
52 
53 
54  old_data=table->data;
55  old_size=table->size;
56 
57  table->size=new_size; /* Must be power of 2 */
58  table->used=0;
59  table->data=(ptr_keyword *)malloc(new_size*sizeof(ptr_keyword));
60 
61  for(i=0;i<new_size;i++)
62  table->data[i]=NULL;
63 
64  for(i=0;i<old_size;i++)
65  if(old_data[i])
66  hash_insert(table,old_data[i]->symbol,old_data[i]);
67 
68  free(old_data);
69 }
ptr_keyword * data
Definition: def_struct.h:139
#define NULL
Definition: def_const.h:533
void hash_insert(ptr_hash_table table, char *symbol, ptr_keyword keyword)
HASH_INSERT.
Definition: hash_table.c:151
int hash_find ( ptr_hash_table  table,
char *  symbol 
)

hash_find

Parameters
table- ptr_hash_table table
symbol- char *symbol

Definition at line 106 of file hash_table.c.

References wl_hash_table::data, hash_code(), wl_hash_table::size, and wl_keyword::symbol.

107 {
108  int n;
109  int i=1;
110 
111  n=hash_code(table,symbol);
112 
113  while(table->data[n] && strcmp(table->data[n]->symbol,symbol)) {
114  /* Not a direct hit... */
115  n+= i*i;
116  /* i++; */
117  n &= table->size-1;
118  }
119 
120  return n;
121 }
ptr_keyword * data
Definition: def_struct.h:139
char * symbol
Definition: def_struct.h:118
int hash_code(ptr_hash_table table, char *symbol)
HASH_CODE.
Definition: hash_table.c:79
void hash_insert ( ptr_hash_table  table,
char *  symbol,
ptr_keyword  keyword 
)

HASH_INSERT.

Parameters
table- ptr_hash_table table
symbol- char *symbol
keyword- ptr_keyword keyword

Add a symbol and data to a table. Overwrite previous data.

Definition at line 151 of file hash_table.c.

References wl_hash_table::data, hash_expand(), hash_find(), wl_hash_table::size, and wl_hash_table::used.

152 {
153  int n;
154 
155  n=hash_find(table,symbol);
156 
157  /* printf("inserting %s at %d keyword %x\n",symbol,n,keyword); */
158 
159  if(!table->data[n])
160  table->used++;
161  table->data[n]=keyword;
162 
163  if(table->used*2>table->size)
164  hash_expand(table,table->size*2);
165 }
ptr_keyword * data
Definition: def_struct.h:139
int hash_find(ptr_hash_table table, char *symbol)
hash_find
Definition: hash_table.c:106
void hash_expand(ptr_hash_table table, int new_size)
HASH_EXPAND.
Definition: hash_table.c:47
ptr_keyword hash_lookup ( ptr_hash_table  table,
char *  symbol 
)

HASH_LOOKUP.

Parameters
table- ptr_hash_table table
symbol- char *symbol

Look up a symbol in the symbol table.

Definition at line 131 of file hash_table.c.

References wl_hash_table::data, and hash_find().

132 {
133  int n;
134 
135  n=hash_find(table,symbol);
136 
137  /* printf("found %s at %d keyword %x\n",symbol,n,table->data[n]); */
138 
139  return table->data[n];
140 }
ptr_keyword * data
Definition: def_struct.h:139
int hash_find(ptr_hash_table table, char *symbol)
hash_find
Definition: hash_table.c:106

Variable Documentation

ptr_definition first_definition =NULL

Definition at line 14 of file hash_table.c.

long rand_array[256]

Definition at line 16 of file hash_table.c.