Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/graph/example/last-mod-time.cpp @ 29

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

updated boost from 1_33_1 to 1_34_1

File size: 3.3 KB
Line 
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 <unistd.h>
13#include <sys/stat.h>
14#include <boost/graph/adjacency_list.hpp>
15
16using namespace boost;
17
18template < typename Graph, typename VertexNamePropertyMap > void
19read_graph_file(std::istream & graph_in, std::istream & name_in,
20                Graph & g, VertexNamePropertyMap name_map)
21{
22  typedef typename graph_traits < Graph >::vertices_size_type size_type;
23  size_type n_vertices;
24  typename graph_traits < Graph >::vertex_descriptor u;
25  typename property_traits < VertexNamePropertyMap >::value_type name;
26
27  graph_in >> n_vertices;       // read in number of vertices
28  for (size_type i = 0; i < n_vertices; ++i) {  // Add n vertices to the graph
29    u = add_vertex(g);
30    name_in >> name;
31    put(name_map, u, name);     // ** Attach name property to vertex u **
32  }
33  size_type src, targ;
34  while (graph_in >> src)       // Read in edges
35    if (graph_in >> targ)
36      add_edge(src, targ, g);   // add an edge to the graph
37    else
38      break;
39}
40
41
42int
43main()
44{
45  typedef adjacency_list < listS,       // Store out-edges of each vertex in a std::list
46    vecS,                       // Store vertex set in a std::vector
47    directedS,                  // The graph is directed
48    property < vertex_name_t, std::string >     // Add a vertex property
49   >graph_type;
50
51  graph_type g;                 // use default constructor to create empty graph
52  std::ifstream file_in("makefile-dependencies.dat"),
53    name_in("makefile-target-names.dat");
54  if (!file_in) {
55    std::cerr << "** Error: could not open file makefile-target-names.dat"
56      << std::endl;
57    exit(-1);
58  }
59  // Obtain internal property map from the graph
60  property_map < graph_type, vertex_name_t >::type name_map =
61    get(vertex_name, g);
62  read_graph_file(file_in, name_in, g, name_map);
63
64  // Create storage for last modified times
65  std::vector < time_t > last_mod_vec(num_vertices(g));
66  // Create nickname for the property map type
67  typedef iterator_property_map < std::vector < time_t >::iterator,
68    property_map < graph_type, vertex_index_t >::type, time_t, time_t&> iter_map_t;
69  // Create last modified time property map
70  iter_map_t mod_time_map(last_mod_vec.begin(), get(vertex_index, g));
71
72  property_map < graph_type, vertex_name_t >::type name = get(vertex_name, g);
73  struct stat stat_buf;
74  graph_traits < graph_type >::vertex_descriptor u;
75  typedef graph_traits < graph_type >::vertex_iterator vertex_iter_t;
76  std::pair < vertex_iter_t, vertex_iter_t > p;
77  for (p = vertices(g); p.first != p.second; ++p.first) {
78    u = *p.first;
79    if (stat(name[u].c_str(), &stat_buf) != 0)
80      std::cerr << "error in stat() for file " << name[u] << std::endl;
81    put(mod_time_map, u, stat_buf.st_mtime);
82  }
83
84  for (p = vertices(g); p.first != p.second; ++p.first) {
85    std::cout << name[*p.first] << " was last modified at "
86      << ctime(&mod_time_map[*p.first]);
87  }
88  assert(num_vertices(g) == 15);
89  assert(num_edges(g) == 19);
90  return 0;
91}
Note: See TracBrowser for help on using the repository browser.