forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reversed.hpp
160 lines (131 loc) · 4.89 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#ifndef REVERSE_HPP__
#define REVERSE_HPP__
#include "iterbase.hpp"
#include <utility>
#include <iterator>
namespace iter {
//Forward declarations of Reverser and reversed
template <typename Container>
class Reverser;
template <typename Container>
Reverser<Container> reversed(Container&&);
template <typename Container>
class Reverser {
private:
Container container;
// The reversed function is the only thing allowed to create a
// Reverser
friend Reverser reversed<Container>(Container&&);
// Value constructor for use only in the reversed function
Reverser(Container container)
: container(std::forward<Container>(container))
{ }
Reverser() = delete;
Reverser& operator=(const Reverser&) = delete;
public:
Reverser(const Reverser&) = default;
class Iterator {
private:
reverse_iterator_type<Container> sub_iter;
public:
Iterator (reverse_iterator_type<Container> iter)
: sub_iter{iter}
{ }
reverse_iterator_deref<Container> operator*() {
return *this->sub_iter;
}
Iterator& operator++() {
++this->sub_iter;
return *this;
}
bool operator!=(const Iterator& other) const {
return this->sub_iter != other.sub_iter;
}
};
Iterator begin() {
return {this->container.rbegin()};
}
Iterator end() {
return {this->container.rend()};
}
};
// Helper function to instantiate a Reverser
template <typename Container>
Reverser<Container> reversed(Container&& container) {
return {std::forward<Container>(container)};
}
//
//
// specialization for statically allocated arrays
// this involves some tricks
//
template <typename T, std::size_t N>
Reverser<T[N]> reversed(T (&)[N]);
template <typename T, std::size_t N>
class Reverser<T[N]> {
private:
T *array;
// The reversed function is the only thing allowed to create a
// Reverser
friend Reverser reversed<T, N>(T (&)[N]);
// Value constructor for use only in the reversed function
Reverser(T *array)
: array{array}
{ }
Reverser() = delete;
Reverser& operator=(const Reverser&) = delete;
public:
Reverser(const Reverser&) = default;
class Iterator {
private:
T *sub_iter;
T *stop;
T *dummy_end;
public:
// iter should be the last element in the array
// stop should be the first element
// dummy should be what iter is set to when complete
// the implementation below sets the dummy to one-past-
// the end, since that's the only non-nullptr value that
// the pointer can be set to that is also not a part
// of the actual array
Iterator (T *iter, T *stop, T *dummy)
: sub_iter{iter},
stop{stop},
dummy_end{dummy}
{ }
auto operator*() -> decltype(*array) {
return *this->sub_iter;
}
Iterator& operator++() {
if (this->sub_iter == this->stop) {
this->sub_iter = this->dummy_end;
} else {
// decrementing the pointer is going forwards
// in the reversed direction
--this->sub_iter;
}
return *this;
}
bool operator!=(const Iterator& other) const {
return this->sub_iter != other.sub_iter;
}
};
T *dummy_end() const {
return this->array + N;
}
Iterator begin() {
return {this->array + N - 1,
this->array,
this->dummy_end()};
}
Iterator end() {
return {this->dummy_end(), this->dummy_end(), this->dummy_end()};
}
};
template <typename T, std::size_t N>
Reverser<T[N]> reversed(T (&array)[N]) {
return {array};
}
}
#endif //REVERSE_HPP__