6#ifndef ALT_INTEGRATION_INCLUDE_VERIBLOCK_BLOCKCHAIN_CHAIN_SLICE_HPP_
7#define ALT_INTEGRATION_INCLUDE_VERIBLOCK_BLOCKCHAIN_CHAIN_SLICE_HPP_
9#include <veriblock/pop/blockchain/block_index.hpp>
19template <
typename BlockIndexT>
21 using index_t = BlockIndexT;
22 using block_t =
typename index_t::block_t;
23 using hash_t =
typename index_t::hash_t;
24 using height_t =
typename block_t::height_t;
27 using const_iterator_t =
typename chain_t::const_iterator_t;
28 using const_reverse_iterator_t =
typename chain_t::const_reverse_iterator_t;
35 chain.firstHeight() + chain.blocksCount() - firstHeight) {}
38 : chain_(chain), firstHeight_(firstHeight), size_(size) {
39 VBK_ASSERT(firstHeight >= chain.firstHeight());
40 VBK_ASSERT(firstHeight + size <= chain.firstHeight() + chain.blocksCount());
44 height_t getStartHeight()
const {
return firstHeight(); }
46 bool contains(
const index_t* index)
const {
47 return index !=
nullptr && this->operator[](index->getHeight()) == index;
50 index_t* operator[](height_t height)
const {
51 return height < firstHeight() || height > tipHeight() ? nullptr
55 index_t* next(
const index_t* index)
const {
56 return !contains(index) ? nullptr : (*this)[index->getHeight() + 1];
59 height_t firstHeight()
const {
return firstHeight_; }
61 height_t tipHeight()
const {
return (
int)(firstHeight() + size()) - 1; }
64 height_t chainHeight()
const {
return tip()->getHeight(); }
66 bool empty()
const {
return size() == 0; }
68 size_t size()
const {
return size_; }
70 size_t blocksCount()
const {
return size(); }
72 index_t* tip()
const {
return empty() ? nullptr : chain_[tipHeight()]; }
73 index_t* first()
const {
return empty() ? nullptr : chain_[firstHeight()]; }
75 size_t firstOffset()
const {
return firstHeight() - chain_.firstHeight(); }
76 size_t tipOffset()
const {
return chain_.chainHeight() - tipHeight(); }
78 const_reverse_iterator_t rbegin()
const {
79 return chain_.rbegin() + tipOffset();
81 const_reverse_iterator_t rend()
const {
82 return chain_.rend() - firstOffset();
85 const_iterator_t begin()
const {
return chain_.begin() + firstOffset(); }
86 const_iterator_t end()
const {
return chain_.end() - tipOffset(); }
90 if (a.empty() != b.empty())
return false;
92 if (a.empty())
return true;
93 return a.tip()->getHash() == b.tip()->getHash();
102 const height_t firstHeight_;
Non-owning partial chain view similar to std::span.
Fully in-memory chain representation.