Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

wshash.c File Reference

#include "wsint.h"
#include "wshash.h"

Include dependency graph for wshash.c:

Include dependency graph

Go to the source code of this file.

Data Structures

struct  WsHashItemRec
struct  WsHashRec

Defines

#define WS_HASH_TABLE_SIZE   256

Typedefs

typedef WsHashItemRec WsHashItem

Functions

size_t count_hash (const char *string)
WsHashPtr ws_hash_create (WsHashItemDestructor destructor, void *context)
void ws_hash_destroy (WsHashPtr hash)
WsBool ws_hash_put (WsHashPtr hash, const char *name, void *data)
void * ws_hash_get (WsHashPtr hash, const char *name)
void ws_hash_clear (WsHashPtr hash)


Define Documentation

#define WS_HASH_TABLE_SIZE   256
 

Definition at line 76 of file wshash.c.


Typedef Documentation

typedef struct WsHashItemRec WsHashItem
 

Definition at line 86 of file wshash.c.

Referenced by ws_hash_clear(), ws_hash_get(), and ws_hash_put().


Function Documentation

size_t count_hash const char *  string  )  [static]
 

Definition at line 200 of file wshash.c.

References string.

Referenced by ws_hash_get(), and ws_hash_put().

00201 {
00202     size_t val = 0;
00203     int i;
00204 
00205     for (i = 0; string[i]; i++) {
00206         val <<= 3;
00207         val ^= string[i];
00208         val ^= (val & 0xff00) >> 5;
00209         val ^= (val & 0xff0000) >> 16;
00210     }
00211 
00212     return val % WS_HASH_TABLE_SIZE;
00213 }

void ws_hash_clear WsHashPtr  hash  ) 
 

Definition at line 180 of file wshash.c.

References WsHashItemRec::data, WsHashRec::destructor, WsHashRec::destructor_context, WsHashRec::items, WsHashItemRec::name, WsHashItemRec::next, ws_free(), WsHashItem, and WsHashPtr.

Referenced by ws_hash_destroy().

00181 {
00182     WsHashItem *i, *n;
00183     size_t j;
00184 
00185     for (j = 0; j < WS_HASH_TABLE_SIZE; j++) {
00186         for (i = hash->items[j]; i; i = n) {
00187             n = i->next;
00188             if (hash->destructor)
00189                 (*hash->destructor)(i->data, hash->destructor_context);
00190 
00191             ws_free(i->name);
00192             ws_free(i);
00193         }
00194         hash->items[j] = NULL;
00195     }
00196 }

Here is the call graph for this function:

WsHashPtr ws_hash_create WsHashItemDestructor  destructor,
void *  context
 

Definition at line 103 of file wshash.c.

References WsHashRec::destructor, WsHashRec::destructor_context, ws_calloc(), and WsHashPtr.

Referenced by ws_function_hash_create(), ws_pragma_use_hash_create(), and ws_variable_hash_create().

00104 {
00105     WsHashPtr hash = ws_calloc(1, sizeof(*hash));
00106 
00107     if (hash) {
00108         hash->destructor = destructor;
00109         hash->destructor_context = context;
00110     }
00111 
00112     return hash;
00113 }

Here is the call graph for this function:

void ws_hash_destroy WsHashPtr  hash  ) 
 

Definition at line 116 of file wshash.c.

References ws_free(), ws_hash_clear(), and WsHashPtr.

Referenced by compile_stream().

00117 {
00118     if (hash == NULL)
00119         return;
00120 
00121     ws_hash_clear(hash);
00122     ws_free(hash);
00123 }

Here is the call graph for this function:

void* ws_hash_get WsHashPtr  hash,
const char *  name
 

Definition at line 167 of file wshash.c.

References count_hash(), WsHashItemRec::data, WsHashRec::items, name, WsHashItemRec::name, WsHashItemRec::next, WsHashItem, and WsHashPtr.

Referenced by ws_expr_linearize(), ws_function_hash(), ws_pragma_use(), ws_variable_define(), and ws_variable_lookup().

00168 {
00169     WsHashItem *i;
00170     size_t h = count_hash(name);
00171 
00172     for (i = hash->items[h]; i; i = i->next)
00173         if (strcmp(i->name, name) == 0)
00174             return i->data;
00175 
00176     return NULL;
00177 }

Here is the call graph for this function:

WsBool ws_hash_put WsHashPtr  hash,
const char *  name,
void *  data
 

Definition at line 126 of file wshash.c.

References count_hash(), WsHashItemRec::data, WsHashRec::destructor, WsHashRec::destructor_context, WsHashRec::items, name, WsHashItemRec::name, WsHashItemRec::next, ws_calloc(), ws_free(), ws_strdup(), WsBool, WsHashItem, and WsHashPtr.

Referenced by ws_function_hash(), ws_pragma_use(), and ws_variable_define().

00127 {
00128     WsHashItem *i;
00129     size_t h = count_hash(name);
00130 
00131     for (i = hash->items[h]; i; i = i->next) {
00132         if (strcmp(i->name, name) == 0) {
00133             /* Found it. */
00134 
00135             /* Destroy the old item */
00136             if (hash->destructor)
00137                 (*hash->destructor)(i->data, hash->destructor_context);
00138 
00139             i->data = data;
00140 
00141             return WS_FALSE;
00142         }
00143     }
00144 
00145     /* Must create a new mapping. */
00146     i = ws_calloc(1, sizeof(*i));
00147 
00148     if (i == NULL)
00149         return WS_FALSE;
00150 
00151     i->name = ws_strdup(name);
00152     if (i->name == NULL) {
00153         ws_free(i);
00154         return WS_FALSE;
00155     }
00156 
00157     i->data = data;
00158 
00159     /* Link it to our hash. */
00160     i->next = hash->items[h];
00161     hash->items[h] = i;
00162 
00163     return WS_TRUE;
00164 }

Here is the call graph for this function:

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.