]>
git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/hash.c
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 00 Hash Tables */
29 extern void my_free(char *, int, void *);
31 #define free(a) my_free(__FILE__, __LINE__, a)
33 extern void print_stats();
35 * hash_url() - Returns a well-distributed hash function for URLs.
36 * The best way is to sum up the last half of the string.
37 * Adapted from code written by Mic Bowman. -Darren
38 * Generates a standard deviation = 15.73
41 hash_url(const void *data
, unsigned int size
)
46 for (i
= j
/ 2, n
= 0; i
< j
; i
++)
47 n
^= 271 * (unsigned) s
[i
];
53 hash_string(const void *data
, unsigned int size
)
61 n
^= 271 * (unsigned) *s
++;
67 /* the following 4 functions were adapted from
68 * usr/src/lib/libc/db/hash_func.c, 4.4 BSD lite */
73 * Assume that we've already split the bucket to which this key hashes,
74 * calculate that bucket, and check that in fact we did already split it.
76 * This came from ejb's hsearch.
80 #define PRIME2 1048583
82 /* Hash function from Chris Torek. */
84 hash4(const void *data
, unsigned int size
)
86 const char *key
= data
;
91 #define HASH4a h = (h << 5) - h + *key++;
92 #define HASH4b h = (h << 5) + h + *key++;
97 loop
= (len
+ 8 - 1) >> 3;
98 switch (len
& (8 - 1)) {
129 * hash_create - creates a new hash table, uses the cmp_func
130 * to compare keys. Returns the identification for the hash table;
131 * otherwise returns a negative number on error.
134 hash_create(HASHCMP
* cmp_func
, int hash_sz
, HASHHASH
* hash_func
)
136 hash_table
*hid
= calloc(1, sizeof(hash_table
));
138 hid
->size
= (unsigned int) DEFAULT_HASH_SIZE
;
140 hid
->size
= (unsigned int) hash_sz
;
141 /* allocate and null the buckets */
142 hid
->buckets
= calloc(hid
->size
, sizeof(hash_link
*));
144 hid
->hash
= hash_func
;
145 hid
->current_ptr
= NULL
;
146 hid
->current_slot
= 0;
151 * hash_insert - inserts the given item 'item' under the given key 'k'
152 * into the hash table 'hid'. Returns non-zero on error; otherwise,
153 * returns 0 and inserts the item.
155 * It does not copy any data into the hash table, only pointers.
158 hash_insert(hash_table
* hid
, const char *k
, void *item
)
163 /* Add to the given hash table 'hid' */
164 new = calloc(1, sizeof(hash_link
));
166 fprintf(stderr
, "calloc failed!\n");
171 new->key
= (char *) k
;
172 i
= hid
->hash(k
, hid
->size
);
173 new->next
= hid
->buckets
[i
];
174 hid
->buckets
[i
] = new;
178 * hash_join - joins a hash_link under its key lnk->key
179 * into the hash table 'hid'.
181 * It does not copy any data into the hash table, only links pointers.
184 hash_join(hash_table
* hid
, hash_link
* lnk
)
187 i
= hid
->hash(lnk
->key
, hid
->size
);
188 lnk
->next
= hid
->buckets
[i
];
189 hid
->buckets
[i
] = lnk
;
193 * hash_lookup - locates the item under the key 'k' in the hash table
194 * 'hid'. Returns a pointer to the hash bucket on success; otherwise
198 hash_lookup(hash_table
* hid
, const void *k
)
203 b
= hid
->hash(k
, hid
->size
);
204 for (walker
= hid
->buckets
[b
]; walker
!= NULL
; walker
= walker
->next
) {
205 if ((hid
->cmp
) (k
, walker
->key
) == 0)
207 assert(walker
!= walker
->next
);
213 * hash_first - returns the first item in the hash table 'hid'.
214 * Otherwise, returns NULL on error.
217 hash_first(hash_table
* hid
)
221 for (i
= 0; i
< hid
->size
; i
++) {
222 hid
->current_slot
= i
;
223 if (hid
->buckets
[i
] != NULL
)
224 return (hid
->current_ptr
= hid
->buckets
[i
]);
230 * hash_next - returns the next item in the hash table 'hid'.
231 * Otherwise, returns NULL on error or end of list.
233 * MUST call hash_first() before hash_next().
236 hash_next(hash_table
* hid
)
240 if (hid
->current_ptr
!= NULL
) {
241 hid
->current_ptr
= hid
->current_ptr
->next
;
242 if (hid
->current_ptr
!= NULL
)
243 return (hid
->current_ptr
); /* next item */
245 /* find next bucket */
246 for (i
= hid
->current_slot
+ 1; i
< hid
->size
; i
++) {
247 hid
->current_slot
= i
;
248 if (hid
->buckets
[i
] != NULL
)
249 return (hid
->current_ptr
= hid
->buckets
[i
]);
251 return NULL
; /* end of list */
255 hash_delete(hash_table
* hid
, const char *key
)
257 return hash_delete_link(hid
, hash_lookup(hid
, key
));
261 * hash_delete_link - deletes the given hash_link node from the
262 * hash table 'hid'. If FreeLink then free the given hash_link.
264 * On success, it returns 0 and deletes the link; otherwise,
265 * returns non-zero on error.
268 hash_unlink(hash_table
* hid
, hash_link
* hl
, int FreeLink
)
270 hash_link
*walker
, *prev
;
274 i
= hid
->hash(hl
->key
, hid
->size
);
275 for (prev
= NULL
, walker
= hid
->buckets
[i
];
276 walker
!= NULL
; prev
= walker
, walker
= walker
->next
) {
278 if (prev
== NULL
) { /* it's the head */
279 hid
->buckets
[i
] = walker
->next
;
281 prev
->next
= walker
->next
; /* skip it */
283 /* fix walker state if needed */
284 if (walker
== hid
->current_ptr
)
285 hid
->current_ptr
= walker
->next
;
297 /* take link off and free link node */
299 hash_delete_link(hash_table
* hid
, hash_link
* hl
)
301 return (hash_unlink(hid
, hl
, 1));
304 /* take link off only */
306 hash_remove_link(hash_table
* hid
, hash_link
* hl
)
308 return (hash_unlink(hid
, hl
, 0));
312 * hash_get_bucket - returns the head item of the bucket
313 * in the hash table 'hid'. Otherwise, returns NULL on error.
316 hash_get_bucket(hash_table
* hid
, unsigned int bucket
)
318 if (bucket
>= hid
->size
)
320 return (hid
->buckets
[bucket
]);
324 hashFreeMemory(hash_table
* hid
)
334 * hash-driver - Run with a big file as stdin to insert each line into the
335 * hash table, then prints the whole hash table, then deletes a random item,
336 * and prints the table again...
343 LOCAL_ARRAY(char, buf
, BUFSIZ
);
344 LOCAL_ARRAY(char, todelete
, BUFSIZ
);
345 hash_link
*walker
= NULL
;
350 printf("creating hash table\n");
351 if ((hid
= hash_create(strcmp
, 229, hash_string
)) < 0) {
352 printf("hash_create error.\n");
355 printf("done creating hash table: %d\n", hid
);
357 while (fgets(buf
, BUFSIZ
, stdin
)) {
358 buf
[strlen(buf
) - 1] = '\0';
359 printf("Inserting '%s' for item %p to hash table: %d\n",
361 hash_insert(hid
, xstrdup(buf
), (void *) 0x12345678);
362 if (random() % 17 == 0)
363 strcpy(todelete
, buf
);
366 printf("walking hash table...\n");
367 for (i
= 0, walker
= hash_first(hid
); walker
; walker
= hash_next(hid
)) {
368 printf("item %5d: key: '%s' item: %p\n", i
++, walker
->key
,
371 printf("done walking hash table...\n");
374 printf("deleting %s from %d\n", todelete
, hid
);
375 if (hash_delete(hid
, todelete
))
376 printf("hash_delete error\n");
378 printf("walking hash table...\n");
379 for (i
= 0, walker
= hash_first(hid
); walker
; walker
= hash_next(hid
)) {
380 printf("item %5d: key: '%s' item: %p\n", i
++, walker
->key
,
383 printf("done walking hash table...\n");
385 printf("driver finished.\n");