4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
12 ** This is the implementation of generic hash-tables used in SQLite.
13 ** We've modified it slightly to serve as a standalone hash table
14 ** implementation for the full-text indexing module.
22 void *malloc_and_zero(int n
){
30 /* Turn bulk memory into a hash table object by initializing the
31 ** fields of the Hash structure.
33 ** "pNew" is a pointer to the hash table that is to be initialized.
34 ** keyClass is one of the constants HASH_INT, HASH_POINTER,
35 ** HASH_BINARY, or HASH_STRING. The value of keyClass
36 ** determines what kind of key the hash table will use. "copyKey" is
37 ** true if the hash table should make its own private copy of keys and
38 ** false if it should just use the supplied pointer. CopyKey only makes
39 ** sense for HASH_STRING and HASH_BINARY and is ignored
40 ** for other key classes.
42 void HashInit(Hash
*pNew
, int keyClass
, int copyKey
){
44 assert( keyClass
>=HASH_STRING
&& keyClass
<=HASH_BINARY
);
45 pNew
->keyClass
= keyClass
;
47 if( keyClass
==HASH_POINTER
|| keyClass
==HASH_INT
) copyKey
= 0;
49 pNew
->copyKey
= copyKey
;
54 pNew
->xMalloc
= malloc_and_zero
;
58 /* Remove all entries from a hash table. Reclaim all memory.
59 ** Call this routine to delete a hash table or to reset a hash table
60 ** to the empty state.
62 void HashClear(Hash
*pH
){
63 HashElem
*elem
; /* For looping over all elements of the table */
68 if( pH
->ht
) pH
->xFree(pH
->ht
);
72 HashElem
*next_elem
= elem
->next
;
73 if( pH
->copyKey
&& elem
->pKey
){
74 pH
->xFree(elem
->pKey
);
84 ** Hash and comparison functions when the mode is HASH_INT
86 static int intHash(const void *pKey
, int nKey
){
87 return nKey
^ (nKey
<<8) ^ (nKey
>>8);
89 static int intCompare(const void *pKey1
, int n1
, const void *pKey2
, int n2
){
96 ** Hash and comparison functions when the mode is HASH_POINTER
98 static int ptrHash(const void *pKey
, int nKey
){
100 return x
^ (x
<<8) ^ (x
>>8);
102 static int ptrCompare(const void *pKey1
, int n1
, const void *pKey2
, int n2
){
103 if( pKey1
==pKey2
) return 0;
104 if( pKey1
<pKey2
) return -1;
110 ** Hash and comparison functions when the mode is HASH_STRING
112 static int strHash(const void *pKey
, int nKey
){
113 const char *z
= (const char *)pKey
;
115 if( nKey
<=0 ) nKey
= (int) strlen(z
);
117 h
= (h
<<3) ^ h
^ *z
++;
120 return h
& 0x7fffffff;
122 static int strCompare(const void *pKey1
, int n1
, const void *pKey2
, int n2
){
123 if( n1
!=n2
) return 1;
124 return strncmp((const char*)pKey1
,(const char*)pKey2
,n1
);
128 ** Hash and comparison functions when the mode is HASH_BINARY
130 static int binHash(const void *pKey
, int nKey
){
132 const char *z
= (const char *)pKey
;
134 h
= (h
<<3) ^ h
^ *(z
++);
136 return h
& 0x7fffffff;
138 static int binCompare(const void *pKey1
, int n1
, const void *pKey2
, int n2
){
139 if( n1
!=n2
) return 1;
140 return memcmp(pKey1
,pKey2
,n1
);
144 ** Return a pointer to the appropriate hash function given the key class.
146 ** The C syntax in this function definition may be unfamilar to some
147 ** programmers, so we provide the following additional explanation:
149 ** The name of the function is "hashFunction". The function takes a
150 ** single parameter "keyClass". The return value of hashFunction()
151 ** is a pointer to another function. Specifically, the return value
152 ** of hashFunction() is a pointer to a function that takes two parameters
153 ** with types "const void*" and "int" and returns an "int".
155 static int (*hashFunction(int keyClass
))(const void*,int){
156 #if 0 /* HASH_INT and HASH_POINTER are never used */
158 case HASH_INT
: return &intHash
;
159 case HASH_POINTER
: return &ptrHash
;
160 case HASH_STRING
: return &strHash
;
161 case HASH_BINARY
: return &binHash
;;
166 if( keyClass
==HASH_STRING
){
169 assert( keyClass
==HASH_BINARY
);
176 ** Return a pointer to the appropriate hash function given the key class.
178 ** For help in interpreted the obscure C code in the function definition,
179 ** see the header comment on the previous function.
181 static int (*compareFunction(int keyClass
))(const void*,int,const void*,int){
182 #if 0 /* HASH_INT and HASH_POINTER are never used */
184 case HASH_INT
: return &intCompare
;
185 case HASH_POINTER
: return &ptrCompare
;
186 case HASH_STRING
: return &strCompare
;
187 case HASH_BINARY
: return &binCompare
;
192 if( keyClass
==HASH_STRING
){
195 assert( keyClass
==HASH_BINARY
);
201 /* Link an element into the hash table
203 static void insertElement(
204 Hash
*pH
, /* The complete hash table */
205 struct _ht
*pEntry
, /* The entry into which pNew is inserted */
206 HashElem
*pNew
/* The element to be inserted */
208 HashElem
*pHead
; /* First element already in pEntry */
209 pHead
= pEntry
->chain
;
212 pNew
->prev
= pHead
->prev
;
213 if( pHead
->prev
){ pHead
->prev
->next
= pNew
; }
214 else { pH
->first
= pNew
; }
217 pNew
->next
= pH
->first
;
218 if( pH
->first
){ pH
->first
->prev
= pNew
; }
223 pEntry
->chain
= pNew
;
227 /* Resize the hash table so that it cantains "new_size" buckets.
228 ** "new_size" must be a power of 2. The hash table might fail
229 ** to resize if sqliteMalloc() fails.
231 static void rehash(Hash
*pH
, int new_size
){
232 struct _ht
*new_ht
; /* The new hash table */
233 HashElem
*elem
, *next_elem
; /* For looping over existing elements */
234 int (*xHash
)(const void*,int); /* The hash function */
236 assert( (new_size
& (new_size
-1))==0 );
237 new_ht
= (struct _ht
*)pH
->xMalloc( new_size
*sizeof(struct _ht
) );
238 if( new_ht
==0 ) return;
239 if( pH
->ht
) pH
->xFree(pH
->ht
);
241 pH
->htsize
= new_size
;
242 xHash
= hashFunction(pH
->keyClass
);
243 for(elem
=pH
->first
, pH
->first
=0; elem
; elem
= next_elem
){
244 int h
= (*xHash
)(elem
->pKey
, elem
->nKey
) & (new_size
-1);
245 next_elem
= elem
->next
;
246 insertElement(pH
, &new_ht
[h
], elem
);
250 /* This function (for internal use only) locates an element in an
251 ** hash table that matches the given key. The hash for this key has
252 ** already been computed and is passed as the 4th parameter.
254 static HashElem
*findElementGivenHash(
255 const Hash
*pH
, /* The pH to be searched */
256 const void *pKey
, /* The key we are searching for */
258 int h
/* The hash for this key. */
260 HashElem
*elem
; /* Used to loop thru the element list */
261 int count
; /* Number of elements left to test */
262 int (*xCompare
)(const void*,int,const void*,int); /* comparison function */
265 struct _ht
*pEntry
= &pH
->ht
[h
];
266 elem
= pEntry
->chain
;
267 count
= pEntry
->count
;
268 xCompare
= compareFunction(pH
->keyClass
);
269 while( count
-- && elem
){
270 if( (*xCompare
)(elem
->pKey
,elem
->nKey
,pKey
,nKey
)==0 ){
279 /* Remove a single entry from the hash table given a pointer to that
280 ** element and a hash on the element's key.
282 static void removeElementGivenHash(
283 Hash
*pH
, /* The pH containing "elem" */
284 HashElem
* elem
, /* The element to be removed from the pH */
285 int h
/* Hash value for the element */
289 elem
->prev
->next
= elem
->next
;
291 pH
->first
= elem
->next
;
294 elem
->next
->prev
= elem
->prev
;
297 if( pEntry
->chain
==elem
){
298 pEntry
->chain
= elem
->next
;
301 if( pEntry
->count
<=0 ){
304 if( pH
->copyKey
&& elem
->pKey
){
305 pH
->xFree(elem
->pKey
);
310 assert( pH
->first
==0 );
311 assert( pH
->count
==0 );
316 /* Attempt to locate an element of the hash table pH with a key
317 ** that matches pKey,nKey. Return the data for this element if it is
318 ** found, or NULL if there is no match.
320 void *HashFind(const Hash
*pH
, const void *pKey
, int nKey
){
321 int h
; /* A hash on key */
322 HashElem
*elem
; /* The element that matches key */
323 int (*xHash
)(const void*,int); /* The hash function */
325 if( pH
==0 || pH
->ht
==0 ) return 0;
326 xHash
= hashFunction(pH
->keyClass
);
328 h
= (*xHash
)(pKey
,nKey
);
329 assert( (pH
->htsize
& (pH
->htsize
-1))==0 );
330 elem
= findElementGivenHash(pH
,pKey
,nKey
, h
& (pH
->htsize
-1));
331 return elem
? elem
->data
: 0;
334 /* Insert an element into the hash table pH. The key is pKey,nKey
335 ** and the data is "data".
337 ** If no element exists with a matching key, then a new
338 ** element is created. A copy of the key is made if the copyKey
339 ** flag is set. NULL is returned.
341 ** If another element already exists with the same key, then the
342 ** new data replaces the old data and the old data is returned.
343 ** The key is not copied in this instance. If a malloc fails, then
344 ** the new data is returned and the hash table is unchanged.
346 ** If the "data" parameter to this function is NULL, then the
347 ** element corresponding to "key" is removed from the hash table.
349 void *HashInsert(Hash
*pH
, const void *pKey
, int nKey
, void *data
){
350 int hraw
; /* Raw hash value of the key */
351 int h
; /* the hash of the key modulo hash table size */
352 HashElem
*elem
; /* Used to loop thru the element list */
353 HashElem
*new_elem
; /* New element added to the pH */
354 int (*xHash
)(const void*,int); /* The hash function */
357 xHash
= hashFunction(pH
->keyClass
);
359 hraw
= (*xHash
)(pKey
, nKey
);
360 assert( (pH
->htsize
& (pH
->htsize
-1))==0 );
361 h
= hraw
& (pH
->htsize
-1);
362 elem
= findElementGivenHash(pH
,pKey
,nKey
,h
);
364 void *old_data
= elem
->data
;
366 removeElementGivenHash(pH
,elem
,h
);
372 if( data
==0 ) return 0;
373 new_elem
= (HashElem
*)pH
->xMalloc( sizeof(HashElem
) );
374 if( new_elem
==0 ) return data
;
375 if( pH
->copyKey
&& pKey
!=0 ){
376 new_elem
->pKey
= pH
->xMalloc( nKey
);
377 if( new_elem
->pKey
==0 ){
381 memcpy((void*)new_elem
->pKey
, pKey
, nKey
);
383 new_elem
->pKey
= (void*)pKey
;
385 new_elem
->nKey
= nKey
;
395 if( pH
->count
> pH
->htsize
){
396 rehash(pH
,pH
->htsize
*2);
398 assert( pH
->htsize
>0 );
399 assert( (pH
->htsize
& (pH
->htsize
-1))==0 );
400 h
= hraw
& (pH
->htsize
-1);
401 insertElement(pH
, &pH
->ht
[h
], new_elem
);
402 new_elem
->data
= data
;