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