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.

717 lines
28 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: hashtable.c,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. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <assert.h>
  27. #include <utils/hashtable.h>
  28. static int pointercmp(const void *pointer1, const void *pointer2);
  29. static int stringcmp(const void *pointer1, const void *pointer2);
  30. static unsigned long pointerHashFunction(const void *pointer);
  31. static int isProbablePrime(long number);
  32. static long calculateIdealNumOfBuckets(hashtable_t *hashTable);
  33. /*--------------------------------------------------------------------------*\
  34. * NAME:
  35. * hashtable_create() - creates a new HashTable
  36. * DESCRIPTION:
  37. * Creates a new HashTable. When finished with this HashTable, it
  38. * should be explicitly destroyed by calling the hashtable_destroy()
  39. * function.
  40. * EFFICIENCY:
  41. * O(1)
  42. * ARGUMENTS:
  43. * numOfBuckets - the number of buckets to start the HashTable out with.
  44. * Must be greater than zero, and should be prime.
  45. * Ideally, the number of buckets should between 1/5
  46. * and 1 times the expected number of elements in the
  47. * HashTable. Values much more or less than this will
  48. * result in wasted memory or decreased performance
  49. * respectively. The number of buckets in a HashTable
  50. * can be re-calculated to an appropriate number by
  51. * calling the hashtable_rehash() function once the
  52. * HashTable has been populated. The number of buckets
  53. * in a HashTable may also be re-calculated
  54. * automatically if the ratio of elements to buckets
  55. * passes the thresholds set by hashtable_set_ideal_ration().
  56. * RETURNS:
  57. * HashTable - a new Hashtable, or NULL on error
  58. \*--------------------------------------------------------------------------*/
  59. hashtable_t *hashtable_create(long numOfBuckets) {
  60. hashtable_t *hashTable;
  61. int i;
  62. assert(numOfBuckets > 0);
  63. hashTable = (hashtable_t *) malloc(sizeof(hashtable_t));
  64. if (hashTable == NULL)
  65. return NULL;
  66. hashTable->bucketArray = (hashpair_t **)
  67. malloc(numOfBuckets * sizeof(hashpair_t *));
  68. if (hashTable->bucketArray == NULL) {
  69. free(hashTable);
  70. return NULL;
  71. }
  72. hashTable->numOfBuckets = numOfBuckets;
  73. hashTable->numOfElements = 0;
  74. for (i=0; i<numOfBuckets; i++)
  75. hashTable->bucketArray[i] = NULL;
  76. hashTable->idealRatio = 3.0;
  77. hashTable->lowerRehashThreshold = 0.0;
  78. hashTable->upperRehashThreshold = 15.0;
  79. hashTable->keycmp = stringcmp; /*pointercmp;*/
  80. hashTable->valuecmp = pointercmp;
  81. hashTable->hashFunction = hashtable_string_hash_function; /*pointerHashFunction;*/
  82. hashTable->keyDeallocator = NULL;
  83. hashTable->valueDeallocator = NULL;
  84. return hashTable;
  85. }
  86. /*--------------------------------------------------------------------------*\
  87. * NAME:
  88. * hashtable_destroy() - destroys an existing HashTable
  89. * DESCRIPTION:
  90. * Destroys an existing HashTable.
  91. * EFFICIENCY:
  92. * O(n)
  93. * ARGUMENTS:
  94. * hashTable - the HashTable to destroy
  95. * RETURNS:
  96. * <nothing>
  97. \*--------------------------------------------------------------------------*/
  98. void hashtable_destroy(hashtable_t *hashTable) {
  99. int i;
  100. hashpair_t *pair, *nextPair;
  101. for (i=0; i<hashTable->numOfBuckets; i++) {
  102. pair = hashTable->bucketArray[i];
  103. while (pair != NULL) {
  104. nextPair = pair->next;
  105. if (hashTable->keyDeallocator != NULL)
  106. hashTable->keyDeallocator((void *) pair->key);
  107. if (hashTable->valueDeallocator != NULL)
  108. hashTable->valueDeallocator(pair->value);
  109. free(pair);
  110. pair = nextPair;
  111. }
  112. }
  113. free(hashTable->bucketArray);
  114. free(hashTable);
  115. }
  116. /*--------------------------------------------------------------------------*\
  117. * NAME:
  118. * hashtable_contains_key() - checks the existence of a key in a HashTable
  119. * DESCRIPTION:
  120. * Determines whether or not the specified HashTable contains the
  121. * specified key. Uses the comparison function specified by
  122. * hashtable_set_key_comparision_function().
  123. * EFFICIENCY:
  124. * O(1), assuming a good hash function and element-to-bucket ratio
  125. * ARGUMENTS:
  126. * hashTable - the HashTable to search
  127. * key - the key to search for
  128. * RETURNS:
  129. * bool - whether or not the specified HashTable contains the
  130. * specified key.
  131. \*--------------------------------------------------------------------------*/
  132. int hashtable_contains_key(const hashtable_t *hashTable, const void *key) {
  133. return (hashtable_get(hashTable, key) != NULL);
  134. }
  135. /*--------------------------------------------------------------------------*\
  136. * NAME:
  137. * hashtable_contains_value()
  138. * - checks the existence of a value in a HashTable
  139. * DESCRIPTION:
  140. * Determines whether or not the specified HashTable contains the
  141. * specified value. Unlike hashtable_contains_key(), this function is
  142. * not very efficient, since it has to scan linearly looking for a
  143. * match. Uses the comparison function specified by
  144. * hashtable_set_value_comparision_function().
  145. * EFFICIENCY:
  146. * O(n)
  147. * ARGUMENTS:
  148. * hashTable - the HashTable to search
  149. * value - the value to search for
  150. * RETURNS:
  151. * bool - whether or not the specified HashTable contains the
  152. * specified value.
  153. \*--------------------------------------------------------------------------*/
  154. int hashtable_contains_value(const hashtable_t *hashTable, const void *value) {
  155. int i;
  156. hashpair_t *pair;
  157. for (i=0; i<hashTable->numOfBuckets; i++) {
  158. pair = hashTable->bucketArray[i];
  159. while (pair != NULL) {
  160. if (hashTable->valuecmp(value, pair->value) == 0)
  161. return 1;
  162. pair = pair->next;
  163. }
  164. }
  165. return 0;
  166. }
  167. /*--------------------------------------------------------------------------*\
  168. * NAME:
  169. * hashtable_put() - adds a key/value pair to a HashTable
  170. * DESCRIPTION:
  171. * Adds the specified key/value pair to the specified HashTable. If
  172. * the key already exists in the HashTable (determined by the comparison
  173. * function specified by hashtable_set_key_comparision_function()), its value
  174. * is replaced by the new value. May trigger an auto-rehash (see
  175. * hashtable_set_ideal_ration()). It is illegal to specify NULL as the
  176. * key or value.
  177. * EFFICIENCY:
  178. * O(1), assuming a good hash function and element-to-bucket ratio
  179. * ARGUMENTS:
  180. * hashTable - the HashTable to add to
  181. * key - the key to add or whose value to replace
  182. * value - the value associated with the key
  183. * RETURNS:
  184. * err - 0 if successful, -1 if an error was encountered
  185. \*--------------------------------------------------------------------------*/
  186. int hashtable_put(hashtable_t *hashTable, const void *key, void *value) {
  187. long hashValue;
  188. hashpair_t *pair;
  189. assert(key != NULL);
  190. assert(value != NULL);
  191. hashValue = hashTable->hashFunction(key) % hashTable->numOfBuckets;
  192. pair = hashTable->bucketArray[hashValue];
  193. while (pair != NULL && hashTable->keycmp(key, pair->key) != 0)
  194. pair = pair->next;
  195. if (pair) {
  196. if (pair->key != key) {
  197. if (hashTable->keyDeallocator != NULL)
  198. hashTable->keyDeallocator((void *) pair->key);
  199. pair->key = key;
  200. }
  201. if (pair->value != value) {
  202. if (hashTable->valueDeallocator != NULL)
  203. hashTable->valueDeallocator(pair->value);
  204. pair->value = value;
  205. }
  206. }
  207. else {
  208. hashpair_t *newPair = (hashpair_t *) malloc(sizeof(hashpair_t));
  209. if (newPair == NULL) {
  210. return -1;
  211. }
  212. else {
  213. newPair->key = key;
  214. newPair->value = value;
  215. newPair->next = hashTable->bucketArray[hashValue];
  216. hashTable->bucketArray[hashValue] = newPair;
  217. hashTable->numOfElements++;
  218. if (hashTable->upperRehashThreshold > hashTable->idealRatio) {
  219. float elementToBucketRatio = (float) hashTable->numOfElements /
  220. (float) hashTable->numOfBuckets;
  221. if (elementToBucketRatio > hashTable->upperRehashThreshold)
  222. hashtable_rehash(hashTable, 0);
  223. }
  224. }
  225. }
  226. return 0;
  227. }
  228. /*--------------------------------------------------------------------------*\
  229. * NAME:
  230. * HashTableGet() - retrieves the value of a key in a HashTable
  231. * DESCRIPTION:
  232. * Retrieves the value of the specified key in the specified HashTable.
  233. * Uses the comparison function specified by
  234. * hashtable_set_key_comparision_function().
  235. * EFFICIENCY:
  236. * O(1), assuming a good hash function and element-to-bucket ratio
  237. * ARGUMENTS:
  238. * hashTable - the HashTable to search
  239. * key - the key whose value is desired
  240. * RETURNS:
  241. * void * - the value of the specified key, or NULL if the key
  242. * doesn't exist in the HashTable
  243. \*--------------------------------------------------------------------------*/
  244. void *hashtable_get(const hashtable_t *hashTable, const void *key) {
  245. long hashValue = hashTable->hashFunction(key) % hashTable->numOfBuckets;
  246. hashpair_t *pair = hashTable->bucketArray[hashValue];
  247. while (pair != NULL && hashTable->keycmp(key, pair->key) != 0)
  248. pair = pair->next;
  249. return (pair == NULL)? NULL : pair->value;
  250. }
  251. /*--------------------------------------------------------------------------*\
  252. * NAME:
  253. * hashtable_remove() - removes a key/value pair from a HashTable
  254. * DESCRIPTION:
  255. * Removes the key/value pair identified by the specified key from the
  256. * specified HashTable if the key exists in the HashTable. May trigger
  257. * an auto-rehash (see hashtable_set_ideal_ration()).
  258. * EFFICIENCY:
  259. * O(1), assuming a good hash function and element-to-bucket ratio
  260. * ARGUMENTS:
  261. * hashTable - the HashTable to remove the key/value pair from
  262. * key - the key specifying the key/value pair to be removed
  263. * RETURNS:
  264. * <nothing>
  265. \*--------------------------------------------------------------------------*/
  266. void hashtable_remove(hashtable_t *hashTable, const void *key) {
  267. long hashValue = hashTable->hashFunction(key) % hashTable->numOfBuckets;
  268. hashpair_t *pair = hashTable->bucketArray[hashValue];
  269. hashpair_t *previousPair = NULL;
  270. while (pair != NULL && hashTable->keycmp(key, pair->key) != 0) {
  271. previousPair = pair;
  272. pair = pair->next;
  273. }
  274. if (pair != NULL) {
  275. if (hashTable->keyDeallocator != NULL)
  276. hashTable->keyDeallocator((void *) pair->key);
  277. if (hashTable->valueDeallocator != NULL)
  278. hashTable->valueDeallocator(pair->value);
  279. if (previousPair != NULL)
  280. previousPair->next = pair->next;
  281. else
  282. hashTable->bucketArray[hashValue] = pair->next;
  283. free(pair);
  284. hashTable->numOfElements--;
  285. if (hashTable->lowerRehashThreshold > 0.0) {
  286. float elementToBucketRatio = (float) hashTable->numOfElements /
  287. (float) hashTable->numOfBuckets;
  288. if (elementToBucketRatio < hashTable->lowerRehashThreshold)
  289. hashtable_rehash(hashTable, 0);
  290. }
  291. }
  292. }
  293. /*--------------------------------------------------------------------------*\
  294. * NAME:
  295. * hashtable_remove_all() - removes all key/value pairs from a HashTable
  296. * DESCRIPTION:
  297. * Removes all key/value pairs from the specified HashTable. May trigger
  298. * an auto-rehash (see hashtable_set_ideal_ration()).
  299. * EFFICIENCY:
  300. * O(n)
  301. * ARGUMENTS:
  302. * hashTable - the HashTable to remove all key/value pairs from
  303. * RETURNS:
  304. * <nothing>
  305. \*--------------------------------------------------------------------------*/
  306. void hashtable_remove_all(hashtable_t *hashTable) {
  307. int i;
  308. for (i=0; i<hashTable->numOfBuckets; i++) {
  309. hashpair_t *pair = hashTable->bucketArray[i];
  310. while (pair != NULL) {
  311. hashpair_t *nextPair = pair->next;
  312. if (hashTable->keyDeallocator != NULL)
  313. hashTable->keyDeallocator((void *) pair->key);
  314. if (hashTable->valueDeallocator != NULL)
  315. hashTable->valueDeallocator(pair->value);
  316. free(pair);
  317. pair = nextPair;
  318. }
  319. hashTable->bucketArray[i] = NULL;
  320. }
  321. hashTable->numOfElements = 0;
  322. hashtable_rehash(hashTable, 5);
  323. }
  324. /*--------------------------------------------------------------------------*\
  325. * NAME:
  326. * hashtable_is_empty() - determines if a HashTable is empty
  327. * DESCRIPTION:
  328. * Determines whether or not the specified HashTable contains any
  329. * key/value pairs.
  330. * EFFICIENCY:
  331. * O(1)
  332. * ARGUMENTS:
  333. * hashTable - the HashTable to check
  334. * RETURNS:
  335. * bool - whether or not the specified HashTable contains any
  336. * key/value pairs
  337. \*--------------------------------------------------------------------------*/
  338. int hashtable_is_empty(const hashtable_t *hashTable) {
  339. return (hashTable->numOfElements == 0);
  340. }
  341. /*--------------------------------------------------------------------------*\
  342. * NAME:
  343. * hashtable_size() - returns the number of elements in a HashTable
  344. * DESCRIPTION:
  345. * Returns the number of key/value pairs that are present in the
  346. * specified HashTable.
  347. * EFFICIENCY:
  348. * O(1)
  349. * ARGUMENTS:
  350. * hashTable - the HashTable whose size is requested
  351. * RETURNS:
  352. * long - the number of key/value pairs that are present in
  353. * the specified HashTable
  354. \*--------------------------------------------------------------------------*/
  355. long hashtable_size(const hashtable_t *hashTable) {
  356. return hashTable->numOfElements;
  357. }
  358. /*--------------------------------------------------------------------------*\
  359. * NAME:
  360. * HashTableGetNumBuckets() - returns the number of buckets in a HashTable
  361. * DESCRIPTION:
  362. * Returns the number of buckets that are in the specified HashTable.
  363. * This may change dynamically throughout the life of a HashTable if
  364. * automatic or manual rehashing is performed.
  365. * EFFICIENCY:
  366. * O(1)
  367. * ARGUMENTS:
  368. * hashTable - the HashTable whose number of buckets is requested
  369. * RETURNS:
  370. * long - the number of buckets that are in the specified
  371. * HashTable
  372. \*--------------------------------------------------------------------------*/
  373. long hashtable_get_num_buckets(const hashtable_t *hashTable) {
  374. return hashTable->numOfBuckets;
  375. }
  376. /*--------------------------------------------------------------------------*\
  377. * NAME:
  378. * hashtable_set_key_comparision_function()
  379. * - specifies the function used to compare keys in a HashTable
  380. * DESCRIPTION:
  381. * Specifies the function used to compare keys in the specified
  382. * HashTable. The specified function should return zero if the two
  383. * keys are considered equal, and non-zero otherwise. The default
  384. * function is one that simply compares pointers.
  385. * ARGUMENTS:
  386. * hashTable - the HashTable whose key comparison function is being
  387. * specified
  388. * keycmp - a function which returns zero if the two arguments
  389. * passed to it are considered "equal" keys and non-zero
  390. * otherwise
  391. * RETURNS:
  392. * <nothing>
  393. \*--------------------------------------------------------------------------*/
  394. void hashtable_set_key_comparision_function(hashtable_t *hashTable,
  395. int (*keycmp)(const void *key1, const void *key2)) {
  396. assert(keycmp != NULL);
  397. hashTable->keycmp = keycmp;
  398. }
  399. /*--------------------------------------------------------------------------*\
  400. * NAME:
  401. * hashtable_set_value_comparision_function()
  402. * - specifies the function used to compare values in a HashTable
  403. * DESCRIPTION:
  404. * Specifies the function used to compare values in the specified
  405. * HashTable. The specified function should return zero if the two
  406. * values are considered equal, and non-zero otherwise. The default
  407. * function is one that simply compares pointers.
  408. * ARGUMENTS:
  409. * hashTable - the HashTable whose value comparison function is being
  410. * specified
  411. * valuecmp - a function which returns zero if the two arguments
  412. * passed to it are considered "equal" values and non-zero
  413. * otherwise
  414. * RETURNS:
  415. * <nothing>
  416. \*--------------------------------------------------------------------------*/
  417. void hashtable_set_value_comparision_function(hashtable_t *hashTable,
  418. int (*valuecmp)(const void *value1, const void *value2)) {
  419. assert(valuecmp != NULL);
  420. hashTable->valuecmp = valuecmp;
  421. }
  422. /*--------------------------------------------------------------------------*\
  423. * NAME:
  424. * hashtable_set_hashfunction()
  425. * - specifies the hash function used by a HashTable
  426. * DESCRIPTION:
  427. * Specifies the function used to determine the hash value for a key
  428. * in the specified HashTable (before modulation). An ideal hash
  429. * function is one which is easy to compute and approximates a
  430. * "random" function. The default function is one that works
  431. * relatively well for pointers. If the HashTable keys are to be
  432. * strings (which is probably the case), then this default function
  433. * will not suffice, in which case consider using the provided
  434. * hashtable_string_hash_function() function.
  435. * ARGUMENTS:
  436. * hashTable - the HashTable whose hash function is being specified
  437. * hashFunction - a function which returns an appropriate hash code
  438. * for a given key
  439. * RETURNS:
  440. * <nothing>
  441. \*--------------------------------------------------------------------------*/
  442. void hashtable_set_hashfunction(hashtable_t *hashTable,
  443. unsigned long (*hashFunction)(const void *key))
  444. {
  445. assert(hashFunction != NULL);
  446. hashTable->hashFunction = hashFunction;
  447. }
  448. /*--------------------------------------------------------------------------*\
  449. * NAME:
  450. * hashtable_rehash() - reorganizes a HashTable to be more efficient
  451. * DESCRIPTION:
  452. * Reorganizes a HashTable to be more efficient. If a number of
  453. * buckets is specified, the HashTable is rehashed to that number of
  454. * buckets. If 0 is specified, the HashTable is rehashed to a number
  455. * of buckets which is automatically calculated to be a prime number
  456. * that achieves (as closely as possible) the ideal element-to-bucket
  457. * ratio specified by the hashtable_set_ideal_ration() function.
  458. * EFFICIENCY:
  459. * O(n)
  460. * ARGUMENTS:
  461. * hashTable - the HashTable to be reorganized
  462. * numOfBuckets - the number of buckets to rehash the HashTable to.
  463. * Should be prime. Ideally, the number of buckets
  464. * should be between 1/5 and 1 times the expected
  465. * number of elements in the HashTable. Values much
  466. * more or less than this will result in wasted memory
  467. * or decreased performance respectively. If 0 is
  468. * specified, an appropriate number of buckets is
  469. * automatically calculated.
  470. * RETURNS:
  471. * <nothing>
  472. \*--------------------------------------------------------------------------*/
  473. void hashtable_rehash(hashtable_t *hashTable, long numOfBuckets) {
  474. hashpair_t **newBucketArray;
  475. int i;
  476. assert(numOfBuckets >= 0);
  477. if (numOfBuckets == 0)
  478. numOfBuckets = calculateIdealNumOfBuckets(hashTable);
  479. if (numOfBuckets == hashTable->numOfBuckets)
  480. return; /* already the right size! */
  481. newBucketArray = (hashpair_t **)
  482. malloc(numOfBuckets * sizeof(hashpair_t *));
  483. if (newBucketArray == NULL) {
  484. /* Couldn't allocate memory for the new array. This isn't a fatal
  485. * error; we just can't perform the rehash. */
  486. return;
  487. }
  488. for (i=0; i<numOfBuckets; i++)
  489. newBucketArray[i] = NULL;
  490. for (i=0; i<hashTable->numOfBuckets; i++) {
  491. hashpair_t *pair = hashTable->bucketArray[i];
  492. while (pair != NULL) {
  493. hashpair_t *nextPair = pair->next;
  494. long hashValue = hashTable->hashFunction(pair->key) % numOfBuckets;
  495. pair->next = newBucketArray[hashValue];
  496. newBucketArray[hashValue] = pair;
  497. pair = nextPair;
  498. }
  499. }
  500. free(hashTable->bucketArray);
  501. hashTable->bucketArray = newBucketArray;
  502. hashTable->numOfBuckets = numOfBuckets;
  503. }
  504. /*--------------------------------------------------------------------------*\
  505. * NAME:
  506. * hashtable_set_ideal_ration()
  507. * - sets the ideal element-to-bucket ratio of a HashTable
  508. * DESCRIPTION:
  509. * Sets the ideal element-to-bucket ratio, as well as the lower and
  510. * upper auto-rehash thresholds, of the specified HashTable. Note
  511. * that this function doesn't actually perform a rehash.
  512. *
  513. * The default values for these properties are 3.0, 0.0 and 15.0
  514. * respectively. This is likely fine for most situations, so there
  515. * is probably no need to call this function.
  516. * ARGUMENTS:
  517. * hashTable - a HashTable
  518. * idealRatio - the ideal element-to-bucket ratio. When a rehash
  519. * occurs (either manually via a call to the
  520. * hashtable_rehash() function or automatically due the
  521. * the triggering of one of the thresholds below), the
  522. * number of buckets in the HashTable will be
  523. * recalculated to be a prime number that achieves (as
  524. * closely as possible) this ideal ratio. Must be a
  525. * positive number.
  526. * lowerRehashThreshold
  527. * - the element-to-bucket ratio that is considered
  528. * unacceptably low (i.e., too few elements per bucket).
  529. * If the actual ratio falls below this number, a
  530. * rehash will automatically be performed. Must be
  531. * lower than the value of idealRatio. If no ratio
  532. * is considered unacceptably low, a value of 0.0 can
  533. * be specified.
  534. * upperRehashThreshold
  535. * - the element-to-bucket ratio that is considered
  536. * unacceptably high (i.e., too many elements per bucket).
  537. * If the actual ratio rises above this number, a
  538. * rehash will automatically be performed. Must be
  539. * higher than idealRatio. However, if no ratio
  540. * is considered unacceptably high, a value of 0.0 can
  541. * be specified.
  542. * RETURNS:
  543. * <nothing>
  544. \*--------------------------------------------------------------------------*/
  545. void hashtable_set_ideal_ration(hashtable_t *hashTable, float idealRatio,
  546. float lowerRehashThreshold, float upperRehashThreshold) {
  547. assert(idealRatio > 0.0);
  548. assert(lowerRehashThreshold < idealRatio);
  549. assert(upperRehashThreshold == 0.0 || upperRehashThreshold > idealRatio);
  550. hashTable->idealRatio = idealRatio;
  551. hashTable->lowerRehashThreshold = lowerRehashThreshold;
  552. hashTable->upperRehashThreshold = upperRehashThreshold;
  553. }
  554. /*--------------------------------------------------------------------------*\
  555. * NAME:
  556. * hashtable_set_deallocation_function()
  557. * - sets the key and value deallocation functions of a HashTable
  558. * DESCRIPTION:
  559. * Sets the key and value deallocation functions of the specified
  560. * HashTable. This determines what happens to a key or a value when it
  561. * is removed from the HashTable. If the deallocation function is NULL
  562. * (the default if this function is never called), its reference is
  563. * simply dropped and it is up to the calling program to perform the
  564. * proper memory management. If the deallocation function is non-NULL,
  565. * it is called to free the memory used by the object. E.g., for simple
  566. * objects, an appropriate deallocation function may be free().
  567. *
  568. * This affects the behaviour of the hashtable_destroy(), hashtable_put(),
  569. * hashtable_remove() and hashtable_remove_all() functions.
  570. * ARGUMENTS:
  571. * hashTable - a HashTable
  572. * keyDeallocator
  573. * - if non-NULL, the function to be called when a key is
  574. * removed from the HashTable.
  575. * valueDeallocator
  576. * - if non-NULL, the function to be called when a value is
  577. * removed from the HashTable.
  578. * RETURNS:
  579. * <nothing>
  580. \*--------------------------------------------------------------------------*/
  581. void hashtable_set_deallocation_function(hashtable_t *hashTable,
  582. void (*keyDeallocator)(void *key),
  583. void (*valueDeallocator)(void *value)) {
  584. hashTable->keyDeallocator = keyDeallocator;
  585. hashTable->valueDeallocator = valueDeallocator;
  586. }
  587. /*--------------------------------------------------------------------------*\
  588. * NAME:
  589. * hashtable_string_hash_function() - a good hash function for strings
  590. * DESCRIPTION:
  591. * A hash function that is appropriate for hashing strings. Note that
  592. * this is not the default hash function. To make it the default hash
  593. * function, call hashtable_set_hashfunction(hashTable,
  594. * hashtable_string_hash_function).
  595. * ARGUMENTS:
  596. * key - the key to be hashed
  597. * RETURNS:
  598. * unsigned long - the unmodulated hash value of the key
  599. \*--------------------------------------------------------------------------*/
  600. unsigned long hashtable_string_hash_function(const void *key) {
  601. const unsigned char *str = (const unsigned char *) key;
  602. unsigned long hashValue = 0;
  603. int i;
  604. for (i=0; str[i] != '\0'; i++)
  605. hashValue = hashValue * 37 + str[i];
  606. return hashValue;
  607. }
  608. static int stringcmp(const void *pointer1, const void *pointer2) {
  609. return (strcmp((const char*)pointer1, (const char*)pointer2));
  610. }
  611. static int pointercmp(const void *pointer1, const void *pointer2) {
  612. return (pointer1 != pointer2);
  613. }
  614. static unsigned long pointerHashFunction(const void *pointer) {
  615. return ((unsigned long) pointer) >> 4;
  616. }
  617. static int isProbablePrime(long oddNumber) {
  618. long i;
  619. for (i=3; i<51; i+=2)
  620. if (oddNumber == i)
  621. return 1;
  622. else if (oddNumber%i == 0)
  623. return 0;
  624. return 1; /* maybe */
  625. }
  626. static long calculateIdealNumOfBuckets(hashtable_t *hashTable) {
  627. long idealNumOfBuckets = hashTable->numOfElements / hashTable->idealRatio;
  628. if (idealNumOfBuckets < 5)
  629. idealNumOfBuckets = 5;
  630. else
  631. idealNumOfBuckets |= 0x01; /* make it an odd number */
  632. while (!isProbablePrime(idealNumOfBuckets))
  633. idealNumOfBuckets += 2;
  634. return idealNumOfBuckets;
  635. }