File: bundled_properties.cpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (252 lines) | stat: -rw-r--r-- 7,538 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Boost Graph library

//  Copyright Douglas Gregor 2004. 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)

#include <boost/core/lightweight_test.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/adjacency_matrix.hpp>
#include <boost/graph/filtered_graph.hpp>
#include <boost/graph/subgraph.hpp>
#include <string>
#include <vector>
#include <boost/graph/adjacency_list_io.hpp>
#include <sstream>
#include <boost/graph/iteration_macros.hpp>
#include <algorithm>
#include <iterator>

using namespace std;
using namespace boost;

struct City
{
    City() {}
    City(const std::string& name, int pop, int zipcode)
    : name(name), population(pop)
    {
        zipcodes.push_back(zipcode);
    }

    string name;
    int population;
    vector< int > zipcodes;
};

std::ostream& operator<<(std::ostream& out, const City& city)
{
    out << city.name << ' ' << city.population << ' ';
    copy(city.zipcodes.begin(), city.zipcodes.end(),
        ostream_iterator< int >(out, " "));
    out << -1;
    return out;
}

std::istream& operator>>(std::istream& in, City& city)
{
    if (in >> city.name >> city.population)
    {
        int zip;
        city.zipcodes.clear();
        while (in >> zip && zip != -1)
            city.zipcodes.push_back(zip);
    }
    return in;
}

bool operator==(const City& c1, const City& c2)
{
    return (c1.name == c2.name && c1.population == c2.population
        && c1.zipcodes == c2.zipcodes);
}

struct Highway
{
    Highway() {}
    Highway(const string& name, double miles, int speed_limit = 65,
        int lanes = 4, bool divided = true)
    : name(name)
    , miles(miles)
    , speed_limit(speed_limit)
    , lanes(lanes)
    , divided(divided)
    {
    }

    string name;
    double miles;
    int speed_limit;
    int lanes;
    bool divided;
};

std::ostream& operator<<(std::ostream& out, const Highway& highway)
{
    return out << highway.name << ' ' << highway.miles << ' ' << highway.miles
               << ' ' << highway.speed_limit << ' ' << highway.lanes << ' '
               << highway.divided;
}

std::istream& operator>>(std::istream& in, Highway& highway)
{
    return in >> highway.name >> highway.miles >> highway.miles
        >> highway.speed_limit >> highway.lanes >> highway.divided;
}

bool operator==(const Highway& h1, const Highway& h2)
{
    return (h1.name == h2.name && h1.miles == h2.miles
        && h1.speed_limit == h2.speed_limit && h1.lanes == h2.lanes
        && h1.divided == h2.divided);
}

template < bool > struct truth
{
};

template < typename Map, typename VertexIterator, typename Bundle >
typename boost::graph_traits< Map >::vertex_descriptor do_add_vertex(
    Map& map, VertexIterator, const Bundle& bundle, truth< true >)
{
    return add_vertex(bundle, map);
}

template < typename Map, typename VertexIterator, typename Bundle >
typename boost::graph_traits< Map >::vertex_descriptor do_add_vertex(
    Map& map, VertexIterator& vi, const Bundle& bundle, truth< false >)
{
    get(boost::vertex_bundle, map)[*vi] = bundle;
    return *vi++;
}

template < class EL, class VL, class D, class VP, class EP, class GP >
void test_io(adjacency_list< EL, VL, D, VP, EP, GP >& map, int)
{
    typedef adjacency_list< EL, VL, D, VP, EP, GP > Map;

    ostringstream out;
    cout << write(map);
    out << write(map);

    istringstream in(out.str());
    adjacency_list< EL, VL, D, VP, EP, GP > map2;
    in >> read(map2);
    typename graph_traits<
        adjacency_list< EL, VL, D, VP, EP, GP > >::vertex_iterator v2
        = vertices(map2).first;
    BGL_FORALL_VERTICES_T(v, map, Map)
    {
        BOOST_TEST(map[v] == map2[*v2]);
        typename graph_traits<
            adjacency_list< EL, VL, D, VP, EP, GP > >::out_edge_iterator e2
            = out_edges(*v2, map2).first;
        BGL_FORALL_OUTEDGES_T(v, e, map, Map)
        {
            BOOST_TEST(map[e] == map[*e2]);
            ++e2;
        }
        ++v2;
    }
}

template < typename Map > void test_io(const Map&, long)
{
    // Nothing to test
}

template < typename Map, bool CanAddVertex >
void test_bundled_properties(Map*, truth< CanAddVertex > can_add_vertex)
{
    typedef
        typename boost::graph_traits< Map >::vertex_iterator vertex_iterator;
    typedef typename boost::graph_traits< Map >::vertex_descriptor
        vertex_descriptor;
    typedef
        typename boost::graph_traits< Map >::edge_descriptor edge_descriptor;

    Map map(CanAddVertex ? 2 : 3);

    vertex_iterator vi = vertices(map).first;
    vertex_descriptor v = *vi;
    map[v].name = "Troy";
    map[v].population = 49170;
    map[v].zipcodes.push_back(12180);

    ++vi;
    vertex_descriptor u = *vi++;
    map[u].name = "Albany";
    map[u].population = 95658;
    map[u].zipcodes.push_back(12201);

    // Try adding a vertex with a property value
    vertex_descriptor bloomington = do_add_vertex(
        map, vi, City("Bloomington", 39000, 47401), can_add_vertex);
    BOOST_TEST(
        get(boost::vertex_bundle, map)[bloomington].zipcodes[0] == 47401);

    edge_descriptor e = add_edge(v, u, map).first;
    map[e].name = "I-87";
    map[e].miles = 10;
    map[e].speed_limit = 65;
    map[e].lanes = 4;
    map[e].divided = true;

    edge_descriptor our_trip
        = add_edge(v, bloomington, Highway("Long", 1000), map).first;
    BOOST_TEST(get(boost::edge_bundle, map, our_trip).miles == 1000);

    BOOST_TEST(get(get(&City::name, map), v) == "Troy");
    BOOST_TEST(get(get(&Highway::name, map), e) == "I-87");
    BOOST_TEST(get(&City::name, map, u) == "Albany");
    BOOST_TEST(get(&Highway::name, map, e) == "I-87");
    put(&City::population, map, v, 49168);
    BOOST_TEST(get(&City::population, map)[v] == 49168);

    boost::filtered_graph< Map, boost::keep_all > fmap(map, boost::keep_all());
    BOOST_TEST(get(boost::edge_bundle, map, our_trip).miles == 1000);

    BOOST_TEST(get(get(&City::name, fmap), v) == "Troy");
    BOOST_TEST(get(get(&Highway::name, fmap), e) == "I-87");
    BOOST_TEST(get(&City::name, fmap, u) == "Albany");
    BOOST_TEST(get(&Highway::name, fmap, e) == "I-87");
    put(&City::population, fmap, v, 49169);
    BOOST_TEST(get(&City::population, fmap)[v] == 49169);

    test_io(map, 0);
}

void test_subgraph_bundled_properties()
{
    typedef boost::subgraph<
        boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS,
            City, boost::property< boost::edge_index_t, int, Highway > > >
        SubMap;
    typedef boost::graph_traits< SubMap >::vertex_descriptor Vertex;
    typedef boost::graph_traits< SubMap >::vertex_iterator vertex_iterator;

    SubMap map(3);
    vertex_iterator vi = vertices(map).first;
    Vertex troy = *vi++;
    map[troy].name = "Troy";
    map[*vi++].name = "Bloomington";
    map[*vi++].name = "Endicott";

    SubMap& g1 = map.create_subgraph();
    Vertex troy1 = add_vertex(*vertices(map).first, g1);
    BOOST_TEST(map[troy1].name == g1[troy1].name);
}

int main(int, char*[])
{
    typedef boost::adjacency_list< boost::listS, boost::vecS,
        boost::bidirectionalS, City, Highway >
        Map1;
    typedef boost::adjacency_matrix< boost::directedS, City, Highway > Map2;

    test_bundled_properties(static_cast< Map1* >(0), truth< true >());
    test_bundled_properties(static_cast< Map2* >(0), truth< false >());
    test_subgraph_bundled_properties();
    return boost::report_errors();
}