hash.h

Go to the documentation of this file.
00001 /*
00002 ** 2001 September 22
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 *************************************************************************
00012 ** This is the header file for the generic hash-table implemenation
00013 ** used in SQLite.
00014 **
00015 ** $Id: hash.h,v 1.12 2008/10/10 17:41:29 drh Exp $
00016 */
00017 #ifndef _SQLITE_HASH_H_
00018 #define _SQLITE_HASH_H_
00019 
00020 /* Forward declarations of structures. */
00021 typedef struct Hash Hash;
00022 typedef struct HashElem HashElem;
00023 
00024 /* A complete hash table is an instance of the following structure.
00025 ** The internals of this structure are intended to be opaque -- client
00026 ** code should not attempt to access or modify the fields of this structure
00027 ** directly.  Change this structure only by using the routines below.
00028 ** However, many of the "procedures" and "functions" for modifying and
00029 ** accessing this structure are really macros, so we can't really make
00030 ** this structure opaque.
00031 */
00032 struct Hash {
00033   unsigned int copyKey: 1;  /* True if copy of key made on insert */
00034   unsigned int htsize : 31; /* Number of buckets in the hash table */
00035   unsigned int count;       /* Number of entries in this table */
00036   HashElem *first;          /* The first element of the array */
00037   struct _ht {              /* the hash table */
00038     int count;                 /* Number of entries with this hash */
00039     HashElem *chain;           /* Pointer to first entry with this hash */
00040   } *ht;
00041 };
00042 
00043 /* Each element in the hash table is an instance of the following 
00044 ** structure.  All elements are stored on a single doubly-linked list.
00045 **
00046 ** Again, this structure is intended to be opaque, but it can't really
00047 ** be opaque because it is used by macros.
00048 */
00049 struct HashElem {
00050   HashElem *next, *prev;   /* Next and previous elements in the table */
00051   void *data;              /* Data associated with this element */
00052   void *pKey; int nKey;    /* Key associated with this element */
00053 };
00054 
00055 /*
00056 ** Access routines.  To delete, insert a NULL pointer.
00057 */
00058 void sqlite3HashInit(Hash*, int copyKey);
00059 void *sqlite3HashInsert(Hash*, const void *pKey, int nKey, void *pData);
00060 void *sqlite3HashFind(const Hash*, const void *pKey, int nKey);
00061 HashElem *sqlite3HashFindElem(const Hash*, const void *pKey, int nKey);
00062 void sqlite3HashClear(Hash*);
00063 
00064 /*
00065 ** Macros for looping over all elements of a hash table.  The idiom is
00066 ** like this:
00067 **
00068 **   Hash h;
00069 **   HashElem *p;
00070 **   ...
00071 **   for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
00072 **     SomeStructure *pData = sqliteHashData(p);
00073 **     // do something with pData
00074 **   }
00075 */
00076 #define sqliteHashFirst(H)  ((H)->first)
00077 #define sqliteHashNext(E)   ((E)->next)
00078 #define sqliteHashData(E)   ((E)->data)
00079 #define sqliteHashKey(E)    ((E)->pKey)
00080 #define sqliteHashKeysize(E) ((E)->nKey)
00081 
00082 /*
00083 ** Number of entries in a hash table
00084 */
00085 #define sqliteHashCount(H)  ((H)->count)
00086 
00087 #endif /* _SQLITE_HASH_H_ */

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:54 2011 by Doxygen 1.6.1