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.

141 lines
5.0 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995.
  5. //
  6. // File: map_kv.h
  7. //
  8. // Contents:
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 10-29-1996 JohannP (Johann Posch) taken from OLE
  15. //
  16. //----------------------------------------------------------------------------
  17. #ifndef __MAP_KV_H__
  18. #define __MAP_KV_H__
  19. typedef void FAR* POSITION;
  20. #define BEFORE_START_POSITION ((POSITION)(ULONG_PTR)-1L)
  21. /////////////////////////////////////////////////////////////////////////////
  22. // class CMapKeyToValue - a mapping from 'KEY's to 'VALUE's, passed in as
  23. // pv/cb pairs. The keys can be variable length, although we optmizize the
  24. // case when they are all the same.
  25. //
  26. /////////////////////////////////////////////////////////////////////////////
  27. STDAPI_(UINT) MKVDefaultHashKey(LPVOID pKey, UINT cbKey);
  28. DECLARE_HANDLE(HMAPKEY);
  29. typedef UINT (STDAPICALLTYPE FAR* LPFNHASHKEY)(LPVOID, UINT);
  30. class FAR CMapKeyToValue
  31. {
  32. public:
  33. CMapKeyToValue(UINT cbValue, UINT cbKey = 0,
  34. int nBlockSize=10,
  35. LPFNHASHKEY lpfnHashKey = NULL,
  36. UINT nHashSize = 17);
  37. ~CMapKeyToValue();
  38. // number of elements
  39. int GetCount() const { return m_nCount; }
  40. BOOL IsEmpty() const { return m_nCount == 0; }
  41. // Lookup; return FALSE if not found
  42. BOOL Lookup(LPVOID pKey, UINT cbKey, LPVOID pValue) const;
  43. BOOL LookupHKey(HMAPKEY hKey, LPVOID pValue) const;
  44. BOOL LookupAdd(LPVOID pKey, UINT cbKey, LPVOID pValue) const;
  45. // add a new (key, value) pair; return FALSE if out of memory
  46. BOOL SetAt(LPVOID pKey, UINT cbKey, LPVOID pValue);
  47. BOOL SetAtHKey(HMAPKEY hKey, LPVOID pValue);
  48. // removing existing (key, ?) pair; return FALSE if no such key
  49. BOOL RemoveKey(LPVOID pKey, UINT cbKey);
  50. BOOL RemoveHKey(HMAPKEY hKey);
  51. void RemoveAll();
  52. // iterating all (key, value) pairs
  53. POSITION GetStartPosition() const
  54. { return (m_nCount == 0) ? (POSITION)NULL : BEFORE_START_POSITION; }
  55. void GetNextAssoc(POSITION FAR* pNextPosition, LPVOID pKey,
  56. UINT FAR* pcbKey, LPVOID pValue) const;
  57. // return HMAPKEY for given key; returns NULL if not currently in map
  58. HMAPKEY GetHKey(LPVOID pKey, UINT cbKey) const;
  59. void AssertValid() const;
  60. #ifndef unix
  61. private:
  62. #else
  63. // If this was not made public we get complier warnings
  64. // that CKeyWrap is not accessible from CAssoc declared below
  65. // which means global functions cant return CNode pointers
  66. public:
  67. #endif /* unix */
  68. // abstracts, somewhat, variable and fixed sized keys; size is really
  69. // m_cbKeyInAssoc.
  70. union CKeyWrap
  71. {
  72. BYTE rgbKey[sizeof(LPVOID) + sizeof(UINT)];
  73. struct
  74. {
  75. LPVOID pKey;
  76. UINT cbKey;
  77. }
  78. #ifdef unix
  79. n
  80. #endif /* unix */
  81. ;
  82. };
  83. // Association of one key and one value; NOTE: even though in general
  84. // the size of the key and value varies, for any given map,
  85. // the size of an assoc is fixed.
  86. struct CAssoc
  87. {
  88. CAssoc FAR* pNext;
  89. UINT nHashValue; // needed for efficient iteration
  90. CKeyWrap key; // size is really m_cbKeyInAssoc
  91. // BYTE rgbValue[m_cbValue];
  92. };
  93. #ifdef unix
  94. private:
  95. #endif /* unix */
  96. UINT SizeAssoc() const
  97. { return sizeof(CAssoc)-sizeof(CKeyWrap) + m_cbKeyInAssoc + m_cbValue; }
  98. CAssoc FAR* NewAssoc(UINT hash, LPVOID pKey, UINT cbKey, LPVOID pValue);
  99. void FreeAssoc(CAssoc FAR* pAssoc);
  100. BOOL CompareAssocKey(CAssoc FAR* pAssoc, LPVOID pKey, UINT cbKey) const;
  101. CAssoc FAR* GetAssocAt(LPVOID pKey, UINT cbKey, UINT FAR& nHash) const;
  102. BOOL SetAssocKey(CAssoc FAR* pAssoc, LPVOID pKey, UINT cbKey) const;
  103. void GetAssocKeyPtr(CAssoc FAR* pAssoc, LPVOID FAR* ppKey,UINT FAR* pcbKey) const;
  104. void FreeAssocKey(CAssoc FAR* pAssoc) const;
  105. void GetAssocValuePtr(CAssoc FAR* pAssoc, LPVOID FAR* ppValue) const;
  106. void GetAssocValue(CAssoc FAR* pAssoc, LPVOID pValue) const;
  107. void SetAssocValue(CAssoc FAR* pAssoc, LPVOID pValue) const;
  108. BOOL InitHashTable();
  109. UINT m_cbValue;
  110. UINT m_cbKey; // variable length if 0
  111. UINT m_cbKeyInAssoc; // always non-zero
  112. CAssoc FAR* FAR* m_pHashTable;
  113. UINT m_nHashTableSize;
  114. LPFNHASHKEY m_lpfnHashKey;
  115. int m_nCount;
  116. CAssoc FAR* m_pFreeList;
  117. struct CPlex FAR* m_pBlocks;
  118. int m_nBlockSize;
  119. };
  120. #endif // !__MAP_KV_H__