update xhash

This commit is contained in:
Yuichi Nishiwaki 2014-09-20 11:45:40 +09:00
parent b212e278a0
commit 1d7669a5d4
1 changed files with 25 additions and 21 deletions

View File

@ -43,7 +43,7 @@ typedef struct xhash {
size_t size, count, kwidth, vwidth; size_t size, count, kwidth, vwidth;
xh_hashf hashf; xh_hashf hashf;
xh_equalf equalf; xh_equalf equalf;
xh_entry *chain; xh_entry *head, *tail;
void *data; void *data;
} xhash; } xhash;
@ -98,7 +98,8 @@ xh_init_(xhash *x, size_t kwidth, size_t vwidth, xh_hashf hashf, xh_equalf equal
x->vwidth = vwidth; x->vwidth = vwidth;
x->hashf = hashf; x->hashf = hashf;
x->equalf = equalf; x->equalf = equalf;
x->chain = NULL; x->head = NULL;
x->tail = NULL;
x->data = data; x->data = data;
xh_bucket_realloc(x, XHASH_INIT_SIZE); xh_bucket_realloc(x, XHASH_INIT_SIZE);
@ -138,7 +139,8 @@ xh_resize_(xhash *x, size_t newsize)
y.count++; y.count++;
} }
y.chain = x->chain; y.head = x->head;
y.tail = x->tail;
free(x->buckets); free(x->buckets);
@ -171,14 +173,14 @@ xh_put_(xhash *x, const void *key, void *val)
memcpy((void *)e->key, key, x->kwidth); memcpy((void *)e->key, key, x->kwidth);
memcpy(e->val, val, x->vwidth); memcpy(e->val, val, x->vwidth);
if (x->chain == NULL) { if (x->head == NULL) {
x->chain = e; x->head = x->tail = e;
e->fw = e->bw = NULL; e->fw = e->bw = NULL;
} else { } else {
x->chain->fw = e; x->tail->bw = e;
e->bw = x->chain; e->fw = x->tail;
e->fw = NULL; e->bw = NULL;
x->chain = e; x->tail = e;
} }
x->count++; x->count++;
@ -197,15 +199,16 @@ xh_del_(xhash *x, const void *key)
idx = ((unsigned)hash) % x->size; idx = ((unsigned)hash) % x->size;
if (x->buckets[idx]->hash == hash && x->equalf(key, x->buckets[idx]->key, x->data)) { if (x->buckets[idx]->hash == hash && x->equalf(key, x->buckets[idx]->key, x->data)) {
q = x->buckets[idx]; q = x->buckets[idx];
if (q->fw) { if (q->fw == NULL) {
x->head = q->bw;
} else {
q->fw->bw = q->bw; q->fw->bw = q->bw;
} }
if (q->bw) { if (q->bw == NULL) {
x->tail = q->fw;
} else {
q->bw->fw = q->fw; q->bw->fw = q->fw;
} }
if (x->chain == q) {
x->chain = q->bw;
}
r = q->next; r = q->next;
free(q); free(q);
x->buckets[idx] = r; x->buckets[idx] = r;
@ -216,15 +219,16 @@ xh_del_(xhash *x, const void *key)
break; break;
} }
q = p->next; q = p->next;
if (q->fw) { if (q->fw == NULL) {
x->head = q->bw;
} else {
q->fw->bw = q->bw; q->fw->bw = q->bw;
} }
if (q->bw) { if (q->bw == NULL) {
x->tail = q->fw;
} else {
q->bw->fw = q->fw; q->bw->fw = q->fw;
} }
if (x->chain == q) {
x->chain = q->bw;
}
r = q->next; r = q->next;
free(q); free(q);
p->next = r; p->next = r;
@ -255,7 +259,7 @@ xh_clear(xhash *x)
x->buckets[i] = NULL; x->buckets[i] = NULL;
} }
x->chain = NULL; x->head = x->tail = NULL;
x->count = 0; x->count = 0;
} }
@ -403,7 +407,7 @@ xh_del_int(xhash *x, int key)
static inline xh_entry * static inline xh_entry *
xh_begin(xhash *x) xh_begin(xhash *x)
{ {
return x->chain; return x->head;
} }
static inline xh_entry * static inline xh_entry *