Skip to content

Commit

Permalink
use bitset in bit_span::operator*
Browse files Browse the repository at this point in the history
Change-Id: I34772c33111e073f58d75c7949bea68dc232353a
  • Loading branch information
oliverlee committed Sep 20, 2023
1 parent 62b38e9 commit 1ebc412
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions huffman/src/bit_span.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ namespace starflate::huffman {
/// A non-owning span of bits. Allows for iteration over the individual bits.
class bit_span
{

const std::byte* data_;
size_t bit_size_;
std::size_t bit_size_;

public:
/// An iterator over the bits in a bit_span.
class iterator : public detail::iterator_interface<iterator>
{
private:
const bit_span* parent_;
size_t offset_;
std::size_t offset_;

public:
using difference_type = std::ptrdiff_t;
Expand All @@ -43,13 +42,10 @@ class bit_span

constexpr auto operator*() const -> reference
{
// TODO: switch to std::bitset once GCC's
// std::bitset<_Nb>::reference::operator bool() is constexpr
const std::uint8_t byte{
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
static_cast<std::uint8_t>(parent_->data_[offset_ / CHAR_BIT])};
// most significant bit first
return bit{bool(byte & 1 << (CHAR_BIT - 1 - (offset_ % CHAR_BIT)))};
const auto byte = std::bitset<CHAR_BIT>{
static_cast<unsigned long long>(parent_->data_[offset_ / CHAR_BIT])};

return bit{byte[CHAR_BIT - 1 - (offset_ % CHAR_BIT)]};
}

constexpr auto operator+=(difference_type n) -> iterator&
Expand Down

0 comments on commit 1ebc412

Please sign in to comment.