hashtable.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2009-2013 Petri Lehtinen <petri@digip.org>
  3. *
  4. * This library is free software; you can redistribute it and/or modify
  5. * it under the terms of the MIT license. See LICENSE for details.
  6. */
  7. #ifndef HASHTABLE_H
  8. #define HASHTABLE_H
  9. struct hashtable_list {
  10. struct hashtable_list *prev;
  11. struct hashtable_list *next;
  12. };
  13. /* "pair" may be a bit confusing a name, but think of it as a
  14. key-value pair. In this case, it just encodes some extra data,
  15. too */
  16. struct hashtable_pair {
  17. size_t hash;
  18. struct hashtable_list list;
  19. json_t *value;
  20. size_t serial;
  21. char key[1];
  22. };
  23. struct hashtable_bucket {
  24. struct hashtable_list *first;
  25. struct hashtable_list *last;
  26. };
  27. typedef struct hashtable {
  28. size_t size;
  29. struct hashtable_bucket *buckets;
  30. size_t num_buckets; /* index to primes[] */
  31. struct hashtable_list list;
  32. } hashtable_t;
  33. #define hashtable_key_to_iter(key_) \
  34. (&(container_of(key_, struct hashtable_pair, key)->list))
  35. /**
  36. * hashtable_init - Initialize a hashtable object
  37. *
  38. * @hashtable: The (statically allocated) hashtable object
  39. *
  40. * Initializes a statically allocated hashtable object. The object
  41. * should be cleared with hashtable_close when it's no longer used.
  42. *
  43. * Returns 0 on success, -1 on error (out of memory).
  44. */
  45. int hashtable_init(hashtable_t *hashtable);
  46. /**
  47. * hashtable_close - Release all resources used by a hashtable object
  48. *
  49. * @hashtable: The hashtable
  50. *
  51. * Destroys a statically allocated hashtable object.
  52. */
  53. void hashtable_close(hashtable_t *hashtable);
  54. /**
  55. * hashtable_set - Add/modify value in hashtable
  56. *
  57. * @hashtable: The hashtable object
  58. * @key: The key
  59. * @serial: For addition order of keys
  60. * @value: The value
  61. *
  62. * If a value with the given key already exists, its value is replaced
  63. * with the new value. Value is "stealed" in the sense that hashtable
  64. * doesn't increment its refcount but decreases the refcount when the
  65. * value is no longer needed.
  66. *
  67. * Returns 0 on success, -1 on failure (out of memory).
  68. */
  69. int hashtable_set(hashtable_t *hashtable,
  70. const char *key, size_t serial,
  71. json_t *value);
  72. /**
  73. * hashtable_get - Get a value associated with a key
  74. *
  75. * @hashtable: The hashtable object
  76. * @key: The key
  77. *
  78. * Returns value if it is found, or NULL otherwise.
  79. */
  80. void *hashtable_get(hashtable_t *hashtable, const char *key);
  81. /**
  82. * hashtable_del - Remove a value from the hashtable
  83. *
  84. * @hashtable: The hashtable object
  85. * @key: The key
  86. *
  87. * Returns 0 on success, or -1 if the key was not found.
  88. */
  89. int hashtable_del(hashtable_t *hashtable, const char *key);
  90. /**
  91. * hashtable_clear - Clear hashtable
  92. *
  93. * @hashtable: The hashtable object
  94. *
  95. * Removes all items from the hashtable.
  96. */
  97. void hashtable_clear(hashtable_t *hashtable);
  98. /**
  99. * hashtable_iter - Iterate over hashtable
  100. *
  101. * @hashtable: The hashtable object
  102. *
  103. * Returns an opaque iterator to the first element in the hashtable.
  104. * The iterator should be passed to hashtable_iter_* functions.
  105. * The hashtable items are not iterated over in any particular order.
  106. *
  107. * There's no need to free the iterator in any way. The iterator is
  108. * valid as long as the item that is referenced by the iterator is not
  109. * deleted. Other values may be added or deleted. In particular,
  110. * hashtable_iter_next() may be called on an iterator, and after that
  111. * the key/value pair pointed by the old iterator may be deleted.
  112. */
  113. void *hashtable_iter(hashtable_t *hashtable);
  114. /**
  115. * hashtable_iter_at - Return an iterator at a specific key
  116. *
  117. * @hashtable: The hashtable object
  118. * @key: The key that the iterator should point to
  119. *
  120. * Like hashtable_iter() but returns an iterator pointing to a
  121. * specific key.
  122. */
  123. void *hashtable_iter_at(hashtable_t *hashtable, const char *key);
  124. /**
  125. * hashtable_iter_next - Advance an iterator
  126. *
  127. * @hashtable: The hashtable object
  128. * @iter: The iterator
  129. *
  130. * Returns a new iterator pointing to the next element in the
  131. * hashtable or NULL if the whole hastable has been iterated over.
  132. */
  133. void *hashtable_iter_next(hashtable_t *hashtable, void *iter);
  134. /**
  135. * hashtable_iter_key - Retrieve the key pointed by an iterator
  136. *
  137. * @iter: The iterator
  138. */
  139. void *hashtable_iter_key(void *iter);
  140. /**
  141. * hashtable_iter_serial - Retrieve the serial number pointed to by an iterator
  142. *
  143. * @iter: The iterator
  144. */
  145. size_t hashtable_iter_serial(void *iter);
  146. /**
  147. * hashtable_iter_value - Retrieve the value pointed by an iterator
  148. *
  149. * @iter: The iterator
  150. */
  151. void *hashtable_iter_value(void *iter);
  152. /**
  153. * hashtable_iter_set - Set the value pointed by an iterator
  154. *
  155. * @iter: The iterator
  156. * @value: The value to set
  157. */
  158. void hashtable_iter_set(void *iter, json_t *value);
  159. #endif