8 hash_status_enum hash_insert(hash_type* hash_table, void* key, void* rec) {
9 hash_node_type *p, *p0;
12 /************************************************
13 * allocate node for data and insert in table *
14 ************************************************/
17 /* insert node at beginning of list */
18 bucket = hash_table->hash(key);
19 p = xmalloc(sizeof *p);
20 p0 = hash_table->table[bucket];
21 hash_table->table[bucket] = p;
23 p->key = hash_table->copy_key(key);
25 return HASH_STATUS_OK;
28 hash_status_enum hash_delete(hash_type* hash_table, void* key) {
29 hash_node_type *p0, *p;
32 /********************************************
33 * delete node containing data from table *
34 ********************************************/
38 bucket = hash_table->hash(key);
39 p = hash_table->table[bucket];
40 while (p && !hash_table->compare(p->key, key)) {
44 if (!p) return HASH_STATUS_KEY_NOT_FOUND;
46 /* p designates node to delete, remove it from list */
48 /* not first node, p0 points to previous node */
52 /* first node on chain */
53 hash_table->table[bucket] = p->next;
56 hash_table->delete_key(p->key);
58 return HASH_STATUS_OK;
61 hash_status_enum hash_find(hash_type* hash_table, void* key, void **rec) {
64 /*******************************
65 * find node containing data *
66 *******************************/
67 p = hash_table->table[hash_table->hash(key)];
69 while (p && !hash_table->compare(p->key, key)) {
72 if (!p) return HASH_STATUS_KEY_NOT_FOUND;
74 return HASH_STATUS_OK;
77 hash_status_enum hash_next_item(hash_type* hash_table, hash_node_type** ppnode) {
80 if (hash_table == 0) {
81 return HASH_STATUS_KEY_NOT_FOUND;
85 if((*ppnode)->next != NULL) {
86 *ppnode = (*ppnode)->next;
87 return HASH_STATUS_OK;
89 i = hash_table->hash((*ppnode)->key) + 1;
95 while(i < hash_table->size && hash_table->table[i] == NULL) {
98 if(i == hash_table->size) {
100 return HASH_STATUS_KEY_NOT_FOUND;
102 *ppnode = hash_table->table[i];
103 return HASH_STATUS_OK;
106 void hash_delete_all(hash_type* hash_table) {
108 hash_node_type *n, *nn;
110 if(hash_table == 0) {
114 for(i = 0; i < hash_table->size; i++) {
115 n = hash_table->table[i];
118 hash_table->delete_key(n->key);
122 hash_table->table[i] = NULL;
128 * Allocate and return a hash
130 hash_status_enum hash_initialise(hash_type* hash_table) {
131 hash_table->table = xcalloc(hash_table->size, sizeof *hash_table->table);
132 return HASH_STATUS_OK;
135 hash_status_enum hash_destroy(hash_type* hash_table) {
136 free(hash_table->table);
137 return HASH_STATUS_OK;