edge-function.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //=======================================================================
  2. // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //=======================================================================
  8. #include <boost/config.hpp>
  9. #include <iostream>
  10. #include <fstream>
  11. #include <string>
  12. #include <boost/graph/adjacency_list.hpp>
  13. using namespace boost;
  14. template < typename Graph, typename VertexNamePropertyMap > void
  15. read_graph_file(std::istream & graph_in, std::istream & name_in,
  16. Graph & g, VertexNamePropertyMap name_map)
  17. {
  18. typedef typename graph_traits < Graph >::vertices_size_type size_type;
  19. size_type n_vertices;
  20. typename graph_traits < Graph >::vertex_descriptor u;
  21. typename property_traits < VertexNamePropertyMap >::value_type name;
  22. graph_in >> n_vertices; // read in number of vertices
  23. for (size_type i = 0; i < n_vertices; ++i) { // Add n vertices to the graph
  24. u = add_vertex(g);
  25. name_in >> name;
  26. put(name_map, u, name); // ** Attach name property to vertex u **
  27. }
  28. size_type src, targ;
  29. while (graph_in >> src) // Read in edges
  30. if (graph_in >> targ)
  31. add_edge(src, targ, g); // add an edge to the graph
  32. else
  33. break;
  34. }
  35. template < typename Graph, typename VertexNameMap > void
  36. output_adjacent_vertices(std::ostream & out,
  37. typename graph_traits < Graph >::vertex_descriptor u,
  38. const Graph & g, VertexNameMap name_map)
  39. {
  40. typename graph_traits < Graph >::adjacency_iterator vi, vi_end;
  41. out << get(name_map, u) << " -> { ";
  42. for (boost::tie(vi, vi_end) = adjacent_vertices(u, g); vi != vi_end; ++vi)
  43. out << get(name_map, *vi) << " ";
  44. out << "}" << std::endl;
  45. }
  46. template < typename NameMap > class name_equals_t {
  47. public:
  48. name_equals_t(const std::string & n, NameMap map)
  49. : m_name(n), m_name_map(map)
  50. {
  51. }
  52. template < typename Vertex > bool operator()(Vertex u) const
  53. {
  54. return get(m_name_map, u) == m_name;
  55. }
  56. private:
  57. std::string m_name;
  58. NameMap m_name_map;
  59. };
  60. // object generator function
  61. template < typename NameMap >
  62. inline name_equals_t < NameMap >
  63. name_equals(const std::string & str, NameMap name)
  64. {
  65. return name_equals_t < NameMap > (str, name);
  66. }
  67. int
  68. main(int argc, const char** argv)
  69. {
  70. typedef adjacency_list<listS,// Store out-edges of each vertex in a std::list
  71. vecS, // Store vertex set in a std::vector
  72. directedS, // The graph is directed
  73. property < vertex_name_t, std::string > // Add a vertex property
  74. >graph_type;
  75. graph_type g; // use default constructor to create empty graph
  76. const char* dep_file_name = argc >= 2 ? argv[1] : "makefile-dependencies.dat";
  77. const char* target_file_name = argc >= 3 ? argv[2] : "makefile-target-names.dat";
  78. std::ifstream file_in(dep_file_name), name_in(target_file_name);
  79. if (!file_in) {
  80. std::cerr << "** Error: could not open file " << dep_file_name
  81. << std::endl;
  82. return -1;
  83. }
  84. if (!name_in) {
  85. std::cerr << "** Error: could not open file " << target_file_name
  86. << std::endl;
  87. return -1;
  88. }
  89. // Obtain internal property map from the graph
  90. property_map < graph_type, vertex_name_t >::type name_map =
  91. get(vertex_name, g);
  92. read_graph_file(file_in, name_in, g, name_map);
  93. graph_traits < graph_type >::vertex_descriptor yow, zag, bar;
  94. // Get vertex name property map from the graph
  95. typedef property_map < graph_type, vertex_name_t >::type name_map_t;
  96. name_map_t name = get(vertex_name, g);
  97. // Get iterators for the vertex set
  98. graph_traits < graph_type >::vertex_iterator i, end;
  99. boost::tie(i, end) = vertices(g);
  100. // Find yow.h
  101. name_equals_t < name_map_t > predicate1("yow.h", name);
  102. yow = *std::find_if(i, end, predicate1);
  103. // Find zag.o
  104. name_equals_t < name_map_t > predicate2("zag.o", name);
  105. zag = *std::find_if(i, end, predicate2);
  106. // Find bar.o
  107. name_equals_t < name_map_t > predicate3("bar.o", name);
  108. bar = *std::find_if(i, end, predicate3);
  109. graph_traits < graph_type >::edge_descriptor e1, e2;
  110. bool exists;
  111. // Get the edge connecting yow.h to zag.o
  112. boost::tie(e1, exists) = edge(yow, zag, g);
  113. assert(exists == true);
  114. assert(source(e1, g) == yow);
  115. assert(target(e1, g) == zag);
  116. // Discover that there is no edge connecting zag.o to bar.o
  117. boost::tie(e2, exists) = edge(zag, bar, g);
  118. assert(exists == false);
  119. assert(num_vertices(g) == 15);
  120. assert(num_edges(g) == 19);
  121. return EXIT_SUCCESS;
  122. }