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