Boost logo

Boost Users :

Subject: Re: [Boost-users] [BGL] Unable to sort list of edge_descriptors
From: Mike Douglass (douglassm13_at_[hidden])
Date: 2010-11-08 03:12:35


Thank you Cedric and Peter. I implemented your suggestions and my program is
working perfectly.

________________________________
From: Cedric Laczny <cedric.laczny_at_[hidden]>
To: boost-users_at_[hidden]
Sent: Sun, November 7, 2010 7:41:18 AM
Subject: Re: [Boost-users] [BGL] Unable to sort list of edge_descriptors

Hi Pete,

On Sunday, 7. November 2010 07:20:50 Peter Wright wrote:

>
> One way to fix it would be as Cedric suggested, and that's the model I
> used in my douglass-working.cpp (attached). I changed SortByName so it
> requires a const Graph& in its constructor, stores a reference to that
> in a private variable, *then* changed SortByName::operator() to take
> two edgeDescriptor args and return bool.
>
> It is dicey in one way, in that it stores a *reference* to its Graph -
> so if the SortByName object outlives the Graph then it could blow up
> spectacularly. But the alternative of copying the entire graph seemed
> a bit excessive.
>
>
> Another way to fix it (and also avoid the reference-storing problem)
> requires using Boost::Bind - change SortByName to this:
>
> ----------------------------------------------------------------------
> template <typename G, typename ED>
> struct SortByName
> {
> typedef bool result_type; // required for use by STL algorithms.
> result_type operator()(G& g, const ED& a, const ED& b)
> {
> return g[a].eName < g[b].eName;
> }
> };
> ----------------------------------------------------------------------
>
> ...and the A.sort call to this:
>
> A.sort( boost::bind(SortByName<Graph, edgeDescriptor>(), g, _1, _2) );
>
> That seems to work fine, and may be a closer match to what you
> originally intended.
>

I have to agree that your solution is nicer with respect to the living time of
the graph and struct, respectively.
Those are things that are easily neglected and can hit you very bad.
Therefore, note to myself: keep such things always in the back of your head!

>
> Pete.

Best regards and thank you for the idea,

Cedric
_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users

      



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net