1 // Copyright (C) 2007-2008 The Trustees of Indiana University.
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)
7 #include <boost/graph/use_mpi.hpp>
8 #include <boost/config.hpp>
9 #include <boost/throw_exception.hpp>
10 #include <boost/core/lightweight_test.hpp>
11 #include <boost/graph/adjacency_list.hpp>
12 #include <boost/graph/iteration_macros.hpp>
16 #ifdef BOOST_NO_EXCEPTIONS
18 boost::throw_exception(std::exception
const& ex
)
20 std::cout
<< ex
.what() << std::endl
;
25 using namespace boost
;
27 /// City structure to be attached to each vertex
31 City(const std::string
& name
, int population
= -1)
32 : name(name
), population(population
) { }
38 namespace boost
{ namespace graph
{
40 /// Use the City name as a key for indexing cities in a graph
42 struct internal_vertex_name
<City
>
44 typedef multi_index::member
<City
, std::string
, &City::name
> type
;
47 /// Allow the graph to build cities given only their names (filling in
48 /// the defaults for fields).
50 struct internal_vertex_constructor
<City
>
52 typedef vertex_from_name
<City
> type
;
55 } } // end namespace boost::graph
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
;
61 int main(int argc
, char* argv
[])
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
);
70 BOOST_TEST(add_vertex(City("Bloomington", 69291), map
) == bloomington
);
72 BGL_FORALL_VERTICES(city
, map
, RoadMap
)
73 std::cout
<< map
[city
].name
<< ", population " << map
[city
].population
76 BOOST_TEST(*find_vertex("Bloomington", map
) == bloomington
);
77 BOOST_TEST(*find_vertex("Indianapolis", map
) == indianapolis
);
78 BOOST_TEST(*find_vertex("Chicago", map
) == chicago
);
80 add_edge(bloomington
, "Indianapolis", map
);
81 add_edge("Indianapolis", chicago
, map
);
82 add_edge("Indianapolis", "Cincinnati", map
);
84 BGL_FORALL_EDGES(road
, map
, RoadMap
)
85 std::cout
<< map
[source(road
, map
)].name
<< " -> "
86 << map
[target(road
, map
)].name
<< std::endl
;
88 BOOST_TEST(map
[*find_vertex("Cincinnati", map
)].population
== -1);
90 return boost::report_errors();