| 31 | }; |
| 32 | |
| 33 | class BitArray { |
| 34 | |
| 35 | private: |
| 36 | enum { |
| 37 | BLOCK_BITNUM = 64, |
| 38 | TABLE_INTERVAL = 4 |
| 39 | }; |
| 40 | |
| 41 | public: |
| 42 | BitArray(); |
| 43 | ~BitArray(); |
| 44 | BitArray(uint64_t size); |
| 45 | uint64_t length() const; |
| 46 | uint64_t one_num() const; |
| 47 | |
| 48 | void Init(uint64_t size); |
| 49 | void Clear(); |
| 50 | void SetBit(uint64_t bit, uint64_t pos); |
| 51 | void Build(); |
| 52 | uint64_t Rank(uint64_t bit, uint64_t pos) const; |
| 53 | uint64_t Select(uint64_t bit, uint64_t rank) const; |
| 54 | uint64_t Lookup(uint64_t pos) const; |
| 55 | |
| 56 | static uint64_t PopCount(uint64_t x); |
| 57 | static uint64_t PopCountMask(uint64_t x, uint64_t offset); |
| 58 | static uint64_t SelectInBlock(uint64_t x, uint64_t rank); |
| 59 | static uint64_t GetBitNum(uint64_t one_num, uint64_t num, uint64_t bit); |
| 60 | void PrintForDebug(std::ostream& os) const; |
| 61 | |
| 62 | void Save(std::ostream& os) const; |
| 63 | void Load(std::istream& is); |
| 64 | |
| 65 | private: |
| 66 | uint64_t RankOne(uint64_t pos) const; |
| 67 | uint64_t SelectOutBlock(uint64_t bit, uint64_t& rank) const; |
| 68 | |
| 69 | private: |
| 70 | std::vector<uint64_t> bit_blocks_; |
| 71 | std::vector<uint64_t> rank_tables_; |
| 72 | uint64_t length_; |
| 73 | uint64_t one_num_; |
| 74 | }; |
| 75 | |
| 76 | } |
| 77 | |