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 for (auto const &e : c) {
31 out = flatten_impl<decltype(e), OutputIterator>::apply(e, out);
32 }
33
34 return out;
35 }
36};
37
38template <class T, class OutputIterator>
39struct flatten_impl<T, OutputIterator,
40 std::enable_if_t<std::is_assignable_v<
41 decltype(*std::declval<OutputIterator>()), T>>> {
42 static OutputIterator apply(T const &v, OutputIterator out) {
43 *out = v;
44 return ++out;
45 }
46};
47} // namespace detail
48
49/**
50 * @brief Flatten a range of ranges.
51 *
52 * Copy a range of ranges to an output range by subsequently
53 * copying the nested ranges to the output. Arbitrary deep
54 * nesting is supported, the elements are copied into the output
55 * in a depth-first fashion.
56 *
57 * @tparam Range A Forward Range
58 * @tparam OutputIterator An OutputIterator
59 * @param v Input Range
60 * @param out Output iterator
61 */
62template <class Range, class OutputIterator>
63void flatten(Range const &v, OutputIterator out) {
64 detail::flatten_impl<Range, OutputIterator>::apply(v, out);
65}
66} // namespace Utils
67
68#endif // ESPRESSO_FLATTEN_HPP
void flatten(Range const &v, OutputIterator out)
Flatten a range of ranges.
Definition flatten.hpp:63