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.

444 lines
7.9 KiB

  1. /*++
  2. Copyright (c) 1991-2000 Microsoft Corporation
  3. Module Name:
  4. bitvect.hxx
  5. Abstract:
  6. This module contains the definition for the BITVECTOR class. BITVECTOR
  7. is a concrete class which implements a bit array.
  8. Author:
  9. David J. Gilman (davegi) 13-Jan-1991
  10. Environment:
  11. ULIB, User Mode
  12. Notes:
  13. BITVECTOR is an implementation of a general purpose bit
  14. vector. It is based around an abstract type called PT
  15. (Primitive Type) to increase it's portability. BITVECTORs
  16. can be created of arbitrary size. If required they will
  17. allocate memory to maintain the bits or will use a memory
  18. buffer supplied by the client. This allows the client to
  19. superimpose a bit vector onto a predefined buffer (e.g. onto
  20. a SECBUF).
  21. --*/
  22. #if ! defined ( _BITVECTOR_ )
  23. #define _BITVECTOR_
  24. DECLARE_CLASS( BITVECTOR );
  25. //
  26. // BITVECTOR allocation increment - Primitive Type
  27. //
  28. DEFINE_TYPE( ULONG, PT );
  29. //
  30. // BIT values
  31. //
  32. enum BIT {
  33. SET = 1,
  34. RESET = 0
  35. };
  36. //
  37. // Invalid bit count
  38. //
  39. extern CONST PT InvalidBitCount;
  40. class BITVECTOR : public OBJECT {
  41. public:
  42. ULIB_EXPORT
  43. DECLARE_CONSTRUCTOR( BITVECTOR );
  44. DECLARE_CAST_MEMBER_FUNCTION( BITVECTOR );
  45. NONVIRTUAL
  46. ULIB_EXPORT
  47. ~BITVECTOR (
  48. );
  49. NONVIRTUAL
  50. ULIB_EXPORT
  51. BOOLEAN
  52. Initialize (
  53. IN PT Size DEFAULT 0,
  54. IN BIT InitialValue DEFAULT RESET,
  55. IN PPT Memory DEFAULT NULL
  56. );
  57. NONVIRTUAL
  58. PCPT
  59. GetBuf (
  60. ) CONST;
  61. NONVIRTUAL
  62. BOOLEAN
  63. IsBitSet (
  64. IN PT Index
  65. ) CONST;
  66. NONVIRTUAL
  67. PT
  68. QueryCountSet (
  69. ) CONST;
  70. NONVIRTUAL
  71. PT
  72. QuerySize (
  73. ) CONST;
  74. NONVIRTUAL
  75. VOID
  76. ResetAll (
  77. );
  78. NONVIRTUAL
  79. VOID
  80. ResetBit (
  81. IN PT Index
  82. );
  83. NONVIRTUAL
  84. ULIB_EXPORT
  85. VOID
  86. ResetBit (
  87. IN PT Index,
  88. IN PT Count
  89. );
  90. NONVIRTUAL
  91. VOID
  92. SetAll (
  93. );
  94. NONVIRTUAL
  95. VOID
  96. SetBit (
  97. IN PT Index
  98. );
  99. NONVIRTUAL
  100. ULIB_EXPORT
  101. VOID
  102. SetBit (
  103. IN PT Index,
  104. IN PT Count
  105. );
  106. NONVIRTUAL
  107. ULIB_EXPORT
  108. PT
  109. SetSize (
  110. IN PT Size,
  111. IN BIT InitialValue DEFAULT RESET
  112. );
  113. NONVIRTUAL
  114. VOID
  115. ToggleBit (
  116. IN PT Index,
  117. IN PT Count DEFAULT 1
  118. );
  119. private:
  120. NONVIRTUAL
  121. VOID
  122. Construct (
  123. );
  124. NONVIRTUAL
  125. ULIB_EXPORT
  126. PT
  127. ComputeCountSet (
  128. ) CONST;
  129. NONVIRTUAL
  130. VOID
  131. Destroy (
  132. );
  133. VOID
  134. NONVIRTUAL
  135. InitAll (
  136. IN PT InitialValue
  137. );
  138. // Note that these three member data items cannot
  139. // be static because they are used in INLINE functions
  140. // that are called from outside the DLL.
  141. PT _BitsPerPT;
  142. PT _IndexShiftCount;
  143. PT _BitPositionMask;
  144. STATIC
  145. CONST
  146. BYTE _BitsSetLookUp[ 256 ];
  147. PPT _BitVector;
  148. PT _PTCount;
  149. BOOLEAN _FreeBitVector;
  150. };
  151. INLINE
  152. PCPT
  153. BITVECTOR::GetBuf (
  154. ) CONST
  155. /*++
  156. Routine Description:
  157. Returns the underlying bit vector.
  158. Arguments:
  159. None.
  160. Return Value:
  161. PCPT - Returns a pointer to the underlying bitvector.
  162. --*/
  163. {
  164. return( _BitVector );
  165. }
  166. INLINE
  167. PT
  168. BITVECTOR::QuerySize (
  169. ) CONST
  170. /*++
  171. Routine Description:
  172. Returns the number of bits in this BITVECTOR.
  173. Arguments:
  174. None.
  175. Return Value:
  176. PT
  177. --*/
  178. {
  179. return( _PTCount * _BitsPerPT );
  180. }
  181. INLINE
  182. VOID
  183. BITVECTOR::InitAll (
  184. IN PT InitialValue
  185. )
  186. /*++
  187. Routine Description:
  188. Initialize each element in the internal bit vector to the
  189. supplied bit pattern.
  190. Arguments:
  191. InitialValue - Supplies the pattern for each element.
  192. Return Value:
  193. None.
  194. --*/
  195. {
  196. DebugAssert( _BitVector != NULL );
  197. memset(( PVOID ) _BitVector,
  198. ( int ) InitialValue, ( size_t ) ( sizeof( PT ) * _PTCount ));
  199. }
  200. INLINE
  201. VOID
  202. BITVECTOR::SetAll (
  203. )
  204. /*++
  205. Routine Description:
  206. Set (to one) all of the bits in this BITVECTOR.
  207. Arguments:
  208. None.
  209. Return Value:
  210. None.
  211. --*/
  212. {
  213. InitAll(( PT ) ~0 );
  214. }
  215. INLINE
  216. VOID
  217. BITVECTOR::ResetAll (
  218. )
  219. /*++
  220. Routine Description:
  221. Reset (to zero) all of the bits in this BITVECTOR.
  222. Arguments:
  223. None.
  224. Return Value:
  225. None.
  226. --*/
  227. {
  228. InitAll(( PT ) 0 );
  229. }
  230. INLINE
  231. BOOLEAN
  232. BITVECTOR::IsBitSet (
  233. IN PT Index
  234. ) CONST
  235. /*++
  236. Routine Description:
  237. Determine if the bit at the supplied index is set.
  238. Arguments:
  239. Index - Supplies the index to the bit of interest.
  240. Return Value:
  241. BOOLEAN - Returns TRUE if the bit of interest is set.
  242. --*/
  243. {
  244. DebugAssert( _BitVector != NULL );
  245. DebugAssert( Index < ( _PTCount * _BitsPerPT ));
  246. return (_BitVector[Index >> _IndexShiftCount] &
  247. (1 << (Index & _BitPositionMask))) ? TRUE : FALSE;
  248. }
  249. INLINE
  250. VOID
  251. BITVECTOR::SetBit (
  252. IN PT Index
  253. )
  254. /*++
  255. Routine Description:
  256. Set the bit at the supplied index.
  257. Arguments:
  258. Index - Supplies the index to the bit of interest.
  259. Return Value:
  260. None.
  261. --*/
  262. {
  263. DebugAssert( _BitVector != NULL );
  264. DebugAssert( Index < ( _PTCount * _BitsPerPT ));
  265. _BitVector[Index >> _IndexShiftCount] |=
  266. (1 << (Index & _BitPositionMask));
  267. }
  268. INLINE
  269. VOID
  270. BITVECTOR::ResetBit (
  271. IN PT Index
  272. )
  273. /*++
  274. Routine Description:
  275. Reset the bit at the supplied index.
  276. Arguments:
  277. Index - Supplies the index to the bit of interest.
  278. Return Value:
  279. None.
  280. --*/
  281. {
  282. DebugAssert( _BitVector != NULL );
  283. DebugAssert( Index < ( _PTCount * _BitsPerPT ));
  284. _BitVector[Index >> _IndexShiftCount] &=
  285. ~(1 << (Index & _BitPositionMask));
  286. }
  287. INLINE
  288. PT
  289. BITVECTOR::QueryCountSet (
  290. ) CONST
  291. /*++
  292. Routine Description:
  293. Determine the number of bits set in this BITVECTOR.
  294. Arguments:
  295. None.
  296. Return Value:
  297. PT - Returns the number of set bits.
  298. --*/
  299. {
  300. return ComputeCountSet();
  301. }
  302. #endif // _BITVECTOR_