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.

142 lines
4.4 KiB

  1. /*---------------------------------------------------------------------------
  2. File: VarMap.h
  3. Comments: A map string=>Variant, used by VarSet. It is implemented as a hash
  4. table, win and optional red-black tree index.
  5. Added features include:
  6. CaseSensitive property - The case of each key is preserved as it was when
  7. the key was first added to the map. The hash function is not case sensitive,
  8. so the CaseSensitive property can be toggled on and off without rehashing the data.
  9. Optional indexing to allow for fast enumeration in alphabetical order by key.
  10. This will add overhead to insert operations. Without indexing, the contents of
  11. the map can be enumerated, but they will be in arbitrary order.
  12. Stream I/O functions for persistance.
  13. (c) Copyright 1995-1998, Mission Critical Software, Inc., All Rights Reserved
  14. Proprietary and confidential to Mission Critical Software, Inc.
  15. REVISION LOG ENTRY
  16. Revision By: Christy Boles
  17. Revised on 11/19/98 18:03:19
  18. ---------------------------------------------------------------------------
  19. */
  20. #ifndef __VARSETMAP_H
  21. #define __VARSETMAP_H
  22. #include "VarData.h"
  23. #include "VarNdx.h"
  24. class CHashItem // used internally by hash table
  25. {
  26. friend class CMapStringToVar;
  27. friend class CIndexItem;
  28. CHashItem() { pNext = NULL; value = NULL; pIndex = NULL; }
  29. CHashItem* pNext; // used in hash table
  30. UINT nHashValue; // needed for efficient iteration
  31. CString key;
  32. CVarData* value;
  33. CIndexItem* pIndex; // pointer to index, or NULL
  34. };
  35. class CMapStringToVar : public CObject
  36. {
  37. DECLARE_SERIAL(CMapStringToVar)
  38. public:
  39. // Construction
  40. CMapStringToVar(BOOL isCaseSensitive,BOOL isIndexed, BOOL allowRehash, int nBlockSize = 10);
  41. protected:
  42. CMapStringToVar() {};
  43. public:
  44. // Attributes
  45. // number of elements
  46. int GetCount() const { return m_nCount; }
  47. BOOL IsEmpty() const { return m_nCount == 0; }
  48. // Lookup
  49. BOOL Lookup(LPCTSTR key, CVarData*& rValue) const;
  50. BOOL LookupKey(LPCTSTR key, LPCTSTR& rKey) const;
  51. // Operations
  52. // Lookup and add if not there
  53. CVarData*& operator[](LPCTSTR key);
  54. // add a new (key, value) pair
  55. void SetAt(LPCTSTR key, CVarData* newValue) { (*this)[key] = newValue; }
  56. BOOL RemoveKey(LPCTSTR key);
  57. void RemoveAll();
  58. POSITION GetStartPosition() const { return (m_nCount == 0) ? NULL : BEFORE_START_POSITION; }
  59. void GetNextAssoc(POSITION& rNextPosition, CString& rKey, CVarData*& rValue) const;
  60. POSITION GetPositionAt(LPCTSTR key) { UINT hash; return (POSITION)GetAssocAt(key,hash); }
  61. CIndexItem * GetIndexAt(LPCTSTR key) { UINT hash; CHashItem * h = GetAssocAt(key,hash); if ( h ) return h->pIndex; else return NULL; }
  62. UINT GetHashTableSize() const { return m_nHashTableSize; }
  63. void InitHashTable(UINT hashSize, BOOL bAllocNow = TRUE);
  64. UINT HashKey(LPCTSTR key) const;
  65. void SetCaseSensitive(BOOL val) { m_CaseSensitive = val;
  66. m_Index.SetCompareFunctions(val? &CompareItems : &CompareItemsNoCase,
  67. val? CompareStringToItem : CompareStringToItemNoCase); }
  68. void SetIndexed(BOOL val);
  69. void SetAllowRehash(BOOL val) { m_AllowRehash = val; }
  70. HRESULT ReadFromStream(LPSTREAM pStm);
  71. HRESULT WriteToStream(LPSTREAM pStm);
  72. DWORD CalculateStreamedLength();
  73. long CountItems();
  74. CIndexTree * GetIndex() { if ( m_Indexed ) return &m_Index; else return NULL; }
  75. void McLogInternalDiagnostics(CString keyName);
  76. // Implementation
  77. protected:
  78. // Hash table stuff
  79. CHashItem** m_pHashTable;
  80. UINT m_nHashTableSize;
  81. UINT m_nCount;
  82. CHashItem* m_pFreeList;
  83. struct CPlex* m_pBlocks;
  84. int m_nBlockSize;
  85. CHashItem* NewAssoc();
  86. void FreeAssoc(CHashItem*);
  87. CHashItem* GetAssocAt(LPCTSTR, UINT&) const;
  88. void BuildIndex();
  89. void ResizeTable();
  90. BOOL m_CaseSensitive;
  91. BOOL m_Indexed;
  92. BOOL m_AllowRehash;
  93. CIndexTree m_Index;
  94. public:
  95. ~CMapStringToVar();
  96. void Serialize(CArchive&);
  97. #ifdef _DEBUG
  98. void Dump(CDumpContext&) const;
  99. void AssertValid() const;
  100. #endif
  101. };
  102. ////////////////////////////////////////////////////////////////
  103. #endif // __VARSETMAP