veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
algorithm.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 VERIBLOCK_POP_CPP_ALGORITHM_HPP
7#define VERIBLOCK_POP_CPP_ALGORITHM_HPP
8
9#include <algorithm>
10#include <functional>
11#include <iterator>
12#include <set>
13#include <unordered_set>
14#include <vector>
15#include <veriblock/pop/assert.hpp>
16#include <veriblock/pop/blob.hpp>
17
18namespace altintegration {
19
21template <typename A, typename B>
22std::vector<B> map_vector(const std::vector<A>& a,
23 std::function<B(const A&)> f) {
24 std::vector<B> b;
25 b.reserve(a.size());
26 std::transform(a.begin(), a.end(), std::back_inserter(b), f);
27 return b;
28}
29
31template <typename T>
32typename T::id_t get_id(const T& t) {
33 return t.getId();
34}
35
37template <typename T>
38std::vector<uint8_t> getIdVector(const T& t) {
39 auto id = get_id<T>(t);
40 std::vector<uint8_t> v(id.begin(), id.end());
41 return v;
42}
43
45template <size_t N>
46std::vector<uint8_t> getIdVector(const Blob<N>& t) {
47 std::vector<uint8_t> v(t.begin(), t.end());
48 return v;
49}
50
52template <typename P>
53std::vector<typename P::id_t> map_get_id(const std::vector<P>& a) {
54 return map_vector<P, typename P::id_t>(a, get_id<P>);
55}
56
58template <typename A, typename B>
59std::vector<A> map_get_id_from_pointers(const std::vector<B*>& b) {
60 std::vector<A> a;
61 a.reserve(b.size());
62 std::transform(b.begin(), b.end(), std::back_inserter(a), [&](B* t) -> A {
63 return t->getId();
64 });
65 return a;
66}
67
69template <typename T>
70bool same_vectors_unique_unordered(const std::vector<T>& a,
71 const std::vector<T>& b) {
72 const auto setA = std::set<T>(a.begin(), a.end());
73 const auto setB = std::set<T>(b.begin(), b.end());
74 return setA == setB;
75}
76
78template <typename T>
79bool same_vectors_unordered(const std::vector<T>& a, const std::vector<T>& b) {
80 std::vector<T> sortedVectorA = a;
81 std::vector<T> sortedVectorB = b;
82 std::sort(sortedVectorA.begin(), sortedVectorA.end());
83 std::sort(sortedVectorB.begin(), sortedVectorB.end());
84 return sortedVectorA == sortedVectorB;
85}
86
88template <typename T>
89std::set<typename T::id_t> make_idset(const std::vector<T>& v) {
90 auto ids = map_get_id(v);
91 std::set<typename T::id_t> s(ids.begin(), ids.end());
92 return s;
93}
94
96template <typename T>
97bool erase_last_item_if(std::vector<T>& v,
98 const std::function<bool(const T&)>& locator) {
99 // find and erase the last occurrence of item
100 size_t last = v.size();
101 for (size_t i = v.size(); i-- > 0;) {
102 const T& el = v[i];
103 if (locator(el)) {
104 last = i;
105 break;
106 }
107 }
108
109 if (last >= v.size()) {
110 // not found
111 return false;
112 }
113
114 auto it = v.begin() + last;
115 v.erase(it);
116
117 return true;
118}
119
122template <typename container_t, typename val_t>
123void erase_if(container_t& c, std::function<bool(const val_t&)> pred) {
124 for (auto it = c.begin(); it != c.end();) {
125 it = pred(*it) ? c.erase(it) : std::next(it);
126 }
127}
128
131template <typename T, typename... Args>
132std::unique_ptr<T> make_unique(Args&&... args) {
133 auto* ptr = new T(std::forward<Args>(args)...);
134 return std::unique_ptr<T>(ptr);
135}
136
138template <typename T>
139const T& as_const(const T& t) {
140 return t;
141}
142
144template <typename T>
145T& as_mut(const T& t) {
146 return const_cast<T&>(t);
147}
148
151template <typename T, typename Container>
152const T min_or_default(Container& c, const T default_) {
153 const auto it = std::min_element(c.begin(), c.end());
154 return it == c.end() ? default_ : *it;
155}
156
157} // namespace altintegration
158
159#endif // VERIBLOCK_POP_CPP_ALGORITHM_HPP
Defines logging helpers.
Definition: block.hpp:14
const T min_or_default(Container &c, const T default_)
returns min value in a container (vector, array...), or "default" value if container is empty
Definition: algorithm.hpp:152