File: named_vertices_test.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (95 lines) | stat: -rw-r--r-- 2,769 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (C) 2007-2008 The Trustees of Indiana University.

// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

// Test support for named vertices in adjacency_list.
#include <boost/config.hpp>
#include <boost/throw_exception.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/iteration_macros.hpp>
#include <string>
#include <iostream>

#ifdef BOOST_NO_EXCEPTIONS
void boost::throw_exception(std::exception const& ex)
{
    std::cout << ex.what() << std::endl;
    abort();
}
#endif

using namespace boost;

/// City structure to be attached to each vertex
struct City
{
    City() {}

    City(const std::string& name, int population = -1)
    : name(name), population(population)
    {
    }

    std::string name;
    int population;
};

namespace boost
{
namespace graph
{

    /// Use the City name as a key for indexing cities in a graph
    template <> struct internal_vertex_name< City >
    {
        typedef multi_index::member< City, std::string, &City::name > type;
    };

    /// Allow the graph to build cities given only their names (filling in
    /// the defaults for fields).
    template <> struct internal_vertex_constructor< City >
    {
        typedef vertex_from_name< City > type;
    };

}
} // end namespace boost::graph

/// Our road map, where each of the vertices are cities
typedef adjacency_list< vecS, vecS, directedS, City > RoadMap;
typedef graph_traits< RoadMap >::vertex_descriptor Vertex;

int main(int, char*[])
{
    RoadMap map;

    /// Create vertices for Bloomington, Indianapolis, Chicago
    Vertex bloomington = add_vertex(City("Bloomington", 69291), map);
    Vertex indianapolis = add_vertex(City("Indianapolis", 791926), map);
    Vertex chicago = add_vertex(City("Chicago", 9500000), map);

    BOOST_TEST(add_vertex(City("Bloomington", 69291), map) == bloomington);

    BGL_FORALL_VERTICES(city, map, RoadMap)
    std::cout << map[city].name << ", population " << map[city].population
              << std::endl;

    BOOST_TEST(*find_vertex("Bloomington", map) == bloomington);
    BOOST_TEST(*find_vertex("Indianapolis", map) == indianapolis);
    BOOST_TEST(*find_vertex("Chicago", map) == chicago);

    add_edge(bloomington, "Indianapolis", map);
    add_edge("Indianapolis", chicago, map);
    add_edge("Indianapolis", "Cincinnatti", map);

    BGL_FORALL_EDGES(road, map, RoadMap)
    std::cout << map[source(road, map)].name << " -> "
              << map[target(road, map)].name << std::endl;

    BOOST_TEST(map[*find_vertex("Cincinnatti", map)].population == -1);

    return boost::report_errors();
}