forked from alabuzhev/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reversed.hpp
91 lines (69 loc) · 2 KB
/
reversed.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#ifndef ITER_REVERSE_HPP_
#define ITER_REVERSE_HPP_
#include "internal/iterbase.hpp"
#include <utility>
#include <iterator>
namespace iter {
namespace impl {
template <typename Container>
class Reverser;
}
template <typename Container>
impl::Reverser<Container> reversed(Container&&);
}
template <typename Container>
class iter::impl::Reverser {
private:
Container container;
friend Reverser iter::reversed<Container>(Container&&);
Reverser(Container&& in_container)
: container(std::forward<Container>(in_container)) {}
using reverse_iterator_type =
decltype(std::rbegin(std::declval<Container&>()));
using reverse_iterator_deref =
decltype(*std::declval<reverse_iterator_type&>());
using reverse_iterator_traits_deref =
std::remove_reference_t<reverse_iterator_deref>;
using reverse_iterator_arrow = detail::arrow<reverse_iterator_type>;
public:
Reverser(Reverser&&) = default;
class Iterator : public std::iterator<std::input_iterator_tag,
reverse_iterator_traits_deref> {
private:
reverse_iterator_type sub_iter;
public:
Iterator(reverse_iterator_type&& iter) : sub_iter{std::move(iter)} {}
reverse_iterator_deref operator*() {
return *this->sub_iter;
}
reverse_iterator_arrow operator->() {
return apply_arrow(this->sub_iter);
}
Iterator& operator++() {
++this->sub_iter;
return *this;
}
Iterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
bool operator!=(const Iterator& other) const {
return this->sub_iter != other.sub_iter;
}
bool operator==(const Iterator& other) const {
return !(*this != other);
}
};
Iterator begin() {
return {std::rbegin(this->container)};
}
Iterator end() {
return {std::rend(this->container)};
}
};
template <typename Container>
iter::impl::Reverser<Container> iter::reversed(Container&& container) {
return {std::forward<Container>(container)};
}
#endif