File: index_graph.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (98 lines) | stat: -rw-r--r-- 2,568 bytes parent folder | download | duplicates (10)
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
96
97
98
// (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;
    (void)(IndexMap) 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_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);

    (void)(IndexMap) get(vertex_index, g);

    // Each vertex should be numbered correctly.
    Iterator i, end;
    boost::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;
}