hash.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef HEADER_CURL_HASH_H
  2. #define HEADER_CURL_HASH_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at https://curl.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. * SPDX-License-Identifier: curl
  24. *
  25. ***************************************************************************/
  26. #include "curl_setup.h"
  27. #include <stddef.h>
  28. #include "llist.h"
  29. /* Hash function prototype */
  30. typedef size_t (*hash_function) (void *key,
  31. size_t key_length,
  32. size_t slots_num);
  33. /*
  34. Comparator function prototype. Compares two keys.
  35. */
  36. typedef size_t (*comp_function) (void *key1,
  37. size_t key1_len,
  38. void *key2,
  39. size_t key2_len);
  40. typedef void (*Curl_hash_dtor)(void *);
  41. struct Curl_hash {
  42. struct Curl_llist *table;
  43. /* Hash function to be used for this hash table */
  44. hash_function hash_func;
  45. /* Comparator function to compare keys */
  46. comp_function comp_func;
  47. Curl_hash_dtor dtor;
  48. int slots;
  49. size_t size;
  50. };
  51. struct Curl_hash_element {
  52. struct Curl_llist_element list;
  53. void *ptr;
  54. size_t key_len;
  55. char key[1]; /* allocated memory following the struct */
  56. };
  57. struct Curl_hash_iterator {
  58. struct Curl_hash *hash;
  59. int slot_index;
  60. struct Curl_llist_element *current_element;
  61. };
  62. void Curl_hash_init(struct Curl_hash *h,
  63. int slots,
  64. hash_function hfunc,
  65. comp_function comparator,
  66. Curl_hash_dtor dtor);
  67. void *Curl_hash_add(struct Curl_hash *h, void *key, size_t key_len, void *p);
  68. int Curl_hash_delete(struct Curl_hash *h, void *key, size_t key_len);
  69. void *Curl_hash_pick(struct Curl_hash *, void *key, size_t key_len);
  70. void Curl_hash_apply(struct Curl_hash *h, void *user,
  71. void (*cb)(void *user, void *ptr));
  72. #define Curl_hash_count(h) ((h)->size)
  73. void Curl_hash_destroy(struct Curl_hash *h);
  74. void Curl_hash_clean(struct Curl_hash *h);
  75. void Curl_hash_clean_with_criterium(struct Curl_hash *h, void *user,
  76. int (*comp)(void *, void *));
  77. size_t Curl_hash_str(void *key, size_t key_length, size_t slots_num);
  78. size_t Curl_str_key_compare(void *k1, size_t key1_len, void *k2,
  79. size_t key2_len);
  80. void Curl_hash_start_iterate(struct Curl_hash *hash,
  81. struct Curl_hash_iterator *iter);
  82. struct Curl_hash_element *
  83. Curl_hash_next_element(struct Curl_hash_iterator *iter);
  84. void Curl_hash_print(struct Curl_hash *h,
  85. void (*func)(void *));
  86. #endif /* HEADER_CURL_HASH_H */