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 header file for the generic hash-table implemenation ds@1952: ** used in SQLite. ds@1952: ** ds@1952: ** $Id$ ds@1952: */ ds@1952: #ifndef _SQLITE_HASH_H_ ds@1952: #define _SQLITE_HASH_H_ ds@1952: ds@1952: /* Forward declarations of structures. */ ds@1952: typedef struct Hash Hash; ds@1952: typedef struct HashElem HashElem; ds@1952: ds@1952: /* A complete hash table is an instance of the following structure. ds@1952: ** The internals of this structure are intended to be opaque -- client ds@1952: ** code should not attempt to access or modify the fields of this structure ds@1952: ** directly. Change this structure only by using the routines below. ds@1952: ** However, many of the "procedures" and "functions" for modifying and ds@1952: ** accessing this structure are really macros, so we can't really make ds@1952: ** this structure opaque. ds@1952: */ ds@1952: struct Hash { ds@1952: char keyClass; /* SQLITE_HASH_INT, _POINTER, _STRING, _BINARY */ ds@1952: char copyKey; /* True if copy of key made on insert */ ds@1952: int count; /* Number of entries in this table */ ds@1952: int htsize; /* Number of buckets in the hash table */ ds@1952: HashElem *first; /* The first element of the array */ ds@1952: struct _ht { /* the hash table */ ds@1952: int count; /* Number of entries with this hash */ ds@1952: HashElem *chain; /* Pointer to first entry with this hash */ ds@1952: } *ht; ds@1952: }; ds@1952: ds@1952: /* Each element in the hash table is an instance of the following ds@1952: ** structure. All elements are stored on a single doubly-linked list. ds@1952: ** ds@1952: ** Again, this structure is intended to be opaque, but it can't really ds@1952: ** be opaque because it is used by macros. ds@1952: */ ds@1952: struct HashElem { ds@1952: HashElem *next, *prev; /* Next and previous elements in the table */ ds@1952: void *data; /* Data associated with this element */ ds@1952: void *pKey; int nKey; /* Key associated with this element */ ds@1952: }; ds@1952: ds@1952: /* ds@1952: ** There are 4 different modes of operation for a hash table: ds@1952: ** ds@1952: ** SQLITE_HASH_INT nKey is used as the key and pKey is ignored. ds@1952: ** ds@1952: ** SQLITE_HASH_POINTER pKey is used as the key and nKey is ignored. ds@1952: ** ds@1952: ** SQLITE_HASH_STRING pKey points to a string that is nKey bytes long ds@1952: ** (including the null-terminator, if any). Case ds@1952: ** is ignored in comparisons. ds@1952: ** ds@1952: ** SQLITE_HASH_BINARY pKey points to binary data nKey bytes long. ds@1952: ** memcmp() is used to compare keys. ds@1952: ** ds@1952: ** A copy of the key is made for SQLITE_HASH_STRING and SQLITE_HASH_BINARY ds@1952: ** if the copyKey parameter to HashInit is 1. ds@1952: */ ds@1952: /* #define SQLITE_HASH_INT 1 // NOT USED */ ds@1952: /* #define SQLITE_HASH_POINTER 2 // NOT USED */ ds@1952: #define SQLITE_HASH_STRING 3 ds@1952: #define SQLITE_HASH_BINARY 4 ds@1952: ds@1952: /* ds@1952: ** Access routines. To delete, insert a NULL pointer. ds@1952: */ ds@1952: void sqlite3HashInit(Hash*, int keytype, int copyKey); ds@1952: void *sqlite3HashInsert(Hash*, const void *pKey, int nKey, void *pData); ds@1952: void *sqlite3HashFind(const Hash*, const void *pKey, int nKey); ds@1952: HashElem *sqlite3HashFindElem(const Hash*, const void *pKey, int nKey); ds@1952: void sqlite3HashClear(Hash*); ds@1952: ds@1952: /* ds@1952: ** Macros for looping over all elements of a hash table. The idiom is ds@1952: ** like this: ds@1952: ** ds@1952: ** Hash h; ds@1952: ** HashElem *p; ds@1952: ** ... ds@1952: ** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){ ds@1952: ** SomeStructure *pData = sqliteHashData(p); ds@1952: ** // do something with pData ds@1952: ** } ds@1952: */ ds@1952: #define sqliteHashFirst(H) ((H)->first) ds@1952: #define sqliteHashNext(E) ((E)->next) ds@1952: #define sqliteHashData(E) ((E)->data) ds@1952: #define sqliteHashKey(E) ((E)->pKey) ds@1952: #define sqliteHashKeysize(E) ((E)->nKey) ds@1952: ds@1952: /* ds@1952: ** Number of entries in a hash table ds@1952: */ ds@1952: #define sqliteHashCount(H) ((H)->count) ds@1952: ds@1952: #endif /* _SQLITE_HASH_H_ */