ft_hash.h 3.8 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 implementation
  13. ** used in SQLite. We've modified it slightly to serve as a standalone
  14. ** hash table implementation for the full-text indexing module.
  15. **
  16. */
  17. #ifndef _HASH_H_
  18. #define _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; /* 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. HashElem *first; /* The first element of the array */
  35. void *(*xMalloc)(int); /* malloc() function to use */
  36. void (*xFree)(void *); /* free() function to use */
  37. int htsize; /* Number of buckets in the hash table */
  38. struct _ht { /* the hash table */
  39. int count; /* Number of entries with this hash */
  40. HashElem *chain; /* Pointer to first entry with this hash */
  41. } *ht;
  42. };
  43. /* Each element in the hash table is an instance of the following
  44. ** structure. All elements are stored on a single doubly-linked list.
  45. **
  46. ** Again, this structure is intended to be opaque, but it can't really
  47. ** be opaque because it is used by macros.
  48. */
  49. struct HashElem {
  50. HashElem *next, *prev; /* Next and previous elements in the table */
  51. void *data; /* Data associated with this element */
  52. void *pKey; int nKey; /* Key associated with this element */
  53. };
  54. /*
  55. ** There are 4 different modes of operation for a hash table:
  56. **
  57. ** HASH_INT nKey is used as the key and pKey is ignored.
  58. **
  59. ** HASH_POINTER pKey is used as the key and nKey is ignored.
  60. **
  61. ** HASH_STRING pKey points to a string that is nKey bytes long
  62. ** (including the null-terminator, if any). Case
  63. ** is respected in comparisons.
  64. **
  65. ** HASH_BINARY pKey points to binary data nKey bytes long.
  66. ** memcmp() is used to compare keys.
  67. **
  68. ** A copy of the key is made for HASH_STRING and HASH_BINARY
  69. ** if the copyKey parameter to HashInit is 1.
  70. */
  71. /* #define HASH_INT 1 // NOT USED */
  72. /* #define HASH_POINTER 2 // NOT USED */
  73. #define HASH_STRING 3
  74. #define HASH_BINARY 4
  75. /*
  76. ** Access routines. To delete, insert a NULL pointer.
  77. */
  78. void HashInit(Hash*, int keytype, int copyKey);
  79. void *HashInsert(Hash*, const void *pKey, int nKey, void *pData);
  80. void *HashFind(const Hash*, const void *pKey, int nKey);
  81. void HashClear(Hash*);
  82. /*
  83. ** Macros for looping over all elements of a hash table. The idiom is
  84. ** like this:
  85. **
  86. ** Hash h;
  87. ** HashElem *p;
  88. ** ...
  89. ** for(p=HashFirst(&h); p; p=HashNext(p)){
  90. ** SomeStructure *pData = HashData(p);
  91. ** // do something with pData
  92. ** }
  93. */
  94. #define HashFirst(H) ((H)->first)
  95. #define HashNext(E) ((E)->next)
  96. #define HashData(E) ((E)->data)
  97. #define HashKey(E) ((E)->pKey)
  98. #define HashKeysize(E) ((E)->nKey)
  99. /*
  100. ** Number of entries in a hash table
  101. */
  102. #define HashCount(H) ((H)->count)
  103. #endif /* _HASH_H_ */