hash_map.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*!
  2. Temelia - Hash Map interface.
  3. Copyright (C) 2008, 2009 Ceata (http://cod.ceata.org/proiecte/temelia).
  4. @author Dascalu Laurentiu
  5. This program is free software; you can redistribute it and
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 3
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #ifndef HASHSET_H_
  18. #define HASHSET_H_
  19. #include "platform.h"
  20. struct _hash_map_t;
  21. typedef struct _hash_map_t *hash_map_t;
  22. /*!
  23. * @brief Constructor.
  24. * Complexity O(1)
  25. *
  26. * @return Empty hash map
  27. */
  28. DECLSPEC hash_map_t hash_map_new(void);
  29. /*!
  30. * @brief Clears hash map.
  31. * Complexity O(1)
  32. *
  33. * @param Hash map
  34. */
  35. DECLSPEC void hash_map_clear(hash_map_t hash_map);
  36. /*!
  37. * @brief Destructor. Frees the memory occupied by hash map.
  38. * Complexity O(n)
  39. *
  40. * @param Hash map
  41. */
  42. DECLSPEC void hash_map_delete(hash_map_t hash_map);
  43. /*!
  44. * @brief Checks if hash map contains key. Return 1 if it does, 0 if not
  45. * or -1 if an error occurred.
  46. * Complexity O(log(n))
  47. *
  48. * @param Hash map
  49. * @param Key
  50. * @param Pointer to keys comparison function
  51. * @param Comparison context
  52. */
  53. DECLSPEC int hash_map_contains_key(hash_map_t hash_map, void *key, int compare(void *x,
  54. void *y, void *context), void *context);
  55. /*!
  56. * @brief Checks if hash map contains value.
  57. * Complexity O(log(n))
  58. *
  59. * @param Hash map
  60. * @param Value
  61. * @param Pointer to values comparison function
  62. * @param Comparison context
  63. * @see hash_map_contains_key
  64. */
  65. DECLSPEC int hash_map_contains_value(hash_map_t hash_map, void *value,
  66. int compare_values(void *x, void *y, void *context), void *context);
  67. /*!
  68. * @brief Checks if hash map is empty
  69. * Complexity O(1)
  70. *
  71. * @param Hash map
  72. */
  73. DECLSPEC int hash_map_is_empty(hash_map_t hash_map);
  74. /*!
  75. * @brief Returns the size of hash map.
  76. * Complexity O(1)
  77. *
  78. * @param Hash map
  79. */
  80. DECLSPEC int hash_map_get_size(hash_map_t hash_map);
  81. /*!
  82. * @brief Adds pair (key, value) to hash map. If key is found then
  83. * it's value is actualized to "value".
  84. * Complexity O(log(n))
  85. *
  86. * @param Hash map
  87. * @param Key
  88. * @param Value
  89. * @param Pointer to keys comparison function
  90. * @return key's old value or NULL if it was not in this hash map
  91. */
  92. DECLSPEC void *hash_map_put(hash_map_t hash_map, void *key, void *value,
  93. int compare_keys(void *x, void *y, void *context), void *context);
  94. /*!
  95. * @brief Removes key from hash map and it's value.
  96. * Complexity O(log(n))
  97. *
  98. * @param Hash map
  99. * @param Key
  100. * @param Pointer to keys comparison function
  101. * @return 1 if success, 0 if not, -1 if an error occurred
  102. */
  103. DECLSPEC int hash_map_remove_key(hash_map_t hash_map, void *key, int compare_keys(
  104. void *x, void *y, void *context), void *context);
  105. /*!
  106. * @brief Removes value from hash map and it's key.
  107. * Complexity O(log(n))
  108. *
  109. * @param Hash map
  110. * @param Value
  111. * @param Pointer to values comparison function
  112. * @return 1 if success, 0 if not, -1 if an error occurred
  113. */
  114. DECLSPEC int hash_map_remove_value(hash_map_t hash_map, void *value, int compare_values(
  115. void *x, void *y, void *context), void *context);
  116. /*!
  117. * @brief Returns hash map internal representation; it's a root of red-black tree.
  118. * Complexity O(1)
  119. *
  120. * @param Hash map
  121. */
  122. DECLSPEC void *hash_map_get_internal_representation(hash_map_t hash_map);
  123. /*!
  124. * @brief Prints the content of hash map calling key_handler and value_handler for each
  125. * pair stored in this container
  126. * Complexity O(n)
  127. *
  128. * @param Hash map
  129. * @param Pointer to key handling function
  130. * @param Pointer to context
  131. */
  132. DECLSPEC void hash_map_iterate_keys(hash_map_t hash_map, void handler(void *key,
  133. void *context), void *context);
  134. /*!
  135. * @brief Prints the content of hash map calling key_handler and value_handler for each
  136. * pair stored in this container.
  137. * Complexity O(n)
  138. *
  139. * @param Hash map
  140. * @param Pointer to value handling function
  141. * @param Pointer to context
  142. */
  143. DECLSPEC void hash_map_iterate_values(hash_map_t hash_map, void handler(void *value,
  144. void *context), void *context);
  145. /*!
  146. * @brief Iterates over hash map and calls given handler with pairs (key, value).
  147. * Complexity O(n)
  148. *
  149. * @param Hash map
  150. * @param Pointer to handling function
  151. * @param Pointer to context
  152. */
  153. DECLSPEC void hash_map_iterate(hash_map_t hash_map, void handler(void *key, void *value,
  154. void *context), void *context);
  155. #endif /* HASHSET_H_ */