Furrent
|
#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) |
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.
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:
bitset
only works with a number of bits known at compile time but we don't know the number of bits, which matches the number of pieces, until we read a torrent file.std::vector<bool>
is a specialized template which shouldn't actually result in entire bytes being allocated. Because it is not, then, a standard STL container, its usage is discouraged. See https://en.wikipedia.org/wiki/Bit_array.
|
explicit |
Create a new Bitfield
with the provided length (in bits).
|
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.
bool fur::download::bitfield::Bitfield::get | ( | int64_t | index | ) | const |
Get the value of the bit at the provided index.
|
inline |
Get the bitfield as an array of bytes.
|
inline |
Set the bit to the provided index to 1.
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
.
|
inline |
Set the bit to the provided index to 0.
|
friend |
Display the Bitfield
as a 8-column grid. Each row is a byte. Bit 0 is in the topmost left corner.
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.
Bitfield
cannot be resized once created.