Bitcoin ABC 0.30.13
P2P Digital Currency
vector.h
Go to the documentation of this file.
1// Copyright (c) 2019 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_UTIL_VECTOR_H
6#define BITCOIN_UTIL_VECTOR_H
7
8#include <functional>
9#include <initializer_list>
10#include <optional>
11#include <type_traits>
12#include <vector>
13
21template <typename... Args>
22inline std::vector<typename std::common_type<Args...>::type>
23Vector(Args &&...args) {
24 std::vector<typename std::common_type<Args...>::type> ret;
25 ret.reserve(sizeof...(args));
26 // The line below uses the trick from
27 // https://www.experts-exchange.com/articles/32502/None-recursive-variadic-templates-with-std-initializer-list.html
28 (void)std::initializer_list<int>{
29 (ret.emplace_back(std::forward<Args>(args)), 0)...};
30 return ret;
31}
32
34template <typename V> inline V Cat(V v1, V &&v2) {
35 v1.reserve(v1.size() + v2.size());
36 for (auto &arg : v2) {
37 v1.push_back(std::move(arg));
38 }
39 return v1;
40}
41
43template <typename V> inline V Cat(V v1, const V &v2) {
44 v1.reserve(v1.size() + v2.size());
45 for (const auto &arg : v2) {
46 v1.push_back(arg);
47 }
48 return v1;
49}
50
51template <typename V, typename L>
52inline std::optional<V> FindFirst(const std::vector<V> &vec, const L fnc) {
53 for (const auto &el : vec) {
54 if (fnc(el)) {
55 return el;
56 }
57 }
58 return std::nullopt;
59}
60
61#endif // BITCOIN_UTIL_VECTOR_H
V Cat(V v1, V &&v2)
Concatenate two vectors, moving elements.
Definition: vector.h:34
std::optional< V > FindFirst(const std::vector< V > &vec, const L fnc)
Definition: vector.h:52
std::vector< typename std::common_type< Args... >::type > Vector(Args &&...args)
Construct a vector with the specified elements.
Definition: vector.h:23