blob: 0ac54196cb39ebac28dcbed4c7ec74f77b155366 [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**
drh2ce1a6e2002-05-21 23:44:30 +000015** $Id: hash.c,v 1.8 2002/05/21 23:44:30 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*/
32void sqliteHashInit(Hash *new, int keyClass, int copyKey){
33 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*/
48void sqliteHashClear(Hash *pH){
49 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
68/*
69** Hash and comparison functions when the mode is SQLITE_HASH_INT
70*/
71static int intHash(const void *pKey, int nKey){
72 return nKey ^ (nKey<<8) ^ (nKey>>8);
73}
74static int intCompare(const void *pKey1, int n1, const void *pKey2, int n2){
75 return n2 - n1;
76}
77
78/*
79** Hash and comparison functions when the mode is SQLITE_HASH_POINTER
80*/
81static int ptrHash(const void *pKey, int nKey){
drh5a2c2c22001-11-21 02:21:11 +000082 uptr x = Addr(pKey);
83 return x ^ (x<<8) ^ (x>>8);
drhbeae3192001-09-22 18:12:08 +000084}
85static int ptrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
drh5a2c2c22001-11-21 02:21:11 +000086 if( pKey1==pKey2 ) return 0;
87 if( pKey1<pKey2 ) return -1;
88 return 1;
drhbeae3192001-09-22 18:12:08 +000089}
90
91/*
92** Hash and comparison functions when the mode is SQLITE_HASH_STRING
93*/
94static int strHash(const void *pKey, int nKey){
95 return sqliteHashNoCase((const char*)pKey, nKey);
96}
97static int strCompare(const void *pKey1, int n1, const void *pKey2, int n2){
98 if( n1!=n2 ) return n2-n1;
99 return sqliteStrNICmp((const char*)pKey1,(const char*)pKey2,n1);
100}
101
102/*
103** Hash and comparison functions when the mode is SQLITE_HASH_BINARY
104*/
105static int binHash(const void *pKey, int nKey){
106 int h = 0;
107 const char *z = (const char *)pKey;
108 while( nKey-- > 0 ){
109 h = (h<<3) ^ h ^ *(z++);
110 }
111 if( h<0 ) h = -h;
112 return h;
113}
114static int binCompare(const void *pKey1, int n1, const void *pKey2, int n2){
115 if( n1!=n2 ) return n2-n1;
116 return memcmp(pKey1,pKey2,n1);
117}
118
119/*
120** Return a pointer to the appropriate hash function given the key class.
drhaacc5432002-01-06 17:07:40 +0000121**
122** The C syntax in this function definition may be unfamilar to some
123** programmers, so we provide the following additional explanation:
124**
125** The name of the function is "hashFunction". The function takes a
126** single parameter "keyClass". The return value of hashFunction()
127** is a pointer to another function. Specifically, the return value
128** of hashFunction() is a pointer to a function that takes two parameters
129** with types "const void*" and "int" and returns an "int".
drhbeae3192001-09-22 18:12:08 +0000130*/
131static int (*hashFunction(int keyClass))(const void*,int){
132 switch( keyClass ){
drh1ab43002002-01-14 09:28:19 +0000133 case SQLITE_HASH_INT: return &intHash;
134 case SQLITE_HASH_POINTER: return &ptrHash;
135 case SQLITE_HASH_STRING: return &strHash;
136 case SQLITE_HASH_BINARY: return &binHash;;
drhbeae3192001-09-22 18:12:08 +0000137 default: break;
138 }
139 return 0;
140}
141
142/*
143** Return a pointer to the appropriate hash function given the key class.
drhaacc5432002-01-06 17:07:40 +0000144**
145** For help in interpreted the obscure C code in the function definition,
146** see the header comment on the previous function.
drhbeae3192001-09-22 18:12:08 +0000147*/
148static int (*compareFunction(int keyClass))(const void*,int,const void*,int){
149 switch( keyClass ){
drh1ab43002002-01-14 09:28:19 +0000150 case SQLITE_HASH_INT: return &intCompare;
151 case SQLITE_HASH_POINTER: return &ptrCompare;
152 case SQLITE_HASH_STRING: return &strCompare;
153 case SQLITE_HASH_BINARY: return &binCompare;
drhbeae3192001-09-22 18:12:08 +0000154 default: break;
155 }
156 return 0;
157}
158
159
drhaacc5432002-01-06 17:07:40 +0000160/* Resize the hash table so that it cantains "new_size" buckets.
161** "new_size" must be a power of 2. The hash table might fail
162** to resize if sqliteMalloc() fails.
drhbeae3192001-09-22 18:12:08 +0000163*/
164static void rehash(Hash *pH, int new_size){
165 struct _ht *new_ht; /* The new hash table */
166 HashElem *elem, *next_elem; /* For looping over existing elements */
167 HashElem *x; /* Element being copied to new hash table */
168 int (*xHash)(const void*,int); /* The hash function */
169
170 assert( (new_size & (new_size-1))==0 );
171 new_ht = (struct _ht *)sqliteMalloc( new_size*sizeof(struct _ht) );
172 if( new_ht==0 ) return;
173 if( pH->ht ) sqliteFree(pH->ht);
174 pH->ht = new_ht;
175 pH->htsize = new_size;
176 xHash = hashFunction(pH->keyClass);
177 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
178 int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
179 next_elem = elem->next;
180 x = new_ht[h].chain;
181 if( x ){
182 elem->next = x;
183 elem->prev = x->prev;
184 if( x->prev ) x->prev->next = elem;
185 else pH->first = elem;
186 x->prev = elem;
187 }else{
188 elem->next = pH->first;
189 if( pH->first ) pH->first->prev = elem;
190 elem->prev = 0;
191 pH->first = elem;
192 }
193 new_ht[h].chain = elem;
194 new_ht[h].count++;
195 }
196}
197
198/* This function (for internal use only) locates an element in an
drhaacc5432002-01-06 17:07:40 +0000199** hash table that matches the given key. The hash for this key has
200** already been computed and is passed as the 4th parameter.
drhbeae3192001-09-22 18:12:08 +0000201*/
202static HashElem *findElementGivenHash(
203 const Hash *pH, /* The pH to be searched */
204 const void *pKey, /* The key we are searching for */
205 int nKey,
206 int h /* The hash for this key. */
207){
208 HashElem *elem; /* Used to loop thru the element list */
209 int count; /* Number of elements left to test */
210 int (*xCompare)(const void*,int,const void*,int); /* comparison function */
211
212 if( pH->ht ){
213 elem = pH->ht[h].chain;
214 count = pH->ht[h].count;
215 xCompare = compareFunction(pH->keyClass);
216 while( count-- && elem ){
217 if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
218 return elem;
219 }
220 elem = elem->next;
221 }
222 }
223 return 0;
224}
225
drh81a20f22001-10-12 17:30:04 +0000226/* Remove a single entry from the hash table given a pointer to that
drhbeae3192001-09-22 18:12:08 +0000227** element and a hash on the element's key.
228*/
229static void removeElementGivenHash(
230 Hash *pH, /* The pH containing "elem" */
231 HashElem* elem, /* The element to be removed from the pH */
drhaacc5432002-01-06 17:07:40 +0000232 int h /* Hash value for the element */
drhbeae3192001-09-22 18:12:08 +0000233){
234 if( elem->prev ){
235 elem->prev->next = elem->next;
236 }else{
237 pH->first = elem->next;
238 }
239 if( elem->next ){
240 elem->next->prev = elem->prev;
241 }
242 if( pH->ht[h].chain==elem ){
243 pH->ht[h].chain = elem->next;
244 }
245 pH->ht[h].count--;
246 if( pH->ht[h].count<=0 ){
247 pH->ht[h].chain = 0;
248 }
249 if( pH->copyKey && elem->pKey ){
250 sqliteFree(elem->pKey);
251 }
252 sqliteFree( elem );
253 pH->count--;
254}
255
drhaacc5432002-01-06 17:07:40 +0000256/* Attempt to locate an element of the hash table pH with a key
drh81a20f22001-10-12 17:30:04 +0000257** that matches pKey,nKey. Return the data for this element if it is
drhaacc5432002-01-06 17:07:40 +0000258** found, or NULL if there is no match.
drhbeae3192001-09-22 18:12:08 +0000259*/
260void *sqliteHashFind(const Hash *pH, const void *pKey, int nKey){
261 int h; /* A hash on key */
262 HashElem *elem; /* The element that matches key */
263 int (*xHash)(const void*,int); /* The hash function */
264
265 if( pH==0 || pH->ht==0 ) return 0;
266 xHash = hashFunction(pH->keyClass);
267 assert( xHash!=0 );
268 h = (*xHash)(pKey,nKey);
269 assert( (pH->htsize & (pH->htsize-1))==0 );
270 elem = findElementGivenHash(pH,pKey,nKey, h & (pH->htsize-1));
271 return elem ? elem->data : 0;
272}
273
drh81a20f22001-10-12 17:30:04 +0000274/* Insert an element into the hash table pH. The key is pKey,nKey
275** and the data is "data".
drhbeae3192001-09-22 18:12:08 +0000276**
drh81a20f22001-10-12 17:30:04 +0000277** If no element exists with a matching key, then a new
278** element is created. A copy of the key is made if the copyKey
279** flag is set. NULL is returned.
drhbeae3192001-09-22 18:12:08 +0000280**
281** If another element already exists with the same key, then the
282** new data replaces the old data and the old data is returned.
drh6d4abfb2001-10-22 02:58:08 +0000283** The key is not copied in this instance. If a malloc fails, then
drhaacc5432002-01-06 17:07:40 +0000284** the new data is returned and the hash table is unchanged.
drhbeae3192001-09-22 18:12:08 +0000285**
286** If the "data" parameter to this function is NULL, then the
drh81a20f22001-10-12 17:30:04 +0000287** element corresponding to "key" is removed from the hash table.
drhbeae3192001-09-22 18:12:08 +0000288*/
drh8e0a2f92002-02-23 23:45:45 +0000289void *sqliteHashInsert(Hash *pH, const void *pKey, int nKey, void *data){
drhbeae3192001-09-22 18:12:08 +0000290 int hraw; /* Raw hash value of the key */
291 int h; /* the hash of the key modulo hash table size */
292 HashElem *elem; /* Used to loop thru the element list */
293 HashElem *new_elem; /* New element added to the pH */
294 int (*xHash)(const void*,int); /* The hash function */
295
296 assert( pH!=0 );
297 xHash = hashFunction(pH->keyClass);
298 assert( xHash!=0 );
299 hraw = (*xHash)(pKey, nKey);
300 assert( (pH->htsize & (pH->htsize-1))==0 );
301 h = hraw & (pH->htsize-1);
302 elem = findElementGivenHash(pH,pKey,nKey,h);
303 if( elem ){
304 void *old_data = elem->data;
305 if( data==0 ){
306 removeElementGivenHash(pH,elem,h);
307 }else{
308 elem->data = data;
309 }
310 return old_data;
311 }
312 if( data==0 ) return 0;
313 new_elem = (HashElem*)sqliteMalloc( sizeof(HashElem) );
drh6d4abfb2001-10-22 02:58:08 +0000314 if( new_elem==0 ) return data;
drhbeae3192001-09-22 18:12:08 +0000315 if( pH->copyKey && pKey!=0 ){
316 new_elem->pKey = sqliteMalloc( nKey );
317 if( new_elem->pKey==0 ){
318 sqliteFree(new_elem);
drh6d4abfb2001-10-22 02:58:08 +0000319 return data;
drhbeae3192001-09-22 18:12:08 +0000320 }
321 memcpy((void*)new_elem->pKey, pKey, nKey);
322 }else{
drh2ce1a6e2002-05-21 23:44:30 +0000323 new_elem->pKey = (void*)pKey;
drhbeae3192001-09-22 18:12:08 +0000324 }
325 new_elem->nKey = nKey;
326 pH->count++;
327 if( pH->htsize==0 ) rehash(pH,8);
328 if( pH->htsize==0 ){
329 pH->count = 0;
330 sqliteFree(new_elem);
drh6d4abfb2001-10-22 02:58:08 +0000331 return data;
drhbeae3192001-09-22 18:12:08 +0000332 }
333 if( pH->count > pH->htsize ){
334 rehash(pH,pH->htsize*2);
335 }
336 assert( (pH->htsize & (pH->htsize-1))==0 );
337 h = hraw & (pH->htsize-1);
338 elem = pH->ht[h].chain;
339 if( elem ){
340 new_elem->next = elem;
341 new_elem->prev = elem->prev;
342 if( elem->prev ){ elem->prev->next = new_elem; }
343 else { pH->first = new_elem; }
344 elem->prev = new_elem;
345 }else{
346 new_elem->next = pH->first;
347 new_elem->prev = 0;
348 if( pH->first ){ pH->first->prev = new_elem; }
349 pH->first = new_elem;
350 }
351 pH->ht[h].count++;
352 pH->ht[h].chain = new_elem;
353 new_elem->data = data;
354 return 0;
355}