BitSet.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #pragma once
  2. #include "BFPlatform.h"
  3. NS_BF_BEGIN;
  4. #define BF_BITSET_ELEM_BITCOUNT (sizeof(uintptr)*8)
  5. class BitSet
  6. {
  7. public:
  8. uintptr* mBits;
  9. int mNumBits;
  10. public:
  11. BitSet()
  12. {
  13. mNumBits = 0;
  14. mBits = NULL;
  15. }
  16. BitSet(int numBits)
  17. {
  18. mNumBits = 0;
  19. mBits = NULL;
  20. this->Resize(numBits);
  21. }
  22. BitSet(BitSet&& other)
  23. {
  24. mNumBits = other.mNumBits;
  25. mBits = other.mBits;
  26. other.mNumBits = 0;
  27. other.mBits = NULL;
  28. }
  29. BitSet(const BitSet& other)
  30. {
  31. mNumBits = 0;
  32. mBits = NULL;
  33. *this = other;
  34. }
  35. ~BitSet()
  36. {
  37. delete [] this->mBits;
  38. }
  39. void Resize(int numBits)
  40. {
  41. int numInts = (numBits + BF_BITSET_ELEM_BITCOUNT - 1) / BF_BITSET_ELEM_BITCOUNT;
  42. int curNumInts = (mNumBits + BF_BITSET_ELEM_BITCOUNT - 1) / BF_BITSET_ELEM_BITCOUNT;
  43. mNumBits = numBits;
  44. if (numInts == curNumInts)
  45. return;
  46. this->mNumBits = numBits;
  47. delete this->mBits;
  48. this->mBits = new uintptr[numInts];
  49. memset(this->mBits, 0, numInts * sizeof(uintptr));
  50. }
  51. void Clear()
  52. {
  53. int curNumInts = (mNumBits + BF_BITSET_ELEM_BITCOUNT - 1) / BF_BITSET_ELEM_BITCOUNT;
  54. memset(mBits, 0, curNumInts * sizeof(uintptr));
  55. }
  56. bool IsSet(int idx) const
  57. {
  58. BF_ASSERT((uintptr)idx < (uintptr)mNumBits);
  59. return (this->mBits[idx / BF_BITSET_ELEM_BITCOUNT] & ((uintptr)1 << (idx % BF_BITSET_ELEM_BITCOUNT))) != 0;
  60. }
  61. void Set(int idx)
  62. {
  63. BF_ASSERT((uintptr)idx < (uintptr)mNumBits);
  64. this->mBits[idx / BF_BITSET_ELEM_BITCOUNT] |= ((uintptr)1 << (idx % BF_BITSET_ELEM_BITCOUNT));
  65. }
  66. void Clear(int idx)
  67. {
  68. BF_ASSERT((uintptr)idx < (uintptr)mNumBits);
  69. this->mBits[idx / BF_BITSET_ELEM_BITCOUNT] &= ~((uintptr)1 << (idx % BF_BITSET_ELEM_BITCOUNT));
  70. }
  71. bool IsEmpty()
  72. {
  73. return mNumBits == 0;
  74. }
  75. bool operator==(const BitSet& other) const
  76. {
  77. int curNumInts = (mNumBits + BF_BITSET_ELEM_BITCOUNT - 1) / BF_BITSET_ELEM_BITCOUNT;
  78. if (mNumBits != other.mNumBits)
  79. return false;
  80. for (int i = 0; i < curNumInts; i++)
  81. if (mBits[i] != other.mBits[i])
  82. return false;
  83. return true;
  84. }
  85. bool operator!=(const BitSet& other) const
  86. {
  87. return !(*this == other);
  88. }
  89. BitSet& operator=(const BitSet& other)
  90. {
  91. Resize(other.mNumBits);
  92. int curNumInts = (mNumBits + BF_BITSET_ELEM_BITCOUNT - 1) / BF_BITSET_ELEM_BITCOUNT;
  93. memcpy(mBits, other.mBits, curNumInts * sizeof(uintptr));
  94. return *this;
  95. }
  96. };
  97. NS_BF_END;