HashTable.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /**
  2. * WinPR: Windows Portable Runtime
  3. * System.Collections.Hashtable
  4. *
  5. * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include <winpr/crt.h>
  23. #include <winpr/collections.h>
  24. /**
  25. * This implementation is based on the public domain
  26. * hash table implementation made by Keith Pomakis:
  27. *
  28. * http://www.pomakis.com/hashtable/hashtable.c
  29. * http://www.pomakis.com/hashtable/hashtable.h
  30. */
  31. BOOL HashTable_PointerCompare(void* pointer1, void* pointer2)
  32. {
  33. return (pointer1 == pointer2);
  34. }
  35. UINT32 HashTable_PointerHash(void* pointer)
  36. {
  37. return ((UINT32)(UINT_PTR)pointer) >> 4;
  38. }
  39. BOOL HashTable_StringCompare(void* string1, void* string2)
  40. {
  41. if (!string1 || !string2)
  42. return (string1 == string2);
  43. return (strcmp((char*)string1, (char*)string2) == 0);
  44. }
  45. UINT32 HashTable_StringHash(void* key)
  46. {
  47. UINT32 c;
  48. UINT32 hash = 5381;
  49. BYTE* str = (BYTE*)key;
  50. /* djb2 algorithm */
  51. while ((c = *str++) != '\0')
  52. hash = (hash * 33) + c;
  53. return hash;
  54. }
  55. void* HashTable_StringClone(void* str)
  56. {
  57. return _strdup((char*)str);
  58. }
  59. void HashTable_StringFree(void* str)
  60. {
  61. free(str);
  62. }
  63. static int HashTable_IsProbablePrime(int oddNumber)
  64. {
  65. int i;
  66. for (i = 3; i < 51; i += 2)
  67. {
  68. if (oddNumber == i)
  69. return 1;
  70. else if (oddNumber % i == 0)
  71. return 0;
  72. }
  73. return 1; /* maybe */
  74. }
  75. static long HashTable_CalculateIdealNumOfBuckets(wHashTable* table)
  76. {
  77. int idealNumOfBuckets = table->numOfElements / ((int)table->idealRatio);
  78. if (idealNumOfBuckets < 5)
  79. idealNumOfBuckets = 5;
  80. else
  81. idealNumOfBuckets |= 0x01;
  82. while (!HashTable_IsProbablePrime(idealNumOfBuckets))
  83. idealNumOfBuckets += 2;
  84. return idealNumOfBuckets;
  85. }
  86. static void HashTable_Rehash(wHashTable* table, int numOfBuckets)
  87. {
  88. int index;
  89. UINT32 hashValue;
  90. wKeyValuePair* pair;
  91. wKeyValuePair* nextPair;
  92. wKeyValuePair** newBucketArray;
  93. if (numOfBuckets == 0)
  94. numOfBuckets = HashTable_CalculateIdealNumOfBuckets(table);
  95. if (numOfBuckets == table->numOfBuckets)
  96. return; /* already the right size! */
  97. newBucketArray = (wKeyValuePair**)calloc(numOfBuckets, sizeof(wKeyValuePair*));
  98. if (!newBucketArray)
  99. {
  100. /*
  101. * Couldn't allocate memory for the new array.
  102. * This isn't a fatal error; we just can't perform the rehash.
  103. */
  104. return;
  105. }
  106. for (index = 0; index < table->numOfBuckets; index++)
  107. {
  108. pair = table->bucketArray[index];
  109. while (pair)
  110. {
  111. nextPair = pair->next;
  112. hashValue = table->hash(pair->key) % numOfBuckets;
  113. pair->next = newBucketArray[hashValue];
  114. newBucketArray[hashValue] = pair;
  115. pair = nextPair;
  116. }
  117. }
  118. free(table->bucketArray);
  119. table->bucketArray = newBucketArray;
  120. table->numOfBuckets = numOfBuckets;
  121. }
  122. static void HashTable_SetIdealRatio(wHashTable* table, float idealRatio, float lowerRehashThreshold,
  123. float upperRehashThreshold)
  124. {
  125. table->idealRatio = idealRatio;
  126. table->lowerRehashThreshold = lowerRehashThreshold;
  127. table->upperRehashThreshold = upperRehashThreshold;
  128. }
  129. wKeyValuePair* HashTable_Get(wHashTable* table, void* key)
  130. {
  131. UINT32 hashValue;
  132. wKeyValuePair* pair;
  133. hashValue = table->hash(key) % table->numOfBuckets;
  134. pair = table->bucketArray[hashValue];
  135. while (pair && !table->keyCompare(key, pair->key))
  136. pair = pair->next;
  137. return pair;
  138. }
  139. /**
  140. * C equivalent of the C# Hashtable Class:
  141. * http://msdn.microsoft.com/en-us/library/system.collections.hashtable.aspx
  142. */
  143. /**
  144. * Properties
  145. */
  146. /**
  147. * Gets the number of key/value pairs contained in the HashTable.
  148. */
  149. int HashTable_Count(wHashTable* table)
  150. {
  151. return table->numOfElements;
  152. }
  153. /**
  154. * Methods
  155. */
  156. /**
  157. * Adds an element with the specified key and value into the HashTable.
  158. */
  159. int HashTable_Add(wHashTable* table, void* key, void* value)
  160. {
  161. int status = 0;
  162. UINT32 hashValue;
  163. wKeyValuePair* pair;
  164. wKeyValuePair* newPair;
  165. if (!key || !value)
  166. return -1;
  167. if (table->keyClone)
  168. {
  169. key = table->keyClone(key);
  170. if (!key)
  171. return -1;
  172. }
  173. if (table->valueClone)
  174. {
  175. value = table->valueClone(value);
  176. if (!value)
  177. return -1;
  178. }
  179. if (table->synchronized)
  180. EnterCriticalSection(&table->lock);
  181. hashValue = table->hash(key) % table->numOfBuckets;
  182. pair = table->bucketArray[hashValue];
  183. while (pair && !table->keyCompare(key, pair->key))
  184. pair = pair->next;
  185. if (pair)
  186. {
  187. if (pair->key != key)
  188. {
  189. if (table->keyFree)
  190. table->keyFree(pair->key);
  191. pair->key = key;
  192. }
  193. if (pair->value != value)
  194. {
  195. if (table->valueFree)
  196. table->valueFree(pair->value);
  197. pair->value = value;
  198. }
  199. }
  200. else
  201. {
  202. newPair = (wKeyValuePair*)malloc(sizeof(wKeyValuePair));
  203. if (!newPair)
  204. {
  205. status = -1;
  206. }
  207. else
  208. {
  209. newPair->key = key;
  210. newPair->value = value;
  211. newPair->next = table->bucketArray[hashValue];
  212. table->bucketArray[hashValue] = newPair;
  213. table->numOfElements++;
  214. if (table->upperRehashThreshold > table->idealRatio)
  215. {
  216. float elementToBucketRatio =
  217. (float)table->numOfElements / (float)table->numOfBuckets;
  218. if (elementToBucketRatio > table->upperRehashThreshold)
  219. HashTable_Rehash(table, 0);
  220. }
  221. }
  222. }
  223. if (table->synchronized)
  224. LeaveCriticalSection(&table->lock);
  225. return status;
  226. }
  227. /**
  228. * Removes the element with the specified key from the HashTable.
  229. */
  230. BOOL HashTable_Remove(wHashTable* table, void* key)
  231. {
  232. UINT32 hashValue;
  233. BOOL status = TRUE;
  234. wKeyValuePair* pair = NULL;
  235. wKeyValuePair* previousPair = NULL;
  236. if (table->synchronized)
  237. EnterCriticalSection(&table->lock);
  238. hashValue = table->hash(key) % table->numOfBuckets;
  239. pair = table->bucketArray[hashValue];
  240. while (pair && !table->keyCompare(key, pair->key))
  241. {
  242. previousPair = pair;
  243. pair = pair->next;
  244. }
  245. if (!pair)
  246. {
  247. status = FALSE;
  248. }
  249. else
  250. {
  251. if (table->keyFree)
  252. table->keyFree(pair->key);
  253. if (table->valueFree)
  254. table->valueFree(pair->value);
  255. if (previousPair)
  256. previousPair->next = pair->next;
  257. else
  258. table->bucketArray[hashValue] = pair->next;
  259. free(pair);
  260. table->numOfElements--;
  261. if (table->lowerRehashThreshold > 0.0)
  262. {
  263. float elementToBucketRatio = (float)table->numOfElements / (float)table->numOfBuckets;
  264. if (elementToBucketRatio < table->lowerRehashThreshold)
  265. HashTable_Rehash(table, 0);
  266. }
  267. }
  268. if (table->synchronized)
  269. LeaveCriticalSection(&table->lock);
  270. return status;
  271. }
  272. /**
  273. * Get an item value using key
  274. */
  275. void* HashTable_GetItemValue(wHashTable* table, void* key)
  276. {
  277. void* value = NULL;
  278. wKeyValuePair* pair;
  279. if (table->synchronized)
  280. EnterCriticalSection(&table->lock);
  281. pair = HashTable_Get(table, key);
  282. if (pair)
  283. value = pair->value;
  284. if (table->synchronized)
  285. LeaveCriticalSection(&table->lock);
  286. return value;
  287. }
  288. /**
  289. * Set an item value using key
  290. */
  291. BOOL HashTable_SetItemValue(wHashTable* table, void* key, void* value)
  292. {
  293. BOOL status = TRUE;
  294. wKeyValuePair* pair;
  295. if (table->valueClone && value)
  296. {
  297. value = table->valueClone(value);
  298. if (!value)
  299. return FALSE;
  300. }
  301. if (table->synchronized)
  302. EnterCriticalSection(&table->lock);
  303. pair = HashTable_Get(table, key);
  304. if (!pair)
  305. status = FALSE;
  306. else
  307. {
  308. if (table->valueClone && table->valueFree)
  309. table->valueFree(pair->value);
  310. pair->value = value;
  311. }
  312. if (table->synchronized)
  313. LeaveCriticalSection(&table->lock);
  314. return status;
  315. }
  316. /**
  317. * Removes all elements from the HashTable.
  318. */
  319. void HashTable_Clear(wHashTable* table)
  320. {
  321. int index;
  322. wKeyValuePair* pair;
  323. wKeyValuePair* nextPair;
  324. if (table->synchronized)
  325. EnterCriticalSection(&table->lock);
  326. for (index = 0; index < table->numOfBuckets; index++)
  327. {
  328. pair = table->bucketArray[index];
  329. while (pair)
  330. {
  331. nextPair = pair->next;
  332. if (table->keyFree)
  333. table->keyFree(pair->key);
  334. if (table->valueFree)
  335. table->valueFree(pair->value);
  336. free(pair);
  337. pair = nextPair;
  338. }
  339. table->bucketArray[index] = NULL;
  340. }
  341. table->numOfElements = 0;
  342. HashTable_Rehash(table, 5);
  343. if (table->synchronized)
  344. LeaveCriticalSection(&table->lock);
  345. }
  346. /**
  347. * Gets the list of keys as an array
  348. */
  349. int HashTable_GetKeys(wHashTable* table, ULONG_PTR** ppKeys)
  350. {
  351. int iKey;
  352. int count;
  353. int index;
  354. ULONG_PTR* pKeys;
  355. wKeyValuePair* pair;
  356. wKeyValuePair* nextPair;
  357. if (table->synchronized)
  358. EnterCriticalSection(&table->lock);
  359. iKey = 0;
  360. count = table->numOfElements;
  361. *ppKeys = NULL;
  362. if (count < 1)
  363. {
  364. if (table->synchronized)
  365. LeaveCriticalSection(&table->lock);
  366. return 0;
  367. }
  368. pKeys = (ULONG_PTR*)calloc(count, sizeof(ULONG_PTR));
  369. if (!pKeys)
  370. {
  371. if (table->synchronized)
  372. LeaveCriticalSection(&table->lock);
  373. return -1;
  374. }
  375. for (index = 0; index < table->numOfBuckets; index++)
  376. {
  377. pair = table->bucketArray[index];
  378. while (pair)
  379. {
  380. nextPair = pair->next;
  381. pKeys[iKey++] = (ULONG_PTR)pair->key;
  382. pair = nextPair;
  383. }
  384. }
  385. if (table->synchronized)
  386. LeaveCriticalSection(&table->lock);
  387. *ppKeys = pKeys;
  388. return count;
  389. }
  390. /**
  391. * Determines whether the HashTable contains a specific key.
  392. */
  393. BOOL HashTable_Contains(wHashTable* table, void* key)
  394. {
  395. BOOL status;
  396. if (table->synchronized)
  397. EnterCriticalSection(&table->lock);
  398. status = (HashTable_Get(table, key) != NULL) ? TRUE : FALSE;
  399. if (table->synchronized)
  400. LeaveCriticalSection(&table->lock);
  401. return status;
  402. }
  403. /**
  404. * Determines whether the HashTable contains a specific key.
  405. */
  406. BOOL HashTable_ContainsKey(wHashTable* table, void* key)
  407. {
  408. BOOL status;
  409. if (table->synchronized)
  410. EnterCriticalSection(&table->lock);
  411. status = (HashTable_Get(table, key) != NULL) ? TRUE : FALSE;
  412. if (table->synchronized)
  413. LeaveCriticalSection(&table->lock);
  414. return status;
  415. }
  416. /**
  417. * Determines whether the HashTable contains a specific value.
  418. */
  419. BOOL HashTable_ContainsValue(wHashTable* table, void* value)
  420. {
  421. int index;
  422. BOOL status = FALSE;
  423. wKeyValuePair* pair;
  424. if (table->synchronized)
  425. EnterCriticalSection(&table->lock);
  426. for (index = 0; index < table->numOfBuckets; index++)
  427. {
  428. pair = table->bucketArray[index];
  429. while (pair)
  430. {
  431. if (table->valueCompare(value, pair->value))
  432. {
  433. status = TRUE;
  434. break;
  435. }
  436. pair = pair->next;
  437. }
  438. if (status)
  439. break;
  440. }
  441. if (table->synchronized)
  442. LeaveCriticalSection(&table->lock);
  443. return status;
  444. }
  445. /**
  446. * Construction, Destruction
  447. */
  448. wHashTable* HashTable_New(BOOL synchronized)
  449. {
  450. wHashTable* table;
  451. table = (wHashTable*)calloc(1, sizeof(wHashTable));
  452. if (table)
  453. {
  454. table->synchronized = synchronized;
  455. InitializeCriticalSectionAndSpinCount(&(table->lock), 4000);
  456. table->numOfBuckets = 64;
  457. table->numOfElements = 0;
  458. table->bucketArray = (wKeyValuePair**)calloc(table->numOfBuckets, sizeof(wKeyValuePair*));
  459. if (!table->bucketArray)
  460. {
  461. free(table);
  462. return NULL;
  463. }
  464. table->idealRatio = 3.0;
  465. table->lowerRehashThreshold = 0.0;
  466. table->upperRehashThreshold = 15.0;
  467. table->hash = HashTable_PointerHash;
  468. table->keyCompare = HashTable_PointerCompare;
  469. table->valueCompare = HashTable_PointerCompare;
  470. table->keyClone = NULL;
  471. table->valueClone = NULL;
  472. table->keyFree = NULL;
  473. table->valueFree = NULL;
  474. }
  475. return table;
  476. }
  477. void HashTable_Free(wHashTable* table)
  478. {
  479. int index;
  480. wKeyValuePair* pair;
  481. wKeyValuePair* nextPair;
  482. if (table)
  483. {
  484. for (index = 0; index < table->numOfBuckets; index++)
  485. {
  486. pair = table->bucketArray[index];
  487. while (pair)
  488. {
  489. nextPair = pair->next;
  490. if (table->keyFree)
  491. table->keyFree(pair->key);
  492. if (table->valueFree)
  493. table->valueFree(pair->value);
  494. free(pair);
  495. pair = nextPair;
  496. }
  497. }
  498. DeleteCriticalSection(&(table->lock));
  499. free(table->bucketArray);
  500. free(table);
  501. }
  502. }