blob: 3761fb547917d9a602cc9161fd01c312f72fe831 [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**
drha83ccca2009-04-28 13:01:09 +000015** $Id: hash.c,v 1.34 2009/04/28 13:01:09 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.
drh1b67f3c2008-10-10 17:41:28 +000024** "copyKey" is true if the hash table should make its own private
25** copy of keys and false if it should just use the supplied pointer.
drhbeae3192001-09-22 18:12:08 +000026*/
drh1b67f3c2008-10-10 17:41:28 +000027void sqlite3HashInit(Hash *pNew, int copyKey){
drh76d7f8b2004-06-30 22:43:21 +000028 assert( pNew!=0 );
drh1b67f3c2008-10-10 17:41:28 +000029 pNew->copyKey = copyKey!=0;
drh76d7f8b2004-06-30 22:43:21 +000030 pNew->first = 0;
31 pNew->count = 0;
32 pNew->htsize = 0;
33 pNew->ht = 0;
drhbeae3192001-09-22 18:12:08 +000034}
35
36/* Remove all entries from a hash table. Reclaim all memory.
drhaacc5432002-01-06 17:07:40 +000037** Call this routine to delete a hash table or to reset a hash table
38** to the empty state.
drhbeae3192001-09-22 18:12:08 +000039*/
danielk19774adee202004-05-08 08:23:19 +000040void sqlite3HashClear(Hash *pH){
drhbeae3192001-09-22 18:12:08 +000041 HashElem *elem; /* For looping over all elements of the table */
42
43 assert( pH!=0 );
44 elem = pH->first;
45 pH->first = 0;
drh41eb9e92008-04-02 18:33:07 +000046 sqlite3_free(pH->ht);
drhbeae3192001-09-22 18:12:08 +000047 pH->ht = 0;
48 pH->htsize = 0;
49 while( elem ){
50 HashElem *next_elem = elem->next;
drhe2f02ba2009-01-09 01:12:27 +000051 if( pH->copyKey ){
drh17435752007-08-16 04:30:38 +000052 sqlite3_free(elem->pKey);
drhbeae3192001-09-22 18:12:08 +000053 }
drh17435752007-08-16 04:30:38 +000054 sqlite3_free(elem);
drhbeae3192001-09-22 18:12:08 +000055 elem = next_elem;
56 }
57 pH->count = 0;
58}
59
drhbeae3192001-09-22 18:12:08 +000060/*
61** Hash and comparison functions when the mode is SQLITE_HASH_STRING
62*/
63static int strHash(const void *pKey, int nKey){
danielk197752a83fb2005-01-31 12:56:44 +000064 const char *z = (const char *)pKey;
65 int h = 0;
drha83ccca2009-04-28 13:01:09 +000066 assert( nKey>0 );
danielk197752a83fb2005-01-31 12:56:44 +000067 while( nKey > 0 ){
68 h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
69 nKey--;
70 }
71 return h & 0x7fffffff;
drhbeae3192001-09-22 18:12:08 +000072}
73static int strCompare(const void *pKey1, int n1, const void *pKey2, int n2){
drhe8cf2ca2004-08-20 14:08:50 +000074 if( n1!=n2 ) return 1;
danielk19774adee202004-05-08 08:23:19 +000075 return sqlite3StrNICmp((const char*)pKey1,(const char*)pKey2,n1);
drhbeae3192001-09-22 18:12:08 +000076}
77
drhe8cf2ca2004-08-20 14:08:50 +000078
79/* Link an element into the hash table
80*/
81static void insertElement(
82 Hash *pH, /* The complete hash table */
83 struct _ht *pEntry, /* The entry into which pNew is inserted */
84 HashElem *pNew /* The element to be inserted */
85){
86 HashElem *pHead; /* First element already in pEntry */
87 pHead = pEntry->chain;
88 if( pHead ){
89 pNew->next = pHead;
90 pNew->prev = pHead->prev;
91 if( pHead->prev ){ pHead->prev->next = pNew; }
92 else { pH->first = pNew; }
93 pHead->prev = pNew;
94 }else{
95 pNew->next = pH->first;
96 if( pH->first ){ pH->first->prev = pNew; }
97 pNew->prev = 0;
98 pH->first = pNew;
99 }
100 pEntry->count++;
101 pEntry->chain = pNew;
drhbeae3192001-09-22 18:12:08 +0000102}
103
104
drhaacc5432002-01-06 17:07:40 +0000105/* Resize the hash table so that it cantains "new_size" buckets.
106** "new_size" must be a power of 2. The hash table might fail
drh17435752007-08-16 04:30:38 +0000107** to resize if sqlite3_malloc() fails.
drhbeae3192001-09-22 18:12:08 +0000108*/
109static void rehash(Hash *pH, int new_size){
110 struct _ht *new_ht; /* The new hash table */
111 HashElem *elem, *next_elem; /* For looping over existing elements */
drhbeae3192001-09-22 18:12:08 +0000112
drheee4c8c2008-02-18 22:24:57 +0000113#ifdef SQLITE_MALLOC_SOFT_LIMIT
114 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
115 new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
116 }
117 if( new_size==pH->htsize ) return;
118#endif
danielk1977a1644fd2007-08-29 12:31:25 +0000119
120 /* There is a call to sqlite3_malloc() inside rehash(). If there is
121 ** already an allocation at pH->ht, then if this malloc() fails it
122 ** is benign (since failing to resize a hash table is a performance
123 ** hit only, not a fatal error).
124 */
danielk19772d1d86f2008-06-20 14:59:51 +0000125 if( pH->htsize>0 ) sqlite3BeginBenignMalloc();
danielk19771e536952007-08-16 10:09:01 +0000126 new_ht = (struct _ht *)sqlite3MallocZero( new_size*sizeof(struct _ht) );
danielk19772d1d86f2008-06-20 14:59:51 +0000127 if( pH->htsize>0 ) sqlite3EndBenignMalloc();
drh643167f2008-01-22 21:30:53 +0000128
drhbeae3192001-09-22 18:12:08 +0000129 if( new_ht==0 ) return;
drh41eb9e92008-04-02 18:33:07 +0000130 sqlite3_free(pH->ht);
drhbeae3192001-09-22 18:12:08 +0000131 pH->ht = new_ht;
132 pH->htsize = new_size;
drhbeae3192001-09-22 18:12:08 +0000133 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
drh1b67f3c2008-10-10 17:41:28 +0000134 int h = strHash(elem->pKey, elem->nKey) & (new_size-1);
drhbeae3192001-09-22 18:12:08 +0000135 next_elem = elem->next;
drhe8cf2ca2004-08-20 14:08:50 +0000136 insertElement(pH, &new_ht[h], elem);
drhbeae3192001-09-22 18:12:08 +0000137 }
138}
139
140/* This function (for internal use only) locates an element in an
drhaacc5432002-01-06 17:07:40 +0000141** hash table that matches the given key. The hash for this key has
142** already been computed and is passed as the 4th parameter.
drhbeae3192001-09-22 18:12:08 +0000143*/
144static HashElem *findElementGivenHash(
145 const Hash *pH, /* The pH to be searched */
146 const void *pKey, /* The key we are searching for */
147 int nKey,
148 int h /* The hash for this key. */
149){
150 HashElem *elem; /* Used to loop thru the element list */
151 int count; /* Number of elements left to test */
drhbeae3192001-09-22 18:12:08 +0000152
153 if( pH->ht ){
drhe8cf2ca2004-08-20 14:08:50 +0000154 struct _ht *pEntry = &pH->ht[h];
155 elem = pEntry->chain;
156 count = pEntry->count;
drhbeae3192001-09-22 18:12:08 +0000157 while( count-- && elem ){
drh1b67f3c2008-10-10 17:41:28 +0000158 if( strCompare(elem->pKey,elem->nKey,pKey,nKey)==0 ){
drhbeae3192001-09-22 18:12:08 +0000159 return elem;
160 }
161 elem = elem->next;
162 }
163 }
164 return 0;
165}
166
drh81a20f22001-10-12 17:30:04 +0000167/* Remove a single entry from the hash table given a pointer to that
drhbeae3192001-09-22 18:12:08 +0000168** element and a hash on the element's key.
169*/
170static void removeElementGivenHash(
171 Hash *pH, /* The pH containing "elem" */
172 HashElem* elem, /* The element to be removed from the pH */
drhaacc5432002-01-06 17:07:40 +0000173 int h /* Hash value for the element */
drhbeae3192001-09-22 18:12:08 +0000174){
drhe8cf2ca2004-08-20 14:08:50 +0000175 struct _ht *pEntry;
drhbeae3192001-09-22 18:12:08 +0000176 if( elem->prev ){
177 elem->prev->next = elem->next;
178 }else{
179 pH->first = elem->next;
180 }
181 if( elem->next ){
182 elem->next->prev = elem->prev;
183 }
drhe8cf2ca2004-08-20 14:08:50 +0000184 pEntry = &pH->ht[h];
185 if( pEntry->chain==elem ){
186 pEntry->chain = elem->next;
drhbeae3192001-09-22 18:12:08 +0000187 }
drhe8cf2ca2004-08-20 14:08:50 +0000188 pEntry->count--;
189 if( pEntry->count<=0 ){
190 pEntry->chain = 0;
drhbeae3192001-09-22 18:12:08 +0000191 }
drh93553ad2007-03-31 03:59:23 +0000192 if( pH->copyKey ){
drh17435752007-08-16 04:30:38 +0000193 sqlite3_free(elem->pKey);
drhbeae3192001-09-22 18:12:08 +0000194 }
drh17435752007-08-16 04:30:38 +0000195 sqlite3_free( elem );
drhbeae3192001-09-22 18:12:08 +0000196 pH->count--;
drh762e5842005-10-03 15:11:08 +0000197 if( pH->count<=0 ){
198 assert( pH->first==0 );
199 assert( pH->count==0 );
200 sqlite3HashClear(pH);
201 }
drhbeae3192001-09-22 18:12:08 +0000202}
203
drhaacc5432002-01-06 17:07:40 +0000204/* Attempt to locate an element of the hash table pH with a key
danielk19777c836f02007-09-04 14:31:47 +0000205** that matches pKey,nKey. Return a pointer to the corresponding
206** HashElem structure for this element if it is found, or NULL
207** otherwise.
drhbeae3192001-09-22 18:12:08 +0000208*/
danielk19777c836f02007-09-04 14:31:47 +0000209HashElem *sqlite3HashFindElem(const Hash *pH, const void *pKey, int nKey){
drhbeae3192001-09-22 18:12:08 +0000210 int h; /* A hash on key */
211 HashElem *elem; /* The element that matches key */
drhbeae3192001-09-22 18:12:08 +0000212
213 if( pH==0 || pH->ht==0 ) return 0;
drh1b67f3c2008-10-10 17:41:28 +0000214 h = strHash(pKey,nKey);
drheee4c8c2008-02-18 22:24:57 +0000215 elem = findElementGivenHash(pH,pKey,nKey, h % pH->htsize);
danielk19777c836f02007-09-04 14:31:47 +0000216 return elem;
217}
218
219/* Attempt to locate an element of the hash table pH with a key
220** that matches pKey,nKey. Return the data for this element if it is
221** found, or NULL if there is no match.
222*/
223void *sqlite3HashFind(const Hash *pH, const void *pKey, int nKey){
224 HashElem *elem; /* The element that matches key */
225 elem = sqlite3HashFindElem(pH, pKey, nKey);
drhbeae3192001-09-22 18:12:08 +0000226 return elem ? elem->data : 0;
227}
228
drh81a20f22001-10-12 17:30:04 +0000229/* Insert an element into the hash table pH. The key is pKey,nKey
230** and the data is "data".
drhbeae3192001-09-22 18:12:08 +0000231**
drh81a20f22001-10-12 17:30:04 +0000232** If no element exists with a matching key, then a new
233** element is created. A copy of the key is made if the copyKey
234** flag is set. NULL is returned.
drhbeae3192001-09-22 18:12:08 +0000235**
236** If another element already exists with the same key, then the
237** new data replaces the old data and the old data is returned.
drh6d4abfb2001-10-22 02:58:08 +0000238** The key is not copied in this instance. If a malloc fails, then
drhaacc5432002-01-06 17:07:40 +0000239** the new data is returned and the hash table is unchanged.
drhbeae3192001-09-22 18:12:08 +0000240**
241** If the "data" parameter to this function is NULL, then the
drh81a20f22001-10-12 17:30:04 +0000242** element corresponding to "key" is removed from the hash table.
drhbeae3192001-09-22 18:12:08 +0000243*/
danielk19774adee202004-05-08 08:23:19 +0000244void *sqlite3HashInsert(Hash *pH, const void *pKey, int nKey, void *data){
drhbeae3192001-09-22 18:12:08 +0000245 int hraw; /* Raw hash value of the key */
246 int h; /* the hash of the key modulo hash table size */
247 HashElem *elem; /* Used to loop thru the element list */
248 HashElem *new_elem; /* New element added to the pH */
drhbeae3192001-09-22 18:12:08 +0000249
250 assert( pH!=0 );
drh1b67f3c2008-10-10 17:41:28 +0000251 hraw = strHash(pKey, nKey);
drheee4c8c2008-02-18 22:24:57 +0000252 if( pH->htsize ){
253 h = hraw % pH->htsize;
254 elem = findElementGivenHash(pH,pKey,nKey,h);
255 if( elem ){
256 void *old_data = elem->data;
257 if( data==0 ){
258 removeElementGivenHash(pH,elem,h);
259 }else{
260 elem->data = data;
261 if( !pH->copyKey ){
262 elem->pKey = (void *)pKey;
263 }
264 assert(nKey==elem->nKey);
danielk1977cd2543b2007-09-03 15:03:20 +0000265 }
drheee4c8c2008-02-18 22:24:57 +0000266 return old_data;
drhbeae3192001-09-22 18:12:08 +0000267 }
drhbeae3192001-09-22 18:12:08 +0000268 }
269 if( data==0 ) return 0;
drhe5ae5732008-06-15 02:51:47 +0000270 new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
drh6d4abfb2001-10-22 02:58:08 +0000271 if( new_elem==0 ) return data;
drhbeae3192001-09-22 18:12:08 +0000272 if( pH->copyKey && pKey!=0 ){
drhe5ae5732008-06-15 02:51:47 +0000273 new_elem->pKey = sqlite3Malloc( nKey );
drhbeae3192001-09-22 18:12:08 +0000274 if( new_elem->pKey==0 ){
drh17435752007-08-16 04:30:38 +0000275 sqlite3_free(new_elem);
drh6d4abfb2001-10-22 02:58:08 +0000276 return data;
drhbeae3192001-09-22 18:12:08 +0000277 }
278 memcpy((void*)new_elem->pKey, pKey, nKey);
279 }else{
drh2ce1a6e2002-05-21 23:44:30 +0000280 new_elem->pKey = (void*)pKey;
drhbeae3192001-09-22 18:12:08 +0000281 }
282 new_elem->nKey = nKey;
283 pH->count++;
drhbeae3192001-09-22 18:12:08 +0000284 if( pH->htsize==0 ){
drheee4c8c2008-02-18 22:24:57 +0000285 rehash(pH, 128/sizeof(pH->ht[0]));
drhe8cf2ca2004-08-20 14:08:50 +0000286 if( pH->htsize==0 ){
287 pH->count = 0;
drh93553ad2007-03-31 03:59:23 +0000288 if( pH->copyKey ){
drh17435752007-08-16 04:30:38 +0000289 sqlite3_free(new_elem->pKey);
drh93553ad2007-03-31 03:59:23 +0000290 }
drh17435752007-08-16 04:30:38 +0000291 sqlite3_free(new_elem);
drhe8cf2ca2004-08-20 14:08:50 +0000292 return data;
293 }
drhbeae3192001-09-22 18:12:08 +0000294 }
295 if( pH->count > pH->htsize ){
296 rehash(pH,pH->htsize*2);
297 }
drhe8cf2ca2004-08-20 14:08:50 +0000298 assert( pH->htsize>0 );
drheee4c8c2008-02-18 22:24:57 +0000299 h = hraw % pH->htsize;
drhe8cf2ca2004-08-20 14:08:50 +0000300 insertElement(pH, &pH->ht[h], new_elem);
drhbeae3192001-09-22 18:12:08 +0000301 new_elem->data = data;
302 return 0;
303}