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.

145 lines
3.0 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1999.
  5. //
  6. // File: set.hxx
  7. //
  8. // Contents:
  9. //
  10. // History: 26 Mar 1999 MihaiPS Created
  11. //
  12. //--------------------------------------------------------------------------
  13. #pragma once
  14. template< class CElem >
  15. class TSet
  16. {
  17. public:
  18. TSet( unsigned size = arraySize )
  19. :
  20. _array( size ),
  21. _iHintEmpty( 0 ),
  22. _count( 0 )
  23. {}
  24. unsigned Count() const
  25. {
  26. return _count;
  27. }
  28. void Add( CElem* pElem );
  29. void Remove( CElem const * pElem );
  30. #if 0
  31. BOOL Contains( CElem const * pElem ) const;
  32. CElem* Element();
  33. #endif
  34. protected:
  35. CDynArray< CElem > _array;
  36. // this is the count of elements in the array
  37. unsigned _count;
  38. // this is a hint for an empty location
  39. unsigned _iHintEmpty;
  40. };
  41. template< class CElem >
  42. void TSet< CElem >::Add( CElem* pElem )
  43. {
  44. while( 0 != _array[ _iHintEmpty ] && _iHintEmpty < _array.Size() )
  45. {
  46. _iHintEmpty ++;
  47. }
  48. if( _iHintEmpty < _array.Size() )
  49. {
  50. Win4Assert( 0 == _array[ _iHintEmpty ] );
  51. _array[ _iHintEmpty ] = pElem;
  52. }
  53. else
  54. {
  55. Win4Assert( _array.Size() == _iHintEmpty );
  56. _array.Add( pElem, _iHintEmpty );
  57. }
  58. _iHintEmpty++;
  59. _count++;
  60. Win4Assert( _count <= _array.Size() );
  61. }
  62. template< class CElem >
  63. void TSet< CElem >::Remove( CElem const * pElem )
  64. {
  65. unsigned count = 0;
  66. Win4Assert( _count <= _array.Size() );
  67. for( unsigned iElem = 0;
  68. count < _count && iElem < _array.Size();
  69. iElem++ )
  70. {
  71. if( 0 != _array[ iElem ] )
  72. {
  73. count++;
  74. if( _array[ iElem ] == pElem )
  75. {
  76. _array[ iElem ] = 0;
  77. _count--;
  78. return;
  79. }
  80. }
  81. else if( _iHintEmpty > iElem )
  82. {
  83. _iHintEmpty = iElem;
  84. }
  85. }
  86. Win4Assert( !"Element not in set" );
  87. }
  88. #if 0
  89. template< class CElem >
  90. BOOL TSet< CElem >::Contains( CElem const * pElem ) const
  91. {
  92. unsigned count = 0;
  93. Win4Assert( _count <= _array.Size() );
  94. for( unsigned iElem = 0;
  95. count < _count && iElem < _array.Size();
  96. iElem++ )
  97. {
  98. if( 0 != _array[ iElem ] )
  99. {
  100. count++;
  101. if( _array[ iElem ] == pElem )
  102. {
  103. return TRUE;
  104. }
  105. }
  106. }
  107. return FALSE;
  108. }
  109. template< class CElem >
  110. CElem* TSet< CElem >::Element()
  111. {
  112. for( unsigned iElem = 0;
  113. iElem < _array.Size();
  114. iElem++ )
  115. {
  116. CElem* pElem = _array[ iElem ];
  117. if( 0 != pElem )
  118. {
  119. _array[ iElem ] = 0;
  120. _count --;
  121. _iHintEmpty = 0;
  122. return pElem;
  123. }
  124. }
  125. return 0;
  126. }
  127. #endif