1 //=======================================================================
2 // Copyright 2009 Trustees of Indiana University
3 // Author: Jeremiah Willcock
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //=======================================================================
9
10 #ifndef BOOST_GRAPH_LOOKUP_EDGE_HPP
11 #define BOOST_GRAPH_LOOKUP_EDGE_HPP
12
13 #include <utility>
14 #include <boost/config.hpp>
15 #include <boost/utility/enable_if.hpp>
16 #include <boost/graph/graph_traits.hpp>
17
18 // lookup_edge: a function that acts like edge() but falls back to out_edges()
19 // and a search when edge() is not provided.
20
21 namespace boost
22 {
23
24 template < typename Graph >
25 std::pair< typename boost::graph_traits< Graph >::edge_descriptor, bool >
lookup_edge(typename boost::graph_traits<Graph>::vertex_descriptor src,typename boost::graph_traits<Graph>::vertex_descriptor tgt,const Graph & g,typename boost::enable_if<is_adjacency_matrix<Graph>,int>::type=0)26 lookup_edge(typename boost::graph_traits< Graph >::vertex_descriptor src,
27 typename boost::graph_traits< Graph >::vertex_descriptor tgt,
28 const Graph& g,
29 typename boost::enable_if< is_adjacency_matrix< Graph >, int >::type = 0)
30 {
31 return edge(src, tgt, g);
32 }
33
34 template < typename Graph >
35 std::pair< typename boost::graph_traits< Graph >::edge_descriptor, bool >
lookup_edge(typename boost::graph_traits<Graph>::vertex_descriptor src,typename boost::graph_traits<Graph>::vertex_descriptor tgt,const Graph & g,typename boost::disable_if<is_adjacency_matrix<Graph>,int>::type=0)36 lookup_edge(typename boost::graph_traits< Graph >::vertex_descriptor src,
37 typename boost::graph_traits< Graph >::vertex_descriptor tgt,
38 const Graph& g,
39 typename boost::disable_if< is_adjacency_matrix< Graph >, int >::type = 0)
40 {
41 typedef typename boost::graph_traits< Graph >::out_edge_iterator it;
42 typedef typename boost::graph_traits< Graph >::edge_descriptor edesc;
43 std::pair< it, it > oe = out_edges(src, g);
44 for (; oe.first != oe.second; ++oe.first)
45 {
46 edesc e = *oe.first;
47 if (target(e, g) == tgt)
48 return std::make_pair(e, true);
49 }
50 return std::make_pair(edesc(), false);
51 }
52
53 }
54
55 #endif // BOOST_GRAPH_LOOKUP_EDGE_HPP
56