]>
git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/hash.c
5 * DEBUG: section 00 Hash Tables
6 * AUTHOR: Harvest Derived
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
56 extern void my_free(char *, int, void *);
58 #define free(a) my_free(__FILE__, __LINE__, a)
60 extern void print_stats();
62 * hash_url() - Returns a well-distributed hash function for URLs.
63 * The best way is to sum up the last half of the string.
64 * Adapted from code written by Mic Bowman. -Darren
65 * Generates a standard deviation = 15.73
68 hash_url(const void *data
, unsigned int size
)
73 for (i
= j
/ 2, n
= 0; i
< j
; i
++)
74 n
^= 271 * (unsigned) s
[i
];
80 hash_string(const void *data
, unsigned int size
)
88 n
^= 271 * (unsigned) *s
++;
94 /* the following 4 functions were adapted from
95 * usr/src/lib/libc/db/hash_func.c, 4.4 BSD lite */
100 * Assume that we've already split the bucket to which this key hashes,
101 * calculate that bucket, and check that in fact we did already split it.
103 * This came from ejb's hsearch.
107 #define PRIME2 1048583
109 /* Hash function from Chris Torek. */
111 hash4(const void *data
, unsigned int size
)
113 const char *key
= data
;
118 #define HASH4a h = (h << 5) - h + *key++;
119 #define HASH4b h = (h << 5) + h + *key++;
124 loop
= (len
+ 8 - 1) >> 3;
125 switch (len
& (8 - 1)) {
156 * hash_create - creates a new hash table, uses the cmp_func
157 * to compare keys. Returns the identification for the hash table;
158 * otherwise returns a negative number on error.
161 hash_create(HASHCMP
* cmp_func
, int hash_sz
, HASHHASH
* hash_func
)
163 hash_table
*hid
= calloc(1, sizeof(hash_table
));
165 hid
->size
= (unsigned int) DEFAULT_HASH_SIZE
;
167 hid
->size
= (unsigned int) hash_sz
;
168 /* allocate and null the buckets */
169 hid
->buckets
= calloc(hid
->size
, sizeof(hash_link
*));
171 hid
->hash
= hash_func
;
172 hid
->current_ptr
= NULL
;
173 hid
->current_slot
= 0;
178 * hash_insert - inserts the given item 'item' under the given key 'k'
179 * into the hash table 'hid'. Returns non-zero on error; otherwise,
180 * returns 0 and inserts the item.
182 * It does not copy any data into the hash table, only pointers.
185 hash_insert(hash_table
* hid
, const char *k
, void *item
)
190 /* Add to the given hash table 'hid' */
191 new = calloc(1, sizeof(hash_link
));
193 fprintf(stderr
, "calloc failed!\n");
198 new->key
= (char *) k
;
199 i
= hid
->hash(k
, hid
->size
);
200 new->next
= hid
->buckets
[i
];
201 hid
->buckets
[i
] = new;
205 * hash_join - joins a hash_link under its key lnk->key
206 * into the hash table 'hid'.
208 * It does not copy any data into the hash table, only links pointers.
211 hash_join(hash_table
* hid
, hash_link
* lnk
)
214 i
= hid
->hash(lnk
->key
, hid
->size
);
215 lnk
->next
= hid
->buckets
[i
];
216 hid
->buckets
[i
] = lnk
;
220 * hash_lookup - locates the item under the key 'k' in the hash table
221 * 'hid'. Returns a pointer to the hash bucket on success; otherwise
225 hash_lookup(hash_table
* hid
, const void *k
)
230 b
= hid
->hash(k
, hid
->size
);
231 for (walker
= hid
->buckets
[b
]; walker
!= NULL
; walker
= walker
->next
) {
232 if ((hid
->cmp
) (k
, walker
->key
) == 0)
234 assert(walker
!= walker
->next
);
240 * hash_first - returns the first item in the hash table 'hid'.
241 * Otherwise, returns NULL on error.
244 hash_first(hash_table
* hid
)
248 for (i
= 0; i
< hid
->size
; i
++) {
249 hid
->current_slot
= i
;
250 if (hid
->buckets
[i
] != NULL
)
251 return (hid
->current_ptr
= hid
->buckets
[i
]);
257 * hash_next - returns the next item in the hash table 'hid'.
258 * Otherwise, returns NULL on error or end of list.
260 * MUST call hash_first() before hash_next().
263 hash_next(hash_table
* hid
)
267 if (hid
->current_ptr
!= NULL
) {
268 hid
->current_ptr
= hid
->current_ptr
->next
;
269 if (hid
->current_ptr
!= NULL
)
270 return (hid
->current_ptr
); /* next item */
272 /* find next bucket */
273 for (i
= hid
->current_slot
+ 1; i
< hid
->size
; i
++) {
274 hid
->current_slot
= i
;
275 if (hid
->buckets
[i
] != NULL
)
276 return (hid
->current_ptr
= hid
->buckets
[i
]);
278 return NULL
; /* end of list */
282 hash_delete(hash_table
* hid
, const char *key
)
284 return hash_delete_link(hid
, hash_lookup(hid
, key
));
288 * hash_delete_link - deletes the given hash_link node from the
289 * hash table 'hid'. If FreeLink then free the given hash_link.
291 * On success, it returns 0 and deletes the link; otherwise,
292 * returns non-zero on error.
295 hash_unlink(hash_table
* hid
, hash_link
* hl
, int FreeLink
)
297 hash_link
*walker
, *prev
;
301 i
= hid
->hash(hl
->key
, hid
->size
);
302 for (prev
= NULL
, walker
= hid
->buckets
[i
];
303 walker
!= NULL
; prev
= walker
, walker
= walker
->next
) {
305 if (prev
== NULL
) { /* it's the head */
306 hid
->buckets
[i
] = walker
->next
;
308 prev
->next
= walker
->next
; /* skip it */
310 /* fix walker state if needed */
311 if (walker
== hid
->current_ptr
)
312 hid
->current_ptr
= walker
->next
;
324 /* take link off and free link node */
326 hash_delete_link(hash_table
* hid
, hash_link
* hl
)
328 return (hash_unlink(hid
, hl
, 1));
331 /* take link off only */
333 hash_remove_link(hash_table
* hid
, hash_link
* hl
)
335 return (hash_unlink(hid
, hl
, 0));
339 * hash_get_bucket - returns the head item of the bucket
340 * in the hash table 'hid'. Otherwise, returns NULL on error.
343 hash_get_bucket(hash_table
* hid
, unsigned int bucket
)
345 if (bucket
>= hid
->size
)
347 return (hid
->buckets
[bucket
]);
351 hashFreeMemory(hash_table
* hid
)
361 * hash-driver - Run with a big file as stdin to insert each line into the
362 * hash table, then prints the whole hash table, then deletes a random item,
363 * and prints the table again...
370 LOCAL_ARRAY(char, buf
, BUFSIZ
);
371 LOCAL_ARRAY(char, todelete
, BUFSIZ
);
372 hash_link
*walker
= NULL
;
377 printf("creating hash table\n");
378 if ((hid
= hash_create(strcmp
, 229, hash_string
)) < 0) {
379 printf("hash_create error.\n");
382 printf("done creating hash table: %d\n", hid
);
384 while (fgets(buf
, BUFSIZ
, stdin
)) {
385 buf
[strlen(buf
) - 1] = '\0';
386 printf("Inserting '%s' for item %p to hash table: %d\n",
388 hash_insert(hid
, xstrdup(buf
), (void *) 0x12345678);
389 if (random() % 17 == 0)
390 strcpy(todelete
, buf
);
393 printf("walking hash table...\n");
394 for (i
= 0, walker
= hash_first(hid
); walker
; walker
= hash_next(hid
)) {
395 printf("item %5d: key: '%s' item: %p\n", i
++, walker
->key
,
398 printf("done walking hash table...\n");
401 printf("deleting %s from %d\n", todelete
, hid
);
402 if (hash_delete(hid
, todelete
))
403 printf("hash_delete error\n");
405 printf("walking hash table...\n");
406 for (i
= 0, walker
= hash_first(hid
); walker
; walker
= hash_next(hid
)) {
407 printf("item %5d: key: '%s' item: %p\n", i
++, walker
->key
,
410 printf("done walking hash table...\n");
412 printf("driver finished.\n");