ds@1952: /* ds@1952: ** 2001 September 22 ds@1952: ** ds@1952: ** The author disclaims copyright to this source code. In place of ds@1952: ** a legal notice, here is a blessing: ds@1952: ** ds@1952: ** May you do good and not evil. ds@1952: ** May you find forgiveness for yourself and forgive others. ds@1952: ** May you share freely, never taking more than you give. ds@1952: ** ds@1952: ************************************************************************* ds@1952: ** This is the implementation of generic hash-tables ds@1952: ** used in SQLite. ds@1952: ** ds@1952: ** $Id$ ds@1952: */ ds@1952: #include "sqliteInt.h" ds@1952: #include ds@1952: ds@1952: /* Turn bulk memory into a hash table object by initializing the ds@1952: ** fields of the Hash structure. ds@1952: ** ds@1952: ** "pNew" is a pointer to the hash table that is to be initialized. ds@1952: ** keyClass is one of the constants SQLITE_HASH_INT, SQLITE_HASH_POINTER, ds@1952: ** SQLITE_HASH_BINARY, or SQLITE_HASH_STRING. The value of keyClass ds@1952: ** determines what kind of key the hash table will use. "copyKey" is ds@1952: ** true if the hash table should make its own private copy of keys and ds@1952: ** false if it should just use the supplied pointer. CopyKey only makes ds@1952: ** sense for SQLITE_HASH_STRING and SQLITE_HASH_BINARY and is ignored ds@1952: ** for other key classes. ds@1952: */ ds@1952: void sqlite3HashInit(Hash *pNew, int keyClass, int copyKey){ ds@1952: assert( pNew!=0 ); ds@1952: assert( keyClass>=SQLITE_HASH_STRING && keyClass<=SQLITE_HASH_BINARY ); ds@1952: pNew->keyClass = keyClass; ds@1952: #if 0 ds@1952: if( keyClass==SQLITE_HASH_POINTER || keyClass==SQLITE_HASH_INT ) copyKey = 0; ds@1952: #endif ds@1952: pNew->copyKey = copyKey; ds@1952: pNew->first = 0; ds@1952: pNew->count = 0; ds@1952: pNew->htsize = 0; ds@1952: pNew->ht = 0; ds@1952: } ds@1952: ds@1952: /* Remove all entries from a hash table. Reclaim all memory. ds@1952: ** Call this routine to delete a hash table or to reset a hash table ds@1952: ** to the empty state. ds@1952: */ ds@1952: void sqlite3HashClear(Hash *pH){ ds@1952: HashElem *elem; /* For looping over all elements of the table */ ds@1952: ds@1952: assert( pH!=0 ); ds@1952: elem = pH->first; ds@1952: pH->first = 0; ds@1952: if( pH->ht ) sqlite3_free(pH->ht); ds@1952: pH->ht = 0; ds@1952: pH->htsize = 0; ds@1952: while( elem ){ ds@1952: HashElem *next_elem = elem->next; ds@1952: if( pH->copyKey && elem->pKey ){ ds@1952: sqlite3_free(elem->pKey); ds@1952: } ds@1952: sqlite3_free(elem); ds@1952: elem = next_elem; ds@1952: } ds@1952: pH->count = 0; ds@1952: } ds@1952: ds@1952: #if 0 /* NOT USED */ ds@1952: /* ds@1952: ** Hash and comparison functions when the mode is SQLITE_HASH_INT ds@1952: */ ds@1952: static int intHash(const void *pKey, int nKey){ ds@1952: return nKey ^ (nKey<<8) ^ (nKey>>8); ds@1952: } ds@1952: static int intCompare(const void *pKey1, int n1, const void *pKey2, int n2){ ds@1952: return n2 - n1; ds@1952: } ds@1952: #endif ds@1952: ds@1952: #if 0 /* NOT USED */ ds@1952: /* ds@1952: ** Hash and comparison functions when the mode is SQLITE_HASH_POINTER ds@1952: */ ds@1952: static int ptrHash(const void *pKey, int nKey){ ds@1952: uptr x = Addr(pKey); ds@1952: return x ^ (x<<8) ^ (x>>8); ds@1952: } ds@1952: static int ptrCompare(const void *pKey1, int n1, const void *pKey2, int n2){ ds@1952: if( pKey1==pKey2 ) return 0; ds@1952: if( pKey1 0 ){ ds@1952: h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++]; ds@1952: nKey--; ds@1952: } ds@1952: return h & 0x7fffffff; ds@1952: } ds@1952: static int strCompare(const void *pKey1, int n1, const void *pKey2, int n2){ ds@1952: if( n1!=n2 ) return 1; ds@1952: return sqlite3StrNICmp((const char*)pKey1,(const char*)pKey2,n1); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Hash and comparison functions when the mode is SQLITE_HASH_BINARY ds@1952: */ ds@1952: static int binHash(const void *pKey, int nKey){ ds@1952: int h = 0; ds@1952: const char *z = (const char *)pKey; ds@1952: while( nKey-- > 0 ){ ds@1952: h = (h<<3) ^ h ^ *(z++); ds@1952: } ds@1952: return h & 0x7fffffff; ds@1952: } ds@1952: static int binCompare(const void *pKey1, int n1, const void *pKey2, int n2){ ds@1952: if( n1!=n2 ) return 1; ds@1952: return memcmp(pKey1,pKey2,n1); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return a pointer to the appropriate hash function given the key class. ds@1952: ** ds@1952: ** The C syntax in this function definition may be unfamilar to some ds@1952: ** programmers, so we provide the following additional explanation: ds@1952: ** ds@1952: ** The name of the function is "hashFunction". The function takes a ds@1952: ** single parameter "keyClass". The return value of hashFunction() ds@1952: ** is a pointer to another function. Specifically, the return value ds@1952: ** of hashFunction() is a pointer to a function that takes two parameters ds@1952: ** with types "const void*" and "int" and returns an "int". ds@1952: */ ds@1952: static int (*hashFunction(int keyClass))(const void*,int){ ds@1952: #if 0 /* HASH_INT and HASH_POINTER are never used */ ds@1952: switch( keyClass ){ ds@1952: case SQLITE_HASH_INT: return &intHash; ds@1952: case SQLITE_HASH_POINTER: return &ptrHash; ds@1952: case SQLITE_HASH_STRING: return &strHash; ds@1952: case SQLITE_HASH_BINARY: return &binHash;; ds@1952: default: break; ds@1952: } ds@1952: return 0; ds@1952: #else ds@1952: if( keyClass==SQLITE_HASH_STRING ){ ds@1952: return &strHash; ds@1952: }else{ ds@1952: assert( keyClass==SQLITE_HASH_BINARY ); ds@1952: return &binHash; ds@1952: } ds@1952: #endif ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return a pointer to the appropriate hash function given the key class. ds@1952: ** ds@1952: ** For help in interpreted the obscure C code in the function definition, ds@1952: ** see the header comment on the previous function. ds@1952: */ ds@1952: static int (*compareFunction(int keyClass))(const void*,int,const void*,int){ ds@1952: #if 0 /* HASH_INT and HASH_POINTER are never used */ ds@1952: switch( keyClass ){ ds@1952: case SQLITE_HASH_INT: return &intCompare; ds@1952: case SQLITE_HASH_POINTER: return &ptrCompare; ds@1952: case SQLITE_HASH_STRING: return &strCompare; ds@1952: case SQLITE_HASH_BINARY: return &binCompare; ds@1952: default: break; ds@1952: } ds@1952: return 0; ds@1952: #else ds@1952: if( keyClass==SQLITE_HASH_STRING ){ ds@1952: return &strCompare; ds@1952: }else{ ds@1952: assert( keyClass==SQLITE_HASH_BINARY ); ds@1952: return &binCompare; ds@1952: } ds@1952: #endif ds@1952: } ds@1952: ds@1952: /* Link an element into the hash table ds@1952: */ ds@1952: static void insertElement( ds@1952: Hash *pH, /* The complete hash table */ ds@1952: struct _ht *pEntry, /* The entry into which pNew is inserted */ ds@1952: HashElem *pNew /* The element to be inserted */ ds@1952: ){ ds@1952: HashElem *pHead; /* First element already in pEntry */ ds@1952: pHead = pEntry->chain; ds@1952: if( pHead ){ ds@1952: pNew->next = pHead; ds@1952: pNew->prev = pHead->prev; ds@1952: if( pHead->prev ){ pHead->prev->next = pNew; } ds@1952: else { pH->first = pNew; } ds@1952: pHead->prev = pNew; ds@1952: }else{ ds@1952: pNew->next = pH->first; ds@1952: if( pH->first ){ pH->first->prev = pNew; } ds@1952: pNew->prev = 0; ds@1952: pH->first = pNew; ds@1952: } ds@1952: pEntry->count++; ds@1952: pEntry->chain = pNew; ds@1952: } ds@1952: ds@1952: ds@1952: /* Resize the hash table so that it cantains "new_size" buckets. ds@1952: ** "new_size" must be a power of 2. The hash table might fail ds@1952: ** to resize if sqlite3_malloc() fails. ds@1952: */ ds@1952: static void rehash(Hash *pH, int new_size){ ds@1952: struct _ht *new_ht; /* The new hash table */ ds@1952: HashElem *elem, *next_elem; /* For looping over existing elements */ ds@1952: int (*xHash)(const void*,int); /* The hash function */ ds@1952: ds@1952: assert( (new_size & (new_size-1))==0 ); ds@1952: ds@1952: /* There is a call to sqlite3_malloc() inside rehash(). If there is ds@1952: ** already an allocation at pH->ht, then if this malloc() fails it ds@1952: ** is benign (since failing to resize a hash table is a performance ds@1952: ** hit only, not a fatal error). ds@1952: */ ds@1952: sqlite3MallocBenignFailure(pH->htsize>0); ds@1952: ds@1952: new_ht = (struct _ht *)sqlite3MallocZero( new_size*sizeof(struct _ht) ); ds@1952: if( new_ht==0 ) return; ds@1952: if( pH->ht ) sqlite3_free(pH->ht); ds@1952: pH->ht = new_ht; ds@1952: pH->htsize = new_size; ds@1952: xHash = hashFunction(pH->keyClass); ds@1952: for(elem=pH->first, pH->first=0; elem; elem = next_elem){ ds@1952: int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1); ds@1952: next_elem = elem->next; ds@1952: insertElement(pH, &new_ht[h], elem); ds@1952: } ds@1952: } ds@1952: ds@1952: /* This function (for internal use only) locates an element in an ds@1952: ** hash table that matches the given key. The hash for this key has ds@1952: ** already been computed and is passed as the 4th parameter. ds@1952: */ ds@1952: static HashElem *findElementGivenHash( ds@1952: const Hash *pH, /* The pH to be searched */ ds@1952: const void *pKey, /* The key we are searching for */ ds@1952: int nKey, ds@1952: int h /* The hash for this key. */ ds@1952: ){ ds@1952: HashElem *elem; /* Used to loop thru the element list */ ds@1952: int count; /* Number of elements left to test */ ds@1952: int (*xCompare)(const void*,int,const void*,int); /* comparison function */ ds@1952: ds@1952: if( pH->ht ){ ds@1952: struct _ht *pEntry = &pH->ht[h]; ds@1952: elem = pEntry->chain; ds@1952: count = pEntry->count; ds@1952: xCompare = compareFunction(pH->keyClass); ds@1952: while( count-- && elem ){ ds@1952: if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){ ds@1952: return elem; ds@1952: } ds@1952: elem = elem->next; ds@1952: } ds@1952: } ds@1952: return 0; ds@1952: } ds@1952: ds@1952: /* Remove a single entry from the hash table given a pointer to that ds@1952: ** element and a hash on the element's key. ds@1952: */ ds@1952: static void removeElementGivenHash( ds@1952: Hash *pH, /* The pH containing "elem" */ ds@1952: HashElem* elem, /* The element to be removed from the pH */ ds@1952: int h /* Hash value for the element */ ds@1952: ){ ds@1952: struct _ht *pEntry; ds@1952: if( elem->prev ){ ds@1952: elem->prev->next = elem->next; ds@1952: }else{ ds@1952: pH->first = elem->next; ds@1952: } ds@1952: if( elem->next ){ ds@1952: elem->next->prev = elem->prev; ds@1952: } ds@1952: pEntry = &pH->ht[h]; ds@1952: if( pEntry->chain==elem ){ ds@1952: pEntry->chain = elem->next; ds@1952: } ds@1952: pEntry->count--; ds@1952: if( pEntry->count<=0 ){ ds@1952: pEntry->chain = 0; ds@1952: } ds@1952: if( pH->copyKey ){ ds@1952: sqlite3_free(elem->pKey); ds@1952: } ds@1952: sqlite3_free( elem ); ds@1952: pH->count--; ds@1952: if( pH->count<=0 ){ ds@1952: assert( pH->first==0 ); ds@1952: assert( pH->count==0 ); ds@1952: sqlite3HashClear(pH); ds@1952: } ds@1952: } ds@1952: ds@1952: /* Attempt to locate an element of the hash table pH with a key ds@1952: ** that matches pKey,nKey. Return a pointer to the corresponding ds@1952: ** HashElem structure for this element if it is found, or NULL ds@1952: ** otherwise. ds@1952: */ ds@1952: HashElem *sqlite3HashFindElem(const Hash *pH, const void *pKey, int nKey){ ds@1952: int h; /* A hash on key */ ds@1952: HashElem *elem; /* The element that matches key */ ds@1952: int (*xHash)(const void*,int); /* The hash function */ ds@1952: ds@1952: if( pH==0 || pH->ht==0 ) return 0; ds@1952: xHash = hashFunction(pH->keyClass); ds@1952: assert( xHash!=0 ); ds@1952: h = (*xHash)(pKey,nKey); ds@1952: assert( (pH->htsize & (pH->htsize-1))==0 ); ds@1952: elem = findElementGivenHash(pH,pKey,nKey, h & (pH->htsize-1)); ds@1952: return elem; ds@1952: } ds@1952: ds@1952: /* Attempt to locate an element of the hash table pH with a key ds@1952: ** that matches pKey,nKey. Return the data for this element if it is ds@1952: ** found, or NULL if there is no match. ds@1952: */ ds@1952: void *sqlite3HashFind(const Hash *pH, const void *pKey, int nKey){ ds@1952: HashElem *elem; /* The element that matches key */ ds@1952: elem = sqlite3HashFindElem(pH, pKey, nKey); ds@1952: return elem ? elem->data : 0; ds@1952: } ds@1952: ds@1952: /* Insert an element into the hash table pH. The key is pKey,nKey ds@1952: ** and the data is "data". ds@1952: ** ds@1952: ** If no element exists with a matching key, then a new ds@1952: ** element is created. A copy of the key is made if the copyKey ds@1952: ** flag is set. NULL is returned. ds@1952: ** ds@1952: ** If another element already exists with the same key, then the ds@1952: ** new data replaces the old data and the old data is returned. ds@1952: ** The key is not copied in this instance. If a malloc fails, then ds@1952: ** the new data is returned and the hash table is unchanged. ds@1952: ** ds@1952: ** If the "data" parameter to this function is NULL, then the ds@1952: ** element corresponding to "key" is removed from the hash table. ds@1952: */ ds@1952: void *sqlite3HashInsert(Hash *pH, const void *pKey, int nKey, void *data){ ds@1952: int hraw; /* Raw hash value of the key */ ds@1952: int h; /* the hash of the key modulo hash table size */ ds@1952: HashElem *elem; /* Used to loop thru the element list */ ds@1952: HashElem *new_elem; /* New element added to the pH */ ds@1952: int (*xHash)(const void*,int); /* The hash function */ ds@1952: ds@1952: assert( pH!=0 ); ds@1952: xHash = hashFunction(pH->keyClass); ds@1952: assert( xHash!=0 ); ds@1952: hraw = (*xHash)(pKey, nKey); ds@1952: assert( (pH->htsize & (pH->htsize-1))==0 ); ds@1952: h = hraw & (pH->htsize-1); ds@1952: elem = findElementGivenHash(pH,pKey,nKey,h); ds@1952: if( elem ){ ds@1952: void *old_data = elem->data; ds@1952: if( data==0 ){ ds@1952: removeElementGivenHash(pH,elem,h); ds@1952: }else{ ds@1952: elem->data = data; ds@1952: if( !pH->copyKey ){ ds@1952: elem->pKey = (void *)pKey; ds@1952: } ds@1952: assert(nKey==elem->nKey); ds@1952: } ds@1952: return old_data; ds@1952: } ds@1952: if( data==0 ) return 0; ds@1952: new_elem = (HashElem*)sqlite3_malloc( sizeof(HashElem) ); ds@1952: if( new_elem==0 ) return data; ds@1952: if( pH->copyKey && pKey!=0 ){ ds@1952: new_elem->pKey = sqlite3_malloc( nKey ); ds@1952: if( new_elem->pKey==0 ){ ds@1952: sqlite3_free(new_elem); ds@1952: return data; ds@1952: } ds@1952: memcpy((void*)new_elem->pKey, pKey, nKey); ds@1952: }else{ ds@1952: new_elem->pKey = (void*)pKey; ds@1952: } ds@1952: new_elem->nKey = nKey; ds@1952: pH->count++; ds@1952: if( pH->htsize==0 ){ ds@1952: rehash(pH,8); ds@1952: if( pH->htsize==0 ){ ds@1952: pH->count = 0; ds@1952: if( pH->copyKey ){ ds@1952: sqlite3_free(new_elem->pKey); ds@1952: } ds@1952: sqlite3_free(new_elem); ds@1952: return data; ds@1952: } ds@1952: } ds@1952: if( pH->count > pH->htsize ){ ds@1952: rehash(pH,pH->htsize*2); ds@1952: } ds@1952: assert( pH->htsize>0 ); ds@1952: assert( (pH->htsize & (pH->htsize-1))==0 ); ds@1952: h = hraw & (pH->htsize-1); ds@1952: insertElement(pH, &pH->ht[h], new_elem); ds@1952: new_elem->data = data; ds@1952: return 0; ds@1952: }