2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A** 2001 September 22
2N/A**
2N/A** The author disclaims copyright to this source code. In place of
2N/A** a legal notice, here is a blessing:
2N/A**
2N/A** May you do good and not evil.
2N/A** May you find forgiveness for yourself and forgive others.
2N/A** May you share freely, never taking more than you give.
2N/A**
2N/A*************************************************************************
2N/A** This is the header file for the generic hash-table implemenation
2N/A** used in SQLite.
2N/A**
2N/A** $Id: hash.h,v 1.6 2004/01/08 02:17:33 drh Exp $
2N/A*/
2N/A#ifndef _SQLITE_HASH_H_
2N/A#define _SQLITE_HASH_H_
2N/A
2N/A/* Forward declarations of structures. */
2N/Atypedef struct Hash Hash;
2N/Atypedef struct HashElem HashElem;
2N/A
2N/A/* A complete hash table is an instance of the following structure.
2N/A** The internals of this structure are intended to be opaque -- client
2N/A** code should not attempt to access or modify the fields of this structure
2N/A** directly. Change this structure only by using the routines below.
2N/A** However, many of the "procedures" and "functions" for modifying and
2N/A** accessing this structure are really macros, so we can't really make
2N/A** this structure opaque.
2N/A*/
2N/Astruct Hash {
2N/A char keyClass; /* SQLITE_HASH_INT, _POINTER, _STRING, _BINARY */
2N/A char copyKey; /* True if copy of key made on insert */
2N/A int count; /* Number of entries in this table */
2N/A HashElem *first; /* The first element of the array */
2N/A int htsize; /* Number of buckets in the hash table */
2N/A struct _ht { /* the hash table */
2N/A int count; /* Number of entries with this hash */
2N/A HashElem *chain; /* Pointer to first entry with this hash */
2N/A } *ht;
2N/A};
2N/A
2N/A/* Each element in the hash table is an instance of the following
2N/A** structure. All elements are stored on a single doubly-linked list.
2N/A**
2N/A** Again, this structure is intended to be opaque, but it can't really
2N/A** be opaque because it is used by macros.
2N/A*/
2N/Astruct HashElem {
2N/A HashElem *next, *prev; /* Next and previous elements in the table */
2N/A void *data; /* Data associated with this element */
2N/A void *pKey; int nKey; /* Key associated with this element */
2N/A};
2N/A
2N/A/*
2N/A** There are 4 different modes of operation for a hash table:
2N/A**
2N/A** SQLITE_HASH_INT nKey is used as the key and pKey is ignored.
2N/A**
2N/A** SQLITE_HASH_POINTER pKey is used as the key and nKey is ignored.
2N/A**
2N/A** SQLITE_HASH_STRING pKey points to a string that is nKey bytes long
2N/A** (including the null-terminator, if any). Case
2N/A** is ignored in comparisons.
2N/A**
2N/A** SQLITE_HASH_BINARY pKey points to binary data nKey bytes long.
2N/A** memcmp() is used to compare keys.
2N/A**
2N/A** A copy of the key is made for SQLITE_HASH_STRING and SQLITE_HASH_BINARY
2N/A** if the copyKey parameter to HashInit is 1.
2N/A*/
2N/A#define SQLITE_HASH_INT 1
2N/A/* #define SQLITE_HASH_POINTER 2 // NOT USED */
2N/A#define SQLITE_HASH_STRING 3
2N/A#define SQLITE_HASH_BINARY 4
2N/A
2N/A/*
2N/A** Access routines. To delete, insert a NULL pointer.
2N/A*/
2N/Avoid sqliteHashInit(Hash*, int keytype, int copyKey);
2N/Avoid *sqliteHashInsert(Hash*, const void *pKey, int nKey, void *pData);
2N/Avoid *sqliteHashFind(const Hash*, const void *pKey, int nKey);
2N/Avoid sqliteHashClear(Hash*);
2N/A
2N/A/*
2N/A** Macros for looping over all elements of a hash table. The idiom is
2N/A** like this:
2N/A**
2N/A** Hash h;
2N/A** HashElem *p;
2N/A** ...
2N/A** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
2N/A** SomeStructure *pData = sqliteHashData(p);
2N/A** // do something with pData
2N/A** }
2N/A*/
2N/A#define sqliteHashFirst(H) ((H)->first)
2N/A#define sqliteHashNext(E) ((E)->next)
2N/A#define sqliteHashData(E) ((E)->data)
2N/A#define sqliteHashKey(E) ((E)->pKey)
2N/A#define sqliteHashKeysize(E) ((E)->nKey)
2N/A
2N/A/*
2N/A** Number of entries in a hash table
2N/A*/
2N/A#define sqliteHashCount(H) ((H)->count)
2N/A
2N/A#endif /* _SQLITE_HASH_H_ */