Leaked source code of windows server 2003
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.

105 lines
4.0 KiB

  1. #ifndef __STRING_TABLE_H
  2. #define __STRING_TABLE_H
  3. ///////////////////////////////////////////////////////////////////////////////
  4. // Class: StringTable
  5. //
  6. // This class implements a simple hash table for storing text strings.
  7. // The purpose of the table is to store strings and then verify later
  8. // if the table contains a given string. Since there is no data associated
  9. // with the string, the stored strings act as both key and data. Therefore,
  10. // there is no requirement for string retrieval. Only existence checks
  11. // are required.
  12. // The structure maintains a fixed-length array of pointers, each pointing
  13. // to a linked list structure (List). These lists are used to handle the
  14. // problem of hash collisions (sometimes known as "separate chaining").
  15. //
  16. // Note that these classes do not contain all the stuff that is usually
  17. // considered necessary in C++ classes. Things like copy constructors,
  18. // assignment operator, type conversion etc are excluded. The classes
  19. // are very specialized for the Font Folder application and these things
  20. // would be considered "fat". Should this hash table class be later used
  21. // in a situation where these things are needed, they can be added then.
  22. //
  23. // The public interfaces to the table are:
  24. //
  25. // Initialize - Initialize a new string table.
  26. // Add - Add a new string to a table.
  27. // Exists - Determine if a string exists in a table.
  28. // Count - Return the number of strings in a table.
  29. //
  30. // Destruction of the object automatically releases all memory associated
  31. // with the table.
  32. //
  33. // BrianAu - 4/11/96
  34. ///////////////////////////////////////////////////////////////////////////////
  35. #include <windows.h>
  36. #include <tchar.h>
  37. //
  38. // Hash table containing text strings.
  39. //
  40. class StringTable {
  41. private:
  42. //
  43. // Linked list for hash collisions.
  44. //
  45. class List {
  46. private:
  47. //
  48. // Element in hash collision list.
  49. //
  50. class Element {
  51. public:
  52. LPTSTR m_pszText; // Pointer to string text.
  53. Element *m_pNext; // Pointer to next in list.
  54. Element(void);
  55. ~Element(void);
  56. BOOL Initialize(LPCTSTR pszItem);
  57. BOOL operator == (const Element& ele) const;
  58. BOOL operator != (const Element& ele) const;
  59. #ifdef DEBUG
  60. void DebugOut(void) const;
  61. #endif
  62. };
  63. Element *m_pHead; // Ptr to head of list;
  64. DWORD m_dwCount; // Count of elements in list.
  65. public:
  66. List(void);
  67. ~List(void);
  68. BOOL Add(LPCTSTR pszText, BOOL bAllowDuplicates = TRUE);
  69. BOOL Exists(LPCTSTR pszText) const;
  70. DWORD Count(void) const { return m_dwCount; }
  71. #ifdef DEBUG
  72. void DebugOut(void) const;
  73. #endif
  74. };
  75. List **m_apLists; // Array of ptrs to collision lists.
  76. DWORD m_dwItemCount; // Number of items in table.
  77. DWORD m_dwHashBuckets; // Number of pointers in hash array.
  78. BOOL m_bCaseSensitive; // Key strings treated case-sensitive?
  79. BOOL m_bAllowDuplicates; // Allow duplicate strings?
  80. DWORD Hash(LPCTSTR pszText) const;
  81. LPTSTR StringTable::CreateUpperCaseString(LPCTSTR pszText) const;
  82. BOOL Exists(DWORD dwHashCode, LPCTSTR pszText);
  83. public:
  84. StringTable(void);
  85. ~StringTable(void);
  86. HRESULT Initialize(DWORD dwHashBuckets,
  87. BOOL bCaseSensitive = TRUE,
  88. BOOL bAllowDuplicates = FALSE);
  89. void Destroy(void);
  90. BOOL Add(LPCTSTR pszText);
  91. BOOL Exists(LPCTSTR pszText);
  92. DWORD Count(void) const { return m_dwItemCount; }
  93. #ifdef DEBUG
  94. void DebugOut(void) const;
  95. #endif
  96. };
  97. #endif