11#ifndef JAU_BITFIELD_HPP_
12#define JAU_BITFIELD_HPP_
51 template<jau::req::
unsigned_
integral StorageType,
size_t BitSize>
52 requires requires (StorageType) {
sizeof(StorageType) <=
sizeof(
size_t); }
71 typedef std::array<unit_type, unit_size> storage_t;
91 if( !
put(0, bitstr) ) {
97 constexpr void clear() noexcept {
116 return storage[u] & (one_u << b);
149 if ( storage[u] & m ) {
183 const bool s =
get(l);
211 return put(dstBitpos,
get(srcBitpos));
224 return m & storage[u];
228 const size_type l1 = std::min(length, left);
234 return d1 | ((
m2 & storage[u + 1]) << l1);
256 storage[u] = ((~m) & storage[u])
261 const size_type l1 = std::min(length, left);
263 storage[u] = ((~(
m1 << b)) & storage[u])
264 | ((
m1 & data) << b);
268 storage[u + 1] = ((
~m2) & storage[u + 1])
269 | (
m2 & (data >> l1));
282 if ( 0 == bitstr.size() ) {
284 }
else if ( !
in_range(bitpos, bitstr.size()) ) {
291 std::string_view segment = bitstr.substr(strPos, len);
310 }
else if ( !
in_range(bitpos, length) ) {
313 const unit_type v = bit ? std::numeric_limits<unit_type>::max() : 0;
333 if ( remaining > 0 ) {
340 assert(0 == remaining);
367 if (
this == &rhs ) {
374 while ( i <
unit_size && storage[i] == rhs.storage[i] ) {
380 template<
size_t OBitSize>
385 }
else if ( !
in_range(bitpos, length) ) {
391 if ( 0 == unit_bit_pos ) {
394 for (
size_type u = 0; u < unit_count && 0 < length; ++u ) {
395 if( !
putUnit( i, l, o.storage[u] ) ) {
403 for (
size_type i = 0; i < length; ++i ) {
404 if( !
put(bitpos + i, o.
get(i)) ) {
412 template<
size_t BitLength>
413 std::pair<bitfield_t<StorageType, BitLength>,
bool>
subbits(
size_type bitpos)
const noexcept {
414 if ( 0 == BitLength ) {
416 }
else if ( !
in_range(bitpos, BitLength) ) {
424 if ( 0 == unit_bit_pos ) {
427 for (
size_type u = unit_pos; u < unit_count && 0 < length; ++u ) {
428 if( !r.first.putUnit( i, l, storage[u] ) ) {
437 if( !r.first.put(i,
get(bitpos+i)) ) {
446 if ( 0 == length || !
in_range(bitpos, length) ) {
457 if ( 0 == bit_pos ) {
461 for (
size_type i = unit_pos + unit_count; i-- > unit_pos && 0 < length; ) {
468 while(length-- > 0) {
469 r.push_back(
get(--i) ?
'1' :
'0');
484 template<jau::req::
unsigned_
integral StorageType,
size_t BitSize>
485 requires requires (StorageType) {
sizeof(StorageType) <=
sizeof(
size_t); }
499 template<
size_t BitSize>
Simple statically sized bitfield template for efficient bit storage access.
constexpr bitfield_t & reset() noexcept
static constexpr bool in_range(size_type bitpos)
size_t size_type
size_t data type, bit position and count
constexpr bitfield_t & flip() noexcept
static constexpr size_type unit_shift
std::pair< bitfield_t< StorageType, BitLength >, bool > subbits(size_type bitpos) const noexcept
constexpr bool operator==(const bitfield_t &rhs) const noexcept
bool set(size_type bitpos, size_type length, bool bit) noexcept
bool putUnit(size_type bitpos, size_type length, unit_type data) noexcept
bitfield_t(std::string_view bitstr)
bool put(size_t bitpos, const bitfield_t< StorageType, OBitSize > &o)
bitfield_t & setAll(bool bit) noexcept
constexpr bool put(size_type bitpos, bool v) noexcept
Writes the bit value v to position bitpos into this storage.
static constexpr size_type unit_bit_size
static constexpr size_type unit_size
constexpr void clear() noexcept
std::string toString(PrefixOpt prefix=PrefixOpt::none) const noexcept
bool put(size_type bitpos, std::string_view bitstr) noexcept
std::string infoString() const noexcept
constexpr bool clr(size_type bitpos) noexcept
constexpr bool set(size_type bitpos) noexcept
constexpr bitfield_t() noexcept
Constructs an empty bitfield instance.
constexpr bitfield_t & reverse() noexcept
bool copyUnit(size_type srcBitpos, size_type dstBitpos, size_type length) noexcept
static constexpr size_type unit_byte_size
std::string toString(size_type bitpos, size_type length, PrefixOpt prefix=PrefixOpt::none) const noexcept
static constexpr bool in_range(size_type bitpos, size_type length)
size_type count() const noexcept
bool copy(size_type srcBitpos, size_type dstBitpos) noexcept
StorageType unit_type
Unit data type.
constexpr bool operator[](size_type bitpos) const noexcept
unit_type getUnit(size_type bitpos, size_type length) const noexcept
constexpr bool reset(size_type bitpos) noexcept
constexpr bool get(size_type bitpos) const noexcept
static constexpr size_type bit_size
constexpr size_type size() const noexcept
constexpr bool flip(size_type bitpos) noexcept
bitfield_t< jau::nsize_t, BitSize > bitfield
Simple bitfield template for efficient bit storage access.
static constexpr T bit_mask(size_t n) noexcept
Returns the T bit mask of n-bits, i.e.
std::ostream & operator<<(std::ostream &out, const bitfield_t< StorageType, BitSize > &v)
constexpr uint8_t rev_bits(uint8_t v) noexcept
Reverse bits of one byte.
constexpr size_t log2_byteshift(const size_t bytesize) noexcept
Returns log2(bytesize*8), e.g.
constexpr size_t bit_count(T n) noexcept
Returns the number of set bits within given unsigned integral.
std::string toBitString(const void *data, const nsize_t length, const bit_order_t bitOrder=bit_order_t::msb, const PrefixOpt prefix=PrefixOpt::prefix, size_t bit_len=0) noexcept
Produce a binary string representation of the given lsb-first byte values.
SizeBoolPair fromBitString(std::vector< uint8_t > &out, const uint8_t bitstr[], const size_t bitstr_len, const bit_order_t bitOrder=bit_order_t::msb, const Bool checkPrefix=Bool::True) noexcept
Converts a given binary string representation into a byte vector, lsb-first.
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
@ msb
Identifier for most-significant-bit (msb) first.
static const Mat4f m1(m1_0)
static const Mat4f m2(m2_0)