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