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.

131 lines
2.7 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1998, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // BitVec.h
  8. //
  9. // SYNOPSIS
  10. //
  11. // This file implements the class BitVector
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 02/09/1998 Original version.
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #ifndef _BITVEC_H_
  19. #define _BITVEC_H_
  20. ///////////////////////////////////////////////////////////////////////////////
  21. //
  22. // CLASS
  23. //
  24. // BitVector
  25. //
  26. // DESCRIPTION
  27. //
  28. // Very simple bit vector optimized for use by the CSimpleTable class.
  29. //
  30. ///////////////////////////////////////////////////////////////////////////////
  31. class BitVector
  32. {
  33. public:
  34. // Type used to store bits.
  35. typedef unsigned long Bucket;
  36. BitVector()
  37. : numBuckets(0), numSet(0), bits(NULL) { }
  38. ~BitVector()
  39. {
  40. delete[] bits;
  41. }
  42. // Returns true if any bits are set.
  43. bool any() const
  44. {
  45. return numSet != 0;
  46. }
  47. // Returns the number of bits set.
  48. size_t count() const
  49. {
  50. return numSet;
  51. }
  52. // Returns true if no bits are set.
  53. bool none() const
  54. {
  55. return numSet == 0;
  56. }
  57. // Clears all bits.
  58. void reset()
  59. {
  60. if (any())
  61. {
  62. memset(bits, 0, numBuckets * sizeof(Bucket));
  63. numSet = 0;
  64. }
  65. }
  66. // Resizes the bitvector to have room for at least 'n' bits. Also clears
  67. // any existing bits.
  68. void resize(size_t n)
  69. {
  70. size_t newBuckets = (n + sizeof(Bucket) - 1)/sizeof(Bucket);
  71. if (newBuckets >= numBuckets)
  72. {
  73. numBuckets = newBuckets;
  74. delete[] bits;
  75. bits = new Bucket[numBuckets];
  76. }
  77. memset(bits, 0, numBuckets * sizeof(Bucket));
  78. numSet = 0;
  79. }
  80. // Sets the given bit.
  81. void set(size_t i)
  82. {
  83. if (!test(i))
  84. {
  85. ++numSet;
  86. getBucket(i) |= getBit(i);
  87. }
  88. }
  89. // Returns true if the given bit is set.
  90. bool test(size_t i) const
  91. {
  92. return (getBucket(i) & getBit(i)) != 0;
  93. }
  94. protected:
  95. // Return the bit for a given index.
  96. static Bucket getBit(size_t i)
  97. { return (Bucket)1 << (i % sizeof(Bucket)); }
  98. // Return the bucket for a given index.
  99. Bucket& getBucket(size_t i) const
  100. { return bits[i / sizeof(Bucket)]; }
  101. size_t numBuckets; // Number of bit buckets.
  102. size_t numSet; // Number of bits currently set.
  103. Bucket* bits; // Array of bit buckets.
  104. // Not implemented.
  105. BitVector(const BitVector&);
  106. BitVector& operator=(const BitVector&);
  107. };
  108. #endif // _BITVEC_H_