-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtransform.hpp
72 lines (60 loc) · 2.49 KB
/
transform.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
#ifndef TRANSFORM_HPP
#define TRANSFORM_HPP
#include "iterator_range.hpp"
#include <iterator>
#include <type_traits>
namespace ranges
{
template<typename UnderlyingIterator, typename Function>
class transform_iterator
: public std::iterator<
typename std::iterator_traits<UnderlyingIterator>::iterator_category,
std::result_of_t<Function(typename std::iterator_traits<UnderlyingIterator>::value_type)>,
typename std::iterator_traits<UnderlyingIterator>::difference_type,
const std::result_of_t<Function(typename std::iterator_traits<UnderlyingIterator>::value_type)>*,
const std::result_of_t<Function(typename std::iterator_traits<UnderlyingIterator>::value_type)>&
>
{
public:
transform_iterator(UnderlyingIterator iterator, Function func) : iterator_(iterator), func_(func) {}
transform_iterator operator++() {++iterator_; return *this;}
auto operator*() { return func_(*iterator_);}
transform_iterator& operator=(const transform_iterator& other) { iterator_ = other.iterator_; func_ = other.func_;}
bool operator==(const transform_iterator& other){ return iterator_ == other.iterator_; }
bool operator!=(const transform_iterator& other){ return !(*this == other); }
auto operator-(const transform_iterator& other) { return iterator_ - other.iterator_;}
private:
UnderlyingIterator iterator_;
Function func_;
};
template<typename Iterator, typename Function>
transform_iterator<Iterator, Function> make_transform_iterator(Iterator underlyingIterator, Function func)
{
return transform_iterator<Iterator, Function>(underlyingIterator, func);
}
namespace view
{
template<typename Function>
class transform_adaptor
{
public:
explicit transform_adaptor(Function func) : func_(func) {}
Function getFunction() const {return func_;}
private:
Function func_;
};
template<typename Function>
transform_adaptor<Function> transform(Function func)
{
return transform_adaptor<Function>(func);
}
template<typename Range, typename Function>
iterator_range<transform_iterator<typename Range::const_iterator, Function>> operator|(Range const& range, transform_adaptor<Function> adaptor)
{
return iterator_range<transform_iterator<typename Range::const_iterator, Function>>(
make_transform_iterator(range.begin(), adaptor.getFunction()),
make_transform_iterator(range.end(), adaptor.getFunction()));
}
} // namespace view
} // namespace ranges
#endif