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.

209 lines
5.8 KiB

  1. /*++
  2. Copyright (c) 1999-1999 Microsoft Corporation
  3. Module Name:
  4. mapset.hxx
  5. Abstract:
  6. Generic maps and sets based on the Dictionary class.
  7. Author:
  8. Mike Zoran mzoran 11-Nov-99
  9. Revision History:
  10. Notes:
  11. The file contains template classes for simple generic maps and sets
  12. that use the Dictionary class as the internal storage. If pointers
  13. are stored, use the PTR versions of these classes for performance.
  14. Note that these classes do not assume ownership of any pointers inserted
  15. into the containers. The pointee as not freed when the structure is
  16. destroyed.
  17. --*/
  18. #ifndef __MAPSET_HXX__
  19. #define __MAPSET_HXX__
  20. #include <dict.hxx>
  21. ////////////////////////////////////////////////////////////////////////
  22. //
  23. // MAP
  24. //
  25. ////////////////////////////////////////////////////////////////////////
  26. template<class K, class V>
  27. class MAP : private Dictionary
  28. {
  29. public:
  30. MAP() : Dictionary() {}
  31. ~MAP() { Discard(); }
  32. BOOL Insert( K & NewKey, V & NewValue )
  33. {
  34. MapElement *pNewElement = new MapElement( NewKey, NewValue);
  35. Dict_Status Status = Dict_Insert( pNewElement );
  36. if (Status != SUCCESS)
  37. {
  38. delete pNewElement;
  39. return FALSE;
  40. }
  41. return TRUE;
  42. }
  43. BOOL Lookup( K & FindKey, V * pFindValue = NULL)
  44. {
  45. MapElement TempElement( FindKey );
  46. Dict_Status Status = Dict_Find( &TempElement );
  47. if (SUCCESS != Status )
  48. return FALSE;
  49. if ( pFindValue )
  50. *pFindValue = static_cast<MapElement*>( Dict_Item() )->Value;
  51. return TRUE;
  52. }
  53. BOOL Delete( K & pFindKey )
  54. {
  55. MapElement TempElement( FindKey );
  56. MapElement *pMapElement = &TempElement;
  57. Dict_Status Status = Dict_Find( &pMapElement );
  58. if (SUCCESS != Status)
  59. return FALSE;
  60. delete pMapElement;
  61. return TRUE;
  62. }
  63. void Discard()
  64. {
  65. MapElement *pMapElement = NULL;
  66. while ( pMapElement = static_cast<MapElement*>( Dict_Delete_One() ) )
  67. delete pMapElement;
  68. }
  69. private:
  70. struct MapElement
  71. {
  72. K Key;
  73. V Value;
  74. MapElement(K & NewKey, V & NewValue) :
  75. Key(NewKey),
  76. Value(NewValue) {}
  77. MapElement(K & NewKey) :
  78. Key(NewKey) {}
  79. };
  80. virtual
  81. SSIZE_T Compare (pUserType p1, pUserType p2)
  82. {
  83. if ( static_cast<MapElement*>( p1 )->Key == static_cast<MapElement*>( p2 )->Key )
  84. return 0;
  85. if ( static_cast<MapElement*>( p1 )->Key < static_cast<MapElement*>( p2 )->Key )
  86. return -1;
  87. return 1;
  88. }
  89. };
  90. template<class K, class V>
  91. class PTR_MAP : private MAP<INT_PTR,INT_PTR>
  92. {
  93. public:
  94. PTR_MAP() : MAP<INT_PTR,INT_PTR>() {}
  95. ~PTR_MAP() { Discard(); }
  96. BOOL Insert( K * NewKey, V * NewValue)
  97. {
  98. INT_PTR TempNewKey = reinterpret_cast<INT_PTR>( NewKey );
  99. INT_PTR TempNewValue = reinterpret_cast<INT_PTR>( NewValue);
  100. return MAP<INT_PTR,INT_PTR>::Insert(TempNewKey,TempNewValue);
  101. }
  102. BOOL Lookup( K * FindKey, V ** pFindValue = NULL)
  103. {
  104. INT_PTR TempFindKey = reinterpret_cast<INT_PTR>( FindKey );
  105. INT_PTR *pTempFindValue = reinterpret_cast<INT_PTR*>( pFindValue );
  106. return MAP<INT_PTR,INT_PTR>::Lookup( TempFindKey, pTempFindValue );
  107. }
  108. BOOL Delete( K * FindKey )
  109. {
  110. INT_PTR TempFindKey = reinterpret_cast<INT_PTR>( FindKey );
  111. return MAP<INT_PTR,INT_PTR>::Delete( TempFindKey );
  112. }
  113. void Discard() { MAP<INT_PTR,INT_PTR>::Discard(); }
  114. };
  115. ////////////////////////////////////////////////////////////////////////
  116. //
  117. // SET
  118. //
  119. ////////////////////////////////////////////////////////////////////////
  120. template<class T> class SET : private Dictionary
  121. {
  122. public:
  123. SET() : Dictionary() {}
  124. ~SET() { Discard(); }
  125. BOOL Insert( T & NewItem )
  126. {
  127. T *pNewItem = new T(NewItem);
  128. Dict_Status Status = Dict_Insert( pNewItem );
  129. if ( SUCCESS == Status )
  130. return TRUE;
  131. delete pNewItem;
  132. return FALSE;
  133. }
  134. BOOL Lookup( T & FindItem ) { return ( SUCCESS == Dict_Find( &NewItem ) ); }
  135. BOOL Delete( T & DeleteItem )
  136. {
  137. void* pDeleteItem = &DeleteItem;
  138. return (SUCCESS == Dict_Delete( &pDeleteItem ) );
  139. }
  140. void Discard()
  141. {
  142. T * pDeleteItem = NULL;
  143. while( ( pDeleteItem = static_cast<T*>( Dict_Delete_One() ) ) != NULL )
  144. delete pDeleteItem;
  145. }
  146. private:
  147. SSIZE_T Compare(pUserType p1, pUserType p2)
  148. {
  149. if ( *static_cast<T*>( p1 ) == *static_cast<T*>( p2 ) )
  150. return 0;
  151. if ( *static_cast<T*>( p1 ) < *static_cast<T*>( p2 ) )
  152. return -1;
  153. return 1;
  154. }
  155. };
  156. template<>
  157. class SET<void*> : private Dictionary
  158. {
  159. public:
  160. SET() : Dictionary() {}
  161. ~SET() { Discard(); }
  162. BOOL Insert( void *pNewItem ) { return ( SUCCESS == Dict_Insert( pNewItem ) ); }
  163. BOOL Lookup( void *pFindItem ) { return ( SUCCESS == Dict_Find( pFindItem ) ); }
  164. BOOL Delete( void *pDeleteItem ) { return ( SUCCESS == Dict_Delete( &pDeleteItem ) );}
  165. void Discard() { Dict_Discard(); }
  166. };
  167. template<class T>
  168. class PTR_SET : private SET<void*>
  169. {
  170. public:
  171. PTR_SET() : SET<void*>() {}
  172. ~PTR_SET() { Discard(); }
  173. BOOL Insert( T *pNewItem ) { return SET<void*>::Insert( pNewItem ); }
  174. BOOL Lookup( T *pFindItem ) { return SET<void*>::Lookup( pFindItem ); }
  175. BOOL Delete( T *pDeleteItem ) { return SET<void*>::Delete( pDeleteItem ); }
  176. void Discard() { SET<void*>::Discard(); }
  177. };
  178. #endif //__MAPSET_HXX__