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 ************************************************************************* 1248864df9Smistachkin ** This is the header file for the generic hash-table implementation 13beae3194Sdrh ** used in SQLite. 14beae3194Sdrh */ 1543f58d6aSdrh #ifndef SQLITE_HASH_H 1643f58d6aSdrh #define SQLITE_HASH_H 17beae3194Sdrh 18beae3194Sdrh /* Forward declarations of structures. */ 19beae3194Sdrh typedef struct Hash Hash; 20beae3194Sdrh typedef struct HashElem HashElem; 21beae3194Sdrh 22beae3194Sdrh /* A complete hash table is an instance of the following structure. 23beae3194Sdrh ** The internals of this structure are intended to be opaque -- client 24beae3194Sdrh ** code should not attempt to access or modify the fields of this structure 25beae3194Sdrh ** directly. Change this structure only by using the routines below. 268a1e594cSdrh ** However, some of the "procedures" and "functions" for modifying and 27beae3194Sdrh ** accessing this structure are really macros, so we can't really make 28beae3194Sdrh ** this structure opaque. 298a1e594cSdrh ** 308a1e594cSdrh ** All elements of the hash table are on a single doubly-linked list. 318a1e594cSdrh ** Hash.first points to the head of this list. 328a1e594cSdrh ** 338a1e594cSdrh ** There are Hash.htsize buckets. Each bucket points to a spot in 348a1e594cSdrh ** the global doubly-linked list. The contents of the bucket are the 358a1e594cSdrh ** element pointed to plus the next _ht.count-1 elements in the list. 368a1e594cSdrh ** 378a1e594cSdrh ** Hash.htsize and Hash.ht may be zero. In that case lookup is done 388a1e594cSdrh ** by a linear search of the global list. For small tables, the 398a1e594cSdrh ** Hash.ht table is never allocated because if there are few elements 408a1e594cSdrh ** in the table, it is faster to do a linear search than to manage 418a1e594cSdrh ** the hash table. 42beae3194Sdrh */ 43beae3194Sdrh struct Hash { 44e61922a6Sdrh unsigned int htsize; /* Number of buckets in the hash table */ 451b67f3caSdrh unsigned int count; /* Number of entries in this table */ 4617435752Sdrh HashElem *first; /* The first element of the array */ 47beae3194Sdrh struct _ht { /* the hash table */ 48f6ad201aSdrh unsigned int count; /* Number of entries with this hash */ 49beae3194Sdrh HashElem *chain; /* Pointer to first entry with this hash */ 50beae3194Sdrh } *ht; 51beae3194Sdrh }; 52beae3194Sdrh 53beae3194Sdrh /* Each element in the hash table is an instance of the following 54beae3194Sdrh ** structure. All elements are stored on a single doubly-linked list. 55beae3194Sdrh ** 56beae3194Sdrh ** Again, this structure is intended to be opaque, but it can't really 57beae3194Sdrh ** be opaque because it is used by macros. 58beae3194Sdrh */ 59beae3194Sdrh struct HashElem { 60beae3194Sdrh HashElem *next, *prev; /* Next and previous elements in the table */ 61beae3194Sdrh void *data; /* Data associated with this element */ 62acbcb7e0Sdrh const char *pKey; /* Key associated with this element */ 63beae3194Sdrh }; 64beae3194Sdrh 65beae3194Sdrh /* 66beae3194Sdrh ** Access routines. To delete, insert a NULL pointer. 67beae3194Sdrh */ 68e61922a6Sdrh void sqlite3HashInit(Hash*); 69acbcb7e0Sdrh void *sqlite3HashInsert(Hash*, const char *pKey, void *pData); 70acbcb7e0Sdrh void *sqlite3HashFind(const Hash*, const char *pKey); 714adee20fSdanielk1977 void sqlite3HashClear(Hash*); 72beae3194Sdrh 73beae3194Sdrh /* 74beae3194Sdrh ** Macros for looping over all elements of a hash table. The idiom is 75beae3194Sdrh ** like this: 76beae3194Sdrh ** 77beae3194Sdrh ** Hash h; 78beae3194Sdrh ** HashElem *p; 79beae3194Sdrh ** ... 80beae3194Sdrh ** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){ 81beae3194Sdrh ** SomeStructure *pData = sqliteHashData(p); 82beae3194Sdrh ** // do something with pData 83beae3194Sdrh ** } 84beae3194Sdrh */ 85beae3194Sdrh #define sqliteHashFirst(H) ((H)->first) 86beae3194Sdrh #define sqliteHashNext(E) ((E)->next) 87beae3194Sdrh #define sqliteHashData(E) ((E)->data) 888a1e594cSdrh /* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */ 898a1e594cSdrh /* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */ 901dd397f0Sdrh 911dd397f0Sdrh /* 921dd397f0Sdrh ** Number of entries in a hash table 931dd397f0Sdrh */ 94*dc88b402Sdrh #define sqliteHashCount(H) ((H)->count) 95beae3194Sdrh 9643f58d6aSdrh #endif /* SQLITE_HASH_H */ 97