1*c5c4113dSnw141292 2*c5c4113dSnw141292 #pragma ident "%Z%%M% %I% %E% SMI" 3*c5c4113dSnw141292 4*c5c4113dSnw141292 /* 5*c5c4113dSnw141292 ** 2001 September 22 6*c5c4113dSnw141292 ** 7*c5c4113dSnw141292 ** The author disclaims copyright to this source code. In place of 8*c5c4113dSnw141292 ** a legal notice, here is a blessing: 9*c5c4113dSnw141292 ** 10*c5c4113dSnw141292 ** May you do good and not evil. 11*c5c4113dSnw141292 ** May you find forgiveness for yourself and forgive others. 12*c5c4113dSnw141292 ** May you share freely, never taking more than you give. 13*c5c4113dSnw141292 ** 14*c5c4113dSnw141292 ************************************************************************* 15*c5c4113dSnw141292 ** This is the header file for the generic hash-table implemenation 16*c5c4113dSnw141292 ** used in SQLite. 17*c5c4113dSnw141292 ** 18*c5c4113dSnw141292 ** $Id: hash.h,v 1.6 2004/01/08 02:17:33 drh Exp $ 19*c5c4113dSnw141292 */ 20*c5c4113dSnw141292 #ifndef _SQLITE_HASH_H_ 21*c5c4113dSnw141292 #define _SQLITE_HASH_H_ 22*c5c4113dSnw141292 23*c5c4113dSnw141292 /* Forward declarations of structures. */ 24*c5c4113dSnw141292 typedef struct Hash Hash; 25*c5c4113dSnw141292 typedef struct HashElem HashElem; 26*c5c4113dSnw141292 27*c5c4113dSnw141292 /* A complete hash table is an instance of the following structure. 28*c5c4113dSnw141292 ** The internals of this structure are intended to be opaque -- client 29*c5c4113dSnw141292 ** code should not attempt to access or modify the fields of this structure 30*c5c4113dSnw141292 ** directly. Change this structure only by using the routines below. 31*c5c4113dSnw141292 ** However, many of the "procedures" and "functions" for modifying and 32*c5c4113dSnw141292 ** accessing this structure are really macros, so we can't really make 33*c5c4113dSnw141292 ** this structure opaque. 34*c5c4113dSnw141292 */ 35*c5c4113dSnw141292 struct Hash { 36*c5c4113dSnw141292 char keyClass; /* SQLITE_HASH_INT, _POINTER, _STRING, _BINARY */ 37*c5c4113dSnw141292 char copyKey; /* True if copy of key made on insert */ 38*c5c4113dSnw141292 int count; /* Number of entries in this table */ 39*c5c4113dSnw141292 HashElem *first; /* The first element of the array */ 40*c5c4113dSnw141292 int htsize; /* Number of buckets in the hash table */ 41*c5c4113dSnw141292 struct _ht { /* the hash table */ 42*c5c4113dSnw141292 int count; /* Number of entries with this hash */ 43*c5c4113dSnw141292 HashElem *chain; /* Pointer to first entry with this hash */ 44*c5c4113dSnw141292 } *ht; 45*c5c4113dSnw141292 }; 46*c5c4113dSnw141292 47*c5c4113dSnw141292 /* Each element in the hash table is an instance of the following 48*c5c4113dSnw141292 ** structure. All elements are stored on a single doubly-linked list. 49*c5c4113dSnw141292 ** 50*c5c4113dSnw141292 ** Again, this structure is intended to be opaque, but it can't really 51*c5c4113dSnw141292 ** be opaque because it is used by macros. 52*c5c4113dSnw141292 */ 53*c5c4113dSnw141292 struct HashElem { 54*c5c4113dSnw141292 HashElem *next, *prev; /* Next and previous elements in the table */ 55*c5c4113dSnw141292 void *data; /* Data associated with this element */ 56*c5c4113dSnw141292 void *pKey; int nKey; /* Key associated with this element */ 57*c5c4113dSnw141292 }; 58*c5c4113dSnw141292 59*c5c4113dSnw141292 /* 60*c5c4113dSnw141292 ** There are 4 different modes of operation for a hash table: 61*c5c4113dSnw141292 ** 62*c5c4113dSnw141292 ** SQLITE_HASH_INT nKey is used as the key and pKey is ignored. 63*c5c4113dSnw141292 ** 64*c5c4113dSnw141292 ** SQLITE_HASH_POINTER pKey is used as the key and nKey is ignored. 65*c5c4113dSnw141292 ** 66*c5c4113dSnw141292 ** SQLITE_HASH_STRING pKey points to a string that is nKey bytes long 67*c5c4113dSnw141292 ** (including the null-terminator, if any). Case 68*c5c4113dSnw141292 ** is ignored in comparisons. 69*c5c4113dSnw141292 ** 70*c5c4113dSnw141292 ** SQLITE_HASH_BINARY pKey points to binary data nKey bytes long. 71*c5c4113dSnw141292 ** memcmp() is used to compare keys. 72*c5c4113dSnw141292 ** 73*c5c4113dSnw141292 ** A copy of the key is made for SQLITE_HASH_STRING and SQLITE_HASH_BINARY 74*c5c4113dSnw141292 ** if the copyKey parameter to HashInit is 1. 75*c5c4113dSnw141292 */ 76*c5c4113dSnw141292 #define SQLITE_HASH_INT 1 77*c5c4113dSnw141292 /* #define SQLITE_HASH_POINTER 2 // NOT USED */ 78*c5c4113dSnw141292 #define SQLITE_HASH_STRING 3 79*c5c4113dSnw141292 #define SQLITE_HASH_BINARY 4 80*c5c4113dSnw141292 81*c5c4113dSnw141292 /* 82*c5c4113dSnw141292 ** Access routines. To delete, insert a NULL pointer. 83*c5c4113dSnw141292 */ 84*c5c4113dSnw141292 void sqliteHashInit(Hash*, int keytype, int copyKey); 85*c5c4113dSnw141292 void *sqliteHashInsert(Hash*, const void *pKey, int nKey, void *pData); 86*c5c4113dSnw141292 void *sqliteHashFind(const Hash*, const void *pKey, int nKey); 87*c5c4113dSnw141292 void sqliteHashClear(Hash*); 88*c5c4113dSnw141292 89*c5c4113dSnw141292 /* 90*c5c4113dSnw141292 ** Macros for looping over all elements of a hash table. The idiom is 91*c5c4113dSnw141292 ** like this: 92*c5c4113dSnw141292 ** 93*c5c4113dSnw141292 ** Hash h; 94*c5c4113dSnw141292 ** HashElem *p; 95*c5c4113dSnw141292 ** ... 96*c5c4113dSnw141292 ** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){ 97*c5c4113dSnw141292 ** SomeStructure *pData = sqliteHashData(p); 98*c5c4113dSnw141292 ** // do something with pData 99*c5c4113dSnw141292 ** } 100*c5c4113dSnw141292 */ 101*c5c4113dSnw141292 #define sqliteHashFirst(H) ((H)->first) 102*c5c4113dSnw141292 #define sqliteHashNext(E) ((E)->next) 103*c5c4113dSnw141292 #define sqliteHashData(E) ((E)->data) 104*c5c4113dSnw141292 #define sqliteHashKey(E) ((E)->pKey) 105*c5c4113dSnw141292 #define sqliteHashKeysize(E) ((E)->nKey) 106*c5c4113dSnw141292 107*c5c4113dSnw141292 /* 108*c5c4113dSnw141292 ** Number of entries in a hash table 109*c5c4113dSnw141292 */ 110*c5c4113dSnw141292 #define sqliteHashCount(H) ((H)->count) 111*c5c4113dSnw141292 112*c5c4113dSnw141292 #endif /* _SQLITE_HASH_H_ */ 113