hash.h 3.1 KB

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