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