hash.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* $Id$
  2. *
  3. * Copyright 2001-2003, Meiosys (www.meiosys.com). All rights reserved.
  4. * Author: Marc Vertes <mvertes@meiosys.com>
  5. * See the COPYING file for the terms of usage and distribution.
  6. */
  7. #ifndef __sd_hash_h
  8. #define __sd_hash_h
  9. /**
  10. * @file hash.h
  11. *
  12. * @brief Generic hash table. It is implemented as an array of doubly-linked
  13. * lists of iterators. The index within the array is computed by a efficient
  14. * hash function.
  15. */
  16. #include <stddef.h>
  17. #include <sd/defs.h>
  18. __SD_BEGIN_DECLS
  19. struct __sd_hash_ops {
  20. unsigned int (*hash) (const void*);
  21. int (*compare) (const void*, const void*);
  22. void* (*key_dup) (const void*);
  23. void (*key_free) (void*);
  24. void* (*data_dup) (const void*);
  25. void (*data_free) (void*);
  26. };
  27. /**
  28. * This structure holds hash table operations.
  29. */
  30. typedef struct __sd_hash_ops sd_hash_ops_t;
  31. /**
  32. * This is the hash table.
  33. */
  34. typedef struct __sd_hash sd_hash_t;
  35. struct __sd_hash_iter {
  36. void* key;
  37. void* data;
  38. struct __sd_hash* hash;
  39. unsigned int __hkey;
  40. struct __sd_hash_iter* __next;
  41. struct __sd_hash_iter* __prev;
  42. int __foreach;
  43. };
  44. /**
  45. * This is the elementary container for storing data into the hash table.
  46. */
  47. typedef struct __sd_hash_iter sd_hash_iter_t;
  48. /**
  49. * Signature of a "foreach" function.
  50. */
  51. typedef unsigned int (*sd_hash_func_t)(void* a_key, void* a_data,
  52. void* a_userdata);
  53. /**
  54. * Creates a new hash table. One can customize the memory (de)allocation
  55. * policy for keys and data stored in the hash table.
  56. * @param a_size the initial size of the array.
  57. * @param a_ops the hash operations. If NULL, then string keys are assumed and
  58. * no memory (de)allocation is performed for keys and data.
  59. * @return a dynamicaly allocated hash table.
  60. */
  61. extern sd_hash_t* sd_hash_new(size_t a_size, const sd_hash_ops_t* a_ops);
  62. /**
  63. * Destroys the hash table.
  64. */
  65. extern void sd_hash_delete(sd_hash_t* a_this);
  66. /**
  67. * clears the hash table.
  68. */
  69. extern void sd_hash_clear(sd_hash_t* a_this);
  70. /**
  71. * Looks for the iterator associated to the given key in the hash table.
  72. * @param a_key the key associated to the iterator.
  73. * @return a pointer to the found iterator or NULL.
  74. */
  75. extern sd_hash_iter_t* sd_hash_lookup(sd_hash_t* a_this, const void* a_key);
  76. /**
  77. * Looks for the iterator associated to the given key in the hash table and
  78. * creates it if doesn't exist.
  79. * @param a_key the key associated to the iterator.
  80. * @return a pointer to the found iterator or NULL.
  81. */
  82. extern sd_hash_iter_t* sd_hash_lookadd(sd_hash_t* a_this, const void* a_key);
  83. /**
  84. * Adds data associated with the given key into the hash table. If the
  85. * key already exists, the old iterator is freed according to the memory
  86. * management operations passed to sd_hash_new().
  87. * @param a_key the key associated to the iterator.
  88. * @return a pointer to the created or found iterator.
  89. */
  90. extern sd_hash_iter_t* sd_hash_add(sd_hash_t* a_this, const void* a_key,
  91. void* a_data);
  92. /**
  93. * Removes an iterator from the hash table.
  94. * @param a_key the key associated to the iterator.
  95. */
  96. extern void sd_hash_del(sd_hash_t* a_this, const void* a_key);
  97. /**
  98. * Calls \a a_func for each element of the hash table, as long as \a a_func
  99. * returns 0.
  100. * @param a_func the "foreach" function.
  101. * @param a_data the user data passed to \a a_func.
  102. */
  103. extern void sd_hash_foreach(sd_hash_t* a_this, sd_hash_func_t a_func,
  104. void* a_data);
  105. /**
  106. * Gets the number of iterators.
  107. */
  108. extern unsigned int sd_hash_get_nelem(sd_hash_t* a_this);
  109. /**
  110. * Gets the size of the array.
  111. */
  112. extern unsigned int sd_hash_get_size(sd_hash_t* a_this);
  113. /**
  114. * Gets the first iterator.
  115. */
  116. extern sd_hash_iter_t* sd_hash_begin(sd_hash_t* a_this);
  117. /**
  118. * Gets the last iterator.
  119. */
  120. extern sd_hash_iter_t* sd_hash_end(sd_hash_t* a_this);
  121. /**
  122. * Gets a pointer to the next iterator.
  123. */
  124. extern sd_hash_iter_t* sd_hash_iter_next(sd_hash_iter_t* a_this);
  125. /**
  126. * Gets a pointer to the previous iterator.
  127. */
  128. extern sd_hash_iter_t* sd_hash_iter_prev(sd_hash_iter_t* a_this);
  129. /**
  130. * Gets a pointer to the previous iterator.
  131. */
  132. extern void sd_hash_iter_del(sd_hash_iter_t* a_this);
  133. /**
  134. * Hashes strings.
  135. */
  136. extern unsigned int sd_hash_hash_string(const char* a_string);
  137. __SD_END_DECLS
  138. #endif