-
Notifications
You must be signed in to change notification settings - Fork 1
/
TransfIterTest.cpp
114 lines (79 loc) · 2.96 KB
/
TransfIterTest.cpp
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
#include <boost/iterator_adaptors.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/bind.hpp>
#include <algorithm>
#include<vector>
#include<iostream>
struct M {
float & operator()(int i,int j) {
return m[i][j];
}
/*
const float & operator()(int i,int j) const {
return m[i][j];
}
*/
const float & data(std::pair<int,int> const& p) const {
return m[p.first][p.second];
}
float m[3][3];
};
template<typename T>
const float & cg(const T& t,std::pair<int,int> const& p) {
return t(p.first,p.second);
}
template<typename T>
float & g(T& t,std::pair<int,int> const& p) {
return t(p.first,p.second);
}
int main() {
M m;
float k[3][3] = { {1.,2.,3.}, {4.,5.,6.}, {7.,8.,9.} };
std::copy(&k[0][0],&k[3][3],&m.m[0][0]);
typedef std::vector<std::pair<int,int> > VI;
std::vector<std::pair<int,int> > v;
v.push_back(std::pair<int,int>(2,2));
v.push_back(std::pair<int,int>(1,1));
v.push_back(std::pair<int,int>(0,0));
// external function
{
typedef
boost::_bi::bind_t<float&, float& (*)(M&, const std::pair<int, int>&), boost::_bi::list2<boost::reference_wrapper<M>, boost::arg<1> > > Function;
typedef
boost::_bi::bind_t<const float&, const float& (*)(const M&, const std::pair<int, int>&), boost::_bi::list2<boost::_bi::value<M*>, boost::arg<1> > > ConstFunction;
// boost::_bi::bind_t<boost::_bi::unspecified, g<M>, typename boost::_bi::list_av_2<M, boost::arg<1> >::type> Function;
typedef boost::transform_iterator<Function,VI::const_iterator, const float&> const_iterator;
typedef boost::transform_iterator<Function,VI::const_iterator, float&> iterator;
iterator b =
boost::make_transform_iterator(v.begin(),boost::bind(g<M>,boost::ref(m),_1));
const_iterator p =
boost::make_transform_iterator(v.begin(),boost::bind(g<M>,boost::ref(m),_1));
iterator e =
boost::make_transform_iterator(v.end(),boost::bind(g<M>,boost::ref(m),_1));
// *b = 4;
for (;p!=e;p++) {
float f =*p;
std::cout << f << std::endl;
}
std::cout << "m(0,0) " << m(0,0) << std::endl;
// std::sort(b,e);
std::sort(v.begin(),v.end(), boost::bind(std::less<float>(), boost::bind(g<M>,boost::ref(m),_1), boost::bind(g<M>,boost::ref(m),_2)));
p=b;
for (;p!=e;p++) {
std::cout << *p << std::endl;
}
std::cout << "m(0,0) " << m(0,0) << std::endl;
}
// member function
{
typedef
boost::_bi::bind_t<const float&, boost::_mfi::cmf1<const float&, M, const std::pair<int, int>&>, boost::_bi::list2<boost::_bi::value<M*>, boost::arg<1> > > Function;
typedef boost::transform_iterator<Function,VI::const_iterator> const_iterator;
const_iterator p =
boost::make_transform_iterator(v.begin(),boost::bind(&M::data,&m,_1));
const_iterator e =
boost::make_transform_iterator(v.end(),boost::bind(&M::data,&m,_1));
for (;p!=e;p++)
std::cout << (*p) << std::endl;
}
}