hash.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 implemenation
  13. ** used in SQLite.
  14. **
  15. ** $Id: hash.h,v 1.11 2007/09/04 14:31:47 danielk1977 Exp $
  16. */
  17. #ifndef _SQLITE_HASH_H_
  18. #define _SQLITE_HASH_H_
  19. /* Forward declarations of structures. */
  20. typedef struct Hash Hash;
  21. typedef struct HashElem HashElem;
  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.
  26. ** However, many of the "procedures" and "functions" for modifying and
  27. ** accessing this structure are really macros, so we can't really make
  28. ** this structure opaque.
  29. */
  30. struct Hash {
  31. char keyClass; /* SQLITE_HASH_INT, _POINTER, _STRING, _BINARY */
  32. char copyKey; /* True if copy of key made on insert */
  33. int count; /* Number of entries in this table */
  34. int htsize; /* Number of buckets in the hash table */
  35. HashElem *first; /* The first element of the array */
  36. struct _ht { /* the hash table */
  37. int count; /* Number of entries with this hash */
  38. HashElem *chain; /* Pointer to first entry with this hash */
  39. } *ht;
  40. };
  41. /* Each element in the hash table is an instance of the following
  42. ** structure. All elements are stored on a single doubly-linked list.
  43. **
  44. ** Again, this structure is intended to be opaque, but it can't really
  45. ** be opaque because it is used by macros.
  46. */
  47. struct HashElem {
  48. HashElem *next, *prev; /* Next and previous elements in the table */
  49. void *data; /* Data associated with this element */
  50. void *pKey; int nKey; /* Key associated with this element */
  51. };
  52. /*
  53. ** There are 4 different modes of operation for a hash table:
  54. **
  55. ** SQLITE_HASH_INT nKey is used as the key and pKey is ignored.
  56. **
  57. ** SQLITE_HASH_POINTER pKey is used as the key and nKey is ignored.
  58. **
  59. ** SQLITE_HASH_STRING pKey points to a string that is nKey bytes long
  60. ** (including the null-terminator, if any). Case
  61. ** is ignored in comparisons.
  62. **
  63. ** SQLITE_HASH_BINARY pKey points to binary data nKey bytes long.
  64. ** memcmp() is used to compare keys.
  65. **
  66. ** A copy of the key is made for SQLITE_HASH_STRING and SQLITE_HASH_BINARY
  67. ** if the copyKey parameter to HashInit is 1.
  68. */
  69. /* #define SQLITE_HASH_INT 1 // NOT USED */
  70. /* #define SQLITE_HASH_POINTER 2 // NOT USED */
  71. #define SQLITE_HASH_STRING 3
  72. #define SQLITE_HASH_BINARY 4
  73. /*
  74. ** Access routines. To delete, insert a NULL pointer.
  75. */
  76. void sqlite3HashInit(Hash*, int keytype, int copyKey);
  77. void *sqlite3HashInsert(Hash*, const void *pKey, int nKey, void *pData);
  78. void *sqlite3HashFind(const Hash*, const void *pKey, int nKey);
  79. HashElem *sqlite3HashFindElem(const Hash*, const void *pKey, int nKey);
  80. void sqlite3HashClear(Hash*);
  81. /*
  82. ** Macros for looping over all elements of a hash table. The idiom is
  83. ** like this:
  84. **
  85. ** Hash h;
  86. ** HashElem *p;
  87. ** ...
  88. ** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
  89. ** SomeStructure *pData = sqliteHashData(p);
  90. ** // do something with pData
  91. ** }
  92. */
  93. #define sqliteHashFirst(H) ((H)->first)
  94. #define sqliteHashNext(E) ((E)->next)
  95. #define sqliteHashData(E) ((E)->data)
  96. #define sqliteHashKey(E) ((E)->pKey)
  97. #define sqliteHashKeysize(E) ((E)->nKey)
  98. /*
  99. ** Number of entries in a hash table
  100. */
  101. #define sqliteHashCount(H) ((H)->count)
  102. #endif /* _SQLITE_HASH_H_ */