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