version 3.8
enumerate.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3//
4// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
13#ifndef DUMUX_COMMON_ENUMERATE_HH
14#define DUMUX_COMMON_ENUMERATE_HH
15
16#include <tuple>
17
18namespace Dumux {
19
27template <typename Range,
28 typename RangeIterator = decltype(std::begin(std::declval<Range>())),
29 typename = decltype(std::end(std::declval<Range>()))>
30constexpr auto enumerate(Range&& iterable)
31{
32 struct Iterator
33 {
34 std::size_t i;
35 RangeIterator iter;
36 bool operator != (const Iterator & other) const { return iter != other.iter; }
37 void operator ++ () { ++i; ++iter; }
38 auto operator * () const { return std::tie(i, *iter); }
39 };
40
41 struct Iterable
42 {
43 Range iterable;
44 auto begin() { return Iterator{ 0, std::begin(iterable) }; }
45 auto end() { return Iterator{ 0, std::end(iterable) }; }
46 };
47
48 return Iterable{ std::forward<Range>(iterable) };
49}
50
51} // end namespace Dumux
52
53#endif
constexpr bool operator!=(Tag< T1 >, Tag< T2 >)
Definition: tag.hh:38
Definition: adapt.hh:17
constexpr auto enumerate(Range &&iterable)
A Python-like enumerate function.
Definition: enumerate.hh:30