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.

114 lines
3.8 KiB

  1. #ifndef __MAP_KV_H__
  2. #define __MAP_KV_H__
  3. /////////////////////////////////////////////////////////////////////////////
  4. // class CMapKeyToValue - a mapping from 'KEY's to 'VALUE's, passed in as
  5. // pv/cb pairs. The keys can be variable length, although we optmizize the
  6. // case when they are all the same.
  7. //
  8. /////////////////////////////////////////////////////////////////////////////
  9. STDAPI_(UINT) MKVDefaultHashKey(LPVOID pKey, UINT cbKey);
  10. #ifdef WIN32
  11. DECLARE_HANDLE(HMAPKEY);
  12. #else
  13. DECLARE_HANDLE32(HMAPKEY);
  14. #endif
  15. typedef UINT (STDAPICALLTYPE FAR* LPFNHASHKEY)(LPVOID, UINT);
  16. class FAR CMapKeyToValue
  17. {
  18. public:
  19. CMapKeyToValue(DWORD memctx, UINT cbValue, UINT cbKey = 0,
  20. int nBlockSize=10, LPFNHASHKEY lpfnHashKey = &MKVDefaultHashKey,
  21. UINT nHashSize = 17);
  22. ~CMapKeyToValue();
  23. // number of elements
  24. int GetCount() const { return m_nCount; }
  25. BOOL IsEmpty() const { return m_nCount == 0; }
  26. // Lookup; return FALSE if not found
  27. BOOL Lookup(LPVOID pKey, UINT cbKey, LPVOID pValue) const;
  28. BOOL LookupHKey(HMAPKEY hKey, LPVOID pValue) const;
  29. BOOL LookupAdd(LPVOID pKey, UINT cbKey, LPVOID pValue) const;
  30. // add a new (key, value) pair; return FALSE if out of memory
  31. BOOL SetAt(LPVOID pKey, UINT cbKey, LPVOID pValue);
  32. BOOL SetAtHKey(HMAPKEY hKey, LPVOID pValue);
  33. // removing existing (key, ?) pair; return FALSE if no such key
  34. BOOL RemoveKey(LPVOID pKey, UINT cbKey);
  35. BOOL RemoveHKey(HMAPKEY hKey);
  36. void RemoveAll();
  37. // iterating all (key, value) pairs
  38. POSITION GetStartPosition() const
  39. { return (m_nCount == 0) ? (POSITION)NULL : BEFORE_START_POSITION; }
  40. void GetNextAssoc(POSITION FAR* pNextPosition, LPVOID pKey,
  41. UINT FAR* pcbKey, LPVOID pValue) const;
  42. // return HMAPKEY for given key; returns NULL if not currently in map
  43. HMAPKEY GetHKey(LPVOID pKey, UINT cbKey) const;
  44. void AssertValid() const;
  45. private:
  46. // abstracts, somewhat, variable and fixed sized keys; size is really
  47. // m_cbKeyInAssoc.
  48. union CKeyWrap
  49. {
  50. BYTE rgbKey[sizeof(LPVOID) + sizeof(UINT)];
  51. struct
  52. {
  53. LPVOID pKey;
  54. UINT cbKey;
  55. };
  56. };
  57. // Association of one key and one value; NOTE: even though in general
  58. // the size of the key and value varies, for any given map,
  59. // the size of an assoc is fixed.
  60. struct CAssoc
  61. {
  62. CAssoc FAR* pNext;
  63. UINT nHashValue; // needed for efficient iteration
  64. CKeyWrap key; // size is really m_cbKeyInAssoc
  65. // BYTE rgbValue[m_cbValue];
  66. };
  67. UINT SizeAssoc() const
  68. { return sizeof(CAssoc)-sizeof(CKeyWrap) + m_cbKeyInAssoc + m_cbValue; }
  69. CAssoc FAR* NewAssoc(UINT hash, LPVOID pKey, UINT cbKey, LPVOID pValue);
  70. void FreeAssoc(CAssoc FAR* pAssoc);
  71. BOOL CompareAssocKey(CAssoc FAR* pAssoc, LPVOID pKey, UINT cbKey) const;
  72. CAssoc FAR* GetAssocAt(LPVOID pKey, UINT cbKey, UINT FAR& nHash) const;
  73. BOOL SetAssocKey(CAssoc FAR* pAssoc, LPVOID pKey, UINT cbKey) const;
  74. void GetAssocKeyPtr(CAssoc FAR* pAssoc, LPVOID FAR* ppKey,UINT FAR* pcbKey) const;
  75. void FreeAssocKey(CAssoc FAR* pAssoc) const;
  76. void GetAssocValuePtr(CAssoc FAR* pAssoc, LPVOID FAR* ppValue) const;
  77. void GetAssocValue(CAssoc FAR* pAssoc, LPVOID pValue) const;
  78. void SetAssocValue(CAssoc FAR* pAssoc, LPVOID pValue) const;
  79. BOOL InitHashTable();
  80. UINT m_cbValue;
  81. UINT m_cbKey; // variable length if 0
  82. UINT m_cbKeyInAssoc; // always non-zero
  83. CAssoc FAR* FAR* m_pHashTable;
  84. UINT m_nHashTableSize;
  85. LPFNHASHKEY m_lpfnHashKey;
  86. int m_nCount;
  87. CAssoc FAR* m_pFreeList;
  88. struct CPlex FAR* m_pBlocks;
  89. int m_nBlockSize;
  90. DWORD m_memctx;
  91. };
  92. #endif // !__MAP_KV_H__