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