Debian Codegraph @

common/hash.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2006 Christophe Fillot ([email protected])
4 *
5 * Generic Hash Tables.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <assert.h>
15
16 #include "utils.h"
17 #include "hash.h"
18
19 /* Compare two strings */
20 int str_equal(void *s1, void *s2)
21 {
22 return(strcmp((char *)s1, (char *)s2) == 0);
23 }
24
25 /* Hash function for a string */
26 u_int str_hash(void *str)
27 {
28 char *p,*s = (char *)str;
29 u_int h,g;
30
31 for(h=0,p=s;*p!='\0';p+=1)
32 {
33 h = (h << 4) + *p;
34 if ((g = h & 0xf0000000)) {
35 h = h ^ (g >> 24);
36 h = h ^ g;
37 }
38 }
39
40 return(h);
41 }
42
43 /* Compare two integers (yes, it's stupid) */
44 int int_equal(void *i1, void *i2)
45 {
46 return(((int)(long)i1) == (int)(long)i2);
47 }
48
49 /* Hash function for an integer (see above) */
50 u_int int_hash(void *i)
51 {
52 u_int val = (u_int)(long)i;
53 return(val ^ (val >> 16));
54 }
55
56 /* Compare two u64 (yes, it's stupid) */
57 int u64_equal(void *i1, void *i2)
58 {
59 return((*(m_uint64_t *)i1) == (*(m_uint64_t *)i2));
60 }
61
62 /* Hash function for an u64 (see above) */
63 u_int u64_hash(void *i)
64 {
65 m_uint64_t val = *(m_uint64_t *)i;
66 return((u_int)(val ^ (val >> 32)));
67 }
68
69 /* Compare 2 pointers */
70 int ptr_equal(void *i1,void *i2)
71 {
72 return((int)(i1 == i2));
73 }
74
75 /* Hash function for a pointer (see above) */
76 u_int ptr_hash(void *i)
77 {
78 m_uint64_t val = (m_uint64_t)(m_iptr_t)i;
79 return((u_int)((val & 0xFFFF) ^ ((val >> 24) & 0xFFFF) ^
80 ((val >> 48) & 0xFFFF)));
81 }
82
83 /* Free memory used by a node */
84 static inline void hash_node_free(hash_node_t *node)
85 {
86 free(node);
87 }
88
89 /* Allocate memory for a new node */
90 static hash_node_t *hash_node_alloc(hash_table_t *ht,void *key,void *value)
91 {
92 hash_node_t *node;
93
94 node = malloc(sizeof(*node));
95 assert(node!=NULL);
96 node->key = key;
97 node->value = value;
98 node->next = NULL;
99 return node;
100 }
101
102 /* Create a new hash table */
103 hash_table_t *hash_table_create(hash_fcompute hash_func,hash_fcompare key_cmp,
104 int hash_size)
105 {
106 hash_table_t *ht;
107
108 if (!hash_func || (hash_size <= 0))
109 return NULL;
110
111 ht = malloc(sizeof(*ht));
112 assert(ht!=NULL);
113
114 memset(ht,0,sizeof(*ht));
115 ht->hash_func = hash_func;
116 ht->key_cmp = key_cmp;
117 ht->size = hash_size;
118 ht->nodes = calloc(ht->size,sizeof(hash_node_t *));
119 assert(ht->nodes!=NULL);
120 return ht;
121 }
122
123 /* Delete an existing Hash Table */
124 void hash_table_delete(hash_table_t *ht)
125 {
126 hash_node_t *node, *node_next;
127 u_int hash_val;
128
129 if (!ht)
130 return;
131
132 for (hash_val = 0; hash_val < ht->size; hash_val++) {
133 for (node = ht->nodes[hash_val]; node; node = node_next) {
134 node_next = node->next;
135 hash_node_free(node);
136 }
137 ht->nodes[hash_val] = NULL;
138 }
139 free(ht->nodes);
140 free(ht);
141 }
142
143 /* Insert a new (key,value). If key already exists in table, replace value */
144 int hash_table_insert(hash_table_t *ht,void *key,void *value)
145 {
146 hash_node_t *node;
147 u_int hash_val;
148
149 assert(ht!=NULL);
150
151 hash_val = ht->hash_func(key) % ht->size;
152
153 for(node=ht->nodes[hash_val];node;node=node->next)
154 if (ht->key_cmp(node->key,key)) {
155 node->value = value;
156 return(0);
157 }
158
159 node = hash_node_alloc(ht,key,value);
160 node->next = ht->nodes[hash_val];
161 ht->nodes[hash_val] = node;
162 ht->nnodes++;
163 return(0);
164 }
165
166 /* Remove a pair (key,value) from an hash table */
167 void *hash_table_remove(hash_table_t *ht,void *key)
168 {
169 hash_node_t **node,*tmp;
170 u_int hash_val;
171 void *value;
172
173 assert(ht!=NULL);
174
175 hash_val = ht->hash_func(key) % ht->size;
176
177 for(node=&ht->nodes[hash_val];*node;node=&(*node)->next)
178 if (ht->key_cmp((*node)->key,key)) {
179 tmp = *node;
180 value = tmp->value;
181 *node = tmp->next;
182
183 hash_node_free(tmp);
184 return(value);
185 }
186
187 return NULL;
188 }
189
190 /* Hash Table Lookup */
191 void *hash_table_lookup(hash_table_t *ht,void *key)
192 {
193 hash_node_t *node;
194 u_int hash_val;
195
196 assert(ht!=NULL);
197
198 hash_val = ht->hash_func(key) % ht->size;
199
200 for(node=ht->nodes[hash_val];node;node=node->next)
201 if (ht->key_cmp(node->key,key))
202 return node->value;
203
204 return NULL;
205 }
206
207 /* Hash Table Lookup - key direct comparison */
208 void *hash_table_lookup_dcmp(hash_table_t *ht,void *key)
209 {
210 hash_node_t *node;
211 u_int hash_val;
212
213 assert(ht!=NULL);
214
215 hash_val = ht->hash_func(key) % ht->size;
216
217 for(node=ht->nodes[hash_val];node;node=node->next)
218 if (node->key == key)
219 return node->value;
220
221 return NULL;
222 }
223
224 /* Call the specified function for each node found in hash table */
225 int hash_table_foreach(hash_table_t *ht,hash_fforeach user_fn,void *opt_arg)
226 {
227 hash_node_t *node;
228 int i;
229
230 assert(ht!=NULL);
231
232 for(i=0;i<ht->size;i++)
233 for(node=ht->nodes[i];node;node=node->next)
234 user_fn(node->key,node->value,opt_arg);
235
236 return(0);
237 }