Skip to content

Commit 623aa49

Browse files
committed
combine enumerate() and const_enumerate()
1 parent b23cf48 commit 623aa49

File tree

5 files changed

+54
-15
lines changed

5 files changed

+54
-15
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ option(INSTALL_ONLY "Enable for installation only" OFF)
99
# Note: update this to your new project's name and version
1010
project(
1111
Py2Cpp
12-
VERSION 1.5
12+
VERSION 1.5.1
1313
LANGUAGES CXX
1414
)
1515

include/py2cpp/dict.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,24 @@ namespace py {
8080
return this->at(key);
8181
}
8282

83+
/**
84+
* @brief
85+
*
86+
* @return auto
87+
*/
88+
auto begin() -> key_iterator<decltype(Base::begin())> {
89+
return key_iterator<decltype(Base::begin())>{Base::begin()};
90+
}
91+
92+
/**
93+
* @brief
94+
*
95+
* @return auto
96+
*/
97+
auto end() -> key_iterator<decltype(Base::end())> {
98+
return key_iterator<decltype(Base::end())>{Base::end()};
99+
}
100+
83101
/**
84102
* @brief
85103
*

include/py2cpp/enumerate.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ namespace py {
139139
}
140140

141141
/**
142-
* @brief const_enumerate(const T &iterable)
142+
* @brief enumerate(const T &iterable)
143143
*
144144
* The `const_enumerate(const T &iterable)` function is a utility function that
145145
* allows you to iterate over a constant container or range and also get the
@@ -152,9 +152,16 @@ namespace py {
152152
* @param[in] iterable
153153
* @return detail::EnumerateIterableWrapper<const T>
154154
*/
155-
template <typename T> inline auto const_enumerate(const T &iterable)
155+
template <typename T> inline auto enumerate(const T &iterable)
156156
-> detail::EnumerateIterableWrapper<const T> {
157157
return detail::EnumerateIterableWrapper<const T>{iterable};
158158
}
159159

160+
/**
161+
* @brief (deprecated) const_enumerate(const T &iterable)
162+
*/
163+
template <typename T> inline auto const_enumerate(const T &iterable)
164+
-> detail::EnumerateIterableWrapper<const T> {
165+
return detail::EnumerateIterableWrapper<const T>{iterable};
166+
}
160167
} // namespace py

include/py2cpp/range.hpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ namespace py {
1818
* @tparam T
1919
*/
2020
template <typename T> struct RangeIterator {
21-
using iterator_category = std::output_iterator_tag;
21+
using iterator_category = std::input_iterator_tag;
2222
using difference_type = std::ptrdiff_t;
2323
using value_type = T;
24-
using pointer = T *; // or also value_type*
25-
using reference = T &; // or also value_type&
24+
using pointer = const T *; // or also value_type*
25+
using reference = const T &; // or also value_type&
2626
using const_reference = const T &; // or also value_type&
2727
using key_type = T; // luk:
2828

@@ -67,15 +67,6 @@ namespace py {
6767
*/
6868
CONSTEXPR14 auto operator*() const -> const_reference { return this->i; }
6969

70-
/**
71-
* The `operator*()` function is used to dereference the iterator and return the value it
72-
* points to.
73-
*
74-
* @return The `operator*()` function is returning a reference to the value that the
75-
* iterator points to.
76-
*/
77-
CONSTEXPR14 auto operator*() -> reference { return this->i; }
78-
7970
/**
8071
* @brief
8172
*

test/source/test_enumerate.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <py2cpp/enumerate.hpp> // for enumerate, iterable_wrapper
44
#include <py2cpp/range.hpp> // for range, iterable_wrapper
55
#include <utility> // for pair
6+
#include <vector>
67

78
TEST_CASE("Test enumerate") {
89
auto R = py::range(10);
@@ -14,3 +15,25 @@ TEST_CASE("Test enumerate") {
1415
}
1516
CHECK(count == R.size());
1617
}
18+
19+
TEST_CASE("Test enumerate with const vector") {
20+
const std::vector<int> V = {10, 20, 30, 40, 50};
21+
size_t count = 0;
22+
for (const auto &p : py::enumerate(V)) {
23+
CHECK(p.first == count);
24+
CHECK(p.second == V[count]);
25+
++count;
26+
}
27+
CHECK(count == V.size());
28+
}
29+
30+
TEST_CASE("Test enumerate with const vector") {
31+
const std::vector<int> V = {10, 20, 30, 40, 50};
32+
size_t count = 0;
33+
for (const auto &p : py::const_enumerate(V)) {
34+
CHECK(p.first == count);
35+
CHECK(p.second == V[count]);
36+
++count;
37+
}
38+
CHECK(count == V.size());
39+
}

0 commit comments

Comments
 (0)