CPP-TOOLBOX
Loading...
Searching...
No Matches
collection_utils.hpp
Go to the documentation of this file.
1#ifndef COLLECTION_UTILS_HPP
2#define COLLECTION_UTILS_HPP
3
4#include <functional>
5#include <vector>
6#include <algorithm>
7
8namespace collection_utils {
9
18template <typename T> std::vector<T> join_vectors(const std::vector<T> &v1, const std::vector<T> &v2) {
19 std::vector<T> result;
20 result.reserve(v1.size() + v2.size());
21 result.insert(result.end(), v1.begin(), v1.end());
22 result.insert(result.end(), v2.begin(), v2.end());
23 return result;
24}
25
34template <typename T, typename Func> void for_each_in_vector(std::vector<T> &vec, Func func) {
35 for (auto &elem : vec) {
36 func(elem);
37 }
38}
39
49template <typename Container> bool any_of(const Container &c) {
50 return std::any_of(c.begin(), c.end(), [](auto v) { return static_cast<bool>(v); });
51}
52
62template <typename Container> bool all_of(const Container &c) {
63 return std::all_of(c.begin(), c.end(), [](auto v) { return static_cast<bool>(v); });
64}
65
74template <typename T, typename Func> void for_each_in_vector(const std::vector<T> &vec, Func func) {
75 for (const auto &elem : vec) {
76 func(elem);
77 }
78}
79
87template <typename T> std::vector<T> join_all_vectors(const std::vector<std::vector<T>> &vectors) {
88
89 size_t total_size = 0;
90 for (const auto &v : vectors) {
91 total_size += v.size();
92 }
93
94 std::vector<T> result;
95 result.reserve(total_size);
96
97 for (const auto &v : vectors) {
98 result.insert(result.end(), v.begin(), v.end());
99 }
100
101 return result;
102}
103
113template <typename T, typename Func> auto map_vector(const std::vector<T> &vec, Func func) {
114 using U = decltype(func(std::declval<const T &>()));
115 std::vector<U> result;
116 result.reserve(vec.size());
117 for (const auto &elem : vec) {
118 result.push_back(func(elem));
119 }
120 return result;
121}
122
123}; // namespace collection_utils
124
125#endif // COLLECTION_UTILS_HPP
@ c
Definition input_state.hpp:29
@ U
Definition input_state.hpp:74
@ v
Definition input_state.hpp:48
Definition collection_utils.hpp:8
bool any_of(const Container &c)
Check if any element in the container evaluates to true.
Definition collection_utils.hpp:49
bool all_of(const Container &c)
Check if all elements in the container evaluate to true.
Definition collection_utils.hpp:62
auto map_vector(const std::vector< T > &vec, Func func)
Transform a vector by applying a function to each element.
Definition collection_utils.hpp:113
void for_each_in_vector(std::vector< T > &vec, Func func)
Apply a function to each element of a modifiable vector.
Definition collection_utils.hpp:34
std::vector< T > join_all_vectors(const std::vector< std::vector< T > > &vectors)
Concatenate a list of vectors into a single vector.
Definition collection_utils.hpp:87
std::vector< T > join_vectors(const std::vector< T > &v1, const std::vector< T > &v2)
Concatenate two vectors into a single vector.
Definition collection_utils.hpp:18