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.

136 lines
2.8 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1998-1999, 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. bool 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 (std::nothrow) Bucket[numBuckets];
  76. if ( !bits )
  77. {
  78. return false;
  79. }
  80. }
  81. memset(bits, 0, numBuckets * sizeof(Bucket));
  82. numSet = 0;
  83. return true;
  84. }
  85. // Sets the given bit.
  86. void set(size_t i)
  87. {
  88. if (!test(i))
  89. {
  90. ++numSet;
  91. getBucket(i) |= getBit(i);
  92. }
  93. }
  94. // Returns true if the given bit is set.
  95. bool test(size_t i) const
  96. {
  97. return (getBucket(i) & getBit(i)) != 0;
  98. }
  99. protected:
  100. // Return the bit for a given index.
  101. static Bucket getBit(size_t i)
  102. { return (Bucket)1 << (i % sizeof(Bucket)); }
  103. // Return the bucket for a given index.
  104. Bucket& getBucket(size_t i) const
  105. { return bits[i / sizeof(Bucket)]; }
  106. size_t numBuckets; // Number of bit buckets.
  107. size_t numSet; // Number of bits currently set.
  108. Bucket* bits; // Array of bit buckets.
  109. // Not implemented.
  110. BitVector(const BitVector&);
  111. BitVector& operator=(const BitVector&);
  112. };
  113. #endif // _BITVEC_H_