RE: [Boost-Users] iterating over a tuple
Toon Knapen wrote:
I have a list of heterogeneous objects, e.g.
struct Foo { Foo(int v) : value_( v ) {} int value() const { return value_ ; } int value_; };
struct Bar { Bar(int v) : value_( v ) {} int value() const { return value_ ; } int value_; };
int main() { Foo foo(3); Bar bar(7); typedef boost::tuple< Foo, Bar > MyTuple ; MyTuple my_tuple( foo, bar ); return 0; }
And now I want to have a function that can accumulate the value sof all the objects in the tuple, e.g. (pseudocode since my problem is exacly that I can't figure out how to code this)
template < typename TypleType > int accumulate_values(const TupleType& t) { int acc = 0; for(int i = 0 ; i < t.size() ; ++i ) acc += (get< i >( t )).value(); }
Can I do this using mpl (mpl looks great but I can't figure out how to do this).
It's not exactly a part of MPL (the library doesn't target "run-time" stuff to that extent yet; I hope it will some day), but "tuple extension" components I wrote some time ago (http://groups.yahoo.com/group/boost/files/tuple_ext/tuple_ext-08-nov-01.zip ) allow one to do exactly these kinds of things: // untested! :) template< typename Seq, typename T, typename BinaryOp > T accumulate(Seq const& s, T const& Init, BinaryOp op) { T result = Init; for_each( begin(s) , end(s) , boost::bind<T>(op, boost::ref(result), _1) ); return result; } With the above, you can do something like this: struct get_value { template< typename T > int operator()(T const& x) const { return x.value(); } }; int sum = accumulate( tuple , 0 , boost::bind(std::plus<int>() , _1 , boost::bind<int>(get_value(),_2) ) ); Hope it's not too complicated :). LL might allow to do better on this one. Aleksey
On Friday 26 April 2002 01:37 pm, Aleksey Gurtovoy wrote:
Toon Knapen wrote:
I have a list of heterogeneous objects, e.g.
struct Foo { Foo(int v) : value_( v ) {} int value() const { return value_ ; } int value_; };
struct Bar { Bar(int v) : value_( v ) {} int value() const { return value_ ; } int value_; };
int main() { Foo foo(3); Bar bar(7); typedef boost::tuple< Foo, Bar > MyTuple ; MyTuple my_tuple( foo, bar ); return 0; }
And now I want to have a function that can accumulate the value sof all the objects in the tuple, e.g. (pseudocode since my problem is exacly that I can't figure out how to code this)
template < typename TypleType > int accumulate_values(const TupleType& t) { int acc = 0; for(int i = 0 ; i < t.size() ; ++i ) acc += (get< i >( t )).value(); }
Can I do this using mpl (mpl looks great but I can't figure out how to do this).
[snip Aleksey's solution]
Hope it's not too complicated :). LL might allow to do better on this one. Thanks, I'll try it out. Coudl you enlighten me on how to solve it with the lambda library. I don't see how to iterate over the different indexes in combination with LL ?
t
On Friday 26 April 2002 13:37, Aleksey Gurtovoy wrote:
Toon Knapen wrote:
I have a list of heterogeneous objects, e.g. [snip sample code]
And now I want to have a function that can accumulate the value sof all the objects in the tuple, e.g. (pseudocode since my problem is exacly that I can't figure out how to code this)
template < typename TypleType > int accumulate_values(const TupleType& t) { int acc = 0; for(int i = 0 ; i < t.size() ; ++i ) acc += (get< i >( t )).value(); }
Can I do this using mpl (mpl looks great but I can't figure out how to do this).
It's not exactly a part of MPL (the library doesn't target "run-time" stuff to that extent yet; I hope it will some day), but "tuple extension" components I wrote some time ago (http://groups.yahoo.com/group/boost/files/tuple_ext/tuple_ext-08-nov-01.zi p ) allow one to do exactly these kinds of things: [snip aleksey's solution]
Thanks, I like tuple_ext a _lot_ ! It would be great to have complete STL-like functionality like this. I created an accumulate function (similar to the STL one) (see below) I also had to change your for_each. Actually, as in the STL, the unary-function does not necessarily need to be const so I removed the const. And finally, I also removed the friend declaration in the tuple_iterator since it complains about an implicit friend declaration to itself when using an iterator in a const-member function (gcc 2.95.3 and 3.x) Plans on adding this tuple_ext to the CVS ? toon [Non-text portions of this message have been removed]
participants (2)
-
Aleksey Gurtovoy
-
Toon Knapen