1
0

ft_hash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. ** 2001 September 22
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. *************************************************************************
  12. ** This is the implementation of generic hash-tables used in SQLite.
  13. ** We've modified it slightly to serve as a standalone hash table
  14. ** implementation for the full-text indexing module.
  15. */
  16. #include <assert.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "ft_hash.h"
  20. void *malloc_and_zero(int n){
  21. void *p = malloc(n);
  22. if( p ){
  23. memset(p, 0, n);
  24. }
  25. return p;
  26. }
  27. /* Turn bulk memory into a hash table object by initializing the
  28. ** fields of the Hash structure.
  29. **
  30. ** "pNew" is a pointer to the hash table that is to be initialized.
  31. ** keyClass is one of the constants HASH_INT, HASH_POINTER,
  32. ** HASH_BINARY, or HASH_STRING. The value of keyClass
  33. ** determines what kind of key the hash table will use. "copyKey" is
  34. ** true if the hash table should make its own private copy of keys and
  35. ** false if it should just use the supplied pointer. CopyKey only makes
  36. ** sense for HASH_STRING and HASH_BINARY and is ignored
  37. ** for other key classes.
  38. */
  39. void HashInit(Hash *pNew, int keyClass, int copyKey){
  40. assert( pNew!=0 );
  41. assert( keyClass>=HASH_STRING && keyClass<=HASH_BINARY );
  42. pNew->keyClass = keyClass;
  43. #if 0
  44. if( keyClass==HASH_POINTER || keyClass==HASH_INT ) copyKey = 0;
  45. #endif
  46. pNew->copyKey = copyKey;
  47. pNew->first = 0;
  48. pNew->count = 0;
  49. pNew->htsize = 0;
  50. pNew->ht = 0;
  51. pNew->xMalloc = malloc_and_zero;
  52. pNew->xFree = free;
  53. }
  54. /* Remove all entries from a hash table. Reclaim all memory.
  55. ** Call this routine to delete a hash table or to reset a hash table
  56. ** to the empty state.
  57. */
  58. void HashClear(Hash *pH){
  59. HashElem *elem; /* For looping over all elements of the table */
  60. assert( pH!=0 );
  61. elem = pH->first;
  62. pH->first = 0;
  63. if( pH->ht ) pH->xFree(pH->ht);
  64. pH->ht = 0;
  65. pH->htsize = 0;
  66. while( elem ){
  67. HashElem *next_elem = elem->next;
  68. if( pH->copyKey && elem->pKey ){
  69. pH->xFree(elem->pKey);
  70. }
  71. pH->xFree(elem);
  72. elem = next_elem;
  73. }
  74. pH->count = 0;
  75. }
  76. #if 0 /* NOT USED */
  77. /*
  78. ** Hash and comparison functions when the mode is HASH_INT
  79. */
  80. static int intHash(const void *pKey, int nKey){
  81. return nKey ^ (nKey<<8) ^ (nKey>>8);
  82. }
  83. static int intCompare(const void *pKey1, int n1, const void *pKey2, int n2){
  84. return n2 - n1;
  85. }
  86. #endif
  87. #if 0 /* NOT USED */
  88. /*
  89. ** Hash and comparison functions when the mode is HASH_POINTER
  90. */
  91. static int ptrHash(const void *pKey, int nKey){
  92. uptr x = Addr(pKey);
  93. return x ^ (x<<8) ^ (x>>8);
  94. }
  95. static int ptrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
  96. if( pKey1==pKey2 ) return 0;
  97. if( pKey1<pKey2 ) return -1;
  98. return 1;
  99. }
  100. #endif
  101. /*
  102. ** Hash and comparison functions when the mode is HASH_STRING
  103. */
  104. static int strHash(const void *pKey, int nKey){
  105. const char *z = (const char *)pKey;
  106. int h = 0;
  107. if( nKey<=0 ) nKey = (int) strlen(z);
  108. while( nKey > 0 ){
  109. h = (h<<3) ^ h ^ *z++;
  110. nKey--;
  111. }
  112. return h & 0x7fffffff;
  113. }
  114. static int strCompare(const void *pKey1, int n1, const void *pKey2, int n2){
  115. if( n1!=n2 ) return 1;
  116. return strncmp((const char*)pKey1,(const char*)pKey2,n1);
  117. }
  118. /*
  119. ** Hash and comparison functions when the mode is HASH_BINARY
  120. */
  121. static int binHash(const void *pKey, int nKey){
  122. int h = 0;
  123. const char *z = (const char *)pKey;
  124. while( nKey-- > 0 ){
  125. h = (h<<3) ^ h ^ *(z++);
  126. }
  127. return h & 0x7fffffff;
  128. }
  129. static int binCompare(const void *pKey1, int n1, const void *pKey2, int n2){
  130. if( n1!=n2 ) return 1;
  131. return memcmp(pKey1,pKey2,n1);
  132. }
  133. /*
  134. ** Return a pointer to the appropriate hash function given the key class.
  135. **
  136. ** The C syntax in this function definition may be unfamilar to some
  137. ** programmers, so we provide the following additional explanation:
  138. **
  139. ** The name of the function is "hashFunction". The function takes a
  140. ** single parameter "keyClass". The return value of hashFunction()
  141. ** is a pointer to another function. Specifically, the return value
  142. ** of hashFunction() is a pointer to a function that takes two parameters
  143. ** with types "const void*" and "int" and returns an "int".
  144. */
  145. static int (*hashFunction(int keyClass))(const void*,int){
  146. #if 0 /* HASH_INT and HASH_POINTER are never used */
  147. switch( keyClass ){
  148. case HASH_INT: return &intHash;
  149. case HASH_POINTER: return &ptrHash;
  150. case HASH_STRING: return &strHash;
  151. case HASH_BINARY: return &binHash;;
  152. default: break;
  153. }
  154. return 0;
  155. #else
  156. if( keyClass==HASH_STRING ){
  157. return &strHash;
  158. }else{
  159. assert( keyClass==HASH_BINARY );
  160. return &binHash;
  161. }
  162. #endif
  163. }
  164. /*
  165. ** Return a pointer to the appropriate hash function given the key class.
  166. **
  167. ** For help in interpreted the obscure C code in the function definition,
  168. ** see the header comment on the previous function.
  169. */
  170. static int (*compareFunction(int keyClass))(const void*,int,const void*,int){
  171. #if 0 /* HASH_INT and HASH_POINTER are never used */
  172. switch( keyClass ){
  173. case HASH_INT: return &intCompare;
  174. case HASH_POINTER: return &ptrCompare;
  175. case HASH_STRING: return &strCompare;
  176. case HASH_BINARY: return &binCompare;
  177. default: break;
  178. }
  179. return 0;
  180. #else
  181. if( keyClass==HASH_STRING ){
  182. return &strCompare;
  183. }else{
  184. assert( keyClass==HASH_BINARY );
  185. return &binCompare;
  186. }
  187. #endif
  188. }
  189. /* Link an element into the hash table
  190. */
  191. static void insertElement(
  192. Hash *pH, /* The complete hash table */
  193. struct _ht *pEntry, /* The entry into which pNew is inserted */
  194. HashElem *pNew /* The element to be inserted */
  195. ){
  196. HashElem *pHead; /* First element already in pEntry */
  197. pHead = pEntry->chain;
  198. if( pHead ){
  199. pNew->next = pHead;
  200. pNew->prev = pHead->prev;
  201. if( pHead->prev ){ pHead->prev->next = pNew; }
  202. else { pH->first = pNew; }
  203. pHead->prev = pNew;
  204. }else{
  205. pNew->next = pH->first;
  206. if( pH->first ){ pH->first->prev = pNew; }
  207. pNew->prev = 0;
  208. pH->first = pNew;
  209. }
  210. pEntry->count++;
  211. pEntry->chain = pNew;
  212. }
  213. /* Resize the hash table so that it cantains "new_size" buckets.
  214. ** "new_size" must be a power of 2. The hash table might fail
  215. ** to resize if sqliteMalloc() fails.
  216. */
  217. static void rehash(Hash *pH, int new_size){
  218. struct _ht *new_ht; /* The new hash table */
  219. HashElem *elem, *next_elem; /* For looping over existing elements */
  220. int (*xHash)(const void*,int); /* The hash function */
  221. assert( (new_size & (new_size-1))==0 );
  222. new_ht = (struct _ht *)pH->xMalloc( new_size*sizeof(struct _ht) );
  223. if( new_ht==0 ) return;
  224. if( pH->ht ) pH->xFree(pH->ht);
  225. pH->ht = new_ht;
  226. pH->htsize = new_size;
  227. xHash = hashFunction(pH->keyClass);
  228. for(elem=pH->first, pH->first=0; elem; elem = next_elem){
  229. int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
  230. next_elem = elem->next;
  231. insertElement(pH, &new_ht[h], elem);
  232. }
  233. }
  234. /* This function (for internal use only) locates an element in an
  235. ** hash table that matches the given key. The hash for this key has
  236. ** already been computed and is passed as the 4th parameter.
  237. */
  238. static HashElem *findElementGivenHash(
  239. const Hash *pH, /* The pH to be searched */
  240. const void *pKey, /* The key we are searching for */
  241. int nKey,
  242. int h /* The hash for this key. */
  243. ){
  244. HashElem *elem; /* Used to loop thru the element list */
  245. int count; /* Number of elements left to test */
  246. int (*xCompare)(const void*,int,const void*,int); /* comparison function */
  247. if( pH->ht ){
  248. struct _ht *pEntry = &pH->ht[h];
  249. elem = pEntry->chain;
  250. count = pEntry->count;
  251. xCompare = compareFunction(pH->keyClass);
  252. while( count-- && elem ){
  253. if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
  254. return elem;
  255. }
  256. elem = elem->next;
  257. }
  258. }
  259. return 0;
  260. }
  261. /* Remove a single entry from the hash table given a pointer to that
  262. ** element and a hash on the element's key.
  263. */
  264. static void removeElementGivenHash(
  265. Hash *pH, /* The pH containing "elem" */
  266. HashElem* elem, /* The element to be removed from the pH */
  267. int h /* Hash value for the element */
  268. ){
  269. struct _ht *pEntry;
  270. if( elem->prev ){
  271. elem->prev->next = elem->next;
  272. }else{
  273. pH->first = elem->next;
  274. }
  275. if( elem->next ){
  276. elem->next->prev = elem->prev;
  277. }
  278. pEntry = &pH->ht[h];
  279. if( pEntry->chain==elem ){
  280. pEntry->chain = elem->next;
  281. }
  282. pEntry->count--;
  283. if( pEntry->count<=0 ){
  284. pEntry->chain = 0;
  285. }
  286. if( pH->copyKey && elem->pKey ){
  287. pH->xFree(elem->pKey);
  288. }
  289. pH->xFree( elem );
  290. pH->count--;
  291. if( pH->count<=0 ){
  292. assert( pH->first==0 );
  293. assert( pH->count==0 );
  294. HashClear(pH);
  295. }
  296. }
  297. /* Attempt to locate an element of the hash table pH with a key
  298. ** that matches pKey,nKey. Return the data for this element if it is
  299. ** found, or NULL if there is no match.
  300. */
  301. void *HashFind(const Hash *pH, const void *pKey, int nKey){
  302. int h; /* A hash on key */
  303. HashElem *elem; /* The element that matches key */
  304. int (*xHash)(const void*,int); /* The hash function */
  305. if( pH==0 || pH->ht==0 ) return 0;
  306. xHash = hashFunction(pH->keyClass);
  307. assert( xHash!=0 );
  308. h = (*xHash)(pKey,nKey);
  309. assert( (pH->htsize & (pH->htsize-1))==0 );
  310. elem = findElementGivenHash(pH,pKey,nKey, h & (pH->htsize-1));
  311. return elem ? elem->data : 0;
  312. }
  313. /* Insert an element into the hash table pH. The key is pKey,nKey
  314. ** and the data is "data".
  315. **
  316. ** If no element exists with a matching key, then a new
  317. ** element is created. A copy of the key is made if the copyKey
  318. ** flag is set. NULL is returned.
  319. **
  320. ** If another element already exists with the same key, then the
  321. ** new data replaces the old data and the old data is returned.
  322. ** The key is not copied in this instance. If a malloc fails, then
  323. ** the new data is returned and the hash table is unchanged.
  324. **
  325. ** If the "data" parameter to this function is NULL, then the
  326. ** element corresponding to "key" is removed from the hash table.
  327. */
  328. void *HashInsert(Hash *pH, const void *pKey, int nKey, void *data){
  329. int hraw; /* Raw hash value of the key */
  330. int h; /* the hash of the key modulo hash table size */
  331. HashElem *elem; /* Used to loop thru the element list */
  332. HashElem *new_elem; /* New element added to the pH */
  333. int (*xHash)(const void*,int); /* The hash function */
  334. assert( pH!=0 );
  335. xHash = hashFunction(pH->keyClass);
  336. assert( xHash!=0 );
  337. hraw = (*xHash)(pKey, nKey);
  338. assert( (pH->htsize & (pH->htsize-1))==0 );
  339. h = hraw & (pH->htsize-1);
  340. elem = findElementGivenHash(pH,pKey,nKey,h);
  341. if( elem ){
  342. void *old_data = elem->data;
  343. if( data==0 ){
  344. removeElementGivenHash(pH,elem,h);
  345. }else{
  346. elem->data = data;
  347. }
  348. return old_data;
  349. }
  350. if( data==0 ) return 0;
  351. new_elem = (HashElem*)pH->xMalloc( sizeof(HashElem) );
  352. if( new_elem==0 ) return data;
  353. if( pH->copyKey && pKey!=0 ){
  354. new_elem->pKey = pH->xMalloc( nKey );
  355. if( new_elem->pKey==0 ){
  356. pH->xFree(new_elem);
  357. return data;
  358. }
  359. memcpy((void*)new_elem->pKey, pKey, nKey);
  360. }else{
  361. new_elem->pKey = (void*)pKey;
  362. }
  363. new_elem->nKey = nKey;
  364. pH->count++;
  365. if( pH->htsize==0 ){
  366. rehash(pH,8);
  367. if( pH->htsize==0 ){
  368. pH->count = 0;
  369. pH->xFree(new_elem);
  370. return data;
  371. }
  372. }
  373. if( pH->count > pH->htsize ){
  374. rehash(pH,pH->htsize*2);
  375. }
  376. assert( pH->htsize>0 );
  377. assert( (pH->htsize & (pH->htsize-1))==0 );
  378. h = hraw & (pH->htsize-1);
  379. insertElement(pH, &pH->ht[h], new_elem);
  380. new_elem->data = data;
  381. return 0;
  382. }