|
7 | 7 | #include <iterator> |
8 | 8 |
|
9 | 9 | namespace iter { |
10 | | - template <typename Container> |
11 | | - class Reverser; |
| 10 | + namespace impl { |
| 11 | + template <typename Container> |
| 12 | + class Reverser; |
12 | 13 |
|
13 | | - template <typename Container> |
14 | | - Reverser<Container> reversed(Container&&); |
| 14 | + template <typename T, std::size_t N> |
| 15 | + class Reverser<T[N]>; |
| 16 | + } |
15 | 17 |
|
16 | 18 | template <typename Container> |
17 | | - class Reverser { |
18 | | - private: |
19 | | - Container container; |
20 | | - friend Reverser reversed<Container>(Container&&); |
| 19 | + impl::Reverser<Container> reversed(Container&&); |
21 | 20 |
|
22 | | - Reverser(Container&& in_container) |
23 | | - : container(std::forward<Container>(in_container)) {} |
| 21 | + template <typename T, std::size_t N> |
| 22 | + impl::Reverser<T[N]> reversed(T(&)[N]); |
| 23 | +} |
| 24 | + |
| 25 | +template <typename Container> |
| 26 | +class iter::impl::Reverser { |
| 27 | + private: |
| 28 | + Container container; |
| 29 | + friend Reverser iter::reversed<Container>(Container&&); |
| 30 | + |
| 31 | + Reverser(Container&& in_container) |
| 32 | + : container(std::forward<Container>(in_container)) {} |
| 33 | + |
| 34 | + public: |
| 35 | + class Iterator : public std::iterator<std::input_iterator_tag, |
| 36 | + iterator_traits_deref<Container>> { |
| 37 | + private: |
| 38 | + reverse_iterator_type<Container> sub_iter; |
24 | 39 |
|
25 | 40 | public: |
26 | | - class Iterator : public std::iterator<std::input_iterator_tag, |
27 | | - iterator_traits_deref<Container>> { |
28 | | - private: |
29 | | - reverse_iterator_type<Container> sub_iter; |
30 | | - |
31 | | - public: |
32 | | - Iterator(reverse_iterator_type<Container>&& iter) |
33 | | - : sub_iter{std::move(iter)} {} |
34 | | - |
35 | | - reverse_iterator_deref<Container> operator*() { |
36 | | - return *this->sub_iter; |
37 | | - } |
38 | | - |
39 | | - reverse_iterator_arrow<Container> operator->() { |
40 | | - return apply_arrow(this->sub_iter); |
41 | | - } |
42 | | - |
43 | | - Iterator& operator++() { |
44 | | - ++this->sub_iter; |
45 | | - return *this; |
46 | | - } |
47 | | - |
48 | | - Iterator operator++(int) { |
49 | | - auto ret = *this; |
50 | | - ++*this; |
51 | | - return ret; |
52 | | - } |
53 | | - |
54 | | - bool operator!=(const Iterator& other) const { |
55 | | - return this->sub_iter != other.sub_iter; |
56 | | - } |
57 | | - |
58 | | - bool operator==(const Iterator& other) const { |
59 | | - return !(*this != other); |
60 | | - } |
61 | | - }; |
62 | | - |
63 | | - Iterator begin() { |
64 | | - return {this->container.rbegin()}; |
| 41 | + Iterator(reverse_iterator_type<Container>&& iter) |
| 42 | + : sub_iter{std::move(iter)} {} |
| 43 | + |
| 44 | + reverse_iterator_deref<Container> operator*() { |
| 45 | + return *this->sub_iter; |
65 | 46 | } |
66 | 47 |
|
67 | | - Iterator end() { |
68 | | - return {this->container.rend()}; |
| 48 | + reverse_iterator_arrow<Container> operator->() { |
| 49 | + return apply_arrow(this->sub_iter); |
| 50 | + } |
| 51 | + |
| 52 | + Iterator& operator++() { |
| 53 | + ++this->sub_iter; |
| 54 | + return *this; |
| 55 | + } |
| 56 | + |
| 57 | + Iterator operator++(int) { |
| 58 | + auto ret = *this; |
| 59 | + ++*this; |
| 60 | + return ret; |
| 61 | + } |
| 62 | + |
| 63 | + bool operator!=(const Iterator& other) const { |
| 64 | + return this->sub_iter != other.sub_iter; |
| 65 | + } |
| 66 | + |
| 67 | + bool operator==(const Iterator& other) const { |
| 68 | + return !(*this != other); |
69 | 69 | } |
70 | 70 | }; |
71 | 71 |
|
72 | | - template <typename Container> |
73 | | - Reverser<Container> reversed(Container&& container) { |
74 | | - return {std::forward<Container>(container)}; |
| 72 | + Iterator begin() { |
| 73 | + return {this->container.rbegin()}; |
75 | 74 | } |
76 | 75 |
|
77 | | - // |
78 | | - // specialization for statically allocated arrays |
79 | | - // |
80 | | - template <typename T, std::size_t N> |
81 | | - Reverser<T[N]> reversed(T(&)[N]); |
| 76 | + Iterator end() { |
| 77 | + return {this->container.rend()}; |
| 78 | + } |
| 79 | +}; |
82 | 80 |
|
83 | | - template <typename T, std::size_t N> |
84 | | - class Reverser<T[N]> { |
85 | | - private: |
86 | | - T* array; |
87 | | - friend Reverser reversed<T, N>(T(&)[N]); |
| 81 | +template <typename Container> |
| 82 | +iter::impl::Reverser<Container> iter::reversed(Container&& container) { |
| 83 | + return {std::forward<Container>(container)}; |
| 84 | +} |
| 85 | + |
| 86 | +// specialization for statically allocated arrays |
| 87 | + |
| 88 | +template <typename T, std::size_t N> |
| 89 | +class iter::impl::Reverser<T[N]> { |
| 90 | + private: |
| 91 | + T* array; |
| 92 | + friend Reverser iter::reversed<T, N>(T(&)[N]); |
88 | 93 |
|
89 | | - // Value constructor for use only in the reversed function |
90 | | - Reverser(T* in_array) : array{in_array} {} |
| 94 | + // Value constructor for use only in the reversed function |
| 95 | + Reverser(T* in_array) : array{in_array} {} |
| 96 | + |
| 97 | + public: |
| 98 | + Reverser(const Reverser&) = default; |
| 99 | + class Iterator : public std::iterator<std::input_iterator_tag, T> { |
| 100 | + private: |
| 101 | + T* sub_iter; |
91 | 102 |
|
92 | 103 | public: |
93 | | - Reverser(const Reverser&) = default; |
94 | | - class Iterator : public std::iterator<std::input_iterator_tag, T> { |
95 | | - private: |
96 | | - T* sub_iter; |
97 | | - |
98 | | - public: |
99 | | - Iterator(T* iter) : sub_iter{iter} {} |
100 | | - |
101 | | - T& operator*() { |
102 | | - return *(this->sub_iter - 1); |
103 | | - } |
104 | | - |
105 | | - T* operator->() { |
106 | | - return (this->sub_iter - 1); |
107 | | - } |
108 | | - |
109 | | - Iterator& operator++() { |
110 | | - --this->sub_iter; |
111 | | - return *this; |
112 | | - } |
113 | | - |
114 | | - Iterator operator++(int) { |
115 | | - auto ret = *this; |
116 | | - ++*this; |
117 | | - return ret; |
118 | | - } |
119 | | - |
120 | | - bool operator!=(const Iterator& other) const { |
121 | | - return this->sub_iter != other.sub_iter; |
122 | | - } |
123 | | - |
124 | | - bool operator==(const Iterator& other) const { |
125 | | - return !(*this != other); |
126 | | - } |
127 | | - }; |
128 | | - |
129 | | - Iterator begin() { |
130 | | - return {this->array + N}; |
| 104 | + Iterator(T* iter) : sub_iter{iter} {} |
| 105 | + |
| 106 | + T& operator*() { |
| 107 | + return *(this->sub_iter - 1); |
| 108 | + } |
| 109 | + |
| 110 | + T* operator->() { |
| 111 | + return (this->sub_iter - 1); |
131 | 112 | } |
132 | 113 |
|
133 | | - Iterator end() { |
134 | | - return {this->array}; |
| 114 | + Iterator& operator++() { |
| 115 | + --this->sub_iter; |
| 116 | + return *this; |
| 117 | + } |
| 118 | + |
| 119 | + Iterator operator++(int) { |
| 120 | + auto ret = *this; |
| 121 | + ++*this; |
| 122 | + return ret; |
| 123 | + } |
| 124 | + |
| 125 | + bool operator!=(const Iterator& other) const { |
| 126 | + return this->sub_iter != other.sub_iter; |
| 127 | + } |
| 128 | + |
| 129 | + bool operator==(const Iterator& other) const { |
| 130 | + return !(*this != other); |
135 | 131 | } |
136 | 132 | }; |
137 | 133 |
|
138 | | - template <typename T, std::size_t N> |
139 | | - Reverser<T[N]> reversed(T(&array)[N]) { |
140 | | - return {array}; |
| 134 | + Iterator begin() { |
| 135 | + return {this->array + N}; |
141 | 136 | } |
| 137 | + |
| 138 | + Iterator end() { |
| 139 | + return {this->array}; |
| 140 | + } |
| 141 | +}; |
| 142 | + |
| 143 | +template <typename T, std::size_t N> |
| 144 | +iter::impl::Reverser<T[N]> iter::reversed(T(&array)[N]) { |
| 145 | + return {array}; |
142 | 146 | } |
143 | 147 |
|
144 | 148 | #endif |
0 commit comments