]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | //======================================================================= |
f67539c2 | 2 | // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, |
7c673cae FG |
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 | ||
f67539c2 TL |
16 | template < typename Graph, typename VertexNamePropertyMap > |
17 | void read_graph_file(std::istream& graph_in, std::istream& name_in, Graph& g, | |
18 | VertexNamePropertyMap name_map) | |
7c673cae | 19 | { |
f67539c2 TL |
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; | |
7c673cae | 24 | |
f67539c2 TL |
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; | |
7c673cae FG |
38 | } |
39 | ||
f67539c2 TL |
40 | template < typename Graph, typename VertexNameMap > |
41 | void output_in_edges(std::ostream& out, const Graph& g, | |
42 | typename graph_traits< Graph >::vertex_descriptor v, VertexNameMap name_map) | |
7c673cae | 43 | { |
f67539c2 TL |
44 | typename graph_traits< Graph >::in_edge_iterator ei, ei_end; |
45 | for (boost::tie(ei, ei_end) = in_edges(v, g); ei != ei_end; ++ei) | |
46 | out << get(name_map, source(*ei, g)) << " -> " | |
47 | << get(name_map, target(*ei, g)) << std::endl; | |
7c673cae FG |
48 | } |
49 | ||
f67539c2 TL |
50 | template < typename NameMap > class name_equals_t |
51 | { | |
7c673cae | 52 | public: |
f67539c2 TL |
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 | ||
7c673cae FG |
62 | private: |
63 | std::string m_name; | |
f67539c2 | 64 | NameMap m_name_map; |
7c673cae FG |
65 | }; |
66 | ||
67 | // object generator function | |
68 | template < typename NameMap > | |
f67539c2 TL |
69 | inline name_equals_t< NameMap > name_equals( |
70 | const std::string& str, NameMap name) | |
7c673cae | 71 | { |
f67539c2 | 72 | return name_equals_t< NameMap >(str, name); |
7c673cae FG |
73 | } |
74 | ||
f67539c2 | 75 | int main(int argc, const char** argv) |
7c673cae | 76 | { |
f67539c2 TL |
77 | typedef adjacency_list< listS, // Store out-edges of each vertex in a |
78 | // std::list | |
79 | vecS, // Store vertex set in a std::vector | |
80 | bidirectionalS, // The graph is directed, with both out-edges and | |
81 | // in-edges | |
82 | property< vertex_name_t, std::string > // Add a vertex property | |
83 | > | |
84 | graph_type; | |
7c673cae | 85 | |
f67539c2 TL |
86 | graph_type g; // use default constructor to create empty graph |
87 | const char* dep_file_name | |
88 | = argc >= 2 ? argv[1] : "makefile-dependencies.dat"; | |
89 | const char* target_file_name | |
90 | = argc >= 3 ? argv[2] : "makefile-target-names.dat"; | |
91 | std::ifstream file_in(dep_file_name), name_in(target_file_name); | |
92 | if (!file_in) | |
93 | { | |
94 | std::cerr << "** Error: could not open file " << dep_file_name | |
95 | << std::endl; | |
96 | return -1; | |
97 | } | |
98 | if (!name_in) | |
99 | { | |
100 | std::cerr << "** Error: could not open file " << target_file_name | |
101 | << std::endl; | |
102 | return -1; | |
103 | } | |
7c673cae | 104 | |
f67539c2 TL |
105 | // Obtain internal property map from the graph |
106 | property_map< graph_type, vertex_name_t >::type name_map | |
107 | = get(vertex_name, g); | |
108 | read_graph_file(file_in, name_in, g, name_map); | |
7c673cae | 109 | |
f67539c2 TL |
110 | graph_traits< graph_type >::vertex_iterator i, end; |
111 | boost::tie(i, end) = vertices(g); | |
112 | i = std::find_if(i, end, name_equals("libzigzag.a", get(vertex_name, g))); | |
113 | output_in_edges(std::cout, g, *i, get(vertex_name, g)); | |
114 | assert(num_vertices(g) == 15); | |
115 | assert(num_edges(g) == 19); | |
116 | return 0; | |
7c673cae | 117 | } |