#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "symtab.h"
#include <limits.h>
Go to the source code of this file.
Defines |
| #define | HASH(x) hash(x) |
| #define | BITS_IN_int ( sizeof(int) * CHAR_BIT ) |
| #define | THREE_QUARTERS ((int) ((BITS_IN_int * 3) / 4)) |
| #define | ONE_EIGHTH ((int) (BITS_IN_int / 8)) |
| #define | HIGH_BITS ( ~((unsigned int)(~0) >> ONE_EIGHTH )) |
Functions |
| unsigned int | HashPJW (const char *) |
| unsigned int | hash (const char *) |
| SYMTABLE * | new_symtable (unsigned int numentries) |
| int | hash_insert (SYMTABLE *table, void *node_val, char *tag) |
| HASHNODE * | hash_delete (SYMTABLE *table, char *tag) |
| HASHNODE * | hash_lookup (SYMTABLE *table, char *id) |
| HASHNODE * | search_hashlist (HASHNODE *list, char *id) |
| void | hash_dump (SYMTABLE *table, void(*dump_function)(char *, void *)) |
Define Documentation
| #define BITS_IN_int ( sizeof(int) * CHAR_BIT ) |
| #define HASH |
( |
x |
|
) |
hash(x) |
| #define HIGH_BITS ( ~((unsigned int)(~0) >> ONE_EIGHTH )) |
| #define ONE_EIGHTH ((int) (BITS_IN_int / 8)) |
| #define THREE_QUARTERS ((int) ((BITS_IN_int * 3) / 4)) |
Function Documentation
| unsigned int hash |
( |
const char * |
str |
) |
|
Definition at line 248 of file symtab.c.
{
int sum = 0;
if (str == NULL)
return 0;
while (*str)
sum += *str++;
return sum;
}
| HASHNODE* hash_delete |
( |
SYMTABLE * |
table, |
|
|
char * |
tag | |
|
) |
| | |
Definition at line 112 of file symtab.c.
{
HASHNODE *list, *prev;
int idx;
if ((table == NULL) || (tag == NULL))
return NULL;
idx = HASH(tag) % table->num_entries;
list = table->entry[idx];
for (prev = NULL; list; list = list->next) {
if (list->tag == NULL)
prev = list;
else if (!strcmp(list->tag, tag)) {
if (prev)
prev->next = list->next;
else
table->entry[idx] = list->next;
table->num_items--;
return (list);
}
prev = list;
}
return NULL;
}
| void hash_dump |
( |
SYMTABLE * |
table, |
|
|
void(*)(char *, void *) |
dump_function | |
|
) |
| | |
Definition at line 219 of file symtab.c.
{
HASHNODE *tmp;
int i;
for(i=0;i<table->num_entries;i++) {
for(tmp = table->entry[i]; tmp != NULL; tmp = tmp->next) {
dump_function(tmp->tag, tmp->item);
}
}
return;
}
| int hash_insert |
( |
SYMTABLE * |
table, |
|
|
void * |
node_val, |
|
|
char * |
tag | |
|
) |
| | |
Definition at line 76 of file symtab.c.
{
HASHNODE *newnode;
int idx;
if ((table == NULL) || (tag == NULL))
return -1;
idx = HASH(tag) % table->num_entries;
newnode = (HASHNODE *) malloc(sizeof(HASHNODE));
if (!newnode)
return -1;
newnode->tag = tag;
newnode->item = node_val;
newnode->next = table->entry[idx];
table->entry[idx] = newnode;
table->num_items++;
return 0;
}
| HASHNODE* hash_lookup |
( |
SYMTABLE * |
table, |
|
|
char * |
id | |
|
) |
| | |
Definition at line 156 of file symtab.c.
{
int index;
HASHNODE *hash_entry;
if ((table == NULL) || (id == NULL))
return NULL;
index = HASH(id) % table->num_entries;
hash_entry = search_hashlist(table->entry[index], id);
if (hash_entry == NULL) {
#ifdef SYM_DEBUG
printf("Not in table.\n");
#endif
return NULL;
} else {
#ifdef SYM_DEBUG
printf("In table.\n");
#endif
return (hash_entry);
}
}
| unsigned int HashPJW |
( |
const char * |
datum |
) |
|
Definition at line 279 of file symtab.c.
{
unsigned int hash_value, i;
for (hash_value = 0; *datum; ++datum) {
hash_value = (hash_value << ONE_EIGHTH) + *datum;
if ((i = hash_value & HIGH_BITS) != 0)
hash_value = (hash_value ^ (i >> THREE_QUARTERS)) & ~HIGH_BITS;
}
return (hash_value);
}
| SYMTABLE* new_symtable |
( |
unsigned int |
numentries |
) |
|
Definition at line 40 of file symtab.c.
{
SYMTABLE *newtable;
newtable = (SYMTABLE *) malloc(sizeof(SYMTABLE));
if (!newtable)
return NULL;
newtable->num_entries = numentries;
newtable->num_items = 0;
newtable->entry = (HASHNODE **) calloc(numentries, sizeof(HASHNODE *));
if (!newtable->entry) {
free(newtable);
return NULL;
}
return (newtable);
}
| HASHNODE* search_hashlist |
( |
HASHNODE * |
list, |
|
|
char * |
id | |
|
) |
| | |
Definition at line 194 of file symtab.c.
{
if (id == NULL)
return NULL;
for (; list; list = list->next) {
if (list->tag == NULL)
continue;
if (!strcmp(list->tag, id))
return (list);
}
return NULL;
}