return numBits;
}
-struct cso_hash_data {
- struct cso_node *fakeNext;
- struct cso_node **buckets;
- int size;
- int nodeSize;
- short userNumBits;
- short numBits;
- int numBuckets;
-};
-
static void *cso_data_allocate_node(struct cso_hash_data *hash)
{
- return MALLOC(hash->nodeSize);
+ return MALLOC(sizeof(struct cso_node));
}
static void cso_free_node(struct cso_node *node)
return e;
}
-static struct cso_node **cso_hash_find_node(struct cso_hash *hash, unsigned akey)
-{
- struct cso_node **node;
-
- if (hash->data.d->numBuckets) {
- node = (struct cso_node **)(&hash->data.d->buckets[akey % hash->data.d->numBuckets]);
- assert(*node == hash->data.e || (*node)->next);
- while (*node != hash->data.e && (*node)->key != akey)
- node = &(*node)->next;
- } else {
- node = (struct cso_node **)((const struct cso_node * const *)(&hash->data.e));
- }
- return node;
-}
-
struct cso_hash_iter cso_hash_insert(struct cso_hash *hash,
unsigned key, void *data)
{
}
}
-struct cso_hash * cso_hash_create(void)
+bool cso_hash_init(struct cso_hash *hash)
{
- struct cso_hash *hash = MALLOC_STRUCT(cso_hash);
- if (!hash)
- return NULL;
-
hash->data.d = MALLOC_STRUCT(cso_hash_data);
- if (!hash->data.d) {
- FREE(hash);
- return NULL;
- }
+ if (!hash->data.d)
+ return false;
hash->data.d->fakeNext = 0;
hash->data.d->buckets = 0;
hash->data.d->size = 0;
- hash->data.d->nodeSize = sizeof(struct cso_node);
hash->data.d->userNumBits = (short)MinNumBits;
hash->data.d->numBits = 0;
hash->data.d->numBuckets = 0;
-
- return hash;
+ return true;
}
-void cso_hash_delete(struct cso_hash *hash)
+void cso_hash_deinit(struct cso_hash *hash)
{
struct cso_node *e_for_x = (struct cso_node *)(hash->data.d);
struct cso_node **bucket = (struct cso_node **)(hash->data.d->buckets);
}
FREE(hash->data.d->buckets);
FREE(hash->data.d);
- FREE(hash);
-}
-
-struct cso_hash_iter cso_hash_find(struct cso_hash *hash,
- unsigned key)
-{
- struct cso_node **nextNode = cso_hash_find_node(hash, key);
- struct cso_hash_iter iter = {hash, *nextNode};
- return iter;
+ hash->data.d = NULL;
}
unsigned cso_hash_iter_key(struct cso_hash_iter iter)
return iter.node->key;
}
-static struct cso_node *cso_hash_data_next(struct cso_node *node)
+struct cso_node *cso_hash_data_next(struct cso_node *node)
{
union {
struct cso_node *next;
return a.e;
}
-struct cso_hash_iter cso_hash_iter_next(struct cso_hash_iter iter)
-{
- struct cso_hash_iter next = {iter.hash, cso_hash_data_next(iter.node)};
- return next;
-}
-
void * cso_hash_take(struct cso_hash *hash,
unsigned akey)
{