-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmap.h
More file actions
176 lines (140 loc) · 4.42 KB
/
map.h
File metadata and controls
176 lines (140 loc) · 4.42 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Chris Welshman 2014
*/
#ifndef INCLUDED_MAP
#define INCLUDED_MAP
#include <iterator>
#include <utility>
#include <type_traits>
template<typename F,typename Iterator>
struct map_iterator {
typedef typename std::iterator_traits<Iterator>::value_type original_value_type;
typedef typename std::iterator_traits<Iterator>::difference_type difference_type;
typedef typename std::iterator_traits<Iterator>::iterator_category iterator_category;
typedef typename std::result_of<F(original_value_type)>::type value_type;
typedef std::pair<Iterator,Iterator> range_type;
typedef const value_type* pointer;
typedef value_type reference;
map_iterator() = default;
map_iterator( const F& f, const range_type& range ) : f(f), range(range), it(range.first) {}
map_iterator( const F& f, const range_type& range, const Iterator& it ) : f(f), range(range), it(it) {}
map_iterator( const F& f, const Iterator& first, const Iterator& last ) : map_iterator(f,range_type(first,last)) {}
map_iterator( const F& f, const Iterator& first, const Iterator& last, const Iterator& it ) : map_iterator(f,range_type(first,last),it) {}
value_type operator*() const {
return f(*it);
}
map_iterator<F,Iterator>& operator++() {
++it;
return *this;
}
map_iterator<F,Iterator> operator++(int) {
map_iterator<F,Iterator> temp = *this;
++(*this);
return temp;
}
map_iterator<F,Iterator>& operator--() {
--it;
return *this;
}
map_iterator<F,Iterator> operator--(int) {
map_iterator<F,Iterator> temp = *this;
--(*this);
return temp;
}
map_iterator<F,Iterator>& operator+=( difference_type offset ) {
it += offset;
return *this;
}
map_iterator<F,Iterator> operator+( difference_type offset ) const {
map_iterator<F,Iterator> temp = *this;
return temp += offset;
}
map_iterator<F,Iterator>& operator-=( difference_type offset ) {
return *this += -offset;
}
map_iterator<F,Iterator> operator-( difference_type offset ) const {
map_iterator<F,Iterator> temp = *this;
return temp -= offset;
}
difference_type operator-( const map_iterator<F,Iterator>& rhs ) const {
return std::distance( rhs.it, it );
}
value_type operator[]( difference_type offset ) const {
return *(*this + offset);
}
difference_type index() const {
return N = std::distance( range.first, it );
}
bool operator==( const map_iterator<F,Iterator>& rhs ) const {
return it == rhs.it;
}
bool operator!=( const map_iterator<F,Iterator>& rhs ) const {
return !(*this == rhs);
}
bool operator<( const map_iterator<F,Iterator>& rhs ) const {
return rhs - *this > 0;
}
bool operator>( const map_iterator<F,Iterator>& rhs ) const {
return rhs < *this;
}
bool operator<=( const map_iterator<F,Iterator>& rhs ) const {
return !( *this > rhs );
}
bool operator>=( const map_iterator<F,Iterator>& rhs ) const {
return !( *this < rhs );
}
protected:
range_type range;
Iterator it;
F f;
};
template<typename F,typename Iterator>
struct map_range {
typedef typename std::iterator_traits<Iterator>::value_type original_value_type;
typedef typename std::result_of<F(original_value_type)>::type value_type;
typedef typename std::iterator_traits<Iterator>::difference_type difference_type;
typedef Iterator original_iterator;
typedef map_iterator<F,original_iterator> iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::pair<Iterator,Iterator> range_type;
map_range( const F& f, const range_type& range ) : f(f), range(range) {}
map_range( const F& f, const Iterator& first, const Iterator& last ) : map_range(f,range_type(first,last)) {}
difference_type size() const {
return std::distance( range.first, range.second );
}
iterator begin() const {
return iterator( f, range );
}
iterator end() const {
return iterator( f, range, range.second );
}
protected:
range_type range;
F f;
};
template<typename F,typename Iterator>
inline map_range<F,Iterator> map( Iterator&& first, Iterator&& last, F&& f ) {
return map_range<F,Iterator>( std::forward<F>(f),
std::make_pair(
std::forward<Iterator>(first),
std::forward<Iterator>(last)
)
);
}
template<typename Range,typename F>
inline auto map( Range&& r, F&& f ) {
return map(
begin( std::forward<Range>(r) ),
end( std::forward<Range>(r) ),
std::forward<F>(f)
);
}
template<typename Range,typename F>
inline auto cmap( const Range& r, F&& f ) {
return map(
cbegin( r ),
cend( r ),
std::forward<F>(f)
);
}
#endif