Line | Branch | Exec | Source |
---|---|---|---|
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 | // | ||
7 | /*! | ||
8 | * \file | ||
9 | * \ingroup Core | ||
10 | * \brief A Python-like enumerate function | ||
11 | */ | ||
12 | |||
13 | #ifndef DUMUX_COMMON_ENUMERATE_HH | ||
14 | #define DUMUX_COMMON_ENUMERATE_HH | ||
15 | |||
16 | #include <tuple> | ||
17 | |||
18 | namespace Dumux { | ||
19 | |||
20 | /*! | ||
21 | * \brief A Python-like enumerate function | ||
22 | * \param iterable Some iterable type with begin/end (e.g. std::vector) | ||
23 | * \note From Nathan Reed (CC BY 4.0): http://reedbeta.com/blog/python-like-enumerate-in-cpp17/ | ||
24 | * | ||
25 | * Usage example: for (const auto& [i, item] : enumerate(list)) | ||
26 | */ | ||
27 | template <typename Range, | ||
28 | typename RangeIterator = decltype(std::begin(std::declval<Range>())), | ||
29 | typename = decltype(std::end(std::declval<Range>()))> | ||
30 | constexpr auto enumerate(Range&& iterable) | ||
31 | { | ||
32 | struct Iterator | ||
33 | { | ||
34 | std::size_t i; | ||
35 | RangeIterator iter; | ||
36 |
20/20✓ Branch 0 taken 31950694 times.
✓ Branch 1 taken 12939282 times.
✓ Branch 2 taken 31950694 times.
✓ Branch 3 taken 12939282 times.
✓ Branch 4 taken 8280803 times.
✓ Branch 5 taken 815961 times.
✓ Branch 6 taken 8280803 times.
✓ Branch 7 taken 815961 times.
✓ Branch 8 taken 4651288 times.
✓ Branch 9 taken 431017 times.
✓ Branch 10 taken 4651288 times.
✓ Branch 11 taken 431017 times.
✓ Branch 12 taken 37638 times.
✓ Branch 13 taken 4362 times.
✓ Branch 14 taken 37638 times.
✓ Branch 15 taken 4362 times.
✓ Branch 16 taken 18283 times.
✓ Branch 17 taken 2014 times.
✓ Branch 18 taken 18283 times.
✓ Branch 19 taken 2014 times.
|
118262684 | bool operator != (const Iterator & other) const { return iter != other.iter; } |
37 | 89877412 | void operator ++ () { ++i; ++iter; } | |
38 |
12/16✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
✓ Branch 9 taken 37638 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 3594 times.
✓ Branch 12 taken 34044 times.
✓ Branch 13 taken 3594 times.
✓ Branch 14 taken 34044 times.
✓ Branch 16 taken 18283 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 3594 times.
✓ Branch 19 taken 14689 times.
✓ Branch 20 taken 3594 times.
✓ Branch 21 taken 14689 times.
|
76889400 | auto operator * () const { return std::tie(i, *iter); } |
39 | }; | ||
40 | |||
41 | struct Iterable | ||
42 | { | ||
43 | Range iterable; | ||
44 | 28385272 | auto begin() { return Iterator{ 0, std::begin(iterable) }; } | |
45 | 28385272 | auto end() { return Iterator{ 0, std::end(iterable) }; } | |
46 | }; | ||
47 | |||
48 | 14192636 | return Iterable{ std::forward<Range>(iterable) }; | |
49 | } | ||
50 | |||
51 | } // end namespace Dumux | ||
52 | |||
53 | #endif | ||
54 |