hash.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #ifndef HEADER_CURL_HASH_H
  2. #define HEADER_CURL_HASH_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) Daniel Stenberg, <[email protected]>, 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. typedef void (*Curl_hash_elem_dtor)(void *key, size_t key_len, void *p);
  42. struct Curl_hash_element {
  43. struct Curl_hash_element *next;
  44. void *ptr;
  45. Curl_hash_elem_dtor dtor;
  46. size_t key_len;
  47. char key[1]; /* allocated memory following the struct */
  48. };
  49. struct Curl_hash {
  50. struct Curl_hash_element **table;
  51. /* Hash function to be used for this hash table */
  52. hash_function hash_func;
  53. /* Comparator function to compare keys */
  54. comp_function comp_func;
  55. /* General element construct, unless element itself carries one */
  56. Curl_hash_dtor dtor;
  57. size_t slots;
  58. size_t size;
  59. #ifdef DEBUGBUILD
  60. int init;
  61. #endif
  62. };
  63. struct Curl_hash_iterator {
  64. struct Curl_hash *hash;
  65. size_t slot_index;
  66. struct Curl_hash_element *current;
  67. #ifdef DEBUGBUILD
  68. int init;
  69. #endif
  70. };
  71. void Curl_hash_init(struct Curl_hash *h,
  72. size_t slots,
  73. hash_function hfunc,
  74. comp_function comparator,
  75. Curl_hash_dtor dtor);
  76. void *Curl_hash_add(struct Curl_hash *h, void *key, size_t key_len, void *p);
  77. void *Curl_hash_add2(struct Curl_hash *h, void *key, size_t key_len, void *p,
  78. Curl_hash_elem_dtor dtor);
  79. int Curl_hash_delete(struct Curl_hash *h, void *key, size_t key_len);
  80. void *Curl_hash_pick(struct Curl_hash *, void *key, size_t key_len);
  81. void Curl_hash_destroy(struct Curl_hash *h);
  82. size_t Curl_hash_count(struct Curl_hash *h);
  83. void Curl_hash_clean(struct Curl_hash *h);
  84. void Curl_hash_clean_with_criterium(struct Curl_hash *h, void *user,
  85. int (*comp)(void *, void *));
  86. size_t Curl_hash_str(void *key, size_t key_length, size_t slots_num);
  87. size_t curlx_str_key_compare(void *k1, size_t key1_len, void *k2,
  88. size_t key2_len);
  89. void Curl_hash_start_iterate(struct Curl_hash *hash,
  90. struct Curl_hash_iterator *iter);
  91. struct Curl_hash_element *
  92. Curl_hash_next_element(struct Curl_hash_iterator *iter);
  93. void Curl_hash_print(struct Curl_hash *h,
  94. void (*func)(void *));
  95. #endif /* HEADER_CURL_HASH_H */