veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
slice.hpp
1// Copyright (c) 2019-2022 Xenios SEZC
2// https://www.veriblock.org
3// Distributed under the MIT software license, see the accompanying
4// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef ALT_INTEGRATION_VERIBLOCK_SLICE_HPP
7#define ALT_INTEGRATION_VERIBLOCK_SLICE_HPP
8
9#include <algorithm>
10#include <cstddef>
11#include <iterator>
12#include <ostream>
13#include <type_traits>
14#include <vector>
15
16namespace altintegration {
17
21template <class ElementType>
22struct Slice {
23 using element_type = ElementType;
24 using value_type = typename std::remove_cv<ElementType>::type;
25 using index_type = std::ptrdiff_t;
26 using pointer = element_type *;
27 using reference = element_type &;
28
29 using iterator = pointer;
30 using const_iterator = const pointer;
31 using reverse_iterator = std::reverse_iterator<iterator>;
32 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
33
34 Slice() : storage_(nullptr), size_(0) {}
35
36 Slice(const pointer ptr, size_t size) : storage_(ptr), size_(size) {}
37
38 // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the
39 // requirement on Container to be a contiguous sequence container.
40 template <
41 class Container,
42 typename = typename std::enable_if<
43 sizeof(typename Container::value_type) == 1 ||
44 std::is_same<typename std::remove_const<ElementType>::type,
45 typename std::remove_const<
46 typename Container::value_type>::type>::value>::type>
47 constexpr Slice(Container &cont) noexcept
48 : Slice(reinterpret_cast<const pointer>(cont.data()), cont.size()) {}
49
50 constexpr pointer data() const noexcept { return storage_; }
51
52 constexpr size_t size() const noexcept { return size_; }
53
54 constexpr reference operator[](index_type idx) const { return data()[idx]; }
55
56 constexpr std::vector<value_type> asVector() const {
57 return std::vector<value_type>{storage_, storage_ + size_};
58 }
59
60 constexpr iterator begin() const noexcept { return storage_; }
61 constexpr iterator end() const noexcept { return storage_ + size_; }
62 constexpr iterator cbegin() const noexcept { return storage_; }
63 constexpr iterator cend() const noexcept { return storage_ + size_; }
64 constexpr reverse_iterator rbegin() const noexcept {
65 return reverse_iterator{end()};
66 }
67 constexpr reverse_iterator rend() const noexcept {
68 return reverse_iterator{begin()};
69 }
70 constexpr const_reverse_iterator crbegin() const noexcept {
71 return const_reverse_iterator{cend()};
72 }
73 constexpr const_reverse_iterator crend() const noexcept {
74 return const_reverse_iterator{cbegin()};
75 }
76
77 std::vector<value_type> reverse() const {
78 return std::vector<value_type>(rbegin(), rend());
79 }
80
81 private:
82 pointer storage_;
83 size_t size_;
84};
85
88template <typename T>
89inline void PrintTo(const Slice<T> &slice, ::std::ostream *os) {
90 *os << "Slice(" << slice.data() << ", " << slice.size() << ")";
91}
92
93} // namespace altintegration
94
95#endif //__SLICE__HPP__
Defines logging helpers.
Definition: block.hpp:14
void PrintTo(const ArithUint256 &uint, ::std::ostream *os)
custom gtest printer, which prints Blob of any size as hexstring @ private
Non-owning contiguous array.
Definition: slice.hpp:22