Quantum++  v1.1
A modern C++11 quantum computing library
qpp::Dynamic_bitset Class Reference

Dynamic bitset class, allows the specification of the number of bits at runtime (unlike std::bitset<N>) More...

#include <classes/reversible.h>

Inheritance diagram for qpp::Dynamic_bitset:
Collaboration diagram for qpp::Dynamic_bitset:

Public Types

using value_type = unsigned int
 Type of the storage elements. More...
 
using storage_type = std::vector< value_type >
 Type of the storage. More...
 

Public Member Functions

 Dynamic_bitset (idx N)
 Constructor, initializes all bits to false (zero) More...
 
const storage_typedata () const
 Raw storage space of the bitset. More...
 
idx size () const noexcept
 Number of bits stored in the bitset. More...
 
idx storage_size () const noexcept
 Size of the underlying storage space (in units of value_type, unsigned int by default) More...
 
idx count () const noexcept
 Number of bits set to one in the bitset (Hamming weight) More...
 
bool get (idx pos) const noexcept
 The value of the bit at position pos. More...
 
bool none () const noexcept
 Checks whether none of the bits are set. More...
 
bool all () const noexcept
 Checks whether all bits are set. More...
 
bool any () const noexcept
 Checks whether any bit is set. More...
 
Dynamic_bitsetset (idx pos, bool value=true)
 Sets the bit at position pos. More...
 
Dynamic_bitsetset () noexcept
 Set all bits to true. More...
 
Dynamic_bitsetrand (idx pos, double p=0.5)
 Sets the bit at position pos according to a Bernoulli(p) distribution. More...
 
Dynamic_bitsetrand (double p=0.5)
 Sets all bits according to a Bernoulli(p) distribution. More...
 
Dynamic_bitsetreset (idx pos)
 Sets the bit at position pos to false. More...
 
Dynamic_bitsetreset () noexcept
 Sets all bits to false. More...
 
Dynamic_bitsetflip (idx pos)
 Flips the bit at position pos. More...
 
Dynamic_bitsetflip () noexcept
 Flips all bits. More...
 
bool operator== (const Dynamic_bitset &rhs) const noexcept
 Equality operator. More...
 
bool operator!= (const Dynamic_bitset &rhs) const noexcept
 Inequality operator. More...
 
idx operator- (const Dynamic_bitset &rhs) const noexcept
 Number of places the two bitsets differ (Hamming distance) More...
 
template<class CharT = char, class Traits = std::char_traits<CharT>, class Allocator = std::allocator<CharT>>
std::basic_string< CharT, Traits, Allocator > to_string (CharT zero=CharT('0'), CharT one=CharT('1')) const
 String representation. More...
 
- Public Member Functions inherited from qpp::IDisplay
 IDisplay ()=default
 Default constructor. More...
 
 IDisplay (const IDisplay &)=default
 Default copy constructor. More...
 
 IDisplay (IDisplay &&)=default
 Default move constructor. More...
 
IDisplayoperator= (const IDisplay &)=default
 Default copy assignment operator. More...
 
IDisplayoperator= (IDisplay &&)=default
 Default move assignment operator. More...
 
virtual ~IDisplay ()=default
 Default virtual destructor. More...
 

Protected Member Functions

idx index_ (idx pos) const
 Index of the pos bit in the storage space. More...
 
idx offset_ (idx pos) const
 Offset of the pos bit in the storage space relative to its index. More...
 

Protected Attributes

idx storage_size_
 Storage size. More...
 
idx N_
 Number of bits. More...
 
std::vector< value_typev_
 Storage space. More...
 

Private Member Functions

std::ostream & display (std::ostream &os) const override
 qpp::IDisplay::display() override, displays the bitset bit by bit More...
 

Detailed Description

Dynamic bitset class, allows the specification of the number of bits at runtime (unlike std::bitset<N>)

Member Typedef Documentation

◆ storage_type

Type of the storage.

◆ value_type

using qpp::Dynamic_bitset::value_type = unsigned int

Type of the storage elements.

Constructor & Destructor Documentation

◆ Dynamic_bitset()

qpp::Dynamic_bitset::Dynamic_bitset ( idx  N)
inline

Constructor, initializes all bits to false (zero)

Parameters
NNumber of bits in the bitset

Member Function Documentation

◆ all()

bool qpp::Dynamic_bitset::all ( ) const
inlinenoexcept

Checks whether all bits are set.

Returns
True if all of the bits are set

◆ any()

bool qpp::Dynamic_bitset::any ( ) const
inlinenoexcept

Checks whether any bit is set.

Returns
True if any of the bits is set

◆ count()

idx qpp::Dynamic_bitset::count ( ) const
inlinenoexcept

Number of bits set to one in the bitset (Hamming weight)

Returns
Hamming weight

◆ data()

const storage_type& qpp::Dynamic_bitset::data ( ) const
inline

Raw storage space of the bitset.

Returns
Const reference to the underlying storage space

◆ display()

std::ostream& qpp::Dynamic_bitset::display ( std::ostream &  os) const
inlineoverrideprivatevirtual

qpp::IDisplay::display() override, displays the bitset bit by bit

Parameters
osOutput stream
Returns
Reference to the output stream

Implements qpp::IDisplay.

◆ flip() [1/2]

Dynamic_bitset& qpp::Dynamic_bitset::flip ( idx  pos)
inline

Flips the bit at position pos.

Parameters
posPosition in the bitset
Returns
Reference to the current instance

◆ flip() [2/2]

Dynamic_bitset& qpp::Dynamic_bitset::flip ( )
inlinenoexcept

Flips all bits.

Returns
Reference to the current instance

◆ get()

bool qpp::Dynamic_bitset::get ( idx  pos) const
inlinenoexcept

The value of the bit at position pos.

Parameters
posPosition in the bitset
Returns
The value of the bit at position pos

◆ index_()

idx qpp::Dynamic_bitset::index_ ( idx  pos) const
inlineprotected

Index of the pos bit in the storage space.

Parameters
posBit location
Returns
Index of the pos bit in the storage space

◆ none()

bool qpp::Dynamic_bitset::none ( ) const
inlinenoexcept

Checks whether none of the bits are set.

Returns
True if none of the bits are set

◆ offset_()

idx qpp::Dynamic_bitset::offset_ ( idx  pos) const
inlineprotected

Offset of the pos bit in the storage space relative to its index.

Parameters
posBit location
Returns
Offset of the pos bit in the storage space relative to its index

◆ operator!=()

bool qpp::Dynamic_bitset::operator!= ( const Dynamic_bitset rhs) const
inlinenoexcept

Inequality operator.

Parameters
rhsDynamic_bitset against which the inequality is being tested
Returns
True if the bitsets are not equal (bit by bit), false otherwise

◆ operator-()

idx qpp::Dynamic_bitset::operator- ( const Dynamic_bitset rhs) const
inlinenoexcept

Number of places the two bitsets differ (Hamming distance)

Parameters
rhsDynamic_bitset against which the the Hamming distance is computed
Returns
Hamming distance

◆ operator==()

bool qpp::Dynamic_bitset::operator== ( const Dynamic_bitset rhs) const
inlinenoexcept

Equality operator.

Parameters
rhsDynamic_bitset against which the equality is being tested
Returns
True if the bitsets are equal (bit by bit), false otherwise

◆ rand() [1/2]

Dynamic_bitset& qpp::Dynamic_bitset::rand ( idx  pos,
double  p = 0.5 
)
inline

Sets the bit at position pos according to a Bernoulli(p) distribution.

Parameters
posPosition in the bitset
pProbability
Returns
Reference to the current instance

◆ rand() [2/2]

Dynamic_bitset& qpp::Dynamic_bitset::rand ( double  p = 0.5)
inline

Sets all bits according to a Bernoulli(p) distribution.

Parameters
pProbability
Returns
Reference to the current instance

◆ reset() [1/2]

Dynamic_bitset& qpp::Dynamic_bitset::reset ( idx  pos)
inline

Sets the bit at position pos to false.

Parameters
posPosition in the bitset
Returns
Reference to the current instance

◆ reset() [2/2]

Dynamic_bitset& qpp::Dynamic_bitset::reset ( )
inlinenoexcept

Sets all bits to false.

Returns
Reference to the current instance

◆ set() [1/2]

Dynamic_bitset& qpp::Dynamic_bitset::set ( idx  pos,
bool  value = true 
)
inline

Sets the bit at position pos.

Parameters
posPosition in the bitset
valueBit value
Returns
Reference to the current instance

◆ set() [2/2]

Dynamic_bitset& qpp::Dynamic_bitset::set ( )
inlinenoexcept

Set all bits to true.

Returns
Reference to the current instance

◆ size()

idx qpp::Dynamic_bitset::size ( ) const
inlinenoexcept

Number of bits stored in the bitset.

Returns
Number of bits stored in the bitset

◆ storage_size()

idx qpp::Dynamic_bitset::storage_size ( ) const
inlinenoexcept

Size of the underlying storage space (in units of value_type, unsigned int by default)

Returns
Size of the underlying storage space

◆ to_string()

template<class CharT = char, class Traits = std::char_traits<CharT>, class Allocator = std::allocator<CharT>>
std::basic_string<CharT, Traits, Allocator> qpp::Dynamic_bitset::to_string ( CharT  zero = CharT('0'),
CharT  one = CharT('1') 
) const
inline

String representation.

Template Parameters
CharTString character type
TraitsString traits
AllocatorString Allocator
Parameters
zeroCharacter representing the zero
oneCharacter representing the one
Returns
The bitset as a string

Member Data Documentation

◆ N_

idx qpp::Dynamic_bitset::N_
protected

Number of bits.

◆ storage_size_

idx qpp::Dynamic_bitset::storage_size_
protected

Storage size.

◆ v_

std::vector<value_type> qpp::Dynamic_bitset::v_
protected

Storage space.


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