Navigation Menu

Skip to content

Commit

Permalink
Adjust hashtable namespace to avoid conflicts with Wisp.
Browse files Browse the repository at this point in the history
  • Loading branch information
skeeto committed Jan 7, 2011
1 parent a1018d3 commit 1c2ee06
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 38 deletions.
6 changes: 3 additions & 3 deletions gc.c
Expand Up @@ -169,11 +169,11 @@ void mark_reachable(object * root) {
mark_reachable(CIENV(root));
break;
case HASH_TABLE:
ht_iter_init(HTAB(root), &htab_iter);
htb_iter_init(HTAB(root), &htab_iter);
while(htab_iter.key != NULL) {
mark_reachable((object *) htab_iter.key);
mark_reachable((object *) htab_iter.value);
ht_iter_inc(&htab_iter);
htb_iter_inc(&htab_iter);
}
default:
break;
Expand All @@ -200,7 +200,7 @@ long sweep_unmarked() {
free(VARRAY(head));
break;
case HASH_TABLE:
ht_destroy(HTAB(head));
htb_destroy(HTAB(head));
default:
break;
}
Expand Down
38 changes: 19 additions & 19 deletions hashtab.c
Expand Up @@ -29,7 +29,7 @@ void xfree(void *p) {
free(p);
}

hashtab_t *ht_init(size_t size, int (*hash_func) (void *, size_t)) {
hashtab_t *htb_init(size_t size, int (*hash_func) (void *, size_t)) {
hashtab_t *new_ht = (hashtab_t *) xmalloc(sizeof(hashtab_t));
new_ht->arr = (hashtab_node_t **) xmalloc(sizeof(hashtab_node_t *) * size);
new_ht->size = size;
Expand All @@ -41,15 +41,15 @@ hashtab_t *ht_init(size_t size, int (*hash_func) (void *, size_t)) {
new_ht->arr[i] = NULL;

if(hash_func == NULL)
new_ht->hash_func = &ht_hash;
new_ht->hash_func = &htb_hash;
else
new_ht->hash_func = hash_func;

return new_ht;
}

void *ht_search(hashtab_t * hashtable, void *key) {
int index = ht_hash(key, hashtable->size);
void *htb_search(hashtab_t * hashtable, void *key) {
int index = htb_hash(key, hashtable->size);
if(hashtable->arr[index] == NULL)
return NULL;

Expand All @@ -62,8 +62,8 @@ void *ht_search(hashtab_t * hashtable, void *key) {
return NULL;
}

void *ht_insert(hashtab_t * hashtable, void *key, void *value) {
int index = ht_hash(key, hashtable->size);
void *htb_insert(hashtab_t * hashtable, void *key, void *value) {
int index = htb_hash(key, hashtable->size);

hashtab_node_t *next_node, *last_node;
next_node = hashtable->arr[index];
Expand Down Expand Up @@ -97,9 +97,9 @@ void *ht_insert(hashtab_t * hashtable, void *key, void *value) {
}

/* delete the given key from the hashtable */
void ht_remove(hashtab_t * hashtable, void *key) {
void htb_remove(hashtab_t * hashtable, void *key) {
hashtab_node_t *last_node, *next_node;
int index = ht_hash(key, hashtable->size);
int index = htb_hash(key, hashtable->size);
next_node = hashtable->arr[index];
last_node = NULL;

Expand All @@ -121,26 +121,26 @@ void ht_remove(hashtab_t * hashtable, void *key) {
}

/* grow the hashtable */
hashtab_t *ht_grow(hashtab_t * old_ht, size_t new_size) {
hashtab_t *htb_grow(hashtab_t * old_ht, size_t new_size) {
/* create new hashtable */
hashtab_t *new_ht = ht_init(new_size, old_ht->hash_func);
hashtab_t *new_ht = htb_init(new_size, old_ht->hash_func);
if(new_ht == NULL)
return NULL;

/* Iterate through the old hashtable. */
hashtab_iter_t ii;
ht_iter_init(old_ht, &ii);
for(; ii.key != NULL; ht_iter_inc(&ii))
ht_insert(new_ht, ii.key, ii.value);
htb_iter_init(old_ht, &ii);
for(; ii.key != NULL; htb_iter_inc(&ii))
htb_insert(new_ht, ii.key, ii.value);

/* Destroy the old hashtable. */
ht_destroy(old_ht);
htb_destroy(old_ht);

return new_ht;
}

/* free all resources used by the hashtable */
void ht_destroy(hashtab_t * hashtable) {
void htb_destroy(hashtab_t * hashtable) {
hashtab_node_t *next_node, *last_node;

/* Free each linked list in hashtable. */
Expand All @@ -160,18 +160,18 @@ void ht_destroy(hashtab_t * hashtable) {
}

/* iterator initilaize */
void ht_iter_init(hashtab_t * hashtable, hashtab_iter_t * ii) {
void htb_iter_init(hashtab_t * hashtable, hashtab_iter_t * ii) {
/* stick in initial bookeeping data */
ii->internal.hashtable = hashtable;
ii->internal.node = NULL;
ii->internal.index = -1;

/* have iterator point to first element */
ht_iter_inc(ii);
htb_iter_inc(ii);
}

/* iterator increment */
void ht_iter_inc(hashtab_iter_t * ii) {
void htb_iter_inc(hashtab_iter_t * ii) {
hashtab_t *hashtable = ii->internal.hashtable;
int index = ii->internal.index;

Expand Down Expand Up @@ -207,7 +207,7 @@ void ht_iter_inc(hashtab_iter_t * ii) {
ii->value = ii->internal.node->value;
}

int ht_hash(void *key, size_t hashtab_size) {
int htb_hash(void *key, size_t hashtab_size) {
/* One-at-a-time hash */
uint32_t hash, i;
for(hash = 0, i = 0; i < sizeof(key); ++i) {
Expand Down
20 changes: 10 additions & 10 deletions hashtab.h
Expand Up @@ -49,44 +49,44 @@ typedef struct hashtab_iter_t
* function pointer is given (a NULL pointer), then the built in hash
* function is used. A NULL pointer returned if the creation of the
* hashtable failed due to a failed malloc(). */
hashtab_t *ht_init (size_t size,
hashtab_t *htb_init (size_t size,
int (*hash_func)
(void *key, size_t ht_size));
(void *key, size_t htb_size));

/* Fetch a value from table matching the key. Returns a pointer to
* the value matching the given key. */
void *ht_search (hashtab_t * hashtable, void *key);
void *htb_search (hashtab_t * hashtable, void *key);

/* Put a value into the table with the given key. Returns NULL if
* malloc() fails to allocate memory for the new node. */
void *ht_insert (hashtab_t * hashtable,
void *htb_insert (hashtab_t * hashtable,
void *key, void *value);

/* Delete the given key and value pair from the hashtable. If the key
* does not exist, no error is given. */
void ht_remove (hashtab_t * hashtable, void *key);
void htb_remove (hashtab_t * hashtable, void *key);

/* Change the size of the hashtable. It will allocate a new hashtable
* and move all keys and values over. The pointer to the new hashtable
* is returned. Will return NULL if the new hashtable fails to be
* allocated. If this happens, the old hashtable will not be altered
* in any way. The old hashtable is destroyed upon a successful
* grow. */
hashtab_t *ht_grow (hashtab_t * hashtable, size_t new_size);
hashtab_t *htb_grow (hashtab_t * hashtable, size_t new_size);

/* Free all resources used by the hashtable. */
void ht_destroy (hashtab_t * hashtable);
void htb_destroy (hashtab_t * hashtable);

/* Initialize the given iterator. It will point to the first element
* in the hashtable. */
void ht_iter_init (hashtab_t * hashtable, hashtab_iter_t * ii);
void htb_iter_init (hashtab_t * hashtable, hashtab_iter_t * ii);

/* Increment the iterator to the next element. The iterator key and
* value will point to NULL values when the iterator has reached the
* end of the hashtable. */
void ht_iter_inc (hashtab_iter_t * ii);
void htb_iter_inc (hashtab_iter_t * ii);

/* Default hashtable hash function. */
int ht_hash (void *key, size_t hashtab_size);
int htb_hash (void *key, size_t hashtab_size);

#endif
12 changes: 6 additions & 6 deletions types.c
Expand Up @@ -182,7 +182,7 @@ object *list_to_vector(object * list) {
object *make_hashtab(long size) {
object *obj = alloc_object();
obj->type = HASH_TABLE;
HTAB(obj) = ht_init(size, NULL);
HTAB(obj) = htb_init(size, NULL);
return obj;
}

Expand All @@ -191,15 +191,15 @@ char is_hashtab(object * obj) {
}

void set_hashtab(object * tab, object * key, object * val) {
ht_insert(HTAB(tab), key, val);
htb_insert(HTAB(tab), key, val);
}

void remkey_hashtab(object * tab, object * key) {
ht_remove(HTAB(tab), key);
htb_remove(HTAB(tab), key);
}

object *get_hashtab(object * tab, object * key, object * fail) {
object *val = ht_search(HTAB(tab), key);
object *val = htb_search(HTAB(tab), key);
if(val == NULL) {
return fail;
}
Expand All @@ -213,10 +213,10 @@ object *get_hashtab_keys(object * table) {
push_root(&result);

hashtab_iter_t iter;
ht_iter_init(HTAB(table), &iter);
htb_iter_init(HTAB(table), &iter);
while(iter.key != NULL) {
result = cons((object *) iter.key, result);
ht_iter_inc(&iter);
htb_iter_inc(&iter);
}
pop_root(&result);
return result;
Expand Down

0 comments on commit 1c2ee06

Please sign in to comment.