Skip to content

Commit

Permalink
fix for py::enumerate
Browse files Browse the repository at this point in the history
  • Loading branch information
luk036 committed Mar 17, 2023
1 parent ff75d6e commit f61a9c2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ option(INSTALL_ONLY "Enable for installation only" OFF)
# Note: update this to your new project's name and version
project(
Py2Cpp
VERSION 1.4.2
VERSION 1.4.3
LANGUAGES CXX
)

Expand Down
4 changes: 2 additions & 2 deletions include/py2cpp/enumerate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ template <typename T> struct EnumerateIterator {
return *this;
}

auto operator*() const -> std::pair<size_t, iter_ref> {
return std::make_pair(i, *iter);
auto operator*() -> std::pair<size_t, iter_ref> {
return std::pair<size_t, iter_ref>{i, *iter};
}
};

Expand Down
22 changes: 14 additions & 8 deletions include/py2cpp/range.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cstddef>
#include <iterator>
#include <type_traits>

#if __cpp_constexpr >= 201304
Expand All @@ -14,8 +15,13 @@ namespace py {
namespace detail {

template <typename T> struct RangeIterator {
using value_type = T; // luk:
using key_type = T; // luk:
using iterator_category = std::output_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = T *; // or also value_type*
using reference = T &; // or also value_type&
using const_reference = const T &; // or also value_type&
using key_type = T; // luk:

T i;
constexpr auto operator!=(const RangeIterator &other) const -> bool {
Expand All @@ -24,24 +30,24 @@ template <typename T> struct RangeIterator {
constexpr auto operator==(const RangeIterator &other) const -> bool {
return this->i == other.i;
}
CONSTEXPR14 auto operator*() const -> const T & { return this->i; }
CONSTEXPR14 auto operator*() -> T & { return this->i; }
CONSTEXPR14 auto operator*() const -> const_reference { return this->i; }
CONSTEXPR14 auto operator*() -> reference { return this->i; }
CONSTEXPR14 auto operator++() -> RangeIterator & {
++this->i;
return *this;
}
CONSTEXPR14 auto operator++(int) -> RangeIterator {
auto temp = *this;
++*this;
++(*this);
return temp;
}
};

template <typename T> struct RangeIterableWrapper {
public:
using value_type = T; // luk:
using key_type = T; // luk:
using iterator = RangeIterator<T>; // luk
using iterator = RangeIterator<T>;
using value_type = T;
using key_type = T;

// static_assert(sizeof(value_type) >= 0, "make compiler happy");
// static_assert(sizeof(key_type) >= 0, "make compiler happy");
Expand Down
28 changes: 14 additions & 14 deletions test/source/test_enumerate.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include <doctest/doctest.h> // for ResultBuilder, CHECK, TestCase, TEST...

// #include <py2cpp/enumerate.hpp> // for enumerate, iterable_wrapper
// #include <py2cpp/range.hpp> // for range, iterable_wrapper
// #include <utility> // for pair
//
// TEST_CASE("Test enumerate") {
// const auto R = py::range(10);
// auto count = 0;
// for (const auto& p : py::enumerate(R)) {
// static_assert(sizeof p.first >= 0, "make comipler happy");
// CHECK(p.first == count);
// ++count;
// }
// CHECK(count == R.size());
// }
#include <py2cpp/enumerate.hpp> // for enumerate, iterable_wrapper
#include <py2cpp/range.hpp> // for range, iterable_wrapper
#include <utility> // for pair

TEST_CASE("Test enumerate") {
auto R = py::range(10);
auto count = 0;
for (const auto &p : py::enumerate(R)) {
static_assert(sizeof p.first >= 0, "make comipler happy");
CHECK(p.first == count);
++count;
}
CHECK(count == R.size());
}

0 comments on commit f61a9c2

Please sign in to comment.