13 static unsigned int hash_key(
const char *str);
14 static List *get_list(
Bin *bin,
const char *key);
15 static unsigned int hecmw_hashsize_init_index = 0;
39 static const unsigned int hecmw_hashsize_template[] = {
40 1021, 2039, 4093, 8191, 16381, 32749,
41 65521, 131071, 262139, 524287, 1048573, 2097143,
42 4194301, 8388593, 16777213, 33554393, 67108859, 134217689,
43 268435399, 536870909, 1073741789, 2147483647
55 size = hecmw_hashsize_template[hecmw_hashsize_init_index];
57 hash->
bin = (
Bin *)malloc(hash->
n *
sizeof(
Bin));
64 for (i = 0; i < size; i++) {
74 unsigned int i, j, k, l;
77 if (hash ==
NULL)
return;
80 for (i = 0; i < k; i++) {
83 for (j = 0; j < l; j++) {
101 index = hash_key(key) % hash->
n;
102 list = get_list(&(hash->
bin[index]), key);
113 if (hash ==
NULL)
return 0;
114 if (key ==
NULL)
return 0;
116 index = hash_key(key) % hash->
n;
117 list = get_list(&(hash->
bin[index]), key);
119 if (list ==
NULL)
return 0;
124 unsigned int key_len, index, stat;
125 unsigned int hashkey, n;
127 List *tmp_list, *list;
130 if (hash ==
NULL)
return 0;
131 if (key ==
NULL)
return 0;
132 if (value ==
NULL)
return 0;
134 stat = 0.8 * hash->
n;
140 hashkey = hash_key(key);
141 index = hashkey % hash->
n;
142 key_len = strlen(key);
144 bin = &(hash->
bin[index]);
145 list = get_list(bin, key);
151 new_key = (
char *)malloc((key_len + 1) *
sizeof(char));
152 if (new_key ==
NULL)
return 0;
156 tmp_list = (
List *)malloc(
sizeof(
List));
157 if (tmp_list ==
NULL) {
161 bin->
list = tmp_list;
163 tmp_list = (
List *)realloc(bin->
list, (n + 1) *
sizeof(
List));
164 if (tmp_list ==
NULL) {
168 bin->
list = tmp_list;
171 list = &(bin->
list[n]);
173 strcpy(list->
key, key);
183 unsigned int i, j, n;
184 unsigned int newsize, index;
185 Bin *newbin, *bin, *tmp_bin;
186 List *newlist, *list, *tmp_list;
189 for (i = 0; i < 22; i++) {
190 if (hash->
n == hecmw_hashsize_template[i]) {
197 printf(
"ERROR in hash table resize\n");
201 newsize = hecmw_hashsize_template[j];
202 newbin = (
Bin *)malloc(newsize *
sizeof(
Bin));
203 if (newbin ==
NULL)
return 1;
206 for (i = 0; i < newsize; i++) {
213 for (i = 0; i < hash->
n; i++) {
215 for (j = 0; j < bin->
n; j++) {
216 index = list->
hashkey % newsize;
217 tmp_bin = &(newbin[index]);
221 newlist = (
List *)malloc(
sizeof(
List));
222 if (newlist ==
NULL)
return 1;
223 tmp_bin->
list = newlist;
225 newlist = (
List *)realloc(tmp_bin->
list, (n + 1) *
sizeof(
List));
226 if (newlist ==
NULL)
return 1;
227 tmp_bin->
list = newlist;
230 tmp_list = &(tmp_bin->
list[n]);
231 tmp_list->
key = list->
key;
248 static List *get_list(
Bin *bin,
const char *key) {
253 if (n == 0)
return NULL;
256 for (i = 0; i < n; i++) {
258 if (strcmp(list->
key, key) == 0) {
268 static unsigned int hash_key(
const char *c) {
269 unsigned int hash_key = 5381;
274 hash_key = ((hash_key << 5) + hash_key) + i;