os/ossrv/stdcpp/tsrc/Boost_test/graph/src/bfs-example.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
//=======================================================================
sl@0
     2
// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, 
sl@0
     3
//
sl@0
     4
// Distributed under the Boost Software License, Version 1.0. (See
sl@0
     5
// 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
sl@0
     9
/*
sl@0
    10
 * © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
sl@0
    11
*/
sl@0
    12
sl@0
    13
sl@0
    14
#include <boost/graph/adjacency_list.hpp>
sl@0
    15
#include <boost/graph/breadth_first_search.hpp>
sl@0
    16
#include <boost/pending/indirect_cmp.hpp>
sl@0
    17
#include <boost/pending/integer_range.hpp>
sl@0
    18
sl@0
    19
#include <iostream>
sl@0
    20
sl@0
    21
#ifdef __SYMBIAN32__
sl@0
    22
#include "std_log_result.h"
sl@0
    23
#define LOG_FILENAME_LINE __FILE__, __LINE__
sl@0
    24
#endif
sl@0
    25
sl@0
    26
using namespace boost;
sl@0
    27
template < typename TimeMap > class bfs_time_visitor:public default_bfs_visitor {
sl@0
    28
  typedef typename property_traits < TimeMap >::value_type T;
sl@0
    29
public:
sl@0
    30
  bfs_time_visitor(TimeMap tmap, T & t):m_timemap(tmap), m_time(t) { }
sl@0
    31
  template < typename Vertex, typename Graph >
sl@0
    32
    void discover_vertex(Vertex u, const Graph & g) const
sl@0
    33
  {
sl@0
    34
    put(m_timemap, u, m_time++);
sl@0
    35
  }
sl@0
    36
  TimeMap m_timemap;
sl@0
    37
  T & m_time;
sl@0
    38
};
sl@0
    39
sl@0
    40
sl@0
    41
int
sl@0
    42
main()
sl@0
    43
{
sl@0
    44
  using namespace boost;
sl@0
    45
  // Select the graph type we wish to use
sl@0
    46
  typedef adjacency_list < vecS, vecS, undirectedS > graph_t;
sl@0
    47
  // Set up the vertex IDs and names
sl@0
    48
  enum { r, s, t, u, v, w, x, y, N };
sl@0
    49
  const char *name = "rstuvwxy";
sl@0
    50
  // Specify the edges in the graph
sl@0
    51
  typedef std::pair < int, int >E;
sl@0
    52
  E edge_array[] = { E(r, s), E(r, v), E(s, w), E(w, r), E(w, t),
sl@0
    53
    E(w, x), E(x, t), E(t, u), E(x, y), E(u, y)
sl@0
    54
  };
sl@0
    55
  // Create the graph object
sl@0
    56
  const int n_edges = sizeof(edge_array) / sizeof(E);
sl@0
    57
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
sl@0
    58
  // VC++ has trouble with the edge iterator constructor
sl@0
    59
  graph_t g(N);
sl@0
    60
  for (std::size_t j = 0; j < n_edges; ++j)
sl@0
    61
    add_edge(edge_array[j].first, edge_array[j].second, g);
sl@0
    62
#else
sl@0
    63
  typedef graph_traits<graph_t>::vertices_size_type v_size_t;
sl@0
    64
  graph_t g(edge_array, edge_array + n_edges, v_size_t(N));
sl@0
    65
#endif
sl@0
    66
sl@0
    67
  // Typedefs
sl@0
    68
  typedef graph_traits < graph_t >::vertex_descriptor Vertex;
sl@0
    69
  typedef graph_traits < graph_t >::vertices_size_type Size;
sl@0
    70
  typedef Size* Iiter;
sl@0
    71
sl@0
    72
  // a vector to hold the discover time property for each vertex
sl@0
    73
  std::vector < Size > dtime(num_vertices(g));
sl@0
    74
sl@0
    75
  Size time = 0;
sl@0
    76
  bfs_time_visitor < Size * >vis(&dtime[0], time);
sl@0
    77
  breadth_first_search(g, vertex(s, g), visitor(vis));
sl@0
    78
sl@0
    79
  // Use std::sort to order the vertices by their discover time
sl@0
    80
  std::vector<graph_traits<graph_t>::vertices_size_type > discover_order(N);
sl@0
    81
  integer_range < int >range(0, N);
sl@0
    82
  std::copy(range.begin(), range.end(), discover_order.begin());
sl@0
    83
  std::sort(discover_order.begin(), discover_order.end(),
sl@0
    84
            indirect_cmp < Iiter, std::less < Size > >(&dtime[0]));
sl@0
    85
sl@0
    86
  std::cout << "order of discovery: ";
sl@0
    87
  for (int i = 0; i < N; ++i)
sl@0
    88
    std::cout << name[discover_order[i]] << " ";
sl@0
    89
  std::cout << std::endl;
sl@0
    90
sl@0
    91
   #ifdef __SYMBIAN32__
sl@0
    92
	std_log(LOG_FILENAME_LINE,"[End Test Case ]");
sl@0
    93
  	testResultXml("bfs-example");
sl@0
    94
	close_log_file();
sl@0
    95
  #endif
sl@0
    96
  return EXIT_SUCCESS;
sl@0
    97
}