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.

260 lines
5.2 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. FTMan
  5. File Name:
  6. Set.h
  7. Abstract:
  8. Definition and implementation of template class CSet.
  9. CSet is a set of elements of the same type. The type of the elements must have a equivalence operator ("==")
  10. defined.
  11. No duplicate elements are allowed in the set. The elements are stored in ascendent sort order
  12. Main set operations:
  13. - Reunion
  14. - Intersection
  15. - Difference
  16. Author:
  17. Cristian Teodorescu November 4, 1998
  18. Notes:
  19. Revision History:
  20. --*/
  21. /////////////////////////////////////////////////////////////////////////////
  22. #if !defined(AFX_SET_H_INCLUDED_)
  23. #define AFX_SET_H_INCLUDED_
  24. #if _MSC_VER > 1000
  25. #pragma once
  26. #endif // _MSC_VER > 1000
  27. #include <afxtempl.h>
  28. template< class TYPE, class ARG_TYPE >
  29. class CSet : protected CArray<TYPE, ARG_TYPE>
  30. {
  31. // Public constructors
  32. public:
  33. CSet() {};
  34. CSet( const CSet& set );
  35. // Public operations
  36. public:
  37. // Check if the set is empty
  38. BOOL IsEmpty() const;
  39. // Get the size of the set
  40. int GetSize( ) const;
  41. // Check if an element is in the set
  42. BOOL InSet( ARG_TYPE elm ) const;
  43. // Add a element to the set
  44. void Add( ARG_TYPE elm );
  45. // Remove a element from the set
  46. void Remove( ARG_TYPE elm );
  47. // Remove all elements from the set
  48. void RemoveAll();
  49. // Subscript operator
  50. TYPE operator []( int nIndex ) const;
  51. // Assignment operator
  52. void operator=( const CSet& set );
  53. // Reunion of two sets
  54. void operator+=( const CSet& set );
  55. //const CSet& operator+( const CSet& set ) const;
  56. // Intersection of two sets
  57. void operator*=(const CSet& set );
  58. //const CSet& operator*( const CSet& set ) const;
  59. // Difference of two sets
  60. void operator-=( const CSet& set );
  61. //const CSet& operator-( const CSet& set ) const;
  62. };
  63. // Off-line methods
  64. // Copy constructor
  65. template< class TYPE, class ARG_TYPE >
  66. CSet<TYPE, ARG_TYPE>::CSet( const CSet& set )
  67. {
  68. MY_TRY
  69. for( int i = 0; i < set.GetSize(); i++ )
  70. CArray<TYPE, ARG_TYPE>::Add( set[i] );
  71. MY_CATCH_AND_THROW
  72. }
  73. // Check if the set is empty
  74. template< class TYPE, class ARG_TYPE >
  75. BOOL CSet<TYPE, ARG_TYPE>::IsEmpty() const
  76. {
  77. return ( GetSize() == 0 );
  78. }
  79. template< class TYPE, class ARG_TYPE >
  80. int CSet<TYPE, ARG_TYPE>::GetSize() const
  81. {
  82. return (int)CArray<TYPE, ARG_TYPE>::GetSize();
  83. }
  84. // Check if an element is in the set
  85. template< class TYPE, class ARG_TYPE >
  86. BOOL CSet<TYPE, ARG_TYPE>::InSet( ARG_TYPE elm ) const
  87. {
  88. // The array is sorted !!
  89. for( int i = 0; i < GetSize(); i++ )
  90. {
  91. if( GetAt(i) > elm )
  92. return FALSE;
  93. else if( GetAt(i) == elm )
  94. return TRUE;
  95. }
  96. return FALSE;
  97. }
  98. // Add a element to the set
  99. template< class TYPE, class ARG_TYPE >
  100. void CSet<TYPE, ARG_TYPE>::Add( ARG_TYPE elm )
  101. {
  102. MY_TRY
  103. // No duplicates are allowed
  104. // The new element is inserted in the right place in the ascending sorted array
  105. for( int i = 0; i < GetSize(); i++ )
  106. {
  107. if( GetAt(i) > elm )
  108. break;
  109. else if( GetAt(i) == elm )
  110. return;
  111. }
  112. InsertAt( i, elm );
  113. MY_CATCH_AND_THROW
  114. }
  115. // Remove a element from the set
  116. template< class TYPE, class ARG_TYPE >
  117. void CSet<TYPE, ARG_TYPE>::Remove( ARG_TYPE elm )
  118. {
  119. // The array is sorted !!
  120. for( int i = 0; i < GetSize(); i++ )
  121. {
  122. if( GetAt(i) > elm )
  123. return;
  124. else if( GetAt(i) == elm )
  125. {
  126. RemoveAt(i);
  127. return;
  128. }
  129. }
  130. }
  131. // Remove all elements from the set
  132. template< class TYPE, class ARG_TYPE >
  133. void CSet<TYPE, ARG_TYPE>::RemoveAll()
  134. {
  135. CArray<TYPE, ARG_TYPE>::RemoveAll();
  136. }
  137. // Subscript operator
  138. template< class TYPE, class ARG_TYPE >
  139. TYPE CSet<TYPE, ARG_TYPE>::operator []( int nIndex ) const
  140. {
  141. return CArray<TYPE, ARG_TYPE>::operator[]( nIndex );
  142. }
  143. // Assignment operator
  144. template< class TYPE, class ARG_TYPE >
  145. void CSet<TYPE, ARG_TYPE>::operator=( const CSet& set )
  146. {
  147. MY_TRY
  148. RemoveAll();
  149. for( int i = 0; i < set.GetSize(); i++ )
  150. CArray<TYPE, ARG_TYPE>::Add( set[i] );
  151. MY_CATCH_AND_THROW
  152. }
  153. // Reunion of two sets. The result is stored in the first operand
  154. template< class TYPE, class ARG_TYPE >
  155. void CSet<TYPE, ARG_TYPE>::operator+=( const CSet& set )
  156. {
  157. MY_TRY
  158. for( int i = 0; i < set.GetSize(); i++ )
  159. Add( set[i] );
  160. MY_CATCH_AND_THROW
  161. }
  162. // Reunion of two sets. The result is stored in a third set
  163. /*
  164. template< class TYPE, class ARG_TYPE >
  165. const CSet<TYPE, ARG_TYPE>& CSet<TYPE, ARG_TYPE>::operator+( const CSet& set ) const
  166. {
  167. }
  168. */
  169. // Intersection of two sets. The result is stored in the first operand
  170. template< class TYPE, class ARG_TYPE >
  171. void CSet<TYPE, ARG_TYPE>::operator*=( const CSet& set )
  172. {
  173. for( int i = GetSize()-1; i >= 0; i-- )
  174. {
  175. ARG_TYPE elm = GetAt(i);
  176. if( !set.InSet(elm) )
  177. RemoveAt(i);
  178. }
  179. }
  180. // Intersection of two sets. The result is stored in a third set
  181. /*
  182. template< class TYPE, class ARG_TYPE >
  183. const CSet<TYPE, ARG_TYPE>& CSet<TYPE, ARG_TYPE>::operator*( const CSet& set ) const
  184. {
  185. }
  186. */
  187. // Difference of two sets. The result is stored in the first operand
  188. template< class TYPE, class ARG_TYPE >
  189. void CSet<TYPE, ARG_TYPE>::operator-=( const CSet& set )
  190. {
  191. for( int i = GetSize()-1; i >= 0; i-- )
  192. {
  193. ARG_TYPE elm = GetAt(i);
  194. if( set.InSet(elm) )
  195. RemoveAt[i];
  196. }
  197. }
  198. // Difference of two sets. The result is stored in a third set
  199. /*
  200. template< class TYPE, class ARG_TYPE >
  201. const CSet<TYPE, ARG_TYPE>& CSet<TYPE, ARG_TYPE>::operator-( const CSet& set ) const
  202. {
  203. }
  204. */
  205. #endif // !defined(AFX_SET_H_INCLUDED_)
  206.