named_vertices_seq.cpp 2.6 KB

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