lookup_edge.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. #ifndef BOOST_GRAPH_LOOKUP_EDGE_HPP
  10. #define BOOST_GRAPH_LOOKUP_EDGE_HPP
  11. #include <utility>
  12. #include <boost/config.hpp>
  13. #include <boost/utility/enable_if.hpp>
  14. #include <boost/graph/graph_traits.hpp>
  15. // lookup_edge: a function that acts like edge() but falls back to out_edges()
  16. // and a search when edge() is not provided.
  17. namespace boost
  18. {
  19. template < typename Graph >
  20. std::pair< typename boost::graph_traits< Graph >::edge_descriptor, bool >
  21. lookup_edge(typename boost::graph_traits< Graph >::vertex_descriptor src,
  22. typename boost::graph_traits< Graph >::vertex_descriptor tgt,
  23. const Graph& g,
  24. typename boost::enable_if< is_adjacency_matrix< Graph >, int >::type = 0)
  25. {
  26. return edge(src, tgt, g);
  27. }
  28. template < typename Graph >
  29. std::pair< typename boost::graph_traits< Graph >::edge_descriptor, bool >
  30. lookup_edge(typename boost::graph_traits< Graph >::vertex_descriptor src,
  31. typename boost::graph_traits< Graph >::vertex_descriptor tgt,
  32. const Graph& g,
  33. typename boost::disable_if< is_adjacency_matrix< Graph >, int >::type = 0)
  34. {
  35. typedef typename boost::graph_traits< Graph >::out_edge_iterator it;
  36. typedef typename boost::graph_traits< Graph >::edge_descriptor edesc;
  37. std::pair< it, it > oe = out_edges(src, g);
  38. for (; oe.first != oe.second; ++oe.first)
  39. {
  40. edesc e = *oe.first;
  41. if (target(e, g) == tgt)
  42. return std::make_pair(e, true);
  43. }
  44. return std::make_pair(edesc(), false);
  45. }
  46. }
  47. #endif // BOOST_GRAPH_LOOKUP_EDGE_HPP