version 3.11-dev
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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-FileCopyrightText: 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#include <iterator>
18
19namespace Dumux {
20
28template <typename Range,
29 typename RangeIterator = decltype(std::begin(std::declval<Range>())),
30 typename = decltype(std::end(std::declval<Range>()))>
31constexpr auto enumerate(Range&& iterable)
32{
33 struct Iterator
34 {
35 std::size_t i;
36 RangeIterator iter;
37 bool operator != (const Iterator & other) const { return iter != other.iter; }
38 void operator ++ () { ++i; ++iter; }
39 auto operator * () const { return std::tie(i, *iter); }
40 };
41
42 struct Iterable
43 {
44 Range iterable;
45 auto begin() { return Iterator{ 0, std::begin(iterable) }; }
46 auto end() { return Iterator{ 0, std::end(iterable) }; }
47 };
48
49 return Iterable{ std::forward<Range>(iterable) };
50}
51
52} // end namespace Dumux
53
54#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:31