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 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
|
/*
* Copyright (c) 2012 Tim Ruehsen
* Copyright (c) 2015-2024 Free Software Foundation, Inc.
*
* This file is part of libwget.
*
* Libwget is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Libwget 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libwget. If not, see <https://www.gnu.org/licenses/>.
*
*
* hashmap routines
*
* Changelog
* 06.11.2012 Tim Ruehsen created
*
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <wget.h>
#include "private.h"
typedef struct entry_st entry_t;
struct entry_st {
void
*key,
*value;
entry_t
*next;
unsigned int
hash;
};
struct wget_hashmap_st {
wget_hashmap_hash_fn
*hash; // hash function
wget_hashmap_compare_fn
*cmp; // compare function
wget_hashmap_key_destructor
*key_destructor; // key destructor function
wget_hashmap_value_destructor
*value_destructor; // value destructor function
entry_t
**entry; // pointer to array of pointers to entries
int
max, // allocated entries
cur, // number of entries in use
threshold; // resize when max reaches threshold
float
resize_factor, // resize strategy: >0: resize = off * max, <0: resize = max + (-off)
load_factor;
};
struct wget_hashmap_iterator_st {
struct wget_hashmap_st
*h;
entry_t
*entry;
int
pos;
};
/**
* \file
* \brief Hashmap functions
* \defgroup libwget-hashmap Hashmap functions
* @{
*
* Hashmaps are key/value stores that perform at O(1) for insertion, searching and removing.
*/
/**
* \param[in] h Hashmap
* \return New iterator instance for \p h
*
* Creates a hashmap iterator for \p h.
*/
wget_hashmap_iterator *wget_hashmap_iterator_alloc(wget_hashmap *h)
{
struct wget_hashmap_iterator_st *iter = wget_calloc(1, sizeof(struct wget_hashmap_iterator_st));
if (iter)
iter->h = h;
return (wget_hashmap_iterator *) iter;
}
/**
* \param[in] iter Hashmap iterator
*
* Free the given iterator \p iter.
*/
void wget_hashmap_iterator_free(wget_hashmap_iterator **iter)
{
if (iter)
xfree(*iter);
}
/**
* \param[in] iter Hashmap iterator
* \param[out] value Pointer to the value belonging to the returned key
* \return Pointer to the key or NULL if no more elements left
*
* Returns the next key / value in the hashmap. If all key/value pairs have been
* iterated over the function returns NULL and \p value is untouched.
*
* When iterating over a hashmap, the order of returned key/value pairs is not defined.
*/
void *wget_hashmap_iterator_next(wget_hashmap_iterator *iter, void **value)
{
struct wget_hashmap_iterator_st *_iter = (struct wget_hashmap_iterator_st *) iter;
struct wget_hashmap_st *h = _iter->h;
if (_iter->entry) {
if ((_iter->entry = _iter->entry->next)) {
found:
if (value)
*value = _iter->entry->value;
return _iter->entry->key;
}
_iter->pos++;
}
if (!_iter->entry && h) {
for (; _iter->pos < h->max; _iter->pos++) {
if (h->entry[_iter->pos]) {
_iter->entry = h->entry[_iter->pos];
goto found;
}
}
}
return NULL;
}
/**
* \param[in] max Initial number of pre-allocated entries
* \param[in] hash Hash function to build hashes from elements
* \param[in] cmp Comparison function used to find elements
* \return New hashmap instance
*
* Create a new hashmap instance with initial size \p max.
* It should be free'd after use with wget_hashmap_free().
*
* Before the first insertion of an element, \p hash and \p cmp must be set.
* So if you use %NULL values here, you have to call wget_hashmap_setcmpfunc() and/or
* wget_hashmap_hashcmpfunc() with appropriate function pointers. No doing so will result
* in undefined behavior (likely you'll see a segmentation fault).
*/
wget_hashmap *wget_hashmap_create(int max, wget_hashmap_hash_fn *hash, wget_hashmap_compare_fn *cmp)
{
wget_hashmap *h = wget_malloc(sizeof(wget_hashmap));
if (!h)
return NULL;
h->entry = wget_calloc(max, sizeof(entry_t *));
if (!h->entry) {
xfree(h);
return NULL;
}
h->max = max;
h->cur = 0;
h->resize_factor = 2;
h->hash = hash;
h->cmp = cmp;
h->key_destructor = free;
h->value_destructor = free;
h->load_factor = 0.75;
h->threshold = (int)(max * h->load_factor);
return h;
}
WGET_GCC_NONNULL_ALL
static entry_t * hashmap_find_entry(const wget_hashmap *h, const char *key, unsigned int hash)
{
for (entry_t * e = h->entry[hash % h->max]; e; e = e->next) {
if (hash == e->hash && (key == e->key || !h->cmp(key, e->key))) {
return e;
}
}
return NULL;
}
WGET_GCC_NONNULL_ALL
static void hashmap_rehash(wget_hashmap *h, entry_t **new_entry, int newmax, int recalc_hash)
{
entry_t *entry, *next;
int cur = h->cur;
for (int it = 0; it < h->max && cur; it++) {
for (entry = h->entry[it]; entry; entry = next) {
next = entry->next;
// now move entry from 'h' to 'new_hashmap'
if (recalc_hash)
entry->hash = h->hash(entry->key);
int pos = entry->hash % newmax;
entry->next = new_entry[pos];
new_entry[pos] = entry;
cur--;
}
}
xfree(h->entry);
h->entry = new_entry;
h->max = newmax;
h->threshold = (int)(newmax * h->load_factor);
}
WGET_GCC_NONNULL((1,3))
static int hashmap_new_entry(wget_hashmap *h, unsigned int hash, const char *key, const char *value)
{
entry_t *entry;
if (!(entry = wget_malloc(sizeof(entry_t))))
return WGET_E_MEMORY;
int pos = hash % h->max;
entry->key = (void *)key;
entry->value = (void *)value;
entry->hash = hash;
entry->next = h->entry[pos];
h->entry[pos] = entry;
if (++h->cur >= h->threshold) {
int newsize = (int) (h->max * h->resize_factor);
if (newsize > 0) {
entry_t **new_entry;
if (!(new_entry = wget_calloc(newsize, sizeof(entry_t *)))) {
h->cur--;
xfree(h->entry[pos]);
return WGET_E_MEMORY;
}
// h->cur is always > 0 here, so we don't need a check
hashmap_rehash(h, new_entry, newsize, 0);
}
}
return WGET_E_SUCCESS;
}
/**
* \param[in] h Hashmap to put data into
* \param[in] key Key to insert into \p h
* \param[in] value Value to insert into \p h
* \return 0 if inserted a new entry, 1 if entry existed, WGET_E_MEMORY if internal allocation failed
*
* Insert a key/value pair into hashmap \p h.
*
* \p key and \p value are *not* cloned, the hashmap takes 'ownership' of both.
*
* If \p key already exists and the pointer values the old and the new key differ,
* the old key will be destroyed by calling the key destructor function (default is free()).
*
* To realize a hashset (just keys without values), \p value may be %NULL.
*
* Neither \p h nor \p key must be %NULL, else the return value will always be 0.
*/
int wget_hashmap_put(wget_hashmap *h, const void *key, const void *value)
{
if (h && key) {
entry_t *entry;
unsigned int hash = h->hash(key);
int rc;
if ((entry = hashmap_find_entry(h, key, hash))) {
if (entry->key != key && entry->key != value) {
if (h->key_destructor)
h->key_destructor(entry->key);
if (entry->key == entry->value)
entry->value = NULL;
}
if (entry->value != value && entry->value != key) {
if (h->value_destructor)
h->value_destructor(entry->value);
}
entry->key = (void *) key;
entry->value = (void *) value;
return 1;
}
// a new entry
if ((rc = hashmap_new_entry(h, hash, key, value)) < 0)
return rc;
}
return 0;
}
/**
* \param[in] h Hashmap
* \param[in] key Key to search for
* \return 1 if \p key has been found, 0 if not found
*
* Check if \p key exists in \p h.
*/
int wget_hashmap_contains(const wget_hashmap *h, const void *key)
{
return wget_hashmap_get(h, key, NULL);
}
/**
* \param[in] h Hashmap
* \param[in] key Key to search for
* \param[out] value Value to be returned
* \return 1 if \p key has been found, 0 if not found
*
* Get the value for a given key.
*
* Neither \p h nor \p key must be %NULL.
*/
#undef wget_hashmap_get
int wget_hashmap_get(const wget_hashmap *h, const void *key, void **value)
{
if (h && key) {
entry_t *entry;
if ((entry = hashmap_find_entry(h, key, h->hash(key)))) {
if (value)
*value = entry->value;
return 1;
}
}
return 0;
}
WGET_GCC_NONNULL_ALL
static int hashmap_remove_entry(wget_hashmap *h, const char *key, int free_kv)
{
entry_t *entry, *next, *prev = NULL;
unsigned int hash = h->hash(key);
int pos = hash % h->max;
for (entry = h->entry[pos]; entry; prev = entry, entry = next) {
next = entry->next;
if (hash == entry->hash && (key == entry->key || !h->cmp(key, entry->key))) {
if (prev)
prev->next = next;
else
h->entry[pos] = next;
if (free_kv) {
if (h->key_destructor)
h->key_destructor(entry->key);
if (entry->value != entry->key) {
if (h->value_destructor)
h->value_destructor(entry->value);
}
entry->key = NULL;
entry->value = NULL;
}
xfree(entry);
h->cur--;
return 1;
}
}
return 0;
}
/**
* \param[in] h Hashmap
* \param[in] key Key to be removed
* \return 1 if \p key has been removed, 0 if not found
*
* Remove \p key from hashmap \p h.
*
* If \p key is found, the key and value destructor functions are called
* when removing the entry from the hashmap.
*/
int wget_hashmap_remove(wget_hashmap *h, const void *key)
{
if (h && key)
return hashmap_remove_entry(h, key, 1);
else
return 0;
}
/**
* \param[in] h Hashmap
* \param[in] key Key to be removed
* \return 1 if \p key has been removed, 0 if not found
*
* Remove \p key from hashmap \p h.
*
* Key and value destructor functions are *not* called when removing the entry from the hashmap.
*/
int wget_hashmap_remove_nofree(wget_hashmap *h, const void *key)
{
if (h && key)
return hashmap_remove_entry(h, key, 0);
else
return 0;
}
/**
* \param[in] h Hashmap to be free'd
*
* Remove all entries from hashmap \p h and free the hashmap instance.
*
* Key and value destructor functions are called for each entry in the hashmap.
*/
void wget_hashmap_free(wget_hashmap **h)
{
if (h && *h) {
wget_hashmap_clear(*h);
xfree((*h)->entry);
xfree(*h);
}
}
/**
* \param[in] h Hashmap to be cleared
*
* Remove all entries from hashmap \p h.
*
* Key and value destructor functions are called for each entry in the hashmap.
*/
void wget_hashmap_clear(wget_hashmap *h)
{
if (h) {
entry_t *entry, *next;
int it, cur = h->cur;
for (it = 0; it < h->max && cur; it++) {
for (entry = h->entry[it]; entry; entry = next) {
next = entry->next;
if (h->key_destructor)
h->key_destructor(entry->key);
// free value if different from key
if (h->value_destructor) {
if (entry->value != entry->key || (entry->value == entry->key && !h->key_destructor))
h->value_destructor(entry->value);
}
entry->key = NULL;
entry->value = NULL;
xfree(entry);
cur--;
}
h->entry[it] = NULL;
}
h->cur = 0;
}
}
/**
* \param[in] h Hashmap
* \return Number of entries in hashmap \p h
*
* Return the number of entries in the hashmap \p h.
*/
int wget_hashmap_size(const wget_hashmap *h)
{
return h ? h->cur : 0;
}
/**
* \param[in] h Hashmap
* \param[in] browse Function to be called for each element of \p h
* \param[in] ctx Context variable use as param to \p browse
* \return Return value of the last call to \p browse
*
* Call function \p browse for each element of hashmap \p h or until \p browse
* returns a value not equal to zero.
*
* \p browse is called with \p ctx and the pointer to the current element.
*
* The return value of the last call to \p browse is returned or 0 if either \p h or \p browse is %NULL.
*/
int wget_hashmap_browse(const wget_hashmap *h, wget_hashmap_browse_fn *browse, void *ctx)
{
if (h && browse) {
entry_t *entry;
int it, ret, cur = h->cur;
for (it = 0; it < h->max && cur; it++) {
for (entry = h->entry[it]; entry; entry = entry->next) {
if ((ret = browse(ctx, entry->key, entry->value)) != 0)
return ret;
cur--;
}
}
}
return 0;
}
/**
* \param[in] h Hashmap
* \param[in] cmp Comparison function used to find keys
*
* Set the comparison function.
*/
void wget_hashmap_setcmpfunc(wget_hashmap *h, wget_hashmap_compare_fn *cmp)
{
if (h)
h->cmp = cmp;
}
/**
* \param[in] h Hashmap
* \param[in] hash Hash function used to hash keys
* \return WGET_E_SUCCESS if set successfully, else WGET_E_MEMORY or WGET_E_INVALID
*
* Set the key hash function.
*
* The keys of all entries in the hashmap will be hashed again. This includes a memory allocation, so
* there is a possibility of failure.
*/
int wget_hashmap_sethashfunc(wget_hashmap *h, wget_hashmap_hash_fn *hash)
{
if (!h)
return WGET_E_INVALID;
if (!h->cur)
return WGET_E_SUCCESS; // no re-hashing needed
entry_t **new_entry = wget_calloc(h->max, sizeof(entry_t *));
if (!new_entry)
return WGET_E_MEMORY;
h->hash = hash;
hashmap_rehash(h, new_entry, h->max, 1);
return WGET_E_SUCCESS;
}
/**
* \param[in] h Hashmap
* \param[in] destructor Destructor function for keys
*
* Set the key destructor function.
*
* Default is free().
*/
void wget_hashmap_set_key_destructor(wget_hashmap *h, wget_hashmap_key_destructor *destructor)
{
if (h)
h->key_destructor = destructor;
}
/**
* \param[in] h Hashmap
* \param[in] destructor Destructor function for values
*
* Set the value destructor function.
*
* Default is free().
*/
void wget_hashmap_set_value_destructor(wget_hashmap *h, wget_hashmap_value_destructor *destructor)
{
if (h)
h->value_destructor = destructor;
}
/**
* \param[in] h Hashmap
* \param[in] factor The load factor
*
* Set the load factor function.
*
* The load factor is determines when to resize the internal memory.
* 0.75 means "resize if 75% or more of all slots are used".
*
* The resize strategy is set by wget_hashmap_set_growth_policy().
*
* The resize (and rehashing) occurs earliest on the next insertion of a new key.
*
* Default is 0.75.
*/
void wget_hashmap_set_load_factor(wget_hashmap *h, float factor)
{
if (h) {
h->load_factor = factor;
h->threshold = (int)(h->max * h->load_factor);
// rehashing occurs earliest on next put()
}
}
/**
* \param[in] h Hashmap
* \param[in] factor Hashmap growth factor
*
* Set the factor for resizing the hashmap when it's load factor is reached.
*
* The new size is 'factor * oldsize'. If the new size is less or equal 0,
* the involved put function will do nothing and the internal state of
* the hashmap will not change.
*
* Default is 2.
*/
void wget_hashmap_set_resize_factor(wget_hashmap *h, float factor)
{
if (h)
h->resize_factor = factor;
}
/**@}*/
|