]> git.ipfire.org Git - thirdparty/squid.git/blame - lib/hash.cc
Change increment and decrement operators from postfix to prefix form.
[thirdparty/squid.git] / lib / hash.cc
CommitLineData
f52a7d75 1
2/*
262a0e14 3 * $Id$
f52a7d75 4 *
b510f3a1 5 * DEBUG: section 00 Hash Tables
f52a7d75 6 * AUTHOR: Harvest Derived
7 *
2b6662ba 8 * SQUID Web Proxy Cache http://www.squid-cache.org/
f52a7d75 9 * ----------------------------------------------------------
10 *
2b6662ba 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.
f52a7d75 19 *
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.
26ac0430 24 *
f52a7d75 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.
26ac0430 29 *
f52a7d75 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.
33 *
34 */
35
f7f3304a 36#include "squid.h"
25f98340
AJ
37#include "hash.h"
38#include "profiler/Profiler.h"
f52a7d75 39
40#if HAVE_STDIO_H
41#include <stdio.h>
42#endif
43#if HAVE_STDLIB_H
44#include <stdlib.h>
45#endif
46#if HAVE_STRING_H
47#include <string.h>
48#endif
49#if HAVE_UNISTD_H
50#include <unistd.h>
51#endif
52#if HAVE_GNUMALLLOC_H
53#include <gnumalloc.h>
482aa790 54#elif HAVE_MALLOC_H
f52a7d75 55#include <malloc.h>
56#endif
57#if HAVE_ASSERT_H
58#include <assert.h>
59#endif
e880cefa 60#if HAVE_MATH_H
61#include <math.h>
62#endif
f52a7d75 63
f52a7d75 64static void hash_next_bucket(hash_table * hid);
65
66unsigned int
67hash_string(const void *data, unsigned int size)
68{
209663bb 69 const unsigned char *s = static_cast<const unsigned char *>(data);
f52a7d75 70 unsigned int n = 0;
71 unsigned int j = 0;
72 unsigned int i = 0;
73 while (*s) {
26ac0430 74 j++;
209663bb 75 n ^= 271 * (*s++);
f52a7d75 76 }
77 i = n ^ (j * 271);
78 return i % size;
79}
80
81/* the following function(s) were adapted from
82 * usr/src/lib/libc/db/hash_func.c, 4.4 BSD lite */
83
84/* Hash function from Chris Torek. */
85unsigned int
86hash4(const void *data, unsigned int size)
87{
209663bb 88 const char *key = static_cast<const char *>(data);
f52a7d75 89 size_t loop;
90 unsigned int h;
91 size_t len;
92
93#define HASH4a h = (h << 5) - h + *key++;
94#define HASH4b h = (h << 5) + h + *key++;
95#define HASH4 HASH4b
96
97 h = 0;
98 len = strlen(key);
99 loop = len >> 3;
100 switch (len & (8 - 1)) {
101 case 0:
26ac0430 102 break;
f52a7d75 103 case 7:
26ac0430
AJ
104 HASH4;
105 /* FALLTHROUGH */
f52a7d75 106 case 6:
26ac0430
AJ
107 HASH4;
108 /* FALLTHROUGH */
f52a7d75 109 case 5:
26ac0430
AJ
110 HASH4;
111 /* FALLTHROUGH */
f52a7d75 112 case 4:
26ac0430
AJ
113 HASH4;
114 /* FALLTHROUGH */
f52a7d75 115 case 3:
26ac0430
AJ
116 HASH4;
117 /* FALLTHROUGH */
f52a7d75 118 case 2:
26ac0430
AJ
119 HASH4;
120 /* FALLTHROUGH */
f52a7d75 121 case 1:
26ac0430 122 HASH4;
f52a7d75 123 }
124 while (loop--) {
26ac0430
AJ
125 HASH4;
126 HASH4;
127 HASH4;
128 HASH4;
129 HASH4;
130 HASH4;
131 HASH4;
132 HASH4;
f52a7d75 133 }
134 return h % size;
135}
136
209663bb 137/**
f52a7d75 138 * hash_create - creates a new hash table, uses the cmp_func
139 * to compare keys. Returns the identification for the hash table;
140 * otherwise returns a negative number on error.
141 */
142hash_table *
143hash_create(HASHCMP * cmp_func, int hash_sz, HASHHASH * hash_func)
144{
209663bb 145 hash_table *hid = (hash_table *)xcalloc(1, sizeof(hash_table));
f52a7d75 146 if (!hash_sz)
26ac0430 147 hid->size = (unsigned int) DEFAULT_HASH_SIZE;
f52a7d75 148 else
26ac0430 149 hid->size = (unsigned int) hash_sz;
f52a7d75 150 /* allocate and null the buckets */
209663bb 151 hid->buckets = (hash_link **)xcalloc(hid->size, sizeof(hash_link *));
f52a7d75 152 hid->cmp = cmp_func;
153 hid->hash = hash_func;
154 hid->next = NULL;
155 hid->current_slot = 0;
156 return hid;
157}
158
209663bb 159/**
f52a7d75 160 * hash_join - joins a hash_link under its key lnk->key
26ac0430 161 * into the hash table 'hid'.
f52a7d75 162 *
163 * It does not copy any data into the hash table, only links pointers.
164 */
165void
166hash_join(hash_table * hid, hash_link * lnk)
167{
168 int i;
169 i = hid->hash(lnk->key, hid->size);
170 lnk->next = hid->buckets[i];
171 hid->buckets[i] = lnk;
172 hid->count++;
173}
174
209663bb 175/**
f52a7d75 176 * hash_lookup - locates the item under the key 'k' in the hash table
177 * 'hid'. Returns a pointer to the hash bucket on success; otherwise
178 * returns NULL.
179 */
4a8b20e8 180hash_link *
f52a7d75 181hash_lookup(hash_table * hid, const void *k)
182{
f52a7d75 183 int b;
88bfe092 184 PROF_start(hash_lookup);
f52a7d75 185 assert(k != NULL);
186 b = hid->hash(k, hid->size);
209663bb 187 for (hash_link *walker = hid->buckets[b]; walker != NULL; walker = walker->next) {
26ac0430
AJ
188 if ((hid->cmp) (k, walker->key) == 0) {
189 PROF_stop(hash_lookup);
190 return (walker);
191 }
192 assert(walker != walker->next);
f52a7d75 193 }
88bfe092 194 PROF_stop(hash_lookup);
f52a7d75 195 return NULL;
196}
197
198static void
199hash_next_bucket(hash_table * hid)
200{
201 while (hid->next == NULL && ++hid->current_slot < hid->size)
26ac0430 202 hid->next = hid->buckets[hid->current_slot];
f52a7d75 203}
204
209663bb 205/**
f52a7d75 206 * hash_first - initializes the hash table for the hash_next()
207 * function.
208 */
209void
210hash_first(hash_table * hid)
211{
212 assert(NULL == hid->next);
213 hid->current_slot = 0;
214 hid->next = hid->buckets[hid->current_slot];
215 if (NULL == hid->next)
26ac0430 216 hash_next_bucket(hid);
f52a7d75 217}
218
209663bb 219/**
f52a7d75 220 * hash_next - returns the next item in the hash table 'hid'.
26ac0430 221 * Otherwise, returns NULL on error or end of list.
f52a7d75 222 *
223 * MUST call hash_first() before hash_next().
224 */
4a8b20e8 225hash_link *
f52a7d75 226hash_next(hash_table * hid)
227{
209663bb
AJ
228 hash_link *p = hid->next;
229 if (NULL == p)
26ac0430 230 return NULL;
209663bb 231 hid->next = p->next;
f52a7d75 232 if (NULL == hid->next)
26ac0430 233 hash_next_bucket(hid);
209663bb 234 return p;
f52a7d75 235}
236
209663bb 237/**
2c4f7ab2 238 * hash_last - resets hash traversal state to NULL
239 *
240 */
241void
ab96e65c 242hash_last(hash_table * hid)
2c4f7ab2 243{
137a13ea 244 assert(hid != NULL);
2c4f7ab2 245 hid->next = NULL;
246 hid->current_slot = 0;
247}
248
209663bb 249/**
26ac0430 250 * hash_remove_link - deletes the given hash_link node from the
f52a7d75 251 * hash table 'hid'. Does not free the item, only removes it
252 * from the list.
253 *
4fba1a24 254 * An assertion is triggered if the hash_link is not found in the
255 * list.
f52a7d75 256 */
257void
258hash_remove_link(hash_table * hid, hash_link * hl)
259{
f52a7d75 260 assert(hl != NULL);
209663bb
AJ
261 int i = hid->hash(hl->key, hid->size);
262 for (hash_link **P = &hid->buckets[i]; *P; P = &(*P)->next) {
26ac0430
AJ
263 if (*P != hl)
264 continue;
265 *P = hl->next;
266 if (hid->next == hl) {
267 hid->next = hl->next;
268 if (NULL == hid->next)
269 hash_next_bucket(hid);
270 }
271 hid->count--;
272 return;
f52a7d75 273 }
e530de6c 274 assert(0);
f52a7d75 275}
276
209663bb 277/**
26ac0430 278 * hash_get_bucket - returns the head item of the bucket
f52a7d75 279 * in the hash table 'hid'. Otherwise, returns NULL on error.
280 */
281hash_link *
282hash_get_bucket(hash_table * hid, unsigned int bucket)
283{
284 if (bucket >= hid->size)
26ac0430 285 return NULL;
f52a7d75 286 return (hid->buckets[bucket]);
287}
288
289void
290hashFreeItems(hash_table * hid, HASHFREE * free_func)
291{
292 hash_link *l;
f52a7d75 293 int i = 0;
209663bb 294 hash_link **list = (hash_link **)xcalloc(hid->count, sizeof(hash_link *));
f52a7d75 295 hash_first(hid);
296 while ((l = hash_next(hid)) && i < hid->count) {
26ac0430
AJ
297 *(list + i) = l;
298 i++;
f52a7d75 299 }
209663bb 300 for (int j = 0; j < i; j++)
26ac0430 301 free_func(*(list + j));
f52a7d75 302 xfree(list);
303}
304
305void
306hashFreeMemory(hash_table * hid)
307{
04f7fd38 308 if (hid == NULL)
928b6c10 309 return;
efacbef0 310 if (hid->buckets)
26ac0430 311 xfree(hid->buckets);
f52a7d75 312 xfree(hid);
313}
314
26ac0430 315static int hash_primes[] = {
f52a7d75 316 103,
317 229,
318 467,
319 977,
320 1979,
321 4019,
322 6037,
323 7951,
324 12149,
325 16231,
326 33493,
327 65357
328};
329
330int
331hashPrime(int n)
332{
333 int I = sizeof(hash_primes) / sizeof(int);
f52a7d75 334 int best_prime = hash_primes[0];
d87ebd78 335 double min = fabs(log((double) n) - log((double) hash_primes[0]));
f52a7d75 336 double d;
209663bb 337 for (int i = 0; i < I; i++) {
26ac0430
AJ
338 d = fabs(log((double) n) - log((double) hash_primes[i]));
339 if (d > min)
340 continue;
341 min = d;
342 best_prime = hash_primes[i];
f52a7d75 343 }
344 return best_prime;
345}
346
209663bb 347/**
186477c1 348 * return the key of a hash_link as a const string
349 */
350const char *
351hashKeyStr(hash_link * hl)
352{
353 return (const char *) hl->key;
354}
355
f52a7d75 356
32d002cb 357#if USE_HASH_DRIVER
209663bb 358/**
f52a7d75 359 * hash-driver - Run with a big file as stdin to insert each line into the
360 * hash table, then prints the whole hash table, then deletes a random item,
361 * and prints the table again...
362 */
363int
364main(void)
365{
366 hash_table *hid;
f52a7d75 367 LOCAL_ARRAY(char, buf, BUFSIZ);
368 LOCAL_ARRAY(char, todelete, BUFSIZ);
369 hash_link *walker = NULL;
370
371 todelete[0] = '\0';
372 printf("init\n");
373
374 printf("creating hash table\n");
375 if ((hid = hash_create((HASHCMP *) strcmp, 229, hash4)) < 0) {
26ac0430
AJ
376 printf("hash_create error.\n");
377 exit(1);
f52a7d75 378 }
379 printf("done creating hash table: %d\n", hid);
380
381 while (fgets(buf, BUFSIZ, stdin)) {
26ac0430
AJ
382 buf[strlen(buf) - 1] = '\0';
383 printf("Inserting '%s' for item %p to hash table: %d\n",
384 buf, buf, hid);
385 hash_insert(hid, xstrdup(buf), (void *) 0x12345678);
386 if (random() % 17 == 0)
387 strcpy(todelete, buf);
f52a7d75 388 }
389
390 printf("walking hash table...\n");
209663bb 391 for (int i = 0, walker = hash_first(hid); walker; walker = hash_next(hid)) {
26ac0430
AJ
392 printf("item %5d: key: '%s' item: %p\n", i++, walker->key,
393 walker->item);
f52a7d75 394 }
395 printf("done walking hash table...\n");
396
397 if (todelete[0]) {
26ac0430
AJ
398 printf("deleting %s from %d\n", todelete, hid);
399 if (hash_delete(hid, todelete))
400 printf("hash_delete error\n");
f52a7d75 401 }
402 printf("walking hash table...\n");
209663bb 403 for (int i = 0, walker = hash_first(hid); walker; walker = hash_next(hid)) {
26ac0430
AJ
404 printf("item %5d: key: '%s' item: %p\n", i++, walker->key,
405 walker->item);
f52a7d75 406 }
407 printf("done walking hash table...\n");
408
409
410 printf("driver finished.\n");
411 exit(0);
412}
413#endif