Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
vvutils.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2026 by the Vulpes authors.
4// This file is part of Vulpes.
5//
6// Vulpes is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Vulpes is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Vulpes. If not, see <http://www.gnu.org/licenses/>.
18//
19// ----------------------------------------------------------------------
20
21// Author: Matteo Caldana <matteo.caldana@polimi.it>
22
23#ifndef VULPES_UTILS_VVUTILS_HPP_
24#define VULPES_UTILS_VVUTILS_HPP_
25
26#include <algorithm>
27#include <iterator>
28#include <ostream>
29#include <set>
30#include <string>
31#include <utility>
32#include <vector>
33
34namespace vulpes {
35
36namespace vvutils {
37namespace internal {
38// Trait: true for std::vector, std::set, and std::array; enables the
39// generic operator<< below for these container types.
40template <typename T>
41struct is_printable_container : std::false_type {};
42
43template <typename... Args>
44struct is_printable_container<std::vector<Args...>> : std::true_type {};
45
46template <typename... Args>
47struct is_printable_container<std::set<Args...>> : std::true_type {};
48
49template <typename T, std::size_t N>
50struct is_printable_container<std::array<T, N>> : std::true_type {};
51} // namespace internal
52} // namespace vvutils
53
66template <typename Container>
67std::ostream& print(
68 std::ostream& os,
69 const Container& container,
70 const std::string& separator = ",",
71 const std::string& postpend = "\n",
72 const std::string& prepend = "") {
73 auto it = container.begin();
74 if (!prepend.empty()) {
75 os << prepend;
76 }
77 if (it != container.end()) {
78 os << *it;
79 ++it;
80 for (; it != container.end(); ++it) {
81 os << separator << *it;
82 }
83 }
84 if (!postpend.empty()) {
85 os << postpend;
86 }
87 return os;
88}
89
97template <typename Container>
98typename std::enable_if<
99 vvutils::internal::is_printable_container<Container>::value,
100 std::ostream&>::type
101operator<<(std::ostream& os, const Container& container) {
102 return print(os, container);
103}
104
105namespace vutils {
116template <typename T, typename IndexType>
117std::vector<T> slice(
118 const std::vector<T>& source, const std::vector<IndexType>& indices) {
119 std::vector<T> result;
120 result.reserve(indices.size());
121 for (size_t index : indices) {
122 result.push_back(source[index]);
123 }
124 return result;
125}
126
127// Implementation helper for for_each(): applies func to every tuple
128// element.
129template <typename Tuple, typename Func, std::size_t... Is>
130void for_each_impl(Tuple&& tuple, Func&& func, std::index_sequence<Is...>) {
131 (func(std::get<Is>(tuple)), ...); // Fold expression (C++17)
132}
133
141template <typename Tuple, typename Func>
142void for_each(Tuple&& tuple, Func&& func) {
144 std::forward<Tuple>(tuple), std::forward<Func>(func),
145 std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>::value>{});
146}
147
148// Implementation helper for for_each_idxed(): applies func to every tuple
149// element together with its index.
150template <typename Tuple, typename Func, std::size_t... Is>
152 Tuple&& tuple, Func&& func, std::index_sequence<Is...>) {
153 (func(std::get<Is>(tuple), Is), ...); // Fold expression (C++17)
154}
155
164template <typename Tuple, typename Func>
165void for_each_idxed(Tuple&& tuple, Func&& func) {
167 std::forward<Tuple>(tuple), std::forward<Func>(func),
168 std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>::value>{});
169}
170
171} // namespace vutils
172} // namespace vulpes
173
174#endif // VULPES_UTILS_VVUTILS_HPP_
std::vector< T > slice(const std::vector< T > &source, const std::vector< IndexType > &indices)
Build a new vector by selecting elements of source at the given indices.
Definition vvutils.hpp:117
void for_each_impl(Tuple &&tuple, Func &&func, std::index_sequence< Is... >)
Definition vvutils.hpp:130
void for_each_impl_idxed(Tuple &&tuple, Func &&func, std::index_sequence< Is... >)
Definition vvutils.hpp:151
void for_each_idxed(Tuple &&tuple, Func &&func)
Apply func to every element of tuple together with its index.
Definition vvutils.hpp:165
void for_each(Tuple &&tuple, Func &&func)
Apply func to every element of tuple, in order.
Definition vvutils.hpp:142
Definition vvutils.hpp:37
Definition vvutils.hpp:36
Definition cell_manager.hpp:38
std::ostream & print(std::ostream &os, const Container &container, const std::string &separator=",", const std::string &postpend="\n", const std::string &prepend="")
Print a container's elements to a stream, separated and wrapped by the given strings.
Definition vvutils.hpp:67
std::enable_if< vvutils::internal::is_printable_container< Container >::value, std::ostream & >::type operator<<(std::ostream &os, const Container &container)
Stream a std::vector, std::set, or std::array via print().
Definition vvutils.hpp:101