Newer
Older
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
/*
* foreach.hpp
*
* Created on: Apr 1, 2013
* Author: Jan Travnicek
*/
#ifndef __FOREACH_HPP_
#define __FOREACH_HPP_
namespace std {
template <class Iterator1, class Iterator2>
class const_pair_foreach_iterator {
Iterator1 current1;
Iterator2 current2;
public:
typedef Iterator1 iterator_type1;
typedef typename iterator_traits<Iterator1>::iterator_category iterator_category1;
typedef typename iterator_traits<Iterator1>::value_type value_type1;
typedef Iterator1 pointer1;
typedef const value_type1& reference1;
typedef Iterator2 iterator_type2;
typedef typename iterator_traits<Iterator2>::iterator_category iterator_category2;
typedef typename iterator_traits<Iterator2>::value_type value_type2;
typedef Iterator2 pointer2;
typedef const value_type2& reference2;
explicit const_pair_foreach_iterator() {
}
explicit const_pair_foreach_iterator (Iterator1 it1, Iterator2 it2) : current1(it1), current2(it2) {
}
const_pair_foreach_iterator (const const_pair_foreach_iterator<Iterator1, Iterator2>& it) : current1(it.current1), current2(it.current2) {
}
const_pair_foreach_iterator& operator= (const const_pair_foreach_iterator<Iterator1, Iterator2>& it) {
current1 = it.current1;
current2 = it.current2;
}
iterator_type1 base1() const {
return current1;
}
iterator_type2 base2() const {
return current2;
}
std::tuple<reference1, reference2> operator*() const {
return std::tie(*current1, *current2);
}
const_pair_foreach_iterator& operator++() {
++current1;
++current2;
return *this;
}
const_pair_foreach_iterator& operator--() {
--current1;
--current2;
return *this;
}
const_pair_foreach_iterator operator++(int) {
const_pair_foreach_iterator temp = *this;
++current1;
++current2;
return temp;
}
const_pair_foreach_iterator operator--(int) {
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
const_pair_foreach_iterator temp = *this;
--current1;
--current2;
return temp;
}
bool operator== (const const_pair_foreach_iterator<Iterator1, Iterator2>& other) {
return this->current1 == other.current1 && this->current2 == other.current2;
}
bool operator!= (const const_pair_foreach_iterator<Iterator1, Iterator2>& other) {
return ! ( *this == other );
}
};
template<class Iterator1, class Iterator2>
const_pair_foreach_iterator<Iterator1, Iterator2> make_pair_foreach_iterator (Iterator1 it1, Iterator2 it2) {
return const_pair_foreach_iterator<Iterator1, Iterator2>(it1, it2);
}
template<class T, class R>
class const_pair_foreach {
const T& first;
const R& second;
public:
const_pair_foreach(const T& first, const R& second) : first(first), second(second) {}
const_pair_foreach_iterator<typename T::const_iterator, typename R::const_iterator> begin() const {
return make_pair_foreach_iterator(first.begin(), second.begin());
}
const_pair_foreach_iterator<typename T::const_iterator, typename R::const_iterator> end() const {
return make_pair_foreach_iterator(first.end(), second.end());
}
};
template<class T, class R>
const_pair_foreach<T, R> make_pair_foreach(const T& first, const R& second) {
return const_pair_foreach<T, R>(first, second);
}
} /* namespace std */
#endif /* __FOREACH_HPP_ */