ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
flatten.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo 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 * ESPResSo 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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19#ifndef ESPRESSO_UTILS_FLATTEN_HPP
20#define ESPRESSO_UTILS_FLATTEN_HPP
21
22#include <iterator>
23#include <type_traits>
24
25namespace Utils {
26namespace detail {
27template <class Container, class OutputIterator, class = void>
28struct flatten_impl {
29 static OutputIterator apply(Container const &c, OutputIterator out) {
30 using ValueType = typename Container::value_type;
31 for (auto const &e : c) {
32 out = flatten_impl<ValueType, OutputIterator>::apply(e, out);
33 }
34
35 return out;
36 }
37};
38
39template <class T, class OutputIterator>
40struct flatten_impl<T, OutputIterator,
41 std::enable_if_t<std::is_assignable_v<
42 decltype(*std::declval<OutputIterator>()), T>>> {
43 static OutputIterator apply(T const &v, OutputIterator out) {
44 *out = v;
45 return ++out;
46 }
47};
48} // namespace detail
49
50/**
51 * @brief Flatten a range of ranges.
52 *
53 * Copy a range of ranges to an output range by subsequently
54 * copying the nested ranges to the output. Arbitrary deep
55 * nesting is supported, the elements are copied into the output
56 * in a depth-first fashion.
57 *
58 * @tparam Range A Forward Range
59 * @tparam OutputIterator An OutputIterator
60 * @param v Input Range
61 * @param out Output iterator
62 */
63template <class Range, class OutputIterator>
64void flatten(Range const &v, OutputIterator out) {
65 detail::flatten_impl<Range, OutputIterator>::apply(v, out);
66}
67} // namespace Utils
68
69#endif // ESPRESSO_FLATTEN_HPP
void flatten(Range const &v, OutputIterator out)
Flatten a range of ranges.
Definition flatten.hpp:64