]>
git.ipfire.org Git - thirdparty/squid.git/blob - lib/hash.c
3 * $Id: hash.c,v 1.11 2001/01/12 00:37:12 wessels Exp $
5 * DEBUG: section 0 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.
51 #include <gnumalloc.h>
52 #elif HAVE_MALLOC_H && !defined(_SQUID_FREEBSD_) && !defined(_SQUID_NEXT_)
65 static void hash_next_bucket(hash_table
* hid
);
68 hash_string(const void *data
, unsigned int size
)
76 n
^= 271 * (unsigned) *s
++;
82 /* the following function(s) were adapted from
83 * usr/src/lib/libc/db/hash_func.c, 4.4 BSD lite */
85 /* Hash function from Chris Torek. */
87 hash4(const void *data
, unsigned int size
)
89 const char *key
= data
;
94 #define HASH4a h = (h << 5) - h + *key++;
95 #define HASH4b h = (h << 5) + h + *key++;
101 switch (len
& (8 - 1)) {
139 * hash_create - creates a new hash table, uses the cmp_func
140 * to compare keys. Returns the identification for the hash table;
141 * otherwise returns a negative number on error.
144 hash_create(HASHCMP
* cmp_func
, int hash_sz
, HASHHASH
* hash_func
)
146 hash_table
*hid
= xcalloc(1, sizeof(hash_table
));
148 hid
->size
= (unsigned int) DEFAULT_HASH_SIZE
;
150 hid
->size
= (unsigned int) hash_sz
;
151 /* allocate and null the buckets */
152 hid
->buckets
= xcalloc(hid
->size
, sizeof(hash_link
*));
154 hid
->hash
= hash_func
;
156 hid
->current_slot
= 0;
161 * hash_join - joins a hash_link under its key lnk->key
162 * into the hash table 'hid'.
164 * It does not copy any data into the hash table, only links pointers.
167 hash_join(hash_table
* hid
, hash_link
* lnk
)
170 i
= hid
->hash(lnk
->key
, hid
->size
);
171 lnk
->next
= hid
->buckets
[i
];
172 hid
->buckets
[i
] = lnk
;
177 * hash_lookup - locates the item under the key 'k' in the hash table
178 * 'hid'. Returns a pointer to the hash bucket on success; otherwise
182 hash_lookup(hash_table
* hid
, const void *k
)
187 b
= hid
->hash(k
, hid
->size
);
188 for (walker
= hid
->buckets
[b
]; walker
!= NULL
; walker
= walker
->next
) {
189 if ((hid
->cmp
) (k
, walker
->key
) == 0)
191 assert(walker
!= walker
->next
);
197 hash_next_bucket(hash_table
* hid
)
199 while (hid
->next
== NULL
&& ++hid
->current_slot
< hid
->size
)
200 hid
->next
= hid
->buckets
[hid
->current_slot
];
204 * hash_first - initializes the hash table for the hash_next()
208 hash_first(hash_table
* hid
)
210 assert(NULL
== hid
->next
);
211 hid
->current_slot
= 0;
212 hid
->next
= hid
->buckets
[hid
->current_slot
];
213 if (NULL
== hid
->next
)
214 hash_next_bucket(hid
);
218 * hash_next - returns the next item in the hash table 'hid'.
219 * Otherwise, returns NULL on error or end of list.
221 * MUST call hash_first() before hash_next().
224 hash_next(hash_table
* hid
)
226 hash_link
*this = hid
->next
;
229 hid
->next
= this->next
;
230 if (NULL
== hid
->next
)
231 hash_next_bucket(hid
);
236 * hash_last - resets hash traversal state to NULL
240 hash_last(hash_table
* hid
)
244 hid
->current_slot
= 0;
248 * hash_remove_link - deletes the given hash_link node from the
249 * hash table 'hid'. Does not free the item, only removes it
252 * On success, it returns 0 and deletes the link; otherwise,
253 * returns non-zero on error.
256 hash_remove_link(hash_table
* hid
, hash_link
* hl
)
261 i
= hid
->hash(hl
->key
, hid
->size
);
262 for (P
= &hid
->buckets
[i
]; *P
; P
= &(*P
)->next
) {
266 if (hid
->next
== hl
) {
267 hid
->next
= hl
->next
;
268 if (NULL
== hid
->next
)
269 hash_next_bucket(hid
);
278 * hash_get_bucket - returns the head item of the bucket
279 * in the hash table 'hid'. Otherwise, returns NULL on error.
282 hash_get_bucket(hash_table
* hid
, unsigned int bucket
)
284 if (bucket
>= hid
->size
)
286 return (hid
->buckets
[bucket
]);
290 hashFreeItems(hash_table
* hid
, HASHFREE
* free_func
)
296 list
= xcalloc(hid
->count
, sizeof(hash_link
*));
298 while ((l
= hash_next(hid
)) && i
< hid
->count
) {
302 for (j
= 0; j
< i
; j
++)
303 free_func(*(list
+ j
));
308 hashFreeMemory(hash_table
* hid
)
316 static int hash_primes
[] =
335 int I
= sizeof(hash_primes
) / sizeof(int);
337 int best_prime
= hash_primes
[0];
338 double min
= fabs(log((double) n
) - log((double) hash_primes
[0]));
340 for (i
= 0; i
< I
; i
++) {
341 d
= fabs(log((double) n
) - log((double) hash_primes
[i
]));
345 best_prime
= hash_primes
[i
];
351 * return the key of a hash_link as a const string
354 hashKeyStr(hash_link
* hl
)
356 return (const char *) hl
->key
;
360 #ifdef USE_HASH_DRIVER
362 * hash-driver - Run with a big file as stdin to insert each line into the
363 * hash table, then prints the whole hash table, then deletes a random item,
364 * and prints the table again...
371 LOCAL_ARRAY(char, buf
, BUFSIZ
);
372 LOCAL_ARRAY(char, todelete
, BUFSIZ
);
373 hash_link
*walker
= NULL
;
378 printf("creating hash table\n");
379 if ((hid
= hash_create((HASHCMP
*) strcmp
, 229, hash4
)) < 0) {
380 printf("hash_create error.\n");
383 printf("done creating hash table: %d\n", hid
);
385 while (fgets(buf
, BUFSIZ
, stdin
)) {
386 buf
[strlen(buf
) - 1] = '\0';
387 printf("Inserting '%s' for item %p to hash table: %d\n",
389 hash_insert(hid
, xstrdup(buf
), (void *) 0x12345678);
390 if (random() % 17 == 0)
391 strcpy(todelete
, buf
);
394 printf("walking hash table...\n");
395 for (i
= 0, walker
= hash_first(hid
); walker
; walker
= hash_next(hid
)) {
396 printf("item %5d: key: '%s' item: %p\n", i
++, walker
->key
,
399 printf("done walking hash table...\n");
402 printf("deleting %s from %d\n", todelete
, hid
);
403 if (hash_delete(hid
, todelete
))
404 printf("hash_delete error\n");
406 printf("walking hash table...\n");
407 for (i
= 0, walker
= hash_first(hid
); walker
; walker
= hash_next(hid
)) {
408 printf("item %5d: key: '%s' item: %p\n", i
++, walker
->key
,
411 printf("done walking hash table...\n");
414 printf("driver finished.\n");