hash.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #ifndef __HASH_H
  2. #define __HASH_H
  3. /*****************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 2000, Daniel Stenberg, <[email protected]>, et al.
  11. *
  12. * In order to be useful for every potential user, curl and libcurl are
  13. * dual-licensed under the MPL and the MIT/X-derivate licenses.
  14. *
  15. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. * copies of the Software, and permit persons to whom the Software is
  17. * furnished to do so, under the terms of the MPL or the MIT/X-derivate
  18. * licenses. You may pick one of these licenses.
  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. #define CURL_HASH_KEY_IS_STRING 0
  29. #define CURL_HASH_KEY_IS_NUM 1
  30. typedef void (*curl_hash_dtor)(void *);
  31. typedef struct _curl_hash {
  32. curl_llist **table;
  33. curl_hash_dtor dtor;
  34. int slots;
  35. size_t size;
  36. } curl_hash;
  37. typedef struct _curl_hash_key {
  38. union {
  39. struct {
  40. char *val;
  41. unsigned int len;
  42. } str;
  43. unsigned long num;
  44. } value;
  45. int type;
  46. } curl_hash_key;
  47. typedef struct _curl_hash_element {
  48. curl_hash_key key;
  49. void *ptr;
  50. } curl_hash_element;
  51. void curl_hash_init(curl_hash *h, int slots, curl_hash_dtor dtor);
  52. curl_hash *curl_hash_alloc(int slots, curl_hash_dtor dtor);
  53. int curl_hash_add_or_update(curl_hash *h, char *str_key, unsigned int str_key_len,
  54. unsigned long num_key, const void *p);
  55. int curl_hash_extended_delete(curl_hash *h, char *str_key, unsigned int str_key_len,
  56. unsigned long num_key);
  57. int curl_hash_extended_find(curl_hash *h, char *str_key, unsigned int str_key_len,
  58. unsigned long num_key, void **p);
  59. void curl_hash_apply(curl_hash *h, void *user, void (*cb)(void *, curl_hash_element *));
  60. size_t curl_hash_count(curl_hash *h);
  61. void curl_hash_clean(curl_hash *h);
  62. void curl_hash_destroy(curl_hash *h);
  63. #define curl_hash_find(h, key, key_len, p) curl_hash_extended_find(h, key, key_len, 0, p)
  64. #define curl_hash_delete(h, key, key_len) curl_hash_extended_delete(h, key, key_len, 0)
  65. #define curl_hash_add(h, key, key_len, p) curl_hash_add_or_update(h, key, key_len, 0, p)
  66. #define curl_hash_update curl_hash_add
  67. #define curl_hash_index_find(h, key, p) curl_hash_extended_find(h, NULL, 0, key, p)
  68. #define curl_hash_index_delete(h, key) curl_hash_extended_delete(h, NULL, 0, key)
  69. #define curl_hash_index_add(h, key, p) curl_hash_add_or_update(h, NULL, 0, key, p)
  70. #define curl_hash_index_update curl_hash_index_add
  71. #endif