veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
altintegration::ChainSlice< BlockIndexT > Struct Template Reference

Non-owning partial chain view similar to std::span. More...

Detailed Description

template<typename BlockIndexT>
struct altintegration::ChainSlice< BlockIndexT >

API-compatible with Chain.

Template Parameters
BlockIndex

Definition at line 20 of file chain_slice.hpp.

#include <chain_slice.hpp>

+ Collaboration diagram for altintegration::ChainSlice< BlockIndexT >:

Public Types

using index_t = BlockIndexT
 
using block_t = typename index_t::block_t
 
using hash_t = typename index_t::hash_t
 
using height_t = typename block_t::height_t
 
using chain_t = Chain< index_t >
 
using const_iterator_t = typename chain_t::const_iterator_t
 
using const_reverse_iterator_t = typename chain_t::const_reverse_iterator_t
 

Public Member Functions

 ChainSlice (const chain_t &chain)
 
 ChainSlice (const chain_t &chain, height_t firstHeight)
 
 ChainSlice (const chain_t &chain, height_t firstHeight, size_t size)
 
height_t getStartHeight () const
 
bool contains (const index_t *index) const
 
index_t * operator[] (height_t height) const
 
index_t * next (const index_t *index) const
 
height_t firstHeight () const
 
height_t tipHeight () const
 
height_t chainHeight () const
 
bool empty () const
 
size_t size () const
 
size_t blocksCount () const
 
index_t * tip () const
 
index_t * first () const
 
size_t firstOffset () const
 
size_t tipOffset () const
 
const_reverse_iterator_t rbegin () const
 
const_reverse_iterator_t rend () const
 
const_iterator_t begin () const
 
const_iterator_t end () const
 

Friends

bool operator== (const ChainSlice &a, const ChainSlice &b)
 
bool operator!= (const ChainSlice &a, const ChainSlice &b)
 

Member Typedef Documentation

◆ block_t

template<typename BlockIndexT >
using altintegration::ChainSlice< BlockIndexT >::block_t = typename index_t::block_t

Definition at line 22 of file chain_slice.hpp.

◆ chain_t

template<typename BlockIndexT >
using altintegration::ChainSlice< BlockIndexT >::chain_t = Chain<index_t>

Definition at line 25 of file chain_slice.hpp.

◆ const_iterator_t

template<typename BlockIndexT >
using altintegration::ChainSlice< BlockIndexT >::const_iterator_t = typename chain_t::const_iterator_t

Definition at line 27 of file chain_slice.hpp.

◆ const_reverse_iterator_t

template<typename BlockIndexT >
using altintegration::ChainSlice< BlockIndexT >::const_reverse_iterator_t = typename chain_t::const_reverse_iterator_t

Definition at line 28 of file chain_slice.hpp.

◆ hash_t

template<typename BlockIndexT >
using altintegration::ChainSlice< BlockIndexT >::hash_t = typename index_t::hash_t

Definition at line 23 of file chain_slice.hpp.

◆ height_t

template<typename BlockIndexT >
using altintegration::ChainSlice< BlockIndexT >::height_t = typename block_t::height_t

Definition at line 24 of file chain_slice.hpp.

◆ index_t

template<typename BlockIndexT >
using altintegration::ChainSlice< BlockIndexT >::index_t = BlockIndexT

Definition at line 21 of file chain_slice.hpp.

Constructor & Destructor Documentation

◆ ChainSlice() [1/3]

template<typename BlockIndexT >
altintegration::ChainSlice< BlockIndexT >::ChainSlice ( const chain_t chain)
inline

Definition at line 30 of file chain_slice.hpp.

30: ChainSlice(chain, chain.firstHeight()) {}

◆ ChainSlice() [2/3]

template<typename BlockIndexT >
altintegration::ChainSlice< BlockIndexT >::ChainSlice ( const chain_t chain,
height_t  firstHeight 
)
inlineexplicit

Definition at line 32 of file chain_slice.hpp.

33 : ChainSlice(chain,
34 firstHeight,
35 chain.firstHeight() + chain.blocksCount() - firstHeight) {}

◆ ChainSlice() [3/3]

template<typename BlockIndexT >
altintegration::ChainSlice< BlockIndexT >::ChainSlice ( const chain_t chain,
height_t  firstHeight,
size_t  size 
)
inlineexplicit

Definition at line 37 of file chain_slice.hpp.

38 : chain_(chain), firstHeight_(firstHeight), size_(size) {
39 VBK_ASSERT(firstHeight >= chain.firstHeight());
40 VBK_ASSERT(firstHeight + size <= chain.firstHeight() + chain.blocksCount());
41 }

Member Function Documentation

◆ begin()

template<typename BlockIndexT >
const_iterator_t altintegration::ChainSlice< BlockIndexT >::begin ( ) const
inline

Definition at line 85 of file chain_slice.hpp.

85{ return chain_.begin() + firstOffset(); }

◆ blocksCount()

template<typename BlockIndexT >
size_t altintegration::ChainSlice< BlockIndexT >::blocksCount ( ) const
inline

Definition at line 70 of file chain_slice.hpp.

70{ return size(); }

◆ chainHeight()

template<typename BlockIndexT >
height_t altintegration::ChainSlice< BlockIndexT >::chainHeight ( ) const
inline

Definition at line 64 of file chain_slice.hpp.

64{ return tip()->getHeight(); }

◆ contains()

template<typename BlockIndexT >
bool altintegration::ChainSlice< BlockIndexT >::contains ( const index_t *  index) const
inline

Definition at line 46 of file chain_slice.hpp.

46 {
47 return index != nullptr && this->operator[](index->getHeight()) == index;
48 }

◆ empty()

template<typename BlockIndexT >
bool altintegration::ChainSlice< BlockIndexT >::empty ( ) const
inline

Definition at line 66 of file chain_slice.hpp.

66{ return size() == 0; }

◆ end()

template<typename BlockIndexT >
const_iterator_t altintegration::ChainSlice< BlockIndexT >::end ( ) const
inline

Definition at line 86 of file chain_slice.hpp.

86{ return chain_.end() - tipOffset(); }

◆ first()

template<typename BlockIndexT >
index_t * altintegration::ChainSlice< BlockIndexT >::first ( ) const
inline

Definition at line 73 of file chain_slice.hpp.

73{ return empty() ? nullptr : chain_[firstHeight()]; }

◆ firstHeight()

template<typename BlockIndexT >
height_t altintegration::ChainSlice< BlockIndexT >::firstHeight ( ) const
inline

Definition at line 59 of file chain_slice.hpp.

59{ return firstHeight_; }

◆ firstOffset()

template<typename BlockIndexT >
size_t altintegration::ChainSlice< BlockIndexT >::firstOffset ( ) const
inline

Definition at line 75 of file chain_slice.hpp.

75{ return firstHeight() - chain_.firstHeight(); }

◆ getStartHeight()

template<typename BlockIndexT >
height_t altintegration::ChainSlice< BlockIndexT >::getStartHeight ( ) const
inline

Definition at line 44 of file chain_slice.hpp.

44{ return firstHeight(); }

◆ next()

template<typename BlockIndexT >
index_t * altintegration::ChainSlice< BlockIndexT >::next ( const index_t *  index) const
inline

Definition at line 55 of file chain_slice.hpp.

55 {
56 return !contains(index) ? nullptr : (*this)[index->getHeight() + 1];
57 }

◆ operator[]()

template<typename BlockIndexT >
index_t * altintegration::ChainSlice< BlockIndexT >::operator[] ( height_t  height) const
inline

Definition at line 50 of file chain_slice.hpp.

50 {
51 return height < firstHeight() || height > tipHeight() ? nullptr
52 : chain_[height];
53 }

◆ rbegin()

template<typename BlockIndexT >
const_reverse_iterator_t altintegration::ChainSlice< BlockIndexT >::rbegin ( ) const
inline

Definition at line 78 of file chain_slice.hpp.

78 {
79 return chain_.rbegin() + tipOffset();
80 }

◆ rend()

template<typename BlockIndexT >
const_reverse_iterator_t altintegration::ChainSlice< BlockIndexT >::rend ( ) const
inline

Definition at line 81 of file chain_slice.hpp.

81 {
82 return chain_.rend() - firstOffset();
83 }

◆ size()

template<typename BlockIndexT >
size_t altintegration::ChainSlice< BlockIndexT >::size ( ) const
inline

Definition at line 68 of file chain_slice.hpp.

68{ return size_; }

◆ tip()

template<typename BlockIndexT >
index_t * altintegration::ChainSlice< BlockIndexT >::tip ( ) const
inline

Definition at line 72 of file chain_slice.hpp.

72{ return empty() ? nullptr : chain_[tipHeight()]; }

◆ tipHeight()

template<typename BlockIndexT >
height_t altintegration::ChainSlice< BlockIndexT >::tipHeight ( ) const
inline

Definition at line 61 of file chain_slice.hpp.

61{ return (int)(firstHeight() + size()) - 1; }

◆ tipOffset()

template<typename BlockIndexT >
size_t altintegration::ChainSlice< BlockIndexT >::tipOffset ( ) const
inline

Definition at line 76 of file chain_slice.hpp.

76{ return chain_.chainHeight() - tipHeight(); }

Friends And Related Function Documentation

◆ operator!=

template<typename BlockIndexT >
bool operator!= ( const ChainSlice< BlockIndexT > &  a,
const ChainSlice< BlockIndexT > &  b 
)
friend

Definition at line 96 of file chain_slice.hpp.

96 {
97 return !(a == b);
98 }

◆ operator==

template<typename BlockIndexT >
bool operator== ( const ChainSlice< BlockIndexT > &  a,
const ChainSlice< BlockIndexT > &  b 
)
friend

Definition at line 88 of file chain_slice.hpp.

88 {
89 // one of the slices is empty, but the other is not
90 if (a.empty() != b.empty()) return false;
91 // both slices are empty
92 if (a.empty()) return true;
93 return a.tip()->getHash() == b.tip()->getHash();
94 }

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