blob: 86d999502a126560103a998bd626b587d702d358 [file] [log] [blame]
drhbeae3192001-09-22 18:12:08 +00001/*
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**
drh93553ad2007-03-31 03:59:23 +000015** $Id: hash.c,v 1.19 2007/03/31 03:59:24 drh Exp $
drhbeae3192001-09-22 18:12:08 +000016*/
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.
drhaacc5432002-01-06 17:07:40 +000022**
drh76d7f8b2004-06-30 22:43:21 +000023** "pNew" is a pointer to the hash table that is to be initialized.
drhaacc5432002-01-06 17:07:40 +000024** 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.
drhbeae3192001-09-22 18:12:08 +000031*/
drh76d7f8b2004-06-30 22:43:21 +000032void sqlite3HashInit(Hash *pNew, int keyClass, int copyKey){
33 assert( pNew!=0 );
drhe8cf2ca2004-08-20 14:08:50 +000034 assert( keyClass>=SQLITE_HASH_STRING && keyClass<=SQLITE_HASH_BINARY );
drh76d7f8b2004-06-30 22:43:21 +000035 pNew->keyClass = keyClass;
drhe8cf2ca2004-08-20 14:08:50 +000036#if 0
37 if( keyClass==SQLITE_HASH_POINTER || keyClass==SQLITE_HASH_INT ) copyKey = 0;
38#endif
39 pNew->copyKey = copyKey;
drh76d7f8b2004-06-30 22:43:21 +000040 pNew->first = 0;
41 pNew->count = 0;
42 pNew->htsize = 0;
43 pNew->ht = 0;
danielk1977750b03e2006-02-14 10:48:39 +000044 pNew->xMalloc = sqlite3MallocX;
45 pNew->xFree = sqlite3FreeX;
drhbeae3192001-09-22 18:12:08 +000046}
47
48/* Remove all entries from a hash table. Reclaim all memory.
drhaacc5432002-01-06 17:07:40 +000049** Call this routine to delete a hash table or to reset a hash table
50** to the empty state.
drhbeae3192001-09-22 18:12:08 +000051*/
danielk19774adee202004-05-08 08:23:19 +000052void sqlite3HashClear(Hash *pH){
drhbeae3192001-09-22 18:12:08 +000053 HashElem *elem; /* For looping over all elements of the table */
54
55 assert( pH!=0 );
56 elem = pH->first;
57 pH->first = 0;
danielk1977750b03e2006-02-14 10:48:39 +000058 if( pH->ht ) pH->xFree(pH->ht);
drhbeae3192001-09-22 18:12:08 +000059 pH->ht = 0;
60 pH->htsize = 0;
61 while( elem ){
62 HashElem *next_elem = elem->next;
63 if( pH->copyKey && elem->pKey ){
danielk1977750b03e2006-02-14 10:48:39 +000064 pH->xFree(elem->pKey);
drhbeae3192001-09-22 18:12:08 +000065 }
danielk1977750b03e2006-02-14 10:48:39 +000066 pH->xFree(elem);
drhbeae3192001-09-22 18:12:08 +000067 elem = next_elem;
68 }
69 pH->count = 0;
70}
71
drhcc195872004-06-30 03:08:24 +000072#if 0 /* NOT USED */
drhbeae3192001-09-22 18:12:08 +000073/*
74** Hash and comparison functions when the mode is SQLITE_HASH_INT
75*/
76static int intHash(const void *pKey, int nKey){
77 return nKey ^ (nKey<<8) ^ (nKey>>8);
78}
79static int intCompare(const void *pKey1, int n1, const void *pKey2, int n2){
80 return n2 - n1;
81}
drhcc195872004-06-30 03:08:24 +000082#endif
drhbeae3192001-09-22 18:12:08 +000083
drhba212562004-01-08 02:17:31 +000084#if 0 /* NOT USED */
drhbeae3192001-09-22 18:12:08 +000085/*
86** Hash and comparison functions when the mode is SQLITE_HASH_POINTER
87*/
88static int ptrHash(const void *pKey, int nKey){
drh5a2c2c22001-11-21 02:21:11 +000089 uptr x = Addr(pKey);
90 return x ^ (x<<8) ^ (x>>8);
drhbeae3192001-09-22 18:12:08 +000091}
92static int ptrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
drh5a2c2c22001-11-21 02:21:11 +000093 if( pKey1==pKey2 ) return 0;
94 if( pKey1<pKey2 ) return -1;
95 return 1;
drhbeae3192001-09-22 18:12:08 +000096}
drhba212562004-01-08 02:17:31 +000097#endif
drhbeae3192001-09-22 18:12:08 +000098
99/*
100** Hash and comparison functions when the mode is SQLITE_HASH_STRING
101*/
102static int strHash(const void *pKey, int nKey){
danielk197752a83fb2005-01-31 12:56:44 +0000103 const char *z = (const char *)pKey;
104 int h = 0;
105 if( nKey<=0 ) nKey = strlen(z);
106 while( nKey > 0 ){
107 h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
108 nKey--;
109 }
110 return h & 0x7fffffff;
drhbeae3192001-09-22 18:12:08 +0000111}
112static int strCompare(const void *pKey1, int n1, const void *pKey2, int n2){
drhe8cf2ca2004-08-20 14:08:50 +0000113 if( n1!=n2 ) return 1;
danielk19774adee202004-05-08 08:23:19 +0000114 return sqlite3StrNICmp((const char*)pKey1,(const char*)pKey2,n1);
drhbeae3192001-09-22 18:12:08 +0000115}
116
117/*
118** Hash and comparison functions when the mode is SQLITE_HASH_BINARY
119*/
120static int binHash(const void *pKey, int nKey){
121 int h = 0;
122 const char *z = (const char *)pKey;
123 while( nKey-- > 0 ){
124 h = (h<<3) ^ h ^ *(z++);
125 }
drh5364f602003-05-12 23:06:52 +0000126 return h & 0x7fffffff;
drhbeae3192001-09-22 18:12:08 +0000127}
128static int binCompare(const void *pKey1, int n1, const void *pKey2, int n2){
drhe8cf2ca2004-08-20 14:08:50 +0000129 if( n1!=n2 ) return 1;
drhbeae3192001-09-22 18:12:08 +0000130 return memcmp(pKey1,pKey2,n1);
131}
132
133/*
134** Return a pointer to the appropriate hash function given the key class.
drhaacc5432002-01-06 17:07:40 +0000135**
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".
drhbeae3192001-09-22 18:12:08 +0000144*/
145static int (*hashFunction(int keyClass))(const void*,int){
drhe8cf2ca2004-08-20 14:08:50 +0000146#if 0 /* HASH_INT and HASH_POINTER are never used */
drhbeae3192001-09-22 18:12:08 +0000147 switch( keyClass ){
drhe8cf2ca2004-08-20 14:08:50 +0000148 case SQLITE_HASH_INT: return &intHash;
149 case SQLITE_HASH_POINTER: return &ptrHash;
drh1ab43002002-01-14 09:28:19 +0000150 case SQLITE_HASH_STRING: return &strHash;
151 case SQLITE_HASH_BINARY: return &binHash;;
drhbeae3192001-09-22 18:12:08 +0000152 default: break;
153 }
154 return 0;
drhe8cf2ca2004-08-20 14:08:50 +0000155#else
156 if( keyClass==SQLITE_HASH_STRING ){
157 return &strHash;
158 }else{
159 assert( keyClass==SQLITE_HASH_BINARY );
160 return &binHash;
161 }
162#endif
drhbeae3192001-09-22 18:12:08 +0000163}
164
165/*
166** Return a pointer to the appropriate hash function given the key class.
drhaacc5432002-01-06 17:07:40 +0000167**
168** For help in interpreted the obscure C code in the function definition,
169** see the header comment on the previous function.
drhbeae3192001-09-22 18:12:08 +0000170*/
171static int (*compareFunction(int keyClass))(const void*,int,const void*,int){
drhe8cf2ca2004-08-20 14:08:50 +0000172#if 0 /* HASH_INT and HASH_POINTER are never used */
drhbeae3192001-09-22 18:12:08 +0000173 switch( keyClass ){
drhe8cf2ca2004-08-20 14:08:50 +0000174 case SQLITE_HASH_INT: return &intCompare;
175 case SQLITE_HASH_POINTER: return &ptrCompare;
drh1ab43002002-01-14 09:28:19 +0000176 case SQLITE_HASH_STRING: return &strCompare;
177 case SQLITE_HASH_BINARY: return &binCompare;
drhbeae3192001-09-22 18:12:08 +0000178 default: break;
179 }
180 return 0;
drhe8cf2ca2004-08-20 14:08:50 +0000181#else
182 if( keyClass==SQLITE_HASH_STRING ){
183 return &strCompare;
184 }else{
185 assert( keyClass==SQLITE_HASH_BINARY );
186 return &binCompare;
187 }
188#endif
189}
190
191/* Link an element into the hash table
192*/
193static void insertElement(
194 Hash *pH, /* The complete hash table */
195 struct _ht *pEntry, /* The entry into which pNew is inserted */
196 HashElem *pNew /* The element to be inserted */
197){
198 HashElem *pHead; /* First element already in pEntry */
199 pHead = pEntry->chain;
200 if( pHead ){
201 pNew->next = pHead;
202 pNew->prev = pHead->prev;
203 if( pHead->prev ){ pHead->prev->next = pNew; }
204 else { pH->first = pNew; }
205 pHead->prev = pNew;
206 }else{
207 pNew->next = pH->first;
208 if( pH->first ){ pH->first->prev = pNew; }
209 pNew->prev = 0;
210 pH->first = pNew;
211 }
212 pEntry->count++;
213 pEntry->chain = pNew;
drhbeae3192001-09-22 18:12:08 +0000214}
215
216
drhaacc5432002-01-06 17:07:40 +0000217/* Resize the hash table so that it cantains "new_size" buckets.
218** "new_size" must be a power of 2. The hash table might fail
219** to resize if sqliteMalloc() fails.
drhbeae3192001-09-22 18:12:08 +0000220*/
221static void rehash(Hash *pH, int new_size){
222 struct _ht *new_ht; /* The new hash table */
223 HashElem *elem, *next_elem; /* For looping over existing elements */
drhbeae3192001-09-22 18:12:08 +0000224 int (*xHash)(const void*,int); /* The hash function */
225
226 assert( (new_size & (new_size-1))==0 );
danielk1977750b03e2006-02-14 10:48:39 +0000227 new_ht = (struct _ht *)pH->xMalloc( new_size*sizeof(struct _ht) );
drhbeae3192001-09-22 18:12:08 +0000228 if( new_ht==0 ) return;
danielk1977750b03e2006-02-14 10:48:39 +0000229 if( pH->ht ) pH->xFree(pH->ht);
drhbeae3192001-09-22 18:12:08 +0000230 pH->ht = new_ht;
231 pH->htsize = new_size;
232 xHash = hashFunction(pH->keyClass);
233 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
234 int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
235 next_elem = elem->next;
drhe8cf2ca2004-08-20 14:08:50 +0000236 insertElement(pH, &new_ht[h], elem);
drhbeae3192001-09-22 18:12:08 +0000237 }
238}
239
240/* This function (for internal use only) locates an element in an
drhaacc5432002-01-06 17:07:40 +0000241** hash table that matches the given key. The hash for this key has
242** already been computed and is passed as the 4th parameter.
drhbeae3192001-09-22 18:12:08 +0000243*/
244static HashElem *findElementGivenHash(
245 const Hash *pH, /* The pH to be searched */
246 const void *pKey, /* The key we are searching for */
247 int nKey,
248 int h /* The hash for this key. */
249){
250 HashElem *elem; /* Used to loop thru the element list */
251 int count; /* Number of elements left to test */
252 int (*xCompare)(const void*,int,const void*,int); /* comparison function */
253
254 if( pH->ht ){
drhe8cf2ca2004-08-20 14:08:50 +0000255 struct _ht *pEntry = &pH->ht[h];
256 elem = pEntry->chain;
257 count = pEntry->count;
drhbeae3192001-09-22 18:12:08 +0000258 xCompare = compareFunction(pH->keyClass);
259 while( count-- && elem ){
260 if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
261 return elem;
262 }
263 elem = elem->next;
264 }
265 }
266 return 0;
267}
268
drh81a20f22001-10-12 17:30:04 +0000269/* Remove a single entry from the hash table given a pointer to that
drhbeae3192001-09-22 18:12:08 +0000270** element and a hash on the element's key.
271*/
272static void removeElementGivenHash(
273 Hash *pH, /* The pH containing "elem" */
274 HashElem* elem, /* The element to be removed from the pH */
drhaacc5432002-01-06 17:07:40 +0000275 int h /* Hash value for the element */
drhbeae3192001-09-22 18:12:08 +0000276){
drhe8cf2ca2004-08-20 14:08:50 +0000277 struct _ht *pEntry;
drhbeae3192001-09-22 18:12:08 +0000278 if( elem->prev ){
279 elem->prev->next = elem->next;
280 }else{
281 pH->first = elem->next;
282 }
283 if( elem->next ){
284 elem->next->prev = elem->prev;
285 }
drhe8cf2ca2004-08-20 14:08:50 +0000286 pEntry = &pH->ht[h];
287 if( pEntry->chain==elem ){
288 pEntry->chain = elem->next;
drhbeae3192001-09-22 18:12:08 +0000289 }
drhe8cf2ca2004-08-20 14:08:50 +0000290 pEntry->count--;
291 if( pEntry->count<=0 ){
292 pEntry->chain = 0;
drhbeae3192001-09-22 18:12:08 +0000293 }
drh93553ad2007-03-31 03:59:23 +0000294 if( pH->copyKey ){
danielk1977750b03e2006-02-14 10:48:39 +0000295 pH->xFree(elem->pKey);
drhbeae3192001-09-22 18:12:08 +0000296 }
danielk1977750b03e2006-02-14 10:48:39 +0000297 pH->xFree( elem );
drhbeae3192001-09-22 18:12:08 +0000298 pH->count--;
drh762e5842005-10-03 15:11:08 +0000299 if( pH->count<=0 ){
300 assert( pH->first==0 );
301 assert( pH->count==0 );
302 sqlite3HashClear(pH);
303 }
drhbeae3192001-09-22 18:12:08 +0000304}
305
drhaacc5432002-01-06 17:07:40 +0000306/* Attempt to locate an element of the hash table pH with a key
drh81a20f22001-10-12 17:30:04 +0000307** that matches pKey,nKey. Return the data for this element if it is
drhaacc5432002-01-06 17:07:40 +0000308** found, or NULL if there is no match.
drhbeae3192001-09-22 18:12:08 +0000309*/
danielk19774adee202004-05-08 08:23:19 +0000310void *sqlite3HashFind(const Hash *pH, const void *pKey, int nKey){
drhbeae3192001-09-22 18:12:08 +0000311 int h; /* A hash on key */
312 HashElem *elem; /* The element that matches key */
313 int (*xHash)(const void*,int); /* The hash function */
314
315 if( pH==0 || pH->ht==0 ) return 0;
316 xHash = hashFunction(pH->keyClass);
317 assert( xHash!=0 );
318 h = (*xHash)(pKey,nKey);
319 assert( (pH->htsize & (pH->htsize-1))==0 );
320 elem = findElementGivenHash(pH,pKey,nKey, h & (pH->htsize-1));
321 return elem ? elem->data : 0;
322}
323
drh81a20f22001-10-12 17:30:04 +0000324/* Insert an element into the hash table pH. The key is pKey,nKey
325** and the data is "data".
drhbeae3192001-09-22 18:12:08 +0000326**
drh81a20f22001-10-12 17:30:04 +0000327** If no element exists with a matching key, then a new
328** element is created. A copy of the key is made if the copyKey
329** flag is set. NULL is returned.
drhbeae3192001-09-22 18:12:08 +0000330**
331** If another element already exists with the same key, then the
332** new data replaces the old data and the old data is returned.
drh6d4abfb2001-10-22 02:58:08 +0000333** The key is not copied in this instance. If a malloc fails, then
drhaacc5432002-01-06 17:07:40 +0000334** the new data is returned and the hash table is unchanged.
drhbeae3192001-09-22 18:12:08 +0000335**
336** If the "data" parameter to this function is NULL, then the
drh81a20f22001-10-12 17:30:04 +0000337** element corresponding to "key" is removed from the hash table.
drhbeae3192001-09-22 18:12:08 +0000338*/
danielk19774adee202004-05-08 08:23:19 +0000339void *sqlite3HashInsert(Hash *pH, const void *pKey, int nKey, void *data){
drhbeae3192001-09-22 18:12:08 +0000340 int hraw; /* Raw hash value of the key */
341 int h; /* the hash of the key modulo hash table size */
342 HashElem *elem; /* Used to loop thru the element list */
343 HashElem *new_elem; /* New element added to the pH */
344 int (*xHash)(const void*,int); /* The hash function */
345
346 assert( pH!=0 );
347 xHash = hashFunction(pH->keyClass);
348 assert( xHash!=0 );
349 hraw = (*xHash)(pKey, nKey);
350 assert( (pH->htsize & (pH->htsize-1))==0 );
351 h = hraw & (pH->htsize-1);
352 elem = findElementGivenHash(pH,pKey,nKey,h);
353 if( elem ){
354 void *old_data = elem->data;
355 if( data==0 ){
356 removeElementGivenHash(pH,elem,h);
357 }else{
358 elem->data = data;
359 }
360 return old_data;
361 }
362 if( data==0 ) return 0;
danielk1977750b03e2006-02-14 10:48:39 +0000363 new_elem = (HashElem*)pH->xMalloc( sizeof(HashElem) );
drh6d4abfb2001-10-22 02:58:08 +0000364 if( new_elem==0 ) return data;
drhbeae3192001-09-22 18:12:08 +0000365 if( pH->copyKey && pKey!=0 ){
danielk1977750b03e2006-02-14 10:48:39 +0000366 new_elem->pKey = pH->xMalloc( nKey );
drhbeae3192001-09-22 18:12:08 +0000367 if( new_elem->pKey==0 ){
danielk1977750b03e2006-02-14 10:48:39 +0000368 pH->xFree(new_elem);
drh6d4abfb2001-10-22 02:58:08 +0000369 return data;
drhbeae3192001-09-22 18:12:08 +0000370 }
371 memcpy((void*)new_elem->pKey, pKey, nKey);
372 }else{
drh2ce1a6e2002-05-21 23:44:30 +0000373 new_elem->pKey = (void*)pKey;
drhbeae3192001-09-22 18:12:08 +0000374 }
375 new_elem->nKey = nKey;
376 pH->count++;
drhbeae3192001-09-22 18:12:08 +0000377 if( pH->htsize==0 ){
drhe8cf2ca2004-08-20 14:08:50 +0000378 rehash(pH,8);
379 if( pH->htsize==0 ){
380 pH->count = 0;
drh93553ad2007-03-31 03:59:23 +0000381 if( pH->copyKey ){
382 pH->xFree(new_elem->pKey);
383 }
danielk1977750b03e2006-02-14 10:48:39 +0000384 pH->xFree(new_elem);
drhe8cf2ca2004-08-20 14:08:50 +0000385 return data;
386 }
drhbeae3192001-09-22 18:12:08 +0000387 }
388 if( pH->count > pH->htsize ){
389 rehash(pH,pH->htsize*2);
390 }
drhe8cf2ca2004-08-20 14:08:50 +0000391 assert( pH->htsize>0 );
drhbeae3192001-09-22 18:12:08 +0000392 assert( (pH->htsize & (pH->htsize-1))==0 );
393 h = hraw & (pH->htsize-1);
drhe8cf2ca2004-08-20 14:08:50 +0000394 insertElement(pH, &pH->ht[h], new_elem);
drhbeae3192001-09-22 18:12:08 +0000395 new_elem->data = data;
396 return 0;
397}