1#ifndef COLLECTION_UTILS_HPP
2#define COLLECTION_UTILS_HPP
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());
35 for (
auto &elem : vec) {
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); });
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); });
74template <
typename T,
typename Func>
void for_each_in_vector(
const std::vector<T> &vec, Func func) {
75 for (
const auto &elem : vec) {
87template <
typename T> std::vector<T>
join_all_vectors(
const std::vector<std::vector<T>> &vectors) {
89 size_t total_size = 0;
90 for (
const auto &
v : vectors) {
91 total_size +=
v.size();
94 std::vector<T> result;
95 result.reserve(total_size);
97 for (
const auto &
v : vectors) {
98 result.insert(result.end(),
v.begin(),
v.end());
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));
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