2
* Copyright (C) 2001 - 2005 Mike Wray <mike.wray@hp.com>
4
* This library is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation; either version 2.1 of the License, or
7
* (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU Lesser General Public License for more details.
14
* You should have received a copy of the GNU Lesser General Public License
15
* along with this library; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
# include <linux/config.h>
21
# include <linux/module.h>
22
# include <linux/kernel.h>
23
# include <linux/errno.h>
30
#include "hash_table.h"
33
* Base support for hashtables.
35
* Hash codes are reduced modulo the number of buckets to index tables,
36
* so there is no need for hash functions to limit the range of hashcodes.
37
* In fact it is assumed that hashcodes do not change when the number of
38
* buckets in the table changes.
41
/*============================================================================*/
43
--------------------------------------------------------------------
44
lookup2.c, by Bob Jenkins, December 1996, Public Domain.
45
You can use this free for any purpose. It has no warranty.
46
--------------------------------------------------------------------
49
#define hashsize(n) ((ub4)1<<(n))
50
#define hashmask(n) (hashsize(n)-1)
53
--------------------------------------------------------------------
54
mix -- mix 3 32-bit values reversibly.
55
For every delta with one or two bit set, and the deltas of all three
56
high bits or all three low bits, whether the original value of a,b,c
57
is almost all zero or is uniformly distributed,
58
* If mix() is run forward or backward, at least 32 bits in a,b,c
59
have at least 1/4 probability of changing.
60
* If mix() is run forward, every bit of c will change between 1/3 and
61
2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.)
62
mix() was built out of 36 single-cycle latency instructions in a
63
structure that could supported 2x parallelism, like so:
71
Unfortunately, superscalar Pentiums and Sparcs can't take advantage
72
of that parallelism. They've also turned some of those single-cycle
73
latency instructions into multi-cycle latency instructions. Still,
74
this is the fastest good hash I could find. There were about 2^^68
75
to choose from. I only looked at a billion or so.
76
--------------------------------------------------------------------
80
a -= b; a -= c; a ^= (c>>13); \
81
b -= c; b -= a; b ^= (a<<8); \
82
c -= a; c -= b; c ^= (b>>13); \
83
a -= b; a -= c; a ^= (c>>12); \
84
b -= c; b -= a; b ^= (a<<16); \
85
c -= a; c -= b; c ^= (b>>5); \
86
a -= b; a -= c; a ^= (c>>3); \
87
b -= c; b -= a; b ^= (a<<10); \
88
c -= a; c -= b; c ^= (b>>15); \
92
--------------------------------------------------------------------
93
hash() -- hash a variable-length key into a 32-bit value
94
k : the key (the unaligned variable-length array of bytes)
95
len : the length of the key, counting by bytes
96
level : can be any 4-byte value
97
Returns a 32-bit value. Every bit of the key affects every bit of
98
the return value. Every 1-bit and 2-bit delta achieves avalanche.
99
About 36+6len instructions.
101
The best hash table sizes are powers of 2. There is no need to do
102
mod a prime (mod is sooo slow!). If you need less than 32 bits,
103
use a bitmask. For example, if you need only 10 bits, do
104
h = (h & hashmask(10));
105
In which case, the hash table should have hashsize(10) elements.
107
If you are hashing n strings (ub1 **)k, do it like this:
108
for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h);
110
By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this
111
code any way you wish, private, educational, or commercial. It's free.
113
See http://burlteburtle.net/bob/hash/evahash.html
114
Use for hash table lookup, or anything where one collision in 2^32 is
115
acceptable. Do NOT use for cryptographic purposes.
116
--------------------------------------------------------------------
119
static inline ub4 _hash(const ub1 *k, ub4 length, ub4 initval)
120
//register ub1 *k; /* the key */
121
//register ub4 length; /* the length of the key */
122
//register ub4 initval; /* the previous hash, or an arbitrary value */
124
/*register*/ ub4 a,b,c,len;
126
/* Set up the internal state */
128
a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
129
c = initval; /* the previous hash value */
131
/*---------------------------------------- handle most of the key */
134
a += (k[0] +((ub4)k[1]<<8) +((ub4)k[2]<<16) +((ub4)k[3]<<24));
135
b += (k[4] +((ub4)k[5]<<8) +((ub4)k[6]<<16) +((ub4)k[7]<<24));
136
c += (k[8] +((ub4)k[9]<<8) +((ub4)k[10]<<16)+((ub4)k[11]<<24));
141
/*------------------------------------- handle the last 11 bytes */
143
switch(len) /* all the case statements fall through */
145
case 11: c+=((ub4)k[10]<<24);
146
case 10: c+=((ub4)k[9]<<16);
147
case 9 : c+=((ub4)k[8]<<8);
148
/* the first byte of c is reserved for the length */
149
case 8 : b+=((ub4)k[7]<<24);
150
case 7 : b+=((ub4)k[6]<<16);
151
case 6 : b+=((ub4)k[5]<<8);
153
case 4 : a+=((ub4)k[3]<<24);
154
case 3 : a+=((ub4)k[2]<<16);
155
case 2 : a+=((ub4)k[1]<<8);
157
/* case 0: nothing left to add */
160
/*-------------------------------------------- report the result */
164
ub4 hash(const ub1 *k, ub4 length, ub4 initval){
165
return _hash(k, length, initval);
168
/*============================================================================*/
170
/** Get the bucket for a hashcode in a hash table.
172
* @param table to get bucket from
173
* @param hashcode to get bucket for
176
inline HTBucket * get_bucket(HashTable *table, Hashcode hashcode){
177
return table->buckets + (hashcode % table->buckets_n);
180
/** Initialize a hash table.
182
* @param table to initialize
184
static void HashTable_init(HashTable *table){
187
for(i = 0; i < table->buckets_n; i++){
188
HTBucket *bucket = get_bucket(table, i);
192
table->entry_count = 0;
195
/** Allocate a new hashtable.
196
* If the number of buckets is not positive the default is used.
198
* @param buckets_n number of buckets
199
* @return new hashtable or null
201
HashTable *HashTable_new(int buckets_n){
202
HashTable *z = ALLOCATE(HashTable);
205
buckets_n = HT_BUCKETS_N;
207
z->buckets = (HTBucket*)allocate(buckets_n * sizeof(HTBucket));
213
z->buckets_n = buckets_n;
219
/** Free a hashtable.
220
* Any entries are removed and freed.
222
* @param h hashtable (ignored if null)
224
void HashTable_free(HashTable *h){
227
deallocate(h->buckets);
232
/** Push an entry on the list in the bucket for a given hashcode.
234
* @param table to add entry to
235
* @param hashcode for the entry
236
* @param entry to add
238
static inline void push_on_bucket(HashTable *table, Hashcode hashcode,
243
bucket = get_bucket(table, hashcode);
244
old_head = bucket->head;
246
bucket->head = entry;
247
entry->next = old_head;
250
/** Change the number of buckets in a hashtable.
251
* No-op if the number of buckets is not positive.
252
* Existing entries are reallocated to buckets based on their hashcodes.
253
* The table is unmodified if the number of buckets cannot be changed.
255
* @param table hashtable
256
* @param buckets_n new number of buckets
257
* @return 0 on success, error code otherwise
259
int HashTable_set_buckets_n(HashTable *table, int buckets_n){
261
HTBucket *old_buckets = table->buckets;
262
int old_buckets_n = table->buckets_n;
269
table->buckets = (HTBucket*)allocate(buckets_n * sizeof(HTBucket));
272
table->buckets = old_buckets;
275
table->buckets_n = buckets_n;
276
for(i=0; i < old_buckets_n; i++){
277
HTBucket *bucket = old_buckets + i;
278
HTEntry *entry, *next;
279
for(entry = bucket->head; entry; entry = next){
281
push_on_bucket(table, entry->hashcode, entry);
284
deallocate(old_buckets);
289
/** Adjust the number of buckets so the table is neither too full nor too empty.
290
* The table is unmodified if adjusting fails.
292
* @param table hash table
293
* @param buckets_min minimum number of buckets (use default if 0 or negative)
294
* @return 0 on success, error code otherwise
296
int HashTable_adjust(HashTable *table, int buckets_min){
299
if(buckets_min <= 0) buckets_min = HT_BUCKETS_N;
300
if(table->entry_count >= table->buckets_n){
301
// The table is dense - expand it.
302
buckets_n = 2 * table->buckets_n;
303
} else if((table->buckets_n > buckets_min) &&
304
(4 * table->entry_count < table->buckets_n)){
305
// The table is more than minimum size and sparse - shrink it.
306
buckets_n = 2 * table->entry_count;
307
if(buckets_n < buckets_min) buckets_n = buckets_min;
310
err = HashTable_set_buckets_n(table, buckets_n);
315
/** Allocate a new entry for a given value.
317
* @param value to put in the entry
318
* @return entry, or 0 on failure
320
HTEntry * HTEntry_new(Hashcode hashcode, void *key, void *value){
321
HTEntry *z = ALLOCATE(HTEntry);
323
z->hashcode = hashcode;
332
* @param z entry to free
334
inline void HTEntry_free(HTEntry *z){
340
/** Free an entry in a hashtable.
341
* The table's entry_free_fn is used is defined, otherwise
342
* the HTEntry itself is freed.
344
* @param table hashtable
345
* @param entry to free
347
inline void HashTable_free_entry(HashTable *table, HTEntry *entry){
349
if(table && table->entry_free_fn){
350
table->entry_free_fn(table, entry);
356
/** Get the first entry satisfying a test from the bucket for the
359
* @param table to look in
360
* @param hashcode indicates the bucket
361
* @param test_fn test to apply to elements
362
* @param arg first argument to calls to test_fn
363
* @return entry found, or 0
365
inline HTEntry * HashTable_find_entry(HashTable *table, Hashcode hashcode,
366
TableTestFn *test_fn, TableArg arg){
368
HTEntry *entry = NULL;
371
bucket = get_bucket(table, hashcode);
372
for(entry = bucket->head; entry; entry = next){
374
if(test_fn(arg, table, entry)){
381
/** Test hashtable keys for equality.
382
* Uses the table's key_equal_fn if defined, otherwise pointer equality.
384
* @param key1 key to compare
385
* @param key2 key to compare
386
* @return 1 if equal, 0 otherwise
388
inline int HashTable_key_equal(HashTable *table, void *key1, void *key2){
390
return memcmp(key1, key2, table->key_size) == 0;
392
return (table->key_equal_fn ? table->key_equal_fn(key1, key2) : key1 == key2);
395
/** Compute the hashcode of a hashtable key.
396
* The table's key_hash_fn is used if defined, otherwise the address of
399
* @param table hashtable
403
inline Hashcode HashTable_key_hash(HashTable *table, void *key){
405
return _hash(key, table->key_size, 0);
407
return (table->key_hash_fn
408
? table->key_hash_fn(key)
409
: hash_hvoid(0, &key, sizeof(key)));
412
/** Test if an entry has a given key.
414
* @param arg containing key to test for
415
* @param table the entry is in
416
* @param entry to test
417
* @return 1 if the entry has the key, 0 otherwise
419
static inline int has_key(TableArg arg, HashTable *table, HTEntry *entry){
420
return HashTable_key_equal(table, arg.ptr, entry->key);
423
/** Get an entry with a given key.
425
* @param table to search
426
* @param key to look for
427
* @return entry if found, null otherwise
429
inline HTEntry * HashTable_get_entry(HashTable *table, void *key){
432
HTEntry *entry = NULL;
435
hashcode = HashTable_key_hash(table, key);
436
bucket = get_bucket(table, hashcode);
437
for(entry = bucket->head; entry; entry = next){
439
if(HashTable_key_equal(table, key, entry->key)){
446
/** Get the value of an entry with a given key.
448
* @param table to search
449
* @param key to look for
450
* @return value if an entry was found, null otherwise
452
inline void * HashTable_get(HashTable *table, void *key){
453
HTEntry *entry = HashTable_get_entry(table, key);
454
return (entry ? entry->value : 0);
457
/** Print the buckets in a table.
459
* @param table to print
461
void show_buckets(HashTable *table, IOStream *io){
463
IOStream_print(io, "entry_count=%d buckets_n=%d\n", table->entry_count, table->buckets_n);
464
for(i=0; i < table->buckets_n; i++){
465
if(0 || table->buckets[i].count>0){
466
IOStream_print(io, "bucket %3d %3d %10p ", i,
467
table->buckets[i].count,
468
table->buckets[i].head);
469
for(j = table->buckets[i].count; j>0; j--){
470
IOStream_print(io, "+");
472
IOStream_print(io, "\n");
475
HashTable_print(table, io);
478
/** Print an entry in a table.
480
* @param entry to print
481
* @param arg a pointer to an IOStream to print to
484
static int print_entry(TableArg arg, HashTable *table, HTEntry *entry){
485
IOStream *io = (IOStream*)arg.ptr;
486
IOStream_print(io, " b=%4lx h=%08lx |-> e=%8p k=%8p v=%8p\n",
487
entry->hashcode % table->buckets_n,
489
entry, entry->key, entry->value);
493
/** Print a hash table.
495
* @param table to print
497
void HashTable_print(HashTable *table, IOStream *io){
498
IOStream_print(io, "{\n");
499
HashTable_map(table, print_entry, (TableArg){ ptr: io });
500
IOStream_print(io, "}\n");
502
/*==========================================================================*/
504
/** Add an entry to the bucket for the
507
* @param table to insert in
508
* @param hashcode indicates the bucket
509
* @param key to add an entry for
510
* @param value to add an entry for
511
* @return entry on success, 0 on failure
513
inline HTEntry * HashTable_add_entry(HashTable *table, Hashcode hashcode, void *key, void *value){
514
HTEntry *entry = HTEntry_new(hashcode, key, value);
516
push_on_bucket(table, hashcode, entry);
517
table->entry_count++;
522
/** Move the front entry for a bucket to the correct point in the bucket order as
523
* defined by the order function. If this is called every time a new entry is added
524
* the bucket will be maintained in sorted order.
526
* @param table to modify
527
* @param hashcode indicates the bucket
528
* @param order entry comparison function
529
* @return 0 if an entry was moved, 1 if not
531
int HashTable_order_bucket(HashTable *table, Hashcode hashcode, TableOrderFn *order){
532
HTEntry *new_entry = NULL, *prev = NULL, *entry = NULL;
536
bucket = get_bucket(table, hashcode);
537
new_entry = bucket->head;
538
if(!new_entry || !new_entry->next) goto exit;
539
for(entry = new_entry->next; entry; prev = entry, entry = entry->next){
540
if(order(new_entry, entry) <= 0) break;
544
bucket->head = new_entry->next;
545
new_entry->next = entry;
546
prev->next = new_entry;
552
/** Add an entry to a hashtable.
553
* The entry is added to the bucket for its key's hashcode.
555
* @param table to insert in
556
* @param key to add an entry for
557
* @param value to add an entry for
558
* @return entry on success, 0 on failure
560
inline HTEntry * HashTable_add(HashTable *table, void *key, void *value){
561
return HashTable_add_entry(table, HashTable_key_hash(table, key), key, value);
564
/** Remove entries satisfying a test from the bucket for the
567
* @param table to remove from
568
* @param hashcode indicates the bucket
569
* @param test_fn test to apply to elements
570
* @param arg first argument to calls to test_fn
571
* @return number of entries removed
573
inline int HashTable_remove_entry(HashTable *table, Hashcode hashcode,
574
TableTestFn *test_fn, TableArg arg){
576
HTEntry *entry, *prev = NULL, *next;
577
int removed_count = 0;
579
bucket = get_bucket(table, hashcode);
580
for(entry = bucket->head; entry; entry = next){
582
if(test_fn(arg, table, entry)){
589
table->entry_count--;
591
HashTable_free_entry(table, entry);
596
return removed_count;
599
/** Remove entries with a given key.
601
* @param table to remove from
602
* @param key of entries to remove
603
* @return number of entries removed
605
inline int HashTable_remove(HashTable *table, void *key){
608
HTEntry *entry, *prev = NULL, *next;
609
int removed_count = 0;
611
hashcode = HashTable_key_hash(table, key);
612
bucket = get_bucket(table, hashcode);
613
for(entry = bucket->head; entry; entry = next){
615
if(HashTable_key_equal(table, key, entry->key)){
622
table->entry_count--;
624
HashTable_free_entry(table, entry);
629
return removed_count;
632
/** Remove (and free) all the entries in a bucket.
634
* @param bucket to clear
636
static inline void bucket_clear(HashTable *table, HTBucket *bucket){
637
HTEntry *entry, *next;
639
for(entry = bucket->head; entry; entry = next){
641
HashTable_free_entry(table, entry);
644
table->entry_count -= bucket->count;
648
/** Remove (and free) all the entries in a table.
650
* @param table to clear
652
void HashTable_clear(HashTable *table){
653
int i, n = table->buckets_n;
655
for(i = 0; i < n; i++){
656
bucket_clear(table, table->buckets + i);