| OLD | NEW | 
 | (Empty) | 
|   1 /* |  | 
|   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 header file for the generic hash-table implemenation |  | 
|  13 ** used in SQLite. |  | 
|  14 ** |  | 
|  15 ** $Id: hash.h,v 1.15 2009/05/02 13:29:38 drh Exp $ |  | 
|  16 */ |  | 
|  17 #ifndef _SQLITE_HASH_H_ |  | 
|  18 #define _SQLITE_HASH_H_ |  | 
|  19  |  | 
|  20 /* Forward declarations of structures. */ |  | 
|  21 typedef struct Hash Hash; |  | 
|  22 typedef struct HashElem HashElem; |  | 
|  23  |  | 
|  24 /* A complete hash table is an instance of the following structure. |  | 
|  25 ** The internals of this structure are intended to be opaque -- client |  | 
|  26 ** code should not attempt to access or modify the fields of this structure |  | 
|  27 ** directly.  Change this structure only by using the routines below. |  | 
|  28 ** However, some of the "procedures" and "functions" for modifying and |  | 
|  29 ** accessing this structure are really macros, so we can't really make |  | 
|  30 ** this structure opaque. |  | 
|  31 ** |  | 
|  32 ** All elements of the hash table are on a single doubly-linked list. |  | 
|  33 ** Hash.first points to the head of this list. |  | 
|  34 ** |  | 
|  35 ** There are Hash.htsize buckets.  Each bucket points to a spot in |  | 
|  36 ** the global doubly-linked list.  The contents of the bucket are the |  | 
|  37 ** element pointed to plus the next _ht.count-1 elements in the list. |  | 
|  38 ** |  | 
|  39 ** Hash.htsize and Hash.ht may be zero.  In that case lookup is done |  | 
|  40 ** by a linear search of the global list.  For small tables, the  |  | 
|  41 ** Hash.ht table is never allocated because if there are few elements |  | 
|  42 ** in the table, it is faster to do a linear search than to manage |  | 
|  43 ** the hash table. |  | 
|  44 */ |  | 
|  45 struct Hash { |  | 
|  46   unsigned int htsize;      /* Number of buckets in the hash table */ |  | 
|  47   unsigned int count;       /* Number of entries in this table */ |  | 
|  48   HashElem *first;          /* The first element of the array */ |  | 
|  49   struct _ht {              /* the hash table */ |  | 
|  50     int count;                 /* Number of entries with this hash */ |  | 
|  51     HashElem *chain;           /* Pointer to first entry with this hash */ |  | 
|  52   } *ht; |  | 
|  53 }; |  | 
|  54  |  | 
|  55 /* Each element in the hash table is an instance of the following  |  | 
|  56 ** structure.  All elements are stored on a single doubly-linked list. |  | 
|  57 ** |  | 
|  58 ** Again, this structure is intended to be opaque, but it can't really |  | 
|  59 ** be opaque because it is used by macros. |  | 
|  60 */ |  | 
|  61 struct HashElem { |  | 
|  62   HashElem *next, *prev;       /* Next and previous elements in the table */ |  | 
|  63   void *data;                  /* Data associated with this element */ |  | 
|  64   const char *pKey; int nKey;  /* Key associated with this element */ |  | 
|  65 }; |  | 
|  66  |  | 
|  67 /* |  | 
|  68 ** Access routines.  To delete, insert a NULL pointer. |  | 
|  69 */ |  | 
|  70 void sqlite3HashInit(Hash*); |  | 
|  71 void *sqlite3HashInsert(Hash*, const char *pKey, int nKey, void *pData); |  | 
|  72 void *sqlite3HashFind(const Hash*, const char *pKey, int nKey); |  | 
|  73 void sqlite3HashClear(Hash*); |  | 
|  74  |  | 
|  75 /* |  | 
|  76 ** Macros for looping over all elements of a hash table.  The idiom is |  | 
|  77 ** like this: |  | 
|  78 ** |  | 
|  79 **   Hash h; |  | 
|  80 **   HashElem *p; |  | 
|  81 **   ... |  | 
|  82 **   for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){ |  | 
|  83 **     SomeStructure *pData = sqliteHashData(p); |  | 
|  84 **     // do something with pData |  | 
|  85 **   } |  | 
|  86 */ |  | 
|  87 #define sqliteHashFirst(H)  ((H)->first) |  | 
|  88 #define sqliteHashNext(E)   ((E)->next) |  | 
|  89 #define sqliteHashData(E)   ((E)->data) |  | 
|  90 /* #define sqliteHashKey(E)    ((E)->pKey) // NOT USED */ |  | 
|  91 /* #define sqliteHashKeysize(E) ((E)->nKey)  // NOT USED */ |  | 
|  92  |  | 
|  93 /* |  | 
|  94 ** Number of entries in a hash table |  | 
|  95 */ |  | 
|  96 /* #define sqliteHashCount(H)  ((H)->count) // NOT USED */ |  | 
|  97  |  | 
|  98 #endif /* _SQLITE_HASH_H_ */ |  | 
| OLD | NEW |