os/ossrv/stdcpp/tsrc/Boost_test/graph/src/bundled_properties.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Boost Graph library
sl@0
     2
sl@0
     3
//  Copyright Douglas Gregor 2004. Use, modification and
sl@0
     4
//  distribution is subject to the Boost Software License, Version
sl@0
     5
//  1.0. (See accompanying file LICENSE_1_0.txt or copy at
sl@0
     6
//  http://www.boost.org/LICENSE_1_0.txt)
sl@0
     7
/*
sl@0
     8
 * © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
sl@0
     9
*/
sl@0
    10
sl@0
    11
#include <boost/test/minimal.hpp>
sl@0
    12
#include <boost/graph/adjacency_list.hpp>
sl@0
    13
#include <boost/graph/adjacency_matrix.hpp>
sl@0
    14
#include <boost/graph/filtered_graph.hpp>
sl@0
    15
#include <boost/graph/subgraph.hpp>
sl@0
    16
#include <string>
sl@0
    17
#include <vector>
sl@0
    18
#include <boost/graph/adjacency_list_io.hpp>
sl@0
    19
#include <sstream>
sl@0
    20
#include <boost/graph/iteration_macros.hpp>
sl@0
    21
#include <algorithm>
sl@0
    22
#include <iterator>
sl@0
    23
#ifdef __SYMBIAN32__
sl@0
    24
#include "std_log_result.h"
sl@0
    25
#define LOG_FILENAME_LINE __FILE__, __LINE__
sl@0
    26
#endif
sl@0
    27
using namespace std;
sl@0
    28
using namespace boost;
sl@0
    29
sl@0
    30
struct City
sl@0
    31
{
sl@0
    32
  City() {}
sl@0
    33
  City(const std::string& name, int pop, int zipcode) : name(name), population(pop)
sl@0
    34
  { 
sl@0
    35
    zipcodes.push_back(zipcode); 
sl@0
    36
  }
sl@0
    37
sl@0
    38
  string name;
sl@0
    39
  int population;
sl@0
    40
  vector<int> zipcodes;
sl@0
    41
};
sl@0
    42
sl@0
    43
std::ostream& operator<<(std::ostream& out, const City& city)
sl@0
    44
{
sl@0
    45
  out << city.name << ' ' << city.population << ' ';
sl@0
    46
  copy(city.zipcodes.begin(), city.zipcodes.end(),
sl@0
    47
       ostream_iterator<int>(out, " "));
sl@0
    48
  out << -1;
sl@0
    49
  return out;
sl@0
    50
}
sl@0
    51
sl@0
    52
std::istream& operator>>(std::istream& in, City& city)
sl@0
    53
{
sl@0
    54
  if (in >> city.name >> city.population) {
sl@0
    55
    int zip;
sl@0
    56
    city.zipcodes.clear();
sl@0
    57
    while (in >> zip && zip != -1)
sl@0
    58
      city.zipcodes.push_back(zip);
sl@0
    59
  }
sl@0
    60
  return in;
sl@0
    61
}
sl@0
    62
sl@0
    63
bool operator==(const City& c1, const City& c2)
sl@0
    64
{
sl@0
    65
  return (c1.name == c2.name && c1.population == c2.population
sl@0
    66
          && c1.zipcodes == c2.zipcodes);
sl@0
    67
}
sl@0
    68
sl@0
    69
struct Highway
sl@0
    70
{
sl@0
    71
  Highway() {}
sl@0
    72
  Highway(const string& name, double miles, int speed_limit = 65, int lanes = 4, bool divided = true) 
sl@0
    73
    : name(name), miles(miles), speed_limit(speed_limit), lanes(lanes), divided(divided) {}
sl@0
    74
sl@0
    75
  string name;
sl@0
    76
  double miles;
sl@0
    77
  int speed_limit;
sl@0
    78
  int lanes;
sl@0
    79
  bool divided;
sl@0
    80
};
sl@0
    81
sl@0
    82
std::ostream& operator<<(std::ostream& out, const Highway& highway)
sl@0
    83
{
sl@0
    84
  return out << highway.name << ' ' << highway.miles << ' ' << highway.miles
sl@0
    85
             << ' ' << highway.speed_limit << ' ' << highway.lanes
sl@0
    86
             << ' ' << highway.divided;
sl@0
    87
}
sl@0
    88
sl@0
    89
std::istream& operator>>(std::istream& in, Highway& highway)
sl@0
    90
{
sl@0
    91
  return in >> highway.name >> highway.miles >> highway.miles
sl@0
    92
            >> highway.speed_limit  >> highway.lanes
sl@0
    93
            >> highway.divided;
sl@0
    94
}
sl@0
    95
sl@0
    96
bool operator==(const Highway& h1, const Highway& h2)
sl@0
    97
{
sl@0
    98
  return (h1.name == h2.name && h1.miles == h2.miles 
sl@0
    99
          && h1.speed_limit == h2.speed_limit && h1.lanes == h2.lanes
sl@0
   100
          && h1.divided == h2.divided);
sl@0
   101
}
sl@0
   102
sl@0
   103
template<bool> struct truth {};
sl@0
   104
sl@0
   105
template<typename Map, typename VertexIterator, typename Bundle>
sl@0
   106
typename boost::graph_traits<Map>::vertex_descriptor 
sl@0
   107
do_add_vertex(Map& map, VertexIterator, const Bundle& bundle, truth<true>)
sl@0
   108
{
sl@0
   109
  return add_vertex(bundle, map);
sl@0
   110
}
sl@0
   111
sl@0
   112
template<typename Map, typename VertexIterator, typename Bundle>
sl@0
   113
typename boost::graph_traits<Map>::vertex_descriptor 
sl@0
   114
do_add_vertex(Map& map, VertexIterator& vi, const Bundle& bundle, truth<false>)
sl@0
   115
{
sl@0
   116
  get(boost::vertex_bundle, map)[*vi] = bundle;
sl@0
   117
  return *vi++;
sl@0
   118
}
sl@0
   119
sl@0
   120
template<class EL, class VL, class D, class VP, class EP, class GP>
sl@0
   121
void test_io(adjacency_list<EL,VL,D,VP,EP,GP>& map, int)
sl@0
   122
{
sl@0
   123
  typedef adjacency_list<EL,VL,D,VP,EP,GP> Map;
sl@0
   124
sl@0
   125
  ostringstream out;
sl@0
   126
  cout << write(map);
sl@0
   127
  out << write(map);
sl@0
   128
  
sl@0
   129
  istringstream in(out.str());
sl@0
   130
  adjacency_list<EL,VL,D,VP,EP,GP> map2;
sl@0
   131
  in >> read(map2);
sl@0
   132
  typename graph_traits<adjacency_list<EL,VL,D,VP,EP,GP> >::vertex_iterator
sl@0
   133
    v2 = vertices(map2).first;
sl@0
   134
  BGL_FORALL_VERTICES_T(v, map, Map) {
sl@0
   135
    BOOST_CHECK(map[v] == map2[*v2]);
sl@0
   136
    typename graph_traits<adjacency_list<EL,VL,D,VP,EP,GP> >::out_edge_iterator
sl@0
   137
      e2 = out_edges(*v2, map2).first;
sl@0
   138
    BGL_FORALL_OUTEDGES_T(v, e, map, Map) {
sl@0
   139
      BOOST_CHECK(map[e] == map[*e2]);
sl@0
   140
      ++e2;
sl@0
   141
    }
sl@0
   142
    ++v2;
sl@0
   143
  }
sl@0
   144
}
sl@0
   145
sl@0
   146
template<typename Map>
sl@0
   147
void test_io(const Map&, long)
sl@0
   148
{
sl@0
   149
  // Nothing to test
sl@0
   150
}
sl@0
   151
sl@0
   152
template<typename Map, bool CanAddVertex>
sl@0
   153
void test_bundled_properties(Map*, truth<CanAddVertex> can_add_vertex)
sl@0
   154
{
sl@0
   155
  typedef typename boost::graph_traits<Map>::vertex_iterator   vertex_iterator;
sl@0
   156
  typedef typename boost::graph_traits<Map>::vertex_descriptor vertex_descriptor;
sl@0
   157
  typedef typename boost::graph_traits<Map>::edge_descriptor   edge_descriptor;
sl@0
   158
sl@0
   159
  Map map(CanAddVertex? 2 : 3);
sl@0
   160
sl@0
   161
  vertex_iterator vi = vertices(map).first;
sl@0
   162
  vertex_descriptor v = *vi;
sl@0
   163
  map[v].name = "Troy";
sl@0
   164
  map[v].population = 49170;
sl@0
   165
  map[v].zipcodes.push_back(12180);
sl@0
   166
sl@0
   167
  ++vi;
sl@0
   168
  vertex_descriptor u = *vi++;
sl@0
   169
  map[u].name = "Albany";
sl@0
   170
  map[u].population = 95658;
sl@0
   171
  map[u].zipcodes.push_back(12201);
sl@0
   172
sl@0
   173
  // Try adding a vertex with a property value
sl@0
   174
  vertex_descriptor bloomington = do_add_vertex(map, vi, City("Bloomington", 39000, 47401),
sl@0
   175
                                                can_add_vertex);
sl@0
   176
  BOOST_CHECK(get(boost::vertex_bundle, map)[bloomington].zipcodes[0] == 47401);
sl@0
   177
  
sl@0
   178
  edge_descriptor e = add_edge(v, u, map).first;
sl@0
   179
  map[e].name = "I-87";
sl@0
   180
  map[e].miles = 10;
sl@0
   181
  map[e].speed_limit = 65;
sl@0
   182
  map[e].lanes = 4;
sl@0
   183
  map[e].divided = true;
sl@0
   184
sl@0
   185
  edge_descriptor our_trip = add_edge(v, bloomington, Highway("Long", 1000), map).first;
sl@0
   186
  BOOST_CHECK(get(boost::edge_bundle, map, our_trip).miles == 1000);
sl@0
   187
  
sl@0
   188
  BOOST_CHECK(get(get(&City::name, map), v) == "Troy");
sl@0
   189
  BOOST_CHECK(get(get(&Highway::name, map), e) == "I-87");
sl@0
   190
  BOOST_CHECK(get(&City::name, map, u) == "Albany");
sl@0
   191
  BOOST_CHECK(get(&Highway::name, map, e) == "I-87");
sl@0
   192
  put(&City::population, map, v, 49168);
sl@0
   193
  BOOST_CHECK(get(&City::population, map)[v] == 49168);
sl@0
   194
  
sl@0
   195
  boost::filtered_graph<Map, boost::keep_all> fmap(map, boost::keep_all());
sl@0
   196
  BOOST_CHECK(get(boost::edge_bundle, map, our_trip).miles == 1000);
sl@0
   197
  
sl@0
   198
  BOOST_CHECK(get(get(&City::name, fmap), v) == "Troy");
sl@0
   199
  BOOST_CHECK(get(get(&Highway::name, fmap), e) == "I-87");
sl@0
   200
  BOOST_CHECK(get(&City::name, fmap, u) == "Albany");
sl@0
   201
  BOOST_CHECK(get(&Highway::name, fmap, e) == "I-87");
sl@0
   202
  put(&City::population, fmap, v, 49169);
sl@0
   203
  BOOST_CHECK(get(&City::population, fmap)[v] == 49169);
sl@0
   204
sl@0
   205
  test_io(map, 0);
sl@0
   206
}
sl@0
   207
sl@0
   208
void test_subgraph_bundled_properties()
sl@0
   209
{
sl@0
   210
  typedef boost::subgraph<
sl@0
   211
            boost::adjacency_list<boost::vecS, boost::vecS, 
sl@0
   212
                                  boost::bidirectionalS, City, 
sl@0
   213
                                  boost::property<boost::edge_index_t, int,
sl@0
   214
                                                  Highway> > > SubMap;
sl@0
   215
  typedef boost::graph_traits<SubMap>::vertex_descriptor Vertex;
sl@0
   216
  typedef boost::graph_traits<SubMap>::vertex_iterator vertex_iterator;
sl@0
   217
sl@0
   218
  SubMap map(3);
sl@0
   219
  vertex_iterator vi = vertices(map).first;
sl@0
   220
  Vertex troy = *vi++;
sl@0
   221
  map[troy].name = "Troy";
sl@0
   222
  map[*vi++].name = "Bloomington";
sl@0
   223
  map[*vi++].name = "Endicott";
sl@0
   224
sl@0
   225
  SubMap& g1 = map.create_subgraph();
sl@0
   226
  Vertex troy1 = add_vertex(*vertices(map).first, g1);
sl@0
   227
  BOOST_CHECK(map[troy1].name == g1[troy1].name);
sl@0
   228
}
sl@0
   229
sl@0
   230
int test_main(int, char*[])
sl@0
   231
{
sl@0
   232
  typedef boost::adjacency_list<
sl@0
   233
    boost::listS, boost::vecS, boost::bidirectionalS,
sl@0
   234
    City, Highway> Map1;
sl@0
   235
  typedef boost::adjacency_matrix<boost::directedS,
sl@0
   236
    City, Highway> Map2;
sl@0
   237
sl@0
   238
  test_bundled_properties(static_cast<Map1*>(0), truth<true>());
sl@0
   239
  test_bundled_properties(static_cast<Map2*>(0), truth<false>());
sl@0
   240
  test_subgraph_bundled_properties();
sl@0
   241
  
sl@0
   242
   #ifdef __SYMBIAN32__
sl@0
   243
	std_log(LOG_FILENAME_LINE,"[End Test Case ]");
sl@0
   244
sl@0
   245
	testResultXml("bundled_properties");
sl@0
   246
	close_log_file();
sl@0
   247
#endif
sl@0
   248
  return 0;
sl@0
   249
}