1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
|
/* libnih
*
* hash.c - Fuller/Noll/Vo hash table implementation
*
* Copyright © 2009 Scott James Remnant <scott@netsplit.com>.
* Copyright © 2009 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
#include <string.h>
#include <nih/macros.h>
#include <nih/logging.h>
#include <nih/alloc.h>
#include "hash.h"
/**
* FNV_PRIME:
*
* This constant is defined in the FNV description based on the size of
* the hash, in our case 32-bits.
**/
#define FNV_PRIME 16777619UL
/**
* FNV_OFFSET_BASIS:
*
* This constant is also defined in the FNV description and is the result
* of hashing a known string wth the FNV-0 algorithm and the above prime.
**/
#define FNV_OFFSET_BASIS 2166136261UL
/**
* primes:
*
* Prime numbers always give the best hash table sizes, this is a selected
* list of primes giving a reasonable spread. We pick the largest one that
* is smaller than the estimated number of entries for the hash.
**/
static const uint32_t primes[] = {
17, 37, 79, 163, 331, 673, 1259, 2521, 5051, 10103, 20219, 40459,
80929, 160231, 320449, 640973, 1281563, 2566637, 5136083, 10250323
};
/**
* num_primes:
*
* Number of prime numbers defined above.
**/
static const size_t num_primes = sizeof (primes) / sizeof (uint32_t);
/**
* nih_hash_new:
* @parent: parent of new hash,
* @entries: rough number of entries expected,
* @key_function: function used to obtain keys for entries,
* @hash_function: function used to obtain hash for keys,
* @cmp_function: function used to compare keys.
*
* Allocates a new hash table, the number of buckets selected is a prime
* number that is no larger than @entries; this should be set to a rough
* number of expected entries to ensure optimum distribution.
*
* Individual members of the hash table are NihList members, so to
* associate them with a constant key @key_function must be provided, to
* convert that key into a hash @hash_function must be provided and to
* compare keys @cmp_function must be provided. The nih_hash_string_new()
* macro wraps this function for the most common case of a string key as
* the first structure member.
*
* The structure is allocated using nih_alloc() so it can be used as a
* context to other allocations; there is no non-allocated version of this
* function because the hash must be usable as a parent context to its bins.
*
* If @parent is not NULL, it should be a pointer to another object which
* will be used as a parent for the returned hash table. When all parents
* of the returned hash table are freed, the returned hash table will also be
* freed.
*
* Returns: the new hash table or NULL if the allocation failed.
**/
NihHash *
nih_hash_new (const void *parent,
size_t entries,
NihKeyFunction key_function,
NihHashFunction hash_function,
NihCmpFunction cmp_function)
{
NihHash *hash;
size_t i;
nih_assert (key_function != NULL);
nih_assert (hash_function != NULL);
nih_assert (cmp_function != NULL);
hash = nih_new (parent, NihHash);
if (! hash)
return NULL;
/* Pick the largest prime number smaller than the number of entries */
hash->size = primes[0];
for (i = 0; (i < num_primes) && (primes[i] < entries); i++)
hash->size = primes[i];
/* Allocate bins */
hash->bins = nih_alloc (hash, sizeof (NihList) * hash->size);
if (! hash->bins) {
nih_free (hash);
return NULL;
}
/* Initialise bins */
for (i = 0; i < hash->size; i++)
nih_list_init (&hash->bins[i]);
hash->key_function = key_function;
hash->hash_function = hash_function;
hash->cmp_function = cmp_function;
return hash;
}
/**
* nih_hash_add:
* @hash: destination hash table,
* @entry: entry to be added.
*
* Adds @entry to @hash using the value returned by the hash functions
* to indicate which bin the entry should be placed into.
*
* For speed reasons, this function does not check whether an entry already
* exists with the key. If you need that constraint use either
* nih_hash_add_unique() or nih_hash_replace().
*
* If @entry is already in another list it is removed so there is no need
* to call nih_list_remove() before this function.
*
* Returns: @entry which is now a member of one of @hash's bins.
**/
NihList *
nih_hash_add (NihHash *hash,
NihList *entry)
{
const void *key;
uint32_t hashval;
NihList *bin;
nih_assert (hash != NULL);
nih_assert (entry != NULL);
key = hash->key_function (entry);
hashval = hash->hash_function (key) % hash->size;
bin = &hash->bins[hashval];
return nih_list_add (bin, entry);
}
/**
* nih_hash_add_unique:
* @hash: destination hash table,
* @entry: entry to be added.
*
* Adds @entry to @hash using the value returned by the hash functions
* to indicate which bin the entry should be placed into, provided the key
* is unique.
*
* Because the hash table does not store the key of each entry, this requires
* that the key function be called for each entry in the destination bin, so
* should only be used where the uniqueness constraint is required and not
* already enforced by other code.
*
* If @entry is already in another list it is removed so there is no need
* to call nih_list_remove() before this function.
*
* Returns: @entry which is now a member of one of @hash's bins or NULL if
* an entry already existed with the same key.
**/
NihList *
nih_hash_add_unique (NihHash *hash,
NihList *entry)
{
const void *key;
uint32_t hashval;
NihList *bin;
nih_assert (hash != NULL);
nih_assert (entry != NULL);
key = hash->key_function (entry);
hashval = hash->hash_function (key) % hash->size;
bin = &hash->bins[hashval];
NIH_LIST_FOREACH (bin, iter) {
if (! hash->cmp_function (key, hash->key_function (iter)))
return NULL;
}
return nih_list_add (bin, entry);
}
/**
* nih_hash_replace:
* @hash: destination hash table,
* @entry: entry to be added.
*
* Adds @entry to @hash using the value returned by the hash functions
* to indicate which bin the entry should be placed into, replacing any
* existing entry with the same key.
*
* Because the hash table does not store the key of each entry, this requires
* that the key function be called for each entry in the destination bin, so
* should only be used where the uniqueness constraint is required and not
* already enforced by other code.
*
* The replaced entry is returned, it is up to the caller to free it and
* ensure this does not come as a surprise to other code.
*
* If @entry is already in another list it is removed so there is no need
* to call nih_list_remove() before this function.
*
* Returns: existing entry with the same key replaced in the table, or NULL
* if no such entry existed.
**/
NihList *
nih_hash_replace (NihHash *hash,
NihList *entry)
{
const void *key;
uint32_t hashval;
NihList *bin, *ret = NULL;
nih_assert (hash != NULL);
nih_assert (entry != NULL);
key = hash->key_function (entry);
hashval = hash->hash_function (key) % hash->size;
bin = &hash->bins[hashval];
NIH_LIST_FOREACH (bin, iter) {
if (! hash->cmp_function (key, hash->key_function (iter))) {
ret = nih_list_remove (iter);
break;
}
}
nih_list_add (bin, entry);
return ret;
}
/**
* nih_hash_search:
* @hash: hash table to search,
* @key: key to look for,
* @entry: previous entry found.
*
* Finds all entries in @hash with a key of @key by calling the hash's
* key function on each entry in the appropriate bin, starting with @entry,
* until one is found.
*
* The initial @entry can be found by passing NULL or using nih_hash_lookup().
*
* Returns: next entry in the hash or NULL if there are no more entries.
**/
NihList *
nih_hash_search (NihHash *hash,
const void *key,
NihList *entry)
{
uint32_t hashval;
NihList *bin;
nih_assert (hash != NULL);
nih_assert (key != NULL);
hashval = hash->hash_function (key) % hash->size;
bin = &hash->bins[hashval];
NIH_LIST_FOREACH (bin, iter) {
if (iter == entry) {
entry = NULL;
continue;
} else if (entry) {
continue;
} else if (! hash->cmp_function (key, hash->key_function (iter))) {
return iter;
}
}
return NULL;
}
/**
* nih_hash_lookup:
* @hash: hash table to search.
* @key: key to look for.
*
* Finds the first entry in @hash with a key of @key by calling the hash's
* NihKeyFunction on each entry in the appropriate bin until one is found.
*
* If multiple entries are expected, use nih_hash_search() instead.
*
* Returns: entry found or NULL if no entry existed.
**/
NihList *
nih_hash_lookup (NihHash *hash,
const void *key)
{
return nih_hash_search (hash, key, NULL);
}
/**
* nih_hash_string_key:
* @entry: entry to create key for.
*
* Key function that can be used for any list entry where the first member
* immediately after the list header is a pointer to the string containing
* the name.
*
* Returns: pointer to that string.
**/
const char *
nih_hash_string_key (NihList *entry)
{
nih_assert (entry != NULL);
return *((const char **)((char *)entry + sizeof (NihList)));
}
/**
* nih_hash_string_hash:
* @key: string key to hash.
*
* Generates and returns a 32-bit hash for the given string key using the
* FNV-1 algorithm as documented at http://www.isthe.com/chongo/tech/comp/fnv/
*
* The returned key will need to be bounded within the number of bins
* used in the hash table.
*
* Returns: 32-bit hash.
**/
uint32_t
nih_hash_string_hash (const char *key)
{
register uint32_t hash = FNV_OFFSET_BASIS;
nih_assert (key != NULL);
while (*key) {
hash *= FNV_PRIME;
hash ^= *(key++);
}
return hash;
}
/**
* nih_hash_string_cmp:
* @key1: key to compare,
* @key2: key to compare against.
*
* Compares @key1 to @key2 case-sensitively.
*
* Returns: integer less than, equal to or greater than zero if @key1 is
* respectively less then, equal to or greater than @key2.
**/
int
nih_hash_string_cmp (const char *key1,
const char *key2)
{
nih_assert (key1 != NULL);
nih_assert (key2 != NULL);
return strcmp (key1, key2);
}
|