6#ifndef VERIBLOCK_POP_CPP_ALGORITHM_HPP
7#define VERIBLOCK_POP_CPP_ALGORITHM_HPP
13#include <unordered_set>
15#include <veriblock/pop/assert.hpp>
16#include <veriblock/pop/blob.hpp>
21template <
typename A,
typename B>
22std::vector<B> map_vector(
const std::vector<A>& a,
23 std::function<B(
const A&)> f) {
26 std::transform(a.begin(), a.end(), std::back_inserter(b), f);
32typename T::id_t get_id(
const T& 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());
46std::vector<uint8_t> getIdVector(
const Blob<N>& t) {
47 std::vector<uint8_t> v(t.begin(), t.end());
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>);
58template <
typename A,
typename B>
59std::vector<A> map_get_id_from_pointers(
const std::vector<B*>& b) {
62 std::transform(b.begin(), b.end(), std::back_inserter(a), [&](B* t) -> A {
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());
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;
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());
97bool erase_last_item_if(std::vector<T>& v,
98 const std::function<
bool(
const T&)>& locator) {
100 size_t last = v.size();
101 for (
size_t i = v.size(); i-- > 0;) {
109 if (last >= v.size()) {
114 auto it = v.begin() + last;
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);
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);
139const T& as_const(
const T& t) {
145T& as_mut(
const T& t) {
146 return const_cast<T&
>(t);
151template <
typename T,
typename Container>
153 const auto it = std::min_element(c.begin(), c.end());
154 return it == c.end() ? default_ : *it;
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