Boost logo

Boost Users :

Subject: Re: [Boost-users] [graph] Modifying properties in a visitor?
From: Andrew Sutton (andrew.n.sutton_at_[hidden])
Date: 2009-01-15 18:56:23


On Thu, Jan 15, 2009 at 4:52 PM, James C. Sutherland <
James.Sutherland_at_[hidden]> wrote:

> Can a visitor modify properties in a graph? I would like to use the
> following visitor with the breadth_first_search algorithm:
>
> struct ExecPriorityVisitor : public boost::default_dfs_visitor
> {
> ExecPriorityVisitor();
> void start_vertex( Vertex v, Graph& g ){ g[v].priority = 0; }
> void examine_edge( Edge e, Graph& g ){
> const Vertex src = boost::source(e,g);
> Vertex dest = boost::target(e,g);
> int& priority = g[dest].priority;
> priority = std::max( priority, g[src].priority + 1 );
> }
> };
>
> However, it fails to compile because apparently the graph argument must be
> const. That implies that I cannot modify graph properties from within the
> visitor? Why not?
>

You can, and there are two ways to go about doing it: the dirty way, and the
kind of weird way.

1. You could pass the graph by const&, and then const_cast it and modify the
property. That's the dirty way.

2. The weird (but right) way is to define the visitor to hold a read/write
property map to the graph property in question. The visitor method still
takes the graph by const&, but you have an object that has a non-const& that
allows you to modify properties.

Be careful that the property you're modifying isn't being used by the
algorithm. You might get weird results.

Andrew Sutton
andrew.n.sutton_at_[hidden]



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