]>
git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/hash.c
3 * DEBUG: section 00 Hash Tables
4 * AUTHOR: Harvest Derived
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
51 extern void my_free(char *, int, void *);
53 #define free(a) my_free(__FILE__, __LINE__, a)
55 extern void print_stats();
57 * hash_url() - Returns a well-distributed hash function for URLs.
58 * The best way is to sum up the last half of the string.
59 * Adapted from code written by Mic Bowman. -Darren
60 * Generates a standard deviation = 15.73
63 hash_url(const void *data
, unsigned int size
)
68 for (i
= j
/ 2, n
= 0; i
< j
; i
++)
69 n
^= 271 * (unsigned) s
[i
];
75 hash_string(const void *data
, unsigned int size
)
83 n
^= 271 * (unsigned) *s
++;
89 /* the following 4 functions were adapted from
90 * usr/src/lib/libc/db/hash_func.c, 4.4 BSD lite */
95 * Assume that we've already split the bucket to which this key hashes,
96 * calculate that bucket, and check that in fact we did already split it.
98 * This came from ejb's hsearch.
102 #define PRIME2 1048583
104 /* Hash function from Chris Torek. */
106 hash4(const void *data
, unsigned int size
)
108 const char *key
= data
;
113 #define HASH4a h = (h << 5) - h + *key++;
114 #define HASH4b h = (h << 5) + h + *key++;
119 loop
= (len
+ 8 - 1) >> 3;
120 switch (len
& (8 - 1)) {
151 * hash_create - creates a new hash table, uses the cmp_func
152 * to compare keys. Returns the identification for the hash table;
153 * otherwise returns a negative number on error.
156 hash_create(HASHCMP
* cmp_func
, int hash_sz
, HASHHASH
* hash_func
)
158 hash_table
*hid
= calloc(1, sizeof(hash_table
));
160 hid
->size
= (unsigned int) DEFAULT_HASH_SIZE
;
162 hid
->size
= (unsigned int) hash_sz
;
163 /* allocate and null the buckets */
164 hid
->buckets
= calloc(hid
->size
, sizeof(hash_link
*));
166 hid
->hash
= hash_func
;
167 hid
->current_ptr
= NULL
;
168 hid
->current_slot
= 0;
173 * hash_insert - inserts the given item 'item' under the given key 'k'
174 * into the hash table 'hid'. Returns non-zero on error; otherwise,
175 * returns 0 and inserts the item.
177 * It does not copy any data into the hash table, only pointers.
180 hash_insert(hash_table
* hid
, const char *k
, void *item
)
185 /* Add to the given hash table 'hid' */
186 new = calloc(1, sizeof(hash_link
));
188 fprintf(stderr
, "calloc failed!\n");
193 new->key
= (char *) k
;
194 i
= hid
->hash(k
, hid
->size
);
195 new->next
= hid
->buckets
[i
];
196 hid
->buckets
[i
] = new;
200 * hash_join - joins a hash_link under its key lnk->key
201 * into the hash table 'hid'.
203 * It does not copy any data into the hash table, only links pointers.
206 hash_join(hash_table
* hid
, hash_link
* lnk
)
209 i
= hid
->hash(lnk
->key
, hid
->size
);
210 lnk
->next
= hid
->buckets
[i
];
211 hid
->buckets
[i
] = lnk
;
215 * hash_lookup - locates the item under the key 'k' in the hash table
216 * 'hid'. Returns a pointer to the hash bucket on success; otherwise
220 hash_lookup(hash_table
* hid
, const void *k
)
225 b
= hid
->hash(k
, hid
->size
);
226 for (walker
= hid
->buckets
[b
]; walker
!= NULL
; walker
= walker
->next
) {
227 if ((hid
->cmp
) (k
, walker
->key
) == 0)
229 assert(walker
!= walker
->next
);
235 * hash_first - returns the first item in the hash table 'hid'.
236 * Otherwise, returns NULL on error.
239 hash_first(hash_table
* hid
)
243 for (i
= 0; i
< hid
->size
; i
++) {
244 hid
->current_slot
= i
;
245 if (hid
->buckets
[i
] != NULL
)
246 return (hid
->current_ptr
= hid
->buckets
[i
]);
252 * hash_next - returns the next item in the hash table 'hid'.
253 * Otherwise, returns NULL on error or end of list.
255 * MUST call hash_first() before hash_next().
258 hash_next(hash_table
* hid
)
262 if (hid
->current_ptr
!= NULL
) {
263 hid
->current_ptr
= hid
->current_ptr
->next
;
264 if (hid
->current_ptr
!= NULL
)
265 return (hid
->current_ptr
); /* next item */
267 /* find next bucket */
268 for (i
= hid
->current_slot
+ 1; i
< hid
->size
; i
++) {
269 hid
->current_slot
= i
;
270 if (hid
->buckets
[i
] != NULL
)
271 return (hid
->current_ptr
= hid
->buckets
[i
]);
273 return NULL
; /* end of list */
277 hash_delete(hash_table
* hid
, const char *key
)
279 return hash_delete_link(hid
, hash_lookup(hid
, key
));
283 * hash_delete_link - deletes the given hash_link node from the
284 * hash table 'hid'. If FreeLink then free the given hash_link.
286 * On success, it returns 0 and deletes the link; otherwise,
287 * returns non-zero on error.
290 hash_unlink(hash_table
* hid
, hash_link
* hl
, int FreeLink
)
292 hash_link
*walker
, *prev
;
296 i
= hid
->hash(hl
->key
, hid
->size
);
297 for (prev
= NULL
, walker
= hid
->buckets
[i
];
298 walker
!= NULL
; prev
= walker
, walker
= walker
->next
) {
300 if (prev
== NULL
) { /* it's the head */
301 hid
->buckets
[i
] = walker
->next
;
303 prev
->next
= walker
->next
; /* skip it */
305 /* fix walker state if needed */
306 if (walker
== hid
->current_ptr
)
307 hid
->current_ptr
= walker
->next
;
319 /* take link off and free link node */
321 hash_delete_link(hash_table
* hid
, hash_link
* hl
)
323 return (hash_unlink(hid
, hl
, 1));
326 /* take link off only */
328 hash_remove_link(hash_table
* hid
, hash_link
* hl
)
330 return (hash_unlink(hid
, hl
, 0));
334 * hash_get_bucket - returns the head item of the bucket
335 * in the hash table 'hid'. Otherwise, returns NULL on error.
338 hash_get_bucket(hash_table
* hid
, unsigned int bucket
)
340 if (bucket
>= hid
->size
)
342 return (hid
->buckets
[bucket
]);
346 hashFreeMemory(hash_table
* hid
)
356 * hash-driver - Run with a big file as stdin to insert each line into the
357 * hash table, then prints the whole hash table, then deletes a random item,
358 * and prints the table again...
365 LOCAL_ARRAY(char, buf
, BUFSIZ
);
366 LOCAL_ARRAY(char, todelete
, BUFSIZ
);
367 hash_link
*walker
= NULL
;
372 printf("creating hash table\n");
373 if ((hid
= hash_create(strcmp
, 229, hash_string
)) < 0) {
374 printf("hash_create error.\n");
377 printf("done creating hash table: %d\n", hid
);
379 while (fgets(buf
, BUFSIZ
, stdin
)) {
380 buf
[strlen(buf
) - 1] = '\0';
381 printf("Inserting '%s' for item %p to hash table: %d\n",
383 hash_insert(hid
, xstrdup(buf
), (void *) 0x12345678);
384 if (random() % 17 == 0)
385 strcpy(todelete
, buf
);
388 printf("walking hash table...\n");
389 for (i
= 0, walker
= hash_first(hid
); walker
; walker
= hash_next(hid
)) {
390 printf("item %5d: key: '%s' item: %p\n", i
++, walker
->key
,
393 printf("done walking hash table...\n");
396 printf("deleting %s from %d\n", todelete
, hid
);
397 if (hash_delete(hid
, todelete
))
398 printf("hash_delete error\n");
400 printf("walking hash table...\n");
401 for (i
= 0, walker
= hash_first(hid
); walker
; walker
= hash_next(hid
)) {
402 printf("item %5d: key: '%s' item: %p\n", i
++, walker
->key
,
405 printf("done walking hash table...\n");
407 printf("driver finished.\n");