Boost.lambda: how to sort this vector with function?

I want to sort a vector of Edges according to their remaining estimated distance to the finish point (an own implementation of a graph algorithm). How do I sort this vector with edges: // Returns the cost between start and finish, given a graph int CostEstimate( const Graph& aGraph, const Vertex& start, const Vertex& finish) ... // Member function of a graph std::vector< Edge > v = ...; // Sort them according to the cost std::sort( v.begin(), v.end(), boost::lambda::bind( CostEstimate( (*this), boost::lambda::bind( &Edge::otherSide, &start, boost::lambda::_1), finish)) < boost::lambda::bind( CostEstimate( (*this), boost::lambda::bind( &Edge::otherSide, &start, boost::lambda::_2), finish))); where otherSide is a const function of the class Edge which returns a "const Vertex&" Compiling this results in an error: error: invalid initialization of reference of type ‘const Vertex&’ from expression of type ‘const boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::action<3, boost::lambda::function_action<3, boost::lambda::detail::unspecified> >, boost::tuples::tuple<const Vertex& (Edge::* const)(const Vertex&)const, const Vertex* const, const boost::lambda::lambda_functor<boost::lambda::placeholder<1> >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >’ TIA -- Groeten, Joost Kraaijeveld Askesis B.V. Molukkenstraat 14 6524NB Nijmegen tel: 024-3888063 / 06-51855277 fax: 024-3608416 web: www.askesis.nl

Joost Kraaijeveld wrote:
I want to sort a vector of Edges according to their remaining estimated distance to the finish point (an own implementation of a graph algorithm).
How do I sort this vector with edges:
// Returns the cost between start and finish, given a graph int CostEstimate( const Graph& aGraph, const Vertex& start, const Vertex& finish) ...
// Member function of a graph std::vector< Edge > v = ...; // Sort them according to the cost std::sort( v.begin(), v.end(), boost::lambda::bind( CostEstimate( (*this), boost::lambda::bind( &Edge::otherSide, &start, boost::lambda::_1), finish)) < boost::lambda::bind( CostEstimate( (*this), boost::lambda::bind( &Edge::otherSide, &start, boost::lambda::_2), finish)));
where otherSide is a const function of the class Edge which returns a "const Vertex&"
Compiling this results in an error:
error: invalid initialization of reference of type ‘const Vertex&’ from expression of type ‘const boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::action<3, boost::lambda::function_action<3, boost::lambda::detail::unspecified> >, boost::tuples::tuple<const Vertex& (Edge::* const)(const Vertex&)const, const Vertex* const, const boost::lambda::lambda_functor<boost::lambda::placeholder<1> >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >’
TIA
Hi, I am sure there is some way to extend the code you wrote in such a way that it compiles and works as it should. My question would be: Are you going to still understand it in, say, half a year? Four binds in one expression is certainly not unheard of, but personally I would do it with one bind and an old-fashioned function, see attached code. Hope I got your settings right. I was a bit confused by otherSide being a function of edge. This would mean that start is an Edge, while finish is a Vertex in the sort-code. But start and finish are both Vertexes in the CostEstimate function... To avoid such wondering, it would help if you could provide a small but complete program which shows the problem. Nothing more and nothing less. Regards, Roland

Hi Roland,
I am sure there is some way to extend the code you wrote in such a way that it compiles and works as it should. My question would be: Are you going to still understand it in, say, half a year? Four binds in one expression is certainly not unheard of, but personally I would do it with one bind and an old-fashioned function, see attached code.
Part of the code is just investigating that: am I (and others) able to understand what I wrote. I also implemented something you suggested, to compare it with the lambda expression.
Hope I got your settings right. I was a bit confused by otherSide being a function of edge. This would mean that start is an Edge, while finish is a Vertex in the sort-code. But start and finish are both Vertexes in the CostEstimate function...
To avoid such wondering, it would help if you could provide a small but complete program which shows the problem. Nothing more and nothing less.
http://trac.askesis.nl/svn/tidbits/AIAStar/Main.cpp change the comments from lines 457 and further to get the error TIA -- Groeten, Joost Kraaijeveld Askesis B.V. Molukkenstraat 14 6524NB Nijmegen tel: 024-3888063 / 06-51855277 fax: 024-3608416 web: www.askesis.nl

Hi Joost, well, I am afraid I cannot help you in rectifying the lambda (tried that with the code I wrote). My suggestions would be a) use the currently active code or b) boil down the problem to a complete example of some 30 lines and ask again. I am sure someone will know, but it should be easy to experiment with. Sorry that I cannot solve the original problem :-( Regards, Roland Joost Kraaijeveld wrote:
Hi Roland,
I am sure there is some way to extend the code you wrote in such a way that it compiles and works as it should. My question would be: Are you going to still understand it in, say, half a year? Four binds in one expression is certainly not unheard of, but personally I would do it with one bind and an old-fashioned function, see attached code.
Part of the code is just investigating that: am I (and others) able to understand what I wrote. I also implemented something you suggested, to compare it with the lambda expression.
Hope I got your settings right. I was a bit confused by otherSide being a function of edge. This would mean that start is an Edge, while finish is a Vertex in the sort-code. But start and finish are both Vertexes in the CostEstimate function...
To avoid such wondering, it would help if you could provide a small but complete program which shows the problem. Nothing more and nothing less.
http://trac.askesis.nl/svn/tidbits/AIAStar/Main.cpp
change the comments from lines 457 and further to get the error
TIA

AMDG Joost Kraaijeveld wrote:
I want to sort a vector of Edges according to their remaining estimated distance to the finish point (an own implementation of a graph algorithm).
How do I sort this vector with edges:
// Returns the cost between start and finish, given a graph int CostEstimate( const Graph& aGraph, const Vertex& start, const Vertex& finish) ...
// Member function of a graph std::vector< Edge > v = ...; // Sort them according to the cost std::sort( v.begin(), v.end(), boost::lambda::bind( CostEstimate( (*this), boost::lambda::bind( &Edge::otherSide, &start, boost::lambda::_1), finish)) < boost::lambda::bind( CostEstimate( (*this), boost::lambda::bind( &Edge::otherSide, &start, boost::lambda::_2), finish)));
The outer bind should be boost::lambda::bind( CostEstimate, boost::lambda::var(*this), boost::lambda::bind( &Edge::otherSide, &start, boost::lambda::_1), finish) In Christ, Steven Watanabe
participants (3)
-
Joost Kraaijeveld
-
Roland Bock
-
Steven Watanabe