I have a vector with value:
obj={1.0,2.0,3.0, 4.0,5.0,6.0 ,7.0,8.0,9.0,10.0}
Assuming that, mathematically obj
is partitioned into three subvectors:
obj={P, Q , R}
where P={1.0,2.0,3.0}
, Q={4.0,5.0,6.0}
, and R={7.0,8.0,9.0,10.0}
and I need to change the values of the sub-vector Q
i.e. from index 3
to 6
;
obj={1.0,2.0,3.0, -4.0,-5.0,-6.0 ,7.0,8.0,9.0,10.0}
Not only negate, but also I want to be able to do any other action independently on a subvector of obj
.
The following code works:
#include <armadillo>
#include <iostream>
using namespace std;
typedef arma::vec::fixed<10> x_vec;
class B
{
public:
x_vec data;
B(x_vec init) :
data(init)
{
}
inline decltype(data.subvec(0,2)) P()
{
return data.subvec(0,2);
}
inline decltype(data.subvec(3,5)) Q()
{
return data.subvec(3,5);
}
inline decltype(data.subvec(6,9)) R()
{
return data.subvec(6,9);
}
};
int main()
{
B obj({1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0});
cout<<"sizeof obj.data: "<<sizeof(obj.data)<<endl;
cout<<"sizeof obj: "<<sizeof(obj)<<endl;
cout<<"value of obj:"<<endl;
obj.data.print();
obj.Q()=-obj.Q();
cout<<"value of obj:"<<endl;
obj.data.print();
return 0;
}
Result
sizeof obj.data: 192
sizeof obj: 192
value of obj:
1.0000
2.0000
3.0000
4.0000
5.0000
6.0000
7.0000
8.0000
9.0000
10.0000
value of obj:
1.0000
2.0000
3.0000
-4.0000
-5.0000
-6.0000
7.0000
8.0000
9.0000
10.0000
However, how to make it work without parenthesis on the property?
I mean using
obj.Q=-obj.Q;
instead of
obj.Q()=-obj.Q();
I don't want to increase the size of my obj
too.
Also, I am looking for a general purpose solution. Not a solution that just negates a subvector of my vector.
Update
The final code works:
#include <armadillo>
#include <iostream>
using namespace std;
typedef arma::vec::fixed<10> x_vec;
struct wrapperB
{
wrapperB* operator ->() { return this; }
typedef decltype(std::declval<x_vec>().subvec(0, 2)) P_type;
typedef decltype(std::declval<x_vec>().subvec(3, 5)) Q_type;
typedef decltype(std::declval<x_vec>().subvec(6, 9)) R_type;
P_type& P;
Q_type& Q;
R_type& R;
wrapperB(P_type _p,Q_type _q,R_type _r) :
P(_p), Q(_q), R(_r)
{
}
};
class B
{
public:
x_vec data;
B(x_vec init) : data(init) {}
wrapperB operator -> ()
{
return wrapperB(data.subvec(0,2), data.subvec(3,5), data.subvec(6,9));
}
};
int main()
{
B obj({1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0});
cout<<"sizeof obj.data: "<<sizeof(obj.data)<<endl;
cout<<"sizeof obj: "<<sizeof(obj)<<endl;
cout<<"value of obj:"<<endl;
obj.data.print();
obj->Q=-obj->Q;
cout<<"value of obj:"<<endl;
obj.data.print();
return 0;
}