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
20#pragma once
21
22#include <iterator>
23#include <type_traits>
24
25namespace Utils {
26namespace detail {
27template <class ContainerOrValue, class OutputIterator>
28OutputIterator flatten_impl(ContainerOrValue const &c, OutputIterator out) {
29 if constexpr (std::is_assignable_v<decltype(*out), ContainerOrValue>) {
30 *out = c;
31 return ++out;
32 } else {
33 using ValueType = typename ContainerOrValue::value_type;
34 for (auto const &e : c) {
35 out = flatten_impl<ValueType, OutputIterator>(e, out);
36 }
37 return out;
38 }
39}
40} // namespace detail
41
42/**
43 * @brief Flatten a range of ranges.
44 *
45 * Copy a range of ranges to an output range by subsequently
46 * copying the nested ranges to the output. Arbitrary deep
47 * nesting is supported, the elements are copied into the output
48 * in a depth-first fashion.
49 *
50 * @tparam Range A Forward Range
51 * @tparam OutputIterator An OutputIterator
52 * @param v Input Range
53 * @param out Output iterator
54 */
55template <class Range, class OutputIterator>
56void flatten(Range const &v, OutputIterator out) {
57 detail::flatten_impl<Range, OutputIterator>(v, out);
58}
59} // namespace Utils
void flatten(Range const &v, OutputIterator out)
Flatten a range of ranges.
Definition flatten.hpp:56