jaulib v1.4.0-2-g788cf73
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
jau::bitfield_t< StorageType, BitSize > Class Template Reference

Simple statically sized bitfield template for efficient bit storage access in O(1). More...

#include <bitfield.hpp>

Inheritance diagram for jau::bitfield_t< StorageType, BitSize >:
Collaboration diagram for jau::bitfield_t< StorageType, BitSize >:

Public Types

typedef size_t size_type
 size_t data type, bit position and count
 
typedef StorageType unit_type
 Unit data type.
 

Public Member Functions

constexpr bitfield_t () noexcept
 Constructs an empty bitfield instance.
 
 bitfield_t (std::string_view bitstr)
 Constructs a bitfield instance, initialized with bitstr msb bit-pattern.
 
constexpr void clear () noexcept
 
constexpr bool clr (size_type bitpos) noexcept
 Clear the bit at position bitpos of this storage.
 
bool copy (size_type srcBitpos, size_type dstBitpos) noexcept
 Copies the bit at position srcBitpos to position dstBitpos, returning the copied bit-value.
 
bool copyUnit (size_type srcBitpos, size_type dstBitpos, size_type length) noexcept
 Copies length bits at position srcBitpos to position dstBitpos, returning the copied bits.
 
size_type count () const noexcept
 
constexpr bitfield_tflip () noexcept
 
constexpr bool flip (size_type bitpos) noexcept
 Flips the bit value at position bitpos in this storage.
 
constexpr bool get (size_type bitpos) const noexcept
 
unit_type getUnit (size_type bitpos, size_type length) const noexcept
 
std::string infoString () const noexcept
 
constexpr bool operator== (const bitfield_t &rhs) const noexcept
 
constexpr bool operator[] (size_type bitpos) const noexcept
 
template<size_t OBitSize>
bool put (size_t bitpos, const bitfield_t< StorageType, OBitSize > &o)
 
constexpr bool put (size_type bitpos, bool v) noexcept
 Writes the bit value v to position bitpos into this storage.
 
bool put (size_type bitpos, std::string_view bitstr) noexcept
 Writes bitstr msb bit-pattern into this storage, starting with the lowest bit from the storage position bitpos.
 
bool putUnit (size_type bitpos, size_type length, unit_type data) noexcept
 Writes length bits of given data into this storage, starting with the lowest bit from the storage position bitpos.
 
constexpr bitfield_treset () noexcept
 
constexpr bool reset (size_type bitpos) noexcept
 Clear the bit at position bitpos of this storage.
 
constexpr bitfield_treverse () noexcept
 
constexpr bool set (size_type bitpos) noexcept
 Sets the bit at position bitpos of this storage.
 
bool set (size_type bitpos, size_type length, bool bit) noexcept
 Set length bits starting at bitpos of this bitfield to the given value bit.
 
bitfield_tsetAll (bool bit) noexcept
 Set all bits of this bitfield to the given value bit.
 
constexpr size_type size () const noexcept
 Returns storage size in bits.
 
template<size_t BitLength>
std::pair< bitfield_t< StorageType, BitLength >, bool > subbits (size_type bitpos) const noexcept
 
std::string toString (PrefixOpt prefix=PrefixOpt::none) const noexcept
 
std::string toString (size_type bitpos, size_type length, PrefixOpt prefix=PrefixOpt::none) const noexcept
 

Static Public Member Functions

static constexpr bool in_range (size_type bitpos)
 
static constexpr bool in_range (size_type bitpos, size_type length)
 

Static Public Attributes

static constexpr size_type bit_size = BitSize
 Storage size in bits.
 
static constexpr size_type unit_bit_size = unit_byte_size * CHAR_BIT
 One unit size in bits.
 
static constexpr size_type unit_byte_size = sizeof(unit_type)
 One unit size in bytes.
 
static constexpr size_type unit_shift = jau::log2_byteshift(unit_byte_size)
 One unit shift amount.
 
static constexpr size_type unit_size = std::max<size_type>(1, (bit_size + unit_bit_size - 1) >> unit_shift)
 Storage size in units.
 

Detailed Description

template<jau::req::unsigned_integral StorageType, size_t BitSize>
requires requires (StorageType) { sizeof(StorageType) <= sizeof(size_t); }
class jau::bitfield_t< StorageType, BitSize >

Simple statically sized bitfield template for efficient bit storage access in O(1).

Bit-position and bit-order are in least-significant-bits (lsb) first.

Implementations utilizes an in-memory std::array<StorageType, (BitSize+31)/32> with unsigned integral StorageType of sizeof(StorageType) <= sizeof(size_t).

Similar to std::bitset, but providing custom methods.

See also
jau::bitheap

Definition at line 53 of file bitfield.hpp.

Member Typedef Documentation

◆ unit_type

template<jau::req::unsigned_integral StorageType, size_t BitSize>
typedef StorageType jau::bitfield_t< StorageType, BitSize >::unit_type

Unit data type.

Definition at line 55 of file bitfield.hpp.

◆ size_type

template<jau::req::unsigned_integral StorageType, size_t BitSize>
typedef size_t jau::bitfield_t< StorageType, BitSize >::size_type

size_t data type, bit position and count

Definition at line 56 of file bitfield.hpp.

Constructor & Destructor Documentation

◆ bitfield_t() [1/2]

template<jau::req::unsigned_integral StorageType, size_t BitSize>
jau::bitfield_t< StorageType, BitSize >::bitfield_t ( )
inlineconstexprnoexcept

Constructs an empty bitfield instance.

Definition at line 81 of file bitfield.hpp.

Here is the caller graph for this function:

◆ bitfield_t() [2/2]

template<jau::req::unsigned_integral StorageType, size_t BitSize>
jau::bitfield_t< StorageType, BitSize >::bitfield_t ( std::string_view bitstr)
inline

Constructs a bitfield instance, initialized with bitstr msb bit-pattern.

Parameters
bitstrmost-significat (msb) bit string pattern
Exceptions
jau::IllegalArgumentErrorif bitstr put failed
See also
put(std::string_view)

Definition at line 89 of file bitfield.hpp.

Member Function Documentation

◆ size()

template<jau::req::unsigned_integral StorageType, size_t BitSize>
size_type jau::bitfield_t< StorageType, BitSize >::size ( ) const
inlineconstexprnoexcept

Returns storage size in bits.

Definition at line 63 of file bitfield.hpp.

Here is the caller graph for this function:

◆ in_range() [1/2]

template<jau::req::unsigned_integral StorageType, size_t BitSize>
static constexpr bool jau::bitfield_t< StorageType, BitSize >::in_range ( size_type bitpos)
inlinestaticconstexpr

Definition at line 75 of file bitfield.hpp.

Here is the caller graph for this function:

◆ in_range() [2/2]

template<jau::req::unsigned_integral StorageType, size_t BitSize>
static constexpr bool jau::bitfield_t< StorageType, BitSize >::in_range ( size_type bitpos,
size_type length )
inlinestaticconstexpr

Definition at line 76 of file bitfield.hpp.

◆ clear()

template<jau::req::unsigned_integral StorageType, size_t BitSize>
void jau::bitfield_t< StorageType, BitSize >::clear ( )
inlineconstexprnoexcept

Definition at line 97 of file bitfield.hpp.

Here is the caller graph for this function:

◆ reset() [1/2]

template<jau::req::unsigned_integral StorageType, size_t BitSize>
bitfield_t & jau::bitfield_t< StorageType, BitSize >::reset ( )
inlineconstexprnoexcept

Definition at line 101 of file bitfield.hpp.

Here is the caller graph for this function:

◆ operator[]()

template<jau::req::unsigned_integral StorageType, size_t BitSize>
bool jau::bitfield_t< StorageType, BitSize >::operator[] ( size_type bitpos) const
inlineconstexprnoexcept

Definition at line 107 of file bitfield.hpp.

◆ get()

template<jau::req::unsigned_integral StorageType, size_t BitSize>
bool jau::bitfield_t< StorageType, BitSize >::get ( size_type bitpos) const
inlineconstexprnoexcept

Definition at line 110 of file bitfield.hpp.

Here is the caller graph for this function:

◆ put() [1/3]

template<jau::req::unsigned_integral StorageType, size_t BitSize>
bool jau::bitfield_t< StorageType, BitSize >::put ( size_type bitpos,
bool v )
inlinenodiscardconstexprnoexcept

Writes the bit value v to position bitpos into this storage.

Returns
true on sucess, otherwise false

Definition at line 122 of file bitfield.hpp.

Here is the caller graph for this function:

◆ flip() [1/2]

template<jau::req::unsigned_integral StorageType, size_t BitSize>
bool jau::bitfield_t< StorageType, BitSize >::flip ( size_type bitpos)
inlinenodiscardconstexprnoexcept

Flips the bit value at position bitpos in this storage.

Returns
true on sucess, otherwise false

Definition at line 140 of file bitfield.hpp.

Here is the caller graph for this function:

◆ flip() [2/2]

template<jau::req::unsigned_integral StorageType, size_t BitSize>
bitfield_t & jau::bitfield_t< StorageType, BitSize >::flip ( )
inlineconstexprnoexcept

Definition at line 155 of file bitfield.hpp.

◆ reverse()

template<jau::req::unsigned_integral StorageType, size_t BitSize>
bitfield_t & jau::bitfield_t< StorageType, BitSize >::reverse ( )
inlineconstexprnoexcept

Definition at line 165 of file bitfield.hpp.

Here is the caller graph for this function:

◆ set() [1/2]

template<jau::req::unsigned_integral StorageType, size_t BitSize>
bool jau::bitfield_t< StorageType, BitSize >::set ( size_type bitpos)
inlinenodiscardconstexprnoexcept

Sets the bit at position bitpos of this storage.

Returns
true on sucess, otherwise false

Definition at line 191 of file bitfield.hpp.

Here is the caller graph for this function:

◆ clr()

template<jau::req::unsigned_integral StorageType, size_t BitSize>
bool jau::bitfield_t< StorageType, BitSize >::clr ( size_type bitpos)
inlinenodiscardconstexprnoexcept

Clear the bit at position bitpos of this storage.

Returns
true on sucess, otherwise false

Definition at line 196 of file bitfield.hpp.

Here is the caller graph for this function:

◆ reset() [2/2]

template<jau::req::unsigned_integral StorageType, size_t BitSize>
bool jau::bitfield_t< StorageType, BitSize >::reset ( size_type bitpos)
inlinenodiscardconstexprnoexcept

Clear the bit at position bitpos of this storage.

Returns
true on sucess, otherwise false

Definition at line 201 of file bitfield.hpp.

◆ copy()

template<jau::req::unsigned_integral StorageType, size_t BitSize>
bool jau::bitfield_t< StorageType, BitSize >::copy ( size_type srcBitpos,
size_type dstBitpos )
inlinenodiscardnoexcept

Copies the bit at position srcBitpos to position dstBitpos, returning the copied bit-value.

Returns
true on sucess, otherwise false

Definition at line 207 of file bitfield.hpp.

Here is the caller graph for this function:

◆ getUnit()

template<jau::req::unsigned_integral StorageType, size_t BitSize>
unit_type jau::bitfield_t< StorageType, BitSize >::getUnit ( size_type bitpos,
size_type length ) const
inlinenoexcept

Definition at line 212 of file bitfield.hpp.

Here is the caller graph for this function:

◆ putUnit()

template<jau::req::unsigned_integral StorageType, size_t BitSize>
bool jau::bitfield_t< StorageType, BitSize >::putUnit ( size_type bitpos,
size_type length,
unit_type data )
inlinenodiscardnoexcept

Writes length bits of given data into this storage, starting with the lowest bit from the storage position bitpos.

Returns
true on sucess, otherwise false

Definition at line 242 of file bitfield.hpp.

Here is the caller graph for this function:

◆ put() [2/3]

template<jau::req::unsigned_integral StorageType, size_t BitSize>
bool jau::bitfield_t< StorageType, BitSize >::put ( size_type bitpos,
std::string_view bitstr )
inlinenodiscardnoexcept

Writes bitstr msb bit-pattern into this storage, starting with the lowest bit from the storage position bitpos.

Parameters
bitposbit position to insert
bitstrmost-significat (msb) bit string pattern
Returns
true on sucess, otherwise false

Definition at line 278 of file bitfield.hpp.

◆ set() [2/2]

template<jau::req::unsigned_integral StorageType, size_t BitSize>
bool jau::bitfield_t< StorageType, BitSize >::set ( size_type bitpos,
size_type length,
bool bit )
inlinenodiscardnoexcept

Set length bits starting at bitpos of this bitfield to the given value bit.

Returns
true on sucess, otherwise false

Definition at line 304 of file bitfield.hpp.

◆ setAll()

template<jau::req::unsigned_integral StorageType, size_t BitSize>
bitfield_t & jau::bitfield_t< StorageType, BitSize >::setAll ( bool bit)
inlinenoexcept

Set all bits of this bitfield to the given value bit.

Definition at line 342 of file bitfield.hpp.

Here is the caller graph for this function:

◆ copyUnit()

template<jau::req::unsigned_integral StorageType, size_t BitSize>
bool jau::bitfield_t< StorageType, BitSize >::copyUnit ( size_type srcBitpos,
size_type dstBitpos,
size_type length )
inlinenodiscardnoexcept

Copies length bits at position srcBitpos to position dstBitpos, returning the copied bits.

Returns
true on sucess, otherwise false

Definition at line 351 of file bitfield.hpp.

Here is the caller graph for this function:

◆ count()

template<jau::req::unsigned_integral StorageType, size_t BitSize>
size_type jau::bitfield_t< StorageType, BitSize >::count ( ) const
inlinenoexcept

Definition at line 356 of file bitfield.hpp.

Here is the caller graph for this function:

◆ operator==()

template<jau::req::unsigned_integral StorageType, size_t BitSize>
bool jau::bitfield_t< StorageType, BitSize >::operator== ( const bitfield_t< StorageType, BitSize > & rhs) const
inlineconstexprnoexcept

Definition at line 363 of file bitfield.hpp.

◆ put() [3/3]

template<jau::req::unsigned_integral StorageType, size_t BitSize>
template<size_t OBitSize>
bool jau::bitfield_t< StorageType, BitSize >::put ( size_t bitpos,
const bitfield_t< StorageType, OBitSize > & o )
inlinenodiscard

Definition at line 378 of file bitfield.hpp.

◆ subbits()

template<jau::req::unsigned_integral StorageType, size_t BitSize>
template<size_t BitLength>
std::pair< bitfield_t< StorageType, BitLength >, bool > jau::bitfield_t< StorageType, BitSize >::subbits ( size_type bitpos) const
inlinenodiscardnoexcept

Definition at line 410 of file bitfield.hpp.

◆ toString() [1/2]

template<jau::req::unsigned_integral StorageType, size_t BitSize>
std::string jau::bitfield_t< StorageType, BitSize >::toString ( size_type bitpos,
size_type length,
PrefixOpt prefix = PrefixOpt::none ) const
inlinenoexcept

Definition at line 442 of file bitfield.hpp.

Here is the caller graph for this function:

◆ toString() [2/2]

template<jau::req::unsigned_integral StorageType, size_t BitSize>
std::string jau::bitfield_t< StorageType, BitSize >::toString ( PrefixOpt prefix = PrefixOpt::none) const
inlinenoexcept

Definition at line 471 of file bitfield.hpp.

◆ infoString()

template<jau::req::unsigned_integral StorageType, size_t BitSize>
std::string jau::bitfield_t< StorageType, BitSize >::infoString ( ) const
inlinenoexcept

Definition at line 475 of file bitfield.hpp.

Member Data Documentation

◆ unit_byte_size

template<jau::req::unsigned_integral StorageType, size_t BitSize>
size_type jau::bitfield_t< StorageType, BitSize >::unit_byte_size = sizeof(unit_type)
staticconstexpr

One unit size in bytes.

Definition at line 57 of file bitfield.hpp.

◆ unit_bit_size

template<jau::req::unsigned_integral StorageType, size_t BitSize>
size_type jau::bitfield_t< StorageType, BitSize >::unit_bit_size = unit_byte_size * CHAR_BIT
staticconstexpr

One unit size in bits.

Definition at line 58 of file bitfield.hpp.

◆ unit_shift

template<jau::req::unsigned_integral StorageType, size_t BitSize>
size_type jau::bitfield_t< StorageType, BitSize >::unit_shift = jau::log2_byteshift(unit_byte_size)
staticconstexpr

One unit shift amount.

Definition at line 59 of file bitfield.hpp.

◆ bit_size

template<jau::req::unsigned_integral StorageType, size_t BitSize>
size_type jau::bitfield_t< StorageType, BitSize >::bit_size = BitSize
staticconstexpr

Storage size in bits.

Definition at line 60 of file bitfield.hpp.

◆ unit_size

template<jau::req::unsigned_integral StorageType, size_t BitSize>
size_type jau::bitfield_t< StorageType, BitSize >::unit_size = std::max<size_type>(1, (bit_size + unit_bit_size - 1) >> unit_shift)
staticconstexpr

Storage size in units.

Definition at line 66 of file bitfield.hpp.


The documentation for this class was generated from the following file: