Py2Cpp 1.6.3; VERSION ${PROJECT_VERSION}
Loading...
Searching...
No Matches
enumerate.hpp
Go to the documentation of this file.
1
9#pragma once
10
11#include <cstddef>
12#include <iterator>
13#include <utility>
14
15namespace py {
16
17 namespace detail {
18
27 template <typename T> struct EnumerateIterator {
28 using TIter = decltype(std::begin(std::declval<T>()));
29 using iter_ref = typename std::iterator_traits<TIter>::reference;
30
31 size_t i;
33
42 auto operator!=(const EnumerateIterator& other) const -> bool {
43 return iter != other.iter;
44 }
45
54 ++i;
55 ++iter;
56 return *this;
57 }
58
66 auto operator*() -> std::pair<size_t, iter_ref> {
67 return std::pair<size_t, iter_ref>{i, *iter};
68 }
69 };
70
79 template <typename T> struct EnumerateIterableWrapper {
81
89 auto begin() const -> EnumerateIterator<T> {
90 return EnumerateIterator<T>{0, std::begin(iterable)};
91 }
92
100 auto end() const -> EnumerateIterator<T> {
101 return EnumerateIterator<T>{0, std::end(iterable)};
102 }
103 };
104
105 } // namespace detail
106
117 template <typename T> inline auto enumerate(T& iterable)
120 }
121
132 template <typename T> inline auto enumerate(const T& iterable)
135 }
136
146 template <typename T> inline auto const_enumerate(const T& iterable)
149 }
150} // namespace py
Python-like utilities and data structures for C++.
Definition dict.hpp:18
auto const_enumerate(const T &iterable) -> detail::EnumerateIterableWrapper< const T >
(deprecated) Create an enumerable wrapper for a const container
Definition enumerate.hpp:146
auto enumerate(T &iterable) -> detail::EnumerateIterableWrapper< T >
Create an enumerable wrapper for a container.
Definition enumerate.hpp:117
Wrapper for making containers enumerable.
Definition enumerate.hpp:79
auto begin() const -> EnumerateIterator< T >
Get iterator to the beginning.
Definition enumerate.hpp:89
auto end() const -> EnumerateIterator< T >
Get iterator to the end.
Definition enumerate.hpp:100
T & iterable
Definition enumerate.hpp:80
Iterator for enumerated access to container elements.
Definition enumerate.hpp:27
auto operator!=(const EnumerateIterator &other) const -> bool
Check inequality between iterators.
Definition enumerate.hpp:42
decltype(std::begin(std::declval< T >())) TIter
Definition enumerate.hpp:28
auto operator*() -> std::pair< size_t, iter_ref >
Dereference operator.
Definition enumerate.hpp:66
EnumerateIterator & operator++()
Pre-increment operator.
Definition enumerate.hpp:53
size_t i
Definition enumerate.hpp:31
TIter iter
Definition enumerate.hpp:32
typename std::iterator_traits< TIter >::reference iter_ref
Definition enumerate.hpp:29