1beae3194Sdrh /*
2beae3194Sdrh ** 2001 September 22
3beae3194Sdrh **
4beae3194Sdrh ** The author disclaims copyright to this source code. In place of
5beae3194Sdrh ** a legal notice, here is a blessing:
6beae3194Sdrh **
7beae3194Sdrh ** May you do good and not evil.
8beae3194Sdrh ** May you find forgiveness for yourself and forgive others.
9beae3194Sdrh ** May you share freely, never taking more than you give.
10beae3194Sdrh **
11beae3194Sdrh *************************************************************************
12beae3194Sdrh ** This is the implementation of generic hash-tables
13beae3194Sdrh ** used in SQLite.
14beae3194Sdrh */
15beae3194Sdrh #include "sqliteInt.h"
16beae3194Sdrh #include <assert.h>
17beae3194Sdrh
18beae3194Sdrh /* Turn bulk memory into a hash table object by initializing the
19beae3194Sdrh ** fields of the Hash structure.
20aacc543eSdrh **
2176d7f8b3Sdrh ** "pNew" is a pointer to the hash table that is to be initialized.
22beae3194Sdrh */
sqlite3HashInit(Hash * pNew)23e61922a6Sdrh void sqlite3HashInit(Hash *pNew){
2476d7f8b3Sdrh assert( pNew!=0 );
2576d7f8b3Sdrh pNew->first = 0;
2676d7f8b3Sdrh pNew->count = 0;
2776d7f8b3Sdrh pNew->htsize = 0;
2876d7f8b3Sdrh pNew->ht = 0;
29beae3194Sdrh }
30beae3194Sdrh
31beae3194Sdrh /* Remove all entries from a hash table. Reclaim all memory.
32aacc543eSdrh ** Call this routine to delete a hash table or to reset a hash table
33aacc543eSdrh ** to the empty state.
34beae3194Sdrh */
sqlite3HashClear(Hash * pH)354adee20fSdanielk1977 void sqlite3HashClear(Hash *pH){
36beae3194Sdrh HashElem *elem; /* For looping over all elements of the table */
37beae3194Sdrh
38beae3194Sdrh assert( pH!=0 );
39beae3194Sdrh elem = pH->first;
40beae3194Sdrh pH->first = 0;
4141eb9e99Sdrh sqlite3_free(pH->ht);
42beae3194Sdrh pH->ht = 0;
43beae3194Sdrh pH->htsize = 0;
44beae3194Sdrh while( elem ){
45beae3194Sdrh HashElem *next_elem = elem->next;
4617435752Sdrh sqlite3_free(elem);
47beae3194Sdrh elem = next_elem;
48beae3194Sdrh }
49beae3194Sdrh pH->count = 0;
50beae3194Sdrh }
51beae3194Sdrh
52beae3194Sdrh /*
53e61922a6Sdrh ** The hashing function.
54beae3194Sdrh */
strHash(const char * z)55acbcb7e0Sdrh static unsigned int strHash(const char *z){
56dc3bb0deSdrh unsigned int h = 0;
57acbcb7e0Sdrh unsigned char c;
5875ab50ceSdrh while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
595f33eaa6Sdrh /* Knuth multiplicative hashing. (Sorting & Searching, p. 510).
605f33eaa6Sdrh ** 0x9e3779b1 is 2654435761 which is the closest prime number to
615f33eaa6Sdrh ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
625f33eaa6Sdrh h += sqlite3UpperToLower[c];
635f33eaa6Sdrh h *= 0x9e3779b1;
6452a83fbbSdanielk1977 }
658a1e594cSdrh return h;
66beae3194Sdrh }
67beae3194Sdrh
68e8cf2cacSdrh
698a1e594cSdrh /* Link pNew element into the hash table pH. If pEntry!=0 then also
708a1e594cSdrh ** insert pNew into the pEntry hash bucket.
71e8cf2cacSdrh */
insertElement(Hash * pH,struct _ht * pEntry,HashElem * pNew)72e8cf2cacSdrh static void insertElement(
73e8cf2cacSdrh Hash *pH, /* The complete hash table */
74e8cf2cacSdrh struct _ht *pEntry, /* The entry into which pNew is inserted */
75e8cf2cacSdrh HashElem *pNew /* The element to be inserted */
76e8cf2cacSdrh ){
77e8cf2cacSdrh HashElem *pHead; /* First element already in pEntry */
788a1e594cSdrh if( pEntry ){
798a1e594cSdrh pHead = pEntry->count ? pEntry->chain : 0;
808a1e594cSdrh pEntry->count++;
818a1e594cSdrh pEntry->chain = pNew;
828a1e594cSdrh }else{
838a1e594cSdrh pHead = 0;
848a1e594cSdrh }
85e8cf2cacSdrh if( pHead ){
86e8cf2cacSdrh pNew->next = pHead;
87e8cf2cacSdrh pNew->prev = pHead->prev;
88e8cf2cacSdrh if( pHead->prev ){ pHead->prev->next = pNew; }
89e8cf2cacSdrh else { pH->first = pNew; }
90e8cf2cacSdrh pHead->prev = pNew;
91e8cf2cacSdrh }else{
92e8cf2cacSdrh pNew->next = pH->first;
93e8cf2cacSdrh if( pH->first ){ pH->first->prev = pNew; }
94e8cf2cacSdrh pNew->prev = 0;
95e8cf2cacSdrh pH->first = pNew;
96e8cf2cacSdrh }
97beae3194Sdrh }
98beae3194Sdrh
99beae3194Sdrh
100aacc543eSdrh /* Resize the hash table so that it cantains "new_size" buckets.
1018a1e594cSdrh **
1028a1e594cSdrh ** The hash table might fail to resize if sqlite3_malloc() fails or
1038a1e594cSdrh ** if the new size is the same as the prior size.
1048a1e594cSdrh ** Return TRUE if the resize occurs and false if not.
105beae3194Sdrh */
rehash(Hash * pH,unsigned int new_size)1068a1e594cSdrh static int rehash(Hash *pH, unsigned int new_size){
107beae3194Sdrh struct _ht *new_ht; /* The new hash table */
108beae3194Sdrh HashElem *elem, *next_elem; /* For looping over existing elements */
109beae3194Sdrh
1108a1e594cSdrh #if SQLITE_MALLOC_SOFT_LIMIT>0
111eee4c8caSdrh if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
112eee4c8caSdrh new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
113eee4c8caSdrh }
1148a1e594cSdrh if( new_size==pH->htsize ) return 0;
115eee4c8caSdrh #endif
116a1644fd8Sdanielk1977
1178a1e594cSdrh /* The inability to allocates space for a larger hash table is
1188a1e594cSdrh ** a performance hit but it is not a fatal error. So mark the
11938d07304Sdan ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of
12038d07304Sdan ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
12138d07304Sdan ** only zeroes the requested number of bytes whereas this module will
12238d07304Sdan ** use the actual amount of space allocated for the hash table (which
12338d07304Sdan ** may be larger than the requested amount).
124a1644fd8Sdanielk1977 */
1258a1e594cSdrh sqlite3BeginBenignMalloc();
12638d07304Sdan new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
1278a1e594cSdrh sqlite3EndBenignMalloc();
128643167ffSdrh
1298a1e594cSdrh if( new_ht==0 ) return 0;
13041eb9e99Sdrh sqlite3_free(pH->ht);
131beae3194Sdrh pH->ht = new_ht;
1328a1e594cSdrh pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
13338d07304Sdan memset(new_ht, 0, new_size*sizeof(struct _ht));
134beae3194Sdrh for(elem=pH->first, pH->first=0; elem; elem = next_elem){
135acbcb7e0Sdrh unsigned int h = strHash(elem->pKey) % new_size;
136beae3194Sdrh next_elem = elem->next;
137e8cf2cacSdrh insertElement(pH, &new_ht[h], elem);
138beae3194Sdrh }
1398a1e594cSdrh return 1;
140beae3194Sdrh }
141beae3194Sdrh
142beae3194Sdrh /* This function (for internal use only) locates an element in an
143a35d8518Sdrh ** hash table that matches the given key. If no element is found,
144a35d8518Sdrh ** a pointer to a static null element with HashElem.data==0 is returned.
145a35d8518Sdrh ** If pH is not NULL, then the hash for this key is written to *pH.
146beae3194Sdrh */
findElementWithHash(const Hash * pH,const char * pKey,unsigned int * pHash)147acbcb7e0Sdrh static HashElem *findElementWithHash(
148beae3194Sdrh const Hash *pH, /* The pH to be searched */
149e61922a6Sdrh const char *pKey, /* The key we are searching for */
150acbcb7e0Sdrh unsigned int *pHash /* Write the hash value here */
151beae3194Sdrh ){
152beae3194Sdrh HashElem *elem; /* Used to loop thru the element list */
153*f6ad201aSdrh unsigned int count; /* Number of elements left to test */
154acbcb7e0Sdrh unsigned int h; /* The computed hash */
155a35d8518Sdrh static HashElem nullElement = { 0, 0, 0, 0 };
156beae3194Sdrh
15775ab50ceSdrh if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
158acbcb7e0Sdrh struct _ht *pEntry;
159acbcb7e0Sdrh h = strHash(pKey) % pH->htsize;
160acbcb7e0Sdrh pEntry = &pH->ht[h];
161e8cf2cacSdrh elem = pEntry->chain;
162e8cf2cacSdrh count = pEntry->count;
1638a1e594cSdrh }else{
164acbcb7e0Sdrh h = 0;
1658a1e594cSdrh elem = pH->first;
1668a1e594cSdrh count = pH->count;
1678a1e594cSdrh }
168a35d8518Sdrh if( pHash ) *pHash = h;
169acbcb7e0Sdrh while( count-- ){
170acbcb7e0Sdrh assert( elem!=0 );
171acbcb7e0Sdrh if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
172beae3194Sdrh return elem;
173beae3194Sdrh }
174beae3194Sdrh elem = elem->next;
175beae3194Sdrh }
176a35d8518Sdrh return &nullElement;
177beae3194Sdrh }
178beae3194Sdrh
17981a20f21Sdrh /* Remove a single entry from the hash table given a pointer to that
180beae3194Sdrh ** element and a hash on the element's key.
181beae3194Sdrh */
removeElementGivenHash(Hash * pH,HashElem * elem,unsigned int h)182beae3194Sdrh static void removeElementGivenHash(
183beae3194Sdrh Hash *pH, /* The pH containing "elem" */
184beae3194Sdrh HashElem* elem, /* The element to be removed from the pH */
1858a1e594cSdrh unsigned int h /* Hash value for the element */
186beae3194Sdrh ){
187e8cf2cacSdrh struct _ht *pEntry;
188beae3194Sdrh if( elem->prev ){
189beae3194Sdrh elem->prev->next = elem->next;
190beae3194Sdrh }else{
191beae3194Sdrh pH->first = elem->next;
192beae3194Sdrh }
193beae3194Sdrh if( elem->next ){
194beae3194Sdrh elem->next->prev = elem->prev;
195beae3194Sdrh }
1968a1e594cSdrh if( pH->ht ){
197e8cf2cacSdrh pEntry = &pH->ht[h];
198e8cf2cacSdrh if( pEntry->chain==elem ){
199e8cf2cacSdrh pEntry->chain = elem->next;
200beae3194Sdrh }
201*f6ad201aSdrh assert( pEntry->count>0 );
202e8cf2cacSdrh pEntry->count--;
203beae3194Sdrh }
20417435752Sdrh sqlite3_free( elem );
205beae3194Sdrh pH->count--;
2065f070c7bSmistachkin if( pH->count==0 ){
207762e584eSdrh assert( pH->first==0 );
208762e584eSdrh assert( pH->count==0 );
209762e584eSdrh sqlite3HashClear(pH);
210762e584eSdrh }
211beae3194Sdrh }
212beae3194Sdrh
213aacc543eSdrh /* Attempt to locate an element of the hash table pH with a key
214acbcb7e0Sdrh ** that matches pKey. Return the data for this element if it is
2157c836f06Sdanielk1977 ** found, or NULL if there is no match.
2167c836f06Sdanielk1977 */
sqlite3HashFind(const Hash * pH,const char * pKey)217acbcb7e0Sdrh void *sqlite3HashFind(const Hash *pH, const char *pKey){
2188a1e594cSdrh assert( pH!=0 );
2198a1e594cSdrh assert( pKey!=0 );
220a35d8518Sdrh return findElementWithHash(pH, pKey, 0)->data;
221beae3194Sdrh }
222beae3194Sdrh
223acbcb7e0Sdrh /* Insert an element into the hash table pH. The key is pKey
22481a20f21Sdrh ** and the data is "data".
225beae3194Sdrh **
22681a20f21Sdrh ** If no element exists with a matching key, then a new
227e61922a6Sdrh ** element is created and NULL is returned.
228beae3194Sdrh **
229beae3194Sdrh ** If another element already exists with the same key, then the
230beae3194Sdrh ** new data replaces the old data and the old data is returned.
2316d4abfbeSdrh ** The key is not copied in this instance. If a malloc fails, then
232aacc543eSdrh ** the new data is returned and the hash table is unchanged.
233beae3194Sdrh **
234beae3194Sdrh ** If the "data" parameter to this function is NULL, then the
23581a20f21Sdrh ** element corresponding to "key" is removed from the hash table.
236beae3194Sdrh */
sqlite3HashInsert(Hash * pH,const char * pKey,void * data)237acbcb7e0Sdrh void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){
2388a1e594cSdrh unsigned int h; /* the hash of the key modulo hash table size */
239beae3194Sdrh HashElem *elem; /* Used to loop thru the element list */
240beae3194Sdrh HashElem *new_elem; /* New element added to the pH */
241beae3194Sdrh
242beae3194Sdrh assert( pH!=0 );
2438a1e594cSdrh assert( pKey!=0 );
244acbcb7e0Sdrh elem = findElementWithHash(pH,pKey,&h);
245a35d8518Sdrh if( elem->data ){
246beae3194Sdrh void *old_data = elem->data;
247beae3194Sdrh if( data==0 ){
248beae3194Sdrh removeElementGivenHash(pH,elem,h);
249beae3194Sdrh }else{
250beae3194Sdrh elem->data = data;
251e61922a6Sdrh elem->pKey = pKey;
252beae3194Sdrh }
253beae3194Sdrh return old_data;
254beae3194Sdrh }
255beae3194Sdrh if( data==0 ) return 0;
256e5ae5735Sdrh new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
2576d4abfbeSdrh if( new_elem==0 ) return data;
258e61922a6Sdrh new_elem->pKey = pKey;
259beae3194Sdrh new_elem->data = data;
2608a1e594cSdrh pH->count++;
2618a1e594cSdrh if( pH->count>=10 && pH->count > 2*pH->htsize ){
262782b873bSdrh if( rehash(pH, pH->count*2) ){
263782b873bSdrh assert( pH->htsize>0 );
264acbcb7e0Sdrh h = strHash(pKey) % pH->htsize;
2658a1e594cSdrh }
2668a1e594cSdrh }
267acbcb7e0Sdrh insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
268beae3194Sdrh return 0;
269beae3194Sdrh }
270