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