*** depth_first_search.hpp.orig Thu Aug 28 11:34:20 2003 --- depth_first_search.hpp Sun Aug 31 23:14:58 2003 *************** *** 50,55 **** --- 50,56 ---- #include #include + #include namespace boost { *************** *** 74,81 **** --- 75,97 ---- typename graph_traits::edge_descriptor e; }; + template + class dfs_buffer_traits { + typedef typename graph_traits::vertex_descriptor Vertex; + typedef typename graph_traits::out_edge_iterator Iter; + public: + typedef std::pair > value_type; + }; + namespace detail { + template + class default_dfs_buffer { + typedef typename dfs_buffer_traits::value_type BufferValueType; + public: + typedef std::stack > type; + }; + struct nontruth2 { template bool operator()(const T&, const T2&) const { return false; } *************** *** 99,110 **** // See http://lists.boost.org/MailArchives/boost/msg48752.php for FAQ. template void depth_first_visit_impl (const IncidenceGraph& g, typename graph_traits::vertex_descriptor u, ! DFSVisitor& vis, ! ColorMap color, TerminatorFunc func = TerminatorFunc()) { function_requires >(); function_requires >(); --- 115,126 ---- // See http://lists.boost.org/MailArchives/boost/msg48752.php for FAQ. template void depth_first_visit_impl (const IncidenceGraph& g, typename graph_traits::vertex_descriptor u, ! DFSVisitor& vis, ColorMap color, Buffer& S, ! TerminatorFunc func = TerminatorFunc()) { function_requires >(); function_requires >(); *************** *** 114,126 **** function_requires< ColorValueConcept >(); typedef color_traits Color; typedef typename graph_traits::out_edge_iterator Iter; ! typedef std::pair > VertexInfo; Iter ei, ei_end; - std::vector stack; - - // Possible optimization for vector - //stack.reserve(num_vertices(g)); typedef typename unwrap_reference::type TF; --- 130,138 ---- function_requires< ColorValueConcept >(); typedef color_traits Color; typedef typename graph_traits::out_edge_iterator Iter; ! typedef typename Buffer::value_type VertexInfo; Iter ei, ei_end; typedef typename unwrap_reference::type TF; *************** *** 128,150 **** vis.discover_vertex(u, g); tie(ei, ei_end) = out_edges(u, g); if (static_cast(func)(u, g)) { ! // If this vertex terminates the search, we push empty range ! stack.push_back(std::make_pair(u, std::make_pair(ei_end, ei_end))); } else { ! stack.push_back(std::make_pair(u, std::make_pair(ei, ei_end))); } ! while (!stack.empty()) { ! VertexInfo& back = stack.back(); u = back.first; tie(ei, ei_end) = back.second; ! stack.pop_back(); while (ei != ei_end) { Vertex v = target(*ei, g); vis.examine_edge(*ei, g); ColorValue v_color = get(color, v); if (v_color == Color::white()) { vis.tree_edge(*ei, g); ! stack.push_back(std::make_pair(u, std::make_pair(++ei, ei_end))); u = v; put(color, u, Color::gray()); vis.discover_vertex(u, g); --- 140,162 ---- vis.discover_vertex(u, g); tie(ei, ei_end) = out_edges(u, g); if (static_cast(func)(u, g)) { ! // If this vertex terminates the search, we push empty range ! S.push(std::make_pair(u, std::make_pair(ei_end, ei_end))); } else { ! S.push(std::make_pair(u, std::make_pair(ei, ei_end))); } ! while (!S.empty()) { ! VertexInfo& back = S.top(); u = back.first; tie(ei, ei_end) = back.second; ! S.pop(); while (ei != ei_end) { Vertex v = target(*ei, g); vis.examine_edge(*ei, g); ColorValue v_color = get(color, v); if (v_color == Color::white()) { vis.tree_edge(*ei, g); ! S.push(std::make_pair(u, std::make_pair(++ei, ei_end))); u = v; put(color, u, Color::gray()); vis.discover_vertex(u, g); *************** *** 168,179 **** #else // BOOST_RECURSIVE_DFS is defined template void depth_first_visit_impl (const IncidenceGraph& g, typename graph_traits::vertex_descriptor u, DFSVisitor& vis, // pass-by-reference here, important! ! ColorMap color, TerminatorFunc func) { function_requires >(); function_requires >(); --- 180,191 ---- #else // BOOST_RECURSIVE_DFS is defined template void depth_first_visit_impl (const IncidenceGraph& g, typename graph_traits::vertex_descriptor u, DFSVisitor& vis, // pass-by-reference here, important! ! ColorMap color, Buffer& S, TerminatorFunc func) { function_requires >(); function_requires >(); *************** *** 192,198 **** Vertex v = target(*ei, g); vis.examine_edge(*ei, g); ColorValue v_color = get(color, v); if (v_color == Color::white()) { vis.tree_edge(*ei, g); ! depth_first_visit_impl(g, v, vis, color, func); } else if (v_color == Color::gray()) vis.back_edge(*ei, g); else vis.forward_or_cross_edge(*ei, g); } --- 204,210 ---- Vertex v = target(*ei, g); vis.examine_edge(*ei, g); ColorValue v_color = get(color, v); if (v_color == Color::white()) { vis.tree_edge(*ei, g); ! depth_first_visit_impl(g, v, vis, color, S, func); } else if (v_color == Color::gray()) vis.back_edge(*ei, g); else vis.forward_or_cross_edge(*ei, g); } *************** *** 204,213 **** } // namespace detail template void depth_first_search(const VertexListGraph& g, DFSVisitor vis, ColorMap color, ! Vertex start_vertex) { function_requires >(); typedef typename property_traits::value_type ColorValue; --- 216,225 ---- } // namespace detail template void depth_first_search(const VertexListGraph& g, DFSVisitor vis, ColorMap color, ! Vertex start_vertex, Buffer& S) { function_requires >(); typedef typename property_traits::value_type ColorValue; *************** *** 219,236 **** } if (start_vertex != *vertices(g).first){ vis.start_vertex(start_vertex, g); ! detail::depth_first_visit_impl(g, start_vertex, vis, color, detail::nontruth2()); } for (tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui) { ColorValue u_color = get(color, *ui); if (u_color == Color::white()) { vis.start_vertex(*ui, g); ! detail::depth_first_visit_impl(g, *ui, vis, color, detail::nontruth2()); } } } template void depth_first_search(const VertexListGraph& g, DFSVisitor vis, ColorMap color) --- 231,259 ---- } if (start_vertex != *vertices(g).first){ vis.start_vertex(start_vertex, g); ! detail::depth_first_visit_impl(g, start_vertex, vis, color, S, detail::nontruth2()); } for (tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui) { ColorValue u_color = get(color, *ui); if (u_color == Color::white()) { vis.start_vertex(*ui, g); ! detail::depth_first_visit_impl(g, *ui, vis, color, S, ! detail::nontruth2()); } } } + template + void + depth_first_search(const VertexListGraph& g, DFSVisitor vis, ColorMap color, + Vertex start_vertex) + { + typename detail::default_dfs_buffer::type S; + depth_first_search(g, vis, color, start_vertex, S); + } + template void depth_first_search(const VertexListGraph& g, DFSVisitor vis, ColorMap color) *************** *** 243,252 **** struct dfs_dispatch { template static void apply(const VertexListGraph& g, DFSVisitor vis, Vertex start_vertex, ! const bgl_named_params&, ColorMap color) { depth_first_search(g, vis, color, start_vertex); --- 266,275 ---- struct dfs_dispatch { template static void apply(const VertexListGraph& g, DFSVisitor vis, Vertex start_vertex, ! Buffer& S, const bgl_named_params&, ColorMap color) { depth_first_search(g, vis, color, start_vertex); *************** *** 256,265 **** template <> struct dfs_dispatch { template static void apply(const VertexListGraph& g, DFSVisitor vis, Vertex start_vertex, ! const bgl_named_params& params, detail::error_property_not_found) { std::vector color_vec(num_vertices(g)); --- 279,288 ---- template <> struct dfs_dispatch { template static void apply(const VertexListGraph& g, DFSVisitor vis, Vertex start_vertex, ! Buffer& S, const bgl_named_params& params, detail::error_property_not_found) { std::vector color_vec(num_vertices(g)); *************** *** 269,275 **** (color_vec.begin(), choose_const_pmap(get_param(params, vertex_index), g, vertex_index), c), ! start_vertex); } }; } // namespace detail --- 292,298 ---- (color_vec.begin(), choose_const_pmap(get_param(params, vertex_index), g, vertex_index), c), ! start_vertex, S); } }; } // namespace detail *************** *** 342,353 **** --- 365,384 ---- { typedef typename property_value< bgl_named_params, vertex_color_t>::type C; + + typedef typename detail::default_dfs_buffer::type + stack_t; + stack_t S; + detail::wrap_ref Sref(S); + detail::dfs_dispatch::apply (g, choose_param(get_param(params, graph_visitor), make_dfs_visitor(null_visitor())), choose_param(get_param(params, root_vertex_t()), *vertices(g).first), + choose_param(get_param(params, buffer_param_t()), + Sref).ref, params, get_param(params, vertex_color) ); *************** *** 359,366 **** typename graph_traits::vertex_descriptor u, DFSVisitor vis, ColorMap color) { vis.start_vertex(u, g); ! detail::depth_first_visit_impl(g, u, vis, color, detail::nontruth2()); } template ::vertex_descriptor u, DFSVisitor vis, ColorMap color) { + typename detail::default_dfs_buffer::type S; vis.start_vertex(u, g); ! detail::depth_first_visit_impl(g, u, vis, color, S, detail::nontruth2()); } template ::vertex_descriptor u, DFSVisitor vis, ColorMap color, TerminatorFunc func = TerminatorFunc()) { vis.start_vertex(u, g); ! detail::depth_first_visit_impl(g, u, vis, color, func); } --- 402,410 ---- typename graph_traits::vertex_descriptor u, DFSVisitor vis, ColorMap color, TerminatorFunc func = TerminatorFunc()) { + typename detail::default_dfs_buffer::type S; vis.start_vertex(u, g); ! detail::depth_first_visit_impl(g, u, vis, color, S, func); }