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.

147 lines
3.3 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1991, Microsoft Corporation.
  4. //
  5. // File: StateSet.cxx
  6. //
  7. // Contents:
  8. //
  9. // Classes:
  10. //
  11. // History: 01-20-92 KyleP Created
  12. // 03-11-97 arunk Modified for regex lib
  13. //--------------------------------------------------------------------------
  14. // Local includes:
  15. #include <stateset.hxx>
  16. //+-------------------------------------------------------------------------
  17. //
  18. // Member: CStateSet::Clear, public
  19. //
  20. // Synopsis: Re-initializes a state set to empty.
  21. //
  22. // History: 20-Jan-92 KyleP Created
  23. //
  24. //--------------------------------------------------------------------------
  25. void CStateSet::Clear()
  26. {
  27. delete _puiRest;
  28. _cStates = 0;
  29. _cRest = 0;
  30. _puiRest = 0;
  31. _cuiRest = 0;
  32. }
  33. //+-------------------------------------------------------------------------
  34. //
  35. // Member: CStateSet::Add, public
  36. //
  37. // Synopsis: Adds a state to the state set.
  38. //
  39. // Arguments: [state] -- State to add.
  40. //
  41. // History: 20-Jan-92 KyleP Created
  42. //
  43. //--------------------------------------------------------------------------
  44. void CStateSet::Add( UINT state )
  45. {
  46. if ( IsMember( state ) )
  47. return;
  48. if ( _cStates < CStateSet_cFirst )
  49. {
  50. _auiFirst[ _cStates ] = state;
  51. }
  52. else
  53. {
  54. if ( _cRest >= _cuiRest )
  55. {
  56. UINT * oldpuiRest = _puiRest;
  57. UINT oldcuiRest = _cuiRest;
  58. _cuiRest += 10;
  59. _puiRest = new UINT [ _cuiRest ];
  60. memcpy( _puiRest, oldpuiRest, oldcuiRest * sizeof( UINT ) );
  61. delete oldpuiRest;
  62. }
  63. _puiRest[ _cRest ] = state;
  64. ++_cRest;
  65. }
  66. ++_cStates;
  67. }
  68. //+-------------------------------------------------------------------------
  69. //
  70. // Member: CStateSet::State, public
  71. //
  72. // Arguments: [iState] -- Index of state to return.
  73. //
  74. // Returns: The [iState] state in the state set.
  75. //
  76. // History: 20-Jan-92 KyleP Created
  77. //
  78. //--------------------------------------------------------------------------
  79. UINT CStateSet::State( UINT iState ) const
  80. {
  81. if ( iState <= CStateSet_cFirst )
  82. {
  83. return( _auiFirst[ iState - 1 ] );
  84. }
  85. else
  86. {
  87. return( _puiRest[ iState - CStateSet_cFirst - 1 ] );
  88. }
  89. }
  90. //+-------------------------------------------------------------------------
  91. //
  92. // Member: CStateSet::IsMember, public
  93. //
  94. // Arguments: [state] -- State to look for.
  95. //
  96. // Returns: true if [state] is in set.
  97. //
  98. // History: 20-Jan-92 KyleP Created
  99. //
  100. //--------------------------------------------------------------------------
  101. BOOL CStateSet::IsMember( UINT state )
  102. {
  103. if ( _cStates == 0 )
  104. return( false );
  105. //
  106. // Naive implementation. As long as state sets are small this
  107. // will work fine.
  108. //
  109. for ( int i = min( _cStates, CStateSet_cFirst) - 1;
  110. i >= 0;
  111. i-- )
  112. {
  113. if( _auiFirst[ i ] == state )
  114. return( true );
  115. }
  116. if ( _cStates > CStateSet_cFirst )
  117. {
  118. for ( int i = _cStates - CStateSet_cFirst - 1;
  119. i >= 0;
  120. i-- )
  121. {
  122. if( _puiRest[ i ] == state )
  123. return( true );
  124. }
  125. }
  126. return( false );
  127. }