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