#include "xhash.h"
#include "util.h"
static int _xhasher(const char *s, int len)
{
const unsigned char *name = (const unsigned char *)s;
unsigned long h = 0, g;
int i;
for(i=0;i<len;i++)
{
h = (h << 4) + (unsigned long)(name[i]);
if ((g = (h & 0xF0000000UL))!=0)
h ^= (g >> 24);
h &= ~g;
}
return (int)h;
}
static xhn _xhash_node_new(xht h, int index)
{
xhn n;
int i = index % h->prime;
h->count++;
for(n = &h->zen[i]; n != NULL; n = n->next)
if(n->key == NULL)
return n;
n = pmalloco(h->p, sizeof(_xhn));
n->next = h->zen[i].next;
h->zen[i].next = n;
return n;
}
static xhn _xhash_node_get(xht h, const char *key, int len, int index)
{
xhn n;
int i = index % h->prime;
for(n = &h->zen[i]; n != NULL; n = n->next)
if(n->key != NULL && (strlen(n->key)==len) && (strncmp(key, n->key, len) == 0))
return n;
return NULL;
}
xht xhash_new(int prime)
{
xht xnew;
pool_t p;
p = pool_heap(sizeof(_xhn)*prime + sizeof(_xht));
xnew = pmalloco(p, sizeof(_xht));
xnew->prime = prime;
xnew->p = p;
xnew->zen = pmalloco(p, sizeof(_xhn)*prime);
xnew->iter_bucket = -1;
xnew->iter_node = NULL;
return xnew;
}
void xhash_putx(xht h, const char *key, int len, void *val)
{
int index;
xhn n;
if(h == NULL || key == NULL)
return;
index = _xhasher(key,len);
h->dirty++;
if((n = _xhash_node_get(h, key, len, index)) != NULL)
{
n->key = key;
n->val = val;
return;
}
n = _xhash_node_new(h, index);
n->key = key;
n->val = val;
}
void xhash_put(xht h, const char *key, void *val)
{
if(h == NULL || key == NULL) return;
xhash_putx(h,key,strlen(key),val);
}
void *xhash_getx(xht h, const char *key, int len)
{
xhn n;
if(h == NULL || key == NULL || len <= 0 || (n = _xhash_node_get(h, key, len, _xhasher(key,len))) == NULL)
{
return NULL;
}
return n->val;
}
void *xhash_get(xht h, const char *key)
{
if(h == NULL || key == NULL) return NULL;
return xhash_getx(h,key,strlen(key));
}
void xhash_zapx(xht h, const char *key, int len)
{
xhn n;
if(h == NULL || key == NULL || (n = _xhash_node_get(h, key, len, _xhasher(key,len))) == NULL)
return;
n->key = NULL;
n->val = NULL;
h->dirty++;
h->count--;
if(h->iter_node == n)
xhash_iter_next(h);
}
void xhash_zap(xht h, const char *key)
{
if(h == NULL || key == NULL) return;
xhash_zapx(h,key,strlen(key));
}
void xhash_free(xht h)
{
if(h != NULL)
pool_free(h->p);
}
void xhash_walk(xht h, xhash_walker w, void *arg)
{
int i;
xhn n;
if(h == NULL || w == NULL)
return;
for(i = 0; i < h->prime; i++)
for(n = &h->zen[i]; n != NULL; n = n->next)
if(n->key != NULL && n->val != NULL)
(*w)(h, n->key, n->val, arg);
}
int xhash_dirty(xht h)
{
int dirty;
if(h == NULL) return 1;
dirty = h->dirty;
h->dirty = 0;
return dirty;
}
int xhash_count(xht h)
{
if(h == NULL) return 0;
return h->count;
}
pool_t xhash_pool(xht h)
{
return h->p;
}
int xhash_iter_first(xht h) {
if(h == NULL) return 0;
h->iter_bucket = -1;
h->iter_node = NULL;
return xhash_iter_next(h);
}
int xhash_iter_next(xht h) {
if(h == NULL) return 0;
while(h->iter_node != NULL) {
h->iter_node = h->iter_node->next;
if(h->iter_node != NULL && h->iter_node->key != NULL && h->iter_node->val != NULL)
return 1;
}
for(h->iter_bucket++; h->iter_bucket < h->prime; h->iter_bucket++) {
h->iter_node = &h->zen[h->iter_bucket];
while(h->iter_node != NULL) {
if(h->iter_node->key != NULL && h->iter_node->val != NULL)
return 1;
h->iter_node = h->iter_node->next;
}
}
h->iter_bucket = -1;
h->iter_node = NULL;
return 0;
}
void xhash_iter_zap(xht h) {
if(h == NULL) return;
if(h->iter_node == NULL)
return;
h->iter_node->key = NULL;
h->iter_node->val = NULL;
h->dirty++;
h->count--;
xhash_iter_next(h);
}
int xhash_iter_get(xht h, const char **key, void **val) {
if(h == NULL || (key == NULL && val == NULL)) return 0;
if(h->iter_node == NULL) {
if(key != NULL) *key = NULL;
if(val != NULL) *val = NULL;
return 0;
}
if(key != NULL) *key = h->iter_node->key;
if(val != NULL) *val = h->iter_node->val;
return 1;
}