strtab.h
Upload User: caisha3
Upload Date: 2013-09-21
Package Size: 208739k
Code Size: 4k
Category:

Windows Develop

Development Platform:

Visual C++

  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.         CRITICAL_SECTION m_cs;    // Protect from concurrent access.
  76.         List **m_apLists;         // Array of ptrs to collision lists.
  77.         DWORD m_dwItemCount;      // Number of items in table.
  78.         DWORD m_dwHashBuckets;    // Number of pointers in hash array.
  79.         BOOL  m_bCaseSensitive;   // Key strings treated case-sensitive?
  80.         BOOL  m_bAllowDuplicates; // Allow duplicate strings?
  81.         DWORD Hash(LPCTSTR pszText) const;
  82.         LPTSTR StringTable::CreateUpperCaseString(LPCTSTR pszText) const;
  83.         BOOL Exists(DWORD dwHashCode, LPCTSTR pszText);
  84.     public:
  85.         StringTable(void);
  86.         ~StringTable(void);
  87.         BOOL Initialize(DWORD dwHashBuckets, 
  88.                         BOOL bCaseSensitive   = TRUE,
  89.                         BOOL bAllowDuplicates = FALSE);
  90.         BOOL IsInitialized(void);
  91.         void Destroy(void);
  92.         BOOL Add(LPCTSTR pszText);
  93.         BOOL Exists(LPCTSTR pszText);
  94.         DWORD Count(void) const { return m_dwItemCount; }
  95. #ifdef DEBUG
  96.         void DebugOut(void) const;
  97. #endif
  98. };
  99. #endif