hash.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #ifndef __HASH_H
  2. #define __HASH_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 1998 - 2007, 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 http://curl.haxx.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. * $Id$
  24. ***************************************************************************/
  25. #include "setup.h"
  26. #include <stddef.h>
  27. #include "llist.h"
  28. /* Hash function prototype */
  29. typedef size_t (*hash_function) (void* key,
  30. size_t key_length,
  31. size_t slots_num);
  32. /*
  33. Comparator function prototype. Compares two keys.
  34. */
  35. typedef size_t (*comp_function) (void* key1,
  36. size_t key1_len,
  37. void*key2,
  38. size_t key2_len);
  39. typedef void (*curl_hash_dtor)(void *);
  40. struct curl_hash {
  41. struct curl_llist **table;
  42. /* Hash function to be used for this hash table */
  43. hash_function hash_func;
  44. /* Comparator function to compare keys */
  45. comp_function comp_func;
  46. curl_hash_dtor dtor;
  47. int slots;
  48. size_t size;
  49. };
  50. struct curl_hash_element {
  51. void *ptr;
  52. char *key;
  53. size_t key_len;
  54. };
  55. int Curl_hash_init(struct curl_hash *h,
  56. int slots,
  57. hash_function hfunc,
  58. comp_function comparator,
  59. curl_hash_dtor dtor);
  60. struct curl_hash *Curl_hash_alloc(int slots,
  61. hash_function hfunc,
  62. comp_function comparator,
  63. curl_hash_dtor dtor);
  64. void *Curl_hash_add(struct curl_hash *h, void *key, size_t key_len, void *p);
  65. int Curl_hash_delete(struct curl_hash *h, void *key, size_t key_len);
  66. void *Curl_hash_pick(struct curl_hash *, void * key, size_t key_len);
  67. void Curl_hash_apply(struct curl_hash *h, void *user,
  68. void (*cb)(void *user, void *ptr));
  69. int Curl_hash_count(struct curl_hash *h);
  70. void Curl_hash_clean(struct curl_hash *h);
  71. void Curl_hash_clean_with_criterium(struct curl_hash *h, void *user,
  72. int (*comp)(void *, void *));
  73. void Curl_hash_destroy(struct curl_hash *h);
  74. size_t Curl_hash_str(void* key, size_t key_length, size_t slots_num);
  75. size_t Curl_str_key_compare(void*k1, size_t key1_len, void*k2,
  76. size_t key2_len);
  77. #endif