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