@@ -132,91 +132,32 @@ inline void deleter(T *Ptr) {
132
132
133
133
// mapped_iterator - This is a simple iterator adapter that causes a function to
134
134
// be applied whenever operator* is invoked on the iterator.
135
- template <class RootIt , class UnaryFunc >
136
- class mapped_iterator {
137
- RootIt current;
138
- UnaryFunc Fn;
139
135
136
+ template <typename ItTy, typename FuncTy,
137
+ typename FuncReturnTy =
138
+ decltype (std::declval<FuncTy>()(*std::declval<ItTy>()))>
139
+ class mapped_iterator
140
+ : public iterator_adaptor_base<
141
+ mapped_iterator<ItTy, FuncTy>, ItTy,
142
+ typename std::iterator_traits<ItTy>::iterator_category,
143
+ typename std::remove_reference<FuncReturnTy>::type> {
140
144
public:
141
- using iterator_category =
142
- typename std::iterator_traits<RootIt>::iterator_category;
143
- using difference_type =
144
- typename std::iterator_traits<RootIt>::difference_type;
145
- using value_type =
146
- decltype (std::declval<UnaryFunc>()(*std::declval<RootIt>()));
147
-
148
- using pointer = void ;
149
- using reference = void ; // Can't modify value returned by fn
150
-
151
- using iterator_type = RootIt;
152
-
153
- inline explicit mapped_iterator (const RootIt &I, UnaryFunc F)
154
- : current(I), Fn(F) {}
155
-
156
- inline value_type operator *() const { // All this work to do this
157
- return Fn (*current); // little change
158
- }
159
-
160
- mapped_iterator &operator ++() {
161
- ++current;
162
- return *this ;
163
- }
164
- mapped_iterator &operator --() {
165
- --current;
166
- return *this ;
167
- }
168
- mapped_iterator operator ++(int ) {
169
- mapped_iterator __tmp = *this ;
170
- ++current;
171
- return __tmp;
172
- }
173
- mapped_iterator operator --(int ) {
174
- mapped_iterator __tmp = *this ;
175
- --current;
176
- return __tmp;
177
- }
178
- mapped_iterator operator +(difference_type n) const {
179
- return mapped_iterator (current + n, Fn);
180
- }
181
- mapped_iterator &operator +=(difference_type n) {
182
- current += n;
183
- return *this ;
184
- }
185
- mapped_iterator operator -(difference_type n) const {
186
- return mapped_iterator (current - n, Fn);
187
- }
188
- mapped_iterator &operator -=(difference_type n) {
189
- current -= n;
190
- return *this ;
191
- }
192
- reference operator [](difference_type n) const { return *(*this + n); }
145
+ mapped_iterator (ItTy U, FuncTy F)
146
+ : mapped_iterator::iterator_adaptor_base (std::move (U)), F (std::move (F)) {}
193
147
194
- bool operator !=(const mapped_iterator &X) const { return !operator ==(X); }
195
- bool operator ==(const mapped_iterator &X) const {
196
- return current == X.current ;
197
- }
198
- bool operator <(const mapped_iterator &X) const { return current < X.current ; }
148
+ ItTy getCurrent () { return this ->I ; }
199
149
200
- difference_type operator -(const mapped_iterator &X) const {
201
- return current - X.current ;
202
- }
150
+ FuncReturnTy operator *() { return F (*this ->I ); }
203
151
204
- inline const RootIt & getCurrent () const { return current; }
205
- inline const UnaryFunc & getFunc () const { return Fn; }
152
+ private:
153
+ FuncTy F;
206
154
};
207
155
208
- template <class Iterator , class Func >
209
- inline mapped_iterator<Iterator, Func>
210
- operator +(typename mapped_iterator<Iterator, Func>::difference_type N,
211
- const mapped_iterator<Iterator, Func> &X) {
212
- return mapped_iterator<Iterator, Func>(X.getCurrent () - N, X.getFunc ());
213
- }
214
-
215
156
// map_iterator - Provide a convenient way to create mapped_iterators, just like
216
157
// make_pair is useful for creating pairs...
217
158
template <class ItTy , class FuncTy >
218
- inline mapped_iterator<ItTy, FuncTy> map_iterator (const ItTy & I, FuncTy F) {
219
- return mapped_iterator<ItTy, FuncTy>(I, F );
159
+ inline mapped_iterator<ItTy, FuncTy> map_iterator (ItTy I, FuncTy F) {
160
+ return mapped_iterator<ItTy, FuncTy>(std::move (I), std::move (F) );
220
161
}
221
162
222
163
// / Helper to determine if type T has a member called rbegin().
0 commit comments