hash.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * @file
  3. *
  4. * @brief Chained hash tables
  5. *
  6. * This module implements the hash table support used in
  7. * various places in the library.
  8. *
  9. * @copyright See Copyright for the status of this software.
  10. */
  11. #ifndef __XML_HASH_H__
  12. #define __XML_HASH_H__
  13. #include <libxml/xmlversion.h>
  14. #include <libxml/dict.h>
  15. #include <libxml/xmlstring.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /**
  20. * Hash table mapping strings to pointers
  21. *
  22. * Also supports lookup using two or three strings as key.
  23. */
  24. typedef struct _xmlHashTable xmlHashTable;
  25. typedef xmlHashTable *xmlHashTablePtr;
  26. /*
  27. * Recent version of gcc produce a warning when a function pointer is assigned
  28. * to an object pointer, or vice versa. The following macro is a dirty hack
  29. * to allow suppression of the warning. If your architecture has function
  30. * pointers which are a different size than a void pointer, there may be some
  31. * serious trouble within the library.
  32. */
  33. /**
  34. * Macro to do a casting from an object pointer to a
  35. * function pointer without encountering a warning from
  36. * gcc
  37. *
  38. * \#define XML_CAST_FPTR(fptr) (*(void **)(&fptr))
  39. * This macro violated ISO C aliasing rules (gcc4 on s390 broke)
  40. * so it is disabled now
  41. *
  42. * @param fptr pointer to a function
  43. */
  44. #define XML_CAST_FPTR(fptr) fptr
  45. /*
  46. * function types:
  47. */
  48. /**
  49. * Callback to free data from a hash.
  50. *
  51. * @param payload the data in the hash
  52. * @param name the name associated
  53. */
  54. typedef void (*xmlHashDeallocator)(void *payload, const xmlChar *name);
  55. /**
  56. * Callback to copy data from a hash.
  57. *
  58. * @param payload the data in the hash
  59. * @param name the name associated
  60. * @returns a copy of the data or NULL in case of error.
  61. */
  62. typedef void *(*xmlHashCopier)(void *payload, const xmlChar *name);
  63. /**
  64. * Callback when scanning data in a hash with the simple scanner.
  65. *
  66. * @param payload the data in the hash
  67. * @param data extra scanner data
  68. * @param name the name associated
  69. */
  70. typedef void (*xmlHashScanner)(void *payload, void *data, const xmlChar *name);
  71. /**
  72. * Callback when scanning data in a hash with the full scanner.
  73. *
  74. * @param payload the data in the hash
  75. * @param data extra scanner data
  76. * @param name the name associated
  77. * @param name2 the second name associated
  78. * @param name3 the third name associated
  79. */
  80. typedef void (*xmlHashScannerFull)(void *payload, void *data,
  81. const xmlChar *name, const xmlChar *name2,
  82. const xmlChar *name3);
  83. /*
  84. * Constructor and destructor.
  85. */
  86. XMLPUBFUN xmlHashTable *
  87. xmlHashCreate (int size);
  88. XMLPUBFUN xmlHashTable *
  89. xmlHashCreateDict (int size,
  90. xmlDict *dict);
  91. XMLPUBFUN void
  92. xmlHashFree (xmlHashTable *hash,
  93. xmlHashDeallocator dealloc);
  94. XMLPUBFUN void
  95. xmlHashDefaultDeallocator(void *entry,
  96. const xmlChar *name);
  97. /*
  98. * Add a new entry to the hash table.
  99. */
  100. XMLPUBFUN int
  101. xmlHashAdd (xmlHashTable *hash,
  102. const xmlChar *name,
  103. void *userdata);
  104. XMLPUBFUN int
  105. xmlHashAddEntry (xmlHashTable *hash,
  106. const xmlChar *name,
  107. void *userdata);
  108. XMLPUBFUN int
  109. xmlHashUpdateEntry (xmlHashTable *hash,
  110. const xmlChar *name,
  111. void *userdata,
  112. xmlHashDeallocator dealloc);
  113. XMLPUBFUN int
  114. xmlHashAdd2 (xmlHashTable *hash,
  115. const xmlChar *name,
  116. const xmlChar *name2,
  117. void *userdata);
  118. XMLPUBFUN int
  119. xmlHashAddEntry2 (xmlHashTable *hash,
  120. const xmlChar *name,
  121. const xmlChar *name2,
  122. void *userdata);
  123. XMLPUBFUN int
  124. xmlHashUpdateEntry2 (xmlHashTable *hash,
  125. const xmlChar *name,
  126. const xmlChar *name2,
  127. void *userdata,
  128. xmlHashDeallocator dealloc);
  129. XMLPUBFUN int
  130. xmlHashAdd3 (xmlHashTable *hash,
  131. const xmlChar *name,
  132. const xmlChar *name2,
  133. const xmlChar *name3,
  134. void *userdata);
  135. XMLPUBFUN int
  136. xmlHashAddEntry3 (xmlHashTable *hash,
  137. const xmlChar *name,
  138. const xmlChar *name2,
  139. const xmlChar *name3,
  140. void *userdata);
  141. XMLPUBFUN int
  142. xmlHashUpdateEntry3 (xmlHashTable *hash,
  143. const xmlChar *name,
  144. const xmlChar *name2,
  145. const xmlChar *name3,
  146. void *userdata,
  147. xmlHashDeallocator dealloc);
  148. /*
  149. * Remove an entry from the hash table.
  150. */
  151. XMLPUBFUN int
  152. xmlHashRemoveEntry (xmlHashTable *hash,
  153. const xmlChar *name,
  154. xmlHashDeallocator dealloc);
  155. XMLPUBFUN int
  156. xmlHashRemoveEntry2 (xmlHashTable *hash,
  157. const xmlChar *name,
  158. const xmlChar *name2,
  159. xmlHashDeallocator dealloc);
  160. XMLPUBFUN int
  161. xmlHashRemoveEntry3 (xmlHashTable *hash,
  162. const xmlChar *name,
  163. const xmlChar *name2,
  164. const xmlChar *name3,
  165. xmlHashDeallocator dealloc);
  166. /*
  167. * Retrieve the payload.
  168. */
  169. XMLPUBFUN void *
  170. xmlHashLookup (xmlHashTable *hash,
  171. const xmlChar *name);
  172. XMLPUBFUN void *
  173. xmlHashLookup2 (xmlHashTable *hash,
  174. const xmlChar *name,
  175. const xmlChar *name2);
  176. XMLPUBFUN void *
  177. xmlHashLookup3 (xmlHashTable *hash,
  178. const xmlChar *name,
  179. const xmlChar *name2,
  180. const xmlChar *name3);
  181. XMLPUBFUN void *
  182. xmlHashQLookup (xmlHashTable *hash,
  183. const xmlChar *prefix,
  184. const xmlChar *name);
  185. XMLPUBFUN void *
  186. xmlHashQLookup2 (xmlHashTable *hash,
  187. const xmlChar *prefix,
  188. const xmlChar *name,
  189. const xmlChar *prefix2,
  190. const xmlChar *name2);
  191. XMLPUBFUN void *
  192. xmlHashQLookup3 (xmlHashTable *hash,
  193. const xmlChar *prefix,
  194. const xmlChar *name,
  195. const xmlChar *prefix2,
  196. const xmlChar *name2,
  197. const xmlChar *prefix3,
  198. const xmlChar *name3);
  199. /*
  200. * Helpers.
  201. */
  202. XMLPUBFUN xmlHashTable *
  203. xmlHashCopySafe (xmlHashTable *hash,
  204. xmlHashCopier copy,
  205. xmlHashDeallocator dealloc);
  206. XMLPUBFUN xmlHashTable *
  207. xmlHashCopy (xmlHashTable *hash,
  208. xmlHashCopier copy);
  209. XMLPUBFUN int
  210. xmlHashSize (xmlHashTable *hash);
  211. XMLPUBFUN void
  212. xmlHashScan (xmlHashTable *hash,
  213. xmlHashScanner scan,
  214. void *data);
  215. XMLPUBFUN void
  216. xmlHashScan3 (xmlHashTable *hash,
  217. const xmlChar *name,
  218. const xmlChar *name2,
  219. const xmlChar *name3,
  220. xmlHashScanner scan,
  221. void *data);
  222. XMLPUBFUN void
  223. xmlHashScanFull (xmlHashTable *hash,
  224. xmlHashScannerFull scan,
  225. void *data);
  226. XMLPUBFUN void
  227. xmlHashScanFull3 (xmlHashTable *hash,
  228. const xmlChar *name,
  229. const xmlChar *name2,
  230. const xmlChar *name3,
  231. xmlHashScannerFull scan,
  232. void *data);
  233. #ifdef __cplusplus
  234. }
  235. #endif
  236. #endif /* ! __XML_HASH_H__ */