Chris@16: //======================================================================= Chris@16: // Copyright 2001 Indiana University Chris@16: // Author: Jeremy G. Siek Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: //======================================================================= Chris@16: Chris@16: #ifndef BOOST_GRAPH_ITERATION_MACROS_HPP Chris@16: #define BOOST_GRAPH_ITERATION_MACROS_HPP Chris@16: Chris@16: #include Chris@16: Chris@16: #define BGL_CAT(x,y) x ## y Chris@16: #define BGL_RANGE(linenum) BGL_CAT(bgl_range_,linenum) Chris@16: #define BGL_FIRST(linenum) (BGL_RANGE(linenum).first) Chris@16: #define BGL_LAST(linenum) (BGL_RANGE(linenum).second) Chris@16: Chris@16: /* Chris@16: BGL_FORALL_VERTICES_T(v, g, graph_t) // This is on line 9 Chris@16: expands to the following, but all on the same line Chris@16: Chris@16: for (typename boost::graph_traits::vertex_iterator Chris@16: bgl_first_9 = vertices(g).first, bgl_last_9 = vertices(g).second; Chris@16: bgl_first_9 != bgl_last_9; bgl_first_9 = bgl_last_9) Chris@16: for (typename boost::graph_traits::vertex_descriptor v; Chris@16: bgl_first_9 != bgl_last_9 ? (v = *bgl_first_9, true) : false; Chris@16: ++bgl_first_9) Chris@16: Chris@16: The purpose of having two for-loops is just to provide a place to Chris@16: declare both the iterator and value variables. There is really only Chris@16: one loop. The stopping condition gets executed two more times than it Chris@16: usually would be, oh well. The reason for the bgl_first_9 = bgl_last_9 Chris@16: in the outer for-loop is in case the user puts a break statement Chris@16: in the inner for-loop. Chris@16: Chris@16: The other macros work in a similar fashion. Chris@16: Chris@16: Use the _T versions when the graph type is a template parameter or Chris@16: dependent on a template parameter. Otherwise use the non _T versions. Chris@16: Chris@16: ----------------------- Chris@16: 6/9/09 THK Chris@16: Chris@16: The above contains two calls to the vertices function. I modified these Chris@16: macros to expand to Chris@16: Chris@16: for (std::pair::vertex_iterator, Chris@16: typename boost::graph_traits::vertex_iterator> bgl_range_9 = vertices(g); Chris@16: bgl_range_9.first != bgl_range_9.second; Chris@16: bgl_range_9.first = bgl_range_9.second) Chris@16: for (typename boost::graph_traits::vertex_descriptor v; Chris@16: bgl_range_9.first != bgl_range_9.second ? (v = *bgl_range_9.first, true) : false; Chris@16: ++bgl_range_9.first) Chris@16: Chris@16: */ Chris@16: Chris@16: Chris@16: #define BGL_FORALL_VERTICES_T(VNAME, GNAME, GraphType) \ Chris@16: for (std::pair::vertex_iterator, \ Chris@16: typename boost::graph_traits::vertex_iterator> BGL_RANGE(__LINE__) = vertices(GNAME); \ Chris@16: BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \ Chris@16: for (typename boost::graph_traits::vertex_descriptor VNAME; \ Chris@16: BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (VNAME = *BGL_FIRST(__LINE__), true):false; \ Chris@16: ++BGL_FIRST(__LINE__)) Chris@16: Chris@16: #define BGL_FORALL_VERTICES(VNAME, GNAME, GraphType) \ Chris@16: for (std::pair::vertex_iterator, \ Chris@16: boost::graph_traits::vertex_iterator> BGL_RANGE(__LINE__) = vertices(GNAME); \ Chris@16: BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \ Chris@16: for (boost::graph_traits::vertex_descriptor VNAME; \ Chris@16: BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (VNAME = *BGL_FIRST(__LINE__), true):false; \ Chris@16: ++BGL_FIRST(__LINE__)) Chris@16: Chris@16: #define BGL_FORALL_EDGES_T(ENAME, GNAME, GraphType) \ Chris@16: for (std::pair::edge_iterator, \ Chris@16: typename boost::graph_traits::edge_iterator> BGL_RANGE(__LINE__) = edges(GNAME); \ Chris@16: BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \ Chris@16: for (typename boost::graph_traits::edge_descriptor ENAME; \ Chris@16: BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (ENAME = *BGL_FIRST(__LINE__), true):false; \ Chris@16: ++BGL_FIRST(__LINE__)) Chris@16: Chris@16: #define BGL_FORALL_EDGES(ENAME, GNAME, GraphType) \ Chris@16: for (std::pair::edge_iterator, \ Chris@16: boost::graph_traits::edge_iterator> BGL_RANGE(__LINE__) = edges(GNAME); \ Chris@16: BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \ Chris@16: for (boost::graph_traits::edge_descriptor ENAME; \ Chris@16: BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (ENAME = *BGL_FIRST(__LINE__), true):false; \ Chris@16: ++BGL_FIRST(__LINE__)) Chris@16: Chris@16: #define BGL_FORALL_ADJ_T(UNAME, VNAME, GNAME, GraphType) \ Chris@16: for (std::pair::adjacency_iterator, \ Chris@16: typename boost::graph_traits::adjacency_iterator> BGL_RANGE(__LINE__) = adjacent_vertices(UNAME, GNAME); \ Chris@16: BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \ Chris@16: for (typename boost::graph_traits::vertex_descriptor VNAME; \ Chris@16: BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (VNAME = *BGL_FIRST(__LINE__), true) : false; \ Chris@16: ++BGL_FIRST(__LINE__)) Chris@16: Chris@16: #define BGL_FORALL_ADJ(UNAME, VNAME, GNAME, GraphType) \ Chris@16: for (std::pair::adjacency_iterator, \ Chris@16: boost::graph_traits::adjacency_iterator> BGL_RANGE(__LINE__) = adjacent_vertices(UNAME, GNAME); \ Chris@16: BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \ Chris@16: for (boost::graph_traits::vertex_descriptor VNAME; \ Chris@16: BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (VNAME = *BGL_FIRST(__LINE__), true) : false; \ Chris@16: ++BGL_FIRST(__LINE__)) Chris@16: Chris@16: #define BGL_FORALL_OUTEDGES_T(UNAME, ENAME, GNAME, GraphType) \ Chris@16: for (std::pair::out_edge_iterator, \ Chris@16: typename boost::graph_traits::out_edge_iterator> BGL_RANGE(__LINE__) = out_edges(UNAME, GNAME); \ Chris@16: BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \ Chris@16: for (typename boost::graph_traits::edge_descriptor ENAME; \ Chris@16: BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (ENAME = *BGL_FIRST(__LINE__), true) : false; \ Chris@16: ++BGL_FIRST(__LINE__)) Chris@16: Chris@16: #define BGL_FORALL_OUTEDGES(UNAME, ENAME, GNAME, GraphType) \ Chris@16: for (std::pair::out_edge_iterator, \ Chris@16: boost::graph_traits::out_edge_iterator> BGL_RANGE(__LINE__) = out_edges(UNAME, GNAME); \ Chris@16: BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \ Chris@16: for (boost::graph_traits::edge_descriptor ENAME; \ Chris@16: BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (ENAME = *BGL_FIRST(__LINE__), true) : false; \ Chris@16: ++BGL_FIRST(__LINE__)) Chris@16: Chris@16: #define BGL_FORALL_INEDGES_T(UNAME, ENAME, GNAME, GraphType) \ Chris@16: for (std::pair::in_edge_iterator, \ Chris@16: typename boost::graph_traits::in_edge_iterator> BGL_RANGE(__LINE__) = in_edges(UNAME, GNAME); \ Chris@16: BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \ Chris@16: for (typename boost::graph_traits::edge_descriptor ENAME; \ Chris@16: BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (ENAME = *BGL_FIRST(__LINE__), true) : false; \ Chris@16: ++BGL_FIRST(__LINE__)) Chris@16: Chris@16: #define BGL_FORALL_INEDGES(UNAME, ENAME, GNAME, GraphType) \ Chris@16: for (std::pair::in_edge_iterator, \ Chris@16: boost::graph_traits::in_edge_iterator> BGL_RANGE(__LINE__) = in_edges(UNAME, GNAME); \ Chris@16: BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \ Chris@16: for (boost::graph_traits::edge_descriptor ENAME; \ Chris@16: BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (ENAME = *BGL_FIRST(__LINE__), true) : false; \ Chris@16: ++BGL_FIRST(__LINE__)) Chris@16: Chris@16: #endif // BOOST_GRAPH_ITERATION_MACROS_HPP