• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2007-2008 The Trustees of Indiana University.
2 
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #include <boost/graph/use_mpi.hpp>
8 #include <boost/config.hpp>
9 #include <boost/throw_exception.hpp>
10 #include <boost/test/minimal.hpp>
11 #include <boost/graph/adjacency_list.hpp>
12 #include <boost/graph/iteration_macros.hpp>
13 #include <string>
14 #include <iostream>
15 
16 #ifdef BOOST_NO_EXCEPTIONS
17 void
throw_exception(std::exception const & ex)18 boost::throw_exception(std::exception const& ex)
19 {
20     std::cout << ex.what() << std::endl;
21     abort();
22 }
23 #endif
24 
25 using namespace boost;
26 
27 /// City structure to be attached to each vertex
28 struct City {
CityCity29   City() {}
30 
CityCity31   City(const std::string& name, int population = -1)
32     : name(name), population(population) { }
33 
34   std::string name;
35   int population;
36 };
37 
38 namespace boost { namespace graph {
39 
40 /// Use the City name as a key for indexing cities in a graph
41 template<>
42 struct internal_vertex_name<City>
43 {
44   typedef multi_index::member<City, std::string, &City::name> type;
45 };
46 
47 /// Allow the graph to build cities given only their names (filling in
48 /// the defaults for fields).
49 template<>
50 struct internal_vertex_constructor<City>
51 {
52   typedef vertex_from_name<City> type;
53 };
54 
55 } } // end namespace boost::graph
56 
57 /// Our road map, where each of the vertices are cities
58 typedef adjacency_list<vecS, vecS, directedS, City> RoadMap;
59 typedef graph_traits<RoadMap>::vertex_descriptor Vertex;
60 
test_main(int argc,char * argv[])61 int test_main(int argc, char* argv[])
62 {
63   RoadMap map;
64 
65   /// Create vertices for Bloomington, Indianapolis, Chicago
66   Vertex bloomington = add_vertex(City("Bloomington", 69291), map);
67   Vertex indianapolis = add_vertex(City("Indianapolis", 791926), map);
68   Vertex chicago = add_vertex(City("Chicago", 9500000), map);
69 
70   BOOST_CHECK(add_vertex(City("Bloomington", 69291), map) == bloomington);
71 
72   BGL_FORALL_VERTICES(city, map, RoadMap)
73     std::cout << map[city].name << ", population " << map[city].population
74               << std::endl;
75 
76   BOOST_CHECK(*find_vertex("Bloomington", map) == bloomington);
77   BOOST_CHECK(*find_vertex("Indianapolis", map) == indianapolis);
78   BOOST_CHECK(*find_vertex("Chicago", map) == chicago);
79 
80   add_edge(bloomington, "Indianapolis", map);
81   add_edge("Indianapolis", chicago, map);
82   add_edge("Indianapolis", "Cincinnati", map);
83 
84   BGL_FORALL_EDGES(road, map, RoadMap)
85     std::cout << map[source(road, map)].name << " -> "
86               << map[target(road, map)].name << std::endl;
87 
88   BOOST_CHECK(map[*find_vertex("Cincinnati", map)].population == -1);
89 
90   return 0;
91 }
92