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.

443 lines
5.8 KiB

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