You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

451 lines
20 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: hashtable.h,v 1.1 2004/10/29 09:28:25 snowdrop Exp $
  3. *
  4. * CSOAP Project: A http client/server library in C
  5. * Copyright (C) 2003-2004 Ferhat Ayaz
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. *
  22. * Email: ferhatayaz@yahoo.com
  23. ******************************************************************/
  24. #ifndef UTILS_HASHTABLE_H
  25. #define UTILS_HASHTABLE_H
  26. /* These structs should not be accessed directly from user code.
  27. * All access should be via the public functions declared below. */
  28. typedef struct _hashpair {
  29. const void *key;
  30. void *value;
  31. struct _hashpair *next;
  32. } hashpair_t;
  33. typedef struct {
  34. long numOfBuckets;
  35. long numOfElements;
  36. hashpair_t **bucketArray;
  37. float idealRatio, lowerRehashThreshold, upperRehashThreshold;
  38. int (*keycmp)(const void *key1, const void *key2);
  39. int (*valuecmp)(const void *value1, const void *value2);
  40. unsigned long (*hashFunction)(const void *key);
  41. void (*keyDeallocator)(void *key);
  42. void (*valueDeallocator)(void *value);
  43. } hashtable_t;
  44. /*--------------------------------------------------------------------------*\
  45. * NAME:
  46. * hashtable_create() - creates a new HashTable
  47. * DESCRIPTION:
  48. * Creates a new HashTable. When finished with this HashTable, it
  49. * should be explicitly destroyed by calling the hashtable_destroy()
  50. * function.
  51. * EFFICIENCY:
  52. * O(1)
  53. * ARGUMENTS:
  54. * numOfBuckets - the number of buckets to start the HashTable out with.
  55. * Must be greater than zero, and should be prime.
  56. * Ideally, the number of buckets should between 1/5
  57. * and 1 times the expected number of elements in the
  58. * HashTable. Values much more or less than this will
  59. * result in wasted memory or decreased performance
  60. * respectively. The number of buckets in a HashTable
  61. * can be re-calculated to an appropriate number by
  62. * calling the hashtable_rehash() function once the
  63. * HashTable has been populated. The number of buckets
  64. * in a HashTable may also be re-calculated
  65. * automatically if the ratio of elements to buckets
  66. * passes the thresholds set by hashtable_set_ideal_ration().
  67. * RETURNS:
  68. * HashTable - a new Hashtable, or NULL on error
  69. \*--------------------------------------------------------------------------*/
  70. hashtable_t *hashtable_create(long numOfBuckets);
  71. /*--------------------------------------------------------------------------*\
  72. * NAME:
  73. * hashtable_destroy() - destroys an existing HashTable
  74. * DESCRIPTION:
  75. * Destroys an existing HashTable.
  76. * EFFICIENCY:
  77. * O(n)
  78. * ARGUMENTS:
  79. * hashTable - the HashTable to destroy
  80. * RETURNS:
  81. * <nothing>
  82. \*--------------------------------------------------------------------------*/
  83. void hashtable_destroy(hashtable_t *hashTable);
  84. /*--------------------------------------------------------------------------*\
  85. * NAME:
  86. * hashtable_contains_key() - checks the existence of a key in a HashTable
  87. * DESCRIPTION:
  88. * Determines whether or not the specified HashTable contains the
  89. * specified key. Uses the comparison function specified by
  90. * hashtable_set_key_comparision_function().
  91. * EFFICIENCY:
  92. * O(1), assuming a good hash function and element-to-bucket ratio
  93. * ARGUMENTS:
  94. * hashTable - the HashTable to search
  95. * key - the key to search for
  96. * RETURNS:
  97. * bool - whether or not the specified HashTable contains the
  98. * specified key.
  99. \*--------------------------------------------------------------------------*/
  100. int hashtable_contains_key(const hashtable_t *hashTable, const void *key);
  101. /*--------------------------------------------------------------------------*\
  102. * NAME:
  103. * hashtable_contains_value()
  104. * - checks the existence of a value in a HashTable
  105. * DESCRIPTION:
  106. * Determines whether or not the specified HashTable contains the
  107. * specified value. Unlike hashtable_contains_key(), this function is
  108. * not very efficient, since it has to scan linearly looking for a
  109. * match. Uses the comparison function specified by
  110. * hashtable_set_value_comparision_function().
  111. * EFFICIENCY:
  112. * O(n)
  113. * ARGUMENTS:
  114. * hashTable - the HashTable to search
  115. * value - the value to search for
  116. * RETURNS:
  117. * bool - whether or not the specified HashTable contains the
  118. * specified value.
  119. \*--------------------------------------------------------------------------*/
  120. int hashtable_contains_value(const hashtable_t *hashTable, const void *value);
  121. /*--------------------------------------------------------------------------*\
  122. * NAME:
  123. * hashtable_put() - adds a key/value pair to a HashTable
  124. * DESCRIPTION:
  125. * Adds the specified key/value pair to the specified HashTable. If
  126. * the key already exists in the HashTable (determined by the comparison
  127. * function specified by hashtable_set_key_comparision_function()), its value
  128. * is replaced by the new value. May trigger an auto-rehash (see
  129. * hashtable_set_ideal_ration()). It is illegal to specify NULL as the
  130. * key or value.
  131. * EFFICIENCY:
  132. * O(1), assuming a good hash function and element-to-bucket ratio
  133. * ARGUMENTS:
  134. * hashTable - the HashTable to add to
  135. * key - the key to add or whose value to replace
  136. * value - the value associated with the key
  137. * RETURNS:
  138. * err - 0 if successful, -1 if an error was encountered
  139. \*--------------------------------------------------------------------------*/
  140. int hashtable_put(hashtable_t *hashTable, const void *key, void *value);
  141. /*--------------------------------------------------------------------------*\
  142. * NAME:
  143. * hashtable_get() - retrieves the value of a key in a HashTable
  144. * DESCRIPTION:
  145. * Retrieves the value of the specified key in the specified HashTable.
  146. * Uses the comparison function specified by
  147. * hashtable_set_key_comparision_function().
  148. * EFFICIENCY:
  149. * O(1), assuming a good hash function and element-to-bucket ratio
  150. * ARGUMENTS:
  151. * hashTable - the HashTable to search
  152. * key - the key whose value is desired
  153. * RETURNS:
  154. * void * - the value of the specified key, or NULL if the key
  155. * doesn't exist in the HashTable
  156. \*--------------------------------------------------------------------------*/
  157. void *hashtable_get(const hashtable_t *hashTable, const void *key);
  158. /*--------------------------------------------------------------------------*\
  159. * NAME:
  160. * hashtable_remove() - removes a key/value pair from a HashTable
  161. * DESCRIPTION:
  162. * Removes the key/value pair identified by the specified key from the
  163. * specified HashTable if the key exists in the HashTable. May trigger
  164. * an auto-rehash (see hashtable_set_ideal_ration()).
  165. * EFFICIENCY:
  166. * O(1), assuming a good hash function and element-to-bucket ratio
  167. * ARGUMENTS:
  168. * hashTable - the HashTable to remove the key/value pair from
  169. * key - the key specifying the key/value pair to be removed
  170. * RETURNS:
  171. * <nothing>
  172. \*--------------------------------------------------------------------------*/
  173. void hashtable_remove(hashtable_t *hashTable, const void *key);
  174. /*--------------------------------------------------------------------------*\
  175. * NAME:
  176. * hashtable_remove_all() - removes all key/value pairs from a HashTable
  177. * DESCRIPTION:
  178. * Removes all key/value pairs from the specified HashTable. May trigger
  179. * an auto-rehash (see hashtable_set_ideal_ration()).
  180. * EFFICIENCY:
  181. * O(n)
  182. * ARGUMENTS:
  183. * hashTable - the HashTable to remove all key/value pairs from
  184. * RETURNS:
  185. * <nothing>
  186. \*--------------------------------------------------------------------------*/
  187. void hashtable_remove_all(hashtable_t *hashTable);
  188. /*--------------------------------------------------------------------------*\
  189. * NAME:
  190. * hashtable_is_empty() - determines if a HashTable is empty
  191. * DESCRIPTION:
  192. * Determines whether or not the specified HashTable contains any
  193. * key/value pairs.
  194. * EFFICIENCY:
  195. * O(1)
  196. * ARGUMENTS:
  197. * hashTable - the HashTable to check
  198. * RETURNS:
  199. * bool - whether or not the specified HashTable contains any
  200. * key/value pairs
  201. \*--------------------------------------------------------------------------*/
  202. int hashtable_is_empty(const hashtable_t *hashTable);
  203. /*--------------------------------------------------------------------------*\
  204. * NAME:
  205. * hashtable_size() - returns the number of elements in a HashTable
  206. * DESCRIPTION:
  207. * Returns the number of key/value pairs that are present in the
  208. * specified HashTable.
  209. * EFFICIENCY:
  210. * O(1)
  211. * ARGUMENTS:
  212. * hashTable - the HashTable whose size is requested
  213. * RETURNS:
  214. * long - the number of key/value pairs that are present in
  215. * the specified HashTable
  216. \*--------------------------------------------------------------------------*/
  217. long hashtable_size(const hashtable_t *hashTable);
  218. /*--------------------------------------------------------------------------*\
  219. * NAME:
  220. * hashtable_getNumBuckets() - returns the number of buckets in a HashTable
  221. * DESCRIPTION:
  222. * Returns the number of buckets that are in the specified HashTable.
  223. * This may change dynamically throughout the life of a HashTable if
  224. * automatic or manual rehashing is performed.
  225. * EFFICIENCY:
  226. * O(1)
  227. * ARGUMENTS:
  228. * hashTable - the HashTable whose number of buckets is requested
  229. * RETURNS:
  230. * long - the number of buckets that are in the specified
  231. * HashTable
  232. \*--------------------------------------------------------------------------*/
  233. long hashtable_get_num_buckets(const hashtable_t *hashTable);
  234. /*--------------------------------------------------------------------------*\
  235. * NAME:
  236. * hashtable_set_key_comparision_function()
  237. * - specifies the function used to compare keys in a HashTable
  238. * DESCRIPTION:
  239. * Specifies the function used to compare keys in the specified
  240. * HashTable. The specified function should return zero if the two
  241. * keys are considered equal, and non-zero otherwise. The default
  242. * function is one that simply compares pointers.
  243. * ARGUMENTS:
  244. * hashTable - the HashTable whose key comparison function is being
  245. * specified
  246. * keycmp - a function which returns zero if the two arguments
  247. * passed to it are considered "equal" keys and non-zero
  248. * otherwise
  249. * RETURNS:
  250. * <nothing>
  251. \*--------------------------------------------------------------------------*/
  252. void hashtable_set_key_comparision_function(hashtable_t *hashTable,
  253. int (*keycmp)(const void *key1, const void *key2));
  254. /*--------------------------------------------------------------------------*\
  255. * NAME:
  256. * hashtable_set_value_comparision_function()
  257. * - specifies the function used to compare values in a HashTable
  258. * DESCRIPTION:
  259. * Specifies the function used to compare values in the specified
  260. * HashTable. The specified function should return zero if the two
  261. * values are considered equal, and non-zero otherwise. The default
  262. * function is one that simply compares pointers.
  263. * ARGUMENTS:
  264. * hashTable - the HashTable whose value comparison function is being
  265. * specified
  266. * valuecmp - a function which returns zero if the two arguments
  267. * passed to it are considered "equal" values and non-zero
  268. * otherwise
  269. * RETURNS:
  270. * <nothing>
  271. \*--------------------------------------------------------------------------*/
  272. void hashtable_set_value_comparision_function(hashtable_t *hashTable,
  273. int (*valuecmp)(const void *value1, const void *value2));
  274. /*--------------------------------------------------------------------------*\
  275. * NAME:
  276. * hashtable_set_hashfunction()
  277. * - specifies the hash function used by a HashTable
  278. * DESCRIPTION:
  279. * Specifies the function used to determine the hash value for a key
  280. * in the specified HashTable (before modulation). An ideal hash
  281. * function is one which is easy to compute and approximates a
  282. * "random" function. The default function is one that works
  283. * relatively well for pointers. If the HashTable keys are to be
  284. * strings (which is probably the case), then this default function
  285. * will not suffice, in which case consider using the provided
  286. * hashtable_string_hash_function() function.
  287. * ARGUMENTS:
  288. * hashTable - the HashTable whose hash function is being specified
  289. * hashFunction - a function which returns an appropriate hash code
  290. * for a given key
  291. * RETURNS:
  292. * <nothing>
  293. \*--------------------------------------------------------------------------*/
  294. void hashtable_set_hashfunction(hashtable_t *hashTable,
  295. unsigned long (*hashFunction)(const void *key));
  296. /*--------------------------------------------------------------------------*\
  297. * NAME:
  298. * hashtable_rehash() - reorganizes a HashTable to be more efficient
  299. * DESCRIPTION:
  300. * Reorganizes a HashTable to be more efficient. If a number of
  301. * buckets is specified, the HashTable is rehashed to that number of
  302. * buckets. If 0 is specified, the HashTable is rehashed to a number
  303. * of buckets which is automatically calculated to be a prime number
  304. * that achieves (as closely as possible) the ideal element-to-bucket
  305. * ratio specified by the hashtable_set_ideal_ration() function.
  306. * EFFICIENCY:
  307. * O(n)
  308. * ARGUMENTS:
  309. * hashTable - the HashTable to be reorganized
  310. * numOfBuckets - the number of buckets to rehash the HashTable to.
  311. * Should be prime. Ideally, the number of buckets
  312. * should be between 1/5 and 1 times the expected
  313. * number of elements in the HashTable. Values much
  314. * more or less than this will result in wasted memory
  315. * or decreased performance respectively. If 0 is
  316. * specified, an appropriate number of buckets is
  317. * automatically calculated.
  318. * RETURNS:
  319. * <nothing>
  320. \*--------------------------------------------------------------------------*/
  321. void hashtable_rehash(hashtable_t *hashTable, long numOfBuckets);
  322. /*--------------------------------------------------------------------------*\
  323. * NAME:
  324. * hashtable_set_ideal_ration()
  325. * - sets the ideal element-to-bucket ratio of a HashTable
  326. * DESCRIPTION:
  327. * Sets the ideal element-to-bucket ratio, as well as the lower and
  328. * upper auto-rehash thresholds, of the specified HashTable. Note
  329. * that this function doesn't actually perform a rehash.
  330. *
  331. * The default values for these properties are 3.0, 0.0 and 15.0
  332. * respectively. This is likely fine for most situations, so there
  333. * is probably no need to call this function.
  334. * ARGUMENTS:
  335. * hashTable - a HashTable
  336. * idealRatio - the ideal element-to-bucket ratio. When a rehash
  337. * occurs (either manually via a call to the
  338. * hashtable_rehash() function or automatically due the
  339. * the triggering of one of the thresholds below), the
  340. * number of buckets in the HashTable will be
  341. * recalculated to be a prime number that achieves (as
  342. * closely as possible) this ideal ratio. Must be a
  343. * positive number.
  344. * lowerRehashThreshold
  345. * - the element-to-bucket ratio that is considered
  346. * unacceptably low (i.e., too few elements per bucket).
  347. * If the actual ratio falls below this number, a
  348. * rehash will automatically be performed. Must be
  349. * lower than the value of idealRatio. If no ratio
  350. * is considered unacceptably low, a value of 0.0 can
  351. * be specified.
  352. * upperRehashThreshold
  353. * - the element-to-bucket ratio that is considered
  354. * unacceptably high (i.e., too many elements per bucket).
  355. * If the actual ratio rises above this number, a
  356. * rehash will automatically be performed. Must be
  357. * higher than idealRatio. However, if no ratio
  358. * is considered unacceptably high, a value of 0.0 can
  359. * be specified.
  360. * RETURNS:
  361. * <nothing>
  362. \*--------------------------------------------------------------------------*/
  363. void hashtable_set_ideal_ration(hashtable_t *hashTable, float idealRatio,
  364. float lowerRehashThreshold,
  365. float upperRehashThreshold);
  366. /*--------------------------------------------------------------------------*\
  367. * NAME:
  368. * hashtable_set_deallocation_function()
  369. * - sets the key and value deallocation functions of a HashTable
  370. * DESCRIPTION:
  371. * Sets the key and value deallocation functions of the specified
  372. * HashTable. This determines what happens to a key or a value when it
  373. * is removed from the HashTable. If the deallocation function is NULL
  374. * (the default if this function is never called), its reference is
  375. * simply dropped and it is up to the calling program to perform the
  376. * proper memory management. If the deallocation function is non-NULL,
  377. * it is called to free the memory used by the object. E.g., for simple
  378. * objects, an appropriate deallocation function may be free().
  379. *
  380. * This affects the behaviour of the hashtable_destroy(), hashtable_put(),
  381. * hashtable_remove() and hashtable_remove_all() functions.
  382. * ARGUMENTS:
  383. * hashTable - a HashTable
  384. * keyDeallocator
  385. * - if non-NULL, the function to be called when a key is
  386. * removed from the HashTable.
  387. * valueDeallocator
  388. * - if non-NULL, the function to be called when a value is
  389. * removed from the HashTable.
  390. * RETURNS:
  391. * <nothing>
  392. \*--------------------------------------------------------------------------*/
  393. void hashtable_set_deallocation_function(hashtable_t *hashTable,
  394. void (*keyDeallocator)(void *key),
  395. void (*valueDeallocator)(void *value));
  396. /*--------------------------------------------------------------------------*\
  397. * NAME:
  398. * hashtable_string_hash_function() - a good hash function for strings
  399. * DESCRIPTION:
  400. * A hash function that is appropriate for hashing strings. Note that
  401. * this is not the default hash function. To make it the default hash
  402. * function, call hashtable_set_hashfunction(hashtable_string_hash_function).
  403. * ARGUMENTS:
  404. * key - the key to be hashed
  405. * RETURNS:
  406. * long - the unmodulated hash value of the key
  407. \*--------------------------------------------------------------------------*/
  408. unsigned long hashtable_string_hash_function(const void *key);
  409. #endif /* UTILS_HASHTABLE_H */