Furrent
Public Member Functions | Public Attributes | Friends | List of all members
fur::download::bitfield::Bitfield Class Reference

#include <bitfield.hpp>

Public Member Functions

 Bitfield (int64_t len)
 Create a new Bitfield with the provided length (in bits). More...
 
 Bitfield (std::vector< uint8_t > storage, int64_t len)
 
void set (int64_t index, bool value)
 Set the bit to the provided index to either 1 or 0, depending on value. More...
 
void set (int64_t index)
 Set the bit to the provided index to 1. More...
 
void unset (int64_t index)
 Set the bit to the provided index to 0. More...
 
bool get (int64_t index) const
 Get the value of the bit at the provided index. More...
 
std::vector< uint8_t > get_bytes () const
 Get the bitfield as an array of bytes. More...
 

Public Attributes

const int64_t len
 

Friends

std::ostream & operator<< (std::ostream &os, const Bitfield &bitfield)
 

Detailed Description

Bitfield is basically just a bit array that is used to keep track of what pieces a peer has to offer. A 1-bit indicates a piece that we can ask for while a 0-bit is a piece that the peer doesn't have.

Attention
A Bitfield cannot be resized once created.

Note that we could've used a type from the standard library instead but they had a couple of disadvantages:

Constructor & Destructor Documentation

◆ Bitfield() [1/2]

fur::download::bitfield::Bitfield::Bitfield ( int64_t  len)
explicit

Create a new Bitfield with the provided length (in bits).

◆ Bitfield() [2/2]

fur::download::bitfield::Bitfield::Bitfield ( std::vector< uint8_t >  storage,
int64_t  len 
)
inline

Create a new Bitfield from an existing backing storage. The length in bits must be provided because the last byte is not necessarily used till the last bit.

Member Function Documentation

◆ get()

bool fur::download::bitfield::Bitfield::get ( int64_t  index) const

Get the value of the bit at the provided index.

◆ get_bytes()

std::vector<uint8_t> fur::download::bitfield::Bitfield::get_bytes ( ) const
inline

Get the bitfield as an array of bytes.

◆ set() [1/2]

void fur::download::bitfield::Bitfield::set ( int64_t  index)
inline

Set the bit to the provided index to 1.

◆ set() [2/2]

void fur::download::bitfield::Bitfield::set ( int64_t  index,
bool  value 
)

Set the bit to the provided index to either 1 or 0, depending on value.

◆ unset()

void fur::download::bitfield::Bitfield::unset ( int64_t  index)
inline

Set the bit to the provided index to 0.

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const Bitfield bitfield 
)
friend

Display the Bitfield as a 8-column grid. Each row is a byte. Bit 0 is in the topmost left corner.

Member Data Documentation

◆ len

const int64_t fur::download::bitfield::Bitfield::len

Store the length for checking that no out-of-bounds read or write is performed. The actual length in bits cannot be computed from the number of bytes in storage because the last byte might not be used in its entirety if the number of bits is not a multiple of 8.

Attention
A Bitfield cannot be resized once created.

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