Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/graph/example/vertex-name-property.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 2.8 KB
RevLine 
[29]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
14using namespace boost;
15
16template < typename Graph, typename VertexNamePropertyMap > void
17read_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
40int
41main()
42{
43  typedef adjacency_list < listS,       // Store out-edges of each vertex in a std::list
44    vecS,                       // Store vertex set in a std::vector
45    directedS,                  // The graph is directed
46    property < vertex_name_t, std::string >     // Add a vertex property
47   >graph_type;
48
49  graph_type g;                 // use default constructor to create empty graph
50  const char* dep_file_name = "makefile-dependencies.dat";
51  const char* target_file_name = "makefile-target-names.dat";
52  std::ifstream file_in(dep_file_name), name_in(target_file_name);
53  if (!file_in) {
54    std::cerr << "** Error: could not open file " << dep_file_name
55      << std::endl;
56    return -1;
57  }
58  if (!name_in) {
59    std::cerr << "** Error: could not open file " << target_file_name
60      << std::endl;
61    return -1;
62  }
63
64  // Obtain internal property map from the graph
65  property_map < graph_type, vertex_name_t >::type name_map =
66    get(vertex_name, g);
67  read_graph_file(file_in, name_in, g, name_map);
68
69  // Create storage for last modified times
70  std::vector < time_t > last_mod_vec(num_vertices(g));
71  // Create nickname for the property map type
72  typedef iterator_property_map < std::vector < time_t >::iterator,
73    property_map < graph_type, vertex_index_t >::type, time_t, time_t& > iter_map_t;
74  // Create last modified time property map
75  iter_map_t mod_time_map(last_mod_vec.begin(), get(vertex_index, g));
76
77  assert(num_vertices(g) == 15);
78  assert(num_edges(g) == 19);
79  return 0;
80}
Note: See TracBrowser for help on using the repository browser.