| 29 | namespace internal { |
| 30 | |
| 31 | class BitmapWriter { |
| 32 | // A sequential bitwise writer that preserves surrounding bit values. |
| 33 | |
| 34 | public: |
| 35 | BitmapWriter(uint8_t* bitmap, int64_t start_offset, int64_t length) |
| 36 | : bitmap_(bitmap), position_(0), length_(length) { |
| 37 | byte_offset_ = start_offset / 8; |
| 38 | bit_mask_ = bit_util::kBitmask[start_offset % 8]; |
| 39 | if (length > 0) { |
| 40 | current_byte_ = bitmap[byte_offset_]; |
| 41 | } else { |
| 42 | current_byte_ = 0; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void Set() { current_byte_ |= bit_mask_; } |
| 47 | |
| 48 | void Clear() { current_byte_ &= bit_mask_ ^ 0xFF; } |
| 49 | |
| 50 | void Next() { |
| 51 | bit_mask_ = static_cast<uint8_t>(bit_mask_ << 1); |
| 52 | ++position_; |
| 53 | if (bit_mask_ == 0) { |
| 54 | // Finished this byte, need advancing |
| 55 | bit_mask_ = 0x01; |
| 56 | bitmap_[byte_offset_++] = current_byte_; |
| 57 | if (ARROW_PREDICT_TRUE(position_ < length_)) { |
| 58 | current_byte_ = bitmap_[byte_offset_]; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void Finish() { |
| 64 | // Store current byte if we didn't went past bitmap storage |
| 65 | if (length_ > 0 && (bit_mask_ != 0x01 || position_ < length_)) { |
| 66 | bitmap_[byte_offset_] = current_byte_; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | int64_t position() const { return position_; } |
| 71 | |
| 72 | private: |
| 73 | uint8_t* bitmap_; |
| 74 | int64_t position_; |
| 75 | int64_t length_; |
| 76 | |
| 77 | uint8_t current_byte_; |
| 78 | uint8_t bit_mask_; |
| 79 | int64_t byte_offset_; |
| 80 | }; |
| 81 | |
| 82 | class FirstTimeBitmapWriter { |
| 83 | // Like BitmapWriter, but any bit values *following* the bits written |
no outgoing calls