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.

98 lines
3.7 KiB

  1. /* hash.h
  2. *
  3. * Copyright (c) 1996 by Microsoft Corporation
  4. *
  5. * Written by: Christos Tsollis
  6. *
  7. * Revisions:
  8. *
  9. * Abstract:
  10. *
  11. * This is the interface to a dictionary data structure.
  12. * Both the key and the value in a dictionary entry are DWORD values. So, for example, if the
  13. * value is really a pointer it has to be converted into a DWORD before being passed into a
  14. * member dictionary function.
  15. *
  16. */
  17. #ifndef _HASH_
  18. #define _HASH_
  19. #include <windows.h>
  20. #define DEFAULT_NUMBER_OF_BUCKETS 3
  21. typedef enum {
  22. DWORD_DICTIONARY, /* The key is a 32-bit unsigned value */
  23. STRING_DICTIONARY, /* The key is a NULL-terminated string that is being pointed by
  24. * the "key" field in the structure below */
  25. LENGTH_STRING_DICTIONARY /* The key is a string with a specific length. The "key" field
  26. * in the structure below, points to memory space containing
  27. * the length and a string of that length. */
  28. } DictionaryType;
  29. typedef struct _dictionary_item {
  30. DWORD key; // The key value, or a pointer to a string (depending on the dictionary type)
  31. DWORD value; // This is always a 32-bit unsigned value
  32. struct _dictionary_item *next; // Pointer to the next structure in the dictionary bucket
  33. } DICTIONARY_ITEM, *PDICTIONARY_ITEM;
  34. class DictionaryClass
  35. {
  36. public:
  37. DictionaryClass (ULong num_of_buckets = DEFAULT_NUMBER_OF_BUCKETS, DictionaryType dtype = DWORD_DICTIONARY);
  38. DictionaryClass (const DictionaryClass& original);
  39. ~DictionaryClass ();
  40. BOOL insert (DWORD new_key, DWORD new_value, ULong length = 0);
  41. BOOL remove (DWORD Key, ULong length = 0);
  42. BOOL find (DWORD Key, LPDWORD pValue = NULL, ULong length = 0);
  43. BOOL isEmpty ();
  44. void clear ();
  45. ULong entries () {
  46. return (3 * NumOfBuckets - ItemCount + NumOfExternItems);
  47. };
  48. BOOL iterate (LPDWORD pValue = NULL, LPDWORD pKey = NULL);
  49. void reset () { pCurrent = NULL; }; // Resets the dictionary iterator
  50. BOOL Insert(DWORD new_key, LPVOID new_value, UINT length = 0) { ASSERT(new_value != NULL); return insert(new_key, (DWORD) new_value, (ULONG) length); }
  51. BOOL Remove(DWORD Key, UINT length = 0) { return remove(Key, (ULONG) length); }
  52. LPVOID Find(DWORD Key, UINT length = 0);
  53. LPVOID Iterate(LPDWORD pKey = NULL);
  54. BOOL IsEmpty(void) { return isEmpty(); }
  55. void Clear(void) { clear(); }
  56. UINT GetCount(void) { return (UINT) entries(); }
  57. void Reset(void) { reset(); }
  58. private:
  59. DWORD hashFunction (DWORD key);
  60. int LengthStrcmp (DWORD DictionaryKey, DWORD ChallengeKey, ULong length);
  61. ULong NumOfBuckets; // Number of dictionary buckets. Specified during object construction.
  62. DWORD dwNormalSize; // Initial space allocated for the dictionary
  63. PDICTIONARY_ITEM *Buckets; // Address of the Buckets array
  64. PDICTIONARY_ITEM *ItemArray; // Pointer to the array of initially allocated dictionary items
  65. ULong ItemCount; // Number of dictionary items left in the ItemArray
  66. PDICTIONARY_ITEM pCurrent; // Points to the current dictionary item while we iterate through the dictionary
  67. ULong ulCurrentBucket; // Id of the current bucket while we iterate
  68. DictionaryType Type; // Dictionary type
  69. ULong NumOfExternItems; // Number of external dictionary items
  70. };
  71. typedef DictionaryClass * PDictionaryClass;
  72. #define DEFINE_DICTIONARY_FRIENDLY_METHODS(_ClassName_) \
  73. BOOL Insert(DWORD new_key, _ClassName_ *new_value, UINT length = 0) { return DictionaryClass::Insert(new_key, (LPVOID) new_value, length); } \
  74. BOOL Remove(DWORD Key, UINT length = 0) { return DictionaryClass::Remove(Key, length); } \
  75. _ClassName_ *Find(DWORD Key, UINT length = 0) { return (_ClassName_ *) DictionaryClass::Find(Key, length); } \
  76. _ClassName_ *Iterate(LPDWORD pKey = NULL) { return (_ClassName_ *) DictionaryClass::Iterate(pKey); }
  77. #endif