hash_set.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*!
  2. Temelia - Hash Set interface.
  3. Copyright (C) 2008 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 HASHTABLE_H_
  18. #define HASHTABLE_H_
  19. #include "platform.h"
  20. struct _hash_set_t;
  21. typedef struct _hash_set_t *hash_set_t;
  22. /*!
  23. * @brief Constructor - returns an empty hash set with given size.
  24. * Complexity O(1)
  25. *
  26. * @param Hash set's size
  27. */
  28. DECLSPEC hash_set_t hash_set_new(int size);
  29. /*!
  30. * @brief Clears hash set.
  31. * Complexity O(1)
  32. *
  33. * @param Hash set
  34. */
  35. DECLSPEC void hash_set_clear(hash_set_t hash_set);
  36. /*!
  37. * @brief Frees the memory occupied by hash set.
  38. * Complexity O(1)
  39. *
  40. * @param Hash set
  41. */
  42. DECLSPEC void hash_set_delete(hash_set_t hash_set);
  43. /*!
  44. * @brief Checks if hash set contains key. Return 1 if it does, 0 if not
  45. * or -1 if an error occurred.
  46. * Complexity O(n/memory)
  47. *
  48. * @param Hash set
  49. * @param Key
  50. * @param Pointer to hash function; function should return a number
  51. * between 0 and hash set size - 1, indicating on which linked list
  52. * should the key be inserted
  53. * @param Pointer to comparison function
  54. */
  55. DECLSPEC char hash_set_contains(hash_set_t hash_set, void *key, int hash(void *key,
  56. void *context), void *hash_context, int compare(void *x, void *y,
  57. void *context), void *compare_context);
  58. /*!
  59. * @brief Returns the backbone vector of the hash set. It's a vector instance; in order to use it
  60. * feel free to cast the returned pointer to vector_t.
  61. * Complexity O(1)
  62. *
  63. * @param Hash set
  64. */
  65. DECLSPEC void *hash_set_get_backbone(hash_set_t hash_set);
  66. /*!
  67. * @brief Returns the linked list consisting of keys with given hash value.
  68. * Complexity O(1)
  69. *
  70. * @param Hash set
  71. * @param Hash value
  72. */
  73. DECLSPEC void *hash_set_get_collision_list(hash_set_t hash_set, int hash_value);
  74. /*!
  75. * @brief Checks if hash set is empty.
  76. * Complexity O(1)
  77. *
  78. * @param Hash set
  79. */
  80. DECLSPEC int hash_set_is_empty(hash_set_t hash_set);
  81. /*!
  82. * @brief Returns the size of hash set.
  83. * Complexity O(1)
  84. *
  85. * @param Hash set
  86. */
  87. DECLSPEC int hash_set_get_size(hash_set_t hash_set);
  88. /*!
  89. * @brief Adds key to hash set. Returns 1 if key was added to hash set, 0 if not
  90. * or -1 if an error occurred.
  91. * Complexity O(n/memory)
  92. *
  93. * @param Hash set
  94. * @param Key
  95. * @param Pointer to hash function
  96. * @param Pointer to comparison function
  97. */
  98. DECLSPEC char hash_set_put(hash_set_t hash_set, void *key, int hash(void *key,
  99. void *context), void *hash_context, int compare(void *x, void *y,
  100. void *context), void *compare_context);
  101. /*!
  102. * @brief Removes key from hash set.
  103. * Returns 1 if success, 0 if key wasn't found or -1 if an error occurred.
  104. * Complexity O(n/memory)
  105. *
  106. * @param Hash set
  107. * @param Key reference
  108. * @param Pointer to hash function
  109. * @param Pointer to comparison function
  110. */
  111. DECLSPEC int hash_set_remove(hash_set_t hash_set, void *key, int hash(void *key,
  112. void *context), void *hash_context, int compare(void *x, void *y,
  113. void *context), void *compare_context);
  114. /*!
  115. * @brief Prints the content of hash set. For each linked list print the hash
  116. * and the keys with same hash. After each linked list, key_handler is called with
  117. * a NULL pointer in order to announce that it starts printing the keys of another list.
  118. * Complexity O(n)
  119. *
  120. * @param Hash set
  121. * @param Pointer to iterating function
  122. * @param Context
  123. */
  124. DECLSPEC void hash_set_iterate(hash_set_t hash_set, void key_handler(void *key,
  125. void *context), void *context);
  126. #endif /* HASHTABLE_H_ */