typedef struct phash_list {
struct phash_list* next;
struct phash_list* prev;
char* key;
void* val;
} PHashList;
typedef struct phash {
unsigned int count;
PHashList* tbl[MAX_HASH];
} PHash;
PHash* pugH_new() {
PHash* h = malloc(sizeof(PHash));
h->count = 0;
return h;
}
/* main hash function */
static unsigned int gethash(char* str) {
unsigned int hash = 1315423911;
unsigned int i = 0;
unsigned int len = strlen(str);
for (i = 0; i < len; str++, i++)
hash ^= ((hash << 5) + (*str) + (hash >> 2));
return hash % MAX_HASH;
}
void pugH_insert(PHash *h, char *k, void *v) {
unsigned int hash = gethash(k);
PHashList *l = PHashList_new(k,v);
l->next = h->tbl[hash];
h->tbl[hash] = l;
h->count++;
}
void* pugH_rawlookup(PHash* h, char* k) {
PHashList *l;
for (l=h->tbl[gethash(k)]; l != NULL; l = l->next) {
if (!strcmp(k, l->key)) /* error! */
return l->val;
}
return NULL;
}
but someone clarified that it uses a tree. After a little research it appears to use a "Red-Black-Tree" which is a
self balancing BST. I've enjoyed researching these concepts.
My long term goal is to develop my own language in ANSI-C. I am considering implementing my own associative
array for obvious reasons. I am trying to outweigh the benefits of using either a RBT vs Hash.
Any thoughts? Or, are there any resources out there that you have seen? Such as available libraries with liberal licenses?