Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
Nz::Bitset< Block, Allocator > Class Template Reference

Core class that represents a set of bits. More...

Public Types

using PointerSequence = std::pair< const void *, std::size_t >
 

Public Member Functions

 Bitset ()
 Constructs a Bitset object by default.
 
 Bitset (std::size_t bitCount, bool val)
 Constructs a Bitset object of bitCount bits to value val. More...
 
 Bitset (const char *bits)
 Constructs a Bitset object from the contents initialized with a copy of the null-terminated character string pointed to by bits. More...
 
 Bitset (const char *bits, std::size_t bitCount)
 Constructs a Bitset object from the contents initialized with a copy of the character string pointed to by bits takings the bitCount first characters. More...
 
 Bitset (const Bitset &bitset)=default
 
 Bitset (const String &bits)
 Constructs a Bitset object from a Nz::String. More...
 
template<typename T >
 Bitset (T value)
 Constructs a Bitset copying an unsigned integral number. More...
 
 Bitset (Bitset &&bitset) noexcept=default
 
template<typename T >
void AppendBits (T bits, std::size_t bitCount)
 Appends bits to the bitset. More...
 
void Clear () noexcept
 Clears the content of the bitset. More...
 
std::size_t Count () const
 Counts the number of bits set to 1. More...
 
void Flip ()
 Flips each bit of the bitset. More...
 
std::size_t FindFirst () const
 Finds the first bit set to one in the bitset. More...
 
std::size_t FindNext (std::size_t bit) const
 Finds the next enabled in the bitset. More...
 
Block GetBlock (std::size_t i) const
 Gets the ith block. More...
 
std::size_t GetBlockCount () const
 Gets the number of blocks. More...
 
std::size_t GetCapacity () const
 Gets the capacity of the bitset. More...
 
std::size_t GetSize () const
 Gets the number of bits. More...
 
PointerSequence Read (const void *ptr, std::size_t bitCount)
 Read a byte sequence into a bitset. More...
 
PointerSequence Read (const PointerSequence &sequence, std::size_t bitCount)
 Read a byte sequence into a bitset. More...
 
void PerformsAND (const Bitset &a, const Bitset &b)
 Performs the "AND" operator between two bitsets. More...
 
void PerformsNOT (const Bitset &a)
 Performs the "NOT" operator of the bitset. More...
 
void PerformsOR (const Bitset &a, const Bitset &b)
 Performs the "OR" operator between two bitsets. More...
 
void PerformsXOR (const Bitset &a, const Bitset &b)
 Performs the "XOR" operator between two bitsets. More...
 
bool Intersects (const Bitset &bitset) const
 Checks if bitsets have one block in common. More...
 
void Reserve (std::size_t bitCount)
 Reserves enough blocks to contain bitCount bits. More...
 
void Resize (std::size_t bitCount, bool defaultVal=false)
 Resizes the bitset to the size of bitCount. More...
 
void Reset ()
 Resets the bitset to zero bits.
 
void Reset (std::size_t bit)
 Resets the bit at the index. More...
 
void Reverse ()
 Reverse the order of bits in a bitset. More...
 
void Set (bool val=true)
 Sets the bitset to val. More...
 
void Set (std::size_t bit, bool val=true)
 Sets the bit at the index. More...
 
void SetBlock (std::size_t i, Block block)
 Set the ith block. More...
 
void ShiftLeft (std::size_t pos)
 Shift all the bits toward the left. More...
 
void ShiftRight (std::size_t pos)
 Shift all the bits toward the right. More...
 
void Swap (Bitset &bitset)
 Swaps the two bitsets. More...
 
bool Test (std::size_t bit) const
 Tests the ith bit. More...
 
bool TestAll () const
 Tests each block. More...
 
bool TestAny () const
 Tests if one bit is set. More...
 
bool TestNone () const
 Tests if one bit is not set. More...
 
template<typename T >
To () const
 Converts the bitset to template type. More...
 
String ToString () const
 Gives a string representation. More...
 
void UnboundedReset (std::size_t bit)
 Resets the bit at the index. More...
 
void UnboundedSet (std::size_t bit, bool val=true)
 Sets the bit at the index. More...
 
bool UnboundedTest (std::size_t bit) const
 Tests the ith bit. More...
 
Bit operator[] (std::size_t index)
 Gets the ith bit. More...
 
bool operator[] (std::size_t index) const
 Gets the ith bit. More...
 
Bitset operator~ () const
 Negates the bitset. More...
 
Bitsetoperator= (const Bitset &bitset)=default
 
Bitsetoperator= (const String &bits)
 Sets this bitset from a Nz::String. More...
 
template<typename T >
Bitsetoperator= (T value)
 
Bitsetoperator= (Bitset &&bitset) noexcept=default
 
Bitset operator<< (std::size_t pos) const
 Shift all the bits toward the left. More...
 
Bitsetoperator<<= (std::size_t pos)
 Shift all the bits toward the left. More...
 
Bitset operator>> (std::size_t pos) const
 Shift all the bits toward the right. More...
 
Bitsetoperator>>= (std::size_t pos)
 Shift all the bits toward the right. More...
 
Bitsetoperator &= (const Bitset &bitset)
 
Bitsetoperator|= (const Bitset &bitset)
 Performs an "OR" with another bitset. More...
 
Bitsetoperator^= (const Bitset &bitset)
 Performs an "XOR" with another bitset. More...
 
template<typename T >
Bitset< Block, Allocator > & operator= (T value)
 Copies the internal representation of an unsigned integer. More...
 

Static Public Member Functions

static Bitset FromPointer (const void *ptr, std::size_t bitCount, PointerSequence *sequence=nullptr)
 Builds a bitset from a byte sequence. More...
 

Static Public Attributes

static constexpr Block fullBitMask = std::numeric_limits<Block>::max()
 
static constexpr std::size_t bitsPerBlock = BitCount<Block>()
 
static constexpr std::size_t npos = std::numeric_limits<std::size_t>::max()
 

Detailed Description

template<typename Block = UInt32, class Allocator = std::allocator<Block>>
class Nz::Bitset< Block, Allocator >

Core class that represents a set of bits.

This class meets the requirements of Container, AllocatorAwareContainer, SequenceContainer

Constructor & Destructor Documentation

◆ Bitset() [1/5]

template<typename Block , class Allocator >
Nz::Bitset< Block, Allocator >::Bitset ( std::size_t  bitCount,
bool  val 
)
explicit

Constructs a Bitset object of bitCount bits to value val.

Parameters
bitCountNumber of bits
valValue of those bits, by default false

◆ Bitset() [2/5]

template<typename Block , class Allocator >
Nz::Bitset< Block, Allocator >::Bitset ( const char *  bits)
explicit

Constructs a Bitset object from the contents initialized with a copy of the null-terminated character string pointed to by bits.

Parameters
bitsNull-terminated character string containing only '0' and '1'
Remarks
The length of the string is determined by the first null character, if there is no null character, the behaviour is undefined

◆ Bitset() [3/5]

template<typename Block , class Allocator >
Nz::Bitset< Block, Allocator >::Bitset ( const char *  bits,
std::size_t  bitCount 
)

Constructs a Bitset object from the contents initialized with a copy of the character string pointed to by bits takings the bitCount first characters.

Parameters
bitsCharacter string containing only '0' and '1'
bitCountNumber of characters to take into consideration
Remarks
If the length of the string is inferior to the bitCount, the behaviour is undefined

◆ Bitset() [4/5]

template<typename Block , class Allocator >
Nz::Bitset< Block, Allocator >::Bitset ( const String bits)
explicit

Constructs a Bitset object from a Nz::String.

Parameters
bitsString containing only '0' and '1'

◆ Bitset() [5/5]

template<typename Block , class Allocator >
template<typename T >
Nz::Bitset< Block, Allocator >::Bitset ( value)

Constructs a Bitset copying an unsigned integral number.

Parameters
valueNumber to be used as a base

Member Function Documentation

◆ AppendBits()

template<typename Block , class Allocator >
template<typename T >
void Nz::Bitset< Block, Allocator >::AppendBits ( bits,
std::size_t  bitCount 
)

Appends bits to the bitset.

This function extends the bitset with bits extracted from an integer value

Parameters
bitsAn integer value from where bits will be extracted
bitCountNumber of bits to extract from the value
Remarks
This function does not require bitCount to be lower or equal to the number of bits of T, thus reading 32 bits from a UInt8 will work (by extracting the first 8 bits values and appending 24 zeros afterneath).
See also
AppendBits
Read

◆ Clear()

template<typename Block , class Allocator >
void Nz::Bitset< Block, Allocator >::Clear ( )
noexcept

Clears the content of the bitset.

This function clears the bitset content, resetting its bit and block count at zero.

Remarks
This does not changes the bits values to zero but empties the bitset, to reset the bits use the Reset() function
This call does not changes the bitset capacity
See also
Reset()

◆ Count()

template<typename Block , class Allocator >
std::size_t Nz::Bitset< Block, Allocator >::Count ( ) const

Counts the number of bits set to 1.

Returns
Number of bits set to 1

◆ FindFirst()

template<typename Block , class Allocator >
std::size_t Nz::Bitset< Block, Allocator >::FindFirst ( ) const

Finds the first bit set to one in the bitset.

Returns
The 0-based index of the first bit enabled

◆ FindNext()

template<typename Block , class Allocator >
std::size_t Nz::Bitset< Block, Allocator >::FindNext ( std::size_t  bit) const

Finds the next enabled in the bitset.

Parameters
bitIndex of the last bit found, which will not be treated by this function
Returns
Index of the next enabled bit or npos if all the following bits are disabled
Remarks
This function is typically used in for-loops to iterate on bits

◆ Flip()

template<typename Block , class Allocator >
void Nz::Bitset< Block, Allocator >::Flip ( )

Flips each bit of the bitset.

This function flips every bit of the bitset, which means every '1' turns into a '0' and conversely.

◆ FromPointer()

template<typename Block , class Allocator >
Bitset< Block, Allocator > Nz::Bitset< Block, Allocator >::FromPointer ( const void *  ptr,
std::size_t  bitCount,
PointerSequence *  sequence = nullptr 
)
static

Builds a bitset from a byte sequence.

This function builds a bitset using a byte sequence by reading bitCount bits from it

Parameters
ptrA pointer to the start of the byte sequence
bitCountNumber of bits to read from the byte sequence
sequenceOptional data to pass to a next call to Read
Returns
The constructed bitset
Remarks
For technical reasons, ceil(bitCount / 8) bytes from the sequence will always be read (even with non-multiple-of-8 bitCount)
See also
AppendBits
Read

◆ GetBlock()

template<typename Block , class Allocator >
Block Nz::Bitset< Block, Allocator >::GetBlock ( std::size_t  i) const

Gets the ith block.

Returns
Block in the bitset
Parameters
iIndex of the block
Remarks
Produce a NazaraAssert if i is greather than number of blocks in bitset

◆ GetBlockCount()

template<typename Block , class Allocator >
std::size_t Nz::Bitset< Block, Allocator >::GetBlockCount ( ) const

Gets the number of blocks.

Returns
Number of blocks

◆ GetCapacity()

template<typename Block , class Allocator >
std::size_t Nz::Bitset< Block, Allocator >::GetCapacity ( ) const

Gets the capacity of the bitset.

Returns
Capacity of the bitset

◆ GetSize()

template<typename Block , class Allocator >
std::size_t Nz::Bitset< Block, Allocator >::GetSize ( ) const

Gets the number of bits.

Returns
Number of bits

◆ Intersects()

template<typename Block , class Allocator >
bool Nz::Bitset< Block, Allocator >::Intersects ( const Bitset< Block, Allocator > &  bitset) const

Checks if bitsets have one block in common.

Parameters
bitsetBitset to test

◆ operator<<()

template<typename Block , class Allocator >
Bitset< Block, Allocator > Nz::Bitset< Block, Allocator >::operator<< ( std::size_t  pos) const

Shift all the bits toward the left.

Parameters
posBit shifting to be applied
Returns
A copies of the bitset with shifted bits
Remarks
This does not changes the size of the bitset.
See also
ShiftLeft

◆ operator<<=()

template<typename Block , class Allocator >
Bitset< Block, Allocator > & Nz::Bitset< Block, Allocator >::operator<<= ( std::size_t  pos)

Shift all the bits toward the left.

Parameters
posBit shifting to be applied
Returns
A reference to this
Remarks
This does not changes the size of the bitset.
See also
ShiftLeft

◆ operator=() [1/2]

template<typename Block , class Allocator >
Bitset< Block, Allocator > & Nz::Bitset< Block, Allocator >::operator= ( const String bits)

Sets this bitset from a Nz::String.

Returns
A reference to this
Parameters
bitsString containing only '0' and '1'

◆ operator=() [2/2]

template<typename Block = UInt32, class Allocator = std::allocator<Block>>
template<typename T >
Bitset<Block, Allocator>& Nz::Bitset< Block, Allocator >::operator= ( value)

Copies the internal representation of an unsigned integer.

Returns
A reference to this
Parameters
valueUnsigned number which will be used as a source

◆ operator>>()

template<typename Block , class Allocator >
Bitset< Block, Allocator > Nz::Bitset< Block, Allocator >::operator>> ( std::size_t  pos) const

Shift all the bits toward the right.

Parameters
posBit shifting to be applied
Returns
A copies of the bitset with shifted bits
Remarks
This does not changes the size of the bitset.
See also
ShiftRight

◆ operator>>=()

template<typename Block , class Allocator >
Bitset< Block, Allocator > & Nz::Bitset< Block, Allocator >::operator>>= ( std::size_t  pos)

Shift all the bits toward the right.

Parameters
posBit shifting to be applied
Returns
A reference to this
Remarks
This does not changes the size of the bitset.
See also
ShiftRight

◆ operator[]() [1/2]

template<typename Block , class Allocator >
Bitset< Block, Allocator >::Bit Nz::Bitset< Block, Allocator >::operator[] ( std::size_t  index)

Gets the ith bit.

Returns
bit in ith position

◆ operator[]() [2/2]

template<typename Block , class Allocator >
bool Nz::Bitset< Block, Allocator >::operator[] ( std::size_t  index) const

Gets the ith bit.

Returns
bit in ith position

◆ operator^=()

template<typename Block , class Allocator >
Bitset< Block, Allocator > & Nz::Bitset< Block, Allocator >::operator^= ( const Bitset< Block, Allocator > &  bitset)

Performs an "XOR" with another bitset.

Returns
A reference to this
Parameters
bitsetOther bitset

◆ operator|=()

template<typename Block , class Allocator >
Bitset< Block, Allocator > & Nz::Bitset< Block, Allocator >::operator|= ( const Bitset< Block, Allocator > &  bitset)

Performs an "OR" with another bitset.

Returns
A reference to this
Parameters
bitsetOther bitset

◆ operator~()

template<typename Block , class Allocator >
Bitset< Block, Allocator > Nz::Bitset< Block, Allocator >::operator~ ( ) const

Negates the bitset.

Returns
A new bitset which is the "NOT" of this bitset

◆ PerformsAND()

template<typename Block , class Allocator >
void Nz::Bitset< Block, Allocator >::PerformsAND ( const Bitset< Block, Allocator > &  a,
const Bitset< Block, Allocator > &  b 
)

Performs the "AND" operator between two bitsets.

Parameters
aFirst bitset
bSecond bitset
Remarks
The "AND" is performed with all the bits of the smallest bitset and the capacity of this is set to the largest of the two bitsets

◆ PerformsNOT()

template<typename Block , class Allocator >
void Nz::Bitset< Block, Allocator >::PerformsNOT ( const Bitset< Block, Allocator > &  a)

Performs the "NOT" operator of the bitset.

Parameters
aBitset to negate

◆ PerformsOR()

template<typename Block , class Allocator >
void Nz::Bitset< Block, Allocator >::PerformsOR ( const Bitset< Block, Allocator > &  a,
const Bitset< Block, Allocator > &  b 
)

Performs the "OR" operator between two bitsets.

Parameters
aFirst bitset
bSecond bitset
Remarks
The "OR" is performed with all the bits of the smallest bitset and the others are copied from the largest and the capacity of this is set to the largest of the two bitsets

◆ PerformsXOR()

template<typename Block , class Allocator >
void Nz::Bitset< Block, Allocator >::PerformsXOR ( const Bitset< Block, Allocator > &  a,
const Bitset< Block, Allocator > &  b 
)

Performs the "XOR" operator between two bitsets.

Parameters
aFirst bitset
bSecond bitset
Remarks
The "XOR" is performed with all the bits of the smallest bitset and the others are copied from the largest and the capacity of this is set to the largest of the two bitsets

◆ Read() [1/2]

template<typename Block , class Allocator >
Bitset< Block, Allocator >::PointerSequence Nz::Bitset< Block, Allocator >::Read ( const void *  ptr,
std::size_t  bitCount 
)

Read a byte sequence into a bitset.

This function extends the bitset with bits read from a byte sequence

Parameters
ptrA pointer to the start of the byte sequence
bitCountNumber of bits to read from the byte sequence
Returns
A pointer to the next byte to read along with the next bit index (useful when reading multiple times)
Remarks
For technical reasons, ceil(bitCount / 8) bytes from the sequence will always be read (even with non-multiple-of-8 bitCount)
See also
AppendBits
Read

◆ Read() [2/2]

template<typename Block , class Allocator >
Bitset< Block, Allocator >::PointerSequence Nz::Bitset< Block, Allocator >::Read ( const PointerSequence &  sequence,
std::size_t  bitCount 
)

Read a byte sequence into a bitset.

This function extends the bitset with bits read from a pointer sequence (made of a pointer and a bit index)

Parameters
sequenceA pointer sequence to the start of the byte sequence
bitCountNumber of bits to read from the byte sequence
Returns
A pointer to the next byte to read along with the next bit index (useful when reading multiple times)
Remarks
For technical reasons, ceil(bitCount / 8) bytes from the sequence will always be read (even with non-multiple-of-8 bitCount)
See also
AppendBits
Read

◆ Reserve()

template<typename Block , class Allocator >
void Nz::Bitset< Block, Allocator >::Reserve ( std::size_t  bitCount)

Reserves enough blocks to contain bitCount bits.

Parameters
bitCountNumber of bits to reserve

◆ Reset()

template<typename Block , class Allocator >
void Nz::Bitset< Block, Allocator >::Reset ( std::size_t  bit)

Resets the bit at the index.

Parameters
bitIndex of the bit
See also
UnboundReset

◆ Resize()

template<typename Block , class Allocator >
void Nz::Bitset< Block, Allocator >::Resize ( std::size_t  bitCount,
bool  defaultVal = false 
)

Resizes the bitset to the size of bitCount.

Parameters
bitCountNumber of bits to resize
defaultValValue of the bits if new size is greather than the old one

◆ Reverse()

template<typename Block , class Allocator >
void Nz::Bitset< Block, Allocator >::Reverse ( )

Reverse the order of bits in a bitset.

Reverse the order of bits in the bitset (first bit swap with the last one, etc.)

◆ Set() [1/2]

template<typename Block , class Allocator >
void Nz::Bitset< Block, Allocator >::Set ( bool  val = true)

Sets the bitset to val.

Parameters
valValue of the bits

◆ Set() [2/2]

template<typename Block , class Allocator >
void Nz::Bitset< Block, Allocator >::Set ( std::size_t  bit,
bool  val = true 
)

Sets the bit at the index.

Parameters
bitIndex of the bit
valValue of the bit
Remarks
Produce a NazaraAssert if bit is greather than number of bits in bitset
See also
UnboundSet

◆ SetBlock()

template<typename Block, class Allocator >
void Nz::Bitset< Block, Allocator >::SetBlock ( std::size_t  i,
Block  block 
)

Set the ith block.

Parameters
iIndex of the block
blockBlock to set
Remarks
Produce a NazaraAssert if i is greather than number of blocks in bitset

◆ ShiftLeft()

template<typename Block , class Allocator >
void Nz::Bitset< Block, Allocator >::ShiftLeft ( std::size_t  pos)

Shift all the bits toward the left.

Parameters
posBit shifting to be applied
Remarks
This does not changes the size of the bitset.
See also
operator<<=

◆ ShiftRight()

template<typename Block , class Allocator >
void Nz::Bitset< Block, Allocator >::ShiftRight ( std::size_t  pos)

Shift all the bits toward the right.

Parameters
posBit shifting to be applied
Remarks
This does not changes the size of the bitset.
See also
operator>>=

◆ Swap()

template<typename Block , class Allocator >
void Nz::Bitset< Block, Allocator >::Swap ( Bitset< Block, Allocator > &  bitset)

Swaps the two bitsets.

Parameters
bitsetOther bitset to swap

◆ Test()

template<typename Block , class Allocator >
bool Nz::Bitset< Block, Allocator >::Test ( std::size_t  bit) const

Tests the ith bit.

Returns
true if bit is set
Parameters
bitIndex of the bit
Remarks
Produce a NazaraAssert if bit is greather than number of bits in bitset
See also
UnboundTest

◆ TestAll()

template<typename Block , class Allocator >
bool Nz::Bitset< Block, Allocator >::TestAll ( ) const

Tests each block.

Returns
true if each block is set

◆ TestAny()

template<typename Block , class Allocator >
bool Nz::Bitset< Block, Allocator >::TestAny ( ) const

Tests if one bit is set.

Returns
true if one bit is set

◆ TestNone()

template<typename Block , class Allocator >
bool Nz::Bitset< Block, Allocator >::TestNone ( ) const

Tests if one bit is not set.

Returns
true if one bit is not set

◆ To()

template<typename Block , class Allocator >
template<typename T >
T Nz::Bitset< Block, Allocator >::To ( ) const

Converts the bitset to template type.

Returns
The conversion of the bitset
Remarks
Produce a NazaraAssert if the template type can not hold the number of bits

◆ ToString()

template<typename Block , class Allocator >
String Nz::Bitset< Block, Allocator >::ToString ( ) const

Gives a string representation.

Returns
A string representation of the object with only '0' and '1'

◆ UnboundedReset()

template<typename Block , class Allocator >
void Nz::Bitset< Block, Allocator >::UnboundedReset ( std::size_t  bit)

Resets the bit at the index.

Parameters
bitIndex of the bit
Remarks
if bit is greather than the number of bits, the bitset is enlarged and the added bits are set to false
See also
Reset

◆ UnboundedSet()

template<typename Block , class Allocator >
void Nz::Bitset< Block, Allocator >::UnboundedSet ( std::size_t  bit,
bool  val = true 
)

Sets the bit at the index.

Parameters
bitIndex of the bit
valValue of the bit
Remarks
if bit is greater than the number of bits, the bitset is enlarged and the added bits are set to false and the one at bit is set to val
See also
Set

◆ UnboundedTest()

template<typename Block , class Allocator >
bool Nz::Bitset< Block, Allocator >::UnboundedTest ( std::size_t  bit) const

Tests the ith bit.

Returns
true if bit is set
Parameters
bitIndex of the bit
See also
Test

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