blob: 558180cab003af0859c2b1f58881d386eab4f511 [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**
drhcc195872004-06-30 03:08:24 +000015** $Id: hash.c,v 1.13 2004/06/30 03:08:25 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**
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.
drhbeae3192001-09-22 18:12:08 +000031*/
danielk19774adee202004-05-08 08:23:19 +000032void sqlite3HashInit(Hash *new, int keyClass, int copyKey){
drhbeae3192001-09-22 18:12:08 +000033 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.
drhaacc5432002-01-06 17:07:40 +000045** Call this routine to delete a hash table or to reset a hash table
46** to the empty state.
drhbeae3192001-09-22 18:12:08 +000047*/
danielk19774adee202004-05-08 08:23:19 +000048void sqlite3HashClear(Hash *pH){
drhbeae3192001-09-22 18:12:08 +000049 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
drhcc195872004-06-30 03:08:24 +000068#if 0 /* NOT USED */
drhbeae3192001-09-22 18:12:08 +000069/*
70** Hash and comparison functions when the mode is SQLITE_HASH_INT
71*/
72static int intHash(const void *pKey, int nKey){
73 return nKey ^ (nKey<<8) ^ (nKey>>8);
74}
75static int intCompare(const void *pKey1, int n1, const void *pKey2, int n2){
76 return n2 - n1;
77}
drhcc195872004-06-30 03:08:24 +000078#endif
drhbeae3192001-09-22 18:12:08 +000079
drhba212562004-01-08 02:17:31 +000080#if 0 /* NOT USED */
drhbeae3192001-09-22 18:12:08 +000081/*
82** Hash and comparison functions when the mode is SQLITE_HASH_POINTER
83*/
84static int ptrHash(const void *pKey, int nKey){
drh5a2c2c22001-11-21 02:21:11 +000085 uptr x = Addr(pKey);
86 return x ^ (x<<8) ^ (x>>8);
drhbeae3192001-09-22 18:12:08 +000087}
88static int ptrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
drh5a2c2c22001-11-21 02:21:11 +000089 if( pKey1==pKey2 ) return 0;
90 if( pKey1<pKey2 ) return -1;
91 return 1;
drhbeae3192001-09-22 18:12:08 +000092}
drhba212562004-01-08 02:17:31 +000093#endif
drhbeae3192001-09-22 18:12:08 +000094
95/*
96** Hash and comparison functions when the mode is SQLITE_HASH_STRING
97*/
98static int strHash(const void *pKey, int nKey){
danielk19774adee202004-05-08 08:23:19 +000099 return sqlite3HashNoCase((const char*)pKey, nKey);
drhbeae3192001-09-22 18:12:08 +0000100}
101static int strCompare(const void *pKey1, int n1, const void *pKey2, int n2){
102 if( n1!=n2 ) return n2-n1;
danielk19774adee202004-05-08 08:23:19 +0000103 return sqlite3StrNICmp((const char*)pKey1,(const char*)pKey2,n1);
drhbeae3192001-09-22 18:12:08 +0000104}
105
106/*
107** Hash and comparison functions when the mode is SQLITE_HASH_BINARY
108*/
109static 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 }
drh5364f602003-05-12 23:06:52 +0000115 return h & 0x7fffffff;
drhbeae3192001-09-22 18:12:08 +0000116}
117static 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.
drhaacc5432002-01-06 17:07:40 +0000124**
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".
drhbeae3192001-09-22 18:12:08 +0000133*/
134static int (*hashFunction(int keyClass))(const void*,int){
135 switch( keyClass ){
drhcc195872004-06-30 03:08:24 +0000136 /* case SQLITE_HASH_INT: return &intHash; // NOT USED */
drhba212562004-01-08 02:17:31 +0000137 /* case SQLITE_HASH_POINTER: return &ptrHash; // NOT USED */
drh1ab43002002-01-14 09:28:19 +0000138 case SQLITE_HASH_STRING: return &strHash;
139 case SQLITE_HASH_BINARY: return &binHash;;
drhbeae3192001-09-22 18:12:08 +0000140 default: break;
141 }
142 return 0;
143}
144
145/*
146** Return a pointer to the appropriate hash function given the key class.
drhaacc5432002-01-06 17:07:40 +0000147**
148** For help in interpreted the obscure C code in the function definition,
149** see the header comment on the previous function.
drhbeae3192001-09-22 18:12:08 +0000150*/
151static int (*compareFunction(int keyClass))(const void*,int,const void*,int){
152 switch( keyClass ){
drhcc195872004-06-30 03:08:24 +0000153 /* case SQLITE_HASH_INT: return &intCompare; // NOT USED */
drhba212562004-01-08 02:17:31 +0000154 /* case SQLITE_HASH_POINTER: return &ptrCompare; // NOT USED */
drh1ab43002002-01-14 09:28:19 +0000155 case SQLITE_HASH_STRING: return &strCompare;
156 case SQLITE_HASH_BINARY: return &binCompare;
drhbeae3192001-09-22 18:12:08 +0000157 default: break;
158 }
159 return 0;
160}
161
162
drhaacc5432002-01-06 17:07:40 +0000163/* 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.
drhbeae3192001-09-22 18:12:08 +0000166*/
167static 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
drhaacc5432002-01-06 17:07:40 +0000202** hash table that matches the given key. The hash for this key has
203** already been computed and is passed as the 4th parameter.
drhbeae3192001-09-22 18:12:08 +0000204*/
205static 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
drh81a20f22001-10-12 17:30:04 +0000229/* Remove a single entry from the hash table given a pointer to that
drhbeae3192001-09-22 18:12:08 +0000230** element and a hash on the element's key.
231*/
232static void removeElementGivenHash(
233 Hash *pH, /* The pH containing "elem" */
234 HashElem* elem, /* The element to be removed from the pH */
drhaacc5432002-01-06 17:07:40 +0000235 int h /* Hash value for the element */
drhbeae3192001-09-22 18:12:08 +0000236){
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
drhaacc5432002-01-06 17:07:40 +0000259/* Attempt to locate an element of the hash table pH with a key
drh81a20f22001-10-12 17:30:04 +0000260** that matches pKey,nKey. Return the data for this element if it is
drhaacc5432002-01-06 17:07:40 +0000261** found, or NULL if there is no match.
drhbeae3192001-09-22 18:12:08 +0000262*/
danielk19774adee202004-05-08 08:23:19 +0000263void *sqlite3HashFind(const Hash *pH, const void *pKey, int nKey){
drhbeae3192001-09-22 18:12:08 +0000264 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
drh81a20f22001-10-12 17:30:04 +0000277/* Insert an element into the hash table pH. The key is pKey,nKey
278** and the data is "data".
drhbeae3192001-09-22 18:12:08 +0000279**
drh81a20f22001-10-12 17:30:04 +0000280** 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.
drhbeae3192001-09-22 18:12:08 +0000283**
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.
drh6d4abfb2001-10-22 02:58:08 +0000286** The key is not copied in this instance. If a malloc fails, then
drhaacc5432002-01-06 17:07:40 +0000287** the new data is returned and the hash table is unchanged.
drhbeae3192001-09-22 18:12:08 +0000288**
289** If the "data" parameter to this function is NULL, then the
drh81a20f22001-10-12 17:30:04 +0000290** element corresponding to "key" is removed from the hash table.
drhbeae3192001-09-22 18:12:08 +0000291*/
danielk19774adee202004-05-08 08:23:19 +0000292void *sqlite3HashInsert(Hash *pH, const void *pKey, int nKey, void *data){
drhbeae3192001-09-22 18:12:08 +0000293 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) );
drh6d4abfb2001-10-22 02:58:08 +0000317 if( new_elem==0 ) return data;
drhbeae3192001-09-22 18:12:08 +0000318 if( pH->copyKey && pKey!=0 ){
drh8c1238a2003-01-02 14:43:55 +0000319 new_elem->pKey = sqliteMallocRaw( nKey );
drhbeae3192001-09-22 18:12:08 +0000320 if( new_elem->pKey==0 ){
321 sqliteFree(new_elem);
drh6d4abfb2001-10-22 02:58:08 +0000322 return data;
drhbeae3192001-09-22 18:12:08 +0000323 }
324 memcpy((void*)new_elem->pKey, pKey, nKey);
325 }else{
drh2ce1a6e2002-05-21 23:44:30 +0000326 new_elem->pKey = (void*)pKey;
drhbeae3192001-09-22 18:12:08 +0000327 }
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);
drh6d4abfb2001-10-22 02:58:08 +0000334 return data;
drhbeae3192001-09-22 18:12:08 +0000335 }
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}