File: index_graph.cpp

package info (click to toggle)
boost1.42 1.42.0-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 277,864 kB
  • ctags: 401,076
  • sloc: cpp: 1,235,659; xml: 74,142; ansic: 41,313; python: 26,756; sh: 11,840; cs: 2,118; makefile: 655; perl: 494; yacc: 456; asm: 353; csh: 6
file content (94 lines) | stat: -rw-r--r-- 2,570 bytes parent folder | download
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
// (C) Copyright 2007-2009 Andrew Sutton
//
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0 (See accompanying file
// LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)

#include <iostream>
#include <boost/graph/undirected_graph.hpp>
#include <boost/graph/directed_graph.hpp>

using namespace std;
using namespace boost;

template <typename Graph>
void test()
{
    typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
    typedef typename property_map<Graph, vertex_index_t>::type IndexMap;

    static const size_t N = 5;

    Graph g;
    IndexMap x = get(vertex_index, g);

    // build up the graph
    Vertex v[N];
    for(size_t i = 0; i < N; ++i) {
        v[i] = add_vertex(g);
    }

    // after the first build, we should have these conditions
    BOOST_ASSERT(max_vertex_index(g) == N);
    for(size_t i = 0; i < N; ++i) {
        BOOST_ASSERT(get_vertex_index(v[i], g) == i);
    }

    // Remove all vertices and then re-add them.
    for(size_t i = 0; i < N; ++i) remove_vertex(v[i], g);
    BOOST_ASSERT(num_vertices(g) == 0);
    for(size_t i = 0; i < N; ++i) {
        v[i] = add_vertex(g);
    }
    BOOST_ASSERT(num_vertices(g) == N);

    // Before renumbering, our vertices should be off by N. In other words,
    // we're not in a good state.
    BOOST_ASSERT(max_vertex_index(g) == 10);
    for(size_t i = 0; i < N; ++i) {
        BOOST_ASSERT(get_vertex_index(v[i], g) == N + i);
    }

    // Renumber vertices
    renumber_vertex_indices(g);

    // And we should be back to the initial condition
    BOOST_ASSERT(max_vertex_index(g) == N);
    for(size_t i = 0; i < N; ++i) {
        BOOST_ASSERT(get_vertex_index(v[i], g) == i);
    }
}

// Make sure that graphs constructed over n vertices will actually number
// their vertices correctly.
template <typename Graph>
void build()
{
    typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
    typedef typename graph_traits<Graph>::vertex_iterator Iterator;
    typedef typename property_map<Graph, vertex_index_t>::type IndexMap;

    static const size_t N = 5;

    Graph g(N);
    BOOST_ASSERT(max_vertex_index(g) == N);

    IndexMap x = get(vertex_index, g);

    // Each vertex should be numbered correctly.
    Iterator i, end;
    tie(i, end) = vertices(g);
    for(size_t x = 0; i != end; ++i, ++x) {
        BOOST_ASSERT(get_vertex_index(*i, g) == x);
    }
}

int main(int, char*[])
{
    test< undirected_graph<> >();
    test< directed_graph<> >();

    build< undirected_graph<> >();

    return 0;
}