You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ucl_hash.h 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Copyright (c) 2013, Vsevolod Stakhov
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. *
  12. * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
  13. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
  16. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  19. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #ifndef __UCL_HASH_H
  24. #define __UCL_HASH_H
  25. #include "ucl.h"
  26. /******************************************************************************/
  27. struct ucl_hash_node_s;
  28. typedef struct ucl_hash_node_s ucl_hash_node_t;
  29. typedef int (*ucl_hash_cmp_func) (const void* void_a, const void* void_b);
  30. typedef void (*ucl_hash_free_func) (void *ptr);
  31. typedef void* ucl_hash_iter_t;
  32. /**
  33. * Linear chained hashtable.
  34. */
  35. struct ucl_hash_struct;
  36. typedef struct ucl_hash_struct ucl_hash_t;
  37. /**
  38. * Initializes the hashtable.
  39. */
  40. ucl_hash_t* ucl_hash_create (bool ignore_case);
  41. /**
  42. * Deinitializes the hashtable.
  43. */
  44. void ucl_hash_destroy (ucl_hash_t* hashlin, ucl_hash_free_func func);
  45. /**
  46. * Inserts an element in the the hashtable.
  47. * @return true on success, false on failure (i.e. ENOMEM)
  48. */
  49. bool ucl_hash_insert (ucl_hash_t* hashlin, const ucl_object_t *obj, const char *key,
  50. unsigned keylen);
  51. /**
  52. * Replace element in the hash
  53. */
  54. void ucl_hash_replace (ucl_hash_t* hashlin, const ucl_object_t *old,
  55. const ucl_object_t *new);
  56. /**
  57. * Delete an element from the the hashtable.
  58. */
  59. void ucl_hash_delete (ucl_hash_t* hashlin, const ucl_object_t *obj);
  60. /**
  61. * Searches an element in the hashtable.
  62. */
  63. const ucl_object_t* ucl_hash_search (ucl_hash_t* hashlin, const char *key,
  64. unsigned keylen);
  65. /**
  66. * Iterate over hash table
  67. * @param hashlin hash
  68. * @param iter iterator (must be NULL on first iteration)
  69. * @param ep pointer record exception (such as ENOMEM), could be NULL
  70. * @return the next object
  71. */
  72. const void* ucl_hash_iterate2 (ucl_hash_t *hashlin, ucl_hash_iter_t *iter, int *ep);
  73. /**
  74. * Helper macro to support older code
  75. */
  76. #define ucl_hash_iterate(hl, ip) ucl_hash_iterate2((hl), (ip), NULL)
  77. /**
  78. * Check whether an iterator has next element
  79. */
  80. bool ucl_hash_iter_has_next (ucl_hash_t *hashlin, ucl_hash_iter_t iter);
  81. /**
  82. * Reserves space in hash
  83. * @return true on sucess, false on failure (e.g. ENOMEM)
  84. * @param hashlin
  85. */
  86. bool ucl_hash_reserve (ucl_hash_t *hashlin, size_t sz);
  87. /**
  88. * Sorts keys in a hash
  89. * @param hashlin
  90. * @param fl
  91. */
  92. void ucl_hash_sort (ucl_hash_t *hashlin, enum ucl_object_keys_sort_flags fl);
  93. #endif