blob: 6dfa4e035c26e0e5530f2cb09251a2a201339220 [file] [log] [blame]
drhbeae3192001-09-22 18:12:08 +00001/*
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*************************************************************************
mistachkin48864df2013-03-21 21:20:32 +000012** This is the header file for the generic hash-table implementation
drhbeae3192001-09-22 18:12:08 +000013** used in SQLite.
drhbeae3192001-09-22 18:12:08 +000014*/
15#ifndef _SQLITE_HASH_H_
16#define _SQLITE_HASH_H_
17
18/* Forward declarations of structures. */
19typedef struct Hash Hash;
20typedef struct HashElem HashElem;
21
22/* A complete hash table is an instance of the following structure.
23** The internals of this structure are intended to be opaque -- client
24** code should not attempt to access or modify the fields of this structure
25** directly. Change this structure only by using the routines below.
drh8a1e5942009-04-28 15:43:45 +000026** However, some of the "procedures" and "functions" for modifying and
drhbeae3192001-09-22 18:12:08 +000027** accessing this structure are really macros, so we can't really make
28** this structure opaque.
drh8a1e5942009-04-28 15:43:45 +000029**
30** All elements of the hash table are on a single doubly-linked list.
31** Hash.first points to the head of this list.
32**
33** There are Hash.htsize buckets. Each bucket points to a spot in
34** the global doubly-linked list. The contents of the bucket are the
35** element pointed to plus the next _ht.count-1 elements in the list.
36**
37** Hash.htsize and Hash.ht may be zero. In that case lookup is done
38** by a linear search of the global list. For small tables, the
39** Hash.ht table is never allocated because if there are few elements
40** in the table, it is faster to do a linear search than to manage
41** the hash table.
drhbeae3192001-09-22 18:12:08 +000042*/
43struct Hash {
drhe61922a2009-05-02 13:29:37 +000044 unsigned int htsize; /* Number of buckets in the hash table */
drh1b67f3c2008-10-10 17:41:28 +000045 unsigned int count; /* Number of entries in this table */
46 HashElem *first; /* The first element of the array */
47 struct _ht { /* the hash table */
48 int count; /* Number of entries with this hash */
49 HashElem *chain; /* Pointer to first entry with this hash */
drhbeae3192001-09-22 18:12:08 +000050 } *ht;
51};
52
53/* Each element in the hash table is an instance of the following
54** structure. All elements are stored on a single doubly-linked list.
55**
56** Again, this structure is intended to be opaque, but it can't really
57** be opaque because it is used by macros.
58*/
59struct HashElem {
drhe61922a2009-05-02 13:29:37 +000060 HashElem *next, *prev; /* Next and previous elements in the table */
61 void *data; /* Data associated with this element */
drhacbcb7e2014-08-21 20:26:37 +000062 const char *pKey; /* Key associated with this element */
drhbeae3192001-09-22 18:12:08 +000063};
64
65/*
drhbeae3192001-09-22 18:12:08 +000066** Access routines. To delete, insert a NULL pointer.
67*/
drhe61922a2009-05-02 13:29:37 +000068void sqlite3HashInit(Hash*);
drhacbcb7e2014-08-21 20:26:37 +000069void *sqlite3HashInsert(Hash*, const char *pKey, void *pData);
70void *sqlite3HashFind(const Hash*, const char *pKey);
danielk19774adee202004-05-08 08:23:19 +000071void sqlite3HashClear(Hash*);
drhbeae3192001-09-22 18:12:08 +000072
73/*
74** Macros for looping over all elements of a hash table. The idiom is
75** like this:
76**
77** Hash h;
78** HashElem *p;
79** ...
80** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
81** SomeStructure *pData = sqliteHashData(p);
82** // do something with pData
83** }
84*/
85#define sqliteHashFirst(H) ((H)->first)
86#define sqliteHashNext(E) ((E)->next)
87#define sqliteHashData(E) ((E)->data)
drh8a1e5942009-04-28 15:43:45 +000088/* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
89/* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
drh1dd397f2002-02-03 03:34:07 +000090
91/*
92** Number of entries in a hash table
93*/
drh8a1e5942009-04-28 15:43:45 +000094/* #define sqliteHashCount(H) ((H)->count) // NOT USED */
drhbeae3192001-09-22 18:12:08 +000095
96#endif /* _SQLITE_HASH_H_ */