1 // Copyright 2006 The Android Open Source Project 2 3 #ifndef BITVECTOR_H 4 #define BITVECTOR_H 5 6 #include <inttypes.h> 7 #include <assert.h> 8 9 class Bitvector { 10 public: Bitvector(int num_bits)11 explicit Bitvector(int num_bits) { 12 num_bits_ = num_bits; 13 14 // Round up to a multiple of 32 15 num_bits = (num_bits + 31) & ~31; 16 vector_ = new uint32_t[num_bits >> 5]; 17 } ~Bitvector()18 ~Bitvector() { 19 delete[] vector_; 20 } 21 SetBit(int bitnum)22 void SetBit(int bitnum) { 23 assert(bitnum < num_bits_); 24 vector_[bitnum >> 5] |= 1 << (bitnum & 31); 25 } ClearBit(int bitnum)26 void ClearBit(int bitnum) { 27 assert(bitnum < num_bits_); 28 vector_[bitnum >> 5] &= ~(1 << (bitnum & 31)); 29 } GetBit(int bitnum)30 bool GetBit(int bitnum) { 31 assert(bitnum < num_bits_); 32 return (vector_[bitnum >> 5] >> (bitnum & 31)) & 1; 33 } 34 35 private: 36 int num_bits_; 37 uint32_t *vector_; 38 }; 39 40 #endif // BITVECTOR_H 41