C++ Distributed Hash Table
Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
dht::Hash< N > Class Template Reference

#include <infohash.h>

Public Types

using T = std::array< uint8_t, N >
 
typedef T::iterator iterator
 
typedef T::const_iterator const_iterator
 

Public Member Functions

 Hash (const uint8_t *h, size_t data_len)
 
 Hash (const std::string &hex)
 
 Hash (const msgpack::object &o)
 
size_t size () const
 
const uint8_t * data () const
 
uint8_t * data ()
 
iterator begin ()
 
const_iterator cbegin () const
 
iterator end ()
 
const_iterator cend () const
 
bool operator== (const Hash &h) const
 
bool operator!= (const Hash &h) const
 
bool operator< (const Hash &o) const
 
 operator bool () const
 
uint8_t & operator[] (size_t index)
 
const uint8_t & operator[] (size_t index) const
 
int lowbit () const
 
int xorCmp (const Hash &id1, const Hash &id2) const
 
bool getBit (unsigned nbit) const
 
void setBit (unsigned nbit, bool b)
 
double toFloat () const
 
const char * to_c_str () const
 
std::string toString () const
 
template<typename Packer >
void msgpack_pack (Packer &pk) const
 
void msgpack_unpack (msgpack::object o)
 

Static Public Member Functions

static int cmp (const Hash &id1, const Hash &id2)
 
static unsigned commonBits (const Hash &id1, const Hash &id2)
 
static Hash get (const std::string &data)
 
static Hash get (const std::vector< uint8_t > &data)
 
static Hash get (const uint8_t *data, size_t data_len)
 
static Hash getRandom ()
 

Friends

template<size_t M>
OPENDHT_PUBLIC friend std::ostream & operator<< (std::ostream &s, const Hash< M > &h)
 
template<size_t M>
OPENDHT_PUBLIC friend std::istream & operator>> (std::istream &s, Hash< M > &h)
 

Detailed Description

template<size_t N>
class dht::Hash< N >

Represents an InfoHash. An InfoHash is a byte array of HASH_LEN bytes. InfoHashes identify nodes and values in the Dht.

Definition at line 62 of file infohash.h.

Constructor & Destructor Documentation

◆ Hash()

template<size_t N>
dht::Hash< N >::Hash ( const std::string &  hex)
explicit

Constructor from an hexadecimal string (without "0x"). hex must be at least 2.HASH_LEN characters long. If too long, only the first 2.HASH_LEN characters are read.

Definition at line 290 of file infohash.h.

Member Function Documentation

◆ cmp()

template<size_t N>
static int dht::Hash< N >::cmp ( const Hash< N > &  id1,
const Hash< N > &  id2 
)
inlinestatic

Forget about the `‘XOR-metric’'. An id is just a path from the root of the tree, so bits are numbered from the start.

Definition at line 149 of file infohash.h.

◆ commonBits()

template<size_t N>
static unsigned dht::Hash< N >::commonBits ( const Hash< N > &  id1,
const Hash< N > &  id2 
)
inlinestatic

Find how many bits two ids have in common.

Definition at line 155 of file infohash.h.

◆ get()

template<size_t N>
static Hash dht::Hash< N >::get ( const uint8_t *  data,
size_t  data_len 
)
inlinestatic

Computes the hash from a given data buffer of size data_len.

Definition at line 231 of file infohash.h.

◆ lowbit()

template<size_t N>
int dht::Hash< N >::lowbit ( ) const
inline

Find the lowest 1 bit in an id. Result will allways be lower than 8*N

Definition at line 132 of file infohash.h.

◆ xorCmp()

template<size_t N>
int dht::Hash< N >::xorCmp ( const Hash< N > &  id1,
const Hash< N > &  id2 
) const
inline

Determine whether id1 or id2 is closer to this

Definition at line 180 of file infohash.h.


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