3

As the title suggests, I'm using boost::depth_first_search and using a Visitor (inheriting from boost::default_dfs_visitor) to implement some algorithm.

However, during the algorithm's run, I want to save some information in the visitor, to be queried later. However, the information is erased after the DFS is done, so I assume it uses a copy. Other than just using pointers for all private variables, is there a way to prevent this and make boost use my copy?

dsolimano
  • 8,870
  • 3
  • 48
  • 63
Amir Rachum
  • 76,817
  • 74
  • 166
  • 248

2 Answers2

2

You could try passing your visitor wrapped in a boost::reference_wrapper.

Edit - teh codez

YourVisitorClass your_visitor;
boost::depth_first_search(your_graph, boost::ref(your_visitor), 
                          your_color_map);

boost::ref(your_visitor) returns a boost::reference_wrapper<YourVisitorClass>. When depth_first_search creates a copy of that arguments, it will copy the reference_wrapper instead of the visitor object. Copies of the reference will refer to the same instance as the original.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • @Amir: I do not know your code, but I made something up that should be understandable. – Björn Pollex Jan 16 '11 at 20:53
  • doesn't seem to work. I used to do `boost::depth_first_search(g, boost:visitor(myVisitor))` and tried to change it to `boost::depth_first_search(g, boost::ref(boost:visitor(myVisitor)))` or `boost::depth_first_search(g, boost:visitor(boost::ref(myVisitor)))` and both did not work (did not compile). – Amir Rachum Jan 17 '11 at 13:02
  • @Amir: Please post the error you get. And maybe also post the relevant code (please not in a comment, add it to your question). – Björn Pollex Jan 17 '11 at 13:08
1

Does it really make sense that the information is part of the visitor?

My guess is that the information logically belongs with the graph, and should be stored there, not in the visitor.

You can store a reference to the graph in the visitor. Then, as the visitor traverses the graph, it can update the information that is stored with the graph.

The result is that it is OK to destroy the visitor when its work is done, because the results will persist as part of the graph.

ravenspoint
  • 19,093
  • 6
  • 57
  • 103
  • I can't change the graph class, so I can't add new data to it. Also, the graph exists throughout the program and the data I need from the visitor is only needed in one function and then it is deleted. – Amir Rachum Jan 17 '11 at 13:00
  • @Amir. There are two possibilities. You could create a new specialization of the graph class which inherits the graph behavior and adds the new behavior that you need. Alternatively, you can create a brand new class that contains your 'one function' and the data it needs, then you construct an instance of the new class and store a reference to it in your visitor. IMHO either of these would be a more logical way of arranging things which would be easier to understand and maintain. Plus they do not 'break' the visitor design by using obscure boost syntax! – ravenspoint Jan 17 '11 at 14:24