IOSS 2.0
Loading...
Searching...
No Matches
Ioss_Enumerate.h
Go to the documentation of this file.
1// https://www.reedbeta.com/blog/python-like-enumerate-in-cpp17/
2// std::vector<Thing> things;
3// ...
4// for (auto [i, thing] : enumerate(things))
5// {
6// .. `i` gets the index and `thing` gets the Thing in each iteration
7// }
8
9#include <tuple>
10
11namespace Ioss {
12 template <typename T, typename TIter = decltype(std::begin(std::declval<T>())),
13 typename = decltype(std::end(std::declval<T>()))>
14 constexpr auto enumerate(T &&iterable)
15 {
16 struct iterator
17 {
18 size_t i;
19 TIter iter;
20 bool operator!=(const iterator &other) const { return iter != other.iter; }
21 void operator++()
22 {
23 ++i;
24 ++iter;
25 }
26 auto operator*() const { return std::tie(i, *iter); }
27 };
28 struct iterable_wrapper
29 {
30 T iterable;
31 auto begin() { return iterator{0, std::begin(iterable)}; }
32 auto end() { return iterator{0, std::end(iterable)}; }
33 };
34 return iterable_wrapper{std::forward<T>(iterable)};
35 }
36} // namespace Ioss
The main namespace for the Ioss library.
Definition Ioad_DatabaseIO.C:40
constexpr auto enumerate(T &&iterable)
Definition Ioss_Enumerate.h:14
vector3d operator*(const vector3d &lhs, double scalar)
Definition vector3d.C:69