Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.7 KiB

  1. /*++
  2. Copyright (c) 1999, Microsoft Corporation
  3. Module Name:
  4. sample\hashtable.h
  5. Abstract:
  6. The file contains the header for hashtable.c.
  7. --*/
  8. #ifndef _HASH_TABLE_H_
  9. #define _HASH_TABLE_H_
  10. typedef VOID (*PVOID_FUNCTION)
  11. (PLIST_ENTRY pleEntry);
  12. typedef PVOID_FUNCTION PDISPLAY_FUNCTION;
  13. typedef PVOID_FUNCTION PFREE_FUNCTION;
  14. typedef ULONG (*PHASH_FUNCTION)
  15. (PLIST_ENTRY pleEntry);
  16. // < 0 KeyEntry less than TableEntry
  17. // 0 KeyEntry identical TableEntry
  18. // > 0 KeyEntry more than TableEntry
  19. typedef LONG (*PCOMPARE_FUNCTION)
  20. (PLIST_ENTRY pleTableEntry,
  21. PLIST_ENTRY pleKeyEntry);
  22. typedef struct _HASH_TABLE
  23. {
  24. ULONG ulNumBuckets; // # buckets in hash table
  25. ULONG ulNumEntries; // # entries in hash table
  26. PDISPLAY_FUNCTION pfnDisplay; // display an entry (optional)
  27. PFREE_FUNCTION pfnFree; // free an entry
  28. PHASH_FUNCTION pfnHash; // hash an entry
  29. PCOMPARE_FUNCTION pfnCompare; // compare two entries
  30. PLIST_ENTRY pleBuckets; // the buckets
  31. } HASH_TABLE, *PHASH_TABLE;
  32. // Create the hash table
  33. DWORD
  34. HT_Create(
  35. IN HANDLE hHeap,
  36. IN ULONG ulNumBuckets,
  37. IN PDISPLAY_FUNCTION pfnDisplay OPTIONAL,
  38. IN PFREE_FUNCTION pfnFree,
  39. IN PHASH_FUNCTION pfnHash,
  40. IN PCOMPARE_FUNCTION pfnCompare,
  41. OUT PHASH_TABLE *pphtHashTable);
  42. // Destroy the hash table
  43. DWORD
  44. HT_Destroy(
  45. IN HANDLE hHeap,
  46. IN PHASH_TABLE phtTable);
  47. // Clean hash table by destroying all entries
  48. DWORD
  49. HT_Cleanup(
  50. IN PHASH_TABLE phtHashTable);
  51. // Display all entries in the hash table
  52. #define HT_Display(phtHashTable) \
  53. { \
  54. if (phtHashTable) \
  55. HT_MapCar(phtHashTable, phtHashTable->pfnDisplay); \
  56. }
  57. // # entries in the hash table
  58. #define HT_Size(phtHashTable) \
  59. ( \
  60. phtHashTable->ulNumEntries \
  61. )
  62. // Is hash table empty?
  63. #define HT_IsEmpty(phtHashTable) \
  64. ( \
  65. HT_Size(phtHashTable) is 0 \
  66. )
  67. // Inserts an entry in the hash table
  68. DWORD
  69. HT_InsertEntry(
  70. IN PHASH_TABLE phtHashTable,
  71. IN PLIST_ENTRY pleEntry);
  72. // Gets the hash table entry with the given key
  73. DWORD
  74. HT_GetEntry(
  75. IN PHASH_TABLE phtHashTable,
  76. IN PLIST_ENTRY pleKey,
  77. OUT PLIST_ENTRY *ppleEntry);
  78. // Delete an entry from the hash table
  79. DWORD
  80. HT_DeleteEntry(
  81. IN PHASH_TABLE phtHashTable,
  82. IN PLIST_ENTRY pleKey,
  83. OUT PLIST_ENTRY *ppleEntry);
  84. // Remove this entry from the hash table
  85. #define HT_RemoveEntry(phtHashTable, pleEntry) \
  86. ( \
  87. RemoveEntryList(pleEntry) \
  88. phtHashTable->ulNumEntries--; \
  89. )
  90. DWORD
  91. HT_DeleteEntry(
  92. IN PHASH_TABLE phtHashTable,
  93. IN PLIST_ENTRY pleKey,
  94. OUT PLIST_ENTRY *ppleEntry);
  95. // Is key present in the hash table?
  96. BOOL
  97. HT_IsPresentEntry(
  98. IN PHASH_TABLE phtHashTable,
  99. IN PLIST_ENTRY pleKey);
  100. // Apply the specified function to all entries in the hash table
  101. DWORD
  102. HT_MapCar(
  103. IN PHASH_TABLE phtHashTable,
  104. IN PVOID_FUNCTION pfnVoidFunction);
  105. #endif // _HASH_TABLE_H_