This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 19th, 2012  |  syntax: C++  |  size: 2.78 KB  |  views: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <boost/config.hpp>
  2. #include <iostream>                         // for std::cout
  3. #include <utility>                          // for std::pair
  4. #include <algorithm>                        // for std::for_each
  5. #include <boost/utility.hpp>                // for boost::tie
  6. #include <boost/graph/adjacency_list.hpp>
  7. #include <boost/graph/graphviz.hpp>
  8.  
  9.  
  10.  
  11. #include <boost/range/begin.hpp>
  12. #include <boost/range/end.hpp>
  13.  
  14. using namespace boost;
  15.  
  16.  
  17. #include <boost/range/iterator_range.hpp>
  18. #include <type_traits>
  19.  
  20. template<class T> using Invoke = typename T::type
  21. template<class T> using RemoveRef = Invoke<std::remove_reference>;
  22. template<class G> using OutEdgeIterator = typename boost::graph_traits<G>::out_edge_iterator;
  23.  
  24. template<class V, class G>
  25. auto out_edges_range(V&& v, G&& g)
  26.   -> boost::iterator_range<OutEdgeIterator<RemoveRef<G>>>
  27. {
  28.   auto edge_pair = out_edges(std::forward<V>(v), std::forward<G>(g));
  29.   return boost::make_iterator_range(edge_pair.first, edge_pair.second);
  30. }
  31.  
  32.  
  33.  
  34. template <class Graph> struct exercise_vertex {
  35.   exercise_vertex(Graph& g_, const char name_[]) : g(g_),name(name_) { }
  36.   typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  37.   void operator()(const Vertex& v) const
  38.   {
  39.     using namespace boost;
  40.     typename property_map<Graph, vertex_index_t>::type
  41.       vertex_id = get(vertex_index, g);
  42.     std::cout << "vertex: " << name[get(vertex_id, v)] << std::endl;
  43.  
  44.     std::cout << "\tout-edges: ";
  45.  
  46.     /*for (auto &e : out_edges(v, g)){
  47.         std::cout<<"!";
  48.     }
  49.     //*/
  50.  
  51.     typename boost::graph_traits<Graph>::out_edge_iterator out_i, out_end;
  52.     typename boost::graph_traits<Graph>::edge_descriptor e;
  53.     for (std::tie(out_i, out_end) = out_edges(v, g);
  54.          out_i != out_end; ++out_i)
  55.     {
  56.       e = *out_i;
  57.       Vertex src = source(e, g), targ = target(e, g);
  58.       std::cout << "(" << name[get(vertex_id, src)]
  59.                 << "," << name[get(vertex_id, targ)] << ") ";
  60.     }
  61.     std::cout << std::endl;
  62.  
  63.   }
  64.   Graph& g;
  65.   const char *name;
  66. };
  67.  
  68.  
  69. int main(int,char*[])
  70. {
  71.   typedef adjacency_list<vecS, vecS, bidirectionalS,
  72.      no_property, property<edge_weight_t, float> > Graph;
  73.  
  74.   enum { A, B, C, D, E, N };
  75.   const int num_vertices = N;
  76.   const char name[] = "ABCDE";
  77.  
  78.   typedef std::pair<int,int> Edge;
  79.   Edge edge_array[] =
  80.   { Edge(A,B), Edge(A,D), Edge(C,A), Edge(D,C),
  81.     Edge(C,E), Edge(B,D), Edge(D,E), };
  82.   const int num_edges = sizeof(edge_array)/sizeof(edge_array[0]);
  83.  
  84.   float transmission_delay[] = { 1.2, 4.5, 2.6, 0.4, 5.2, 1.8, 3.3, 9.1 };
  85.  
  86.  
  87.   Graph g(edge_array, edge_array + num_edges,
  88.           transmission_delay, num_vertices);
  89.  
  90.   std::for_each(vertices(g).first, vertices(g).second,
  91.                 exercise_vertex<Graph>(g, name));
  92.  
  93.  
  94.   return 0;
  95. }
clone this paste RAW Paste Data