hash.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  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. *************************************************************************
  12. ** This is the header file for the generic hash-table implementation
  13. ** used in SQLite.
  14. */
  15. #ifndef _SQLITE_HASH_H_
  16. #define _SQLITE_HASH_H_
  17. /* Forward declarations of structures. */
  18. typedef struct Hash Hash;
  19. typedef struct HashElem HashElem;
  20. /* A complete hash table is an instance of the following structure.
  21. ** The internals of this structure are intended to be opaque -- client
  22. ** code should not attempt to access or modify the fields of this structure
  23. ** directly. Change this structure only by using the routines below.
  24. ** However, some of the "procedures" and "functions" for modifying and
  25. ** accessing this structure are really macros, so we can't really make
  26. ** this structure opaque.
  27. **
  28. ** All elements of the hash table are on a single doubly-linked list.
  29. ** Hash.first points to the head of this list.
  30. **
  31. ** There are Hash.htsize buckets. Each bucket points to a spot in
  32. ** the global doubly-linked list. The contents of the bucket are the
  33. ** element pointed to plus the next _ht.count-1 elements in the list.
  34. **
  35. ** Hash.htsize and Hash.ht may be zero. In that case lookup is done
  36. ** by a linear search of the global list. For small tables, the
  37. ** Hash.ht table is never allocated because if there are few elements
  38. ** in the table, it is faster to do a linear search than to manage
  39. ** the hash table.
  40. */
  41. struct Hash {
  42. unsigned int htsize; /* Number of buckets in the hash table */
  43. unsigned int count; /* Number of entries in this table */
  44. HashElem *first; /* The first element of the array */
  45. struct _ht { /* the hash table */
  46. int count; /* Number of entries with this hash */
  47. HashElem *chain; /* Pointer to first entry with this hash */
  48. } *ht;
  49. };
  50. /* Each element in the hash table is an instance of the following
  51. ** structure. All elements are stored on a single doubly-linked list.
  52. **
  53. ** Again, this structure is intended to be opaque, but it can't really
  54. ** be opaque because it is used by macros.
  55. */
  56. struct HashElem {
  57. HashElem *next, *prev; /* Next and previous elements in the table */
  58. void *data; /* Data associated with this element */
  59. const char *pKey; int nKey; /* Key associated with this element */
  60. };
  61. /*
  62. ** Access routines. To delete, insert a NULL pointer.
  63. */
  64. void sqlite3HashInit(Hash*);
  65. void *sqlite3HashInsert(Hash*, const char *pKey, int nKey, void *pData);
  66. void *sqlite3HashFind(const Hash*, const char *pKey, int nKey);
  67. void sqlite3HashClear(Hash*);
  68. /*
  69. ** Macros for looping over all elements of a hash table. The idiom is
  70. ** like this:
  71. **
  72. ** Hash h;
  73. ** HashElem *p;
  74. ** ...
  75. ** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
  76. ** SomeStructure *pData = sqliteHashData(p);
  77. ** // do something with pData
  78. ** }
  79. */
  80. #define sqliteHashFirst(H) ((H)->first)
  81. #define sqliteHashNext(E) ((E)->next)
  82. #define sqliteHashData(E) ((E)->data)
  83. /* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
  84. /* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
  85. /*
  86. ** Number of entries in a hash table
  87. */
  88. /* #define sqliteHashCount(H) ((H)->count) // NOT USED */
  89. #endif /* _SQLITE_HASH_H_ */