bit_vector.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Jonathan Eastep (eastep@mit.edu)
  2. // 10.10.08
  3. // This file provides a bit vector class
  4. #ifndef BIT_VECTOR_H
  5. #define BIT_VECTOR_H
  6. #include "fixed_types.h"
  7. #include <assert.h>
  8. #include <vector>
  9. #define BITVECT_DEBUG 0
  10. class BitVector
  11. {
  12. private:
  13. UInt32 m_capacity;
  14. UInt32 m_size;
  15. SInt32 m_last_pos;
  16. const UInt32 VECTOR_SIZE;
  17. std::vector<UInt64> m_words;
  18. //used in find function, for iterating through the set bits
  19. //marks the position of the last set bit found.
  20. //value of -1 means no last_pos bit
  21. public:
  22. #if BITVECT_DEBUG
  23. void debug();
  24. #endif
  25. BitVector(UInt32 bits):
  26. m_capacity(bits),
  27. m_size(0),
  28. m_last_pos(-1),
  29. VECTOR_SIZE((bits + 64 - 1) >> 6)
  30. {
  31. m_words.resize(VECTOR_SIZE);
  32. }
  33. ~BitVector() {}
  34. //starting from position 'last_pos', find the next "1" bit.
  35. //private variable 'last_pos' remembers the last position found,
  36. //allowing subsequent calls to "find" to effectively iterate
  37. //through the entire BitVector
  38. //returns -1 if no bits set (after last_pos)
  39. SInt32 find();
  40. bool resetFind();
  41. //given an 8bit word, test to see if 'bit' is set
  42. //this is a helper function to the "find" function
  43. bool bTestBit(UInt8 word, UInt32 bit);
  44. UInt32 capacity() { return m_capacity; }
  45. UInt32 size() { return m_size; }
  46. void reset();
  47. bool at(UInt32 bit);
  48. void set(UInt32 bit);
  49. void clear(UInt32 bit);
  50. #ifdef BITVECT_DEBUG
  51. void set(const BitVector& vec2);
  52. void clear(const BitVector& vec2);
  53. bool test(const BitVector& vec2);
  54. #endif
  55. };
  56. #endif