xref: /titanic_41/usr/src/lib/libsqlite/src/hash.c (revision c5c4113dfcabb1eed3d4bdf7609de5170027a794)
1 
2 #pragma ident	"%Z%%M%	%I%	%E% SMI"
3 
4 /*
5 ** 2001 September 22
6 **
7 ** The author disclaims copyright to this source code.  In place of
8 ** a legal notice, here is a blessing:
9 **
10 **    May you do good and not evil.
11 **    May you find forgiveness for yourself and forgive others.
12 **    May you share freely, never taking more than you give.
13 **
14 *************************************************************************
15 ** This is the implementation of generic hash-tables
16 ** used in SQLite.
17 **
18 ** $Id: hash.c,v 1.11 2004/01/08 02:17:33 drh Exp $
19 */
20 #include "sqliteInt.h"
21 #include <assert.h>
22 
23 /* Turn bulk memory into a hash table object by initializing the
24 ** fields of the Hash structure.
25 **
26 ** "new" is a pointer to the hash table that is to be initialized.
27 ** keyClass is one of the constants SQLITE_HASH_INT, SQLITE_HASH_POINTER,
28 ** SQLITE_HASH_BINARY, or SQLITE_HASH_STRING.  The value of keyClass
29 ** determines what kind of key the hash table will use.  "copyKey" is
30 ** true if the hash table should make its own private copy of keys and
31 ** false if it should just use the supplied pointer.  CopyKey only makes
32 ** sense for SQLITE_HASH_STRING and SQLITE_HASH_BINARY and is ignored
33 ** for other key classes.
34 */
sqliteHashInit(Hash * new,int keyClass,int copyKey)35 void sqliteHashInit(Hash *new, int keyClass, int copyKey){
36   assert( new!=0 );
37   assert( keyClass>=SQLITE_HASH_INT && keyClass<=SQLITE_HASH_BINARY );
38   new->keyClass = keyClass;
39   new->copyKey = copyKey &&
40                 (keyClass==SQLITE_HASH_STRING || keyClass==SQLITE_HASH_BINARY);
41   new->first = 0;
42   new->count = 0;
43   new->htsize = 0;
44   new->ht = 0;
45 }
46 
47 /* Remove all entries from a hash table.  Reclaim all memory.
48 ** Call this routine to delete a hash table or to reset a hash table
49 ** to the empty state.
50 */
sqliteHashClear(Hash * pH)51 void sqliteHashClear(Hash *pH){
52   HashElem *elem;         /* For looping over all elements of the table */
53 
54   assert( pH!=0 );
55   elem = pH->first;
56   pH->first = 0;
57   if( pH->ht ) sqliteFree(pH->ht);
58   pH->ht = 0;
59   pH->htsize = 0;
60   while( elem ){
61     HashElem *next_elem = elem->next;
62     if( pH->copyKey && elem->pKey ){
63       sqliteFree(elem->pKey);
64     }
65     sqliteFree(elem);
66     elem = next_elem;
67   }
68   pH->count = 0;
69 }
70 
71 /*
72 ** Hash and comparison functions when the mode is SQLITE_HASH_INT
73 */
intHash(const void * pKey,int nKey)74 static int intHash(const void *pKey, int nKey){
75   return nKey ^ (nKey<<8) ^ (nKey>>8);
76 }
intCompare(const void * pKey1,int n1,const void * pKey2,int n2)77 static int intCompare(const void *pKey1, int n1, const void *pKey2, int n2){
78   return n2 - n1;
79 }
80 
81 #if 0 /* NOT USED */
82 /*
83 ** Hash and comparison functions when the mode is SQLITE_HASH_POINTER
84 */
85 static int ptrHash(const void *pKey, int nKey){
86   uptr x = Addr(pKey);
87   return x ^ (x<<8) ^ (x>>8);
88 }
89 static int ptrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
90   if( pKey1==pKey2 ) return 0;
91   if( pKey1<pKey2 ) return -1;
92   return 1;
93 }
94 #endif
95 
96 /*
97 ** Hash and comparison functions when the mode is SQLITE_HASH_STRING
98 */
strHash(const void * pKey,int nKey)99 static int strHash(const void *pKey, int nKey){
100   return sqliteHashNoCase((const char*)pKey, nKey);
101 }
strCompare(const void * pKey1,int n1,const void * pKey2,int n2)102 static int strCompare(const void *pKey1, int n1, const void *pKey2, int n2){
103   if( n1!=n2 ) return n2-n1;
104   return sqliteStrNICmp((const char*)pKey1,(const char*)pKey2,n1);
105 }
106 
107 /*
108 ** Hash and comparison functions when the mode is SQLITE_HASH_BINARY
109 */
binHash(const void * pKey,int nKey)110 static int binHash(const void *pKey, int nKey){
111   int h = 0;
112   const char *z = (const char *)pKey;
113   while( nKey-- > 0 ){
114     h = (h<<3) ^ h ^ *(z++);
115   }
116   return h & 0x7fffffff;
117 }
binCompare(const void * pKey1,int n1,const void * pKey2,int n2)118 static int binCompare(const void *pKey1, int n1, const void *pKey2, int n2){
119   if( n1!=n2 ) return n2-n1;
120   return memcmp(pKey1,pKey2,n1);
121 }
122 
123 /*
124 ** Return a pointer to the appropriate hash function given the key class.
125 **
126 ** The C syntax in this function definition may be unfamilar to some
127 ** programmers, so we provide the following additional explanation:
128 **
129 ** The name of the function is "hashFunction".  The function takes a
130 ** single parameter "keyClass".  The return value of hashFunction()
131 ** is a pointer to another function.  Specifically, the return value
132 ** of hashFunction() is a pointer to a function that takes two parameters
133 ** with types "const void*" and "int" and returns an "int".
134 */
hashFunction(int keyClass)135 static int (*hashFunction(int keyClass))(const void*,int){
136   switch( keyClass ){
137     case SQLITE_HASH_INT:     return &intHash;
138     /* case SQLITE_HASH_POINTER: return &ptrHash; // NOT USED */
139     case SQLITE_HASH_STRING:  return &strHash;
140     case SQLITE_HASH_BINARY:  return &binHash;;
141     default: break;
142   }
143   return 0;
144 }
145 
146 /*
147 ** Return a pointer to the appropriate hash function given the key class.
148 **
149 ** For help in interpreted the obscure C code in the function definition,
150 ** see the header comment on the previous function.
151 */
compareFunction(int keyClass)152 static int (*compareFunction(int keyClass))(const void*,int,const void*,int){
153   switch( keyClass ){
154     case SQLITE_HASH_INT:     return &intCompare;
155     /* case SQLITE_HASH_POINTER: return &ptrCompare; // NOT USED */
156     case SQLITE_HASH_STRING:  return &strCompare;
157     case SQLITE_HASH_BINARY:  return &binCompare;
158     default: break;
159   }
160   return 0;
161 }
162 
163 
164 /* Resize the hash table so that it cantains "new_size" buckets.
165 ** "new_size" must be a power of 2.  The hash table might fail
166 ** to resize if sqliteMalloc() fails.
167 */
rehash(Hash * pH,int new_size)168 static void rehash(Hash *pH, int new_size){
169   struct _ht *new_ht;            /* The new hash table */
170   HashElem *elem, *next_elem;    /* For looping over existing elements */
171   HashElem *x;                   /* Element being copied to new hash table */
172   int (*xHash)(const void*,int); /* The hash function */
173 
174   assert( (new_size & (new_size-1))==0 );
175   new_ht = (struct _ht *)sqliteMalloc( new_size*sizeof(struct _ht) );
176   if( new_ht==0 ) return;
177   if( pH->ht ) sqliteFree(pH->ht);
178   pH->ht = new_ht;
179   pH->htsize = new_size;
180   xHash = hashFunction(pH->keyClass);
181   for(elem=pH->first, pH->first=0; elem; elem = next_elem){
182     int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
183     next_elem = elem->next;
184     x = new_ht[h].chain;
185     if( x ){
186       elem->next = x;
187       elem->prev = x->prev;
188       if( x->prev ) x->prev->next = elem;
189       else          pH->first = elem;
190       x->prev = elem;
191     }else{
192       elem->next = pH->first;
193       if( pH->first ) pH->first->prev = elem;
194       elem->prev = 0;
195       pH->first = elem;
196     }
197     new_ht[h].chain = elem;
198     new_ht[h].count++;
199   }
200 }
201 
202 /* This function (for internal use only) locates an element in an
203 ** hash table that matches the given key.  The hash for this key has
204 ** already been computed and is passed as the 4th parameter.
205 */
findElementGivenHash(const Hash * pH,const void * pKey,int nKey,int h)206 static HashElem *findElementGivenHash(
207   const Hash *pH,     /* The pH to be searched */
208   const void *pKey,   /* The key we are searching for */
209   int nKey,
210   int h               /* The hash for this key. */
211 ){
212   HashElem *elem;                /* Used to loop thru the element list */
213   int count;                     /* Number of elements left to test */
214   int (*xCompare)(const void*,int,const void*,int);  /* comparison function */
215 
216   if( pH->ht ){
217     elem = pH->ht[h].chain;
218     count = pH->ht[h].count;
219     xCompare = compareFunction(pH->keyClass);
220     while( count-- && elem ){
221       if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
222         return elem;
223       }
224       elem = elem->next;
225     }
226   }
227   return 0;
228 }
229 
230 /* Remove a single entry from the hash table given a pointer to that
231 ** element and a hash on the element's key.
232 */
removeElementGivenHash(Hash * pH,HashElem * elem,int h)233 static void removeElementGivenHash(
234   Hash *pH,         /* The pH containing "elem" */
235   HashElem* elem,   /* The element to be removed from the pH */
236   int h             /* Hash value for the element */
237 ){
238   if( elem->prev ){
239     elem->prev->next = elem->next;
240   }else{
241     pH->first = elem->next;
242   }
243   if( elem->next ){
244     elem->next->prev = elem->prev;
245   }
246   if( pH->ht[h].chain==elem ){
247     pH->ht[h].chain = elem->next;
248   }
249   pH->ht[h].count--;
250   if( pH->ht[h].count<=0 ){
251     pH->ht[h].chain = 0;
252   }
253   if( pH->copyKey && elem->pKey ){
254     sqliteFree(elem->pKey);
255   }
256   sqliteFree( elem );
257   pH->count--;
258 }
259 
260 /* Attempt to locate an element of the hash table pH with a key
261 ** that matches pKey,nKey.  Return the data for this element if it is
262 ** found, or NULL if there is no match.
263 */
sqliteHashFind(const Hash * pH,const void * pKey,int nKey)264 void *sqliteHashFind(const Hash *pH, const void *pKey, int nKey){
265   int h;             /* A hash on key */
266   HashElem *elem;    /* The element that matches key */
267   int (*xHash)(const void*,int);  /* The hash function */
268 
269   if( pH==0 || pH->ht==0 ) return 0;
270   xHash = hashFunction(pH->keyClass);
271   assert( xHash!=0 );
272   h = (*xHash)(pKey,nKey);
273   assert( (pH->htsize & (pH->htsize-1))==0 );
274   elem = findElementGivenHash(pH,pKey,nKey, h & (pH->htsize-1));
275   return elem ? elem->data : 0;
276 }
277 
278 /* Insert an element into the hash table pH.  The key is pKey,nKey
279 ** and the data is "data".
280 **
281 ** If no element exists with a matching key, then a new
282 ** element is created.  A copy of the key is made if the copyKey
283 ** flag is set.  NULL is returned.
284 **
285 ** If another element already exists with the same key, then the
286 ** new data replaces the old data and the old data is returned.
287 ** The key is not copied in this instance.  If a malloc fails, then
288 ** the new data is returned and the hash table is unchanged.
289 **
290 ** If the "data" parameter to this function is NULL, then the
291 ** element corresponding to "key" is removed from the hash table.
292 */
sqliteHashInsert(Hash * pH,const void * pKey,int nKey,void * data)293 void *sqliteHashInsert(Hash *pH, const void *pKey, int nKey, void *data){
294   int hraw;             /* Raw hash value of the key */
295   int h;                /* the hash of the key modulo hash table size */
296   HashElem *elem;       /* Used to loop thru the element list */
297   HashElem *new_elem;   /* New element added to the pH */
298   int (*xHash)(const void*,int);  /* The hash function */
299 
300   assert( pH!=0 );
301   xHash = hashFunction(pH->keyClass);
302   assert( xHash!=0 );
303   hraw = (*xHash)(pKey, nKey);
304   assert( (pH->htsize & (pH->htsize-1))==0 );
305   h = hraw & (pH->htsize-1);
306   elem = findElementGivenHash(pH,pKey,nKey,h);
307   if( elem ){
308     void *old_data = elem->data;
309     if( data==0 ){
310       removeElementGivenHash(pH,elem,h);
311     }else{
312       elem->data = data;
313     }
314     return old_data;
315   }
316   if( data==0 ) return 0;
317   new_elem = (HashElem*)sqliteMalloc( sizeof(HashElem) );
318   if( new_elem==0 ) return data;
319   if( pH->copyKey && pKey!=0 ){
320     new_elem->pKey = sqliteMallocRaw( nKey );
321     if( new_elem->pKey==0 ){
322       sqliteFree(new_elem);
323       return data;
324     }
325     memcpy((void*)new_elem->pKey, pKey, nKey);
326   }else{
327     new_elem->pKey = (void*)pKey;
328   }
329   new_elem->nKey = nKey;
330   pH->count++;
331   if( pH->htsize==0 ) rehash(pH,8);
332   if( pH->htsize==0 ){
333     pH->count = 0;
334     sqliteFree(new_elem);
335     return data;
336   }
337   if( pH->count > pH->htsize ){
338     rehash(pH,pH->htsize*2);
339   }
340   assert( (pH->htsize & (pH->htsize-1))==0 );
341   h = hraw & (pH->htsize-1);
342   elem = pH->ht[h].chain;
343   if( elem ){
344     new_elem->next = elem;
345     new_elem->prev = elem->prev;
346     if( elem->prev ){ elem->prev->next = new_elem; }
347     else            { pH->first = new_elem; }
348     elem->prev = new_elem;
349   }else{
350     new_elem->next = pH->first;
351     new_elem->prev = 0;
352     if( pH->first ){ pH->first->prev = new_elem; }
353     pH->first = new_elem;
354   }
355   pH->ht[h].count++;
356   pH->ht[h].chain = new_elem;
357   new_elem->data = data;
358   return 0;
359 }
360