hash.c 12 KB

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