htable.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef HADOOP_CORE_COMMON_HASH_TABLE
  19. #define HADOOP_CORE_COMMON_HASH_TABLE
  20. #include <inttypes.h>
  21. #include <stdio.h>
  22. #include <stdint.h>
  23. #define HTABLE_MIN_SIZE 4
  24. struct htable;
  25. /**
  26. * An HTable hash function.
  27. *
  28. * @param key The key.
  29. * @param capacity The total capacity.
  30. *
  31. * @return The hash slot. Must be less than the capacity.
  32. */
  33. typedef uint32_t (*htable_hash_fn_t)(const void *key, uint32_t capacity);
  34. /**
  35. * An HTable equality function. Compares two keys.
  36. *
  37. * @param a First key.
  38. * @param b Second key.
  39. *
  40. * @return nonzero if the keys are equal.
  41. */
  42. typedef int (*htable_eq_fn_t)(const void *a, const void *b);
  43. /**
  44. * Allocate a new hash table.
  45. *
  46. * @param capacity The minimum suggested starting capacity.
  47. * @param hash_fun The hash function to use in this hash table.
  48. * @param eq_fun The equals function to use in this hash table.
  49. *
  50. * @return The new hash table on success; NULL on OOM.
  51. */
  52. struct htable *htable_alloc(uint32_t capacity, htable_hash_fn_t hash_fun,
  53. htable_eq_fn_t eq_fun);
  54. typedef void (*visitor_fn_t)(void *ctx, void *key, void *val);
  55. /**
  56. * Visit all of the entries in the hash table.
  57. *
  58. * @param htable The hash table.
  59. * @param fun The callback function to invoke on each key and value.
  60. * @param ctx Context pointer to pass to the callback.
  61. */
  62. void htable_visit(struct htable *htable, visitor_fn_t fun, void *ctx);
  63. /**
  64. * Free the hash table.
  65. *
  66. * It is up the calling code to ensure that the keys and values inside the
  67. * table are de-allocated, if that is necessary.
  68. *
  69. * @param htable The hash table.
  70. */
  71. void htable_free(struct htable *htable);
  72. /**
  73. * Add an entry to the hash table.
  74. *
  75. * @param htable The hash table.
  76. * @param key The key to add. This cannot be NULL.
  77. * @param fun The value to add. This cannot be NULL.
  78. *
  79. * @return 0 on success;
  80. * EEXIST if the value already exists in the table;
  81. * ENOMEM if there is not enough memory to add the element.
  82. * EFBIG if the hash table has too many entries to fit in 32
  83. * bits.
  84. */
  85. int htable_put(struct htable *htable, void *key, void *val);
  86. /**
  87. * Get an entry from the hash table.
  88. *
  89. * @param htable The hash table.
  90. * @param key The key to find.
  91. *
  92. * @return NULL if there is no such entry; the entry otherwise.
  93. */
  94. void *htable_get(const struct htable *htable, const void *key);
  95. /**
  96. * Get an entry from the hash table and remove it.
  97. *
  98. * @param htable The hash table.
  99. * @param key The key for the entry find and remove.
  100. * @param found_key (out param) NULL if the entry was not found; the found key
  101. * otherwise.
  102. * @param found_val (out param) NULL if the entry was not found; the found
  103. * value otherwise.
  104. */
  105. void htable_pop(struct htable *htable, const void *key,
  106. void **found_key, void **found_val);
  107. /**
  108. * Get the number of entries used in the hash table.
  109. *
  110. * @param htable The hash table.
  111. *
  112. * @return The number of entries used in the hash table.
  113. */
  114. uint32_t htable_used(const struct htable *htable);
  115. /**
  116. * Get the capacity of the hash table.
  117. *
  118. * @param htable The hash table.
  119. *
  120. * @return The capacity of the hash table.
  121. */
  122. uint32_t htable_capacity(const struct htable *htable);
  123. /**
  124. * Hash a string.
  125. *
  126. * @param str The string.
  127. * @param max Maximum hash value
  128. *
  129. * @return A number less than max.
  130. */
  131. uint32_t ht_hash_string(const void *str, uint32_t max);
  132. /**
  133. * Compare two strings.
  134. *
  135. * @param a The first string.
  136. * @param b The second string.
  137. *
  138. * @return 1 if the strings are identical; 0 otherwise.
  139. */
  140. int ht_compare_string(const void *a, const void *b);
  141. #endif
  142. // vim: ts=4:sw=4:tw=79:et