| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
#ifndef _hash_h_ |
|---|
| 20 |
#define _hash_h_ |
|---|
| 21 |
|
|---|
| 22 |
#include <stdio.h> |
|---|
| 23 |
#include <ctype.h> |
|---|
| 24 |
|
|---|
| 25 |
#if defined __cplusplus || (defined __STDC__ && __STDC__) || defined WINDOWS32 |
|---|
| 26 |
# if !defined __GLIBC__ || !defined __P |
|---|
| 27 |
# undef __P |
|---|
| 28 |
# define __P(protos) protos |
|---|
| 29 |
# endif |
|---|
| 30 |
#else |
|---|
| 31 |
# undef __P |
|---|
| 32 |
# define __P(protos) () |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
#endif |
|---|
| 37 |
|
|---|
| 38 |
typedef unsigned long (*hash_func_t) __P((void const *key)); |
|---|
| 39 |
typedef int (*hash_cmp_func_t) __P((void const *x, void const *y)); |
|---|
| 40 |
typedef void (*hash_map_func_t) __P((void const *item)); |
|---|
| 41 |
typedef void (*hash_map_arg_func_t) __P((void const *item, void *arg)); |
|---|
| 42 |
|
|---|
| 43 |
struct hash_table |
|---|
| 44 |
{ |
|---|
| 45 |
void **ht_vec; |
|---|
| 46 |
unsigned long ht_size; |
|---|
| 47 |
unsigned long ht_capacity; |
|---|
| 48 |
unsigned long ht_fill; |
|---|
| 49 |
unsigned long ht_empty_slots; |
|---|
| 50 |
unsigned long ht_collisions; |
|---|
| 51 |
unsigned long ht_lookups; |
|---|
| 52 |
unsigned int ht_rehashes; |
|---|
| 53 |
hash_func_t ht_hash_1; |
|---|
| 54 |
hash_func_t ht_hash_2; |
|---|
| 55 |
hash_cmp_func_t ht_compare; |
|---|
| 56 |
}; |
|---|
| 57 |
|
|---|
| 58 |
typedef int (*qsort_cmp_t) __P((void const *, void const *)); |
|---|
| 59 |
|
|---|
| 60 |
void hash_init __P((struct hash_table *ht, unsigned long size, |
|---|
| 61 |
hash_func_t hash_1, hash_func_t hash_2, hash_cmp_func_t hash_cmp)); |
|---|
| 62 |
void hash_load __P((struct hash_table *ht, void *item_table, |
|---|
| 63 |
unsigned long cardinality, unsigned long size)); |
|---|
| 64 |
void **hash_find_slot __P((struct hash_table *ht, void const *key)); |
|---|
| 65 |
void *hash_find_item __P((struct hash_table *ht, void const *key)); |
|---|
| 66 |
void *hash_insert __P((struct hash_table *ht, const void *item)); |
|---|
| 67 |
void *hash_insert_at __P((struct hash_table *ht, const void *item, void const *slot)); |
|---|
| 68 |
void *hash_delete __P((struct hash_table *ht, void const *item)); |
|---|
| 69 |
void *hash_delete_at __P((struct hash_table *ht, void const *slot)); |
|---|
| 70 |
void hash_delete_items __P((struct hash_table *ht)); |
|---|
| 71 |
void hash_free_items __P((struct hash_table *ht)); |
|---|
| 72 |
void hash_free __P((struct hash_table *ht, int free_items)); |
|---|
| 73 |
void hash_map __P((struct hash_table *ht, hash_map_func_t map)); |
|---|
| 74 |
void hash_map_arg __P((struct hash_table *ht, hash_map_arg_func_t map, void *arg)); |
|---|
| 75 |
void hash_print_stats __P((struct hash_table *ht, FILE *out_FILE)); |
|---|
| 76 |
void **hash_dump __P((struct hash_table *ht, void **vector_0, qsort_cmp_t compare)); |
|---|
| 77 |
|
|---|
| 78 |
extern void *hash_deleted_item; |
|---|
| 79 |
#define HASH_VACANT(item) ((item) == 0 || (void *) (item) == hash_deleted_item) |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
#define STRING_HASH_1(KEY, RESULT) do { \ |
|---|
| 85 |
unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \ |
|---|
| 86 |
while (*++_key_) \ |
|---|
| 87 |
(RESULT) += (*_key_ << (_key_[1] & 0xf)); \ |
|---|
| 88 |
} while (0) |
|---|
| 89 |
#define return_STRING_HASH_1(KEY) do { \ |
|---|
| 90 |
unsigned long _result_ = 0; \ |
|---|
| 91 |
STRING_HASH_1 ((KEY), _result_); \ |
|---|
| 92 |
return _result_; \ |
|---|
| 93 |
} while (0) |
|---|
| 94 |
|
|---|
| 95 |
#define STRING_HASH_2(KEY, RESULT) do { \ |
|---|
| 96 |
unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \ |
|---|
| 97 |
while (*++_key_) \ |
|---|
| 98 |
(RESULT) += (*_key_ << (_key_[1] & 0x7)); \ |
|---|
| 99 |
} while (0) |
|---|
| 100 |
#define return_STRING_HASH_2(KEY) do { \ |
|---|
| 101 |
unsigned long _result_ = 0; \ |
|---|
| 102 |
STRING_HASH_2 ((KEY), _result_); \ |
|---|
| 103 |
return _result_; \ |
|---|
| 104 |
} while (0) |
|---|
| 105 |
|
|---|
| 106 |
#define STRING_COMPARE(X, Y, RESULT) do { \ |
|---|
| 107 |
RESULT = strcmp ((X), (Y)); \ |
|---|
| 108 |
} while (0) |
|---|
| 109 |
#define return_STRING_COMPARE(X, Y) do { \ |
|---|
| 110 |
return strcmp ((X), (Y)); \ |
|---|
| 111 |
} while (0) |
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
#define STRING_N_HASH_1(KEY, N, RESULT) do { \ |
|---|
| 115 |
unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \ |
|---|
| 116 |
int _n_ = (N); \ |
|---|
| 117 |
if (_n_) \ |
|---|
| 118 |
while (--_n_ && *++_key_) \ |
|---|
| 119 |
(RESULT) += (*_key_ << (_key_[1] & 0xf)); \ |
|---|
| 120 |
(RESULT) += *++_key_; \ |
|---|
| 121 |
} while (0) |
|---|
| 122 |
#define return_STRING_N_HASH_1(KEY, N) do { \ |
|---|
| 123 |
unsigned long _result_ = 0; \ |
|---|
| 124 |
STRING_N_HASH_1 ((KEY), (N), _result_); \ |
|---|
| 125 |
return _result_; \ |
|---|
| 126 |
} while (0) |
|---|
| 127 |
|
|---|
| 128 |
#define STRING_N_HASH_2(KEY, N, RESULT) do { \ |
|---|
| 129 |
unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \ |
|---|
| 130 |
int _n_ = (N); \ |
|---|
| 131 |
if (_n_) \ |
|---|
| 132 |
while (--_n_ && *++_key_) \ |
|---|
| 133 |
(RESULT) += (*_key_ << (_key_[1] & 0x7)); \ |
|---|
| 134 |
(RESULT) += *++_key_; \ |
|---|
| 135 |
} while (0) |
|---|
| 136 |
#define return_STRING_N_HASH_2(KEY, N) do { \ |
|---|
| 137 |
unsigned long _result_ = 0; \ |
|---|
| 138 |
STRING_N_HASH_2 ((KEY), (N), _result_); \ |
|---|
| 139 |
return _result_; \ |
|---|
| 140 |
} while (0) |
|---|
| 141 |
|
|---|
| 142 |
#define STRING_N_COMPARE(X, Y, N, RESULT) do { \ |
|---|
| 143 |
RESULT = strncmp ((X), (Y), (N)); \ |
|---|
| 144 |
} while (0) |
|---|
| 145 |
#define return_STRING_N_COMPARE(X, Y, N) do { \ |
|---|
| 146 |
return strncmp ((X), (Y), (N)); \ |
|---|
| 147 |
} while (0) |
|---|
| 148 |
|
|---|
| 149 |
#ifdef HAVE_CASE_INSENSITIVE_FS |
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
#define ISTRING_HASH_1(KEY, RESULT) do { \ |
|---|
| 154 |
unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \ |
|---|
| 155 |
while (*++_key_) \ |
|---|
| 156 |
(RESULT) += ((isupper (*_key_) ? tolower (*_key_) : *_key_) << (_key_[1] & 0xf)); \ |
|---|
| 157 |
} while (0) |
|---|
| 158 |
#define return_ISTRING_HASH_1(KEY) do { \ |
|---|
| 159 |
unsigned long _result_ = 0; \ |
|---|
| 160 |
ISTRING_HASH_1 ((KEY), _result_); \ |
|---|
| 161 |
return _result_; \ |
|---|
| 162 |
} while (0) |
|---|
| 163 |
|
|---|
| 164 |
#define ISTRING_HASH_2(KEY, RESULT) do { \ |
|---|
| 165 |
unsigned char const *_key_ = (unsigned char const *) (KEY) - 1; \ |
|---|
| 166 |
while (*++_key_) \ |
|---|
| 167 |
(RESULT) += ((isupper (*_key_) ? tolower (*_key_) : *_key_) << (_key_[1] & 0x7)); \ |
|---|
| 168 |
} while (0) |
|---|
| 169 |
#define return_ISTRING_HASH_2(KEY) do { \ |
|---|
| 170 |
unsigned long _result_ = 0; \ |
|---|
| 171 |
ISTRING_HASH_2 ((KEY), _result_); \ |
|---|
| 172 |
return _result_; \ |
|---|
| 173 |
} while (0) |
|---|
| 174 |
|
|---|
| 175 |
#define ISTRING_COMPARE(X, Y, RESULT) do { \ |
|---|
| 176 |
RESULT = strcmpi ((X), (Y)); \ |
|---|
| 177 |
} while (0) |
|---|
| 178 |
#define return_ISTRING_COMPARE(X, Y) do { \ |
|---|
| 179 |
return strcmpi ((X), (Y)); \ |
|---|
| 180 |
} while (0) |
|---|
| 181 |
|
|---|
| 182 |
#else |
|---|
| 183 |
|
|---|
| 184 |
#define ISTRING_HASH_1(KEY, RESULT) STRING_HASH_1 ((KEY), (RESULT)) |
|---|
| 185 |
#define return_ISTRING_HASH_1(KEY) return_STRING_HASH_1 (KEY) |
|---|
| 186 |
|
|---|
| 187 |
#define ISTRING_HASH_2(KEY, RESULT) STRING_HASH_2 ((KEY), (RESULT)) |
|---|
| 188 |
#define return_ISTRING_HASH_2(KEY) return_STRING_HASH_2 (KEY) |
|---|
| 189 |
|
|---|
| 190 |
#define ISTRING_COMPARE(X, Y, RESULT) STRING_COMPARE ((X), (Y), (RESULT)) |
|---|
| 191 |
#define return_ISTRING_COMPARE(X, Y) return_STRING_COMPARE ((X), (Y)) |
|---|
| 192 |
|
|---|
| 193 |
#endif |
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
#define INTEGER_HASH_1(KEY, RESULT) do { \ |
|---|
| 198 |
(RESULT) += ((unsigned long)(KEY)); \ |
|---|
| 199 |
} while (0) |
|---|
| 200 |
#define return_INTEGER_HASH_1(KEY) do { \ |
|---|
| 201 |
unsigned long _result_ = 0; \ |
|---|
| 202 |
INTEGER_HASH_1 ((KEY), _result_); \ |
|---|
| 203 |
return _result_; \ |
|---|
| 204 |
} while (0) |
|---|
| 205 |
|
|---|
| 206 |
#define INTEGER_HASH_2(KEY, RESULT) do { \ |
|---|
| 207 |
(RESULT) += ~((unsigned long)(KEY)); \ |
|---|
| 208 |
} while (0) |
|---|
| 209 |
#define return_INTEGER_HASH_2(KEY) do { \ |
|---|
| 210 |
unsigned long _result_ = 0; \ |
|---|
| 211 |
INTEGER_HASH_2 ((KEY), _result_); \ |
|---|
| 212 |
return _result_; \ |
|---|
| 213 |
} while (0) |
|---|
| 214 |
|
|---|
| 215 |
#define INTEGER_COMPARE(X, Y, RESULT) do { \ |
|---|
| 216 |
(RESULT) = X - Y; \ |
|---|
| 217 |
} while (0) |
|---|
| 218 |
#define return_INTEGER_COMPARE(X, Y) do { \ |
|---|
| 219 |
int _result_; \ |
|---|
| 220 |
INTEGER_COMPARE (X, Y, _result_); \ |
|---|
| 221 |
return _result_; \ |
|---|
| 222 |
} while (0) |
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
#define ADDRESS_HASH_1(KEY, RESULT) INTEGER_HASH_1 (((unsigned long)(KEY)) >> 3, (RESULT)) |
|---|
| 227 |
#define ADDRESS_HASH_2(KEY, RESULT) INTEGER_HASH_2 (((unsigned long)(KEY)) >> 3, (RESULT)) |
|---|
| 228 |
#define ADDRESS_COMPARE(X, Y, RESULT) INTEGER_COMPARE ((X), (Y), (RESULT)) |
|---|
| 229 |
#define return_ADDRESS_HASH_1(KEY) return_INTEGER_HASH_1 (((unsigned long)(KEY)) >> 3) |
|---|
| 230 |
#define return_ADDRESS_HASH_2(KEY) return_INTEGER_HASH_2 (((unsigned long)(KEY)) >> 3) |
|---|
| 231 |
#define return_ADDRESS_COMPARE(X, Y) return_INTEGER_COMPARE ((X), (Y)) |
|---|
| 232 |
|
|---|
| 233 |
#endif |
|---|