Skip to content

Commit

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

iterator() requires
Expand Down Expand Up @@ -79,6 +80,19 @@ namespace ns {
return tmp;
}

constexpr iterator&
operator--() requires std::ranges::bidirectional_range<View> {
--current_;
--count_;
return *this;
}
constexpr iterator
operator--(int) requires std::ranges::bidirectional_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_;
Expand Down
16 changes: 16 additions & 0 deletions tests/enumerate_view/enumerate_view.cpp
@@ -1,4 +1,5 @@
#include <forward_list>
#include <list>
#include <catch2/catch_test_macros.hpp>
#include <ns/enumerate_view.hpp>

Expand Down Expand Up @@ -85,4 +86,19 @@ TEST_CASE("enumerate_view", "[enumerate_view]") {
CHECK(std::get<1>(*it++) == 'c');
CHECK(it == std::ranges::end(ev));
}
{
std::list<char> l{'a', 'b', 'c'};
ns::enumerate_view ev(l);
static_assert(std::ranges::bidirectional_range<decltype(ev)>);
static_assert(not std::ranges::random_access_range<decltype(ev)>);

std::bidirectional_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 29e9e75

Please sign in to comment.