hash.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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, void* a_userdata);
  52. /**
  53. * Creates a new hash table. One can customize the memory (de)allocation
  54. * policy for keys and data stored in the hash table.
  55. * @param a_size the initial size of the array.
  56. * @param a_ops the hash operations. If NULL, then string keys are assumed and
  57. * no memory (de)allocation is performed for keys and data.
  58. * @return a dynamicaly allocated hash table.
  59. */
  60. extern sd_hash_t* sd_hash_new(size_t a_size, const sd_hash_ops_t* a_ops);
  61. /**
  62. * Destroys the hash table.
  63. */
  64. extern void sd_hash_delete(sd_hash_t* a_this);
  65. /**
  66. * clears the hash table.
  67. */
  68. extern void sd_hash_clear(sd_hash_t* a_this);
  69. /**
  70. * Looks for the iterator associated to the given key in the hash table.
  71. * @param a_key the key associated to the iterator.
  72. * @return a pointer to the found iterator or NULL.
  73. */
  74. extern sd_hash_iter_t* sd_hash_lookup(sd_hash_t* a_this, const void* a_key);
  75. /**
  76. * Looks for the iterator associated to the given key in the hash table and
  77. * creates it if doesn't exist.
  78. * @param a_key the key associated to the iterator.
  79. * @return a pointer to the found iterator or NULL.
  80. */
  81. extern sd_hash_iter_t* sd_hash_lookadd(sd_hash_t* a_this, const void* a_key);
  82. /**
  83. * Adds data associated with the given key into the hash table. If the
  84. * key already exists, the old iterator is freed according to the memory
  85. * management operations passed to sd_hash_new().
  86. * @param a_key the key associated to the iterator.
  87. * @return a pointer to the created or found iterator.
  88. */
  89. extern sd_hash_iter_t* sd_hash_add(sd_hash_t* a_this, const void* a_key, void* a_data);
  90. /**
  91. * Removes an iterator from the hash table.
  92. * @param a_key the key associated to the iterator.
  93. */
  94. extern void sd_hash_del(sd_hash_t* a_this, const void* a_key);
  95. /**
  96. * Calls \a a_func for each element of the hash table, as long as \a a_func
  97. * returns 0.
  98. * @param a_func the "foreach" function.
  99. * @param a_data the user data passed to \a a_func.
  100. */
  101. extern void sd_hash_foreach(sd_hash_t* a_this, sd_hash_func_t a_func, void* a_data);
  102. /**
  103. * Gets the number of iterators.
  104. */
  105. extern unsigned int sd_hash_get_nelem(sd_hash_t* a_this);
  106. /**
  107. * Gets the size of the array.
  108. */
  109. extern unsigned int sd_hash_get_size(sd_hash_t* a_this);
  110. /**
  111. * Gets the first iterator.
  112. */
  113. extern sd_hash_iter_t* sd_hash_begin(sd_hash_t* a_this);
  114. /**
  115. * Gets the last iterator.
  116. */
  117. extern sd_hash_iter_t* sd_hash_end(sd_hash_t* a_this);
  118. /**
  119. * Gets a pointer to the next iterator.
  120. */
  121. extern sd_hash_iter_t* sd_hash_iter_next(sd_hash_iter_t* a_this);
  122. /**
  123. * Gets a pointer to the previous iterator.
  124. */
  125. extern sd_hash_iter_t* sd_hash_iter_prev(sd_hash_iter_t* a_this);
  126. /**
  127. * Gets a pointer to the previous iterator.
  128. */
  129. extern void sd_hash_iter_del(sd_hash_iter_t* a_this);
  130. /**
  131. * Hashes strings.
  132. */
  133. extern unsigned int sd_hash_hash_string(const TCHAR* a_string);
  134. __SD_END_DECLS
  135. #endif