Skip to content

Commit

Permalink
forward_iteratorに対応
Browse files Browse the repository at this point in the history
  • Loading branch information
acd1034 committed Sep 18, 2022
1 parent 69c53e5 commit 302027f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
20 changes: 19 additions & 1 deletion include/ns/enumerate_view.hpp
Expand Up @@ -42,8 +42,15 @@ namespace ns {
public:
using difference_type = std::ranges::range_difference_t<View>;
using value_type = std::pair<std::size_t, std::ranges::range_value_t<View>>;
using iterator_concept = std::input_iterator_tag;
// clang-format off
using iterator_concept =
std::conditional_t<std::ranges::forward_range<View>, std::forward_iterator_tag,
/* else */ std::input_iterator_tag>;
// clang-format on

iterator() requires
std::default_initializable<std::ranges::iterator_t<View>>
= default;
constexpr iterator(std::ranges::iterator_t<View> current, std::size_t count)
: current_(std::move(current)), count_(std::move(count)) {}

Expand All @@ -65,6 +72,17 @@ namespace ns {
return *this;
}
constexpr void operator++(int) { ++*this; }
constexpr iterator
operator++(int) requires std::ranges::forward_range<View> {
auto tmp = *this;
++*this;
return tmp;
}

friend constexpr bool operator==(const iterator& x, const iterator& y) //
requires std::equality_comparable<std::ranges::iterator_t<View>> {
return x.current_ == y.current_;
}
};

template <std::ranges::input_range View>
Expand Down
16 changes: 16 additions & 0 deletions tests/enumerate_view/enumerate_view.cpp
@@ -1,3 +1,4 @@
#include <forward_list>
#include <catch2/catch_test_macros.hpp>
#include <ns/enumerate_view.hpp>

Expand Down Expand Up @@ -69,4 +70,19 @@ TEST_CASE("enumerate_view", "[enumerate_view]") {
++it;
CHECK(it == std::ranges::end(ev));
}
{
std::forward_list<char> fl{'a', 'b', 'c'};
ns::enumerate_view ev(fl);
static_assert(std::ranges::forward_range<decltype(ev)>);
static_assert(not std::ranges::bidirectional_range<decltype(ev)>);

std::forward_iterator auto it = std::ranges::begin(ev);
CHECK(std::get<0>(*it) == 0);
CHECK(std::get<1>(*it++) == 'a');
CHECK(std::get<0>(*it) == 1);
CHECK(std::get<1>(*it++) == 'b');
CHECK(std::get<0>(*it) == 2);
CHECK(std::get<1>(*it++) == 'c');
CHECK(it == std::ranges::end(ev));
}
}

0 comments on commit 302027f

Please sign in to comment.