xref: /illumos-gate/usr/src/lib/libsqlite/src/hash.h (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate ** 2001 September 22
37c478bd9Sstevel@tonic-gate **
47c478bd9Sstevel@tonic-gate ** The author disclaims copyright to this source code.  In place of
57c478bd9Sstevel@tonic-gate ** a legal notice, here is a blessing:
67c478bd9Sstevel@tonic-gate **
77c478bd9Sstevel@tonic-gate **    May you do good and not evil.
87c478bd9Sstevel@tonic-gate **    May you find forgiveness for yourself and forgive others.
97c478bd9Sstevel@tonic-gate **    May you share freely, never taking more than you give.
107c478bd9Sstevel@tonic-gate **
117c478bd9Sstevel@tonic-gate *************************************************************************
127c478bd9Sstevel@tonic-gate ** This is the header file for the generic hash-table implemenation
137c478bd9Sstevel@tonic-gate ** used in SQLite.
147c478bd9Sstevel@tonic-gate **
157c478bd9Sstevel@tonic-gate ** $Id: hash.h,v 1.6 2004/01/08 02:17:33 drh Exp $
167c478bd9Sstevel@tonic-gate */
177c478bd9Sstevel@tonic-gate #ifndef _SQLITE_HASH_H_
187c478bd9Sstevel@tonic-gate #define _SQLITE_HASH_H_
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate /* Forward declarations of structures. */
217c478bd9Sstevel@tonic-gate typedef struct Hash Hash;
227c478bd9Sstevel@tonic-gate typedef struct HashElem HashElem;
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate /* A complete hash table is an instance of the following structure.
257c478bd9Sstevel@tonic-gate ** The internals of this structure are intended to be opaque -- client
267c478bd9Sstevel@tonic-gate ** code should not attempt to access or modify the fields of this structure
277c478bd9Sstevel@tonic-gate ** directly.  Change this structure only by using the routines below.
287c478bd9Sstevel@tonic-gate ** However, many of the "procedures" and "functions" for modifying and
297c478bd9Sstevel@tonic-gate ** accessing this structure are really macros, so we can't really make
307c478bd9Sstevel@tonic-gate ** this structure opaque.
317c478bd9Sstevel@tonic-gate */
327c478bd9Sstevel@tonic-gate struct Hash {
337c478bd9Sstevel@tonic-gate   char keyClass;          /* SQLITE_HASH_INT, _POINTER, _STRING, _BINARY */
347c478bd9Sstevel@tonic-gate   char copyKey;           /* True if copy of key made on insert */
357c478bd9Sstevel@tonic-gate   int count;              /* Number of entries in this table */
367c478bd9Sstevel@tonic-gate   HashElem *first;        /* The first element of the array */
377c478bd9Sstevel@tonic-gate   int htsize;             /* Number of buckets in the hash table */
387c478bd9Sstevel@tonic-gate   struct _ht {            /* the hash table */
397c478bd9Sstevel@tonic-gate     int count;               /* Number of entries with this hash */
407c478bd9Sstevel@tonic-gate     HashElem *chain;         /* Pointer to first entry with this hash */
417c478bd9Sstevel@tonic-gate   } *ht;
427c478bd9Sstevel@tonic-gate };
437c478bd9Sstevel@tonic-gate 
44*1da57d55SToomas Soome /* Each element in the hash table is an instance of the following
457c478bd9Sstevel@tonic-gate ** structure.  All elements are stored on a single doubly-linked list.
467c478bd9Sstevel@tonic-gate **
477c478bd9Sstevel@tonic-gate ** Again, this structure is intended to be opaque, but it can't really
487c478bd9Sstevel@tonic-gate ** be opaque because it is used by macros.
497c478bd9Sstevel@tonic-gate */
507c478bd9Sstevel@tonic-gate struct HashElem {
517c478bd9Sstevel@tonic-gate   HashElem *next, *prev;   /* Next and previous elements in the table */
527c478bd9Sstevel@tonic-gate   void *data;              /* Data associated with this element */
537c478bd9Sstevel@tonic-gate   void *pKey; int nKey;    /* Key associated with this element */
547c478bd9Sstevel@tonic-gate };
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate ** There are 4 different modes of operation for a hash table:
587c478bd9Sstevel@tonic-gate **
597c478bd9Sstevel@tonic-gate **   SQLITE_HASH_INT         nKey is used as the key and pKey is ignored.
607c478bd9Sstevel@tonic-gate **
617c478bd9Sstevel@tonic-gate **   SQLITE_HASH_POINTER     pKey is used as the key and nKey is ignored.
627c478bd9Sstevel@tonic-gate **
637c478bd9Sstevel@tonic-gate **   SQLITE_HASH_STRING      pKey points to a string that is nKey bytes long
647c478bd9Sstevel@tonic-gate **                           (including the null-terminator, if any).  Case
657c478bd9Sstevel@tonic-gate **                           is ignored in comparisons.
667c478bd9Sstevel@tonic-gate **
67*1da57d55SToomas Soome **   SQLITE_HASH_BINARY      pKey points to binary data nKey bytes long.
687c478bd9Sstevel@tonic-gate **                           memcmp() is used to compare keys.
697c478bd9Sstevel@tonic-gate **
707c478bd9Sstevel@tonic-gate ** A copy of the key is made for SQLITE_HASH_STRING and SQLITE_HASH_BINARY
71*1da57d55SToomas Soome ** if the copyKey parameter to HashInit is 1.
727c478bd9Sstevel@tonic-gate */
737c478bd9Sstevel@tonic-gate #define SQLITE_HASH_INT       1
747c478bd9Sstevel@tonic-gate /* #define SQLITE_HASH_POINTER   2 // NOT USED */
757c478bd9Sstevel@tonic-gate #define SQLITE_HASH_STRING    3
767c478bd9Sstevel@tonic-gate #define SQLITE_HASH_BINARY    4
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate ** Access routines.  To delete, insert a NULL pointer.
807c478bd9Sstevel@tonic-gate */
817c478bd9Sstevel@tonic-gate void sqliteHashInit(Hash*, int keytype, int copyKey);
827c478bd9Sstevel@tonic-gate void *sqliteHashInsert(Hash*, const void *pKey, int nKey, void *pData);
837c478bd9Sstevel@tonic-gate void *sqliteHashFind(const Hash*, const void *pKey, int nKey);
847c478bd9Sstevel@tonic-gate void sqliteHashClear(Hash*);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate ** Macros for looping over all elements of a hash table.  The idiom is
887c478bd9Sstevel@tonic-gate ** like this:
897c478bd9Sstevel@tonic-gate **
907c478bd9Sstevel@tonic-gate **   Hash h;
917c478bd9Sstevel@tonic-gate **   HashElem *p;
927c478bd9Sstevel@tonic-gate **   ...
937c478bd9Sstevel@tonic-gate **   for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
947c478bd9Sstevel@tonic-gate **     SomeStructure *pData = sqliteHashData(p);
957c478bd9Sstevel@tonic-gate **     // do something with pData
967c478bd9Sstevel@tonic-gate **   }
977c478bd9Sstevel@tonic-gate */
987c478bd9Sstevel@tonic-gate #define sqliteHashFirst(H)  ((H)->first)
997c478bd9Sstevel@tonic-gate #define sqliteHashNext(E)   ((E)->next)
1007c478bd9Sstevel@tonic-gate #define sqliteHashData(E)   ((E)->data)
1017c478bd9Sstevel@tonic-gate #define sqliteHashKey(E)    ((E)->pKey)
1027c478bd9Sstevel@tonic-gate #define sqliteHashKeysize(E) ((E)->nKey)
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate ** Number of entries in a hash table
1067c478bd9Sstevel@tonic-gate */
1077c478bd9Sstevel@tonic-gate #define sqliteHashCount(H)  ((H)->count)
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate #endif /* _SQLITE_HASH_H_ */
110