diff --git a/example/accum-compile-times.cpp b/example/accum-compile-times.cpp index 85af0482..5dc88c13 100644 --- a/example/accum-compile-times.cpp +++ b/example/accum-compile-times.cpp @@ -13,11 +13,12 @@ #include #include #include +#include "range_pair.hpp" namespace std { - template < typename T > - std::istream& operator >> (std::istream& in, std::pair < T, T > &p) + template + std::istream& operator >> (std::istream& in, std::pair &p) { in >> p.first >> p.second; return in; @@ -32,26 +33,22 @@ namespace boost using namespace boost; -typedef adjacency_list< listS, // Store out-edges of each vertex in a std::list +using file_dep_graph2 = adjacency_list > > >, + property>>>, // an edge property - property < edge_weight_t, float > > - file_dep_graph2; - -typedef graph_traits::vertex_descriptor vertex_t; -typedef graph_traits::edge_descriptor edge_t; + property>; int main() { std::ifstream file_in("makefile-dependencies.dat"); - typedef graph_traits::vertices_size_type size_type; + using size_type = graph_traits::vertices_size_type; size_type n_vertices; file_in >> n_vertices; // read in number of vertices #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 @@ -59,34 +56,27 @@ main() std::vector id2vertex; file_dep_graph2 g; for (std::size_t i = 0; i < n_vertices; ++i) - id2vertex.push_back(add_vertex(g)); + id2vertex.emplace_back(add_vertex(g)); std::pair p; while (file_in >> p) add_edge(id2vertex[p.first], id2vertex[p.second], g); #else - std::istream_iterator > + std::istream_iterator> input_begin(file_in), input_end; file_dep_graph2 g(input_begin, input_end, n_vertices); #endif - typedef property_map < file_dep_graph2, vertex_name_t >::type name_map_t; - typedef property_map < file_dep_graph2, vertex_compile_cost_t >::type - compile_cost_map_t; - - name_map_t name_map = get(vertex_name, g); - compile_cost_map_t compile_cost_map = get(vertex_compile_cost, g); + auto name_map = get(vertex_name, g); + auto compile_cost_map = get(vertex_compile_cost, g); std::ifstream name_in("makefile-target-names.dat"); std::ifstream compile_cost_in("target-compile-costs.dat"); - graph_traits < file_dep_graph2 >::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { - name_in >> name_map[*vi]; - compile_cost_in >> compile_cost_map[*vi]; + for(const auto& vertex : make_range_pair(vertices(g))) { + name_in >> name_map[vertex]; + compile_cost_in >> compile_cost_map[vertex]; } - graph_property_iter_range < file_dep_graph2, - vertex_compile_cost_t >::iterator ci, ci_end; - boost::tie(ci, ci_end) = get_property_iter_range(g, vertex_compile_cost); + const auto [ci, ci_end] = get_property_iter_range(g, vertex_compile_cost); std::cout << "total (sequential) compile time: " << std::accumulate(ci, ci_end, 0.0) << std::endl; diff --git a/example/actor_clustering.cpp b/example/actor_clustering.cpp index 9b1b4022..cd22fc8c 100644 --- a/example/actor_clustering.cpp +++ b/example/actor_clustering.cpp @@ -22,8 +22,8 @@ #include #include #include -#include #include +#include "range_pair.hpp" using namespace boost; @@ -34,10 +34,10 @@ struct Actor int id; }; -typedef adjacency_list > ActorGraph; -typedef graph_traits::vertex_descriptor Vertex; -typedef graph_traits::edge_descriptor Edge; +using ActorGraph = adjacency_list>; +using Vertex = graph_traits::vertex_descriptor; +using Edge = graph_traits::edge_descriptor; void load_actor_graph(std::istream& in, ActorGraph& g) { @@ -48,23 +48,23 @@ void load_actor_graph(std::istream& in, ActorGraph& g) std::vector actors_in_movie; // Map from the actor numbers on this line to the actor vertices - typedef tokenizer > Tok; + using Tok = tokenizer>; Tok tok(line, char_separator(" ")); - for (Tok::iterator id = tok.begin(); id != tok.end(); ++id) { - int actor_id = lexical_cast(*id); - std::map::iterator v = actors.find(actor_id); + for (const auto& id : tok) { + auto actor_id = std::stoi(id); + auto v = actors.find(actor_id); if (v == actors.end()) { - Vertex new_vertex = add_vertex(Actor(actor_id), g); + auto new_vertex = add_vertex(Actor(actor_id), g); actors[actor_id] = new_vertex; - actors_in_movie.push_back(new_vertex); + actors_in_movie.emplace_back(new_vertex); } else { - actors_in_movie.push_back(v->second); + actors_in_movie.emplace_back(v->second); } } - for (std::vector::iterator i = actors_in_movie.begin(); + for (auto i = actors_in_movie.begin(); i != actors_in_movie.end(); ++i) { - for (std::vector::iterator j = i + 1; + for (auto j = i + 1; j != actors_in_movie.end(); ++j) { if (!edge(*i, *j, g).second) add_edge(*i, *j, g); } @@ -78,23 +78,21 @@ write_pajek_graph(std::ostream& out, const Graph& g, VertexIndexMap vertex_index, VertexNameMap vertex_name) { out << "*Vertices " << num_vertices(g) << '\n'; - typedef typename graph_traits::vertex_iterator vertex_iterator; - for (vertex_iterator v = vertices(g).first; v != vertices(g).second; ++v) { - out << get(vertex_index, *v)+1 << " \"" << get(vertex_name, *v) << "\"\n"; + for (const auto& vertex : make_range_pair(vertices(g))) { + out << get(vertex_index, vertex)+1 << " \"" << get(vertex_name, vertex) << "\"\n"; } out << "*Edges\n"; - typedef typename graph_traits::edge_iterator edge_iterator; - for (edge_iterator e = edges(g).first; e != edges(g).second; ++e) { - out << get(vertex_index, source(*e, g))+1 << ' ' - << get(vertex_index, target(*e, g))+1 << " 1.0\n"; // HACK! + for (const auto& edge : make_range_pair(edges(g))) { + out << get(vertex_index, source(edge, g))+1 << ' ' + << get(vertex_index, target(edge, g))+1 << " 1.0\n"; // HACK! } return out; } class actor_clustering_threshold : public bc_clustering_threshold { - typedef bc_clustering_threshold inherited; + using inherited = bc_clustering_threshold; public: actor_clustering_threshold(double threshold, const ActorGraph& g, @@ -133,7 +131,7 @@ int main(int argc, char* argv[]) out_file = argv[on_arg]; } else if (arg == "-threshold") { ++on_arg; assert(on_arg < argc); - threshold = lexical_cast(argv[on_arg]); + threshold = std::stod(argv[on_arg]); } else if (arg == "-normalize") { normalize = true; } else { diff --git a/example/adj_list_ra_edgelist.cpp b/example/adj_list_ra_edgelist.cpp index d92ecd66..2a1becce 100644 --- a/example/adj_list_ra_edgelist.cpp +++ b/example/adj_list_ra_edgelist.cpp @@ -15,18 +15,17 @@ int main() { using namespace boost; - typedef adjacency_list, no_property, vecS> Graph; + using Graph = adjacency_list, no_property, vecS>; const std::size_t n = 3; - typedef std::pair E; + using E = std::pair; E edge_array[] = { E(0,1), E(0,2), E(0,1) }; const std::size_t m = sizeof(edge_array) / sizeof(E); Graph g(edge_array, edge_array + m, n); - graph_traits::edge_iterator edge_iterator; for (std::size_t i = 0; i < m; ++i) { - const graph_traits::edge_iterator e = edges(g).first + i; + const auto e = edges(g).first + i; std::cout << *e << " "; } std::cout << std::endl; diff --git a/example/adjacency_list.cpp b/example/adjacency_list.cpp index 6d805379..2d9a3495 100644 --- a/example/adjacency_list.cpp +++ b/example/adjacency_list.cpp @@ -15,6 +15,7 @@ #include #include #include +#include "range_pair.hpp" /* Sample Output @@ -51,24 +52,19 @@ struct VertexProperties { int main(int , char* []) { using namespace boost; - using namespace std; - typedef adjacency_list Graph; + using Graph = adjacency_list; const int V = 5; Graph g(V); - property_map::type - id = get(&VertexProperties::index, g); - property_map::type - name = get(&EdgeProperties::name, g); + auto id = get(&VertexProperties::index, g); + auto name = get(&EdgeProperties::name, g); - boost::graph_traits::vertex_iterator vi, viend; int vnum = 0; - - for (boost::tie(vi,viend) = vertices(g); vi != viend; ++vi) - id[*vi] = vnum++; + for(const auto& vertex : make_range_pair(vertices(g))) + id[vertex] = vnum++; add_edge(vertex(0, g), vertex(1, g), EdgeProperties("joe"), g); add_edge(vertex(1, g), vertex(2, g), EdgeProperties("curly"), g); @@ -76,29 +72,27 @@ int main(int , char* []) add_edge(vertex(2, g), vertex(4, g), EdgeProperties("tom"), g); add_edge(vertex(3, g), vertex(4, g), EdgeProperties("harry"), g); - graph_traits::vertex_iterator i, end; - graph_traits::out_edge_iterator ei, edge_end; - for (boost::tie(i,end) = vertices(g); i != end; ++i) { - cout << id[*i] << " "; - for (boost::tie(ei,edge_end) = out_edges(*i, g); ei != edge_end; ++ei) - cout << " --" << name[*ei] << "--> " << id[target(*ei, g)] << " "; - cout << endl; + for(const auto& vertex : make_range_pair(vertices(g))) { + std::cout << id[vertex] << " "; + for (const auto out_edge : make_range_pair(out_edges(vertex, g))) + std::cout << " --" << name[out_edge] << "--> " << id[target(out_edge, g)] << " "; + std::cout << std::endl; } print_edges(g, id); - cout << endl << "removing edge (1,3): " << endl; + std::cout << std::endl << "removing edge (1,3): " << std::endl; remove_edge(vertex(1, g), vertex(3, g), g); - ei = out_edges(vertex(1, g), g).first; - cout << "removing edge (" << id[source(*ei, g)] - << "," << id[target(*ei, g)] << ")" << endl; + auto ei = out_edges(vertex(1, g), g).first; + std::cout << "removing edge (" << id[source(*ei, g)] + << "," << id[target(*ei, g)] << ")" << std::endl; remove_edge(ei, g); - for(boost::tie(i,end) = vertices(g); i != end; ++i) { - cout << id[*i] << " "; - for (boost::tie(ei,edge_end) = out_edges(*i, g); ei != edge_end; ++ei) - cout << " --" << name[*ei] << "--> " << id[target(*ei, g)] << " "; - cout << endl; + for(const auto& vertex : make_range_pair(vertices(g))) { + std::cout << id[vertex] << " "; + for (const auto out_edge : make_range_pair(out_edges(vertex, g))) + std::cout << " --" << name[out_edge] << "--> " << id[target(out_edge, g)] << " "; + std::cout << std::endl; } print_edges(g, id); diff --git a/example/adjacency_list_io.cpp b/example/adjacency_list_io.cpp index ab626f89..79bae4dc 100644 --- a/example/adjacency_list_io.cpp +++ b/example/adjacency_list_io.cpp @@ -28,29 +28,27 @@ std::istream& operator >> ( std::istream& in, MyStruct& s ) } //======== vertex properties -struct n1_t { enum { num = 23063}; typedef vertex_property_tag kind; }; -struct n2_t { enum { num = 23062}; typedef vertex_property_tag kind; }; -struct n3_t { enum { num = 23061}; typedef vertex_property_tag kind; }; -typedef property< n1_t, int, +struct n1_t { enum { num = 23063}; using kind = vertex_property_tag; }; +struct n2_t { enum { num = 23062}; using kind = vertex_property_tag; }; +struct n3_t { enum { num = 23061}; using kind = vertex_property_tag; }; +using VertexProperty = property< n1_t, int, property< n2_t, double, - property< n3_t, MyStruct > > > VertexProperty; + property>>; //====== edge properties -struct e1_t { enum { num = 23064}; typedef edge_property_tag kind; }; -typedef property EdgeProperty; +struct e1_t { enum { num = 23064}; using kind = edge_property_tag; }; +using EdgeProperty = property; //===== graph types -typedef - adjacency_list - Graph1; +using Graph1 = + adjacency_list; -typedef - adjacency_list - Graph2; +using Graph2 = + adjacency_list; @@ -84,7 +82,7 @@ main() // read Graph2, incomplete data in a different order. Write it diffently. Graph2 g31; std::ifstream readFile31("data3.txt"); - typedef property< n3_t, MyStruct, property< n1_t, int > > readNodeProp; + using readNodeProp = property>; readFile31 >> read( g31, readNodeProp() , EdgeProperty() ); std::cout << "graph g31 from file data3.txt:\n" << write( g31, property(), EdgeProperty() ) diff --git a/example/adjacency_matrix.cpp b/example/adjacency_matrix.cpp index accbfca1..edd3d304 100644 --- a/example/adjacency_matrix.cpp +++ b/example/adjacency_matrix.cpp @@ -20,7 +20,7 @@ int main() // A directed graph - typedef adjacency_matrix Graph; + using Graph = adjacency_matrix; Graph g(N); add_edge(B, C, g); add_edge(B, F, g); @@ -44,7 +44,7 @@ int main() // An undirected graph - typedef adjacency_matrix UGraph; + using UGraph = adjacency_matrix; UGraph ug(N); add_edge(B, C, ug); add_edge(B, F, ug); diff --git a/example/astar-cities.cpp b/example/astar-cities.cpp index 48b3260f..3bee02ba 100644 --- a/example/astar-cities.cpp +++ b/example/astar-cities.cpp @@ -21,10 +21,9 @@ #include #include #include -#include // for sqrt +#include // for sqrt using namespace boost; -using namespace std; // auxiliary types @@ -32,7 +31,7 @@ struct location { float y, x; // lat, long }; -typedef float cost; +using cost = float; template class city_writer { @@ -43,7 +42,7 @@ class city_writer { : name(n), loc(l), minx(_minx), maxx(_maxx), miny(_miny), maxy(_maxy), ptx(_ptx), pty(_pty) {} template - void operator()(ostream& out, const Vertex& v) const { + void operator()(std::ostream& out, const Vertex& v) const { float px = 1 - (loc[v].x - minx) / (maxx - minx); float py = (loc[v].y - miny) / (maxy - miny); out << "[label=\"" << name[v] << "\", pos=\"" @@ -62,8 +61,8 @@ template class time_writer { public: time_writer(WeightMap w) : wm(w) {} - template - void operator()(ostream &out, const Edge& e) const { + template + void operator()(std::ostream &out, const Edge& e) const { out << "[label=\"" << wm[e] << "\", fontsize=\"11\"]"; } private: @@ -76,13 +75,13 @@ template class distance_heuristic : public astar_heuristic { public: - typedef typename graph_traits::vertex_descriptor Vertex; + using Vertex = typename graph_traits::vertex_descriptor; distance_heuristic(LocMap l, Vertex goal) : m_location(l), m_goal(goal) {} CostType operator()(Vertex u) { - CostType dx = m_location[m_goal].x - m_location[u].x; - CostType dy = m_location[m_goal].y - m_location[u].y; + auto dx = m_location[m_goal].x - m_location[u].x; + auto dy = m_location[m_goal].y - m_location[u].y; return ::sqrt(dx * dx + dy * dy); } private: @@ -113,12 +112,11 @@ int main(int argc, char **argv) { // specify some types - typedef adjacency_list > mygraph_t; - typedef property_map::type WeightMap; - typedef mygraph_t::vertex_descriptor vertex; - typedef mygraph_t::edge_descriptor edge_descriptor; - typedef std::pair edge; + using mygraph_t = adjacency_list>; + using WeightMap = property_map::type; + using vertex = mygraph_t::vertex_descriptor; + using edge = std::pair; // specify data enum nodes { @@ -160,25 +158,24 @@ int main(int argc, char **argv) // create graph mygraph_t g(N); - WeightMap weightmap = get(edge_weight, g); + auto weightmap = get(edge_weight, g); for(std::size_t j = 0; j < num_edges; ++j) { - edge_descriptor e; bool inserted; - boost::tie(e, inserted) = add_edge(edge_array[j].first, + const auto [e, inserted] = add_edge(edge_array[j].first, edge_array[j].second, g); weightmap[e] = weights[j]; } // pick random start/goal - boost::mt19937 gen(time(0)); - vertex start = random_vertex(g, gen); - vertex goal = random_vertex(g, gen); + boost::mt19937 gen(time(nullptr)); + auto start = random_vertex(g, gen); + auto goal = random_vertex(g, gen); - cout << "Start vertex: " << name[start] << endl; - cout << "Goal vertex: " << name[goal] << endl; + std::cout << "Start vertex: " << name[start] << std::endl; + std::cout << "Goal vertex: " << name[goal] << std::endl; - ofstream dotfile; + std::ofstream dotfile; dotfile.open("test-astar-cities.dot"); write_graphviz(dotfile, g, city_writer @@ -187,8 +184,8 @@ int main(int argc, char **argv) time_writer(weightmap)); - vector p(num_vertices(g)); - vector d(num_vertices(g)); + std::vector p(num_vertices(g)); + std::vector d(num_vertices(g)); try { // call astar named parameter interface astar_search_tree @@ -201,24 +198,23 @@ int main(int argc, char **argv) } catch(found_goal fg) { // found a path to the goal - list shortest_path; - for(vertex v = goal;; v = p[v]) { - shortest_path.push_front(v); + std::list shortest_path; + for(auto v = goal;; v = p[v]) { + shortest_path.emplace_front(v); if(p[v] == v) break; } - cout << "Shortest path from " << name[start] << " to " + std::cout << "Shortest path from " << name[start] << " to " << name[goal] << ": "; - list::iterator spi = shortest_path.begin(); - cout << name[start]; - for(++spi; spi != shortest_path.end(); ++spi) - cout << " -> " << name[*spi]; - cout << endl << "Total travel time: " << d[goal] << endl; + std::cout << name[start]; + for(const auto& sp : shortest_path) + std::cout << " -> " << name[sp]; + std::cout << std::endl << "Total travel time: " << d[goal] << std::endl; return 0; } - cout << "Didn't find a path from " << name[start] << "to" - << name[goal] << "!" << endl; + std::cout << "Didn't find a path from " << name[start] << "to" + << name[goal] << "!" << std::endl; return 0; } diff --git a/example/astar_maze.cpp b/example/astar_maze.cpp index fad2c208..c0a7475f 100644 --- a/example/astar_maze.cpp +++ b/example/astar_maze.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include @@ -38,12 +37,12 @@ boost::mt19937 random_generator; // Distance traveled in the maze -typedef double distance; +using distance = double; #define GRID_RANK 2 -typedef boost::grid_graph grid; -typedef boost::graph_traits::vertex_descriptor vertex_descriptor; -typedef boost::graph_traits::vertices_size_type vertices_size_type; +using grid = boost::grid_graph; +using vertex_descriptor = boost::graph_traits::vertex_descriptor; +using vertices_size_type = boost::graph_traits::vertices_size_type; // A hash function for vertices. struct vertex_hash:std::unary_function { @@ -55,9 +54,8 @@ struct vertex_hash:std::unary_function { } }; -typedef boost::unordered_set vertex_set; -typedef boost::vertex_subset_complement_filter::type - filtered_grid; +using vertex_set = boost::unordered_set; +using filtered_grid = boost::vertex_subset_complement_filter::type; // A searchable maze // @@ -166,20 +164,20 @@ struct astar_goal_visitor:public boost::default_astar_visitor { bool maze::solve() { boost::static_property_map weight(1); // The predecessor map is a vertex-to-vertex mapping. - typedef boost::unordered_map pred_map; + vertex_hash>; pred_map predecessor; boost::associative_property_map pred_pmap(predecessor); // The distance map is a vertex-to-distance mapping. - typedef boost::unordered_map dist_map; + vertex_hash>; dist_map distance; boost::associative_property_map dist_pmap(distance); - vertex_descriptor s = source(); - vertex_descriptor g = goal(); + auto s = source(); + auto g = goal(); euclidean_heuristic heuristic(g); astar_goal_visitor visitor(g); @@ -192,7 +190,7 @@ bool maze::solve() { } catch(found_goal fg) { // Walk backwards from the goal through the predecessor chain adding // vertices to the solution path. - for (vertex_descriptor u = g; u != s; u = predecessor[u]) + for (auto u = g; u != s; u = predecessor[u]) m_solution.insert(u); m_solution.insert(s); m_solution_length = distance[g]; @@ -248,7 +246,7 @@ std::size_t random_int(std::size_t a, std::size_t b) { if (b < a) b = a; boost::uniform_int<> dist(a, b); - boost::variate_generator > + boost::variate_generator> generate(random_generator, dist); return generate(); } @@ -256,9 +254,9 @@ std::size_t random_int(std::size_t a, std::size_t b) { // Generate a maze with a random assignment of barriers. maze random_maze(std::size_t x, std::size_t y) { maze m(x, y); - vertices_size_type n = num_vertices(m.m_grid); - vertex_descriptor s = m.source(); - vertex_descriptor g = m.goal(); + auto n = num_vertices(m.m_grid); + auto s = m.source(); + auto g = m.goal(); // One quarter of the cells in the maze should be barriers. int barriers = n/4; while (barriers > 0) { @@ -267,7 +265,7 @@ maze random_maze(std::size_t x, std::size_t y) { // Walls range up to one quarter the dimension length in this direction. vertices_size_type wall = random_int(1, m.length(direction)/4); // Create the wall while decrementing the total barrier count. - vertex_descriptor u = vertex(random_int(0, n-1), m.m_grid); + auto u = vertex(random_int(0, n-1), m.m_grid); while (wall) { // Start and goal spaces should never be barriers. if (u != s && u != g) { @@ -277,7 +275,7 @@ maze random_maze(std::size_t x, std::size_t y) { barriers--; } } - vertex_descriptor v = m.m_grid.next(u, direction); + auto v = m.m_grid.next(u, direction); // Stop creating this wall if we reached the maze's edge. if (u == v) break; @@ -295,12 +293,12 @@ int main (int argc, char const *argv[]) { std::size_t y = 10; if (argc == 3) { - x = boost::lexical_cast(argv[1]); - y = boost::lexical_cast(argv[2]); + x = std::stoul(argv[1]); + y = std::stoul(argv[2]); } - random_generator.seed(std::time(0)); - maze m = random_maze(x, y); + random_generator.seed(std::time(nullptr)); + auto m = random_maze(x, y); if (m.solve()) std::cout << "Solved the maze." << std::endl; diff --git a/example/bellman-example.cpp b/example/bellman-example.cpp index e6ec8750..a0f8f3dc 100644 --- a/example/bellman-example.cpp +++ b/example/bellman-example.cpp @@ -12,10 +12,11 @@ #include #include #include +#include "range_pair.hpp" using namespace boost; -template < typename Graph, typename ParentMap > +template struct edge_writer { edge_writer(const Graph & g, const ParentMap & p) @@ -23,12 +24,11 @@ struct edge_writer { } - template < typename Edge > + template void operator() (std::ostream & out, const Edge & e) const { out << "[label=\"" << get(edge_weight, m_g, e) << "\""; - typename graph_traits < Graph >::vertex_descriptor - u = source(e, m_g), v = target(e, m_g); + auto u = source(e, m_g), v = target(e, m_g); if (m_parent[v] == u) out << ", color=\"black\""; else @@ -38,11 +38,11 @@ struct edge_writer const Graph & m_g; ParentMap m_parent; }; -template < typename Graph, typename Parent > -edge_writer < Graph, Parent > +template +edge_writer make_edge_writer(const Graph & g, const Parent & p) { - return edge_writer < Graph, Parent > (g, p); + return edge_writer (g, p); } struct EdgeProperties { @@ -54,14 +54,14 @@ main() { enum { u, v, x, y, z, N }; char name[] = { 'u', 'v', 'x', 'y', 'z' }; - typedef std::pair < int, int >E; + using E = std::pair; const int n_edges = 10; E edge_array[] = { E(u, y), E(u, x), E(u, v), E(v, u), E(x, y), E(x, v), E(y, v), E(y, z), E(z, u), E(z,x) }; int weight[n_edges] = { -4, 8, 5, -2, 9, -3, 7, 2, 6, 7 }; - typedef adjacency_list < vecS, vecS, directedS, - no_property, EdgeProperties> Graph; + using Graph = adjacency_list < vecS, vecS, directedS, + no_property, EdgeProperties>; #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 // VC++ can't handle the iterator constructor Graph g(N); @@ -70,14 +70,14 @@ main() #else Graph g(edge_array, edge_array + n_edges, N); #endif - graph_traits < Graph >::edge_iterator ei, ei_end; - property_map::type - weight_pmap = get(&EdgeProperties::weight, g); + auto weight_pmap = get(&EdgeProperties::weight, g); int i = 0; - for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei, ++i) - weight_pmap[*ei] = weight[i]; + for (const auto& edge : make_range_pair(edges(g))) { + weight_pmap[edge] = weight[i]; + ++i; + } - std::vector distance(N, (std::numeric_limits < short >::max)()); + std::vector distance(N, (std::numeric_limits::max)()); std::vector parent(N); for (i = 0; i < N; ++i) parent[i] = i; @@ -108,10 +108,8 @@ main() << " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n"; { - for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) { - graph_traits < Graph >::edge_descriptor e = *ei; - graph_traits < Graph >::vertex_descriptor - u = source(e, g), v = target(e, g); + for(const auto& e : make_range_pair(edges(g))) { + auto u = source(e, g), v = target(e, g); // VC++ doesn't like the 3-argument get function, so here // we workaround by using 2-nested get()'s. dot_file << name[u] << " -> " << name[v] diff --git a/example/bellman-ford-internet.cpp b/example/bellman-ford-internet.cpp index 24bbfef4..ee454815 100644 --- a/example/bellman-ford-internet.cpp +++ b/example/bellman-ford-internet.cpp @@ -16,19 +16,18 @@ main() // ID numbers for the routers (vertices). enum { A, B, C, D, E, F, G, H, n_vertices }; - const int n_edges = 11; - typedef std::pair < int, int >Edge; + using Edge = std::pair; // The list of connections between routers stored in an array. - Edge edges[] = { - Edge(A, B), Edge(A, C), - Edge(B, D), Edge(B, E), Edge(C, E), Edge(C, F), Edge(D, H), - Edge(D, E), Edge(E, H), Edge(F, G), Edge(G, H) + const auto edges = { + Edge(A, B), Edge(A, C), + Edge(B, D), Edge(B, E), Edge(C, E), Edge(C, F), Edge(D, H), + Edge(D, E), Edge(E, H), Edge(F, G), Edge(G, H) }; // Specify the graph type and declare a graph object - typedef edge_list < Edge*, Edge, std::ptrdiff_t, std::random_access_iterator_tag> Graph; - Graph g(edges, edges + n_edges); + using Graph = edge_list ; + Graph g(std::begin(edges), std::end(edges)); // The transmission delay values for each edge. float delay[] = @@ -40,7 +39,7 @@ main() for (int i = 0; i < n_vertices; ++i) parent[i] = i; float distance[n_vertices]; - std::fill(distance, distance + n_vertices, (std::numeric_limits < float >::max)()); + std::fill(distance, distance + n_vertices, (std::numeric_limits::max)()); // Specify A as the source vertex distance[A] = 0; diff --git a/example/bfs-example.cpp b/example/bfs-example.cpp index 0436204c..1723ede1 100644 --- a/example/bfs-example.cpp +++ b/example/bfs-example.cpp @@ -13,11 +13,11 @@ #include using namespace boost; -template < typename TimeMap > class bfs_time_visitor:public default_bfs_visitor { - typedef typename property_traits < TimeMap >::value_type T; +template class bfs_time_visitor:public default_bfs_visitor { + using T = typename property_traits::value_type; public: bfs_time_visitor(TimeMap tmap, T & t):m_timemap(tmap), m_time(t) { } - template < typename Vertex, typename Graph > + template void discover_vertex(Vertex u, const Graph & g) const { put(m_timemap, u, m_time++); @@ -32,12 +32,12 @@ main() { using namespace boost; // Select the graph type we wish to use - typedef adjacency_list < vecS, vecS, undirectedS > graph_t; + using graph_t = adjacency_list; // Set up the vertex IDs and names enum { r, s, t, u, v, w, x, y, N }; const char *name = "rstuvwxy"; // Specify the edges in the graph - typedef std::pair < int, int >E; + using E = std::pair; E edge_array[] = { E(r, s), E(r, v), E(s, w), E(w, r), E(w, t), E(w, x), E(x, t), E(t, u), E(x, y), E(u, y) }; @@ -49,31 +49,30 @@ main() for (std::size_t j = 0; j < n_edges; ++j) add_edge(edge_array[j].first, edge_array[j].second, g); #else - typedef graph_traits::vertices_size_type v_size_t; + using v_size_t = graph_traits::vertices_size_type; graph_t g(edge_array, edge_array + n_edges, v_size_t(N)); #endif // Typedefs - typedef graph_traits < graph_t >::vertices_size_type Size; + using Size = graph_traits::vertices_size_type; // a vector to hold the discover time property for each vertex - std::vector < Size > dtime(num_vertices(g)); - typedef + std::vector dtime(num_vertices(g)); + using dtime_pm_type = iterator_property_map::iterator, - property_map::const_type> - dtime_pm_type; + property_map::const_type>; dtime_pm_type dtime_pm(dtime.begin(), get(vertex_index, g)); Size time = 0; - bfs_time_visitor < dtime_pm_type >vis(dtime_pm, time); + bfs_time_visitor vis(dtime_pm, time); breadth_first_search(g, vertex(s, g), visitor(vis)); // Use std::sort to order the vertices by their discover time std::vector::vertices_size_type > discover_order(N); - integer_range < int >range(0, N); + integer_range range(0, N); std::copy(range.begin(), range.end(), discover_order.begin()); std::sort(discover_order.begin(), discover_order.end(), - indirect_cmp < dtime_pm_type, std::less < Size > >(dtime_pm)); + indirect_cmp>(dtime_pm)); std::cout << "order of discovery: "; for (int i = 0; i < N; ++i) diff --git a/example/bfs-example2.cpp b/example/bfs-example2.cpp index bc5d92ab..74038f3b 100644 --- a/example/bfs-example2.cpp +++ b/example/bfs-example2.cpp @@ -10,15 +10,16 @@ #include #include #include +#include "range_pair.hpp" #include using namespace boost; -template < typename TimeMap > class bfs_time_visitor:public default_bfs_visitor { - typedef typename property_traits < TimeMap >::value_type T; +template class bfs_time_visitor:public default_bfs_visitor { + using T = typename property_traits::value_type; public: bfs_time_visitor(TimeMap tmap, T & t):m_timemap(tmap), m_time(t) { } - template < typename Vertex, typename Graph > + template void discover_vertex(Vertex u, const Graph & g) const { put(m_timemap, u, m_time++); @@ -39,13 +40,13 @@ main() { using namespace boost; // Select the graph type we wish to use - typedef adjacency_list < listS, listS, undirectedS, - VertexProps> graph_t; + using graph_t = adjacency_list; // Set up the vertex IDs and names enum { r, s, t, u, v, w, x, y, N }; const char *name = "rstuvwxy"; // Specify the edges in the graph - typedef std::pair < int, int >E; + using E = std::pair; E edge_array[] = { E(r, s), E(r, v), E(s, w), E(w, r), E(w, t), E(w, x), E(x, t), E(t, u), E(x, y), E(u, y) }; @@ -56,41 +57,39 @@ main() graph_t g; std::vector::vertex_descriptor> verts; for (std::size_t i = 0; i < N; ++i) - verts.push_back(add_vertex(g)); + verts.emplace_back(add_vertex(g)); for (std::size_t j = 0; j < n_edges; ++j) add_edge(verts[edge_array[j].first], verts[edge_array[j].second], g); #else - typedef graph_traits::vertices_size_type v_size_t; + using v_size_t = graph_traits::vertices_size_type; graph_t g(edge_array, edge_array + n_edges, v_size_t(N)); #endif // Typedefs - typedef graph_traits::vertices_size_type Size; + using Size = graph_traits::vertices_size_type; Size time = 0; - typedef property_map::type dtime_map_t; - dtime_map_t dtime_map = get(&VertexProps::discover_time, g); - bfs_time_visitor < dtime_map_t > vis(dtime_map, time); + using dtime_map_t = property_map::type; + auto dtime_map = get(&VertexProps::discover_time, g); + bfs_time_visitor vis(dtime_map, time); breadth_first_search(g, vertex(s, g), color_map(get(&VertexProps::color, g)). visitor(vis)); // a vector to hold the discover time property for each vertex - std::vector < Size > dtime(num_vertices(g)); - typedef + std::vector dtime(num_vertices(g)); + using dtime_pm_type = iterator_property_map::iterator, - property_map::type> - dtime_pm_type; - graph_traits::vertex_iterator vi, vi_end; + property_map::type>; std::size_t c = 0; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi, ++c) { - dtime[c] = dtime_map[*vi]; - put(&VertexProps::index, g, *vi, c); + for (const auto& vertex : make_range_pair(vertices(g))) { + dtime[c] = dtime_map[vertex]; + put(&VertexProps::index, g, vertex, c); } dtime_pm_type dtime_pm(dtime.begin(), get(&VertexProps::index, g)); // Use std::sort to order the vertices by their discover time std::vector::vertices_size_type > discover_order(N); - integer_range < int >range(0, N); + integer_range range(0, N); std::copy(range.begin(), range.end(), discover_order.begin()); std::sort(discover_order.begin(), discover_order.end(), make_indirect_cmp( diff --git a/example/bfs-name-printer.cpp b/example/bfs-name-printer.cpp index aab408d9..a70e8f19 100644 --- a/example/bfs-name-printer.cpp +++ b/example/bfs-name-printer.cpp @@ -17,34 +17,30 @@ void build_router_network(Graph & g, VertexNameMap name_map, TransDelayMap delay_map) { - typename graph_traits < Graph >::vertex_descriptor a, b, c, d, e; - a = add_vertex(g); + auto a = add_vertex(g); name_map[a] = 'a'; - b = add_vertex(g); + auto b = add_vertex(g); name_map[b] = 'b'; - c = add_vertex(g); + auto c = add_vertex(g); name_map[c] = 'c'; - d = add_vertex(g); + auto d = add_vertex(g); name_map[d] = 'd'; - e = add_vertex(g); + auto e = add_vertex(g); name_map[e] = 'e'; - typename graph_traits::edge_descriptor ed; - bool inserted; - - boost::tie(ed, inserted) = add_edge(a, b, g); + auto [ed, inserted] = add_edge(a, b, g); delay_map[ed] = 1.2; - boost::tie(ed, inserted) = add_edge(a, d, g); + std::tie(ed, inserted) = add_edge(a, d, g); delay_map[ed] = 4.5; - boost::tie(ed, inserted) = add_edge(b, d, g); + std::tie(ed, inserted) = add_edge(b, d, g); delay_map[ed] = 1.8; - boost::tie(ed, inserted) = add_edge(c, a, g); + std::tie(ed, inserted) = add_edge(c, a, g); delay_map[ed] = 2.6; - boost::tie(ed, inserted) = add_edge(c, e, g); + std::tie(ed, inserted) = add_edge(c, e, g); delay_map[ed] = 5.2; - boost::tie(ed, inserted) = add_edge(d, c, g); + std::tie(ed, inserted) = add_edge(d, c, g); delay_map[ed] = 0.4; - boost::tie(ed, inserted) = add_edge(d, e, g); + std::tie(ed, inserted) = add_edge(d, e, g); delay_map[ed] = 3.3; } @@ -76,16 +72,16 @@ struct EP { int main() { - typedef adjacency_list < listS, vecS, directedS, VP, EP> graph_t; + using graph_t = adjacency_list < listS, vecS, directedS, VP, EP>; graph_t g; - property_map::type name_map = get(&VP::name, g); - property_map::type delay_map = get(&EP::weight, g); + auto name_map = get(&VP::name, g); + auto delay_map = get(&EP::weight, g); build_router_network(g, name_map, delay_map); - typedef property_map::type VertexNameMap; - graph_traits::vertex_descriptor a = *vertices(g).first; + using VertexNameMap = property_map::type; + auto a = *vertices(g).first; bfs_name_printer vis(name_map); std::cout << "BFS vertex discover order: "; breadth_first_search(g, a, visitor(vis)); diff --git a/example/bfs.cpp b/example/bfs.cpp index 1f34c155..0c6fd746 100644 --- a/example/bfs.cpp +++ b/example/bfs.cpp @@ -69,9 +69,9 @@ struct print_parent { template struct graph_copier - : public boost::base_visitor > + : public boost::base_visitor> { - typedef Tag event_filter; + using event_filter = Tag; graph_copier(NewGraph& graph) : new_g(graph) { } @@ -91,13 +91,13 @@ copy_graph(NewGraph& g, Tag) { int main(int , char* []) { - typedef boost::adjacency_list< + using Graph = boost::adjacency_list< boost::mapS, boost::vecS, boost::bidirectionalS, boost::property > > > - > Graph; + boost::property>>> + >; Graph G(5); boost::add_edge(0, 2, G); @@ -112,7 +112,7 @@ int main(int , char* []) boost::add_edge(4, 0, G); boost::add_edge(4, 1, G); - typedef Graph::vertex_descriptor Vertex; + using Vertex = Graph::vertex_descriptor; Graph G_copy(5); // Array to store predecessor (parent) of each vertex. This will be @@ -120,7 +120,7 @@ int main(int , char* []) std::vector p(boost::num_vertices(G)); // VC++ version of std::vector has no ::pointer, so // I use ::value_type* instead. - typedef std::vector::value_type* Piter; + using Piter = std::vector::value_type*; // Array to store distances from the source to each vertex . We use // a built-in array here just for variety. This will also be used as @@ -129,7 +129,7 @@ int main(int , char* []) std::fill_n(d, 5, 0); // The source vertex - Vertex s = *(boost::vertices(G).first); + auto s = *(boost::vertices(G).first); p[s] = s; boost::breadth_first_search (G, s, diff --git a/example/bfs_neighbor.cpp b/example/bfs_neighbor.cpp index 050362f6..1f18b6c6 100644 --- a/example/bfs_neighbor.cpp +++ b/example/bfs_neighbor.cpp @@ -64,9 +64,9 @@ struct print_parent { template struct graph_copier - : public boost::base_visitor > + : public boost::base_visitor> { - typedef Tag event_filter; + using event_filter = Tag; graph_copier(NewGraph& graph) : new_g(graph) { } @@ -86,13 +86,13 @@ copy_graph(NewGraph& g, Tag) { int main(int , char* []) { - typedef boost::adjacency_list< + using Graph = boost::adjacency_list< boost::mapS, boost::vecS, boost::bidirectionalS, boost::property > > > - > Graph; + boost::property>>> + >; Graph G(5); boost::add_edge(0, 2, G); @@ -107,14 +107,14 @@ int main(int , char* []) boost::add_edge(4, 0, G); boost::add_edge(4, 1, G); - typedef Graph::vertex_descriptor Vertex; + using Vertex = Graph::vertex_descriptor; // Array to store predecessor (parent) of each vertex. This will be // used as a Decorator (actually, its iterator will be). std::vector p(boost::num_vertices(G)); // VC++ version of std::vector has no ::pointer, so // I use ::value_type* instead. - typedef std::vector::value_type* Piter; + using Piter = std::vector::value_type*; // Array to store distances from the source to each vertex . We use // a built-in array here just for variety. This will also be used as @@ -123,7 +123,7 @@ int main(int , char* []) std::fill_n(d, 5, 0); // The source vertex - Vertex s = *(boost::vertices(G).first); + auto s = *(boost::vertices(G).first); p[s] = s; boost::neighbor_breadth_first_search (G, s, diff --git a/example/biconnected_components.cpp b/example/biconnected_components.cpp index 0406dcba..6588ae31 100644 --- a/example/biconnected_components.cpp +++ b/example/biconnected_components.cpp @@ -12,6 +12,7 @@ #include #include #include +#include "range_pair.hpp" namespace boost { @@ -19,7 +20,7 @@ namespace boost { enum { num = 555 }; - typedef edge_property_tag kind; + using kind = edge_property_tag; } edge_component; } @@ -28,9 +29,9 @@ int main() { using namespace boost; - typedef adjacency_list < vecS, vecS, undirectedS, - no_property, property < edge_component_t, std::size_t > >graph_t; - typedef graph_traits < graph_t >::vertex_descriptor vertex_t; + using graph_t = adjacency_list>; + using vertex_t = graph_traits::vertex_descriptor; graph_t g(9); add_edge(0, 5, g); add_edge(0, 1, g); @@ -44,10 +45,9 @@ main() add_edge(6, 7, g); add_edge(7, 8, g); - property_map < graph_t, edge_component_t >::type - component = get(edge_component, g); + auto component = get(edge_component, g); - std::size_t num_comps = biconnected_components(g, component); + auto num_comps = biconnected_components(g, component); std::cerr << "Found " << num_comps << " biconnected components.\n"; std::vector art_points; @@ -62,11 +62,10 @@ main() << std::endl; } - graph_traits < graph_t >::edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) - std::cout << (char)(source(*ei, g) + 'A') << " -- " - << (char)(target(*ei, g) + 'A') - << "[label=\"" << component[*ei] << "\"]\n"; + for(const auto& edge : make_range_pair(edges(g))) + std::cout << (char)(source(edge, g) + 'A') << " -- " + << (char)(target(edge, g) + 'A') + << "[label=\"" << component[edge] << "\"]\n"; std::cout << "}\n"; return 0; diff --git a/example/bipartite_example.cpp b/example/bipartite_example.cpp index c8e62ad2..62ae8af8 100644 --- a/example/bipartite_example.cpp +++ b/example/bipartite_example.cpp @@ -13,6 +13,7 @@ #include #include #include +#include "range_pair.hpp" using namespace boost; @@ -21,8 +22,7 @@ using namespace boost; template void print_bipartite (const Graph& g) { - typedef graph_traits traits; - typename traits::vertex_iterator vertex_iter, vertex_end; + using traits = graph_traits ; /// Most simple interface just tests for bipartiteness. @@ -30,9 +30,9 @@ void print_bipartite (const Graph& g) if (bipartite) { - typedef std::vector partition_t; - typedef typename property_map ::type index_map_t; - typedef iterator_property_map partition_map_t; + using partition_t = std::vector ; + using index_map_t = typename property_map ::type; + using partition_map_t = iterator_property_map ; partition_t partition (num_vertices (g)); partition_map_t partition_map (partition.begin (), get (vertex_index, g)); @@ -41,15 +41,15 @@ void print_bipartite (const Graph& g) is_bipartite (g, get (vertex_index, g), partition_map); - for (boost::tie (vertex_iter, vertex_end) = vertices (g); vertex_iter != vertex_end; ++vertex_iter) + for (const auto& vertex : make_range_pair(vertices (g))) { - std::cout << "Vertex " << *vertex_iter << " has color " << (get (partition_map, *vertex_iter) == color_traits < + std::cout << "Vertex " << vertex << " has color " << (get (partition_map, vertex) == color_traits < default_color_type>::white () ? "white" : "black") << std::endl; } } else { - typedef std::vector vertex_vector_t; + using vertex_vector_t = std::vector ; vertex_vector_t odd_cycle; /// A third interface yields an odd-cycle if the graph is not bipartite. @@ -67,8 +67,8 @@ void print_bipartite (const Graph& g) int main (int argc, char **argv) { - typedef adjacency_list vector_graph_t; - typedef std::pair E; + using vector_graph_t = adjacency_list ; + using E = std::pair ; /** * Create the graph drawn below. diff --git a/example/boost_web_graph.cpp b/example/boost_web_graph.cpp index 53da8263..626979c5 100644 --- a/example/boost_web_graph.cpp +++ b/example/boost_web_graph.cpp @@ -18,6 +18,7 @@ #include #include #include +#include "range_pair.hpp" template @@ -30,9 +31,8 @@ class calc_distance_visitor : public boost::bfs_visitor<> void tree_edge(typename boost::graph_traits::edge_descriptor e, Graph& g) { - typename boost::graph_traits::vertex_descriptor u, v; - u = boost::source(e, g); - v = boost::target(e, g); + auto u = boost::source(e, g); + auto v = boost::target(e, g); distance[v] = distance[u] + 1; } private: @@ -50,7 +50,7 @@ class print_tree_visitor : public boost::dfs_visitor<> discover_vertex(typename boost::graph_traits::vertex_descriptor v, Graph&) { - typedef typename boost::property_traits::value_type Dist; + using Dist = typename boost::property_traits::value_type; // indentation based on depth for (Dist i = 0; i < distance[v]; ++i) std::cout << " "; @@ -83,23 +83,22 @@ main() //=========================================================================== // Declare the graph type and object, and some property maps. - typedef adjacency_list >, - property > - > Graph; + property>, + property> + >; - typedef graph_traits Traits; - typedef Traits::vertex_descriptor Vertex; - typedef Traits::edge_descriptor Edge; + using Traits = graph_traits; + using Vertex = Traits::vertex_descriptor; + using Edge = Traits::edge_descriptor; - typedef std::map NameVertexMap; + using NameVertexMap = std::map; NameVertexMap name2vertex; Graph g; - typedef property_map::type NameMap; - NameMap node_name = get(vertex_name, g); - property_map::type link_name = get(edge_name, g); + auto node_name = get(vertex_name, g); + auto link_name = get(edge_name, g); //=========================================================================== // Read the data file and construct the graph. @@ -110,13 +109,11 @@ main() std::list line_toks; boost::stringtok(line_toks, line, "|"); - NameVertexMap::iterator pos; - bool inserted; Vertex u, v; - std::list::iterator i = line_toks.begin(); + auto i = line_toks.begin(); - boost::tie(pos, inserted) = name2vertex.insert(std::make_pair(*i, Vertex())); + auto [pos, inserted] = name2vertex.insert(std::make_pair(*i, Vertex())); if (inserted) { u = add_vertex(g); put(node_name, u, *i); @@ -125,9 +122,9 @@ main() u = pos->second; ++i; - std::string hyperlink_name = *i++; + auto hyperlink_name = *i++; - boost::tie(pos, inserted) = name2vertex.insert(std::make_pair(*i, Vertex())); + std::tie(pos, inserted) = name2vertex.insert(std::make_pair(*i, Vertex())); if (inserted) { v = add_vertex(g); put(node_name, v, *i); @@ -136,7 +133,7 @@ main() v = pos->second; Edge e; - boost::tie(e, inserted) = add_edge(u, v, g); + std::tie(e, inserted) = add_edge(u, v, g); if (inserted) { put(link_name, e, hyperlink_name); } @@ -145,8 +142,8 @@ main() //=========================================================================== // Calculate the diameter of the graph. - typedef Traits::vertices_size_type size_type; - typedef std::vector IntVector; + using size_type = Traits::vertices_size_type; + using IntVector = std::vector; // Create N x N matrix for storing the shortest distances // between each vertex. Initialize all distances to zero. std::vector d_matrix(num_vertices(g), @@ -155,7 +152,7 @@ main() size_type i; for (i = 0; i < num_vertices(g); ++i) { calc_distance_visitor vis(&d_matrix[i][0]); - Traits::vertex_descriptor src = vertices(g).first[i]; + auto src = vertices(g).first[i]; breadth_first_search(g, src, boost::visitor(vis)); } @@ -169,9 +166,8 @@ main() << std::endl << std::endl; std::cout << "Number of clicks from the home page: " << std::endl; - Traits::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) - std::cout << d_matrix[0][*vi] << "\t" << node_name[*vi] << std::endl; + for (const auto& vertex : make_range_pair(vertices(g))) + std::cout << d_matrix[0][vertex] << "\t" << node_name[vertex] << std::endl; std::cout << std::endl; //=========================================================================== @@ -179,12 +175,12 @@ main() // Create storage for a mapping from vertices to their parents std::vector parent(num_vertices(g)); - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) - parent[*vi] = *vi; + for (const auto& vertex : make_range_pair(vertices(g))) + parent[vertex] = vertex; // Do a BFS starting at the home page, recording the parent of each // vertex (where parent is with respect to the search tree). - Traits::vertex_descriptor src = vertices(g).first[0]; + auto src = vertices(g).first[0]; breadth_first_search (g, src, boost::visitor(make_bfs_visitor(record_predecessors(&parent[0], @@ -192,7 +188,7 @@ main() // Add all the search tree edges into a new graph Graph search_tree(num_vertices(g)); - boost::tie(vi, vi_end) = vertices(g); + auto [vi, vi_end] = vertices(g); ++vi; for (; vi != vi_end; ++vi) add_edge(parent[*vi], *vi, search_tree); @@ -203,10 +199,12 @@ main() // the tree nodes in the order that we want to print out: // a directory-structure like format. std::vector dfs_distances(num_vertices(g), 0); + + using NameMap = property_map::type; print_tree_visitor tree_printer(node_name, &dfs_distances[0]); - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) - get(vertex_color, g)[*vi] = white_color; + for(const auto& vertex : make_range_pair(vertices(g))) + get(vertex_color, g)[vertex] = white_color; depth_first_visit(search_tree, src, tree_printer, get(vertex_color, g)); return EXIT_SUCCESS; diff --git a/example/boykov_kolmogorov-eg.cpp b/example/boykov_kolmogorov-eg.cpp index dec3474f..d4fab036 100644 --- a/example/boykov_kolmogorov-eg.cpp +++ b/example/boykov_kolmogorov-eg.cpp @@ -36,6 +36,7 @@ #include #include #include +#include "range_pair.hpp" // Use a DIMACS network flow file as stdin. // boykov_kolmogorov-eg < max_flow.dat @@ -70,42 +71,38 @@ int main() { using namespace boost; - typedef adjacency_list_traits < vecS, vecS, directedS > Traits; - typedef adjacency_list < vecS, vecS, directedS, + using Traits = adjacency_list_traits; + using Graph = adjacency_list < vecS, vecS, directedS, property < vertex_name_t, std::string, property < vertex_index_t, long, property < vertex_color_t, boost::default_color_type, property < vertex_distance_t, long, - property < vertex_predecessor_t, Traits::edge_descriptor > > > > >, + property>>>>, property < edge_capacity_t, long, property < edge_residual_capacity_t, long, - property < edge_reverse_t, Traits::edge_descriptor > > > > Graph; + property>>>; Graph g; - property_map < Graph, edge_capacity_t >::type - capacity = get(edge_capacity, g); - property_map < Graph, edge_residual_capacity_t >::type - residual_capacity = get(edge_residual_capacity, g); - property_map < Graph, edge_reverse_t >::type rev = get(edge_reverse, g); + auto capacity = get(edge_capacity, g); + auto residual_capacity = get(edge_residual_capacity, g); + auto rev = get(edge_reverse, g); Traits::vertex_descriptor s, t; read_dimacs_max_flow(g, capacity, rev, s, t); std::vector color(num_vertices(g)); std::vector distance(num_vertices(g)); - long flow = boykov_kolmogorov_max_flow(g ,s, t); + auto flow = boykov_kolmogorov_max_flow(g ,s, t); std::cout << "c The total flow:" << std::endl; std::cout << "s " << flow << std::endl << std::endl; std::cout << "c flow values:" << std::endl; - graph_traits < Graph >::vertex_iterator u_iter, u_end; - graph_traits < Graph >::out_edge_iterator ei, e_end; - for (boost::tie(u_iter, u_end) = vertices(g); u_iter != u_end; ++u_iter) - for (boost::tie(ei, e_end) = out_edges(*u_iter, g); ei != e_end; ++ei) - if (capacity[*ei] > 0) - std::cout << "f " << *u_iter << " " << target(*ei, g) << " " - << (capacity[*ei] - residual_capacity[*ei]) << std::endl; + for(const auto& vertex : make_range_pair(vertices(g))) + for (const auto& edge : make_range_pair(out_edges(vertex, g))) + if (capacity[edge] > 0) + std::cout << "f " << vertex << " " << target(edge, g) << " " + << (capacity[edge] - residual_capacity[edge]) << std::endl; return EXIT_SUCCESS; } diff --git a/example/bron_kerbosch_clique_number.cpp b/example/bron_kerbosch_clique_number.cpp index 46b5c7a0..1c809fa9 100644 --- a/example/bron_kerbosch_clique_number.cpp +++ b/example/bron_kerbosch_clique_number.cpp @@ -12,24 +12,23 @@ #include "helper.hpp" -using namespace std; using namespace boost; // Declare the graph type and its vertex and edge types. -typedef undirected_graph<> Graph; -typedef graph_traits::vertex_descriptor Vertex; -typedef graph_traits::edge_descriptor Edge; +using Graph = undirected_graph<>; +using Vertex = graph_traits::vertex_descriptor; +using Edge = graph_traits::edge_descriptor; int main(int argc, char *argv[]) { // Create the graph and read it from standard input. Graph g; - read_graph(g, cin); + read_graph(g, std::cin); // Use the Bron-Kerbosch algorithm to find all cliques, and - size_t c = bron_kerbosch_clique_number(g); - cout << "clique number: " << c << endl; + auto c = bron_kerbosch_clique_number(g); + std::cout << "clique number: " << c << std::endl; return 0; } diff --git a/example/bron_kerbosch_print_cliques.cpp b/example/bron_kerbosch_print_cliques.cpp index 93030edd..6aeae032 100644 --- a/example/bron_kerbosch_print_cliques.cpp +++ b/example/bron_kerbosch_print_cliques.cpp @@ -12,7 +12,6 @@ #include "helper.hpp" -using namespace std; using namespace boost; // The clique_printer is a visitor that will print the vertices that comprise @@ -28,11 +27,10 @@ struct clique_printer void clique(const Clique& c, const Graph& g) { // Iterate over the clique and print each vertex within it. - typename Clique::const_iterator i, end = c.end(); - for(i = c.begin(); i != end; ++i) { - os << g[*i].name << " "; + for(const auto& vertex : c) { + os << g[vertex].name << " "; } - os << endl; + os << std::endl; } OutputStream& os; }; @@ -40,17 +38,17 @@ struct clique_printer // The Actor type stores the name of each vertex in the graph. struct Actor { - string name; + std::string name; }; // Declare the graph type and its vertex and edge types. -typedef undirected_graph Graph; -typedef graph_traits::vertex_descriptor Vertex; -typedef graph_traits::edge_descriptor Edge; +using Graph = undirected_graph; +using Vertex = graph_traits::vertex_descriptor; +using Edge = graph_traits::edge_descriptor; // The name map provides an abstract accessor for the names of // each vertex. This is used during graph creation. -typedef property_map::type NameMap; +using NameMap = property_map::type; int main(int argc, char *argv[]) @@ -60,10 +58,10 @@ main(int argc, char *argv[]) NameMap nm(get(&Actor::name, g)); // Read the graph from standard input. - read_graph(g, nm, cin); + read_graph(g, nm, std::cin); // Instantiate the visitor for printing cliques - clique_printer vis(cout); + clique_printer vis(std::cout); // Use the Bron-Kerbosch algorithm to find all cliques, printing them // as they are found. diff --git a/example/bucket_sorter.cpp b/example/bucket_sorter.cpp index 0b5ca055..0b7d4f49 100644 --- a/example/bucket_sorter.cpp +++ b/example/bucket_sorter.cpp @@ -25,21 +25,20 @@ struct trivial_id { int main() { - using namespace std; using boost::bucket_sorter; const std::size_t N = 10; - vector bucket(N); + std::vector bucket(N); for (std::size_t i=0; i ID; - typedef bucket_sorter::iterator, ID> BS; + using ID = trivial_id; + using BS = bucket_sorter::iterator, ID>; BS my_bucket_sorter(N, N, bucket.begin()); for (std::size_t ii=0; ii #include +#include "range_pair.hpp" using namespace boost; @@ -23,14 +24,10 @@ using namespace boost; int main(int argc, char** argv) { - typedef adjacency_list - < vecS, - vecS, - undirectedS, - property, - property - > - graph; + using graph = adjacency_list, + property>; // Create a maximal planar graph on 6 vertices graph g(6); @@ -51,16 +48,15 @@ int main(int argc, char** argv) add_edge(1,5,g); // Initialize the interior edge index - property_map::type e_index = get(edge_index, g); + auto e_index = get(edge_index, g); graph_traits::edges_size_type edge_count = 0; - graph_traits::edge_iterator ei, ei_end; - for(boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) - put(e_index, *ei, edge_count++); + for(const auto& edge : make_range_pair(edges(g))) + put(e_index, edge, edge_count++); // Test for planarity - we know it is planar, we just want to // compute the planar embedding as a side-effect - typedef std::vector< graph_traits::edge_descriptor > vec_t; + using vec_t = std::vector::edge_descriptor>; std::vector embedding(num_vertices(g)); if (boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g, boyer_myrvold_params::embedding = @@ -72,8 +68,7 @@ int main(int argc, char** argv) else std::cout << "Input graph is not planar" << std::endl; - typedef std::vector::vertex_descriptor> - ordering_storage_t; + using ordering_storage_t = std::vector::vertex_descriptor>; ordering_storage_t ordering; planar_canonical_ordering(g, @@ -81,11 +76,9 @@ int main(int argc, char** argv) embedding.begin(), get(vertex_index, g)), std::back_inserter(ordering)); - ordering_storage_t::iterator oi, oi_end; - oi_end = ordering.end(); std::cout << "The planar canonical ordering is: "; - for(oi = ordering.begin(); oi != oi_end; ++oi) - std::cout << *oi << " "; + for(const auto& o : ordering) + std::cout << o << " "; std::cout << std::endl; return 0; diff --git a/example/cc-internet.cpp b/example/cc-internet.cpp index 0f2d930b..26ed1691 100644 --- a/example/cc-internet.cpp +++ b/example/cc-internet.cpp @@ -11,6 +11,7 @@ #include #include #include +#include "range_pair.hpp" int main() @@ -25,16 +26,14 @@ main() (g, make_iterator_property_map(component.begin(), get(vertex_index, g), component[0])); - property_map < GraphvizGraph, vertex_attribute_t >::type - vertex_attr_map = get(vertex_attribute, g); + auto vertex_attr_map = get(vertex_attribute, g); std::string color[] = { "white", "gray", "black", "lightgray"}; - graph_traits < GraphvizGraph >::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { - vertex_attr_map[*vi]["color"] = color[component[*vi]]; - vertex_attr_map[*vi]["style"] = "filled"; - if (vertex_attr_map[*vi]["color"] == "black") - vertex_attr_map[*vi]["fontcolor"] = "white"; + for(const auto& vertex : make_range_pair(vertices(g))) { + vertex_attr_map[vertex]["color"] = color[component[vertex]]; + vertex_attr_map[vertex]["style"] = "filled"; + if (vertex_attr_map[vertex]["color"] == "black") + vertex_attr_map[vertex]["fontcolor"] = "white"; } write_graphviz("figs/cc-internet-out.dot", g); diff --git a/example/city_visitor.cpp b/example/city_visitor.cpp index 85415a6f..21d71896 100644 --- a/example/city_visitor.cpp +++ b/example/city_visitor.cpp @@ -51,42 +51,41 @@ */ -using namespace std; using namespace boost; struct city_arrival : public base_visitor { - city_arrival(string* n) : names(n) { } - typedef on_discover_vertex event_filter; + city_arrival(std::string* n) : names(n) { } + using event_filter = on_discover_vertex; template inline void operator()(Vertex u, Graph&) { - cout << endl << "arriving at " << names[u] << endl + std::cout << std::endl << "arriving at " << names[u] << std::endl << " neighboring cities are: "; } - string* names; + std::string* names; }; struct neighbor_cities : public base_visitor { - neighbor_cities(string* n) : names(n) { } - typedef on_examine_edge event_filter; + neighbor_cities(std::string* n) : names(n) { } + using event_filter = on_examine_edge; template inline void operator()(Edge e, Graph& g) { - cout << names[ target(e, g) ] << ", "; + std::cout << names[ target(e, g) ] << ", "; } - string* names; + std::string* names; }; struct finish_city : public base_visitor { - finish_city(string* n) : names(n) { } - typedef on_finish_vertex event_filter; + finish_city(std::string* n) : names(n) { } + using event_filter = on_finish_vertex; template inline void operator()(Vertex u, Graph&) { - cout << endl << "finished with " << names[u] << endl; + std::cout << std::endl << "finished with " << names[u] << std::endl; } - string* names; + std::string* names; }; int main(int, char*[]) @@ -95,11 +94,11 @@ int main(int, char*[]) enum { SanJose, SanFran, LA, SanDiego, Fresno, LasVegas, Reno, Sacramento, SaltLake, Phoenix, N }; - string names[] = { "San Jose", "San Francisco", "Los Angeles", "San Diego", + std::string names[] = { "San Jose", "San Francisco", "Los Angeles", "San Diego", "Fresno", "Las Vegas", "Reno", "Sacramento", "Salt Lake City", "Phoenix" }; - typedef std::pair E; + using E = std::pair; E edge_array[] = { E(Sacramento, Reno), E(Sacramento, SanFran), E(Reno, SaltLake), E(SanFran, SanJose), @@ -108,7 +107,7 @@ int main(int, char*[]) E(LasVegas, Phoenix) }; /* Create the graph type we want. */ - typedef adjacency_list Graph; + using Graph = adjacency_list; #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 // VC++ has trouble with the edge iterator constructor Graph G(N); @@ -118,19 +117,18 @@ int main(int, char*[]) Graph G(edge_array, edge_array + sizeof(edge_array)/sizeof(E), N); #endif - cout << "*** Depth First ***" << endl; + std::cout << "*** Depth First ***" << std::endl; depth_first_search (G, visitor(make_dfs_visitor(boost::make_list(city_arrival(names), neighbor_cities(names), finish_city(names))))); - cout << endl; + std::cout << std::endl; /* Get the source vertex */ - boost::graph_traits::vertex_descriptor - s = vertex(SanJose,G); + auto s = vertex(SanJose,G); - cout << "*** Breadth First ***" << endl; + std::cout << "*** Breadth First ***" << std::endl; breadth_first_search (G, s, visitor(make_bfs_visitor(boost::make_list(city_arrival(names), neighbor_cities(names), diff --git a/example/closeness_centrality.cpp b/example/closeness_centrality.cpp index ea921114..1a384be2 100644 --- a/example/closeness_centrality.cpp +++ b/example/closeness_centrality.cpp @@ -14,40 +14,40 @@ #include #include #include "helper.hpp" +#include "range_pair.hpp" -using namespace std; using namespace boost; // The Actor type stores the name of each vertex in the graph. struct Actor { - string name; + std::string name; }; // Declare the graph type and its vertex and edge types. -typedef undirected_graph Graph; -typedef graph_traits::vertex_descriptor Vertex; -typedef graph_traits::edge_descriptor Edge; +using Graph = undirected_graph; +using Vertex = graph_traits::vertex_descriptor; +using Edge = graph_traits::edge_descriptor; // The name map provides an abstract accessor for the names of // each vertex. This is used during graph creation. -typedef property_map::type NameMap; +using NameMap = property_map::type; // Declare a matrix type and its corresponding property map that // will contain the distances between each pair of vertices. -typedef exterior_vertex_property DistanceProperty; -typedef DistanceProperty::matrix_type DistanceMatrix; -typedef DistanceProperty::matrix_map_type DistanceMatrixMap; +using DistanceProperty = exterior_vertex_property; +using DistanceMatrix = DistanceProperty::matrix_type; +using DistanceMatrixMap = DistanceProperty::matrix_map_type; // Declare the weight map so that each edge returns the same value. -typedef constant_property_map WeightMap; +using WeightMap = constant_property_map; // Declare a container and its corresponding property map that // will contain the resulting closeness centralities of each // vertex in the graph. -typedef boost::exterior_vertex_property ClosenessProperty; -typedef ClosenessProperty::container_type ClosenessContainer; -typedef ClosenessProperty::map_type ClosenessMap; +using ClosenessProperty = boost::exterior_vertex_property; +using ClosenessContainer = ClosenessProperty::container_type; +using ClosenessMap = ClosenessProperty::map_type; int main(int argc, char *argv[]) @@ -58,7 +58,7 @@ main(int argc, char *argv[]) NameMap nm(get(&Actor::name, g)); // Read the graph from standard input. - read_graph(g, nm, cin); + read_graph(g, nm, std::cin); // Compute the distances between all pairs of vertices using // the Floyd-Warshall algorithm. Note that the weight map is @@ -74,10 +74,9 @@ main(int argc, char *argv[]) all_closeness_centralities(g, dm, cm); // Print the closeness centrality of each vertex. - graph_traits::vertex_iterator i, end; - for(boost::tie(i, end) = vertices(g); i != end; ++i) { - cout << setw(12) << setiosflags(ios::left) - << g[*i].name << get(cm, *i) << endl; + for(const auto& vertex : make_range_pair(vertices(g))) { + std::cout << std::setw(12) << std::setiosflags(std::ios::left) + << g[vertex].name << get(cm, vertex) << std::endl; } return 0; diff --git a/example/clustering_coefficient.cpp b/example/clustering_coefficient.cpp index c4a451f1..84866d4d 100644 --- a/example/clustering_coefficient.cpp +++ b/example/clustering_coefficient.cpp @@ -13,30 +13,30 @@ #include #include #include "helper.hpp" +#include "range_pair.hpp" -using namespace std; using namespace boost; // The Actor type stores the name of each vertex in the graph. struct Actor { - string name; + std::string name; }; // Declare the graph type and its vertex and edge types. -typedef undirected_graph Graph; -typedef graph_traits::vertex_descriptor Vertex; -typedef graph_traits::edge_descriptor Edge; +using Graph = undirected_graph; +using Vertex = graph_traits::vertex_descriptor; +using Edge = graph_traits::edge_descriptor; // The name map provides an abstract accessor for the names of // each vertex. This is used during graph creation. -typedef property_map::type NameMap; +using NameMap = property_map::type; // The clustering property, container, and map define the containment // and abstract accessor for the clustering coefficients of vertices. -typedef exterior_vertex_property ClusteringProperty; -typedef ClusteringProperty::container_type ClusteringContainer; -typedef ClusteringProperty::map_type ClusteringMap; +using ClusteringProperty = exterior_vertex_property; +using ClusteringContainer = ClusteringProperty::container_type; +using ClusteringMap = ClusteringProperty::map_type; int main(int argc, char *argv[]) @@ -47,22 +47,21 @@ main(int argc, char *argv[]) NameMap nm(get(&Actor::name, g)); // Read the graph from standard input. - read_graph(g, nm, cin); + read_graph(g, nm, std::cin); // Compute the clustering coefficients of each vertex in the graph // and the mean clustering coefficient which is returned from the // computation. ClusteringContainer coefs(num_vertices(g)); ClusteringMap cm(coefs, g); - float cc = all_clustering_coefficients(g, cm); + auto cc = all_clustering_coefficients(g, cm); // Print the clustering coefficient of each vertex. - graph_traits::vertex_iterator i, end; - for(boost::tie(i, end) = vertices(g); i != end; ++i) { - cout << setw(12) << setiosflags(ios::left) - << g[*i].name << get(cm, *i) << endl; + for(const auto& vertex : make_range_pair(vertices(g))) { + std::cout << std::setw(12) << std::setiosflags(std::ios::left) + << g[vertex].name << get(cm, vertex) << std::endl; } - cout << "mean clustering coefficient: " << cc << endl; + std::cout << "mean clustering coefficient: " << cc << std::endl; return 0; } diff --git a/example/components_on_edgelist.cpp b/example/components_on_edgelist.cpp index 685f86eb..d9ba92ba 100644 --- a/example/components_on_edgelist.cpp +++ b/example/components_on_edgelist.cpp @@ -49,14 +49,12 @@ */ -using namespace std; -using boost::tie; int main(int , char* []) { using namespace boost; - typedef int Index; // ID of a Vertex - typedef pair Edge; + using Index = int; // ID of a Vertex + using Edge = std::pair; const int N = 6; const int E = 4; Edge edgelist[] = { Edge(0, 1), Edge(1, 4), Edge(4, 0), Edge(2, 5) }; @@ -64,9 +62,9 @@ int main(int , char* []) edge_list g(edgelist, edgelist + E); - cout << "An undirected graph (edge list):" << endl; + std::cout << "An undirected graph (edge list):" << std::endl; print_edges(g, identity_property_map()); - cout << endl; + std::cout << std::endl; disjoint_sets_with_storage<> ds(N); incremental_components(g, ds); @@ -74,20 +72,20 @@ int main(int , char* []) component_index components(&ds.parents()[0], &ds.parents()[0] + ds.parents().size()); - cout << "Total number of components: " << components.size() << endl; + std::cout << "Total number of components: " << components.size() << std::endl; for (int k = 0; k != N; ++k) - cout << "Vertex " << k << " is in the component who's representative is " - << ds.find_set(k) << endl; - cout << endl; + std::cout << "Vertex " << k << " is in the component who's representative is " + << ds.find_set(k) << std::endl; + std::cout << std::endl; for (std::size_t i = 0; i < components.size(); ++i) { - cout << "component " << i << " contains: "; + std::cout << "component " << i << " contains: "; component_index::component_iterator j = components[i].first, jend = components[i].second; for ( ; j != jend; ++j) - cout << *j << " "; - cout << endl; + std::cout << *j << " "; + std::cout << std::endl; } return 0; diff --git a/example/connected-components.cpp b/example/connected-components.cpp index 15a83ca6..5601ccde 100644 --- a/example/connected-components.cpp +++ b/example/connected-components.cpp @@ -15,7 +15,7 @@ int main() { using namespace boost; - typedef adjacency_list < vecS, vecS, undirectedS > Graph; + using Graph = adjacency_list; const int N = 6; Graph G(N); @@ -25,13 +25,12 @@ main() add_edge(2, 5, G); std::vector c(num_vertices(G)); - int num = connected_components + auto num = connected_components (G, make_iterator_property_map(c.begin(), get(vertex_index, G), c[0])); std::cout << std::endl; - std::vector < int >::iterator i; std::cout << "Total number of components: " << num << std::endl; - for (i = c.begin(); i != c.end(); ++i) + for (auto i = c.begin(); i != c.end(); ++i) std::cout << "Vertex " << i - c.begin() << " is in component " << *i << std::endl; std::cout << std::endl; diff --git a/example/connected_components.cpp b/example/connected_components.cpp index 0d9102bb..091b3443 100644 --- a/example/connected_components.cpp +++ b/example/connected_components.cpp @@ -34,13 +34,11 @@ */ -using namespace std; - int main(int , char* []) { using namespace boost; { - typedef adjacency_list Graph; + using Graph = adjacency_list ; Graph G; add_edge(0, 1, G); @@ -49,13 +47,13 @@ int main(int , char* []) add_edge(2, 5, G); std::vector component(num_vertices(G)); - int num = connected_components(G, &component[0]); + auto num = connected_components(G, &component[0]); std::vector::size_type i; - cout << "Total number of components: " << num << endl; + std::cout << "Total number of components: " << num << std::endl; for (i = 0; i != component.size(); ++i) - cout << "Vertex " << i <<" is in component " << component[i] << endl; - cout << endl; + std::cout << "Vertex " << i <<" is in component " << component[i] << std::endl; + std::cout << std::endl; } return 0; } diff --git a/example/container_gen.cpp b/example/container_gen.cpp index 298e3a62..66a2ec00 100644 --- a/example/container_gen.cpp +++ b/example/container_gen.cpp @@ -16,19 +16,19 @@ struct list_with_allocatorS { }; namespace boost { template struct container_gen, ValueType> { - typedef typename Alloc::template rebind::other Allocator; - typedef std::list type; + using Allocator = typename Alloc::template rebind::other; + using type = std::list; }; template - struct parallel_edge_traits< list_with_allocatorS > { - typedef allow_parallel_edge_tag type; + struct parallel_edge_traits> { + using type = allow_parallel_edge_tag; }; } // now you can define a graph using std::list and a specific allocator -typedef boost::adjacency_list< list_with_allocatorS< std::allocator >, - boost::vecS, boost::directedS> MyGraph; +using MyGraph = boost::adjacency_list>, + boost::vecS, boost::directedS>; int main(int, char*[]) { diff --git a/example/copy-example.cpp b/example/copy-example.cpp index 572bb369..25322104 100644 --- a/example/copy-example.cpp +++ b/example/copy-example.cpp @@ -10,30 +10,31 @@ #include #include #include +#include "range_pair.hpp" int main() { using namespace boost; - typedef adjacency_list < vecS, vecS, directedS, - property < vertex_name_t, char > > graph_t; + using graph_t = adjacency_list>; enum { a, b, c, d, e, f, g, N }; graph_t G(N); - property_map < graph_t, vertex_name_t >::type - name_map = get(vertex_name, G); + auto name_map = get(vertex_name, G); char name = 'a'; - graph_traits < graph_t >::vertex_iterator v, v_end; - for (boost::tie(v, v_end) = vertices(G); v != v_end; ++v, ++name) - name_map[*v] = name; + for (const auto& vertex : make_range_pair(vertices(G))) { + name_map[vertex] = name; + ++name; + } - typedef std::pair < int, int >E; + using E = std::pair; E edges[] = { E(a, c), E(a, d), E(b, a), E(b, d), E(c, f), E(d, c), E(d, e), E(d, f), E(e, b), E(e, g), E(f, e), E(f, g) }; - for (int i = 0; i < 12; ++i) - add_edge(edges[i].first, edges[i].second, G); + for (const auto& edge : edges) + add_edge(edge.first, edge.second, G); print_graph(G, name_map); std::cout << std::endl; diff --git a/example/csr-example.cpp b/example/csr-example.cpp index b597193a..cc2191f9 100644 --- a/example/csr-example.cpp +++ b/example/csr-example.cpp @@ -23,7 +23,7 @@ class WebPage int main() { - typedef std::pair E; + using E = std::pair; const char* urls[6] = { "http://www.boost.org/libs/graph/doc/index.html", "http://www.boost.org/libs/graph/doc/table_of_contents.html", @@ -37,7 +37,7 @@ int main() E(2, 0), E(2, 5), E(3, 1), E(3, 4), E(4, 1), E(5, 0), E(5, 2) }; - typedef compressed_sparse_row_graph WebGraph; + using WebGraph = compressed_sparse_row_graph; WebGraph g(boost::edges_are_sorted, &the_edges[0], &the_edges[0] + sizeof(the_edges)/sizeof(E), 6); // Set the URLs of each vertex diff --git a/example/cuthill_mckee_ordering.cpp b/example/cuthill_mckee_ordering.cpp index 23b0238d..42e6c94a 100644 --- a/example/cuthill_mckee_ordering.cpp +++ b/example/cuthill_mckee_ordering.cpp @@ -15,6 +15,7 @@ #include #include #include +#include "range_pair.hpp" /* Sample Output @@ -32,15 +33,14 @@ int main(int , char* []) { using namespace boost; - using namespace std; - typedef adjacency_list > > Graph; - typedef graph_traits::vertex_descriptor Vertex; - typedef graph_traits::vertices_size_type size_type; + property>>; + using Vertex = graph_traits::vertex_descriptor; + using size_type = graph_traits::vertices_size_type; - typedef std::pair Pair; - Pair edges[14] = { Pair(0,3), //a-d + using Pair = std::pair; + Pair edges[] = { Pair(0,3), //a-d Pair(0,5), //a-f Pair(1,2), //b-c Pair(1,4), //b-e @@ -56,33 +56,29 @@ int main(int , char* []) Pair(6,7) }; //g-h Graph G(10); - for (int i = 0; i < 14; ++i) - add_edge(edges[i].first, edges[i].second, G); + for (const auto& edge : edges) + add_edge(edge.first, edge.second, G); - graph_traits::vertex_iterator ui, ui_end; + auto deg = get(vertex_degree, G); + for(const auto& v : make_range_pair(vertices(G))) + deg[v] = degree(v, G); - property_map::type deg = get(vertex_degree, G); - for (boost::tie(ui, ui_end) = vertices(G); ui != ui_end; ++ui) - deg[*ui] = degree(*ui, G); - - property_map::type - index_map = get(vertex_index, G); + auto index_map = get(vertex_index, G); std::cout << "original bandwidth: " << bandwidth(G) << std::endl; std::vector inv_perm(num_vertices(G)); std::vector perm(num_vertices(G)); { - Vertex s = vertex(6, G); + auto s = vertex(6, G); //reverse cuthill_mckee_ordering cuthill_mckee_ordering(G, s, inv_perm.rbegin(), get(vertex_color, G), get(vertex_degree, G)); - cout << "Reverse Cuthill-McKee ordering starting at: " << s << endl; - cout << " "; - for (std::vector::const_iterator i = inv_perm.begin(); - i != inv_perm.end(); ++i) - cout << index_map[*i] << " "; - cout << endl; + std::cout << "Reverse Cuthill-McKee ordering starting at: " << s << std::endl; + std::cout << " "; + for (const auto& vertex : inv_perm) + std::cout << index_map[vertex] << " "; + std::cout << std::endl; for (size_type c = 0; c != inv_perm.size(); ++c) perm[index_map[inv_perm[c]]] = c; @@ -91,16 +87,15 @@ int main(int , char* []) << std::endl; } { - Vertex s = vertex(0, G); + auto s = vertex(0, G); //reverse cuthill_mckee_ordering cuthill_mckee_ordering(G, s, inv_perm.rbegin(), get(vertex_color, G), get(vertex_degree, G)); - cout << "Reverse Cuthill-McKee ordering starting at: " << s << endl; - cout << " "; - for (std::vector::const_iterator i=inv_perm.begin(); - i != inv_perm.end(); ++i) - cout << index_map[*i] << " "; - cout << endl; + std::cout << "Reverse Cuthill-McKee ordering starting at: " << s << std::endl; + std::cout << " "; + for (const auto& vertex : inv_perm) + std::cout << index_map[vertex] << " "; + std::cout << std::endl; for (size_type c = 0; c != inv_perm.size(); ++c) perm[index_map[inv_perm[c]]] = c; @@ -114,12 +109,11 @@ int main(int , char* []) cuthill_mckee_ordering(G, inv_perm.rbegin(), get(vertex_color, G), make_degree_map(G)); - cout << "Reverse Cuthill-McKee ordering:" << endl; - cout << " "; - for (std::vector::const_iterator i=inv_perm.begin(); - i != inv_perm.end(); ++i) - cout << index_map[*i] << " "; - cout << endl; + std::cout << "Reverse Cuthill-McKee ordering:" << std::endl; + std::cout << " "; + for (const auto& vertex : inv_perm) + std::cout << index_map[vertex] << " "; + std::cout << std::endl; for (size_type c = 0; c != inv_perm.size(); ++c) perm[index_map[inv_perm[c]]] = c; diff --git a/example/cycle-file-dep.cpp b/example/cycle-file-dep.cpp index b7b22fac..97d74d7e 100644 --- a/example/cycle-file-dep.cpp +++ b/example/cycle-file-dep.cpp @@ -11,38 +11,38 @@ #include #include #include +#include "range_pair.hpp" using namespace boost; namespace std { - template < typename T > - std::istream & operator >> (std::istream & in, std::pair < T, T > &p) + template + std::istream & operator >> (std::istream & in, std::pair &p) { in >> p.first >> p.second; return in; } } -typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list +using file_dep_graph = adjacency_list file_dep_graph; +>; -typedef graph_traits < file_dep_graph >::vertex_descriptor vertex_t; -typedef graph_traits < file_dep_graph >::edge_descriptor edge_t; +using vertex_t = graph_traits::vertex_descriptor; +using edge_t = graph_traits::edge_descriptor; bool has_cycle_dfs(const file_dep_graph & g, vertex_t u, default_color_type * color) { color[u] = gray_color; - graph_traits < file_dep_graph >::adjacency_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = adjacent_vertices(u, g); vi != vi_end; ++vi) - if (color[*vi] == white_color) { - if (has_cycle_dfs(g, *vi, color)) + for (const auto& vertex : make_range_pair(adjacent_vertices(u, g))) + if (color[vertex] == white_color) { + if (has_cycle_dfs(g, vertex, color)) return true; // cycle detected, return immediately - } else if (color[*vi] == gray_color) // *vi is an ancestor! + } else if (color[vertex] == gray_color) // vertex is an ancestor! return true; color[u] = black_color; return false; @@ -51,11 +51,10 @@ has_cycle_dfs(const file_dep_graph & g, vertex_t u, bool has_cycle(const file_dep_graph & g) { - std::vector < default_color_type > color(num_vertices(g), white_color); - graph_traits < file_dep_graph >::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) - if (color[*vi] == white_color) - if (has_cycle_dfs(g, *vi, &color[0])) + std::vector color(num_vertices(g), white_color); + for (const auto& vertex : make_range_pair(vertices(g))) + if (color[vertex] == white_color) + if (has_cycle_dfs(g, vertex, &color[0])) return true; return false; } @@ -65,28 +64,25 @@ int main() { std::ifstream file_in("makefile-dependencies.dat"); - typedef graph_traits < file_dep_graph >::vertices_size_type size_type; + using size_type = graph_traits::vertices_size_type; size_type n_vertices; file_in >> n_vertices; // read in number of vertices - std::istream_iterator < std::pair < size_type, - size_type > > input_begin(file_in), input_end; + std::istream_iterator> input_begin(file_in), input_end; #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 // VC++ has trouble with the edge iterator constructor file_dep_graph g(n_vertices); while (input_begin != input_end) { - size_type i, j; - boost::tie(i, j) = *input_begin++; + const auto [i, j] = *input_begin++; add_edge(i, j, g); } #else file_dep_graph g(input_begin, input_end, n_vertices); #endif - std::vector < std::string > name(num_vertices(g)); + std::vector name(num_vertices(g)); std::ifstream name_in("makefile-target-names.dat"); - graph_traits < file_dep_graph >::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) - name_in >> name[*vi]; + for (const auto& vertex : make_range_pair(vertices(g))) + name_in >> name[vertex]; assert(has_cycle(g) == false); return 0; diff --git a/example/cycle-file-dep2.cpp b/example/cycle-file-dep2.cpp index 0b04ff41..bf002cc6 100644 --- a/example/cycle-file-dep2.cpp +++ b/example/cycle-file-dep2.cpp @@ -11,6 +11,7 @@ #include #include #include +#include "range_pair.hpp" // can't do using namespace boost because then // we get conflict with boost::default_dfs_visitor. @@ -18,7 +19,7 @@ using namespace boost; namespace std { template - std::istream& operator >> (std::istream & in, std::pair < T, T > &p) + std::istream& operator >> (std::istream & in, std::pair &p) { in >> p.first >> p.second; return @@ -26,43 +27,41 @@ namespace std { } } -typedef adjacency_list< +using file_dep_graph = adjacency_list< listS, // Store out-edges of each vertex in a std::list vecS, // Store vertex set in a std::vector directedS // The file dependency graph is directed - > file_dep_graph; + >; -typedef graph_traits::vertex_descriptor vertex_t; -typedef graph_traits::edge_descriptor edge_t; +using vertex_t = graph_traits::vertex_descriptor; +using edge_t = graph_traits::edge_descriptor; -template < typename Visitor > void +template void dfs_v1(const file_dep_graph & g, vertex_t u, default_color_type * color, Visitor vis) { color[u] = gray_color; vis.discover_vertex(u, g); - graph_traits < file_dep_graph >::out_edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei) { - if (color[target(*ei, g)] == white_color) { - vis.tree_edge(*ei, g); - dfs_v1(g, target(*ei, g), color, vis); - } else if (color[target(*ei, g)] == gray_color) - vis.back_edge(*ei, g); + for (const auto& edge : make_range_pair(out_edges(u, g))) { + if (color[target(edge, g)] == white_color) { + vis.tree_edge(edge, g); + dfs_v1(g, target(edge, g), color, vis); + } else if (color[target(edge, g)] == gray_color) + vis.back_edge(edge, g); else - vis.forward_or_cross_edge(*ei, g); + vis.forward_or_cross_edge(edge, g); } color[u] = black_color; vis.finish_vertex(u, g); } -template < typename Visitor > void +template void generic_dfs_v1(const file_dep_graph & g, Visitor vis) { - std::vector < default_color_type > color(num_vertices(g), white_color); - graph_traits < file_dep_graph >::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { - if (color[*vi] == white_color) - dfs_v1(g, *vi, &color[0], vis); + std::vector color(num_vertices(g), white_color); + for(const auto& vertex : make_range_pair(vertices(g))) { + if (color[vertex] == white_color) + dfs_v1(g, vertex, &color[0], vis); } } @@ -78,17 +77,17 @@ struct dfs_visitor_default { } - template < typename E, typename G > void + template void back_edge(E, const G &) { } - template < typename E, typename G > void + template void forward_or_cross_edge(E, const G &) { } - template < typename V, typename G > void + template void finish_vertex(V, const G &) { } @@ -122,28 +121,25 @@ int main() { std::ifstream file_in("makefile-dependencies.dat"); - typedef graph_traits ::vertices_size_type size_type; + using size_type = graph_traits ::vertices_size_type; size_type n_vertices; file_in >> n_vertices; // read in number of vertices - std::istream_iterator < std::pair < size_type, - size_type > >input_begin(file_in), input_end; + std::istream_iterator> input_begin(file_in), input_end; #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 // VC++ has trouble with the edge iterator constructor file_dep_graph g(n_vertices); while (input_begin != input_end) { - size_type i, j; - boost::tie(i, j) = *input_begin++; + const auto [i, j] = *input_begin++; add_edge(i, j, g); } #else file_dep_graph g(input_begin, input_end, n_vertices); #endif - std::vector < std::string > name(num_vertices(g)); + std::vector name(num_vertices(g)); std::ifstream name_in("makefile-target-names.dat"); - graph_traits < file_dep_graph >::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) - name_in >> name[*vi]; + for (const auto& vertex : make_range_pair(vertices(g))) + name_in >> name[vertex]; assert(has_cycle(g) == false); return 0; diff --git a/example/cycle_canceling_example.cpp b/example/cycle_canceling_example.cpp index 46e85f14..83fb66ac 100644 --- a/example/cycle_canceling_example.cpp +++ b/example/cycle_canceling_example.cpp @@ -21,7 +21,7 @@ int main() { boost::edmonds_karp_max_flow(g, s, t); boost::cycle_canceling(g); - int cost = boost::find_flow_cost(g); + auto cost = boost::find_flow_cost(g); assert(cost == 29); return 0; } diff --git a/example/cycle_ratio_example.cpp b/example/cycle_ratio_example.cpp index b65fdead..65c1a26c 100644 --- a/example/cycle_ratio_example.cpp +++ b/example/cycle_ratio_example.cpp @@ -11,6 +11,7 @@ #include #include #include +#include "range_pair.hpp" /** * @author Dmitry Bufistov @@ -18,65 +19,61 @@ */ using namespace boost; -typedef adjacency_list< +using grap_real_t = adjacency_list< listS, listS, directedS, property, property< edge_weight_t, double, property > -> grap_real_t; +>; template void gen_rand_graph(TG &g, size_t nV, size_t nE) { g.clear(); mt19937 rng; - rng.seed(uint32_t(time(0))); + rng.seed(uint32_t(time(nullptr))); boost::generate_random_graph(g, nV, nE, rng, true, true); boost::uniform_real<> ur(-1,10); - boost::variate_generator > ew1rg(rng, ur); + boost::variate_generator> ew1rg(rng, ur); randomize_property(g, ew1rg); boost::uniform_int uint(1,5); - boost::variate_generator > ew2rg(rng, uint); + boost::variate_generator> ew2rg(rng, uint); randomize_property(g, ew2rg); } int main(int argc, char* argv[]) { - using std::cout; - using std::endl; const double epsilon = 0.0000001; - double min_cr, max_cr; ///Minimum and maximum cycle ratio - typedef std::vector::edge_descriptor> ccReal_t; + using ccReal_t = std::vector::edge_descriptor>; ccReal_t cc; ///critical cycle grap_real_t tgr; - property_map::type vim = get(vertex_index, tgr); - property_map::type ew1 = get(edge_weight, tgr); - property_map::type ew2 = get(edge_weight2, tgr); + auto vim = get(vertex_index, tgr); + auto ew1 = get(edge_weight, tgr); + auto ew2 = get(edge_weight2, tgr); gen_rand_graph(tgr, 1000, 30000); - cout << "Vertices number: " << num_vertices(tgr) << endl; - cout << "Edges number: " << num_edges(tgr) << endl; + std::cout << "Vertices number: " << num_vertices(tgr) << std::endl; + std::cout << "Edges number: " << num_edges(tgr) << std::endl; int i = 0; - graph_traits::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(tgr); vi != vi_end; vi++) { - vim[*vi] = i++; ///Initialize vertex index property + for(const auto& vertex : make_range_pair(vertices(tgr))) { + vim[vertex] = i++; ///Initialize vertex index property } - max_cr = maximum_cycle_ratio(tgr, vim, ew1, ew2); - cout << "Maximum cycle ratio is " << max_cr << endl; - min_cr = minimum_cycle_ratio(tgr, vim, ew1, ew2, &cc); - cout << "Minimum cycle ratio is " << min_cr << endl; + auto max_cr = maximum_cycle_ratio(tgr, vim, ew1, ew2); + std::cout << "Maximum cycle ratio is " << max_cr << std::endl; + auto min_cr = minimum_cycle_ratio(tgr, vim, ew1, ew2, &cc); + std::cout << "Minimum cycle ratio is " << min_cr << std::endl; std::pair cr(.0,.0); - cout << "Critical cycle:\n"; - for (ccReal_t::iterator itr = cc.begin(); itr != cc.end(); ++itr) + std::cout << "Critical cycle:\n"; + for (const auto& edge : cc) { - cr.first += ew1[*itr]; - cr.second += ew2[*itr]; - std::cout << "(" << vim[source(*itr, tgr)] << "," << - vim[target(*itr, tgr)] << ") "; + cr.first += ew1[edge]; + cr.second += ew2[edge]; + std::cout << "(" << vim[source(edge, tgr)] << "," << + vim[target(edge, tgr)] << ") "; } - cout << endl; + std::cout << std::endl; assert(std::abs(cr.first / cr.second - min_cr) < epsilon); return EXIT_SUCCESS; } diff --git a/example/dag_shortest_paths.cpp b/example/dag_shortest_paths.cpp index 97140c8e..2194ea0c 100644 --- a/example/dag_shortest_paths.cpp +++ b/example/dag_shortest_paths.cpp @@ -9,6 +9,7 @@ #include #include +#include "range_pair.hpp" #include @@ -25,8 +26,8 @@ int main() { using namespace boost; - typedef adjacency_list, property > graph_t; + using graph_t = adjacency_list, property>; graph_t g(6); enum verts { r, s, t, u, v, x }; char name[] = "rstuvx"; @@ -41,8 +42,7 @@ int main() add_edge(u, x, 1, g); add_edge(v, x, -2, g); - property_map::type - d_map = get(vertex_distance, g); + auto d_map = get(vertex_distance, g); #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 // VC++ has trouble with the named-parameter mechanism, so @@ -52,18 +52,17 @@ int main() default_dijkstra_visitor vis; std::less compare; closed_plus combine; - property_map::type w_map = get(edge_weight, g); + auto w_map = get(edge_weight, g); dag_shortest_paths(g, s, d_map, w_map, &color[0], &pred[0], vis, compare, combine, (std::numeric_limits::max)(), 0); #else dag_shortest_paths(g, s, distance_map(d_map)); #endif - graph_traits::vertex_iterator vi , vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) - if (d_map[*vi] == (std::numeric_limits::max)()) - std::cout << name[*vi] << ": inifinity\n"; + for(const auto& vertex : make_range_pair(vertices(g))) + if (d_map[vertex] == (std::numeric_limits::max)()) + std::cout << name[vertex] << ": inifinity\n"; else - std::cout << name[*vi] << ": " << d_map[*vi] << '\n'; + std::cout << name[vertex] << ": " << d_map[vertex] << '\n'; return 0; } diff --git a/example/dave.cpp b/example/dave.cpp index 04c69f88..595536fa 100644 --- a/example/dave.cpp +++ b/example/dave.cpp @@ -20,8 +20,8 @@ #include #include #include +#include "range_pair.hpp" -using namespace std; using namespace boost; /* This example does a best-first-search (using dijkstra's) and @@ -71,20 +71,20 @@ b(14); d a */ -typedef property > VProperty; -typedef int weight_t; -typedef property EProperty; +using VProperty = property>; +using weight_t = int; +using EProperty = property; -typedef adjacency_list Graph; +using Graph = adjacency_list; template struct endl_printer - : public boost::base_visitor< endl_printer > + : public boost::base_visitor> { - typedef Tag event_filter; + using event_filter = Tag; endl_printer(std::ostream& os) : m_os(os) { } template void operator()(T, Graph&) { m_os << std::endl; } @@ -97,9 +97,9 @@ endl_printer print_endl(std::ostream& os, Tag) { template struct edge_printer - : public boost::base_visitor< edge_printer > + : public boost::base_visitor> { - typedef Tag event_filter; + using event_filter = Tag; edge_printer(PA pa, std::ostream& os) : m_pa(pa), m_os(os) { } @@ -120,9 +120,9 @@ print_edge(PA pa, std::ostream& os, Tag) { template struct graph_copier - : public boost::base_visitor > + : public boost::base_visitor> { - typedef Tag event_filter; + using event_filter = Tag; graph_copier(NewGraph& graph) : new_g(graph) { } @@ -142,15 +142,12 @@ copy_graph(NewGraph& g, Tag) { template void print(Graph& G, Name name) { - typename boost::graph_traits::vertex_iterator ui, uiend; - for (boost::tie(ui, uiend) = vertices(G); ui != uiend; ++ui) { - cout << name[*ui] << " --> "; - typename boost::graph_traits::adjacency_iterator vi, viend; - for(boost::tie(vi, viend) = adjacent_vertices(*ui, G); vi != viend; ++vi) - cout << name[*vi] << " "; - cout << endl; + for(const auto& u : make_range_pair(vertices(G))) { + std::cout << name[u] << " --> "; + for(const auto& v : make_range_pair(adjacent_vertices(u, G))) + std::cout << name[v] << " "; + std::cout << std::endl; } - } @@ -162,14 +159,13 @@ main(int , char* []) enum { a, b, c, d, e, f, g, N}; Graph G(N); - boost::property_map::type - vertex_id = get(vertex_index, G); + auto vertex_id = get(vertex_index, G); - std::vector distance(N, (numeric_limits::max)()); - typedef boost::graph_traits::vertex_descriptor Vertex; + std::vector distance(N, (std::numeric_limits::max)()); + using Vertex = boost::graph_traits::vertex_descriptor; std::vector parent(N); - typedef std::pair E; + using E = std::pair; E edges[] = { E(a,c), E(a,d), E(b,a), E(b,d), @@ -191,9 +187,9 @@ main(int , char* []) print(G, name); adjacency_list > G_copy(N); + property> G_copy(N); - cout << "Starting graph:" << endl; + std::cout << "Starting graph:" << std::endl; std::ostream_iterator cout_int(std::cout, " "); std::ostream_iterator cout_char(std::cout, " "); @@ -227,8 +223,8 @@ main(int , char* []) parent[0])). visitor(make_dijkstra_visitor(copy_graph(G_copy, on_examine_edge())))); - cout << endl; - cout << "Result:" << endl; + std::cout << std::endl; + std::cout << "Result:" << std::endl; boost::breadth_first_search (G, vertex(a, G), visitor(make_bfs_visitor( diff --git a/example/default-constructor.cpp b/example/default-constructor.cpp index 9d9f41a2..eff073c0 100644 --- a/example/default-constructor.cpp +++ b/example/default-constructor.cpp @@ -11,10 +11,10 @@ using namespace boost; -template < typename Graph > void +template void read_graph_file(std::istream & in, Graph & g) { - typedef typename graph_traits < Graph >::vertices_size_type size_type; + using size_type = typename graph_traits::vertices_size_type; size_type n_vertices; in >> n_vertices; // read in number of vertices for (size_type i = 0; i < n_vertices; ++i) // Add n vertices to the graph @@ -31,10 +31,10 @@ read_graph_file(std::istream & in, Graph & g) int main() { - typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list + using graph_type = adjacency_list < listS, // Store out-edges of each vertex in a std::list vecS, // Store vertex set in a std::vector directedS // The graph is directed - > graph_type; + >; graph_type g; // use default constructor to create empty graph std::ifstream file_in("makefile-dependencies.dat"); diff --git a/example/default-constructor2.cpp b/example/default-constructor2.cpp index 173f10bf..88694aa5 100644 --- a/example/default-constructor2.cpp +++ b/example/default-constructor2.cpp @@ -11,14 +11,14 @@ using namespace boost; -template < typename Graph > void +template void read_graph_file(std::istream & in, Graph & g) { - typedef typename graph_traits < Graph >::vertex_descriptor Vertex; - typedef typename graph_traits < Graph >::vertices_size_type size_type; + using Vertex = typename graph_traits::vertex_descriptor; + using size_type = typename graph_traits::vertices_size_type; size_type n_vertices; in >> n_vertices; // read in number of vertices - std::vector < Vertex > vertex_set(n_vertices); + std::vector vertex_set(n_vertices); for (size_type i = 0; i < n_vertices; ++i) vertex_set[i] = add_vertex(g); @@ -34,10 +34,10 @@ read_graph_file(std::istream & in, Graph & g) int main() { - typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list + using graph_type = adjacency_list < listS, // Store out-edges of each vertex in a std::list vecS, // Store vertex set in a std::vector directedS // The graph is directed - > graph_type; + >; graph_type g; // use default constructor to create empty graph std::ifstream file_in("makefile-dependencies.dat"); diff --git a/example/degree_centrality.cpp b/example/degree_centrality.cpp index a8aae31c..e5256177 100644 --- a/example/degree_centrality.cpp +++ b/example/degree_centrality.cpp @@ -14,30 +14,30 @@ #include #include "helper.hpp" +#include "range_pair.hpp" -using namespace std; using namespace boost; // The Actor type stores the name of each vertex in the graph. struct Actor { - string name; + std::string name; }; // Declare the graph type and its vertex and edge types. -typedef undirected_graph Graph; -typedef graph_traits::vertex_descriptor Vertex; -typedef graph_traits::edge_descriptor Edge; +using Graph = undirected_graph; +using Vertex = graph_traits::vertex_descriptor; +using Edge = graph_traits::edge_descriptor; // The name map provides an abstract accessor for the names of // each vertex. This is used during graph creation. -typedef property_map::type NameMap; +using NameMap = property_map::type; // Declare a container type for degree centralities and its // corresponding property map. -typedef exterior_vertex_property CentralityProperty; -typedef CentralityProperty::container_type CentralityContainer; -typedef CentralityProperty::map_type CentralityMap; +using CentralityProperty = exterior_vertex_property; +using CentralityContainer = CentralityProperty::container_type; +using CentralityMap = CentralityProperty::map_type; int main(int argc, char *argv[]) @@ -48,7 +48,7 @@ main(int argc, char *argv[]) NameMap nm(get(&Actor::name, g)); // Read the graph from standard input. - read_graph(g, nm, cin); + read_graph(g, nm, std::cin); // Compute the degree centrality for graph. CentralityContainer cents(num_vertices(g)); @@ -56,10 +56,9 @@ main(int argc, char *argv[]) all_degree_centralities(g, cm); // Print the degree centrality of each vertex. - graph_traits::vertex_iterator i, end; - for(boost::tie(i, end) = vertices(g); i != end; ++i) { - cout << setiosflags(ios::left) << setw(12) - << g[*i].name << cm[*i] << endl; + for(const auto& vertex : make_range_pair(vertices(g))) { + std::cout << std::setiosflags(std::ios::left) << std::setw(12) + << g[vertex].name << cm[vertex] << std::endl; } return 0; diff --git a/example/dfs-example.cpp b/example/dfs-example.cpp index 0117b81d..ff0fe70d 100644 --- a/example/dfs-example.cpp +++ b/example/dfs-example.cpp @@ -13,18 +13,18 @@ #include using namespace boost; -template < typename TimeMap > class dfs_time_visitor:public default_dfs_visitor { - typedef typename property_traits < TimeMap >::value_type T; +template class dfs_time_visitor:public default_dfs_visitor { + using T = typename property_traits::value_type; public: dfs_time_visitor(TimeMap dmap, TimeMap fmap, T & t) : m_dtimemap(dmap), m_ftimemap(fmap), m_time(t) { } - template < typename Vertex, typename Graph > + template void discover_vertex(Vertex u, const Graph & g) const { put(m_dtimemap, u, m_time++); } - template < typename Vertex, typename Graph > + template void finish_vertex(Vertex u, const Graph & g) const { put(m_ftimemap, u, m_time++); @@ -39,14 +39,14 @@ int main() { // Select the graph type we wish to use - typedef adjacency_list < vecS, vecS, directedS > graph_t; - typedef graph_traits < graph_t >::vertices_size_type size_type; + using graph_t = adjacency_list; + using size_type = graph_traits::vertices_size_type; // Set up the vertex names enum { u, v, w, x, y, z, N }; char name[] = { 'u', 'v', 'w', 'x', 'y', 'z' }; // Specify the edges in the graph - typedef std::pair < int, int >E; + using E = std::pair; E edge_array[] = { E(u, v), E(u, x), E(x, v), E(y, x), E(v, y), E(w, y), E(w, z), E(z, z) }; @@ -59,34 +59,33 @@ main() #endif // discover time and finish time properties - std::vector < size_type > dtime(num_vertices(g)); - std::vector < size_type > ftime(num_vertices(g)); - typedef + std::vector dtime(num_vertices(g)); + std::vector ftime(num_vertices(g)); + using time_pm_type = iterator_property_map::iterator, - property_map::const_type> - time_pm_type; + property_map::const_type>; time_pm_type dtime_pm(dtime.begin(), get(vertex_index, g)); time_pm_type ftime_pm(ftime.begin(), get(vertex_index, g)); size_type t = 0; - dfs_time_visitor < time_pm_type >vis(dtime_pm, ftime_pm, t); + dfs_time_visitor vis(dtime_pm, ftime_pm, t); depth_first_search(g, visitor(vis)); // use std::sort to order the vertices by their discover time - std::vector < size_type > discover_order(N); - integer_range < size_type > r(0, N); + std::vector discover_order(N); + integer_range r(0, N); std::copy(r.begin(), r.end(), discover_order.begin()); std::sort(discover_order.begin(), discover_order.end(), - indirect_cmp < time_pm_type, std::less < size_type > >(dtime_pm)); + indirect_cmp>(dtime_pm)); std::cout << "order of discovery: "; int i; for (i = 0; i < N; ++i) std::cout << name[discover_order[i]] << " "; - std::vector < size_type > finish_order(N); + std::vector finish_order(N); std::copy(r.begin(), r.end(), finish_order.begin()); std::sort(finish_order.begin(), finish_order.end(), - indirect_cmp < time_pm_type, std::less < size_type > >(ftime_pm)); + indirect_cmp>(ftime_pm)); std::cout << std::endl << "order of finish: "; for (i = 0; i < N; ++i) std::cout << name[finish_order[i]] << " "; diff --git a/example/dfs-parenthesis.cpp b/example/dfs-parenthesis.cpp index 1078f90b..a52eb185 100644 --- a/example/dfs-parenthesis.cpp +++ b/example/dfs-parenthesis.cpp @@ -7,6 +7,7 @@ //======================================================================= #include #include +#include "range_pair.hpp" char name[] = "abcdefghij"; @@ -35,10 +36,9 @@ main() using namespace boost; GraphvizGraph g; read_graphviz("figs/dfs-example.dot", g); - graph_traits < GraphvizGraph >::edge_iterator e, e_end; - for (boost::tie(e, e_end) = edges(g); e != e_end; ++e) - std::cout << '(' << name[source(*e, g)] << ' ' - << name[target(*e, g)] << ')' << std::endl; + for (const auto& edge : make_range_pair(edges(g))) + std::cout << '(' << name[source(edge, g)] << ' ' + << name[target(edge, g)] << ')' << std::endl; parenthesis_visitor paren_vis; depth_first_search(g, visitor(paren_vis)); diff --git a/example/dfs.cpp b/example/dfs.cpp index 8d68a09f..021a5b8b 100644 --- a/example/dfs.cpp +++ b/example/dfs.cpp @@ -7,7 +7,7 @@ // http://www.boost.org/LICENSE_1_0.txt) //======================================================================= #include -#include +#include #include #include @@ -48,37 +48,36 @@ */ using namespace boost; -using namespace std; template struct edge_categorizer : public dfs_visitor { - typedef dfs_visitor Base; + using Base = dfs_visitor; edge_categorizer(const VisitorList& v = null_visitor()) : Base(v) { } template void tree_edge(Edge e, Graph& G) { - cout << "Tree edge: " << source(e, G) << - " --> " << target(e, G) << endl; + std::cout << "Tree edge: " << source(e, G) << + " --> " << target(e, G) << std::endl; Base::tree_edge(e, G); } template void back_edge(Edge e, Graph& G) { - cout << "Back edge: " << source(e, G) - << " --> " << target(e, G) << endl; + std::cout << "Back edge: " << source(e, G) + << " --> " << target(e, G) << std::endl; Base::back_edge(e, G); } template void forward_or_cross_edge(Edge e, Graph& G) { - cout << "Forward or cross edge: " << source(e, G) - << " --> " << target(e, G) << endl; + std::cout << "Forward or cross edge: " << source(e, G) + << " --> " << target(e, G) << std::endl; Base::forward_or_cross_edge(e, G); } template void finish_edge(Edge e, Graph& G) { - cout << "Finish edge: " << source(e, G) << - " --> " << target(e, G) << endl; + std::cout << "Finish edge: " << source(e, G) << + " --> " << target(e, G) << std::endl; Base::finish_edge(e, G); } }; @@ -94,7 +93,7 @@ main(int , char* []) using namespace boost; - typedef adjacency_list<> Graph; + using Graph = adjacency_list<>; Graph G(5); add_edge(0, 2, G); @@ -107,18 +106,17 @@ main(int , char* []) add_edge(4, 0, G); add_edge(4, 1, G); - typedef graph_traits::vertices_size_type size_type; + using size_type = graph_traits::vertices_size_type; std::vector d(num_vertices(G)); std::vector f(num_vertices(G)); int t = 0; depth_first_search(G, visitor(categorize_edges( - make_pair(stamp_times(&d[0], t, on_discover_vertex()), + std::make_pair(stamp_times(&d[0], t, on_discover_vertex()), stamp_times(&f[0], t, on_finish_vertex()))))); - std::vector::iterator i, j; - for (i = d.begin(), j = f.begin(); i != d.end(); ++i, ++j) - cout << *i << " " << *j << endl; + for (auto i = d.begin(), j = f.begin(); i != d.end(); ++i, ++j) + std::cout << *i << " " << *j << std::endl; return 0; } diff --git a/example/dfs_parenthesis.cpp b/example/dfs_parenthesis.cpp index 6f03401e..d3ec2eb4 100644 --- a/example/dfs_parenthesis.cpp +++ b/example/dfs_parenthesis.cpp @@ -12,7 +12,7 @@ // (0(2(3(4(11)4)3)2)0) #include -#include +#include #include #include @@ -25,17 +25,16 @@ #include "boost/graph/depth_first_search.hpp" using namespace boost; -using namespace std; struct open_paren : public base_visitor { - typedef on_discover_vertex event_filter; + using event_filter = on_discover_vertex; template void operator()(Vertex v, Graph&) { std::cout << "(" << v; } }; struct close_paren : public base_visitor { - typedef on_finish_vertex event_filter; + using event_filter = on_finish_vertex; template void operator()(Vertex v, Graph&) { std::cout << v << ")"; @@ -49,8 +48,8 @@ main(int, char*[]) using namespace boost; - typedef adjacency_list<> Graph; - typedef std::pair E; + using Graph = adjacency_list<>; + using E = std::pair; E edge_array[] = { E(0, 2), E(1, 1), E(1, 3), E(2, 1), E(2, 3), diff --git a/example/dijkstra-example-listS.cpp b/example/dijkstra-example-listS.cpp index 500f9811..c6f1c352 100644 --- a/example/dijkstra-example-listS.cpp +++ b/example/dijkstra-example-listS.cpp @@ -12,21 +12,22 @@ #include #include #include +#include "range_pair.hpp" using namespace boost; int main(int, char *[]) { - typedef adjacency_list_traits::vertex_descriptor vertex_descriptor; - typedef adjacency_list < listS, listS, directedS, + using vertex_descriptor = adjacency_list_traits::vertex_descriptor; + using graph_t = adjacency_list > > >, - property > graph_t; - typedef std::pair Edge; + property>>>, + property>; + using Edge = std::pair; const int num_nodes = 5; enum nodes { A, B, C, D, E }; @@ -35,34 +36,30 @@ main(int, char *[]) }; int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 }; int num_arcs = sizeof(edge_array) / sizeof(Edge); - graph_traits::vertex_iterator i, iend; graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes); - property_map::type weightmap = get(edge_weight, g); + auto weightmap = get(edge_weight, g); // Manually intialize the vertex index and name maps - property_map::type indexmap = get(vertex_index, g); - property_map::type name = get(vertex_name, g); + auto indexmap = get(vertex_index, g); + auto name = get(vertex_name, g); int c = 0; - for (boost::tie(i, iend) = vertices(g); i != iend; ++i, ++c) { - indexmap[*i] = c; - name[*i] = 'A' + c; + for (const auto& vertex : make_range_pair(vertices(g))) { + indexmap[vertex] = c; + name[vertex] = 'A' + c; + ++c; } - vertex_descriptor s = vertex(A, g); + auto s = vertex(A, g); - property_map::type - d = get(vertex_distance, g); - property_map::type - p = get(vertex_predecessor, g); + auto d = get(vertex_distance, g); + auto p = get(vertex_predecessor, g); dijkstra_shortest_paths(g, s, predecessor_map(p).distance_map(d)); std::cout << "distances and parents:" << std::endl; - graph_traits < graph_t >::vertex_iterator vi, vend; - for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi) { - std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", "; - std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std:: - endl; + for(const auto& vertex : make_range_pair(vertices(g))) { + std::cout << "distance(" << name[vertex] << ") = " << d[vertex] << ", "; + std::cout << "parent(" << name[vertex] << ") = " << name[p[vertex]] << std::endl; } std::cout << std::endl; @@ -73,11 +70,8 @@ main(int, char *[]) << " ratio=\"fill\"\n" << " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n"; - graph_traits < graph_t >::edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) { - graph_traits < graph_t >::edge_descriptor e = *ei; - graph_traits < graph_t >::vertex_descriptor - u = source(e, g), v = target(e, g); + for (const auto& e : make_range_pair(edges(g))) { + auto u = source(e, g), v = target(e, g); dot_file << name[u] << " -> " << name[v] << "[label=\"" << get(weightmap, e) << "\""; if (p[v] == u) diff --git a/example/dijkstra-example.cpp b/example/dijkstra-example.cpp index 269d2570..2349d655 100644 --- a/example/dijkstra-example.cpp +++ b/example/dijkstra-example.cpp @@ -13,16 +13,17 @@ #include #include #include +#include "range_pair.hpp" using namespace boost; int main(int, char *[]) { - typedef adjacency_list < listS, vecS, directedS, - no_property, property < edge_weight_t, int > > graph_t; - typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor; - typedef std::pair Edge; + using graph_t = adjacency_list >; + using vertex_descriptor = graph_traits::vertex_descriptor; + using Edge = std::pair; const int num_nodes = 5; enum nodes { A, B, C, D, E }; @@ -33,21 +34,19 @@ main(int, char *[]) int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 }; int num_arcs = sizeof(edge_array) / sizeof(Edge); graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes); - property_map::type weightmap = get(edge_weight, g); + auto weightmap = get(edge_weight, g); std::vector p(num_vertices(g)); std::vector d(num_vertices(g)); - vertex_descriptor s = vertex(A, g); + auto s = vertex(A, g); dijkstra_shortest_paths(g, s, predecessor_map(boost::make_iterator_property_map(p.begin(), get(boost::vertex_index, g))). distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, g)))); std::cout << "distances and parents:" << std::endl; - graph_traits < graph_t >::vertex_iterator vi, vend; - for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi) { - std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", "; - std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std:: - endl; + for(const auto& vertex : make_range_pair(vertices(g))) { + std::cout << "distance(" << name[vertex] << ") = " << d[vertex] << ", "; + std::cout << "parent(" << name[vertex] << ") = " << name[p[vertex]] << std::endl; } std::cout << std::endl; @@ -59,11 +58,8 @@ main(int, char *[]) << " ratio=\"fill\"\n" << " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n"; - graph_traits < graph_t >::edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) { - graph_traits < graph_t >::edge_descriptor e = *ei; - graph_traits < graph_t >::vertex_descriptor - u = source(e, g), v = target(e, g); + for (const auto& e : make_range_pair(edges(g))) { + auto u = source(e, g), v = target(e, g); dot_file << name[u] << " -> " << name[v] << "[label=\"" << get(weightmap, e) << "\""; if (p[v] == u) diff --git a/example/dijkstra-no-color-map-example.cpp b/example/dijkstra-no-color-map-example.cpp index 14d196d9..dab067db 100644 --- a/example/dijkstra-no-color-map-example.cpp +++ b/example/dijkstra-no-color-map-example.cpp @@ -17,16 +17,17 @@ #include #include #include +#include "range_pair.hpp" using namespace boost; int main(int, char *[]) { - typedef adjacency_list < listS, vecS, directedS, - no_property, property < edge_weight_t, int > > graph_t; - typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor; - typedef std::pair Edge; + using graph_t = adjacency_list>; + using vertex_descriptor = graph_traits::vertex_descriptor; + using Edge = std::pair; const int num_nodes = 5; enum nodes { A, B, C, D, E }; @@ -37,21 +38,19 @@ main(int, char *[]) int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 }; int num_arcs = sizeof(edge_array) / sizeof(Edge); graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes); - property_map::type weightmap = get(edge_weight, g); + auto weightmap = get(edge_weight, g); std::vector p(num_vertices(g)); std::vector d(num_vertices(g)); - vertex_descriptor s = vertex(A, g); + auto s = vertex(A, g); dijkstra_shortest_paths_no_color_map(g, s, predecessor_map(boost::make_iterator_property_map(p.begin(), get(boost::vertex_index, g))). distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, g)))); std::cout << "distances and parents:" << std::endl; - graph_traits < graph_t >::vertex_iterator vi, vend; - for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi) { - std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", "; - std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std:: - endl; + for(const auto& vertex : make_range_pair(vertices(g))) { + std::cout << "distance(" << name[vertex] << ") = " << d[vertex] << ", "; + std::cout << "parent(" << name[vertex] << ") = " << name[p[vertex]] << std::endl; } std::cout << std::endl; @@ -63,11 +62,8 @@ main(int, char *[]) << " ratio=\"fill\"\n" << " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n"; - graph_traits < graph_t >::edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) { - graph_traits < graph_t >::edge_descriptor e = *ei; - graph_traits < graph_t >::vertex_descriptor - u = source(e, g), v = target(e, g); + for (const auto& e : make_range_pair(edges(g))) { + auto u = source(e, g), v = target(e, g); dot_file << name[u] << " -> " << name[v] << "[label=\"" << get(weightmap, e) << "\""; if (p[v] == u) diff --git a/example/directed_graph.cpp b/example/directed_graph.cpp index bb308aa5..15ca176a 100644 --- a/example/directed_graph.cpp +++ b/example/directed_graph.cpp @@ -15,10 +15,10 @@ int main(int,char*[]) // like add_vertex and add_edge, which makes the code easier to understand. However, it hard codes many // of the template parameters, so it is much less flexible. - typedef boost::directed_graph<> Graph; + using Graph = boost::directed_graph<>; Graph g; - boost::graph_traits::vertex_descriptor v0 = g.add_vertex(); - boost::graph_traits::vertex_descriptor v1 = g.add_vertex(); + auto v0 = g.add_vertex(); + auto v1 = g.add_vertex(); g.add_edge(v0, v1); diff --git a/example/eccentricity.cpp b/example/eccentricity.cpp index 3e16e6f2..0a44771d 100644 --- a/example/eccentricity.cpp +++ b/example/eccentricity.cpp @@ -14,40 +14,40 @@ #include #include #include "helper.hpp" +#include "range_pair.hpp" -using namespace std; using namespace boost; // The Actor type stores the name of each vertex in the graph. struct Actor { - string name; + std::string name; }; // Declare the graph type and its vertex and edge types. -typedef undirected_graph Graph; -typedef graph_traits::vertex_descriptor Vertex; -typedef graph_traits::edge_descriptor Edge; +using Graph = undirected_graph; +using Vertex = graph_traits::vertex_descriptor; +using Edge = graph_traits::edge_descriptor; // The name map provides an abstract accessor for the names of // each vertex. This is used during graph creation. -typedef property_map::type NameMap; +using NameMap = property_map::type; // Declare a matrix type and its corresponding property map that // will contain the distances between each pair of vertices. -typedef exterior_vertex_property DistanceProperty; -typedef DistanceProperty::matrix_type DistanceMatrix; -typedef DistanceProperty::matrix_map_type DistanceMatrixMap; +using DistanceProperty = exterior_vertex_property; +using DistanceMatrix = DistanceProperty::matrix_type; +using DistanceMatrixMap = DistanceProperty::matrix_map_type; // Declare the weight map so that each edge returns the same value. -typedef constant_property_map WeightMap; +using WeightMap = constant_property_map; // Declare a container and its corresponding property map that // will contain the resulting eccentricities of each vertex in // the graph. -typedef boost::exterior_vertex_property EccentricityProperty; -typedef EccentricityProperty::container_type EccentricityContainer; -typedef EccentricityProperty::map_type EccentricityMap; +using EccentricityProperty = boost::exterior_vertex_property; +using EccentricityContainer = EccentricityProperty::container_type; +using EccentricityMap = EccentricityProperty::map_type; int main(int argc, char *argv[]) @@ -58,7 +58,7 @@ main(int argc, char *argv[]) NameMap nm(get(&Actor::name, g)); // Read the graph from standard input. - read_graph(g, nm, cin); + read_graph(g, nm, std::cin); // Compute the distances between all pairs of vertices using // the Floyd-Warshall algorithm. Note that the weight map is @@ -70,19 +70,17 @@ main(int argc, char *argv[]) // Compute the eccentricities for graph - this computation returns // both the radius and diameter as well. - int r, d; EccentricityContainer eccs(num_vertices(g)); EccentricityMap em(eccs, g); - boost::tie(r, d) = all_eccentricities(g, dm, em); + const auto [r, d] = all_eccentricities(g, dm, em); // Print the closeness centrality of each vertex. - graph_traits::vertex_iterator i, end; - for(boost::tie(i, end) = vertices(g); i != end; ++i) { - cout << setw(12) << setiosflags(ios::left) - << g[*i].name << get(em, *i) << endl; + for(const auto& vertex : make_range_pair(vertices(g))) { + std::cout << std::setw(12) << std::setiosflags(std::ios::left) + << g[vertex].name << get(em, vertex) << std::endl; } - cout << "radius: " << r << endl; - cout << "diamter: " << d << endl; + std::cout << "radius: " << r << std::endl; + std::cout << "diamter: " << d << std::endl; return 0; } diff --git a/example/edge-connectivity.cpp b/example/edge-connectivity.cpp index bd33ca72..7f87fc95 100644 --- a/example/edge-connectivity.cpp +++ b/example/edge-connectivity.cpp @@ -12,35 +12,34 @@ #include #include #include +#include "range_pair.hpp" namespace boost { - template < typename Graph > - std::pair < typename graph_traits < Graph >::vertex_descriptor, - typename graph_traits < Graph >::degree_size_type > + template + std::pair < typename graph_traits::vertex_descriptor, + typename graph_traits::degree_size_type> min_degree_vertex(Graph & g) { - typename graph_traits < Graph >::vertex_descriptor p; - typedef typename graph_traits < Graph >::degree_size_type size_type; - size_type delta = (std::numeric_limits < size_type >::max)(); - typename graph_traits < Graph >::vertex_iterator i, iend; - for (boost::tie(i, iend) = vertices(g); i != iend; ++i) - if (degree(*i, g) < delta) + typename graph_traits::vertex_descriptor p; + using size_type = typename graph_traits::degree_size_type; + auto delta = (std::numeric_limits::max)(); + for(const auto& vertex : make_range_pair(vertices(g))) + if (degree(vertex, g) < delta) { - delta = degree(*i, g); - p = *i; + delta = degree(vertex, g); + p = vertex; } return std::make_pair(p, delta); } - template < typename Graph, typename OutputIterator > + template void neighbors(const Graph & g, - typename graph_traits < Graph >::vertex_descriptor u, + typename graph_traits::vertex_descriptor u, OutputIterator result) { - typename graph_traits < Graph >::adjacency_iterator ai, aend; - for (boost::tie(ai, aend) = adjacent_vertices(u, g); ai != aend; ++ai) - *result++ = *ai; + for (const auto& vertex : make_range_pair(adjacent_vertices(u, g))) + *result++ = vertex; } template < typename Graph, typename VertexIterator, typename OutputIterator > void neighbors(const Graph & g, @@ -52,53 +51,47 @@ namespace boost neighbors(g, *first, result); } - template < typename VertexListGraph, typename OutputIterator > - typename graph_traits < VertexListGraph >::degree_size_type + template + typename graph_traits::degree_size_type edge_connectivity(VertexListGraph & g, OutputIterator disconnecting_set) { - typedef typename graph_traits < - VertexListGraph >::vertex_descriptor vertex_descriptor; - typedef typename graph_traits < - VertexListGraph >::degree_size_type degree_size_type; - typedef color_traits < default_color_type > Color; - typedef typename adjacency_list_traits < vecS, vecS, - directedS >::edge_descriptor edge_descriptor; - typedef adjacency_list < vecS, vecS, directedS, no_property, + using vertex_descriptor = typename graph_traits < + VertexListGraph >::vertex_descriptor; + using degree_size_type = typename graph_traits < + VertexListGraph >::degree_size_type; + using Color = color_traits; + using edge_descriptor = typename adjacency_list_traits < vecS, vecS, + directedS >::edge_descriptor; + using FlowGraph = adjacency_list < vecS, vecS, directedS, no_property, property < edge_capacity_t, degree_size_type, property < edge_residual_capacity_t, degree_size_type, - property < edge_reverse_t, edge_descriptor > > > > FlowGraph; + property>>>; - vertex_descriptor u, v, p, k; - edge_descriptor e1, e2; + vertex_descriptor u, v, k; bool inserted; - typename graph_traits < VertexListGraph >::vertex_iterator vi, vi_end; - degree_size_type delta, alpha_star, alpha_S_k; - std::set < vertex_descriptor > S, neighbor_S; - std::vector < vertex_descriptor > S_star, nonneighbor_S; - std::vector < default_color_type > color(num_vertices(g)); - std::vector < edge_descriptor > pred(num_vertices(g)); + degree_size_type alpha_star, alpha_S_k; + std::set S, neighbor_S; + std::vector S_star, nonneighbor_S; + std::vector color(num_vertices(g)); + std::vector pred(num_vertices(g)); FlowGraph flow_g(num_vertices(g)); - typename property_map < FlowGraph, edge_capacity_t >::type - cap = get(edge_capacity, flow_g); - typename property_map < FlowGraph, edge_residual_capacity_t >::type - res_cap = get(edge_residual_capacity, flow_g); - typename property_map < FlowGraph, edge_reverse_t >::type - rev_edge = get(edge_reverse, flow_g); - - typename graph_traits < VertexListGraph >::edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) { - u = source(*ei, g), v = target(*ei, g); - boost::tie(e1, inserted) = add_edge(u, v, flow_g); + auto cap = get(edge_capacity, flow_g); + auto res_cap = get(edge_residual_capacity, flow_g); + auto rev_edge = get(edge_reverse, flow_g); + + for (const auto& edge : make_range_pair(edges(g))) { + u = source(edge, g), v = target(edge, g); + const auto [e1, inserted1] = add_edge(u, v, flow_g); cap[e1] = 1; - boost::tie(e2, inserted) = add_edge(v, u, flow_g); + const auto [e2, inserted2] = add_edge(v, u, flow_g); cap[e2] = 1; rev_edge[e1] = e2; rev_edge[e2] = e1; } - boost::tie(p, delta) = min_degree_vertex(g); - S_star.push_back(p); + const auto [p, delta] = min_degree_vertex(g); + S_star.emplace_back(p); alpha_star = delta; S.insert(p); neighbor_S.insert(p); @@ -115,9 +108,9 @@ namespace boost if (alpha_S_k < alpha_star) { alpha_star = alpha_S_k; S_star.clear(); - for (boost::tie(vi, vi_end) = vertices(flow_g); vi != vi_end; ++vi) - if (color[*vi] != Color::white()) - S_star.push_back(*vi); + for (const auto& vertex : make_range_pair(vertices(flow_g))) + if (color[vertex] != Color::white()) + S_star.emplace_back(vertex); } S.insert(k); neighbor_S.insert(k); @@ -128,16 +121,14 @@ namespace boost std::back_inserter(nonneighbor_S)); } - std::vector < bool > in_S_star(num_vertices(g), false); - typename std::vector < vertex_descriptor >::iterator si; - for (si = S_star.begin(); si != S_star.end(); ++si) - in_S_star[*si] = true; + std::vector in_S_star(num_vertices(g), false); + for (const auto& vertex : S_star.begin()) + in_S_star[vertex] = true; degree_size_type c = 0; - for (si = S_star.begin(); si != S_star.end(); ++si) { - typename graph_traits < VertexListGraph >::out_edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = out_edges(*si, g); ei != ei_end; ++ei) - if (!in_S_star[target(*ei, g)]) { - *disconnecting_set++ = *ei; + for (const auto& vertex : S_star.begin()) { + for (const auto& edge : make_range_pair(out_edges(vertex, g))) + if (!in_S_star[target(edge, g)]) { + *disconnecting_set++ = edge; ++c; } } @@ -154,23 +145,18 @@ main() GraphvizGraph g; read_graphviz("figs/edge-connectivity.dot", g); - typedef graph_traits < GraphvizGraph >::edge_descriptor edge_descriptor; - typedef graph_traits < GraphvizGraph >::degree_size_type degree_size_type; - std::vector < edge_descriptor > disconnecting_set; - degree_size_type c = - edge_connectivity(g, std::back_inserter(disconnecting_set)); + using edge_descriptor = graph_traits::edge_descriptor; + std::vector disconnecting_set; + auto c = edge_connectivity(g, std::back_inserter(disconnecting_set)); std::cout << "The edge connectivity is " << c << "." << std::endl; - property_map < GraphvizGraph, vertex_attribute_t >::type - attr_map = get(vertex_attribute, g); + auto attr_map = get(vertex_attribute, g); std::cout << "The disconnecting set is {"; - for (std::vector < edge_descriptor >::iterator i = - disconnecting_set.begin(); i != disconnecting_set.end(); ++i) - std:: - cout << "(" << attr_map[source(*i, g)]["label"] << "," << - attr_map[target(*i, g)]["label"] << ") "; + for (const auto& edge : disconnecting_set) + std::cout << "(" << attr_map[source(edge, g)]["label"] << "," << + attr_map[target(edge, g)]["label"] << ") "; std::cout << "}." << std::endl; return EXIT_SUCCESS; } diff --git a/example/edge-function.cpp b/example/edge-function.cpp index 6ee0b666..a8713c4d 100644 --- a/example/edge-function.cpp +++ b/example/edge-function.cpp @@ -10,17 +10,18 @@ #include #include #include +#include "range_pair.hpp" using namespace boost; -template < typename Graph, typename VertexNamePropertyMap > void +template void read_graph_file(std::istream & graph_in, std::istream & name_in, Graph & g, VertexNamePropertyMap name_map) { - typedef typename graph_traits < Graph >::vertices_size_type size_type; + using size_type = typename graph_traits::vertices_size_type; size_type n_vertices; - typename graph_traits < Graph >::vertex_descriptor u; - typename property_traits < VertexNamePropertyMap >::value_type name; + typename graph_traits::vertex_descriptor u; + typename property_traits::value_type name; graph_in >> n_vertices; // read in number of vertices for (size_type i = 0; i < n_vertices; ++i) { // Add n vertices to the graph @@ -36,25 +37,24 @@ read_graph_file(std::istream & graph_in, std::istream & name_in, break; } -template < typename Graph, typename VertexNameMap > void +template void output_adjacent_vertices(std::ostream & out, - typename graph_traits < Graph >::vertex_descriptor u, + typename graph_traits::vertex_descriptor u, const Graph & g, VertexNameMap name_map) { - typename graph_traits < Graph >::adjacency_iterator vi, vi_end; out << get(name_map, u) << " -> { "; - for (boost::tie(vi, vi_end) = adjacent_vertices(u, g); vi != vi_end; ++vi) - out << get(name_map, *vi) << " "; + for (const auto& vertex : make_range_pair(adjacent_vertices(u, g))) + out << get(name_map, vertex) << " "; out << "}" << std::endl; } -template < typename NameMap > class name_equals_t { +template class name_equals_t { public: name_equals_t(const std::string & n, NameMap map) : m_name(n), m_name_map(map) { } - template < typename Vertex > bool operator()(Vertex u) const + template bool operator()(Vertex u) const { return get(m_name_map, u) == m_name; } @@ -64,22 +64,22 @@ template < typename NameMap > class name_equals_t { }; // object generator function -template < typename NameMap > - inline name_equals_t < NameMap > +template + inline name_equals_t name_equals(const std::string & str, NameMap name) { - return name_equals_t < NameMap > (str, name); + return name_equals_t (str, name); } int main() { - typedef adjacency_list // Add a vertex property - >graph_type; + property // Add a vertex property + >; graph_type g; // use default constructor to create empty graph const char* dep_file_name = "makefile-dependencies.dat"; @@ -98,39 +98,34 @@ main() } // Obtain internal property map from the graph - property_map < graph_type, vertex_name_t >::type name_map = - get(vertex_name, g); + auto name_map = get(vertex_name, g); read_graph_file(file_in, name_in, g, name_map); - graph_traits < graph_type >::vertex_descriptor yow, zag, bar; // Get vertex name property map from the graph - typedef property_map < graph_type, vertex_name_t >::type name_map_t; - name_map_t name = get(vertex_name, g); + auto name = get(vertex_name, g); // Get iterators for the vertex set - graph_traits < graph_type >::vertex_iterator i, end; - boost::tie(i, end) = vertices(g); + const auto [i, end] = vertices(g); // Find yow.h - name_equals_t < name_map_t > predicate1("yow.h", name); - yow = *std::find_if(i, end, predicate1); + using name_map_t = property_map::type; + name_equals_t predicate1("yow.h", name); + auto yow = *std::find_if(i, end, predicate1); // Find zag.o - name_equals_t < name_map_t > predicate2("zag.o", name); - zag = *std::find_if(i, end, predicate2); + name_equals_t predicate2("zag.o", name); + auto zag = *std::find_if(i, end, predicate2); // Find bar.o - name_equals_t < name_map_t > predicate3("bar.o", name); - bar = *std::find_if(i, end, predicate3); + name_equals_t predicate3("bar.o", name); + auto bar = *std::find_if(i, end, predicate3); - graph_traits < graph_type >::edge_descriptor e1, e2; - bool exists; // Get the edge connecting yow.h to zag.o - boost::tie(e1, exists) = edge(yow, zag, g); - assert(exists == true); + const auto [e1, exists1] = edge(yow, zag, g); + assert(exists1 == true); assert(source(e1, g) == yow); assert(target(e1, g) == zag); // Discover that there is no edge connecting zag.o to bar.o - boost::tie(e2, exists) = edge(zag, bar, g); - assert(exists == false); + const auto [e2, exists2] = edge(zag, bar, g); + assert(exists2 == false); assert(num_vertices(g) == 15); assert(num_edges(g) == 19); diff --git a/example/edge-iter-constructor.cpp b/example/edge-iter-constructor.cpp index d9f616aa..582e3080 100644 --- a/example/edge-iter-constructor.cpp +++ b/example/edge-iter-constructor.cpp @@ -12,8 +12,8 @@ using namespace boost; -template < typename T > - std::istream & operator >> (std::istream & in, std::pair < T, T > &p) { +template + std::istream & operator >> (std::istream & in, std::pair &p) { in >> p.first >> p.second; return in; } @@ -22,14 +22,14 @@ template < typename T > int main() { - typedef adjacency_list < + using graph_type = adjacency_list < listS, // Store out-edges of each vertex in a std::list vecS, // Store vertex set in a std::vector directedS // The graph is directed - > graph_type; + >; std::ifstream file_in("makefile-dependencies.dat"); - typedef graph_traits < graph_type >::vertices_size_type size_type; + using size_type = graph_traits::vertices_size_type; size_type n_vertices; file_in >> n_vertices; // read in number of vertices @@ -37,7 +37,7 @@ main() g(n_vertices); // create graph with n vertices // Read in edges - graph_traits < graph_type >::vertices_size_type u, v; + graph_traits::vertices_size_type u, v; while (file_in >> u) if (file_in >> v) add_edge(u, v, g); diff --git a/example/edge_basics.cpp b/example/edge_basics.cpp index 0d1d1152..06c4ff2b 100644 --- a/example/edge_basics.cpp +++ b/example/edge_basics.cpp @@ -12,7 +12,6 @@ #include #include -using namespace std; using namespace boost; @@ -43,8 +42,8 @@ template struct exercise_edge { exercise_edge(Graph& g) : G(g) {} - typedef typename boost::graph_traits::edge_descriptor Edge; - typedef typename boost::graph_traits::vertex_descriptor Vertex; + using Edge = typename boost::graph_traits::edge_descriptor; + using Vertex = typename boost::graph_traits::vertex_descriptor; void operator()(Edge e) const { //begin @@ -52,11 +51,11 @@ struct exercise_edge { // edge_traits class // Use the source() and target() functions to access the vertices // that belong to Edge e - Vertex src = source(e, G); - Vertex targ = target(e, G); + auto src = source(e, G); + auto targ = target(e, G); // print out the vertex id's just because - cout << "(" << src << "," << targ << ") "; + std::cout << "(" << src << "," << targ << ") "; //end } @@ -67,22 +66,22 @@ struct exercise_edge { int main() { - typedef adjacency_list<> MyGraph; + using MyGraph = adjacency_list<>; - typedef pair Pair; - Pair edge_array[8] = { Pair(0,1), Pair(0,2), Pair(0,3), Pair(0,4), + using Pair = std::pair; + Pair edge_array[] = { Pair(0,1), Pair(0,2), Pair(0,3), Pair(0,4), Pair(2,0), Pair(3,0), Pair(2,4), Pair(3,1) }; // Construct a graph using the edge_array (passing in pointers // (iterators) to the beginning and end of the array), and // specifying the number of vertices as 5 MyGraph G(5); - for (int i=0; i<8; ++i) - add_edge(edge_array[i].first, edge_array[i].second, G); + for (const auto& edge : edge_array) + add_edge(edge.first, edge.second, G); // Use the STL for_each algorithm to "exercise" all of the edges in // the graph - for_each(edges(G).first, edges(G).second, exercise_edge(G)); - cout << endl; + std::for_each(edges(G).first, edges(G).second, exercise_edge(G)); + std::cout << std::endl; return 0; } diff --git a/example/edge_coloring.cpp b/example/edge_coloring.cpp index 1a752fd1..685bf05f 100644 --- a/example/edge_coloring.cpp +++ b/example/edge_coloring.cpp @@ -35,11 +35,10 @@ int main(int, char *[]) { using namespace boost; - using namespace std; - typedef adjacency_list Graph; + using Graph = adjacency_list; - typedef std::pair Pair; - Pair edges[14] = { Pair(0,3), //a-d + using Pair = std::pair; + Pair edges[] = { Pair(0,3), //a-d Pair(0,5), //a-f Pair(1,2), //b-c Pair(1,4), //b-e @@ -56,14 +55,14 @@ int main(int, char *[]) Graph G(10); - for (size_t i = 0; i < sizeof(edges)/sizeof(edges[0]); i++) - add_edge(edges[i].first, edges[i].second, G); + for (const auto& edge : edges) + add_edge(edge.first, edge.second, G); - size_t colors = edge_coloring(G, get(edge_bundle, G)); + auto colors = edge_coloring(G, get(edge_bundle, G)); - cout << "Colored using " << colors << " colors" << endl; - for (size_t i = 0; i < sizeof(edges)/sizeof(edges[0]); i++) { - cout << " " << (char)('a' + edges[i].first) << "-" << (char)('a' + edges[i].second) << ": " << G[edge(edges[i].first, edges[i].second, G).first] << endl; + std::cout << "Colored using " << colors << " colors" << std::endl; + for (const auto& edge : edges) { + std::cout << " " << (char)('a' + edge.first) << "-" << (char)('a' + edge.second) << ": " << G[edge(edge.first, edge.second, G).first] << std::endl; } return 0; diff --git a/example/edge_connectivity.cpp b/example/edge_connectivity.cpp index 1e7535a2..5dc581bd 100644 --- a/example/edge_connectivity.cpp +++ b/example/edge_connectivity.cpp @@ -22,7 +22,7 @@ int main() { const int N = 8; - typedef adjacency_list UndirectedGraph; + using UndirectedGraph = adjacency_list; UndirectedGraph g(N); add_edge(0, 1, g); @@ -40,11 +40,10 @@ main() add_edge(5, 7, g); add_edge(6, 7, g); - typedef graph_traits::edge_descriptor edge_descriptor; - typedef graph_traits::degree_size_type degree_size_type; + using edge_descriptor = graph_traits::edge_descriptor; std::vector disconnecting_set; - degree_size_type c = edge_connectivity(g, std::back_inserter(disconnecting_set)); + auto c = edge_connectivity(g, std::back_inserter(disconnecting_set)); std::cout << "The edge connectivity is " << c << "." << std::endl; std::cout << "The disconnecting set is {"; diff --git a/example/edge_iterator_constructor.cpp b/example/edge_iterator_constructor.cpp index 95da715a..8557bffd 100644 --- a/example/edge_iterator_constructor.cpp +++ b/example/edge_iterator_constructor.cpp @@ -43,11 +43,11 @@ class edge_stream_iterator { public: - typedef std::input_iterator_tag iterator_category; - typedef std::pair value_type; - typedef std::ptrdiff_t difference_type; - typedef const value_type* pointer; - typedef const value_type& reference; + using iterator_category = std::input_iterator_tag ; + using value_type = std::pair ; + using difference_type = std::ptrdiff_t ; + using pointer = const value_type* ; + using reference = const value_type& ; edge_stream_iterator() : m_stream(0), m_end_marker(false) {} edge_stream_iterator(std::istream& s) : m_stream(&s) { m_read(); } @@ -57,7 +57,7 @@ class edge_stream_iterator { return *this; } edge_stream_iterator operator++(int) { - edge_stream_iterator tmp = *this; + auto tmp = *this; m_read(); return tmp; } @@ -93,8 +93,8 @@ bool operator!=(const edge_stream_iterator& x, int main() { - typedef boost::adjacency_list<> IteratorConstructibleGraph; - typedef boost::graph_traits Traits; + using IteratorConstructibleGraph = boost::adjacency_list<>; + using Traits = boost::graph_traits; Traits::vertices_size_type size_V; Traits::edges_size_type size_E; @@ -106,8 +106,7 @@ main() // VC++ can't handle the iterator constructor IteratorConstructibleGraph G(size_V); while (edge_iter != end) { - int i, j; - boost::tie(i, j) = *edge_iter++; + const auto [i, j] = *edge_iter++; boost::add_edge(i, j, G); } #else diff --git a/example/edge_property.cpp b/example/edge_property.cpp index 879e7570..1626c16d 100644 --- a/example/edge_property.cpp +++ b/example/edge_property.cpp @@ -54,10 +54,10 @@ #include #include #include +#include "range_pair.hpp" using namespace boost; -using namespace std; enum edge_myflow_t { edge_myflow }; @@ -72,45 +72,32 @@ namespace boost { template void print_network(const Graph& G) { - typedef typename boost::graph_traits::vertex_iterator Viter; - typedef typename boost::graph_traits::out_edge_iterator OutEdgeIter; - typedef typename boost::graph_traits::in_edge_iterator InEdgeIter; - - typename property_map::const_type - capacity = get(edge_mycapacity, G); - typename property_map::const_type - flow = get(edge_myflow, G); - - Viter ui, uiend; - boost::tie(ui, uiend) = vertices(G); - - for (; ui != uiend; ++ui) { - OutEdgeIter out, out_end; - cout << *ui << "\t"; - - boost::tie(out, out_end) = out_edges(*ui, G); - for(; out != out_end; ++out) - cout << "--(" << capacity[*out] << ", " << flow[*out] << ")--> " - << target(*out,G) << "\t"; - - InEdgeIter in, in_end; - cout << endl << "\t"; - boost::tie(in, in_end) = in_edges(*ui, G); - for(; in != in_end; ++in) - cout << "<--(" << capacity[*in] << "," << flow[*in] << ")-- " - << source(*in,G) << "\t"; - - cout << endl; + auto capacity = get(edge_mycapacity, G); + auto flow = get(edge_myflow, G); + + for (const auto& vertex : make_range_pair(vertices(G))) { + std::cout << vertex << "\t"; + + for (const auto& edge : make_range_pair(out_edges(vertex, G))) + std::cout << "--(" << capacity[edge] << ", " << flow[edge] << ")--> " + << target(edge,G) << "\t"; + + std::cout << std::endl << "\t"; + for (const auto& edge : make_range_pair(in_edges(vertex, G))) + std::cout << "<--(" << capacity[edge] << "," << flow[edge] << ")-- " + << source(edge, G) << "\t"; + + std::cout << std::endl; } } int main(int , char* []) { - typedef property Cap; - typedef property Flow; - typedef adjacency_list Graph; + using Cap = property; + using Flow = property; + using Graph = adjacency_list; const int num_vertices = 9; Graph G(num_vertices); @@ -143,16 +130,13 @@ int main(int , char* []) print_network(G); - property_map::type - flow = get(edge_myflow, G); + auto flow = get(edge_myflow, G); - boost::graph_traits::vertex_iterator v, v_end; - boost::graph_traits::out_edge_iterator e, e_end; int f = 0; - for (boost::tie(v, v_end) = vertices(G); v != v_end; ++v) - for (boost::tie(e, e_end) = out_edges(*v, G); e != e_end; ++e) - flow[*e] = ++f; - cout << endl << endl; + for (const auto& vertex : make_range_pair(vertices(G))) + for (const auto& edge : make_range_pair(out_edges(vertex, G))) + flow[edge] = ++f; + std::cout << std::endl << std::endl; remove_edge(6, 8, G); diff --git a/example/edmonds-karp-eg.cpp b/example/edmonds-karp-eg.cpp index 688cb674..da095a44 100644 --- a/example/edmonds-karp-eg.cpp +++ b/example/edmonds-karp-eg.cpp @@ -12,6 +12,7 @@ #include #include #include +#include "range_pair.hpp" // Use a DIMACS network flow file as stdin. // edmonds-karp-eg < max_flow.dat @@ -47,20 +48,18 @@ main() { using namespace boost; - typedef adjacency_list_traits < vecS, vecS, directedS > Traits; - typedef adjacency_list < listS, vecS, directedS, - property < vertex_name_t, std::string >, + using Traits = adjacency_list_traits; + using Graph = adjacency_list < listS, vecS, directedS, + property, property < edge_capacity_t, long, property < edge_residual_capacity_t, long, - property < edge_reverse_t, Traits::edge_descriptor > > > > Graph; + property>>>; Graph g; - property_map < Graph, edge_capacity_t >::type - capacity = get(edge_capacity, g); - property_map < Graph, edge_reverse_t >::type rev = get(edge_reverse, g); - property_map < Graph, edge_residual_capacity_t >::type - residual_capacity = get(edge_residual_capacity, g); + auto capacity = get(edge_capacity, g); + auto rev = get(edge_reverse, g); + auto residual_capacity = get(edge_residual_capacity, g); Traits::vertex_descriptor s, t; read_dimacs_max_flow(g, capacity, rev, s, t); @@ -78,13 +77,11 @@ main() std::cout << "s " << flow << std::endl << std::endl; std::cout << "c flow values:" << std::endl; - graph_traits < Graph >::vertex_iterator u_iter, u_end; - graph_traits < Graph >::out_edge_iterator ei, e_end; - for (boost::tie(u_iter, u_end) = vertices(g); u_iter != u_end; ++u_iter) - for (boost::tie(ei, e_end) = out_edges(*u_iter, g); ei != e_end; ++ei) - if (capacity[*ei] > 0) - std::cout << "f " << *u_iter << " " << target(*ei, g) << " " - << (capacity[*ei] - residual_capacity[*ei]) << std::endl; + for(const auto& vertex : make_range_pair(vertices(g))) + for (const auto& edge : make_range_pair(out_edges(vertex, g))) + if (capacity[edge] > 0) + std::cout << "f " << vertex << " " << target(edge, g) << " " + << (capacity[edge] - residual_capacity[edge]) << std::endl; return EXIT_SUCCESS; } diff --git a/example/exterior_properties.cpp b/example/exterior_properties.cpp index 3ca4edc3..48db4812 100644 --- a/example/exterior_properties.cpp +++ b/example/exterior_properties.cpp @@ -37,28 +37,22 @@ #include #include #include +#include "range_pair.hpp" template void print_network(Graph& G, Capacity capacity, Flow flow) { - typedef typename boost::graph_traits::vertex_iterator Viter; - typedef typename boost::graph_traits::out_edge_iterator OutEdgeIter; - typedef typename boost::graph_traits::in_edge_iterator InEdgeIter; - - Viter ui, uiend; - for (boost::tie(ui, uiend) = boost::vertices(G); ui != uiend; ++ui) { - OutEdgeIter out, out_end; - std::cout << *ui << "\t"; - - for(boost::tie(out, out_end) = boost::out_edges(*ui, G); out != out_end; ++out) - std::cout << "--(" << boost::get(capacity, *out) << ", " - << boost::get(flow, *out) << ")--> " << boost::target(*out,G) << "\t"; + for (const auto& vertex : make_range_pair(vertices(G))) { + std::cout << vertex << "\t"; + + for (const auto& edge : make_range_pair(out_edges(vertex, G))) + std::cout << "--(" << boost::get(capacity, edge) << ", " + << boost::get(flow, edge) << ")--> " << boost::target(edge, G) << "\t"; std::cout << std::endl << "\t"; - InEdgeIter in, in_end; - for(boost::tie(in, in_end) = boost::in_edges(*ui, G); in != in_end; ++in) - std::cout << "<--(" << boost::get(capacity, *in) << "," << boost::get(flow, *in) << ")-- " - << boost::source(*in, G) << "\t"; + for (const auto& edge : make_range_pair(in_edges(vertex, G))) + std::cout << "<--(" << boost::get(capacity, edge) << "," << boost::get(flow, edge) << ")-- " + << boost::source(edge, G) << "\t"; std::cout << std::endl; } } @@ -66,9 +60,9 @@ void print_network(Graph& G, Capacity capacity, Flow flow) int main(int , char* []) { - typedef boost::adjacency_list > Graph; + boost::property>; const int num_vertices = 9; Graph G(num_vertices); @@ -104,10 +98,10 @@ int main(int , char* []) { boost::add_edge(6, 8, 9, G); - typedef boost::property_map::type EdgeIndexMap; - EdgeIndexMap edge_id = boost::get(boost::edge_index, G); + using EdgeIndexMap = boost::property_map::type; + auto edge_id = boost::get(boost::edge_index, G); - typedef boost::iterator_property_map IterMap; + using IterMap = boost::iterator_property_map; print_network(G, IterMap(capacity, edge_id), IterMap(flow, edge_id)); diff --git a/example/exterior_property_map.cpp b/example/exterior_property_map.cpp index 9b9ac79e..f86da59d 100644 --- a/example/exterior_property_map.cpp +++ b/example/exterior_property_map.cpp @@ -14,7 +14,6 @@ #include #include -using namespace std; using namespace boost; /* @@ -59,8 +58,8 @@ void who_owes_who(EdgeIter first, EdgeIter last, const Graph& G, { while (first != last) { - cout << name[source(*first,G)] << " owes " - << name[target(*first,G)] << " some money" << endl; + std::cout << name[source(*first,G)] << " owes " + << name[target(*first,G)] << " some money" << std::endl; ++first; } } @@ -70,23 +69,23 @@ main(int, char*[]) { /* The property will be "names" attached to the vertices */ - string* names = new string[5]; + std::string* names = new std::string[5]; names[0] = "Jeremy"; names[1] = "Rich"; names[2] = "Andrew"; names[3] = "Jeff"; names[4] = "Kinis"; - typedef adjacency_list<> MyGraphType; + using MyGraphType = adjacency_list<>; - typedef pair Pair; - Pair edge_array[11] = { Pair(0,1), Pair(0,2), Pair(0,3), Pair(0,4), + using Pair = std::pair; + Pair edge_array[] = { Pair(0,1), Pair(0,2), Pair(0,3), Pair(0,4), Pair(2,0), Pair(3,0), Pair(2,4), Pair(3,1), Pair(3,4), Pair(4,0), Pair(4,1) }; MyGraphType G(5); - for (int i=0; i<11; ++i) - add_edge(edge_array[i].first, edge_array[i].second, G); + for (const auto& edge : edge_array) + add_edge(edge.first, edge.second, G); who_owes_who(edges(G).first, edges(G).second, G, names); diff --git a/example/family_tree.cpp b/example/family_tree.cpp index c3623935..1e49faf1 100644 --- a/example/family_tree.cpp +++ b/example/family_tree.cpp @@ -11,6 +11,8 @@ #include #include #include +#include "range_pair.hpp" + enum family { Jeanie, Debbie, Rick, John, Amanda, Margaret, Benjamin, N }; int @@ -29,14 +31,11 @@ main() add_edge(Rick, Margaret, g); add_edge(John, Benjamin, g); - graph_traits < adjacency_list <> >::vertex_iterator i, end; - graph_traits < adjacency_list <> >::adjacency_iterator ai, a_end; - property_map < adjacency_list <>, vertex_index_t >::type - index_map = get(vertex_index, g); + auto index_map = get(vertex_index, g); - for (boost::tie(i, end) = vertices(g); i != end; ++i) { - std::cout << name[get(index_map, *i)]; - boost::tie(ai, a_end) = adjacent_vertices(*i, g); + for (const auto& vertex : make_range_pair(vertices(g))) { + std::cout << name[get(index_map, vertex)]; + auto [ai, a_end] = adjacent_vertices(vertex, g); if (ai == a_end) std::cout << " has no children"; else diff --git a/example/fibonacci_heap.cpp b/example/fibonacci_heap.cpp index 3dbdc5c9..fab14e26 100644 --- a/example/fibonacci_heap.cpp +++ b/example/fibonacci_heap.cpp @@ -21,19 +21,19 @@ using namespace boost; int main() { - typedef indirect_cmp > ICmp; + using ICmp = indirect_cmp>; int i; boost::mt19937 gen; for (int N = 2; N < 200; ++N) { uniform_int<> distrib(0, N-1); - variate_generator > rand_gen(gen, distrib); - for (int t = 0; t < 10; ++t) { + variate_generator> rand_gen(gen, distrib); + for (std::size_t t = 0; t < 10; ++t) { std::vector v, w(N); ICmp cmp(&w[0], std::less()); fibonacci_heap Q(N, cmp); - for (int c = 0; c < w.size(); ++c) + for (std::size_t c = 0; c < w.size(); ++c) w[c] = c; std::random_shuffle(w.begin(), w.end()); @@ -48,7 +48,7 @@ main() } for (i = 0; i < N; ++i) { - v.push_back(w[Q.top()]); + v.emplace_back(w[Q.top()]); Q.pop(); } std::sort(w.begin(), w.end()); diff --git a/example/file_dependencies.cpp b/example/file_dependencies.cpp index 9bce7334..9d0ebb0a 100644 --- a/example/file_dependencies.cpp +++ b/example/file_dependencies.cpp @@ -32,7 +32,7 @@ #include #include #include -#include +#include #include #include @@ -40,8 +40,8 @@ #include #include #include +#include "range_pair.hpp" -using namespace std; using namespace boost; enum files_e { dax_h, yow_h, boz_h, zow_h, foo_cpp, @@ -57,7 +57,7 @@ const char* name[] = { "dax.h", "yow.h", "boz.h", "zow.h", "foo.cpp", struct print_visitor : public bfs_visitor<> { template void discover_vertex(Vertex v, Graph&) { - cout << name[v] << " "; + std::cout << name[v] << " "; } }; @@ -79,7 +79,7 @@ struct cycle_detector : public dfs_visitor<> int main(int,char*[]) { - typedef pair Edge; + using Edge = std::pair; Edge used_by[] = { Edge(dax_h, foo_cpp), Edge(dax_h, bar_cpp), Edge(dax_h, yow_h), Edge(yow_h, bar_cpp), Edge(yow_h, zag_cpp), @@ -98,91 +98,87 @@ int main(int,char*[]) }; const std::size_t nedges = sizeof(used_by)/sizeof(Edge); - typedef adjacency_list Graph; + using Graph = adjacency_list; #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 // VC++ can't handle the iterator constructor Graph g(N); for (std::size_t j = 0; j < nedges; ++j) { - graph_traits::edge_descriptor e; bool inserted; - boost::tie(e, inserted) = add_edge(used_by[j].first, used_by[j].second, g); + const auto [e, inserted] = add_edge(used_by[j].first, used_by[j].second, g); } #else Graph g(used_by, used_by + nedges, N); #endif - typedef graph_traits::vertex_descriptor Vertex; + using Vertex = graph_traits::vertex_descriptor; // Determine ordering for a full recompilation // and the order with files that can be compiled in parallel { - typedef list MakeOrder; + using MakeOrder = std::list; MakeOrder::iterator i; MakeOrder make_order; topological_sort(g, std::front_inserter(make_order)); - cout << "make ordering: "; - for (i = make_order.begin(); - i != make_order.end(); ++i) - cout << name[*i] << " "; + std::cout << "make ordering: "; + for (const auto& o : make_order) + std::cout << name[o] << " "; - cout << endl << endl; + std::cout << std::endl << std::endl; // Parallel compilation ordering std::vector time(N, 0); - for (i = make_order.begin(); i != make_order.end(); ++i) { + for (const auto& vertex : make_order) { // Walk through the in_edges an calculate the maximum time. - if (in_degree (*i, g) > 0) { - Graph::in_edge_iterator j, j_end; + if (in_degree (vertex, g) > 0) { int maxdist=0; // Through the order from topological sort, we are sure that every // time we are using here is already initialized. - for (boost::tie(j, j_end) = in_edges(*i, g); j != j_end; ++j) - maxdist=(std::max)(time[source(*j, g)], maxdist); + for (const auto& edge : make_range_pair(in_edges(vertex, g))) + maxdist=(std::max)(time[source(edge, g)], maxdist); time[*i]=maxdist+1; } } - cout << "parallel make ordering, " << endl - << "vertices with same group number can be made in parallel" << endl; + std::cout << "parallel make ordering, " << std::endl + << "vertices with same group number can be made in parallel" << std::endl; { - graph_traits::vertex_iterator i, iend; - for (boost::tie(i,iend) = vertices(g); i != iend; ++i) - cout << "time_slot[" << name[*i] << "] = " << time[*i] << endl; + for (const auto& vertex : make_range_pair(vertices(g))) + std::cout << "time_slot[" << name[vertex] << "] = " << time[vertex] << std::endl; } } - cout << endl; + std::cout << std::endl; // if I change yow.h what files need to be re-made? { - cout << "A change to yow.h will cause what to be re-made?" << endl; + std::cout << "A change to yow.h will cause what to be re-made?" << std::endl; print_visitor vis; breadth_first_search(g, vertex(yow_h, g), visitor(vis)); - cout << endl; + std::cout << std::endl; } - cout << endl; + std::cout << std::endl; // are there any cycles in the graph? { bool has_cycle = false; cycle_detector vis(has_cycle); depth_first_search(g, visitor(vis)); - cout << "The graph has a cycle? " << has_cycle << endl; + std::cout << "The graph has a cycle? " << has_cycle << std::endl; } - cout << endl; + std::cout << std::endl; // add a dependency going from bar.cpp to dax.h { - cout << "adding edge bar_cpp -> dax_h" << endl; + std::cout << "adding edge bar_cpp -> dax_h" << std::endl; add_edge(bar_cpp, dax_h, g); } - cout << endl; + std::cout << std::endl; // are there any cycles in the graph? { bool has_cycle = false; cycle_detector vis(has_cycle); depth_first_search(g, visitor(vis)); - cout << "The graph has a cycle now? " << has_cycle << endl; + std::cout << "The graph has a cycle now? " << has_cycle << std::endl; } return 0; diff --git a/example/filtered-copy-example.cpp b/example/filtered-copy-example.cpp index 226dfd29..9ab3b746 100644 --- a/example/filtered-copy-example.cpp +++ b/example/filtered-copy-example.cpp @@ -11,6 +11,7 @@ #include #include #include +#include "range_pair.hpp" template struct non_zero_degree { @@ -29,24 +30,24 @@ int main() { using namespace boost; - typedef adjacency_list < vecS, vecS, bidirectionalS, - property < vertex_name_t, char > > graph_t; + using graph_t = adjacency_list>; enum { a, b, c, d, e, f, g, N }; graph_t G(N); - property_map < graph_t, vertex_name_t >::type - name_map = get(vertex_name, G); + auto name_map = get(vertex_name, G); char name = 'a'; - graph_traits < graph_t >::vertex_iterator v, v_end; - for (boost::tie(v, v_end) = vertices(G); v != v_end; ++v, ++name) - name_map[*v] = name; + for (const auto& vertex : make_range_pair(vertices(G))) { + name_map[vertex] = name; + ++name; + } - typedef std::pair < int, int >E; + using E = std::pair; E edges[] = { E(a, c), E(a, d), E(b, a), E(b, d), E(c, f), E(d, c), E(d, e), E(d, f), E(e, b), E(e, g), E(f, e), E(f, g) }; - for (int i = 0; i < 12; ++i) - add_edge(edges[i].first, edges[i].second, G); + for (const auto& edge : edges) + add_edge(edge.first, edge.second, G); print_graph(G, name_map); std::cout << std::endl; diff --git a/example/filtered_graph.cpp b/example/filtered_graph.cpp index e325b5f8..964901cd 100644 --- a/example/filtered_graph.cpp +++ b/example/filtered_graph.cpp @@ -41,9 +41,9 @@ int main() { using namespace boost; - typedef adjacency_list > Graph; - typedef property_map::type EdgeWeightMap; + using Graph = adjacency_list>; + using EdgeWeightMap = property_map::type; enum { A, B, C, D, E, N }; const char* name = "ABCDE"; @@ -56,7 +56,7 @@ int main() add_edge(E, C, 0, g); positive_edge_weight filter(get(edge_weight, g)); - filtered_graph > + filtered_graph> fg(g, filter); std::cout << "filtered edge set: "; diff --git a/example/filtered_graph_edge_range.cpp b/example/filtered_graph_edge_range.cpp index eb140f64..42046dbd 100644 --- a/example/filtered_graph_edge_range.cpp +++ b/example/filtered_graph_edge_range.cpp @@ -24,6 +24,7 @@ #include #include #include +#include "range_pair.hpp" template struct positive_edge_weight { @@ -41,9 +42,9 @@ int main() { using namespace boost; - typedef adjacency_list > Graph; - typedef property_map::type EdgeWeightMap; + using Graph = adjacency_list>; + using EdgeWeightMap = property_map::type; enum { A, B, C, D, E, N }; const char* name = "ABCDE"; @@ -57,23 +58,21 @@ int main() add_edge(D, B, 3, g); add_edge(E, C, 0, g); - EdgeWeightMap weight = get(edge_weight, g); + auto weight = get(edge_weight, g); std::cout << "unfiltered edge_range(C,D)\n"; - graph_traits::out_edge_iterator f, l; - for (boost::tie(f, l) = edge_range(C, D, g); f != l; ++f) - std::cout << name[source(*f, g)] << " --" << weight[*f] - << "-> " << name[target(*f, g)] << "\n"; + for (const auto& edge : make_range_pair(edge_range(C, D, g))) + std::cout << name[source(edge, g)] << " --" << weight[edge] + << "-> " << name[target(edge, g)] << "\n"; positive_edge_weight filter(weight); - typedef filtered_graph > FGraph; + using FGraph = filtered_graph>; FGraph fg(g, filter); std::cout << "filtered edge_range(C,D)\n"; - graph_traits::out_edge_iterator first, last; - for (boost::tie(first, last) = edge_range(C, D, fg); first != last; ++first) - std::cout << name[source(*first, fg)] << " --" << weight[*first] - << "-> " << name[target(*first, fg)] << "\n"; + for (const auto& edge : make_range_pair(edge_range(C, D, fg))) + std::cout << name[source(edge, fg)] << " --" << weight[edge] + << "-> " << name[target(edge, fg)] << "\n"; return 0; } diff --git a/example/filtered_vec_as_graph.cpp b/example/filtered_vec_as_graph.cpp index 2d976a0e..f4a31cc2 100644 --- a/example/filtered_vec_as_graph.cpp +++ b/example/filtered_vec_as_graph.cpp @@ -38,16 +38,15 @@ int main() { using namespace boost; - enum { A, B, C, D, E, N }; + enum { A, B, C, D, E }; const char* name = "ABCDE"; - typedef std::vector < std::list < int > > Graph; - Graph g(N); - g[A].push_back(B); - g[A].push_back(C); - g[C].push_back(D); - g[C].push_back(E); - g[D].push_back(E); - g[E].push_back(C); + using Graph = std::vector>; + Graph g = { + {B, C}, //A + {}, //B + {D, E}, //C + {E}, //D + {C}}; //E constant_target filter(E); filtered_graph fg(g, filter); diff --git a/example/fr_layout.cpp b/example/fr_layout.cpp index 2f7e4fea..45189390 100644 --- a/example/fr_layout.cpp +++ b/example/fr_layout.cpp @@ -10,14 +10,13 @@ #include #include #include -#include #include #include #include #include #include #include -#include +#include "range_pair.hpp" using namespace boost; @@ -37,19 +36,19 @@ void usage() << " Vertices and their positions are written to standard output with the label,\n x-position, and y-position of a vertex on each line, separated by spaces.\n"; } -typedef boost::rectangle_topology<> topology_type; -typedef topology_type::point_type point_type; +using topology_type = boost::rectangle_topology<>; +using point_type = topology_type::point_type; -typedef adjacency_list > Graph; +using Graph = adjacency_list>; -typedef graph_traits::vertex_descriptor Vertex; +using Vertex = graph_traits::vertex_descriptor; -typedef std::map NameToVertex; +using NameToVertex = std::map; Vertex get_vertex(const std::string& name, Graph& g, NameToVertex& names) { - NameToVertex::iterator i = names.find(name); + auto i = names.find(name); if (i == names.end()) i = names.insert(std::make_pair(name, add_vertex(name, g))).first; return i->second; @@ -57,7 +56,7 @@ Vertex get_vertex(const std::string& name, Graph& g, NameToVertex& names) class progress_cooling : public linear_cooling { - typedef linear_cooling inherited; + using inherited = linear_cooling; public: explicit progress_cooling(std::size_t iterations) : inherited(iterations) @@ -72,7 +71,7 @@ class progress_cooling : public linear_cooling } private: - shared_ptr display; + std::shared_ptr display; }; int main(int argc, char* argv[]) @@ -89,10 +88,10 @@ int main(int argc, char* argv[]) if (arg == "--iterations") { ++arg_idx; if (arg_idx >= argc) { usage(); return -1; } - iterations = lexical_cast(argv[arg_idx]); + iterations = std::stoi(argv[arg_idx]); } else { - if (width == 0.0) width = lexical_cast(arg); - else if (height == 0.0) height = lexical_cast(arg); + if (width == 0.0) width = std::stod(arg); + else if (height == 0.0) height = std::stod(arg); else { usage(); return -1; @@ -113,11 +112,10 @@ int main(int argc, char* argv[]) add_edge(get_vertex(source, g, names), get_vertex(target, g, names), g); } - typedef std::vector PositionVec; + using PositionVec = std::vector; PositionVec position_vec(num_vertices(g)); - typedef iterator_property_map::type> - PositionMap; + using PositionMap = iterator_property_map::type>; PositionMap position(position_vec.begin(), get(vertex_index, g)); minstd_rand gen; @@ -127,10 +125,9 @@ int main(int argc, char* argv[]) (g, position, topo, cooling(progress_cooling(iterations))); - graph_traits::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { - std::cout << get(vertex_name, g, *vi) << '\t' - << position[*vi][0] << '\t' << position[*vi][1] << std::endl; + for (const auto& vertex : make_range_pair(vertices(g))) { + std::cout << get(vertex_name, g, vertex) << '\t' + << position[vertex][0] << '\t' << position[vertex][1] << std::endl; } return 0; } diff --git a/example/gerdemann.cpp b/example/gerdemann.cpp index ceb8a235..edd27b08 100644 --- a/example/gerdemann.cpp +++ b/example/gerdemann.cpp @@ -11,6 +11,7 @@ #include #include +#include "range_pair.hpp" /* Thanks to Dale Gerdemann for this example, which inspired some @@ -43,18 +44,15 @@ void merge_vertex typename boost::graph_traits::vertex_descriptor v, Graph& g, GetEdgeProperties getp) { - typedef boost::graph_traits Traits; + using Traits = boost::graph_traits; typename Traits::edge_descriptor e; - typename Traits::out_edge_iterator out_i, out_end; - for (boost::tie(out_i, out_end) = out_edges(v, g); out_i != out_end; ++out_i) { - e = *out_i; - typename Traits::vertex_descriptor targ = target(e, g); + for (const auto& e : make_range_pair(out_edges(v, g))) { + auto targ = target(e, g); add_edge(u, targ, getp(e), g); } - typename Traits::in_edge_iterator in_i, in_end; - for (boost::tie(in_i, in_end) = in_edges(v, g); in_i != in_end; ++in_i) { - e = *in_i; - typename Traits::vertex_descriptor src = source(e, g); + + for (const auto& e : make_range_pair(in_edges(v, g))) { + auto src = source(e, g); add_edge(src, u, getp(e), g); } clear_vertex(v, g); @@ -78,11 +76,11 @@ struct ordered_set_by_nameS { }; namespace boost { template struct container_gen { - typedef std::set > type; + using type = std::set>; }; template <> struct parallel_edge_traits { - typedef allow_parallel_edge_tag type; + using type = allow_parallel_edge_tag; }; } #endif @@ -105,9 +103,9 @@ main() std::cout << "This program requires partial specialization." << std::endl; #else using namespace boost; - typedef property EdgeProperty; - typedef adjacency_list graph_type; + using EdgeProperty = property; + using graph_type = adjacency_list ; graph_type g; @@ -121,16 +119,13 @@ main() add_edge(3, 4, EdgeProperty('h'), g); add_edge(0, 1, EdgeProperty('c'), g); - property_map::type id = get(vertex_index, g); - property_map::type name = get(edge_name, g); - - graph_traits::vertex_iterator i, end; - graph_traits::out_edge_iterator ei, edge_end; + auto id = get(vertex_index, g); + auto name = get(edge_name, g); - for (boost::tie(i, end) = vertices(g); i != end; ++i) { - std::cout << id[*i] << " "; - for (boost::tie(ei, edge_end) = out_edges(*i, g); ei != edge_end; ++ei) - std::cout << " --" << name[*ei] << "--> " << id[target(*ei, g)] << " "; + for (const auto& vertex : make_range_pair(vertices(g))) { + std::cout << id[vertex] << " "; + for (const auto& edge : make_range_pair(out_edges(vertex, g))) + std::cout << " --" << name[edge] << "--> " << id[target(edge, g)] << " "; std::cout << std::endl; } std::cout << std::endl; @@ -138,10 +133,10 @@ main() std::cout << "merging vertex 1 into vertex 0" << std::endl << std::endl; merge_vertex(0, 1, g, get_edge_name(g)); - for (boost::tie(i, end) = vertices(g); i != end; ++i) { - std::cout << id[*i] << " "; - for (boost::tie(ei, edge_end) = out_edges(*i, g); ei != edge_end; ++ei) - std::cout << " --" << name[*ei] << "--> " << id[target(*ei, g)] << " "; + for (const auto& vertex : make_range_pair(vertices(g))) { + std::cout << id[vertex] << " "; + for (const auto& edge : make_range_pair(out_edges(vertex, g))) + std::cout << " --" << name[edge] << "--> " << id[target(edge, g)] << " "; std::cout << std::endl; } std::cout << std::endl; diff --git a/example/girth.cpp b/example/girth.cpp index 9c9cc23c..9bcf63c8 100644 --- a/example/girth.cpp +++ b/example/girth.cpp @@ -48,21 +48,22 @@ #include #include #include +#include "range_pair.hpp" -typedef boost::graph_traits Traits; -typedef Traits::vertex_descriptor vertex_descriptor; -typedef Traits::edge_descriptor edge_descriptor; -typedef Traits::vertex_iterator vertex_iterator; +using Traits = boost::graph_traits; +using vertex_descriptor = Traits::vertex_descriptor; +using edge_descriptor = Traits::edge_descriptor; +using vertex_iterator = Traits::vertex_iterator; std::vector distance_list; -typedef boost::v_property dist_t; +using dist_t = boost::v_property; boost::property_map::type d_map; -typedef boost::u_property pred_t; +using pred_t = boost::u_property; boost::property_map::type p_map; -typedef boost::w_property color_t; +using color_t = boost::w_property; boost::property_map::type c_map; class diameter_and_girth_visitor : public boost::bfs_visitor<> @@ -72,14 +73,14 @@ class diameter_and_girth_visitor : public boost::bfs_visitor<> : k(k_), girth(girth_) { } void tree_edge(edge_descriptor e, Graph* g) { - vertex_descriptor u = source(e, g), v = target(e, g); + auto u = source(e, g), v = target(e, g); k = d_map[u] + 1; d_map[v] = k; ++distance_list[k]; p_map[v] = u; } void non_tree_edge(edge_descriptor e, Graph* g) { - vertex_descriptor u = source(e, g), v = target(e, g); + auto u = source(e, g), v = target(e, g); k = d_map[u] + 1; if (d_map[v] + k < girth && v != p_map[u]) girth = d_map[v]+ k; @@ -118,8 +119,7 @@ main() if (q == 0) break; - Graph* g; - g = raman(p, q, 0L, 0L); + auto g = raman(p, q, 0L, 0L); if (g == 0) { std::cerr << " Sorry, I couldn't make that graph (error code " << panic_code << ")" << std::endl; @@ -133,9 +133,8 @@ main() p_map = get(pred_t(), g); c_map = get(color_t(), g); - vertex_iterator i, end; - for (boost::tie(i, end) = boost::vertices(g); i != end; ++i) - d_map[*i] = 0; + for (const auto& vertex : make_range_pair(boost::vertices(g))) + d_map[vertex] = 0; std::size_t k = 0; std::size_t girth = (std::numeric_limits::max)(); diff --git a/example/graph-assoc-types.cpp b/example/graph-assoc-types.cpp index aedeebca..0019f3d0 100644 --- a/example/graph-assoc-types.cpp +++ b/example/graph-assoc-types.cpp @@ -10,26 +10,28 @@ using namespace boost; -template < typename Graph > void +template void generic_foo(Graph & g) { // Access descriptor types - typedef typename graph_traits < Graph >::vertex_descriptor Vertex; - typedef typename graph_traits < Graph >::edge_descriptor Edge; + // using Vertex = typename graph_traits::vertex_descriptor; + // using Edge = typename graph_traits::edge_descriptor; + // Access category types - typedef typename graph_traits < Graph >::directed_category Dir; - typedef typename graph_traits < Graph >::edge_parallel_category Par; + // using Dir = typename graph_traits::directed_category; + // using Par = typename graph_traits::edge_parallel_category; + // Access iterator types... // Access size types... // Now do something useful... } -template < typename Graph > void +template void generic_bar(Graph & g) { // Declare some vertex and edge descriptor variables - typename graph_traits < Graph >::vertex_descriptor u = vertex(0,g), v = vertex(1,g); - typename graph_traits < Graph >::edge_descriptor e1, e2; + auto u = vertex(0,g), v = vertex(1,g); + typename graph_traits::edge_descriptor e1, e2; // Set u and e1 to valid descriptors... v = u; // Make v a handle to the same vertex as u. e2 = e1; // Make e2 a handle to the same edge as e1. @@ -40,46 +42,42 @@ generic_bar(Graph & g) } // This version of foo gets called when g is directed -template < typename Graph > void +template void foo_dispatch(Graph & g, boost::directed_tag) { //... } // This version of foo gets called when g is undirected -template < typename Graph > void +template void foo_dispatch(Graph & g, boost::undirected_tag) { //... } -template < typename Graph > void +template void foo(Graph & g) { - typedef typename boost::graph_traits < Graph >::directed_category Cat; + using Cat = typename boost::graph_traits::directed_category; foo_dispatch(g, Cat()); } -template < typename Digraph > void +template void foo(Digraph & digraph, - typename graph_traits < Digraph >::vertex_descriptor u, - typename graph_traits < Digraph >::vertex_descriptor v) + typename graph_traits::vertex_descriptor u, + typename graph_traits::vertex_descriptor v) { - typedef typename graph_traits < Digraph >::edge_descriptor edge_t; - std::pair e1, e2; - e1 = edge(u, v, digraph); - e2 = edge(v, u, digraph); + auto e1 = edge(u, v, digraph); + auto e2 = edge(v, u, digraph); assert(e1.first != e2.first); } -template < typename Undigraph > void +template void bar(Undigraph & undigraph, - typename graph_traits < Undigraph >::vertex_descriptor u, - typename graph_traits < Undigraph >::vertex_descriptor v) + typename graph_traits::vertex_descriptor u, + typename graph_traits::vertex_descriptor v) { - typedef typename graph_traits < Undigraph >::edge_descriptor edge_t; - std::pair e1, e2; - e1 = edge(u, v, undigraph); - e2 = edge(v, u, undigraph); + auto e1 = edge(u, v, undigraph); + auto e2 = edge(v, u, undigraph); assert(e1.first == e2.first); } @@ -88,7 +86,7 @@ int main() { - boost::adjacency_list < vecS, vecS, directedS > g(2); + boost::adjacency_list g(2); add_edge(0, 1, g); add_edge(1, 0, g); generic_foo(g); @@ -96,7 +94,7 @@ main() foo(g); foo(g, vertex(0, g), vertex(1, g)); - boost::adjacency_list < vecS, vecS, undirectedS > ug(2); + boost::adjacency_list ug(2); add_edge(0, 1, g); bar(ug, vertex(0, g), vertex(1, g)); diff --git a/example/graph-property-iter-eg.cpp b/example/graph-property-iter-eg.cpp index 0c11914a..97b8ef6f 100644 --- a/example/graph-property-iter-eg.cpp +++ b/example/graph-property-iter-eg.cpp @@ -11,24 +11,25 @@ #include #include #include +#include "range_pair.hpp" int main() { using namespace boost; - typedef adjacency_list < listS, vecS, directedS, - property < vertex_name_t, std::string > >graph_t; + using graph_t = adjacency_list>; graph_t g(3); const char *vertex_names[] = { "Kubrick", "Clark", "Hal" }; int i = 0; - graph_property_iter_range < graph_t, vertex_name_t >::iterator v, v_end; - for (boost::tie(v, v_end) = get_property_iter_range(g, vertex_name); - v != v_end; ++v, ++i) - *v = vertex_names[i]; + for (auto& vertex : make_range_pair(get_property_iter_range(g, vertex_name))) { + vertex = vertex_names[i]; + ++i; + } - boost::tie(v, v_end) = get_property_iter_range(g, vertex_name); - std::copy(v, v_end, std::ostream_iterator < std::string > (std::cout, " ")); + auto [v, v_end] = get_property_iter_range(g, vertex_name); + std::copy(v, v_end, std::ostream_iterator (std::cout, " ")); std::cout << std::endl; return 0; } diff --git a/example/graph-thingie.cpp b/example/graph-thingie.cpp index 6b8031dd..ebd17240 100644 --- a/example/graph-thingie.cpp +++ b/example/graph-thingie.cpp @@ -13,62 +13,58 @@ #include #include -#include +#include "range_pair.hpp" #include #include +#include using namespace boost; -using namespace std; // // Create a custom graph properties // (see the documentation for adjacency list) -struct graph_identifier_t { typedef graph_property_tag kind; }; -struct vertex_label_t { typedef vertex_property_tag kind; }; +struct graph_identifier_t { using kind = graph_property_tag; }; +struct vertex_label_t { using kind = vertex_property_tag; }; int main() { // Vertex properties - typedef property < vertex_name_t, string, - property < vertex_label_t, string, - property < vertex_root_t, int > > > vertex_p; + using vertex_p = property>>; // Edge properties - typedef property < edge_name_t, string > edge_p; + using edge_p = property; // Graph properties - typedef property < graph_name_t, string, - property < graph_identifier_t, string > > graph_p; + using graph_p = property>; // adjacency_list-based type - typedef adjacency_list < vecS, vecS, directedS, - vertex_p, edge_p, graph_p > graph_t; + using graph_t = adjacency_list; // Construct an empty graph and prepare the dynamic_property_maps. graph_t graph(0); dynamic_properties dp; - property_map::type vname = - get(vertex_name, graph); + auto vname = get(vertex_name, graph); dp.property("node_id",vname); - property_map::type vlabel = - get(vertex_label_t(), graph); + auto vlabel = get(vertex_label_t(), graph); dp.property("label",vlabel); - property_map::type root = - get(vertex_root, graph); + auto root = get(vertex_root, graph); dp.property("root",root); - property_map::type elabel = - get(edge_name, graph); + auto elabel = get(edge_name, graph); dp.property("label",elabel); // Use ref_property_map to turn a graph property into a property map - ref_property_map + ref_property_map gname(get_property(graph,graph_name)); dp.property("name",gname); // Use ref_property_map to turn a graph property into a property map - ref_property_map + ref_property_map gid(get_property(graph,graph_identifier_t())); dp.property("identifier",gid); // Sample graph as an istream; @@ -86,21 +82,21 @@ const char* dot = }"; - istringstream gvgraph(dot); + std::istringstream gvgraph(dot); bool status = read_graphviz(gvgraph,graph,dp,"node_id"); if (!status) { - cerr << "read_graphviz() failed." << endl; + std::cerr << "read_graphviz() failed." << std::endl; return -1; } - cout << "graph " << get("name",dp,&graph) << + std::cout << "graph " << get("name",dp,&graph) << " (" << get("identifier",dp,&graph) << ")\n\n"; - BOOST_FOREACH( graph_t::vertex_descriptor v, vertices(graph) ) { - cout << "vertex " << get("node_id",dp,v) << + for (auto v : make_range_pair(vertices(graph))) { + std::cout << "vertex " << get("node_id",dp,v) << " (" << get("label",dp,v) << ")\n"; } - return 0; + return status ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/example/graph.cpp b/example/graph.cpp index c73fba7b..165cf12a 100644 --- a/example/graph.cpp +++ b/example/graph.cpp @@ -13,43 +13,40 @@ #include #include +#include "range_pair.hpp" using namespace boost; -using namespace std; -typedef property > > > > VertexProperty; -typedef property EdgeProperty; -typedef adjacency_list Graph; + property>>>>; +using EdgeProperty = property; +using Graph = adjacency_list; template void print(Graph& g) { - typename Graph::vertex_iterator i, end; - typename Graph::out_edge_iterator ei, edge_end; - for(boost::tie(i,end) = vertices(g); i != end; ++i) { - cout << *i << " --> "; - for (boost::tie(ei,edge_end) = out_edges(*i, g); ei != edge_end; ++ei) - cout << target(*ei, g) << " "; - cout << endl; + for (const auto& vertex : make_range_pair(boost::vertices(g))) { + std::cout << vertex << " --> "; + for (const auto& edge : make_range_pair(out_edges(vertex, g))) + std::cout << target(edge, g) << " "; + std::cout << std::endl; } } std::size_t myrand(std::size_t N) { std::size_t ret = rand() % N; - // cout << "N = " << N << " rand = " << ret << endl; + // std::cout << "N = " << N << " rand = " << ret << std::endl; return ret; } template bool check_edge(Graph& g, std::size_t a, std::size_t b) { - typename Graph::adjacency_iterator vi, viend, found; - boost::tie(vi, viend) = adjacent_vertices(vertex(a,g), g); + auto [vi, viend] = adjacent_vertices(vertex(a,g), g); - found = find(vi, viend, vertex(b, g)); + auto found = find(vi, viend, vertex(b, g)); if ( found == viend ) return false; @@ -68,16 +65,16 @@ int main(int, char*[]) for (i=0; i<6; ++i) { std::size_t a = myrand(N), b = myrand(N); while ( a == b ) b = myrand(N); - cout << "edge edge (" << a << "," << b <<")" << endl; + std::cout << "edge edge (" << a << "," << b <<")" << std::endl; //add edges add_edge(a, b, g); is_failed = is_failed || (! check_edge(g, a, b) ); } if ( is_failed ) - cerr << " Failed."<< endl; + std::cerr << " Failed."<< std::endl; else - cerr << " Passed."<< endl; + std::cerr << " Passed."<< std::endl; print(g); @@ -85,14 +82,14 @@ int main(int, char*[]) for (i = 0; i<2; ++i) { std::size_t a = myrand(N), b = myrand(N); while ( a == b ) b = myrand(N); - cout << "remove edge (" << a << "," << b <<")" << endl; + std::cout << "remove edge (" << a << "," << b <<")" << std::endl; remove_edge(a, b, g); is_failed = is_failed || check_edge(g, a, b); } if ( is_failed ) - cerr << " Failed."<< endl; + std::cerr << " Failed."<< std::endl; else - cerr << " Passed."<< endl; + std::cerr << " Passed."<< std::endl; print(g); @@ -104,26 +101,26 @@ int main(int, char*[]) N = num_vertices(g); if ( (N - 2) != old_N ) - cerr << " Failed."<< endl; + std::cerr << " Failed."<< std::endl; else - cerr << " Passed."<< endl; + std::cerr << " Passed."<< std::endl; is_failed = false; for (i=0; i<2; ++i) { std::size_t a = myrand(N), b = myrand(N); while ( a == vid ) a = myrand(N); while ( b == vidp1 ) b = myrand(N); - cout << "add edge (" << vid << "," << a <<")" << endl; - cout << "add edge (" << vid << "," << vidp1 <<")" << endl; + std::cout << "add edge (" << vid << "," << a <<")" << std::endl; + std::cout << "add edge (" << vid << "," << vidp1 <<")" << std::endl; add_edge(vid, a, g); add_edge(b, vidp1, g); is_failed = is_failed || ! check_edge(g, vid, a); is_failed = is_failed || ! check_edge(g, b, vidp1); } if ( is_failed ) - cerr << " Failed."<< endl; + std::cerr << " Failed."<< std::endl; else - cerr << " Passed."<< endl; + std::cerr << " Passed."<< std::endl; print(g); // clear_vertex @@ -134,7 +131,7 @@ int main(int, char*[]) if ( out_degree(c, g) != 0 ) is_failed = true; - cout << "Removing vertex " << c << endl; + std::cout << "Removing vertex " << c << std::endl; remove_vertex(c, g); old_N = N; @@ -144,9 +141,9 @@ int main(int, char*[]) is_failed = true; if ( is_failed ) - cerr << " Failed."<< endl; + std::cerr << " Failed."<< std::endl; else - cerr << " Passed."<< endl; + std::cerr << " Passed."<< std::endl; print(g); diff --git a/example/graph_as_tree.cpp b/example/graph_as_tree.cpp index 635cd179..2dcf62ca 100644 --- a/example/graph_as_tree.cpp +++ b/example/graph_as_tree.cpp @@ -33,29 +33,28 @@ class tree_printer { int main() { using namespace boost; - typedef adjacency_list > graph_t; - typedef graph_traits::vertex_descriptor vertex_t; + using graph_t = adjacency_list>; + using vertex_t = graph_traits::vertex_descriptor; graph_t g; - vertex_t a = add_vertex(g), + auto a = add_vertex(g), b = add_vertex(g), c = add_vertex(g); add_edge(a, b, g); add_edge(a, c, g); - typedef property_map::type vertex_name_map_t; - vertex_name_map_t name = get(vertex_name, g); + auto name = get(vertex_name, g); name[a] = "A"; name[b] = "B"; name[c] = "C"; - typedef iterator_property_map::iterator, - property_map::type> parent_map_t; + using parent_map_t = iterator_property_map::iterator, + property_map::type>; std::vector parent(num_vertices(g)); - typedef graph_as_tree tree_t; + using tree_t = graph_as_tree; tree_t t(g, a, make_iterator_property_map(parent.begin(), get(vertex_index, g))); diff --git a/example/graph_property.cpp b/example/graph_property.cpp index ceb71c47..73fb32e7 100644 --- a/example/graph_property.cpp +++ b/example/graph_property.cpp @@ -13,18 +13,17 @@ int main() { using namespace boost; - using std::string; - typedef adjacency_list, - property > graph_t; + property>; graph_t g; get_property(g, graph_name) = "graph"; std::cout << "name: " << get_property(g, graph_name) << std::endl; - typedef subgraph subgraph_t; + using subgraph_t = subgraph; subgraph_t sg; get_property(sg, graph_name) = "subgraph"; diff --git a/example/graphviz.cpp b/example/graphviz.cpp index a21d8541..d83e8b78 100644 --- a/example/graphviz.cpp +++ b/example/graphviz.cpp @@ -14,13 +14,13 @@ using namespace boost; -typedef boost::adjacency_list, - property > Digraph; + property>; -typedef boost::adjacency_list, - property > Graph; + property>; void test_graph_read_write(const std::string& filename) { @@ -36,7 +36,7 @@ void test_graph_read_write(const std::string& filename) BOOST_CHECK(num_vertices(g) == 4); BOOST_CHECK(num_edges(g) == 4); - typedef graph_traits::vertex_descriptor Vertex; + using Vertex = graph_traits::vertex_descriptor; std::map name_to_vertex; BGL_FORALL_VERTICES(v, g, Graph) diff --git a/example/grid_graph_example.cpp b/example/grid_graph_example.cpp index 1406f2ef..f5f0f27a 100644 --- a/example/grid_graph_example.cpp +++ b/example/grid_graph_example.cpp @@ -14,8 +14,8 @@ #define DIMENSIONS 3 using namespace boost; -typedef grid_graph Graph; -typedef graph_traits Traits; +using Graph = grid_graph; +using Traits = graph_traits; // Define a simple function to print vertices void print_vertex(Traits::vertex_descriptor vertex_to_print) { @@ -63,7 +63,7 @@ int main(int argc, char* argv[]) { (graph.wrapped(2) ? "W" : "U") << std::endl; // prints "W, U, W" // Start with the first vertex in the graph - Traits::vertex_descriptor first_vertex = vertex(0, graph); + auto first_vertex = vertex(0, graph); print_vertex(first_vertex); // prints "(0, 0, 0)" // Print the next vertex in dimension 0 diff --git a/example/grid_graph_properties.cpp b/example/grid_graph_properties.cpp index 37d252a8..22cb50e7 100644 --- a/example/grid_graph_properties.cpp +++ b/example/grid_graph_properties.cpp @@ -14,7 +14,7 @@ int main(int argc, char* argv[]) { // A 2D grid graph - typedef boost::grid_graph<2> GraphType; + using GraphType = boost::grid_graph<2>; // Create a 5x5 graph const unsigned int dimension = 5; @@ -22,7 +22,7 @@ int main(int argc, char* argv[]) GraphType graph(lengths); // Get the index map of the grid graph - typedef boost::property_map::const_type indexMapType; + using indexMapType = boost::property_map::const_type; indexMapType indexMap(get(boost::vertex_index, graph)); // Create a float for every node in the graph @@ -33,7 +33,7 @@ int main(int argc, char* argv[]) put(dataMap, v, 2.0f); // Get the data at the node at position (0,1) in the grid - float retrieved = get(dataMap, v); + auto retrieved = get(dataMap, v); std::cout << "Retrieved value: " << retrieved << std::endl; return 0; diff --git a/example/hawick_circuits.cpp b/example/hawick_circuits.cpp index b29467b9..cbbcdfc8 100644 --- a/example/hawick_circuits.cpp +++ b/example/hawick_circuits.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -32,11 +31,7 @@ struct cycle_printer // Get the property map containing the vertex indices // so we can print them. - typedef typename boost::property_map< - Graph, boost::vertex_index_t - >::const_type IndexMap; - - IndexMap indices = get(boost::vertex_index, g); + auto indices = get(boost::vertex_index, g); // Iterate over path printing each vertex that forms the cycle. typename Path::const_iterator i, before_end = boost::prior(p.end()); @@ -54,8 +49,8 @@ struct cycle_printer template void build_graph(Graph& graph, unsigned int const nvertices, VertexPairIterator first, VertexPairIterator last) { - typedef boost::graph_traits Traits; - typedef typename Traits::vertex_descriptor vertex_descriptor; + using Traits = boost::graph_traits; + using vertex_descriptor = typename Traits::vertex_descriptor; std::map vertices; for (unsigned int i = 0; i < nvertices; ++i) @@ -84,7 +79,7 @@ int main(int argc, char const* argv[]) { return EXIT_FAILURE; } - unsigned int num_vertices = boost::lexical_cast(argv[1]); + unsigned int num_vertices = std::stoul(argv[1]); std::istream_iterator first_vertex(std::cin), last_vertex; boost::directed_graph<> graph; build_graph(graph, num_vertices, first_vertex, last_vertex); diff --git a/example/helper.hpp b/example/helper.hpp index 1247618a..9cd81349 100644 --- a/example/helper.hpp +++ b/example/helper.hpp @@ -18,13 +18,10 @@ template typename boost::graph_traits::vertex_descriptor add_named_vertex(Graph& g, NameMap nm, const std::string& name, VertexMap& vm) { - typedef typename boost::graph_traits::vertex_descriptor Vertex; - typedef typename VertexMap::iterator Iterator; + using Vertex = typename boost::graph_traits::vertex_descriptor; Vertex v; - Iterator iter; - bool inserted; - boost::tie(iter, inserted) = vm.insert(make_pair(name, Vertex())); + auto [iter, inserted] = vm.insert(make_pair(name, Vertex())); if(inserted) { // The name was unique so we need to add a vertex to the graph v = add_vertex(g); @@ -43,16 +40,16 @@ template inline std::map::vertex_descriptor> read_graph(Graph& g, NameMap nm, InputStream& is) { - typedef typename boost::graph_traits::vertex_descriptor Vertex; + using Vertex = typename boost::graph_traits::vertex_descriptor; std::map verts; for(std::string line; std::getline(is, line); ) { if(line.empty()) continue; - std::size_t index = line.find_first_of(','); + auto index = line.find_first_of(','); std::string first(line, 0, index); std::string second(line, index + 1); - Vertex u = add_named_vertex(g, nm, first, verts); - Vertex v = add_named_vertex(g, nm, second, verts); + auto u = add_named_vertex(g, nm, first, verts); + auto v = add_named_vertex(g, nm, second, verts); add_edge(u, v, g); } return verts; @@ -62,8 +59,8 @@ template inline std::map::vertex_descriptor> read_graph(Graph& g, InputStream& is) { - typedef typename boost::graph_traits::vertex_descriptor Vertex; - typedef boost::null_property_map NameMap; + using Vertex = typename boost::graph_traits::vertex_descriptor; + using NameMap = boost::null_property_map; return read_graph(g, NameMap(), is); } @@ -71,13 +68,12 @@ template ::vertex_descriptor> read_weighted_graph(Graph& g, NameMap nm, WeightMap wm, InputStream& is) { - typedef typename boost::graph_traits::vertex_descriptor Vertex; - typedef typename boost::graph_traits::edge_descriptor Edge; + using Vertex = typename boost::graph_traits::vertex_descriptor; std::map verts; for(std::string line; std::getline(is, line); ) { if(line.empty()) continue; - std::size_t i = line.find_first_of(','); - std::size_t j = line.find_first_of(',', i + 1); + auto i = line.find_first_of(','); + auto j = line.find_first_of(',', i + 1); std::string first(line, 0, i); std::string second(line, i + 1, j - i - 1); std::string prob(line, j + 1); @@ -88,11 +84,11 @@ read_weighted_graph(Graph& g, NameMap nm, WeightMap wm, InputStream& is) ss >> p; // add the vertices to the graph - Vertex u = add_named_vertex(g, nm, first, verts); - Vertex v = add_named_vertex(g, nm, second, verts); + auto u = add_named_vertex(g, nm, first, verts); + auto v = add_named_vertex(g, nm, second, verts); // add the edge and set the weight - Edge e = add_edge(u, v, g).first; + auto e = add_edge(u, v, g).first; put(wm, e, p); } return verts; @@ -103,8 +99,8 @@ template inline std::map::vertex_descriptor> read_weighted_graph(Graph& g, WeightMap wm, InputStream& is) { - typedef typename boost::graph_traits::vertex_descriptor Vertex; - typedef boost::null_property_map NameMap; + using Vertex = typename boost::graph_traits::vertex_descriptor; + using NameMap = boost::null_property_map; return read_weighted_graph(g, NameMap(), wm, is); } diff --git a/example/implicit_graph.cpp b/example/implicit_graph.cpp index 0d788735..ee7adf63 100644 --- a/example/implicit_graph.cpp +++ b/example/implicit_graph.cpp @@ -11,9 +11,9 @@ #include #include #include -#include #include #include +#include "range_pair.hpp" /* @@ -91,15 +91,15 @@ struct edge_weight_map; // ReadablePropertyGraph associated types namespace boost { template<> - struct property_map< ring_graph, edge_weight_t > { - typedef edge_weight_map type; - typedef edge_weight_map const_type; + struct property_map { + using type = edge_weight_map; + using const_type = edge_weight_map; }; template<> - struct property_map< const ring_graph, edge_weight_t > { - typedef edge_weight_map type; - typedef edge_weight_map const_type; + struct property_map { + using type = edge_weight_map; + using const_type = edge_weight_map; }; } @@ -121,35 +121,35 @@ indices. Vertex 0 is also adjacent to the vertex n-1. class ring_graph { public: // Graph associated types - typedef std::size_t vertex_descriptor; - typedef boost::undirected_tag directed_category; - typedef boost::disallow_parallel_edge_tag edge_parallel_category; - typedef ring_traversal_catetory traversal_category; + using vertex_descriptor = std::size_t; + using directed_category = boost::undirected_tag; + using edge_parallel_category = boost::disallow_parallel_edge_tag; + using traversal_category = ring_traversal_catetory; // IncidenceGraph associated types - typedef std::pair edge_descriptor; - typedef ring_incident_edge_iterator out_edge_iterator; - typedef std::size_t degree_size_type; + using edge_descriptor = std::pair; + using out_edge_iterator = ring_incident_edge_iterator; + using degree_size_type = std::size_t; // BidirectionalGraph associated types // Note that undirected graphs make no distinction between in- and out- // edges. - typedef ring_incident_edge_iterator in_edge_iterator; + using in_edge_iterator = ring_incident_edge_iterator; // AdjacencyGraph associated types - typedef ring_adjacency_iterator adjacency_iterator; + using adjacency_iterator = ring_adjacency_iterator; // VertexListGraph associated types - typedef boost::counting_iterator vertex_iterator; - typedef std::size_t vertices_size_type; + using vertex_iterator = boost::counting_iterator; + using vertices_size_type = std::size_t; // EdgeListGraph associated types - typedef ring_edge_iterator edge_iterator; - typedef std::size_t edges_size_type; + using edge_iterator = ring_edge_iterator; + using edges_size_type = std::size_t; // This type is not part of a graph concept, but is used to return the // default vertex index map used by the Dijkstra search algorithm. - typedef vertex_descriptor vertex_property_type; + using vertex_property_type = vertex_descriptor; ring_graph(std::size_t n):m_n(n) {}; std::size_t n() const {return m_n;} @@ -160,16 +160,16 @@ class ring_graph { // Use these graph_traits parameterizations to refer to the associated // graph types. -typedef boost::graph_traits::vertex_descriptor vertex_descriptor; -typedef boost::graph_traits::edge_descriptor edge_descriptor; -typedef boost::graph_traits::out_edge_iterator out_edge_iterator; -typedef boost::graph_traits::in_edge_iterator in_edge_iterator; -typedef boost::graph_traits::adjacency_iterator adjacency_iterator; -typedef boost::graph_traits::degree_size_type degree_size_type; -typedef boost::graph_traits::vertex_iterator vertex_iterator; -typedef boost::graph_traits::vertices_size_type vertices_size_type; -typedef boost::graph_traits::edge_iterator edge_iterator; -typedef boost::graph_traits::edges_size_type edges_size_type; +using vertex_descriptor = boost::graph_traits::vertex_descriptor; +using edge_descriptor = boost::graph_traits::edge_descriptor; +using out_edge_iterator = boost::graph_traits::out_edge_iterator; +using in_edge_iterator = boost::graph_traits::in_edge_iterator; +using adjacency_iterator = boost::graph_traits::adjacency_iterator; +using degree_size_type = boost::graph_traits::degree_size_type; +using vertex_iterator = boost::graph_traits::vertex_iterator; +using vertices_size_type = boost::graph_traits::vertices_size_type; +using edge_iterator = boost::graph_traits::edge_iterator; +using edges_size_type = boost::graph_traits::edges_size_type; // Tag values passed to an iterator constructor to specify whether it should @@ -217,7 +217,7 @@ class ring_incident_edge_iterator:public boost::iterator_adaptor < static const int ring_offset[] = {1, -1}; vertex_descriptor v; - std::size_t p = *this->base_reference(); + auto p = *this->base_reference(); if (m_u == 0 && p == 1) v = m_n-1; // Vertex n-1 precedes vertex 0. else @@ -287,10 +287,10 @@ class ring_adjacency_iterator:public boost::adjacency_iterator_generator< out_edge_iterator>::type { // The parent class is an iterator_adpator that turns an iterator over // out edges into an iterator over adjacent vertices. - typedef boost::adjacency_iterator_generator< + using parent_class = boost::adjacency_iterator_generator< ring_graph, vertex_descriptor, - out_edge_iterator>::type parent_class; + out_edge_iterator>::type; public: ring_adjacency_iterator() {}; ring_adjacency_iterator(vertex_descriptor u, @@ -392,10 +392,10 @@ edge(vertex_descriptor u, vertex_descriptor v, const ring_graph& g) { Map from edges to weight values */ struct edge_weight_map { - typedef double value_type; - typedef value_type reference; - typedef edge_descriptor key_type; - typedef boost::readable_property_map_tag category; + using value_type = double; + using reference = value_type; + using key_type = edge_descriptor; + using category = boost::readable_property_map_tag; // Edges have a weight equal to the average of their endpoint indexes. reference operator[](key_type e) const { @@ -405,13 +405,10 @@ struct edge_weight_map { // Use these propety_map and property_traits parameterizations to refer to // the associated property map types. -typedef boost::property_map::const_type - const_edge_weight_map; -typedef boost::property_traits::reference - edge_weight_map_value_type; -typedef boost::property_traits::key_type - edge_weight_map_key; +using const_edge_weight_map = boost::property_map::const_type; +using edge_weight_map_value_type = boost::property_traits::reference; +using edge_weight_map_key = boost::property_traits::key_type; // PropertyMap valid expressions edge_weight_map_value_type @@ -462,7 +459,7 @@ int main (int argc, char const *argv[]) { // Specify the size of the graph on the command line, or use a default size // of 5. - std::size_t n = argc == 2 ? boost::lexical_cast(argv[1]) : 5; + std::size_t n = argc == 2 ? std::stoul(argv[1]) : 5; // Create a small ring graph. ring_graph g(n); @@ -477,20 +474,16 @@ int main (int argc, char const *argv[]) { // Vertex 4: (4, 0) (4, 3) Adjacent vertices 0 3 // 5 vertices std::cout << "Vertices, outgoing edges, and adjacent vertices" << std::endl; - vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; vi++) { - vertex_descriptor u = *vi; + for (const auto& u : make_range_pair(vertices(g))) { std::cout << "Vertex " << u << ": "; // Adjacenct edges - out_edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ei++) - std::cout << *ei << " "; + for (const auto& edge : make_range_pair(out_edges(u, g))) + std::cout << edge << " "; std::cout << " Adjacent vertices "; // Adjacent vertices // Here we want our adjacency_iterator and not boost::adjacency_iterator. - ::adjacency_iterator ai, ai_end; - for (boost::tie(ai, ai_end) = adjacent_vertices(u, g); ai != ai_end; ai++) { - std::cout << *ai << " "; + for (const auto& vertex : make_range_pair(adjacent_vertices(u, g))) { + std::cout << vertex << " "; } std::cout << std::endl; } @@ -507,9 +500,7 @@ int main (int argc, char const *argv[]) { // (4, 0) weight 2 // 5 edges std::cout << "Edges and weights" << std::endl; - edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ei++) { - edge_descriptor e = *ei; + for (const auto& e : make_range_pair(edges(g))) { std::cout << e << " weight " << get(edge_weight, g, e) << std::endl; } std::cout << num_edges(g) << " edges" << std::endl; @@ -539,11 +530,10 @@ int main (int argc, char const *argv[]) { distance_map(dist_pm) ); std::cout << "Dijkstra search from vertex " << source << std::endl; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { - vertex_descriptor u = *vi; - std::cout << "Vertex " << u << ": " - << "parent "<< pred[*vi] << ", " - << "distance " << dist[u] + for (const auto& vertex : make_range_pair(vertices(g))) { + std::cout << "Vertex " << vertex << ": " + << "parent "<< pred[vertex] << ", " + << "distance " << dist[vertex] << std::endl; } } diff --git a/example/in_edges.cpp b/example/in_edges.cpp index f729c44d..fd519b71 100644 --- a/example/in_edges.cpp +++ b/example/in_edges.cpp @@ -12,6 +12,7 @@ #include #include +#include "range_pair.hpp" /* Sample Output @@ -27,10 +28,8 @@ int main(int , char* []) { using namespace boost; - using namespace std; - using namespace boost; - typedef adjacency_list Graph; + using Graph = adjacency_list; const int num_vertices = 5; Graph g(num_vertices); @@ -40,14 +39,11 @@ int main(int , char* []) add_edge(2, 4, g); add_edge(3, 4, g); - boost::graph_traits::vertex_iterator i, end; - boost::graph_traits::in_edge_iterator ei, edge_end; - - for(boost::tie(i,end) = vertices(g); i != end; ++i) { - cout << *i << " <-- "; - for (boost::tie(ei,edge_end) = in_edges(*i, g); ei != edge_end; ++ei) - cout << source(*ei, g) << " "; - cout << endl; + for(const auto& vertex : make_range_pair(vertices(g))) { + std::cout << vertex << " <-- "; + for (const auto& edge : make_range_pair(in_edges(vertex, g))) + std::cout << source(edge, g) << " "; + std::cout << std::endl; } return 0; } diff --git a/example/inclusive_mean_geodesic.cpp b/example/inclusive_mean_geodesic.cpp index d825a27d..b239dd9d 100644 --- a/example/inclusive_mean_geodesic.cpp +++ b/example/inclusive_mean_geodesic.cpp @@ -13,8 +13,8 @@ #include #include #include "helper.hpp" +#include "range_pair.hpp" -using namespace std; using namespace boost; // This template structure defines the function that we will apply @@ -23,11 +23,11 @@ using namespace boost; template > + typename Divides = std::divides> struct inclusive_average { - typedef DistanceType distance_type; - typedef ResultType result_type; + using distance_type = DistanceType; + using result_type = ResultType; result_type operator ()(distance_type d, const Graph& g) { @@ -45,7 +45,7 @@ struct inclusive_average // represents web pages that can be navigated to. struct WebPage { - string name; + std::string name; }; // The Link type stores an associated probability of traveling @@ -56,30 +56,30 @@ struct Link }; // Declare the graph type and its vertex and edge types. -typedef directed_graph Graph; -typedef graph_traits::vertex_descriptor Vertex; -typedef graph_traits::edge_descriptor Edge; +using Graph = directed_graph; +using Vertex = graph_traits::vertex_descriptor; +using Edge = graph_traits::edge_descriptor; // The name map provides an abstract accessor for the names of // each vertex. This is used during graph creation. -typedef property_map::type NameMap; +using NameMap = property_map::type; // Declare a matrix type and its corresponding property map that // will contain the distances between each pair of vertices. -typedef exterior_vertex_property DistanceProperty; -typedef DistanceProperty::matrix_type DistanceMatrix; -typedef DistanceProperty::matrix_map_type DistanceMatrixMap; +using DistanceProperty = exterior_vertex_property; +using DistanceMatrix = DistanceProperty::matrix_type; +using DistanceMatrixMap = DistanceProperty::matrix_map_type; // Declare the weight map as an accessor into the bundled // edge property. -typedef property_map::type WeightMap; +using WeightMap = property_map::type; // Declare a container and its corresponding property map that // will contain the resulting mean geodesic distances of each // vertex in the graph. -typedef exterior_vertex_property GeodesicProperty; -typedef GeodesicProperty::container_type GeodesicContainer; -typedef GeodesicProperty::map_type GeodesicMap; +using GeodesicProperty = exterior_vertex_property; +using GeodesicContainer = GeodesicProperty::container_type; +using GeodesicMap = GeodesicProperty::map_type; static float exclusive_geodesics(const Graph&, DistanceMatrixMap, GeodesicMap); static float inclusive_geodesics(const Graph&, DistanceMatrixMap, GeodesicMap); @@ -95,7 +95,7 @@ main(int argc, char *argv[]) WeightMap wm(get(&Link::probability, g)); // Read the weighted graph from standard input. - read_weighted_graph(g, nm, wm, cin); + read_weighted_graph(g, nm, wm, std::cin); // Compute the distances between all pairs of vertices using // the Floyd-Warshall algorithm. The weight map was created @@ -117,18 +117,17 @@ main(int argc, char *argv[]) // Print the mean geodesic distance of each vertex and finally, // the graph itself. - cout << setw(12) << setiosflags(ios::left) << "vertex"; - cout << setw(12) << setiosflags(ios::left) << "excluding"; - cout << setw(12) << setiosflags(ios::left) << "including" << endl; - graph_traits::vertex_iterator i, end; - for(boost::tie(i, end) = vertices(g); i != end; ++i) { - cout << setw(12) << setiosflags(ios::left) - << g[*i].name - << setw(12) << get(exmap, *i) - << setw(12) << get(inmap, *i) << endl; + std::cout << std::setw(12) << std::setiosflags(std::ios::left) << "vertex"; + std::cout << std::setw(12) << std::setiosflags(std::ios::left) << "excluding"; + std::cout << std::setw(12) << std::setiosflags(std::ios::left) << "including" << std::endl; + for(const auto& vertex : make_range_pair(vertices(g))) { + std::cout << std::setw(12) << std::setiosflags(std::ios::left) + << g[vertex].name + << std::setw(12) << get(exmap, vertex) + << std::setw(12) << get(inmap, vertex) << std::endl; } - cout << "small world (excluding self-loops): " << ex << endl; - cout << "small world (including self-loops): " << in << endl; + std::cout << "small world (excluding self-loops): " << ex << std::endl; + std::cout << "small world (including self-loops): " << in << std::endl; return 0; } diff --git a/example/incremental-components-eg.cpp b/example/incremental-components-eg.cpp index 7faf4418..19192f35 100644 --- a/example/incremental-components-eg.cpp +++ b/example/incremental-components-eg.cpp @@ -11,19 +11,19 @@ #include #include -#include #include #include #include +#include "range_pair.hpp" using namespace boost; int main(int argc, char* argv[]) { - typedef adjacency_list Graph; - typedef graph_traits::vertex_descriptor Vertex; - //typedef graph_traits::edge_descriptor Edge; - typedef graph_traits::vertices_size_type VertexIndex; + using Graph = adjacency_list ; + using Vertex = graph_traits::vertex_descriptor; + //using Edge = graph_traits::edge_descriptor; + using VertexIndex = graph_traits::vertices_size_type; // Create a graph const int VERTEX_COUNT = 6; @@ -37,8 +37,8 @@ int main(int argc, char* argv[]) std::vector rank(num_vertices(graph)); std::vector parent(num_vertices(graph)); - typedef VertexIndex* Rank; - typedef Vertex* Parent; + using Rank = VertexIndex*; + using Parent = Vertex*; disjoint_sets ds(&rank[0], &parent[0]); // Determine the connected components, storing the results in the @@ -53,7 +53,7 @@ int main(int argc, char* argv[]) ds.union_set(4, 0); ds.union_set(2, 5); - BOOST_FOREACH(Vertex current_vertex, vertices(graph)) { + for (auto current_vertex : make_range_pair(vertices(graph))) { std::cout << "representative[" << current_vertex << "] = " << ds.find_set(current_vertex) << std::endl; } @@ -64,16 +64,15 @@ int main(int argc, char* argv[]) // index map into the component_index constructor if our graph type // used listS instead of vecS (identity_property_map is used by // default). - typedef component_index Components; + using Components = component_index; Components components(parent.begin(), parent.end()); // Iterate through the component indices - BOOST_FOREACH(VertexIndex component_index, components) { + for (auto component_index : components) { std::cout << "component " << component_index << " contains: "; // Iterate through the child vertex indices for [component_index] - BOOST_FOREACH(VertexIndex child_index, - components[component_index]) { + for (auto child_index : make_range_pair(components[component_index])) { std::cout << child_index << " "; } diff --git a/example/incremental_components.cpp b/example/incremental_components.cpp index f8f156ef..89e145ef 100644 --- a/example/incremental_components.cpp +++ b/example/incremental_components.cpp @@ -10,11 +10,11 @@ #include #include -#include #include #include #include #include +#include "range_pair.hpp" /* @@ -49,9 +49,9 @@ using namespace boost; int main(int argc, char* argv[]) { - typedef adjacency_list Graph; - typedef graph_traits::vertex_descriptor Vertex; - typedef graph_traits::vertices_size_type VertexIndex; + using Graph = adjacency_list ; + using Vertex = graph_traits::vertex_descriptor; + using VertexIndex = graph_traits::vertices_size_type; const int VERTEX_COUNT = 6; Graph graph(VERTEX_COUNT); @@ -59,41 +59,38 @@ int main(int argc, char* argv[]) std::vector rank(num_vertices(graph)); std::vector parent(num_vertices(graph)); - typedef VertexIndex* Rank; - typedef Vertex* Parent; + using Rank = VertexIndex*; + using Parent = Vertex*; disjoint_sets ds(&rank[0], &parent[0]); initialize_incremental_components(graph, ds); incremental_components(graph, ds); - graph_traits::edge_descriptor edge; - bool flag; - - boost::tie(edge, flag) = add_edge(0, 1, graph); + auto [edge, flag] = add_edge(0, 1, graph); ds.union_set(0,1); - boost::tie(edge, flag) = add_edge(1, 4, graph); + std::tie(edge, flag) = add_edge(1, 4, graph); ds.union_set(1,4); - boost::tie(edge, flag) = add_edge(4, 0, graph); + std::tie(edge, flag) = add_edge(4, 0, graph); ds.union_set(4,0); - boost::tie(edge, flag) = add_edge(2, 5, graph); + std::tie(edge, flag) = add_edge(2, 5, graph); ds.union_set(2,5); std::cout << "An undirected graph:" << std::endl; print_graph(graph, get(boost::vertex_index, graph)); std::cout << std::endl; - BOOST_FOREACH(Vertex current_vertex, vertices(graph)) { + for ( auto current_vertex : make_range_pair(vertices(graph)) ) { std::cout << "representative[" << current_vertex << "] = " << ds.find_set(current_vertex) << std::endl; } std::cout << std::endl; - typedef component_index Components; + using Components = component_index; // NOTE: Because we're using vecS for the graph type, we're // effectively using identity_property_map for a vertex index map. @@ -102,12 +99,11 @@ int main(int argc, char* argv[]) Components components(parent.begin(), parent.end()); // Iterate through the component indices - BOOST_FOREACH(VertexIndex current_index, components) { + for (auto current_index : components) { std::cout << "component " << current_index << " contains: "; // Iterate through the child vertex indices for [current_index] - BOOST_FOREACH(VertexIndex child_index, - components[current_index]) { + for (auto child_index : make_range_pair(components[current_index])) { std::cout << child_index << " "; } diff --git a/example/influence_prestige.cpp b/example/influence_prestige.cpp index e7d26f13..588ebde9 100644 --- a/example/influence_prestige.cpp +++ b/example/influence_prestige.cpp @@ -13,31 +13,31 @@ #include #include "helper.hpp" +#include "range_pair.hpp" -using namespace std; using namespace boost; // The Actor type stores the name of each vertex in the graph. struct Actor { - string name; + std::string name; }; // Declare the graph type and its vertex and edge types. -typedef directed_graph Graph; -typedef graph_traits::vertex_descriptor Vertex; -typedef graph_traits::edge_descriptor Edge; +using Graph = directed_graph; +using Vertex = graph_traits::vertex_descriptor; +using Edge = graph_traits::edge_descriptor; // The name map provides an abstract accessor for the names of // each vertex. This is used during graph creation. -typedef property_map::type NameMap; +using NameMap = property_map::type; // Declare a container type for influence and prestige (both // of which are degree centralities) and its corresponding // property map. -typedef exterior_vertex_property CentralityProperty; -typedef CentralityProperty::container_type CentralityContainer; -typedef CentralityProperty::map_type CentralityMap; +using CentralityProperty = exterior_vertex_property; +using CentralityContainer = CentralityProperty::container_type; +using CentralityMap = CentralityProperty::map_type; int main(int argc, char *argv[]) @@ -48,7 +48,7 @@ main(int argc, char *argv[]) NameMap nm(get(&Actor::name, g)); // Read the graph from standard input. - read_graph(g, nm, cin); + read_graph(g, nm, std::cin); // Compute the influence for the graph. CentralityContainer influence(num_vertices(g)); @@ -61,13 +61,11 @@ main(int argc, char *argv[]) all_prestige_values(g, pm); // Print the degree centrality of each vertex - graph_traits::vertex_iterator i, end; - for(boost::tie(i, end) = vertices(g); i != end; ++i) { - Vertex v = *i; - cout << setiosflags(ios::left) << setw(12) + for(const auto& v : make_range_pair(vertices(g))) { + std::cout << std::setiosflags(std::ios::left) << std::setw(12) << g[v].name << "\t" << im[v] << "\t" - << pm[v] << endl; + << pm[v] << std::endl; } return 0; diff --git a/example/interior_pmap_bundled.cpp b/example/interior_pmap_bundled.cpp index cee50169..3d5d8086 100644 --- a/example/interior_pmap_bundled.cpp +++ b/example/interior_pmap_bundled.cpp @@ -15,7 +15,6 @@ #include #include -using namespace std; using namespace boost; /* @@ -49,15 +48,15 @@ template void who_owes_who(EdgeIter first, EdgeIter last, const Graph& G) { while (first != last) { - cout << G[source(*first, G)].first_name << " owes " - << G[target(*first, G)].first_name << " some money" << endl; + std::cout << G[source(*first, G)].first_name << " owes " + << G[target(*first, G)].first_name << " some money" << std::endl; ++first; } } struct VertexData { - string first_name; + std::string first_name; }; int @@ -66,16 +65,16 @@ main() { // Create the graph, and specify that we will use std::string to // store the first name's. - typedef adjacency_list MyGraphType; + using MyGraphType = adjacency_list; - typedef pair Pair; - Pair edge_array[11] = { Pair(0,1), Pair(0,2), Pair(0,3), Pair(0,4), + using Pair = std::pair; + Pair edge_array[] = { Pair(0,1), Pair(0,2), Pair(0,3), Pair(0,4), Pair(2,0), Pair(3,0), Pair(2,4), Pair(3,1), Pair(3,4), Pair(4,0), Pair(4,1) }; MyGraphType G(5); - for (int i=0; i<11; ++i) - add_edge(edge_array[i].first, edge_array[i].second, G); + for (const auto& edge : edge_array) + add_edge(edge.first, edge.second, G); G[0].first_name = "Jeremy"; G[1].first_name = "Rich"; @@ -86,7 +85,7 @@ main() who_owes_who(edges(G).first, edges(G).second, G); } - cout << endl; + std::cout << std::endl; return 0; } diff --git a/example/interior_property_map.cpp b/example/interior_property_map.cpp index 7083d663..fdfca046 100644 --- a/example/interior_property_map.cpp +++ b/example/interior_property_map.cpp @@ -14,7 +14,6 @@ #include #include -using namespace std; using namespace boost; /* @@ -55,19 +54,19 @@ template void who_owes_who(EdgeIter first, EdgeIter last, const Graph& G) { // Access the propety acessor type for this graph - typedef typename property_map - ::const_type NamePA; - NamePA name = get(vertex_first_name, G); + using NamePA = typename property_map + ::const_type; + auto name = get(vertex_first_name, G); - typedef typename boost::property_traits::value_type NameType; + using NameType = typename boost::property_traits::value_type; NameType src_name, targ_name; while (first != last) { src_name = boost::get(name, source(*first,G)); targ_name = boost::get(name, target(*first,G)); - cout << src_name << " owes " - << targ_name << " some money" << endl; + std::cout << src_name << " owes " + << targ_name << " some money" << std::endl; ++first; } } @@ -78,20 +77,19 @@ main() { // Create the graph, and specify that we will use std::string to // store the first name's. - typedef adjacency_list > MyGraphType; + using MyGraphType = adjacency_list>; - typedef pair Pair; - Pair edge_array[11] = { Pair(0,1), Pair(0,2), Pair(0,3), Pair(0,4), + using Pair = std::pair; + Pair edge_array[] = { Pair(0,1), Pair(0,2), Pair(0,3), Pair(0,4), Pair(2,0), Pair(3,0), Pair(2,4), Pair(3,1), Pair(3,4), Pair(4,0), Pair(4,1) }; MyGraphType G(5); - for (int i=0; i<11; ++i) - add_edge(edge_array[i].first, edge_array[i].second, G); + for (const auto& edge : edge_array) + add_edge(edge.first, edge.second, G); - property_map::type name - = get(vertex_first_name, G); + auto name = get(vertex_first_name, G); boost::put(name, 0, "Jeremy"); boost::put(name, 1, "Rich"); @@ -102,7 +100,7 @@ main() who_owes_who(edges(G).first, edges(G).second, G); } - cout << endl; + std::cout << std::endl; return 0; } diff --git a/example/isomorphism.cpp b/example/isomorphism.cpp index c11d70fa..9e5ae844 100644 --- a/example/isomorphism.cpp +++ b/example/isomorphism.cpp @@ -9,6 +9,7 @@ #include #include +#include "range_pair.hpp" /* Sample output: @@ -23,26 +24,26 @@ main() const int n = 12; - typedef adjacency_list > graph_t; + using graph_t = adjacency_list>; graph_t g1(n), g2(n); std::vector::vertex_descriptor> v1(n), v2(n); - property_map::type - v1_index_map = get(vertex_index, g1), + auto v1_index_map = get(vertex_index, g1), v2_index_map = get(vertex_index, g2); - graph_traits::vertex_iterator i, end; int id = 0; - for (boost::tie(i, end) = vertices(g1); i != end; ++i, ++id) { - put(v1_index_map, *i, id); - v1[id] = *i; + for (const auto& vertex : make_range_pair(vertices(g1))) { + put(v1_index_map, vertex, id); + v1[id] = vertex; + ++id; } id = 0; - for (boost::tie(i, end) = vertices(g2); i != end; ++i, ++id) { - put(v2_index_map, *i, id); - v2[id] = *i; + for (const auto& vertex : make_range_pair(vertices(g2))) { + put(v2_index_map, vertex, id); + v2[id] = vertex; + ++id; } add_edge(v1[0], v1[1], g1); add_edge(v1[1], v1[2], g1); add_edge(v1[0], v1[2], g1); diff --git a/example/iteration_macros.cpp b/example/iteration_macros.cpp index 27451f96..563327e8 100644 --- a/example/iteration_macros.cpp +++ b/example/iteration_macros.cpp @@ -29,10 +29,7 @@ int main() add_edge(Rick, Margaret, g); add_edge(John, Benjamin, g); - graph_traits >::vertex_iterator i, end; - graph_traits >::adjacency_iterator ai, a_end; - property_map, vertex_index_t>::type - index_map = get(vertex_index, g); + auto index_map = get(vertex_index, g); BGL_FORALL_VERTICES(i, g, adjacency_list<>) { std::cout << name[get(index_map, i)]; diff --git a/example/iterator-property-map-eg.cpp b/example/iterator-property-map-eg.cpp index 9257af4e..3113429e 100644 --- a/example/iterator-property-map-eg.cpp +++ b/example/iterator-property-map-eg.cpp @@ -13,7 +13,7 @@ main() { using namespace boost; double x[] = { 0.2, 4.5, 3.2 }; - iterator_property_map < double *, identity_property_map, double, double& > pmap(x); + iterator_property_map pmap(x); std::cout << "x[1] = " << get(pmap, 1) << std::endl; put(pmap, 0, 1.7); std::cout << "x[0] = " << pmap[0] << std::endl; diff --git a/example/johnson-eg.cpp b/example/johnson-eg.cpp index 557b1733..7f4a12c2 100644 --- a/example/johnson-eg.cpp +++ b/example/johnson-eg.cpp @@ -14,15 +14,16 @@ #include #include #include +#include "range_pair.hpp" int main() { using namespace boost; - typedef adjacency_list > > Graph; + using Graph = adjacency_list>>; const int V = 6; - typedef std::pair < int, int >Edge; + using Edge = std::pair; Edge edge_array[] = { Edge(0, 1), Edge(0, 2), Edge(0, 3), Edge(0, 4), Edge(0, 5), Edge(1, 2), Edge(1, 5), Edge(1, 3), Edge(2, 4), Edge(2, 5), @@ -38,15 +39,14 @@ main() Graph g(edge_array, edge_array + E, V); #endif - property_map < Graph, edge_weight_t >::type w = get(edge_weight, g); + auto w = get(edge_weight, g); int weights[] = { 0, 0, 0, 0, 0, 3, -4, 8, 1, 7, 4, -5, 2, 6 }; int *wp = weights; - graph_traits < Graph >::edge_iterator e, e_end; - for (boost::tie(e, e_end) = edges(g); e != e_end; ++e) - w[*e] = *wp++; + for (const auto& edge : make_range_pair(edges(g))) + w[edge] = *wp++; - std::vector < int >d(V, (std::numeric_limits < int >::max)()); + std::vectord(V, (std::numeric_limits < int>::max)()); int D[V][V]; johnson_all_pairs_shortest_paths(g, D, distance_map(&d[0])); @@ -72,10 +72,9 @@ main() << "ratio=\"fill\"\n" << "edge[style=\"bold\"]\n" << "node[shape=\"circle\"]\n"; - graph_traits < Graph >::edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) - fout << source(*ei, g) << " -> " << target(*ei, g) - << "[label=" << get(edge_weight, g)[*ei] << "]\n"; + for (const auto& edge : make_range_pair(edges(g))) + fout << source(edge, g) << " -> " << target(edge, g) + << "[label=" << get(edge_weight, g)[edge] << "]\n"; fout << "}\n"; return 0; diff --git a/example/kevin-bacon.cpp b/example/kevin-bacon.cpp index 5b1dd8b5..e1385b04 100644 --- a/example/kevin-bacon.cpp +++ b/example/kevin-bacon.cpp @@ -14,6 +14,7 @@ #include #include #include +#include "range_pair.hpp" #include using namespace boost; @@ -36,11 +37,11 @@ class bacon_number_recorder : public default_bfs_visitor }; // Convenience function -template < typename DistanceMap > +template bacon_number_recorder record_bacon_number(DistanceMap d) { - return bacon_number_recorder < DistanceMap > (d); + return bacon_number_recorder (d); } @@ -53,28 +54,25 @@ main() return EXIT_FAILURE; } - typedef adjacency_list < vecS, vecS, undirectedS, property < vertex_name_t, - std::string >, property < edge_name_t, std::string > > Graph; + using Graph = adjacency_list , + property>; Graph g; - typedef property_map < Graph, vertex_name_t >::type actor_name_map_t; - actor_name_map_t actor_name = get(vertex_name, g); - typedef property_map < Graph, edge_name_t >::type movie_name_map_t; - movie_name_map_t connecting_movie = get(edge_name, g); + auto actor_name = get(vertex_name, g); + auto connecting_movie = get(edge_name, g); - typedef graph_traits < Graph >::vertex_descriptor Vertex; - typedef std::map < std::string, Vertex > NameVertexMap; + using Vertex = graph_traits::vertex_descriptor; + using NameVertexMap = std::map; NameVertexMap actors; for (std::string line; std::getline(datafile, line);) { - char_delimiters_separator < char >sep(false, "", ";"); + char_delimiters_separator sep(false, "", ";"); tokenizer <> line_toks(line, sep); - tokenizer <>::iterator i = line_toks.begin(); - std::string actors_name = *i++; - NameVertexMap::iterator pos; - bool inserted; + auto i = line_toks.begin(); + auto actors_name = *i++; Vertex u, v; - boost::tie(pos, inserted) = actors.insert(std::make_pair(actors_name, Vertex())); + auto [pos, inserted] = actors.insert(std::make_pair(actors_name, Vertex())); if (inserted) { u = add_vertex(g); actor_name[u] = actors_name; @@ -82,9 +80,9 @@ main() } else u = pos->second; - std::string movie_name = *i++; + auto movie_name = *i++; - boost::tie(pos, inserted) = actors.insert(std::make_pair(*i, Vertex())); + std::tie(pos, inserted) = actors.insert(std::make_pair(*i, Vertex())); if (inserted) { v = add_vertex(g); actor_name[v] = *i; @@ -92,14 +90,13 @@ main() } else v = pos->second; - graph_traits < Graph >::edge_descriptor e; - boost::tie(e, inserted) = add_edge(u, v, g); - if (inserted) + auto [e, insertede] = add_edge(u, v, g); + if (insertede) connecting_movie[e] = movie_name; } - std::vector < int >bacon_number(num_vertices(g)); + std::vectorbacon_number(num_vertices(g)); Vertex src = actors["Kevin Bacon"]; bacon_number[src] = 0; @@ -107,10 +104,9 @@ main() breadth_first_search(g, src, visitor(record_bacon_number(&bacon_number[0]))); - graph_traits < Graph >::vertex_iterator i, end; - for (boost::tie(i, end) = vertices(g); i != end; ++i) { - std::cout << actor_name[*i] << " has a Bacon number of " - << bacon_number[*i] << std::endl; + for (const auto& vertex : make_range_pair(vertices(g))) { + std::cout << actor_name[vertex] << " has a Bacon number of " + << bacon_number[vertex] << std::endl; } return 0; diff --git a/example/kevin-bacon2.cpp b/example/kevin-bacon2.cpp index 2cd17838..62c0aa83 100644 --- a/example/kevin-bacon2.cpp +++ b/example/kevin-bacon2.cpp @@ -17,6 +17,7 @@ #include #include #include +#include "range_pair.hpp" struct vertex_properties { std::string name; @@ -38,10 +39,10 @@ struct edge_properties { using namespace boost; -typedef adjacency_list Graph; -typedef graph_traits::vertex_descriptor Vertex; -typedef graph_traits::edge_descriptor Edge; +using Graph = adjacency_list; +using Vertex = graph_traits::vertex_descriptor; +using Edge = graph_traits::edge_descriptor; class bacon_number_recorder : public default_bfs_visitor { @@ -49,7 +50,7 @@ class bacon_number_recorder : public default_bfs_visitor bacon_number_recorder(int* dist) : d(dist) { } void tree_edge(Edge e, const Graph& g) const { - Vertex u = source(e, g), v = target(e, g); + auto u = source(e, g), v = target(e, g); d[v] = d[u] + 1; } private: @@ -71,10 +72,9 @@ int main() // Get the vertex for Kevin Bacon Vertex src; - graph_traits::vertex_iterator i, end; - for (boost::tie(i, end) = vertices(g); i != end; ++i) - if (g[*i].name == "Kevin Bacon") - src = *i; + for (const auto& vertex : make_range_pair(vertices(g))) + if (g[vertex].name == "Kevin Bacon") + src = vertex; // Set Kevin's number to zero bacon_number[src] = 0; @@ -83,9 +83,9 @@ int main() breadth_first_search(g, src, visitor(bacon_number_recorder(&bacon_number[0]))); - for (boost::tie(i, end) = vertices(g); i != end; ++i) - std::cout << g[*i].name << " has a Bacon number of " - << bacon_number[*i] << std::endl; + for (const auto& vertex : make_range_pair(vertices(g))) + std::cout << g[vertex].name << " has a Bacon number of " + << bacon_number[vertex] << std::endl; return 0; } diff --git a/example/king_ordering.cpp b/example/king_ordering.cpp index 2b081fa2..28ce5841 100644 --- a/example/king_ordering.cpp +++ b/example/king_ordering.cpp @@ -15,6 +15,7 @@ #include #include #include +#include "range_pair.hpp" /* Sample Output @@ -32,15 +33,14 @@ int main(int , char* []) { using namespace boost; - using namespace std; - typedef adjacency_list > > Graph; - typedef graph_traits::vertex_descriptor Vertex; - typedef graph_traits::vertices_size_type size_type; + property>>; + using Vertex = graph_traits::vertex_descriptor; + using size_type = graph_traits::vertices_size_type; - typedef std::pair Pair; - Pair edges[14] = { Pair(0,3), //a-d + using Pair = std::pair; + Pair edges[] = { Pair(0,3), //a-d Pair(0,5), //a-f Pair(1,2), //b-c Pair(1,4), //b-e @@ -56,33 +56,29 @@ int main(int , char* []) Pair(6,7) }; //g-h Graph G(10); - for (int i = 0; i < 14; ++i) - add_edge(edges[i].first, edges[i].second, G); + for (const auto& edge : edges) + add_edge(edge.first, edge.second, G); - graph_traits::vertex_iterator ui, ui_end; + auto deg = get(vertex_degree, G); + for (const auto& vertex : make_range_pair(vertices(G))) + deg[vertex] = degree(vertex, G); - property_map::type deg = get(vertex_degree, G); - for (boost::tie(ui, ui_end) = vertices(G); ui != ui_end; ++ui) - deg[*ui] = degree(*ui, G); - - property_map::type - index_map = get(vertex_index, G); + auto index_map = get(vertex_index, G); std::cout << "original bandwidth: " << bandwidth(G) << std::endl; std::vector inv_perm(num_vertices(G)); std::vector perm(num_vertices(G)); { - Vertex s = vertex(6, G); + auto s = vertex(6, G); //king_ordering king_ordering(G, s, inv_perm.rbegin(), get(vertex_color, G), get(vertex_degree, G), get(vertex_index, G)); - cout << "King ordering starting at: " << s << endl; - cout << " "; - for (std::vector::const_iterator i = inv_perm.begin(); - i != inv_perm.end(); ++i) - cout << index_map[*i] << " "; - cout << endl; + std::cout << "King ordering starting at: " << s << std::endl; + std::cout << " "; + for (const auto& vertex : inv_perm) + std::cout << index_map[vertex] << " "; + std::cout << std::endl; for (size_type c = 0; c != inv_perm.size(); ++c) perm[index_map[inv_perm[c]]] = c; @@ -91,16 +87,15 @@ int main(int , char* []) << std::endl; } { - Vertex s = vertex(0, G); + auto s = vertex(0, G); //king_ordering king_ordering(G, s, inv_perm.rbegin(), get(vertex_color, G), get(vertex_degree, G), get(vertex_index, G)); - cout << "King ordering starting at: " << s << endl; - cout << " "; - for (std::vector::const_iterator i=inv_perm.begin(); - i != inv_perm.end(); ++i) - cout << index_map[*i] << " "; - cout << endl; + std::cout << "King ordering starting at: " << s << std::endl; + std::cout << " "; + for (const auto& vertex : inv_perm) + std::cout << index_map[vertex] << " "; + std::cout << std::endl; for (size_type c = 0; c != inv_perm.size(); ++c) perm[index_map[inv_perm[c]]] = c; @@ -114,12 +109,11 @@ int main(int , char* []) king_ordering(G, inv_perm.rbegin(), get(vertex_color, G), make_degree_map(G), get(vertex_index, G)); - cout << "King ordering:" << endl; - cout << " "; - for (std::vector::const_iterator i=inv_perm.begin(); - i != inv_perm.end(); ++i) - cout << index_map[*i] << " "; - cout << endl; + std::cout << "King ordering:" << std::endl; + std::cout << " "; + for (const auto& vertex : inv_perm) + std::cout << index_map[vertex] << " "; + std::cout << std::endl; for (size_type c = 0; c != inv_perm.size(); ++c) perm[index_map[inv_perm[c]]] = c; diff --git a/example/knights_tour.cpp b/example/knights_tour.cpp index 048cbde4..efbc35f4 100644 --- a/example/knights_tour.cpp +++ b/example/knights_tour.cpp @@ -6,7 +6,7 @@ // http://www.boost.org/LICENSE_1_0.txt) //======================================================================= #include -#include +#include #include #include #include @@ -14,14 +14,13 @@ #include #include #include +#include "range_pair.hpp" using namespace boost; -typedef -std::pair < int, int > - Position; +using Position = std::pair; Position - knight_jumps[8] = { + knight_jumps[] = { Position(2, -1), Position(1, -2), Position(-1, -2), @@ -92,35 +91,22 @@ struct knight_adjacency_iterator: struct knights_tour_graph { - typedef Position - vertex_descriptor; - typedef + using vertex_descriptor = Position; + using edge_descriptor = std::pair < vertex_descriptor, - vertex_descriptor > - edge_descriptor; - typedef knight_adjacency_iterator - adjacency_iterator; - typedef void - out_edge_iterator; - typedef void - in_edge_iterator; - typedef void - edge_iterator; - typedef void - vertex_iterator; - typedef int - degree_size_type; - typedef int - vertices_size_type; - typedef int - edges_size_type; - typedef directed_tag - directed_category; - typedef disallow_parallel_edge_tag - edge_parallel_category; - typedef adjacency_graph_tag - traversal_category; + vertex_descriptor >; + using adjacency_iterator = knight_adjacency_iterator; + using out_edge_iterator = void; + using in_edge_iterator = void; + using edge_iterator = void; + using vertex_iterator = void; + using degree_size_type = int; + using vertices_size_type = int; + using edges_size_type = int; + using directed_category = directed_tag; + using edge_parallel_category = disallow_parallel_edge_tag; + using traversal_category = adjacency_graph_tag; knights_tour_graph(int n): m_board_size(n) { @@ -152,55 +138,52 @@ std::pair < knights_tour_graph::adjacency_iterator, adjacent_vertices(knights_tour_graph::vertex_descriptor v, const knights_tour_graph & g) { - typedef knights_tour_graph::adjacency_iterator Iter; + using Iter = knights_tour_graph::adjacency_iterator; return std::make_pair(Iter(0, v, g), Iter(8, v, g)); } struct compare_first { - template < typename P > bool operator() (const P & x, const P & y) + template bool operator() (const P & x, const P & y) { return x.first < y.first; } }; -template < typename Graph, typename TimePropertyMap > +template bool backtracking_search(Graph & g, typename graph_traits < Graph >::vertex_descriptor src, TimePropertyMap time_map) { - typedef typename graph_traits < Graph >::vertex_descriptor Vertex; - typedef std::pair < int, Vertex > P; - std::stack < P > S; - int time_stamp = 0; + using Vertex = typename graph_traits::vertex_descriptor; + using P = std::pair; + std::stack

S; - S.push(std::make_pair(time_stamp, src)); + S.push(std::make_pair(0, src)); while (!S.empty()) { - Vertex x; - boost::tie(time_stamp, x) = S.top(); + auto [time_stamp, x] = S.top(); put(time_map, x, time_stamp); // all vertices have been visited, success! if (time_stamp == num_vertices(g) - 1) return true; bool deadend = true; - typename graph_traits < Graph >::adjacency_iterator i, end; - for (boost::tie(i, end) = adjacent_vertices(x, g); i != end; ++i) - if (get(time_map, *i) == -1) { - S.push(std::make_pair(time_stamp + 1, *i)); + for (const auto& vertex : make_range_pair(adjacent_vertices(x, g))) + if (get(time_map, vertex) == -1) { + S.push(std::make_pair(time_stamp + 1, vertex)); deadend = false; } if (deadend) { put(time_map, x, -1); S.pop(); - boost::tie(time_stamp, x) = S.top(); + auto [time_stamp, x] = S.top(); while (get(time_map, x) != -1) { // unwind stack to last unexplored vertex put(time_map, x, -1); S.pop(); - boost::tie(time_stamp, x) = S.top(); + std::tie(time_stamp, x) = S.top(); } } @@ -208,59 +191,55 @@ template < typename Graph, typename TimePropertyMap > return false; } -template < typename Vertex, typename Graph, typename TimePropertyMap > int +template int number_of_successors(Vertex x, Graph & g, TimePropertyMap time_map) { int s_x = 0; - typename graph_traits < Graph >::adjacency_iterator i, end; - for (boost::tie(i, end) = adjacent_vertices(x, g); i != end; ++i) - if (get(time_map, *i) == -1) + for (const auto& vertex : make_range_pair(adjacent_vertices(x, g))) + if (get(time_map, vertex) == -1) ++s_x; return s_x; } -template < typename Graph, typename TimePropertyMap > +template bool warnsdorff(Graph & g, - typename graph_traits < Graph >::vertex_descriptor src, + typename graph_traits::vertex_descriptor src, TimePropertyMap time_map) { - typedef typename graph_traits < Graph >::vertex_descriptor Vertex; - typedef std::pair < int, Vertex > P; - std::stack < P > S; - int time_stamp = 0; + using Vertex = typename graph_traits::vertex_descriptor; + using P = std::pair; + std::stack

S; - S.push(std::make_pair(time_stamp, src)); + S.push(std::make_pair(0, src)); while (!S.empty()) { - Vertex x; - boost::tie(time_stamp, x) = S.top(); + auto [time_stamp, x] = S.top(); put(time_map, x, time_stamp); // all vertices have been visited, success! if (time_stamp == num_vertices(g) - 1) return true; // Put adjacent vertices into a local priority queue - std::priority_queue < P, std::vector < P >, compare_first > Q; - typename graph_traits < Graph >::adjacency_iterator i, end; + std::priority_queue, compare_first> Q; int num_succ; - for (boost::tie(i, end) = adjacent_vertices(x, g); i != end; ++i) - if (get(time_map, *i) == -1) { - num_succ = number_of_successors(*i, g, time_map); - Q.push(std::make_pair(num_succ, *i)); + for (const auto& vertex : make_range_pair(adjacent_vertices(x, g))) + if (get(time_map, vertex) == -1) { + num_succ = number_of_successors(vertex, g, time_map); + Q.push(std::make_pair(num_succ, vertex)); } bool deadend = Q.empty(); // move vertices from local priority queue to the stack for (; !Q.empty(); Q.pop()) { - boost::tie(num_succ, x) = Q.top(); + std::tie(num_succ, x) = Q.top(); S.push(std::make_pair(time_stamp + 1, x)); } if (deadend) { put(time_map, x, -1); S.pop(); - boost::tie(time_stamp, x) = S.top(); + std::tie(time_stamp, x) = S.top(); while (get(time_map, x) != -1) { // unwind stack to last unexplored vertex put(time_map, x, -1); S.pop(); - boost::tie(time_stamp, x) = S.top(); + std::tie(time_stamp, x) = S.top(); } } @@ -271,9 +250,9 @@ template < typename Graph, typename TimePropertyMap > struct board_map { - typedef int value_type; - typedef Position key_type; - typedef read_write_property_map_tag category; + using value_type = int; + using key_type = Position; + using category = read_write_property_map_tag; board_map(int *b, int n):m_board(b), m_size(n) { } diff --git a/example/kruskal-example.cpp b/example/kruskal-example.cpp index 64297768..f30e605b 100644 --- a/example/kruskal-example.cpp +++ b/example/kruskal-example.cpp @@ -9,15 +9,16 @@ #include #include #include +#include "range_pair.hpp" int main() { using namespace boost; - typedef adjacency_list < vecS, vecS, undirectedS, - no_property, property < edge_weight_t, int > > Graph; - typedef graph_traits < Graph >::edge_descriptor Edge; - typedef std::pair E; + using Graph = adjacency_list>; + using Edge = graph_traits::edge_descriptor; + using E = std::pair; const int num_nodes = 5; E edge_array[] = { E(0, 2), E(1, 3), E(1, 4), E(2, 1), E(2, 3), @@ -27,25 +28,23 @@ main() std::size_t num_edges = sizeof(edge_array) / sizeof(E); #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 Graph g(num_nodes); - property_map::type weightmap = get(edge_weight, g); + auto = get(edge_weight, g); for (std::size_t j = 0; j < num_edges; ++j) { - Edge e; bool inserted; - boost::tie(e, inserted) = add_edge(edge_array[j].first, edge_array[j].second, g); + auto [e, inserted] = add_edge(edge_array[j].first, edge_array[j].second, g); weightmap[e] = weights[j]; } #else Graph g(edge_array, edge_array + num_edges, weights, num_nodes); #endif - property_map < Graph, edge_weight_t >::type weight = get(edge_weight, g); - std::vector < Edge > spanning_tree; + auto weight = get(edge_weight, g); + std::vector spanning_tree; kruskal_minimum_spanning_tree(g, std::back_inserter(spanning_tree)); std::cout << "Print the edges in the MST:" << std::endl; - for (std::vector < Edge >::iterator ei = spanning_tree.begin(); - ei != spanning_tree.end(); ++ei) { - std::cout << source(*ei, g) << " <--> " << target(*ei, g) - << " with weight of " << weight[*ei] + for (const auto& edge : spanning_tree) { + std::cout << source(edge, g) << " <--> " << target(edge, g) + << " with weight of " << weight[edge] << std::endl; } @@ -55,15 +54,14 @@ main() << " size=\"3,3\"\n" << " ratio=\"filled\"\n" << " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n"; - graph_traits::edge_iterator eiter, eiter_end; - for (boost::tie(eiter, eiter_end) = edges(g); eiter != eiter_end; ++eiter) { - fout << source(*eiter, g) << " -- " << target(*eiter, g); - if (std::find(spanning_tree.begin(), spanning_tree.end(), *eiter) + for (const auto& edge : make_range_pair(edges(g))) { + fout << source(edge, g) << " -- " << target(edge, g); + if (std::find(spanning_tree.begin(), spanning_tree.end(), edge) != spanning_tree.end()) - fout << "[color=\"black\", label=\"" << get(edge_weight, g, *eiter) + fout << "[color=\"black\", label=\"" << get(edge_weight, g, edge) << "\"];\n"; else - fout << "[color=\"gray\", label=\"" << get(edge_weight, g, *eiter) + fout << "[color=\"gray\", label=\"" << get(edge_weight, g, edge) << "\"];\n"; } fout << "}\n"; diff --git a/example/kruskal-telephone.cpp b/example/kruskal-telephone.cpp index 7e53d70a..dde44fa6 100644 --- a/example/kruskal-telephone.cpp +++ b/example/kruskal-telephone.cpp @@ -8,9 +8,9 @@ #include #include #include -#include #include #include +#include "range_pair.hpp" int main() @@ -19,36 +19,33 @@ main() GraphvizGraph g_dot; read_graphviz("figs/telephone-network.dot", g_dot); - typedef adjacency_list < vecS, vecS, undirectedS, no_property, - property < edge_weight_t, int > > Graph; + using Graph = adjacency_list>; Graph g(num_vertices(g_dot)); - property_map < GraphvizGraph, edge_attribute_t >::type - edge_attr_map = get(edge_attribute, g_dot); - graph_traits < GraphvizGraph >::edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = edges(g_dot); ei != ei_end; ++ei) { - int weight = lexical_cast < int >(edge_attr_map[*ei]["label"]); - property < edge_weight_t, int >edge_property(weight); - add_edge(source(*ei, g_dot), target(*ei, g_dot), edge_property, g); + auto edge_attr_map = get(edge_attribute, g_dot); + for (const auto& edge : make_range_pair(edges(g_dot))) { + int weight = std::stoi(edge_attr_map[edge]["label"]); + property edge_property(weight); + add_edge(source(edge, g_dot), target(edge, g_dot), edge_property, g); } - std::vector < graph_traits < Graph >::edge_descriptor > mst; - typedef std::vector < graph_traits < Graph >::edge_descriptor >::size_type size_type; + std::vector::edge_descriptor> mst; + using size_type = std::vector::edge_descriptor>::size_type; kruskal_minimum_spanning_tree(g, std::back_inserter(mst)); - property_map < Graph, edge_weight_t >::type weight = get(edge_weight, g); + auto weight = get(edge_weight, g); int total_weight = 0; - for (size_type e = 0; e < mst.size(); ++e) - total_weight += get(weight, mst[e]); + for (const auto& edge : mst) + total_weight += get(weight, edge); std::cout << "total weight: " << total_weight << std::endl; - typedef graph_traits < Graph >::vertex_descriptor Vertex; - for (size_type i = 0; i < mst.size(); ++i) { - Vertex u = source(mst[i], g), v = target(mst[i], g); + using Vertex = graph_traits::vertex_descriptor; + for (const auto& edge : mst) { + auto u = source(edge, g), v = target(edge, g); edge_attr_map[edge(u, v, g_dot).first]["color"] = "black"; } std::ofstream out("figs/telephone-mst-kruskal.dot"); - graph_property < GraphvizGraph, graph_edge_attribute_t >::type & - graph_edge_attr_map = get_property(g_dot, graph_edge_attribute); + auto graph_edge_attr_map = get_property(g_dot, graph_edge_attribute); graph_edge_attr_map["color"] = "gray"; graph_edge_attr_map["style"] = "bold"; write_graphviz(out, g_dot); diff --git a/example/kuratowski_subgraph.cpp b/example/kuratowski_subgraph.cpp index 8f55a3e6..ebc0fa58 100644 --- a/example/kuratowski_subgraph.cpp +++ b/example/kuratowski_subgraph.cpp @@ -15,6 +15,7 @@ #include #include +#include "range_pair.hpp" using namespace boost; @@ -22,14 +23,10 @@ using namespace boost; int main(int argc, char** argv) { - typedef adjacency_list - < vecS, - vecS, - undirectedS, - property, - property - > - graph; + using graph = adjacency_list, + property>; // Create a K_6 (complete graph on 6 vertices), which // contains both Kuratowski subgraphs as minors. @@ -52,17 +49,15 @@ int main(int argc, char** argv) // Initialize the interior edge index - property_map::type e_index = get(edge_index, g); + auto e_index = get(edge_index, g); graph_traits::edges_size_type edge_count = 0; - graph_traits::edge_iterator ei, ei_end; - for(boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) - put(e_index, *ei, edge_count++); + for(const auto& edge : make_range_pair(edges(g))) + put(e_index, edge, edge_count++); // Test for planarity - we know it is not planar, we just want to // compute the kuratowski subgraph as a side-effect - typedef std::vector< graph_traits::edge_descriptor > - kuratowski_edges_t; + using kuratowski_edges_t = std::vector::edge_descriptor>; kuratowski_edges_t kuratowski_edges; if (boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g, boyer_myrvold_params::kuratowski_subgraph = @@ -75,11 +70,9 @@ int main(int argc, char** argv) std::cout << "Input graph is not planar" << std::endl; std::cout << "Edges in the Kuratowski subgraph: "; - kuratowski_edges_t::iterator ki, ki_end; - ki_end = kuratowski_edges.end(); - for(ki = kuratowski_edges.begin(); ki != ki_end; ++ki) + for(const auto& k : kuratowski_edges) { - std::cout << *ki << " "; + std::cout << k << " "; } std::cout << std::endl; diff --git a/example/labeled_graph.cpp b/example/labeled_graph.cpp index fdcdd4b6..4b04e804 100644 --- a/example/labeled_graph.cpp +++ b/example/labeled_graph.cpp @@ -11,16 +11,15 @@ #include using namespace boost; -using namespace std; int main() { using namespace boost::graph_detail; - typedef directed_graph<> Digraph; + using Digraph = directed_graph<>; { - typedef labeled_graph Graph; + using Graph = labeled_graph; Graph g; add_vertex(1, g); add_vertex(2, g); @@ -29,14 +28,14 @@ int main() { } { - typedef labeled_graph Graph; + using Graph = labeled_graph; Graph g; add_vertex("foo", g); add_vertex("bar", g); } { - typedef labeled_graph Graph; + using Graph = labeled_graph; Graph g; add_vertex("foo", g); add_vertex("bar", g); @@ -44,7 +43,7 @@ int main() { } { - typedef labeled_graph TempGraph; + using TempGraph = labeled_graph; Digraph g; TempGraph h(&g); add_vertex(12, h); @@ -53,8 +52,8 @@ int main() { { // This is actually a fairly complicated specialization. - typedef adjacency_list G; - typedef labeled_graph Graph; + using G = adjacency_list; + using Graph = labeled_graph; Graph g; add_vertex(0, g); add_vertex(1, g); diff --git a/example/last-mod-time.cpp b/example/last-mod-time.cpp index 4788dd72..cbe09801 100644 --- a/example/last-mod-time.cpp +++ b/example/last-mod-time.cpp @@ -12,21 +12,21 @@ #include #include #include +#include "range_pair.hpp" using namespace boost; -template < typename Graph, typename VertexNamePropertyMap > void +template void read_graph_file(std::istream & graph_in, std::istream & name_in, Graph & g, VertexNamePropertyMap name_map) { - typedef typename graph_traits < Graph >::vertices_size_type size_type; + using size_type = typename graph_traits::vertices_size_type; size_type n_vertices; - typename graph_traits < Graph >::vertex_descriptor u; - typename property_traits < VertexNamePropertyMap >::value_type name; + typename property_traits::value_type name; graph_in >> n_vertices; // read in number of vertices for (size_type i = 0; i < n_vertices; ++i) { // Add n vertices to the graph - u = add_vertex(g); + auto u = add_vertex(g); name_in >> name; put(name_map, u, name); // ** Attach name property to vertex u ** } @@ -42,11 +42,11 @@ read_graph_file(std::istream & graph_in, std::istream & name_in, int main() { - typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list + using graph_type = adjacency_list < listS, // Store out-edges of each vertex in a std::list vecS, // Store vertex set in a std::vector directedS, // The graph is directed - property < vertex_name_t, std::string > // Add a vertex property - >graph_type; + property // Add a vertex property + >; graph_type g; // use default constructor to create empty graph std::ifstream file_in("makefile-dependencies.dat"), @@ -57,33 +57,28 @@ main() exit(-1); } // Obtain internal property map from the graph - property_map < graph_type, vertex_name_t >::type name_map = - get(vertex_name, g); + auto name_map = get(vertex_name, g); read_graph_file(file_in, name_in, g, name_map); // Create storage for last modified times - std::vector < time_t > last_mod_vec(num_vertices(g)); + std::vector last_mod_vec(num_vertices(g)); // Create nickname for the property map type - typedef iterator_property_map < std::vector < time_t >::iterator, - property_map < graph_type, vertex_index_t >::type, time_t, time_t&> iter_map_t; + using iter_map_t = iterator_property_map < std::vector::iterator, + property_map::type, time_t, time_t&>; // Create last modified time property map iter_map_t mod_time_map(last_mod_vec.begin(), get(vertex_index, g)); - property_map < graph_type, vertex_name_t >::type name = get(vertex_name, g); + auto name = get(vertex_name, g); struct stat stat_buf; - graph_traits < graph_type >::vertex_descriptor u; - typedef graph_traits < graph_type >::vertex_iterator vertex_iter_t; - std::pair < vertex_iter_t, vertex_iter_t > p; - for (p = vertices(g); p.first != p.second; ++p.first) { - u = *p.first; + for (const auto& u : make_range_pair(vertices(g))) { if (stat(name[u].c_str(), &stat_buf) != 0) std::cerr << "error in stat() for file " << name[u] << std::endl; put(mod_time_map, u, stat_buf.st_mtime); } - for (p = vertices(g); p.first != p.second; ++p.first) { - std::cout << name[*p.first] << " was last modified at " - << ctime(&mod_time_map[*p.first]); + for (const auto& vertex : make_range_pair(vertices(g))) { + std::cout << name[vertex] << " was last modified at " + << ctime(&mod_time_map[vertex]); } assert(num_vertices(g) == 15); assert(num_edges(g) == 19); diff --git a/example/leda-concept-check.cpp b/example/leda-concept-check.cpp index f5720e7d..a0d9f96a 100644 --- a/example/leda-concept-check.cpp +++ b/example/leda-concept-check.cpp @@ -13,10 +13,10 @@ int main() { using namespace boost; - typedef leda::GRAPH Graph; + using Graph = leda::GRAPH; BOOST_CONCEPT_ASSERT(( VertexListGraphConcept )); - BOOST_CONCEPT_ASSERT(( BidirectionalGraphConcept< Graph> )); - BOOST_CONCEPT_ASSERT(( VertexMutableGraphConcept< Graph> )); + BOOST_CONCEPT_ASSERT(( BidirectionalGraphConcept )); + BOOST_CONCEPT_ASSERT(( VertexMutableGraphConcept )); BOOST_CONCEPT_ASSERT(( EdgeMutableGraphConcept )); return EXIT_SUCCESS; } diff --git a/example/leda-graph-eg.cpp b/example/leda-graph-eg.cpp index 4fddab84..78ff9cbf 100644 --- a/example/leda-graph-eg.cpp +++ b/example/leda-graph-eg.cpp @@ -12,17 +12,16 @@ int main() { using namespace boost; - typedef leda::GRAPH < std::string, int >graph_t; + using graph_t = leda::GRAPH; graph_t g; g.new_node("Philoctetes"); g.new_node("Heracles"); g.new_node("Alcmena"); g.new_node("Eurystheus"); g.new_node("Amphitryon"); - typedef property_map < graph_t, vertex_all_t >::type NodeMap; - NodeMap node_name_map = get(vertex_all, g); - graph_traits < graph_t >::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) - std::cout << node_name_map[*vi] << std::endl; + using NodeMap = property_map::type; + auto node_name_map = get(vertex_all, g); + for (const auto& vertex : make_range_pair(vertices(g))) + std::cout << node_name_map[vertex] << std::endl; return EXIT_SUCCESS; } diff --git a/example/loops_dfs.cpp b/example/loops_dfs.cpp index 4bc66004..c97be174 100644 --- a/example/loops_dfs.cpp +++ b/example/loops_dfs.cpp @@ -11,22 +11,22 @@ #include #include #include -#include #include #include #include #include #include +#include "range_pair.hpp" using namespace boost; -template < typename OutputIterator > +template class back_edge_recorder : public default_dfs_visitor { public: back_edge_recorder(OutputIterator out):m_out(out) { } - template < typename Edge, typename Graph > + template void back_edge(Edge e, const Graph &) { *m_out++ = e; @@ -36,65 +36,63 @@ class back_edge_recorder : public default_dfs_visitor }; // object generator function -template < typename OutputIterator > -back_edge_recorder < OutputIterator > +template +back_edge_recorder make_back_edge_recorder(OutputIterator out) { - return back_edge_recorder < OutputIterator > (out); + return back_edge_recorder (out); } -template < typename Graph, typename Loops > void -find_loops(typename graph_traits < Graph >::vertex_descriptor entry, +template void +find_loops(typename graph_traits::vertex_descriptor entry, const Graph & g, Loops & loops) // A container of sets of vertices { BOOST_CONCEPT_ASSERT(( BidirectionalGraphConcept )); - typedef typename graph_traits < Graph >::edge_descriptor Edge; - std::vector < Edge > back_edges; - std::vector < default_color_type > color_map(num_vertices(g)); + using Edge = typename graph_traits::edge_descriptor; + std::vector back_edges; + std::vector color_map(num_vertices(g)); depth_first_visit(g, entry, make_back_edge_recorder(std::back_inserter(back_edges)), make_iterator_property_map(color_map.begin(), get(vertex_index, g), color_map[0])); - for (typename std::vector < Edge >::size_type i = 0; i < back_edges.size(); ++i) { + for (typename std::vector::size_type i = 0; i < back_edges.size(); ++i) { typename Loops::value_type x; - loops.push_back(x); + loops.emplace_back(x); compute_loop_extent(back_edges[i], g, loops.back()); } } -template < typename Graph, typename Set > void +template void compute_loop_extent(typename graph_traits < Graph >::edge_descriptor back_edge, const Graph & g, Set & loop_set) { BOOST_CONCEPT_ASSERT(( BidirectionalGraphConcept )); - typedef typename graph_traits < Graph >::vertex_descriptor Vertex; - typedef color_traits < default_color_type > Color; + using Vertex = typename graph_traits::vertex_descriptor; + using Color = color_traits; - Vertex loop_head, loop_tail; - loop_tail = source(back_edge, g); - loop_head = target(back_edge, g); + auto loop_tail = source(back_edge, g); + auto loop_head = target(back_edge, g); - std::vector < default_color_type > + std::vector reachable_from_head(num_vertices(g), Color::white()); default_color_type c; depth_first_visit(g, loop_head, default_dfs_visitor(), make_iterator_property_map(reachable_from_head.begin(), get(vertex_index, g), c)); - std::vector < default_color_type > reachable_to_tail(num_vertices(g)); - reverse_graph < Graph > reverse_g(g); + std::vector reachable_to_tail(num_vertices(g)); + reverse_graph reverse_g(g); depth_first_visit(reverse_g, loop_tail, default_dfs_visitor(), make_iterator_property_map(reachable_to_tail.begin(), get(vertex_index, g), c)); - typename graph_traits < Graph >::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) - if (reachable_from_head[*vi] != Color::white() - && reachable_to_tail[*vi] != Color::white()) - loop_set.insert(*vi); + for (const auto& vertex : make_range_pair(vertices(g))) + if (reachable_from_head[vertex] != Color::white() + && reachable_to_tail[vertex] != Color::white()) + loop_set.insert(vertex); } @@ -108,10 +106,10 @@ main(int argc, char *argv[]) GraphvizDigraph g_in; read_graphviz(argv[1], g_in); - typedef adjacency_list < vecS, vecS, bidirectionalS, + using Graph = adjacency_list < vecS, vecS, bidirectionalS, GraphvizVertexProperty, - GraphvizEdgeProperty, GraphvizGraphProperty > Graph; - typedef graph_traits < Graph >::vertex_descriptor Vertex; + GraphvizEdgeProperty, GraphvizGraphProperty >; + using Vertex = graph_traits::vertex_descriptor; Graph g; #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 @@ -121,26 +119,25 @@ main(int argc, char *argv[]) copy_graph(g_in, g); - typedef std::set < Vertex > set_t; - typedef std::list < set_t > list_of_sets_t; + using set_t = std::set; + using list_of_sets_t = std::list; list_of_sets_t loops; - Vertex entry = *vertices(g).first; + auto entry = *vertices(g).first; find_loops(entry, g, loops); - property_map::type vattr_map = get(vertex_attribute, g); - property_map::type eattr_map = get(edge_attribute, g); - graph_traits < Graph >::edge_iterator ei, ei_end; + auto vattr_map = get(vertex_attribute, g); + auto eattr_map = get(edge_attribute, g); - for (list_of_sets_t::iterator i = loops.begin(); i != loops.end(); ++i) { - std::vector < bool > in_loop(num_vertices(g), false); - for (set_t::iterator j = (*i).begin(); j != (*i).end(); ++j) { - vattr_map[*j]["color"] = "gray"; - in_loop[*j] = true; + for (const auto& loop : loops) { + std::vector in_loop(num_vertices(g), false); + for (const auto& vertex : loop) { + vattr_map[vertex]["color"] = "gray"; + in_loop[vertex] = true; } - for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) - if (in_loop[source(*ei, g)] && in_loop[target(*ei, g)]) - eattr_map[*ei]["color"] = "gray"; + for (const auto& edge : make_range_pair(edges(g))) + if (in_loop[source(edge, g)] && in_loop[target(edge, g)]) + eattr_map[edge]["color"] = "gray"; } std::ofstream loops_out(argv[2]); @@ -150,22 +147,21 @@ main(int argc, char *argv[]) << "size=\"3,3\"\n" << "ratio=\"fill\"\n" << "shape=\"box\"\n"; - graph_traits::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { - loops_out << *vi << "["; - for (std::map::iterator ai = vattr_map[*vi].begin(); - ai != vattr_map[*vi].end(); ++ai) { + for (const auto& vertex : make_range_pair(vertices(g))) { + loops_out << vertex << "["; + for (auto ai = vattr_map[vertex].begin(); + ai != vattr_map[vertex].end(); ++ai) { loops_out << ai->first << "=" << ai->second; - if (next(ai) != vattr_map[*vi].end()) + if (next(ai) != vattr_map[vertex].end()) loops_out << ", "; } loops_out<< "]"; } - for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) { - loops_out << source(*ei, g) << " -> " << target(*ei, g) << "["; - std::map& attr_map = eattr_map[*ei]; - for (std::map::iterator eai = attr_map.begin(); + for (const auto& edge : make_range_pair(edges(g))) { + loops_out << source(edge, g) << " -> " << target(edge, g) << "["; + auto& attr_map = eattr_map[edge]; + for (auto eai = attr_map.begin(); eai != attr_map.end(); ++eai) { loops_out << eai->first << "=" << eai->second; if (next(eai) != attr_map.end()) diff --git a/example/make_biconnected_planar.cpp b/example/make_biconnected_planar.cpp index d0c0dd8e..f9481cda 100644 --- a/example/make_biconnected_planar.cpp +++ b/example/make_biconnected_planar.cpp @@ -16,6 +16,7 @@ #include #include #include +#include "range_pair.hpp" using namespace boost; @@ -23,14 +24,10 @@ using namespace boost; int main(int argc, char** argv) { - typedef adjacency_list - < vecS, - vecS, - undirectedS, - property, - property - > - graph; + using graph = adjacency_list, + property>; graph g(11); add_edge(0,1,g); @@ -48,15 +45,14 @@ int main(int argc, char** argv) //Initialize the interior edge index - property_map::type e_index = get(edge_index, g); + auto e_index = get(edge_index, g); graph_traits::edges_size_type edge_count = 0; - graph_traits::edge_iterator ei, ei_end; - for(boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) - put(e_index, *ei, edge_count++); + for(const auto& edge : make_range_pair(edges(g))) + put(e_index, edge, edge_count++); //Test for planarity; compute the planar embedding as a side-effect - typedef std::vector< graph_traits::edge_descriptor > vec_t; + using vec_t = std::vector::edge_descriptor>; std::vector embedding(num_vertices(g)); if (boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g, boyer_myrvold_params::embedding = @@ -67,13 +63,11 @@ int main(int argc, char** argv) else std::cout << "Input graph is not planar" << std::endl; - typedef std::vector< graph_traits::edges_size_type > - component_storage_t; - typedef iterator_property_map + using component_storage_t = std::vector::edges_size_type>; + using component_map_t = iterator_property_map < component_storage_t::iterator, property_map::type - > - component_map_t; + >; component_storage_t component_storage(num_edges(g)); component_map_t component(component_storage.begin(), get(edge_index, g)); @@ -86,8 +80,8 @@ int main(int argc, char** argv) // Re-initialize the edge index, since we just added a few edges edge_count = 0; - for(boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) - put(e_index, *ei, edge_count++); + for(const auto& edge : make_range_pair(edges(g))) + put(e_index, edge, edge_count++); // Re-size the storage for the biconnected components, since we // just added a few edges diff --git a/example/make_connected.cpp b/example/make_connected.cpp index 91ceb9a7..48f1d75a 100644 --- a/example/make_connected.cpp +++ b/example/make_connected.cpp @@ -22,13 +22,9 @@ using namespace boost; int main(int argc, char** argv) { - typedef adjacency_list - < vecS, - vecS, - undirectedS, - property - > - graph; + using graph = adjacency_list>; graph g(11); add_edge(0,1,g); @@ -40,7 +36,7 @@ int main(int argc, char** argv) add_edge(9,10,g); add_edge(10,8,g); - std::vector< graph_traits::vertices_size_type > + std::vector::vertices_size_type> component(num_vertices(g)); std::cout << "Before calling make_connected, the graph has " diff --git a/example/make_maximal_planar.cpp b/example/make_maximal_planar.cpp index 50d42e8d..057d261f 100644 --- a/example/make_maximal_planar.cpp +++ b/example/make_maximal_planar.cpp @@ -17,6 +17,7 @@ #include #include #include +#include "range_pair.hpp" @@ -43,14 +44,10 @@ struct face_counter : public planar_face_traversal_visitor int main(int argc, char** argv) { - typedef adjacency_list - < vecS, - vecS, - undirectedS, - property, - property - > - graph; + using graph = adjacency_list, + property>; // Create the graph - a straight line graph g(10); @@ -71,15 +68,14 @@ int main(int argc, char** argv) << 2*num_vertices(g) - 4 << " faces." << std::endl; //Initialize the interior edge index - property_map::type e_index = get(edge_index, g); + auto e_index = get(edge_index, g); graph_traits::edges_size_type edge_count = 0; - graph_traits::edge_iterator ei, ei_end; - for(boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) - put(e_index, *ei, edge_count++); + for(const auto& edge : make_range_pair(edges(g))) + put(e_index, edge, edge_count++); //Test for planarity; compute the planar embedding as a side-effect - typedef std::vector< graph_traits::edge_descriptor > vec_t; + using vec_t = std::vector::edge_descriptor>; std::vector embedding(num_vertices(g)); if (boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g, boyer_myrvold_params::embedding = @@ -94,8 +90,8 @@ int main(int argc, char** argv) // Re-initialize the edge index, since we just added a few edges edge_count = 0; - for(boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) - put(e_index, *ei, edge_count++); + for(const auto& edge : make_range_pair(edges(g))) + put(e_index, edge, edge_count++); //Test for planarity again; compute the planar embedding as a side-effect @@ -116,8 +112,8 @@ int main(int argc, char** argv) // Re-initialize the edge index, since we just added a few edges edge_count = 0; - for(boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) - put(e_index, *ei, edge_count++); + for(const auto& edge : make_range_pair(edges(g))) + put(e_index, edge, edge_count++); // Test for planarity one final time; compute the planar embedding as a // side-effect diff --git a/example/matching_example.cpp b/example/matching_example.cpp index de39f57c..e38984ef 100644 --- a/example/matching_example.cpp +++ b/example/matching_example.cpp @@ -12,11 +12,12 @@ #include #include +#include "range_pair.hpp" using namespace boost; -typedef adjacency_list my_graph; +using my_graph = adjacency_list; int main() { @@ -26,15 +27,14 @@ int main() const int n_vertices = 18; - std::vector ascii_graph; - - ascii_graph.push_back(" 0 1---2 3 "); - ascii_graph.push_back(" \\ / \\ / "); - ascii_graph.push_back(" 4---5 6---7 "); - ascii_graph.push_back(" | | | | "); - ascii_graph.push_back(" 8---9 10---11 "); - ascii_graph.push_back(" / \\ / \\ "); - ascii_graph.push_back(" 12 13 14---15 16 17 "); + std::vector ascii_graph = { + " 0 1---2 3 ", + " \\ / \\ / ", + " 4---5 6---7 ", + " | | | | ", + " 8---9 10---11 ", + " / \\ / \\ ", + " 12 13 14---15 16 17 "}; // It has a perfect matching of size 8. There are two isolated // vertices that we'll use later... @@ -76,24 +76,23 @@ int main() std::cout << "In the following graph:" << std::endl << std::endl; - for(std::vector::iterator itr = ascii_graph.begin(); itr != ascii_graph.end(); ++itr) - std::cout << *itr << std::endl; + for (const auto& str : ascii_graph) + std::cout << str << std::endl; std::cout << std::endl << "Found a matching of size " << matching_size(g, &mate[0]) << std::endl; std::cout << "The matching is:" << std::endl; - - graph_traits::vertex_iterator vi, vi_end; - for(boost::tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi) - if (mate[*vi] != graph_traits::null_vertex() && *vi < mate[*vi]) - std::cout << "{" << *vi << ", " << mate[*vi] << "}" << std::endl; + + for(const auto& vertex : make_range_pair(vertices(g))) + if (mate[vertex] != graph_traits::null_vertex() && vertex < mate[vertex]) + std::cout << "{" << vertex << ", " << mate[vertex] << "}" << std::endl; std::cout << std::endl; //now we'll add two edges, and the perfect matching has size 9 ascii_graph.pop_back(); - ascii_graph.push_back(" 12---13 14---15 16---17 "); + ascii_graph.emplace_back(" 12---13 14---15 16---17 "); add_edge(12,13,g); add_edge(16,17,g); @@ -103,16 +102,16 @@ int main() std::cout << "In the following graph:" << std::endl << std::endl; - for(std::vector::iterator itr = ascii_graph.begin(); itr != ascii_graph.end(); ++itr) - std::cout << *itr << std::endl; + for (const auto& str : ascii_graph) + std::cout << str << std::endl; std::cout << std::endl << "Found a matching of size " << matching_size(g, &mate[0]) << std::endl; std::cout << "The matching is:" << std::endl; - for(boost::tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi) - if (mate[*vi] != graph_traits::null_vertex() && *vi < mate[*vi]) - std::cout << "{" << *vi << ", " << mate[*vi] << "}" << std::endl; + for(const auto& vertex : make_range_pair(vertices(g))) + if (mate[vertex] != graph_traits::null_vertex() && vertex < mate[vertex]) + std::cout << "{" << vertex << ", " << mate[vertex] << "}" << std::endl; return 0; } diff --git a/example/max_flow.cpp b/example/max_flow.cpp index fc9f17c1..003f9ca7 100644 --- a/example/max_flow.cpp +++ b/example/max_flow.cpp @@ -14,6 +14,7 @@ #include #include #include +#include "range_pair.hpp" // Use a DIMACS network flow file as stdin. // max_flow < max_flow.dat @@ -49,47 +50,40 @@ main() { using namespace boost; - typedef adjacency_list_traits Traits; - typedef adjacency_list; + using Graph = adjacency_list, property > > - > Graph; + property>> + >; Graph g; - property_map::type - capacity = get(edge_capacity, g); - property_map::type - rev = get(edge_reverse, g); - property_map::type - residual_capacity = get(edge_residual_capacity, g); + auto capacity = get(edge_capacity, g); + auto rev = get(edge_reverse, g); + auto residual_capacity = get(edge_residual_capacity, g); Traits::vertex_descriptor s, t; read_dimacs_max_flow(g, capacity, rev, s, t); - long flow; #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 // Use non-named parameter version - property_map::type - indexmap = get(vertex_index, g); - flow = push_relabel_max_flow(g, s, t, capacity, residual_capacity, rev, indexmap); + auto indexmap = get(vertex_index, g); + auto flow = push_relabel_max_flow(g, s, t, capacity, residual_capacity, rev, indexmap); #else - flow = push_relabel_max_flow(g, s, t); + auto flow = push_relabel_max_flow(g, s, t); #endif std::cout << "c The total flow:" << std::endl; std::cout << "s " << flow << std::endl << std::endl; std::cout << "c flow values:" << std::endl; - graph_traits::vertex_iterator u_iter, u_end; - graph_traits::out_edge_iterator ei, e_end; - for (boost::tie(u_iter, u_end) = vertices(g); u_iter != u_end; ++u_iter) - for (boost::tie(ei, e_end) = out_edges(*u_iter, g); ei != e_end; ++ei) - if (capacity[*ei] > 0) - std::cout << "f " << *u_iter << " " << target(*ei, g) << " " - << (capacity[*ei] - residual_capacity[*ei]) << std::endl; + for (const auto& vertex : make_range_pair(vertices(g))) + for (const auto& edge : make_range_pair(out_edges(vertex, g))) + if (capacity[edge] > 0) + std::cout << "f " << vertex << " " << target(edge, g) << " " + << (capacity[edge] - residual_capacity[edge]) << std::endl; return 0; } diff --git a/example/mcgregor_subgraphs_example.cpp b/example/mcgregor_subgraphs_example.cpp index 543259fc..4af47e28 100644 --- a/example/mcgregor_subgraphs_example.cpp +++ b/example/mcgregor_subgraphs_example.cpp @@ -11,7 +11,6 @@ #include #include -#include #include #include #include @@ -26,7 +25,7 @@ using namespace boost; template struct example_callback { - typedef typename graph_traits::vertices_size_type VertexSizeFirst; + using VertexSizeFirst = typename graph_traits::vertices_size_type; example_callback(const Graph& graph1) : m_graph1(graph1) { } @@ -38,8 +37,8 @@ struct example_callback { VertexSizeFirst subgraph_size) { // Fill membership map for first graph - typedef typename property_map::type VertexIndexMap; - typedef shared_array_property_map MembershipMap; + using VertexIndexMap = typename property_map::type; + using MembershipMap = shared_array_property_map; MembershipMap membership_map1(num_vertices(m_graph1), get(vertex_index, m_graph1)); @@ -47,8 +46,8 @@ struct example_callback { fill_membership_map(m_graph1, correspondence_map_1_to_2, membership_map1); // Generate filtered graphs using membership map - typedef typename membership_filtered_graph_traits::graph_type - MembershipFilteredGraph; + using MembershipFilteredGraph = + typename membership_filtered_graph_traits::graph_type; MembershipFilteredGraph subgraph1 = make_membership_filtered_graph(m_graph1, membership_map1); @@ -71,19 +70,17 @@ int main (int argc, char *argv[]) { // Using a vecS graph here so that we don't have to mess around with // a vertex index map; it will be implicit. - typedef adjacency_list >, - property > Graph; - - typedef property_map::type VertexNameMap; + property>, + property>; // Test maximum and unique variants on known graphs Graph graph_simple1, graph_simple2; example_callback user_callback(graph_simple1); - VertexNameMap vname_map_simple1 = get(vertex_name, graph_simple1); - VertexNameMap vname_map_simple2 = get(vertex_name, graph_simple2); + auto vname_map_simple1 = get(vertex_name, graph_simple1); + auto vname_map_simple2 = get(vertex_name, graph_simple2); // Graph that looks like a triangle put(vname_map_simple1, add_vertex(graph_simple1), 1); diff --git a/example/mean_geodesic.cpp b/example/mean_geodesic.cpp index ca246b0a..96fc5108 100644 --- a/example/mean_geodesic.cpp +++ b/example/mean_geodesic.cpp @@ -13,40 +13,40 @@ #include #include #include "helper.hpp" +#include "range_pair.hpp" -using namespace std; using namespace boost; // The Actor type stores the name of each vertex in the graph. struct Actor { - string name; + std::string name; }; // Declare the graph type and its vertex and edge types. -typedef undirected_graph Graph; -typedef graph_traits::vertex_descriptor Vertex; -typedef graph_traits::edge_descriptor Edge; +using Graph = undirected_graph; +using Vertex = graph_traits::vertex_descriptor; +using Edge = graph_traits::edge_descriptor; // The name map provides an abstract accessor for the names of // each vertex. This is used during graph creation. -typedef property_map::type NameMap; +using NameMap = property_map::type; // Declare a matrix type and its corresponding property map that // will contain the distances between each pair of vertices. -typedef exterior_vertex_property DistanceProperty; -typedef DistanceProperty::matrix_type DistanceMatrix; -typedef DistanceProperty::matrix_map_type DistanceMatrixMap; +using DistanceProperty = exterior_vertex_property; +using DistanceMatrix = DistanceProperty::matrix_type; +using DistanceMatrixMap = DistanceProperty::matrix_map_type; // Declare the weight map so that each edge returns the same value. -typedef constant_property_map WeightMap; +using WeightMap = constant_property_map; // Declare a container and its corresponding property map that // will contain the resulting mean geodesic distances of each // vertex in the graph. -typedef exterior_vertex_property GeodesicProperty; -typedef GeodesicProperty::container_type GeodesicContainer; -typedef GeodesicProperty::map_type GeodesicMap; +using GeodesicProperty = exterior_vertex_property; +using GeodesicContainer = GeodesicProperty::container_type; +using GeodesicMap = GeodesicProperty::map_type; int main(int argc, char *argv[]) @@ -57,7 +57,7 @@ main(int argc, char *argv[]) NameMap nm(get(&Actor::name, g)); // Read the graph from standad input. - read_graph(g, nm, cin); + read_graph(g, nm, std::cin); // Compute the distances between all pairs of vertices using // the Floyd-Warshall algorithm. Note that the weight map is @@ -72,16 +72,15 @@ main(int argc, char *argv[]) // so-called small-world distance) as a result. GeodesicContainer geodesics(num_vertices(g)); GeodesicMap gm(geodesics, g); - float sw = all_mean_geodesics(g, dm, gm); + auto sw = all_mean_geodesics(g, dm, gm); // Print the mean geodesic distance of each vertex and finally, // the graph itself. - graph_traits::vertex_iterator i, end; - for(boost::tie(i, end) = vertices(g); i != end; ++i) { - cout << setw(12) << setiosflags(ios::left) - << g[*i].name << get(gm, *i) << endl; + for(const auto& vertex : make_range_pair(vertices(g))) { + std::cout << std::setw(12) << std::setiosflags(std::ios::left) + << g[vertex].name << get(gm, vertex) << std::endl; } - cout << "small world distance: " << sw << endl; + std::cout << "small world distance: " << sw << std::endl; return 0; diff --git a/example/miles_span.cpp b/example/miles_span.cpp index e9058673..11d7d40a 100644 --- a/example/miles_span.cpp +++ b/example/miles_span.cpp @@ -24,7 +24,7 @@ // PropertyMap. template struct total_length_visitor : public boost::dijkstra_visitor<> { - typedef typename boost::property_traits::value_type D; + using D = typename boost::property_traits::value_type; total_length_visitor(D& len, Distance d) : _total_length(len), _distance(d) { } template @@ -85,11 +85,9 @@ int main(int argc, char* argv[]) long sp_length = 0; // Use the "z" utility field for distance. - typedef property_map >::type Distance; - Distance d = get(z_property(), g); + auto d = get(z_property(), g); // Use the "w" property for parent - typedef property_map >::type Parent; - Parent p = get(w_property(), g); + auto p = get(w_property(), g); total_length_visitor length_vis(sp_length, d); prim_minimum_spanning_tree(g, p, diff --git a/example/min_max_paths.cpp b/example/min_max_paths.cpp index f32dd76e..e4eecfd1 100644 --- a/example/min_max_paths.cpp +++ b/example/min_max_paths.cpp @@ -42,37 +42,36 @@ main(int , char* []) { using namespace boost; - typedef adjacency_list > Graph; - typedef graph_traits::vertex_descriptor Vertex; + using Graph = adjacency_list>; + using Vertex = graph_traits::vertex_descriptor; - typedef std::pair E; + using E = std::pair; const char name[] = "abcdef"; const int num_nodes = 6; - E edges[] = { E(0,2), E(1,1), E(1,3), E(1,4), E(2,1), E(2,3), + const auto edges = { E(0,2), E(1,1), E(1,3), E(1,4), E(2,1), E(2,3), E(3,4), E(4,0), E(4,1) }; int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1}; const int n_edges = sizeof(edges)/sizeof(E); #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 // VC++ can't handle iterator constructors Graph G(num_nodes); - property_map::type weightmap = get(edge_weight, G); + auto weightmap = get(edge_weight, G); for (std::size_t j = 0; j < sizeof(edges) / sizeof(E); ++j) { - graph_traits::edge_descriptor e; bool inserted; - boost::tie(e, inserted) = add_edge(edges[j].first, edges[j].second, G); + auto [e, inserted] = add_edge(edges[j].first, edges[j].second, G); weightmap[e] = weights[j]; } #else - Graph G(edges, edges + n_edges, weights, num_nodes); - property_map::type weightmap = get(edge_weight, G); + Graph G(std::begin(edges), std::end(edges), weights, num_nodes); + auto = get(edge_weight, G); #endif std::vector p(num_vertices(G)); std::vector d(num_vertices(G)); - Vertex s = *(vertices(G).first); + auto s = *(vertices(G).first); #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 dijkstra_shortest_paths @@ -87,17 +86,16 @@ main(int , char* []) #endif std::cout << "distances from start vertex:" << std::endl; - graph_traits::vertex_iterator vi, vend; - for(boost::tie(vi,vend) = vertices(G); vi != vend; ++vi) - std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << std::endl; + for(const auto& vertex : make_range_pair(vertices(G))) + std::cout << "distance(" << name[vertex] << ") = " << d[vertex] << std::endl; std::cout << std::endl; std::cout << "min-max paths tree" << std::endl; adjacency_list<> tree(num_nodes); - for(boost::tie(vi,vend) = vertices(G); vi != vend; ++vi) - if (*vi != p[*vi]) - add_edge(p[*vi], *vi, tree); + for(const auto& vertex : make_range_pair(vertices(G))) + if (vertex != p[vertex]) + add_edge(p[vertex], vertex, tree); print_graph(tree, name); diff --git a/example/minimum_degree_ordering.cpp b/example/minimum_degree_ordering.cpp index a7b1963a..c972f7e5 100644 --- a/example/minimum_degree_ordering.cpp +++ b/example/minimum_degree_ordering.cpp @@ -37,24 +37,23 @@ struct harwell_boeing { harwell_boeing(char* filename) { int Nrhs; - char* Type; - Type = new char[4]; + auto Type = new char[4]; isComplex = false; readHB_info(filename, &M, &N, &nonzeros, &Type, &Nrhs); colptr = (int *)malloc((N+1)*sizeof(int)); - if ( colptr == NULL ) IOHBTerminate("Insufficient memory for colptr.\n"); + if ( colptr == nullptr ) IOHBTerminate("Insufficient memory for colptr.\n"); rowind = (int *)malloc(nonzeros*sizeof(int)); - if ( rowind == NULL ) IOHBTerminate("Insufficient memory for rowind.\n"); + if ( rowind == nullptr ) IOHBTerminate("Insufficient memory for rowind.\n"); if ( Type[0] == 'C' ) { isComplex = true; val = (double *)malloc(nonzeros*sizeof(double)*2); - if ( val == NULL ) IOHBTerminate("Insufficient memory for val.\n"); + if ( val == nullptr ) IOHBTerminate("Insufficient memory for val.\n"); } else { if ( Type[0] != 'P' ) { val = (double *)malloc(nonzeros*sizeof(double)); - if ( val == NULL ) IOHBTerminate("Insufficient memory for val.\n"); + if ( val == nullptr ) IOHBTerminate("Insufficient memory for val.\n"); } } @@ -86,11 +85,10 @@ struct harwell_boeing int main(int argc, char* argv[]) { - using namespace std; using namespace boost; if (argc < 2) { - cout << argv[0] << " HB file" << endl; + std::cout << argv[0] << " HB file" << std::endl; return -1; } @@ -98,15 +96,15 @@ int main(int argc, char* argv[]) if ( argc >= 4 ) delta = atoi(argv[3]); - + harwell_boeing hbs(argv[1]); //must be BGL directed graph now - typedef adjacency_list Graph; + using Graph = adjacency_list ; - int n = hbs.nrows(); + auto n = hbs.nrows(); - cout << "n is " << n << endl; + std::cout << "n is " << n << std::endl; Graph G(n); @@ -120,17 +118,16 @@ int main(int argc, char* argv[]) num_edge++; } - cout << "number of off-diagnal elements: " << num_edge << endl; + std::cout << "number of off-diagnal elements: " << num_edge << std::endl; - typedef std::vector Vector; + using Vector = std::vector; Vector inverse_perm(n, 0); Vector perm(n, 0); Vector supernode_sizes(n, 1); // init has to be 1 - boost::property_map::type - id = get(vertex_index, G); + auto id = get(vertex_index, G); Vector degree(n, 0); @@ -143,9 +140,9 @@ int main(int argc, char* argv[]) delta, id); if ( argc >= 3 ) { - ifstream input(argv[2]); + std::ifstream input(argv[2]); if ( input.fail() ) { - cout << argv[3] << " is failed to open!. " << endl; + std::cout << argv[3] << " is failed to open!. " << std::endl; return -1; } int comp; @@ -154,24 +151,24 @@ int main(int argc, char* argv[]) for ( i=0; i> comp; if ( comp != inverse_perm[i]+1 ) { - cout << "at i= " << i << ": " << comp - << " ***is NOT EQUAL to*** " << inverse_perm[i]+1 << endl; + std::cout << "at i= " << i << ": " << comp + << " ***is NOT EQUAL to*** " << inverse_perm[i]+1 << std::endl; is_correct = false; } } for ( i=0; i> comp; if ( comp != perm[i]+1 ) { - cout << "at i= " << i << ": " << comp - << " ***is NOT EQUAL to*** " << perm[i]+1 << endl; + std::cout << "at i= " << i << ": " << comp + << " ***is NOT EQUAL to*** " << perm[i]+1 << std::endl; is_correct = false; } } if ( is_correct ) - cout << "Permutation and inverse permutation are correct. "<< endl; + std::cout << "Permutation and inverse permutation are correct. "<< std::endl; else - cout << "WARNING -- Permutation or inverse permutation is not the " - << "same ones generated by Liu's " << endl; + std::cout << "WARNING -- Permutation or inverse permutation is not the " + << "same ones generated by Liu's " << std::endl; } return 0; diff --git a/example/modify_graph.cpp b/example/modify_graph.cpp index 42b668cb..07ccd9f4 100644 --- a/example/modify_graph.cpp +++ b/example/modify_graph.cpp @@ -12,6 +12,7 @@ #include #include #include +#include "range_pair.hpp" using namespace boost; @@ -39,17 +40,14 @@ name_equals(const std::string& str, NamePropertyMap name) { template void modify_demo(MutableGraph& g) { - typedef graph_traits GraphTraits; - typedef typename GraphTraits::vertices_size_type size_type; - typedef typename GraphTraits::edge_descriptor edge_descriptor; + using GraphTraits = graph_traits; + using size_type = typename GraphTraits::vertices_size_type; + using edge_descriptor = typename GraphTraits::edge_descriptor; size_type n = 0; typename GraphTraits::edges_size_type m = 0; typename GraphTraits::vertex_descriptor u, v, w; edge_descriptor e, e1, e2; - typename property_map::type - name_map = get(edge_name, g); - bool added; - typename GraphTraits::vertex_iterator vi, vi_end; + auto name_map = get(edge_name, g); { v = add_vertex(g); @@ -69,7 +67,7 @@ void modify_demo(MutableGraph& g) u = add_vertex(g); v = add_vertex(g); - std::pair p = add_edge(u, v, g); + auto p = add_edge(u, v, g); assert(num_edges(g) == m + 1); assert(p.second == true); // edge should have been added @@ -86,7 +84,7 @@ void modify_demo(MutableGraph& g) u = add_vertex(g); v = add_vertex(g); - boost::tie(e, added) = add_edge(u, v, g); + auto [e, added] = add_edge(u, v, g); assert(num_edges(g) == m + 2); assert(added == true); // edge should have been added @@ -101,15 +99,14 @@ void modify_demo(MutableGraph& g) remove_edge(u, v, g); assert(num_edges(g) == m + 1); - bool exists; - boost::tie(e, exists) = edge(u, v, g); + auto [e, exists] = edge(u, v, g); assert(exists == false); assert(out_degree(u, g) == 0); assert(in_degree(v, g) == 0); } { e = *edges(g).first; - boost::tie(u, v) = incident(e, g); + auto [u, v] = incident(e, g); remove_edge(e, g); @@ -120,8 +117,7 @@ void modify_demo(MutableGraph& g) { add_edge(u, v, g); - typename GraphTraits::out_edge_iterator iter, iter_end; - boost::tie(iter, iter_end) = out_edges(u, g); + auto [iter, iter_end] = out_edges(u, g); remove_edge(iter, g); @@ -131,13 +127,12 @@ void modify_demo(MutableGraph& g) } { w = add_vertex(g); - boost::tie(e1, added) = add_edge(u, v, g); - boost::tie(e2, added) = add_edge(v, w, g); + auto [e1, added1] = add_edge(u, v, g); + auto [e2, added2] = add_edge(v, w, g); name_map[e1] = "I-5"; name_map[e2] = "Route 66"; - typename GraphTraits::out_edge_iterator iter, iter_end; - boost::tie(iter, iter_end) = out_edges(u, g); + auto [iter, iter_end] = out_edges(u, g); remove_edge_if(name_equals("Route 66", name_map), g); @@ -152,8 +147,8 @@ void modify_demo(MutableGraph& g) assert(in_degree(w, g) == 0); } { - boost::tie(e1, added) = add_edge(u, v, g); - boost::tie(e2, added) = add_edge(u, w, g); + auto [e1, added1] = add_edge(u, v, g); + auto [e2, added2] = add_edge(u, w, g); name_map[e1] = "foo"; name_map[e2] = "foo"; @@ -163,8 +158,8 @@ void modify_demo(MutableGraph& g) assert(out_degree(u, g) == 0); } { - boost::tie(e1, added) = add_edge(u, v, g); - boost::tie(e2, added) = add_edge(w, v, g); + auto [e1, added1] = add_edge(u, v, g); + auto [e2, added2] = add_edge(w, v, g); name_map[e1] = "bar"; name_map[e2] = "bar"; @@ -183,11 +178,9 @@ void modify_demo(MutableGraph& g) assert(out_degree(u, g) == 0); - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { - typename GraphTraits::adjacency_iterator ai, ai_end; - for (boost::tie(ai, ai_end) = adjacent_vertices(*vi, g); - ai != ai_end; ++ai) - assert(*ai != u); + for (const auto& vertex : make_range_pair(vertices(g))) { + for (const auto& adjacent_vertex : make_range_pair(adjacent_vertices(vertex, g))) + assert(adjacent_vertex != vertex); } } } @@ -196,7 +189,7 @@ int main() { adjacency_list > g; + no_property, property> g; modify_demo(g); return 0; diff --git a/example/neighbor_bfs.cpp b/example/neighbor_bfs.cpp index 7b3e339a..2963a4ef 100644 --- a/example/neighbor_bfs.cpp +++ b/example/neighbor_bfs.cpp @@ -53,8 +53,8 @@ struct print_parent { template class distance_and_pred_visitor : public neighbor_bfs_visitor<> { - typedef typename property_traits::value_type ColorValue; - typedef color_traits Color; + using ColorValue = typename property_traits::value_type; + using Color = color_traits; public: distance_and_pred_visitor(DistanceMap d, PredecessorMap p, ColorMap c) : m_distance(d), m_predecessor(p), m_color(c) { } @@ -62,16 +62,14 @@ class distance_and_pred_visitor : public neighbor_bfs_visitor<> template void tree_out_edge(Edge e, const Graph& g) const { - typename graph_traits::vertex_descriptor - u = source(e, g), v = target(e, g); + auto u = source(e, g), v = target(e, g); put(m_distance, v, get(m_distance, u) + 1); put(m_predecessor, v, u); } template void tree_in_edge(Edge e, const Graph& g) const { - typename graph_traits::vertex_descriptor - u = source(e, g), v = target(e, g); + auto u = source(e, g), v = target(e, g); put(m_distance, u, get(m_distance, v) + 1); put(m_predecessor, u, v); } @@ -83,13 +81,12 @@ class distance_and_pred_visitor : public neighbor_bfs_visitor<> int main(int , char* []) { - typedef adjacency_list< + using Graph = adjacency_list< mapS, vecS, bidirectionalS, property - > Graph; + >; - typedef property_map::type - ColorMap; + using ColorMap = property_map::type; Graph G(5); add_edge(0, 2, G); @@ -104,24 +101,24 @@ int main(int , char* []) add_edge(4, 0, G); add_edge(4, 1, G); - typedef Graph::vertex_descriptor Vertex; + using Vertex = Graph::vertex_descriptor; // Array to store predecessor (parent) of each vertex. This will be // used as a Decorator (actually, its iterator will be). std::vector p(num_vertices(G)); // VC++ version of std::vector has no ::pointer, so // I use ::value_type* instead. - typedef std::vector::value_type* Piter; + using Piter = std::vector::value_type*; // Array to store distances from the source to each vertex . We use // a built-in array here just for variety. This will also be used as // a Decorator. - typedef graph_traits::vertices_size_type size_type; + using size_type = graph_traits::vertices_size_type; size_type d[5]; std::fill_n(d, 5, 0); // The source vertex - Vertex s = *(vertices(G).first); + auto s = *(vertices(G).first); p[s] = s; distance_and_pred_visitor vis(d, &p[0], get(vertex_color, G)); diff --git a/example/ordered_out_edges.cpp b/example/ordered_out_edges.cpp index 5c45373a..c1e01574 100644 --- a/example/ordered_out_edges.cpp +++ b/example/ordered_out_edges.cpp @@ -13,6 +13,7 @@ #include #include +#include "range_pair.hpp" /* Sample output: @@ -48,17 +49,17 @@ struct ordered_set_by_nameS { }; namespace boost { template struct container_gen { - typedef std::multiset > type; + using type = std::multiset>; }; } #else struct ordered_set_by_nameS { template - struct bind_ { typedef std::multiset > type; }; + struct bind_ { using } = std::multiset> type;; }; namespace boost { template <> struct container_selector { - typedef ordered_set_by_nameS type; + using type = ordered_set_by_nameS; }; } #endif @@ -66,7 +67,7 @@ namespace boost { namespace boost { template <> struct parallel_edge_traits { - typedef allow_parallel_edge_tag type; + using type = allow_parallel_edge_tag; }; } @@ -77,9 +78,9 @@ main() std::cout << "This program requires partial specialization" << std::endl; #else using namespace boost; - typedef property EdgeProperty; - typedef adjacency_list graph_type; + using EdgeProperty = property; + using graph_type = adjacency_list; graph_type g; add_edge(0, 1, EdgeProperty("joe"), g); @@ -90,32 +91,25 @@ main() add_edge(3, 4, EdgeProperty("harry"), g); add_edge(0, 1, EdgeProperty("chandler"), g); - property_map::type id = get(vertex_index, g); - property_map::type name = get(edge_name, g); + auto id = get(vertex_index, g); + auto name = get(edge_name, g); - graph_traits::vertex_iterator i, end; - graph_traits::out_edge_iterator ei, edge_end; - for (boost::tie(i, end) = vertices(g); i != end; ++i) { - std::cout << id[*i] << " "; - for (boost::tie(ei, edge_end) = out_edges(*i, g); ei != edge_end; ++ei) - std::cout << " --" << name[*ei] << "--> " << id[target(*ei, g)] << " "; + for (const auto& vertex : make_range_pair(vertices(g))) { + std::cout << id[vertex] << " "; + for (const auto& edge : make_range_pair(out_edges(vertex, g))) + std::cout << " --" << name[edge] << "--> " << id[target(edge, g)] << " "; std::cout << std::endl; } std::cout << std::endl; - bool found; - typedef graph_traits Traits; - Traits::edge_descriptor e; - Traits::out_edge_iterator e_first, e_last; - - boost::tie(e, found) = edge(0, 1, g); + auto [e, found] = edge(0, 1, g); if (found) std::cout << "name(0,1) = " << name[e] << std::endl; else std::cout << "not found" << std::endl; std::cout << std::endl; - boost::tie(e_first, e_last) = edge_range(0, 1, g); + auto [e_first, e_last] = edge_range(0, 1, g); while (e_first != e_last) std::cout << "name(0,1) = " << name[*e_first++] << std::endl; #endif diff --git a/example/ospf-example.cpp b/example/ospf-example.cpp index 3d0e53b7..2fdbbc9a 100644 --- a/example/ospf-example.cpp +++ b/example/ospf-example.cpp @@ -8,7 +8,7 @@ #include // for file I/O #include // for read/write_graphviz() #include -#include +#include "range_pair.hpp" namespace boost { enum graph_color_t { graph_color = 5556 }; @@ -19,13 +19,12 @@ int main() { using namespace boost; - typedef + using g_dot_type = adjacency_list, property >, - property > - g_dot_type; + property>, + property>; g_dot_type g_dot; dynamic_properties dp(ignore_other_properties); @@ -38,35 +37,33 @@ main() read_graphviz(infile, g_dot, dp); } - typedef adjacency_list < vecS, vecS, directedS, no_property, - property < edge_weight_t, int > > Graph; - typedef graph_traits < Graph >::vertex_descriptor vertex_descriptor; + using Graph = adjacency_list>; + using vertex_descriptor = graph_traits::vertex_descriptor; Graph g(num_vertices(g_dot)); - graph_traits < g_dot_type >::edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = edges(g_dot); ei != ei_end; ++ei) { - int weight = get(edge_weight, g_dot, *ei); - property < edge_weight_t, int >edge_property(weight); - add_edge(source(*ei, g_dot), target(*ei, g_dot), edge_property, g); + for (const auto& edge : make_range_pair(edges(g_dot))) { + auto weight = get(edge_weight, g_dot, edge); + property edge_property(weight); + add_edge(source(edge, g_dot), target(edge, g_dot), edge_property, g); } vertex_descriptor router_six; - graph_traits < g_dot_type >::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g_dot); vi != vi_end; ++vi) - if ("RT6" == get(vertex_name, g_dot, *vi)) { - router_six = *vi; + for (const auto& vertex : make_range_pair(vertices(g_dot))) + if ("RT6" == get(vertex_name, g_dot, vertex)) { + router_six = vertex; break; } - std::vector < vertex_descriptor > parent(num_vertices(g)); + std::vector parent(num_vertices(g)); // All vertices start out as there own parent - typedef graph_traits < Graph >::vertices_size_type size_type; + using size_type = graph_traits::vertices_size_type; for (size_type p = 0; p < num_vertices(g); ++p) parent[p] = p; #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 std::vector distance(num_vertices(g)); - property_map::type weightmap = get(edge_weight, g); - property_map::type indexmap = get(vertex_index, g); + auto weightmap = get(edge_weight, g); + auto indexmap = get(vertex_index, g); dijkstra_shortest_paths (g, router_six, &parent[0], &distance[0], weightmap, indexmap, std::less(), closed_plus(), @@ -75,10 +72,9 @@ main() dijkstra_shortest_paths(g, router_six, predecessor_map(&parent[0])); #endif - graph_traits < g_dot_type >::edge_descriptor e; for (size_type i = 0; i < num_vertices(g); ++i) if (parent[i] != i) { - e = edge(parent[i], i, g_dot).first; + auto e = edge(parent[i], i, g_dot).first; put(edge_color, g_dot, e, "black"); } @@ -90,13 +86,13 @@ main() std::ofstream rtable("routing-table.dat"); rtable << "Dest Next Hop Total Cost" << std::endl; - for (boost::tie(vi, vi_end) = vertices(g_dot); vi != vi_end; ++vi) - if (parent[*vi] != *vi) { - rtable << get(vertex_name, g_dot, *vi) << " "; - vertex_descriptor v = *vi, child; + for (const auto& vertex : make_range_pair(vertices(g_dot))) + if (parent[vertex] != vertex) { + rtable << get(vertex_name, g_dot, vertex) << " "; + auto v = vertex; + vertex_descriptor child; int path_cost = 0; - property_map < Graph, edge_weight_t >::type - weight_map = get(edge_weight, g); + auto weight_map = get(edge_weight, g); do { path_cost += get(weight_map, edge(parent[v], v, g).first); child = v; diff --git a/example/parallel-compile-time.cpp b/example/parallel-compile-time.cpp index 271cdec6..2711429d 100644 --- a/example/parallel-compile-time.cpp +++ b/example/parallel-compile-time.cpp @@ -14,11 +14,12 @@ #include #include #include // for default_dfs_visitor +#include "range_pair.hpp" namespace std { - template < typename T > - std::istream & operator >> (std::istream & in, std::pair < T, T > &p) + template + std::istream & operator >> (std::istream & in, std::pair &p) { in >> p.first >> p.second; return in; @@ -33,67 +34,64 @@ namespace boost using namespace boost; -typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list +using file_dep_graph2 = adjacency_list > > >, + property>>>, // an edge property - property < edge_weight_t, float > > - file_dep_graph2; + property>; -typedef graph_traits::vertex_descriptor vertex_t; -typedef graph_traits::edge_descriptor edge_t; +using vertex_t = graph_traits::vertex_descriptor; +using edge_t = graph_traits::edge_descriptor; -template < typename Graph, typename ColorMap, typename Visitor > void +template void dfs_v2(const Graph & g, - typename graph_traits < Graph >::vertex_descriptor u, + typename graph_traits::vertex_descriptor u, ColorMap color, Visitor vis) { - typedef typename property_traits < ColorMap >::value_type color_type; - typedef color_traits < color_type > ColorT; + using color_type = typename property_traits::value_type; + using ColorT = color_traits; color[u] = ColorT::gray(); vis.discover_vertex(u, g); - typename graph_traits < Graph >::out_edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei) - if (color[target(*ei, g)] == ColorT::white()) { - vis.tree_edge(*ei, g); - dfs_v2(g, target(*ei, g), color, vis); - } else if (color[target(*ei, g)] == ColorT::gray()) - vis.back_edge(*ei, g); + for (const auto& edge : make_range_pair(out_edges(u, g))) + if (color[target(edge, g)] == ColorT::white()) { + vis.tree_edge(edge, g); + dfs_v2(g, target(edge, g), color, vis); + } else if (color[target(edge, g)] == ColorT::gray()) + vis.back_edge(edge, g); else - vis.forward_or_cross_edge(*ei, g); + vis.forward_or_cross_edge(edge, g); color[u] = ColorT::black(); vis.finish_vertex(u, g); } -template < typename Graph, typename Visitor, typename ColorMap > void +template void generic_dfs_v2(const Graph & g, Visitor vis, ColorMap color) { - typedef typename property_traits ::value_type ColorValue; - typedef color_traits < ColorValue > ColorT; - typename graph_traits < Graph >::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) - color[*vi] = ColorT::white(); - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) - if (color[*vi] == ColorT::white()) - dfs_v2(g, *vi, color, vis); + using ColorValue = typename property_traits ::value_type; + using ColorT = color_traits ; + for (const auto& vertex : make_range_pair(vertices(g))) + color[vertex] = ColorT::white(); + for (const auto& vertex : make_range_pair(vertices(g))) + if (color[vertex] == ColorT::white()) + dfs_v2(g, vertex, color, vis); } -template < typename OutputIterator > +template struct topo_visitor: public default_dfs_visitor { topo_visitor(OutputIterator & order): topo_order(order) { } - template < typename Graph > void - finish_vertex(typename graph_traits < Graph >::vertex_descriptor u, + template void + finish_vertex(typename graph_traits::vertex_descriptor u, const Graph &) { *topo_order++ = u; @@ -101,102 +99,87 @@ struct topo_visitor: public default_dfs_visitor OutputIterator & topo_order; }; -template < typename Graph, typename OutputIterator, typename ColorMap > void +template void topo_sort(const Graph & g, OutputIterator topo_order, ColorMap color) { - topo_visitor < OutputIterator > vis(topo_order); + topo_visitor vis(topo_order); generic_dfs_v2(g, vis, color); } -typedef property_map < file_dep_graph2, vertex_name_t >::type name_map_t; -typedef property_map < file_dep_graph2, vertex_compile_cost_t >::type - compile_cost_map_t; -typedef property_map < file_dep_graph2, vertex_distance_t >::type distance_map_t; -typedef property_map < file_dep_graph2, vertex_color_t >::type color_map_t; +using name_map_t = property_map::type; +using compile_cost_map_t = property_map::type; +using distance_map_t = property_map::type; +using color_map_t = property_map::type; int main() { std::ifstream file_in("makefile-dependencies.dat"); - typedef graph_traits < file_dep_graph2 >::vertices_size_type size_type; + using size_type = graph_traits::vertices_size_type; size_type n_vertices; file_in >> n_vertices; // read in number of vertices std::istream_iterator < std::pair < size_type, - size_type > >input_begin(file_in), input_end; + size_type >> input_begin(file_in), input_end; #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 // VC++ can't handle the iterator constructor file_dep_graph2 g; - typedef graph_traits::vertex_descriptor vertex_t; + using vertex_t = graph_traits::vertex_descriptor; std::vector id2vertex; for (std::size_t v = 0; v < n_vertices; ++v) - id2vertex.push_back(add_vertex(g)); + id2vertex.emplace_back(add_vertex(g)); while (input_begin != input_end) { - size_type i, j; - boost::tie(i, j) = *input_begin++; + auto [i, j] = *input_begin++; add_edge(id2vertex[i], id2vertex[j], g); } #else file_dep_graph2 g(input_begin, input_end, n_vertices); #endif - name_map_t - name_map = + auto name_map = get(vertex_name, g); - compile_cost_map_t - compile_cost_map = + auto compile_cost_map = get(vertex_compile_cost, g); - distance_map_t - distance_map = + auto distance_map = get(vertex_distance, g); - color_map_t - color_map = + auto color_map = get(vertex_color, g); { std::ifstream name_in("makefile-target-names.dat"); std::ifstream compile_cost_in("target-compile-costs.dat"); - graph_traits < file_dep_graph2 >::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { - name_in >> name_map[*vi]; - compile_cost_in >> compile_cost_map[*vi]; + for (const auto& vertex : make_range_pair(vertices(g))) { + name_in >> name_map[vertex]; + compile_cost_in >> compile_cost_map[vertex]; } } - std::vector < vertex_t > topo_order(num_vertices(g)); + std::vector topo_order(num_vertices(g)); topo_sort(g, topo_order.rbegin(), color_map); - graph_traits < file_dep_graph2 >::vertex_iterator i, i_end; - graph_traits < file_dep_graph2 >::adjacency_iterator vi, vi_end; - // find source vertices with zero in-degree by marking all vertices with incoming edges - for (boost::tie(i, i_end) = vertices(g); i != i_end; ++i) - color_map[*i] = white_color; - for (boost::tie(i, i_end) = vertices(g); i != i_end; ++i) - for (boost::tie(vi, vi_end) = adjacent_vertices(*i, g); vi != vi_end; ++vi) - color_map[*vi] = black_color; + for (const auto& vertex : make_range_pair(vertices(g))) + color_map[vertex] = white_color; + for (const auto& vertex : make_range_pair(vertices(g))) + for (const auto& vertex_adjacent : make_range_pair(adjacent_vertices(vertex, g))) + color_map[vertex_adjacent] = black_color; // initialize distances to zero, or for source vertices, to the compile cost - for (boost::tie(i, i_end) = vertices(g); i != i_end; ++i) - if (color_map[*i] == white_color) - distance_map[*i] = compile_cost_map[*i]; + for (const auto& vertex : make_range_pair(vertices(g))) + if (color_map[vertex] == white_color) + distance_map[vertex] = compile_cost_map[vertex]; else - distance_map[*i] = 0; - - std::vector < vertex_t >::iterator ui; - for (ui = topo_order.begin(); ui != topo_order.end(); ++ui) { - vertex_t - u = * - ui; - for (boost::tie(vi, vi_end) = adjacent_vertices(u, g); vi != vi_end; ++vi) - if (distance_map[*vi] < distance_map[u] + compile_cost_map[*vi]) - distance_map[*vi] = distance_map[u] + compile_cost_map[*vi]; + distance_map[vertex] = 0; + + + for (const auto& u : topo_order) { + for (const auto& vertex : make_range_pair(adjacent_vertices(u, g))) + if (distance_map[vertex] < distance_map[u] + compile_cost_map[vertex]) + distance_map[vertex] = distance_map[u] + compile_cost_map[vertex]; } - graph_property_iter_range < file_dep_graph2, - vertex_distance_t >::iterator ci, ci_end; - boost::tie(ci, ci_end) = get_property_iter_range(g, vertex_distance); + auto [ci, ci_end] = get_property_iter_range(g, vertex_distance); std::cout << "total (parallel) compile time: " << *std::max_element(ci, ci_end) << std::endl; diff --git a/example/planar_face_traversal.cpp b/example/planar_face_traversal.cpp index fe96d2de..9fc017df 100644 --- a/example/planar_face_traversal.cpp +++ b/example/planar_face_traversal.cpp @@ -15,6 +15,7 @@ #include #include +#include "range_pair.hpp" using namespace boost; @@ -56,14 +57,13 @@ struct edge_output_visitor : public output_visitor int main(int argc, char** argv) { - typedef adjacency_list + using graph = adjacency_list < vecS, vecS, undirectedS, property, property - > - graph; + >; // Create a graph - this is a biconnected, 3 x 3 grid. // It should have four small (four vertex/four edge) faces and @@ -91,16 +91,15 @@ int main(int argc, char** argv) // Initialize the interior edge index - property_map::type e_index = get(edge_index, g); + auto e_index = get(edge_index, g); graph_traits::edges_size_type edge_count = 0; - graph_traits::edge_iterator ei, ei_end; - for(boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) - put(e_index, *ei, edge_count++); + for(const auto& edge : make_range_pair(edges(g))) + put(e_index, edge, edge_count++); // Test for planarity - we know it is planar, we just want to // compute the planar embedding as a side-effect - typedef std::vector< graph_traits::edge_descriptor > vec_t; + using vec_t = std::vector::edge_descriptor>; std::vector embedding(num_vertices(g)); if (boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g, boyer_myrvold_params::embedding = diff --git a/example/prim-example.cpp b/example/prim-example.cpp index b36740bf..4f538269 100644 --- a/example/prim-example.cpp +++ b/example/prim-example.cpp @@ -14,31 +14,30 @@ int main() { using namespace boost; - typedef adjacency_list < vecS, vecS, undirectedS, - property, property < edge_weight_t, int > > Graph; - typedef std::pair < int, int >E; + using Graph = adjacency_list, property>; + using E = std::pair; const int num_nodes = 5; - E edges[] = { E(0, 2), E(1, 3), E(1, 4), E(2, 1), E(2, 3), + const auto edges = { E(0, 2), E(1, 3), E(1, 4), E(2, 1), E(2, 3), E(3, 4), E(4, 0) }; int weights[] = { 1, 1, 2, 7, 3, 1, 1 }; #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 Graph g(num_nodes); - property_map::type weightmap = get(edge_weight, g); + auto weightmap = get(edge_weight, g); for (std::size_t j = 0; j < sizeof(edges) / sizeof(E); ++j) { - graph_traits::edge_descriptor e; bool inserted; - boost::tie(e, inserted) = add_edge(edges[j].first, edges[j].second, g); + auto [e, inserted] = add_edge(edges[j].first, edges[j].second, g); weightmap[e] = weights[j]; } #else - Graph g(edges, edges + sizeof(edges) / sizeof(E), weights, num_nodes); + Graph g(std::begin(edges), std::end(edges), weights, num_nodes); #endif std::vector < graph_traits < Graph >::vertex_descriptor > p(num_vertices(g)); #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 - property_map::type distance = get(vertex_distance, g); - property_map::type indexmap = get(vertex_index, g); + auto distance = get(vertex_distance, g); + auto indexmap = get(vertex_index, g); prim_minimum_spanning_tree (g, *vertices(g).first, &p[0], distance, weightmap, indexmap, default_dijkstra_visitor()); diff --git a/example/prim-telephone.cpp b/example/prim-telephone.cpp index 6b6f914f..1e72f567 100644 --- a/example/prim-telephone.cpp +++ b/example/prim-telephone.cpp @@ -9,9 +9,10 @@ #include #include #include -#include #include #include +#include "range_pair.hpp" + int main() { @@ -19,23 +20,21 @@ main() GraphvizGraph g_dot; read_graphviz("figs/telephone-network.dot", g_dot); - typedef adjacency_list < vecS, vecS, undirectedS, no_property, - property < edge_weight_t, int > > Graph; + using Graph = adjacency_list>; Graph g(num_vertices(g_dot)); - property_map < GraphvizGraph, edge_attribute_t >::type - edge_attr_map = get(edge_attribute, g_dot); - graph_traits < GraphvizGraph >::edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = edges(g_dot); ei != ei_end; ++ei) { - int weight = lexical_cast < int >(edge_attr_map[*ei]["label"]); - property < edge_weight_t, int >edge_property(weight); - add_edge(source(*ei, g_dot), target(*ei, g_dot), edge_property, g); + auto edge_attr_map = get(edge_attribute, g_dot); + for (const auto& edge : make_range_pair(edges(g_dot))) { + auto weight = std::stoi(edge_attr_map[edge]["label"]); + property edge_property(weight); + add_edge(source(edge, g_dot), target(edge, g_dot), edge_property, g); } - typedef graph_traits < Graph >::vertex_descriptor Vertex; + using Vertex = graph_traits < Graph >::vertex_descriptor; std::vector < Vertex > parent(num_vertices(g)); - property_map < Graph, edge_weight_t >::type weight = get(edge_weight, g); + auto weight = get(edge_weight, g); #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 - property_map::type indexmap = get(vertex_index, g); + auto indexmap = get(vertex_index, g); std::vector distance(num_vertices(g)); prim_minimum_spanning_tree(g, *vertices(g).first, &parent[0], &distance[0], weight, indexmap, default_dijkstra_visitor()); @@ -44,17 +43,16 @@ main() #endif int total_weight = 0; - for (int v = 0; v < num_vertices(g); ++v) + for (std::size_t v = 0; v < num_vertices(g); ++v) if (parent[v] != v) total_weight += get(weight, edge(parent[v], v, g).first); std::cout << "total weight: " << total_weight << std::endl; - for (int u = 0; u < num_vertices(g); ++u) + for (std::size_t u = 0; u < num_vertices(g); ++u) if (parent[u] != u) edge_attr_map[edge(parent[u], u, g_dot).first]["color"] = "black"; std::ofstream out("figs/telephone-mst-prim.dot"); - graph_property < GraphvizGraph, graph_edge_attribute_t >::type & - graph_edge_attr_map = get_property(g_dot, graph_edge_attribute); + auto& graph_edge_attr_map = get_property(g_dot, graph_edge_attribute); graph_edge_attr_map["color"] = "gray"; write_graphviz(out, g_dot); diff --git a/example/print-adjacent-vertices.cpp b/example/print-adjacent-vertices.cpp index 6c20941d..9e3dcc0c 100644 --- a/example/print-adjacent-vertices.cpp +++ b/example/print-adjacent-vertices.cpp @@ -10,21 +10,21 @@ #include #include #include +#include "range_pair.hpp" using namespace boost; -template < typename Graph, typename VertexNamePropertyMap > void +template void read_graph_file(std::istream & graph_in, std::istream & name_in, Graph & g, VertexNamePropertyMap name_map) { - typedef typename graph_traits < Graph >::vertices_size_type size_type; + using size_type = typename graph_traits::vertices_size_type; size_type n_vertices; - typename graph_traits < Graph >::vertex_descriptor u; - typename property_traits < VertexNamePropertyMap >::value_type name; + typename property_traits::value_type name; graph_in >> n_vertices; // read in number of vertices for (size_type i = 0; i < n_vertices; ++i) { // Add n vertices to the graph - u = add_vertex(g); + auto u = add_vertex(g); name_in >> name; put(name_map, u, name); // ** Attach name property to vertex u ** } @@ -36,25 +36,24 @@ read_graph_file(std::istream & graph_in, std::istream & name_in, break; } -template < typename Graph, typename VertexNameMap > void +template void output_adjacent_vertices(std::ostream & out, - typename graph_traits < Graph >::vertex_descriptor u, + typename graph_traits::vertex_descriptor u, const Graph & g, VertexNameMap name_map) { - typename graph_traits < Graph >::adjacency_iterator vi, vi_end; out << get(name_map, u) << " -> { "; - for (boost::tie(vi, vi_end) = adjacent_vertices(u, g); vi != vi_end; ++vi) - out << get(name_map, *vi) << " "; + for (const auto& vertex : make_range_pair(adjacent_vertices(u, g))) + out << get(name_map, vertex) << " "; out << "}" << std::endl; } -template < typename NameMap > class name_equals_t { +template class name_equals_t { public: name_equals_t(const std::string & n, NameMap map) : m_name(n), m_name_map(map) { } - template < typename Vertex > bool operator()(Vertex u) const + template bool operator()(Vertex u) const { return get(m_name_map, u) == m_name; } @@ -64,22 +63,22 @@ template < typename NameMap > class name_equals_t { }; // object generator function -template < typename NameMap > - inline name_equals_t < NameMap > +template + inline name_equals_t name_equals(const std::string & str, NameMap name) { - return name_equals_t < NameMap > (str, name); + return name_equals_t (str, name); } int main() { - typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list + using graph_type = adjacency_list < listS, // Store out-edges of each vertex in a std::list vecS, // Store vertex set in a std::vector directedS, // The graph is directed - property < vertex_name_t, std::string > // Add a vertex property - >graph_type; + property // Add a vertex property + >; graph_type g; // use default constructor to create empty graph const char* dep_file_name = "makefile-dependencies.dat"; @@ -96,12 +95,10 @@ main() return -1; } // Obtain internal property map from the graph - property_map < graph_type, vertex_name_t >::type name_map = - get(vertex_name, g); + auto name_map = get(vertex_name, g); read_graph_file(file_in, name_in, g, name_map); - graph_traits < graph_type >::vertex_iterator i, end; - boost::tie(i, end) = vertices(g); + auto [i, end] = vertices(g); i = std::find_if(i, end, name_equals("dax.h", get(vertex_name, g))); output_adjacent_vertices(std::cout, *i, g, get(vertex_name, g)); diff --git a/example/print-edges.cpp b/example/print-edges.cpp index 7616022b..6642e057 100644 --- a/example/print-edges.cpp +++ b/example/print-edges.cpp @@ -10,21 +10,21 @@ #include #include #include +#include "range_pair.hpp" using namespace boost; -template < typename Graph, typename VertexNamePropertyMap > void +template void read_graph_file(std::istream & graph_in, std::istream & name_in, Graph & g, VertexNamePropertyMap name_map) { - typedef typename graph_traits < Graph >::vertices_size_type size_type; + using size_type = typename graph_traits::vertices_size_type; size_type n_vertices; - typename graph_traits < Graph >::vertex_descriptor u; - typename property_traits < VertexNamePropertyMap >::value_type name; + typename property_traits::value_type name; graph_in >> n_vertices; // read in number of vertices for (size_type i = 0; i < n_vertices; ++i) { // Add n vertices to the graph - u = add_vertex(g); + auto u = add_vertex(g); name_in >> name; put(name_map, u, name); // ** Attach name property to vertex u ** } @@ -36,25 +36,24 @@ read_graph_file(std::istream & graph_in, std::istream & name_in, break; } -template < typename Graph, typename VertexNameMap > void +template void print_dependencies(std::ostream & out, const Graph & g, VertexNameMap name_map) { - typename graph_traits < Graph >::edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) - out << get(name_map, source(*ei, g)) << " -$>$ " - << get(name_map, target(*ei, g)) << std::endl; + for (const auto& edge : make_range_pair(edges(g))) + out << get(name_map, source(edge, g)) << " -$>$ " + << get(name_map, target(edge, g)) << std::endl; } int main() { - typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list + using graph_type = adjacency_list < listS, // Store out-edges of each vertex in a std::list vecS, // Store vertex set in a std::vector directedS, // The graph is directed - property < vertex_name_t, std::string > // Add a vertex property - >graph_type; + property // Add a vertex property + >; graph_type g; // use default constructor to create empty graph const char* dep_file_name = "makefile-dependencies.dat"; @@ -72,8 +71,7 @@ main() } // Obtain internal property map from the graph - property_map < graph_type, vertex_name_t >::type name_map = - get(vertex_name, g); + auto name_map = get(vertex_name, g); read_graph_file(file_in, name_in, g, name_map); print_dependencies(std::cout, g, get(vertex_name, g)); diff --git a/example/print-in-edges.cpp b/example/print-in-edges.cpp index 3a889f06..e74594c6 100644 --- a/example/print-in-edges.cpp +++ b/example/print-in-edges.cpp @@ -10,21 +10,21 @@ #include #include #include +#include "range_pair.hpp" using namespace boost; -template < typename Graph, typename VertexNamePropertyMap > void +template void read_graph_file(std::istream & graph_in, std::istream & name_in, Graph & g, VertexNamePropertyMap name_map) { - typedef typename graph_traits < Graph >::vertices_size_type size_type; + using size_type = typename graph_traits::vertices_size_type; size_type n_vertices; - typename graph_traits < Graph >::vertex_descriptor u; - typename property_traits < VertexNamePropertyMap >::value_type name; + typename property_traits::value_type name; graph_in >> n_vertices; // read in number of vertices for (size_type i = 0; i < n_vertices; ++i) { // Add n vertices to the graph - u = add_vertex(g); + auto u = add_vertex(g); name_in >> name; put(name_map, u, name); // ** Attach name property to vertex u ** } @@ -36,24 +36,23 @@ read_graph_file(std::istream & graph_in, std::istream & name_in, break; } -template < typename Graph, typename VertexNameMap > void +template void output_in_edges(std::ostream & out, const Graph & g, - typename graph_traits < Graph >::vertex_descriptor v, + typename graph_traits::vertex_descriptor v, VertexNameMap name_map) { - typename graph_traits < Graph >::in_edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = in_edges(v, g); ei != ei_end; ++ei) - out << get(name_map, source(*ei, g)) << " -> " - << get(name_map, target(*ei, g)) << std::endl; + for (const auto& edge : make_range_pair(in_edges(v, g))) + out << get(name_map, source(edge, g)) << " -> " + << get(name_map, target(edge, g)) << std::endl; } -template < typename NameMap > class name_equals_t { +template class name_equals_t { public: name_equals_t(const std::string & n, NameMap map) : m_name(n), m_name_map(map) { } - template < typename Vertex > bool operator()(Vertex u) const + template bool operator()(Vertex u) const { return get(m_name_map, u) == m_name; } @@ -63,22 +62,22 @@ template < typename NameMap > class name_equals_t { }; // object generator function -template < typename NameMap > - inline name_equals_t < NameMap > +template + inline name_equals_t name_equals(const std::string & str, NameMap name) { - return name_equals_t < NameMap > (str, name); + return name_equals_t (str, name); } int main() { - typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list + using graph_type = adjacency_list < listS, // Store out-edges of each vertex in a std::list vecS, // Store vertex set in a std::vector bidirectionalS, // The graph is directed, with both out-edges and in-edges - property < vertex_name_t, std::string > // Add a vertex property - >graph_type; + property // Add a vertex property + >; graph_type g; // use default constructor to create empty graph const char* dep_file_name = "makefile-dependencies.dat"; @@ -96,12 +95,10 @@ main() } // Obtain internal property map from the graph - property_map < graph_type, vertex_name_t >::type name_map = - get(vertex_name, g); + auto name_map = get(vertex_name, g); read_graph_file(file_in, name_in, g, name_map); - graph_traits < graph_type >::vertex_iterator i, end; - boost::tie(i, end) = vertices(g); + auto [i, end] = vertices(g); i = std::find_if(i, end, name_equals("libzigzag.a", get(vertex_name, g))); output_in_edges(std::cout, g, *i, get(vertex_name, g)); assert(num_vertices(g) == 15); diff --git a/example/print-out-edges.cpp b/example/print-out-edges.cpp index 7e3cbef6..2cd2ebea 100644 --- a/example/print-out-edges.cpp +++ b/example/print-out-edges.cpp @@ -10,17 +10,18 @@ #include #include #include +#include "range_pair.hpp" using namespace boost; -template < typename Graph, typename VertexNamePropertyMap > void +template void read_graph_file(std::istream & graph_in, std::istream & name_in, Graph & g, VertexNamePropertyMap name_map) { - typedef typename graph_traits < Graph >::vertices_size_type size_type; + using size_type = typename graph_traits::vertices_size_type; size_type n_vertices; - typename graph_traits < Graph >::vertex_descriptor u; - typename property_traits < VertexNamePropertyMap >::value_type name; + typename graph_traits::vertex_descriptor u; + typename property_traits::value_type name; graph_in >> n_vertices; // read in number of vertices for (size_type i = 0; i < n_vertices; ++i) { // Add n vertices to the graph @@ -36,24 +37,23 @@ read_graph_file(std::istream & graph_in, std::istream & name_in, break; } -template < typename Graph, typename VertexNameMap > void +template void output_out_edges(std::ostream & out, const Graph & g, - typename graph_traits < Graph >::vertex_descriptor u, + typename graph_traits::vertex_descriptor u, VertexNameMap name_map) { - typename graph_traits < Graph >::out_edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei) - out << get(name_map, source(*ei, g)) << " -> " - << get(name_map, target(*ei, g)) << std::endl; + for (const auto& edge : make_range_pair(out_edges(u, g))) + out << get(name_map, source(edge, g)) << " -> " + << get(name_map, target(edge, g)) << std::endl; } -template < typename NameMap > class name_equals_t { +template class name_equals_t { public: name_equals_t(const std::string & n, NameMap map) : m_name(n), m_name_map(map) { } - template < typename Vertex > bool operator()(Vertex u) const + template bool operator()(Vertex u) const { return get(m_name_map, u) == m_name; } @@ -63,22 +63,22 @@ template < typename NameMap > class name_equals_t { }; // object generator function -template < typename NameMap > - inline name_equals_t < NameMap > +template + inline name_equals_t name_equals(const std::string & str, NameMap name) { - return name_equals_t < NameMap > (str, name); + return name_equals_t (str, name); } int main() { - typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list + using graph_type = adjacency_list < listS, // Store out-edges of each vertex in a std::list vecS, // Store vertex set in a std::vector directedS, // The graph is directed - property < vertex_name_t, std::string > // Add a vertex property - >graph_type; + property // Add a vertex property + >; graph_type g; // use default constructor to create empty graph const char* dep_file_name = "makefile-dependencies.dat"; @@ -95,14 +95,12 @@ main() return -1; } // Obtain internal property map from the graph - property_map < graph_type, vertex_name_t >::type name_map = - get(vertex_name, g); + auto name_map = get(vertex_name, g); read_graph_file(file_in, name_in, g, name_map); - graph_traits < graph_type >::vertex_iterator i, end; - boost::tie(i, end) = vertices(g); - typedef property_map < graph_type, vertex_name_t >::type name_map_t; - name_equals_t < name_map_t > predicate("dax.h", get(vertex_name, g)); + auto [i, end] = vertices(g); + using name_map_t = property_map::type; + name_equals_t predicate("dax.h", get(vertex_name, g)); i = std::find_if(i, end, predicate); output_out_edges(std::cout, g, *i, get(vertex_name, g)); diff --git a/example/property-map-traits-eg.cpp b/example/property-map-traits-eg.cpp index c82ab19c..cde11b7e 100644 --- a/example/property-map-traits-eg.cpp +++ b/example/property-map-traits-eg.cpp @@ -13,12 +13,11 @@ int main() { using namespace boost; - typedef adjacency_list < listS, listS, directedS, - property < vertex_name_t, std::string > >graph_t; + using graph_t = adjacency_list>; graph_t g; - graph_traits < graph_t >::vertex_descriptor u = add_vertex(g); - property_map < graph_t, vertex_name_t >::type - name_map = get(vertex_name, g); + auto u = add_vertex(g); + auto name_map = get(vertex_name, g); name_map[u] = "Joe"; std::cout << name_map[u] << std::endl; return EXIT_SUCCESS; diff --git a/example/property_iterator.cpp b/example/property_iterator.cpp index d8e61cc3..53eb1bbc 100644 --- a/example/property_iterator.cpp +++ b/example/property_iterator.cpp @@ -22,37 +22,37 @@ using namespace boost; //======== vertex properties struct toto_t { enum { num = 23063}; - typedef vertex_property_tag kind; + using kind = vertex_property_tag; }; -typedef property< toto_t, double > Toto; +using Toto = property; struct radius_t { enum { num = 23062}; - typedef vertex_property_tag kind; + using kind = vertex_property_tag; }; -typedef property< radius_t, double, Toto > Radius; +using Radius = property; struct mass_t { enum { num = 23061}; - typedef vertex_property_tag kind; + using kind = vertex_property_tag; }; -typedef property< mass_t, int, Radius > Mass; +using Mass = property; //====== edge properties struct stiff_t { enum { num = 23064}; - typedef edge_property_tag kind; + using kind = edge_property_tag; }; -typedef property Stiff; +using Stiff = property; //===== graph type -typedef Mass VertexProperty; -typedef Stiff EdgeProperty; -typedef adjacency_list Graph; +using VertexProperty = Mass; +using EdgeProperty = Stiff; +using Graph = adjacency_list; //===== utilities @@ -94,14 +94,12 @@ int main(int argc, char* argv[]) std::cout << write( graph ); std::cout << "radii:" << std::endl; - graph_property_iter_range::type - seqRadius = get_property_iter_range(graph,radius_t()); + auto seqRadius = get_property_iter_range(graph,radius_t()); std::for_each( seqRadius.first, seqRadius.second, Print() ); std::cout << std::endl; std::cout << "stiff:" << std::endl; - graph_property_iter_range::type - seqStiff = get_property_iter_range(graph, stiff_t()); + auto seqStiff = get_property_iter_range(graph, stiff_t()); std::for_each( seqStiff.first, seqStiff.second, Print() ); std::cout << std::endl; diff --git a/example/push-relabel-eg.cpp b/example/push-relabel-eg.cpp index 407045f5..55562e42 100644 --- a/example/push-relabel-eg.cpp +++ b/example/push-relabel-eg.cpp @@ -11,6 +11,7 @@ #include #include #include +#include "range_pair.hpp" // Use a DIMACS network flow file as stdin. // push-relabel-eg < max_flow.dat @@ -46,39 +47,35 @@ int main() { using namespace boost; - typedef adjacency_list_traits < vecS, vecS, directedS > Traits; - typedef adjacency_list < vecS, vecS, directedS, - property < vertex_name_t, std::string >, + using Traits = adjacency_list_traits; + using Graph = adjacency_list < vecS, vecS, directedS, + property, property < edge_capacity_t, long, property < edge_residual_capacity_t, long, - property < edge_reverse_t, Traits::edge_descriptor > > > > Graph; + property>>>; Graph g; - property_map < Graph, edge_capacity_t >::type - capacity = get(edge_capacity, g); - property_map < Graph, edge_residual_capacity_t >::type - residual_capacity = get(edge_residual_capacity, g); - property_map < Graph, edge_reverse_t >::type rev = get(edge_reverse, g); + auto capacity = get(edge_capacity, g); + auto residual_capacity = get(edge_residual_capacity, g); + auto rev = get(edge_reverse, g); Traits::vertex_descriptor s, t; read_dimacs_max_flow(g, capacity, rev, s, t); #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 - property_map::type indexmap = get(vertex_index, g); - long flow = push_relabel_max_flow(g, s, t, capacity, residual_capacity, rev, + auto indexmap = get(vertex_index, g); + auto flow = push_relabel_max_flow(g, s, t, capacity, residual_capacity, rev, indexmap); #else - long flow = push_relabel_max_flow(g, s, t); + auto flow = push_relabel_max_flow(g, s, t); #endif std::cout << "c The total flow:" << std::endl; std::cout << "s " << flow << std::endl << std::endl; std::cout << "c flow values:" << std::endl; - graph_traits < Graph >::vertex_iterator u_iter, u_end; - graph_traits < Graph >::out_edge_iterator ei, e_end; - for (boost::tie(u_iter, u_end) = vertices(g); u_iter != u_end; ++u_iter) - for (boost::tie(ei, e_end) = out_edges(*u_iter, g); ei != e_end; ++ei) - if (capacity[*ei] > 0) - std::cout << "f " << *u_iter << " " << target(*ei, g) << " " - << (capacity[*ei] - residual_capacity[*ei]) << std::endl; + for (const auto& vertex : make_range_pair(vertices(g))) + for (const auto& edge : make_range_pair(out_edges(vertex, g))) + if (capacity[edge] > 0) + std::cout << "f " << vertex << " " << target(edge, g) << " " + << (capacity[edge] - residual_capacity[edge]) << std::endl; return EXIT_SUCCESS; } diff --git a/example/put-get-helper-eg.cpp b/example/put-get-helper-eg.cpp index 9c14e1b9..83a511d7 100644 --- a/example/put-get-helper-eg.cpp +++ b/example/put-get-helper-eg.cpp @@ -18,18 +18,16 @@ namespace foo { using namespace boost; - template < class RandomAccessIterator, class IndexMap > + template class iterator_property_map:public boost::put_get_helper < - typename std::iterator_traits < RandomAccessIterator >::reference, - iterator_property_map < RandomAccessIterator, IndexMap > > + typename std::iterator_traits::reference, + iterator_property_map> { public: - typedef std::ptrdiff_t key_type; - typedef typename std::iterator_traits < RandomAccessIterator >::value_type - value_type; - typedef typename std::iterator_traits < RandomAccessIterator >::reference - reference; - typedef boost::lvalue_property_map_tag category; + using key_type = std::ptrdiff_t; + using value_type = typename std::iterator_traits::value_type; + using reference = typename std::iterator_traits::reference; + using category = boost::lvalue_property_map_tag; iterator_property_map(RandomAccessIterator cc = RandomAccessIterator(), const IndexMap & _id = @@ -50,9 +48,9 @@ namespace foo int main() { - typedef std::vector < std::string > vec_t; - typedef foo::iterator_property_map < vec_t::iterator, - boost::identity_property_map > pmap_t; + using vec_t = std::vector; + using pmap_t = foo::iterator_property_map; using namespace boost; BOOST_CONCEPT_ASSERT(( Mutable_LvaluePropertyMapConcept )); return 0; diff --git a/example/quick-tour.cpp b/example/quick-tour.cpp index 31ffb606..2a9fbc5f 100644 --- a/example/quick-tour.cpp +++ b/example/quick-tour.cpp @@ -13,14 +13,14 @@ using namespace boost; -template < typename VertexDescriptor, typename VertexNameMap > void +template void print_vertex_name(VertexDescriptor v, VertexNameMap name_map) { std::cout << get(name_map, v); } -template < typename Graph, typename TransDelayMap, typename VertexNameMap > void -print_trans_delay(typename graph_traits < Graph >::edge_descriptor e, +template void +print_trans_delay(typename graph_traits::edge_descriptor e, const Graph & g, TransDelayMap delay_map, VertexNameMap name_map) { @@ -28,12 +28,12 @@ print_trans_delay(typename graph_traits < Graph >::edge_descriptor e, << get(name_map, target(e, g)) << ") = " << get(delay_map, e); } -template < typename Graph, typename VertexNameMap > void +template void print_vertex_names(const Graph & g, VertexNameMap name_map) { std::cout << "vertices(g) = { "; - typedef typename graph_traits < Graph >::vertex_iterator iter_t; - for (std::pair < iter_t, iter_t > p = vertices(g); p.first != p.second; + using iter_t = typename graph_traits::vertex_iterator; + for (auto p = vertices(g); p.first != p.second; ++p.first) { print_vertex_name(*p.first, name_map); std::cout << ' '; @@ -41,22 +41,21 @@ print_vertex_names(const Graph & g, VertexNameMap name_map) std::cout << "}" << std::endl; } -template < typename Graph, typename TransDelayMap, typename VertexNameMap > void +template void print_trans_delays(const Graph & g, TransDelayMap trans_delay_map, VertexNameMap name_map) { - typename graph_traits < Graph >::edge_iterator first, last; - for (boost::tie(first, last) = edges(g); first != last; ++first) { - print_trans_delay(*first, g, trans_delay_map, name_map); + for (const auto& edge : make_range_pair(edges(g))) { + print_trans_delay(edge, g, trans_delay_map, name_map); std::cout << std::endl; } } -template < typename Graph, typename VertexNameMap, typename TransDelayMap > void +template void build_router_network(Graph & g, VertexNameMap name_map, TransDelayMap delay_map) { - typename graph_traits < Graph >::vertex_descriptor a, b, c, d, e; + typename graph_traits::vertex_descriptor a, b, c, d, e; a = add_vertex(g); name_map[a] = 'a'; b = add_vertex(g); @@ -68,22 +67,22 @@ build_router_network(Graph & g, VertexNameMap name_map, e = add_vertex(g); name_map[e] = 'e'; - typename graph_traits < Graph >::edge_descriptor ed; + typename graph_traits::edge_descriptor ed; bool inserted; - boost::tie(ed, inserted) = add_edge(a, b, g); + auto [ed, inserted] = add_edge(a, b, g); delay_map[ed] = 1.2; - boost::tie(ed, inserted) = add_edge(a, d, g); + auto [ed, inserted] = add_edge(a, d, g); delay_map[ed] = 4.5; - boost::tie(ed, inserted) = add_edge(b, d, g); + auto [ed, inserted] = add_edge(b, d, g); delay_map[ed] = 1.8; - boost::tie(ed, inserted) = add_edge(c, a, g); + auto [ed, inserted] = add_edge(c, a, g); delay_map[ed] = 2.6; - boost::tie(ed, inserted) = add_edge(c, e, g); + auto [ed, inserted] = add_edge(c, e, g); delay_map[ed] = 5.2; - boost::tie(ed, inserted) = add_edge(d, c, g); + auto [ed, inserted] = add_edge(d, c, g); delay_map[ed] = 0.4; - boost::tie(ed, inserted) = add_edge(d, e, g); + auto [ed, inserted] = add_edge(d, e, g); delay_map[ed] = 3.3; } @@ -92,15 +91,13 @@ build_router_network(Graph & g, VertexNameMap name_map, int main() { - typedef adjacency_list < listS, listS, directedS, - property < vertex_name_t, char >, - property < edge_weight_t, double > > graph_t; + using graph_t = adjacency_list < listS, listS, directedS, + property, + property>; graph_t g; - property_map < graph_t, vertex_name_t >::type name_map = - get(vertex_name, g); - property_map < graph_t, edge_weight_t >::type delay_map = - get(edge_weight, g); + auto name_map = get(vertex_name, g); + auto delay_map = get(edge_weight, g); build_router_network(g, name_map, delay_map); print_vertex_names(g, name_map); diff --git a/example/quick_tour.cpp b/example/quick_tour.cpp index e639fc5a..4836b427 100644 --- a/example/quick_tour.cpp +++ b/example/quick_tour.cpp @@ -11,31 +11,27 @@ #include // for std::cout #include // for std::pair #include // for std::for_each -#include // for boost::tie +#include // for std::tie #include #include +#include "range_pair.hpp" using namespace boost; template struct exercise_vertex { exercise_vertex(Graph& g_, const char name_[]) : g(g_),name(name_) { } - typedef typename graph_traits::vertex_descriptor Vertex; + using Vertex = typename graph_traits::vertex_descriptor; void operator()(const Vertex& v) const { using namespace boost; - typename property_map::type - vertex_id = get(vertex_index, g); + auto vertex_id = get(vertex_index, g); std::cout << "vertex: " << name[get(vertex_id, v)] << std::endl; // Write out the outgoing edges std::cout << "\tout-edges: "; - typename graph_traits::out_edge_iterator out_i, out_end; - typename graph_traits::edge_descriptor e; - for (boost::tie(out_i, out_end) = out_edges(v, g); - out_i != out_end; ++out_i) + for (const auto& e : make_range_pair(out_edges(v, g))) { - e = *out_i; - Vertex src = source(e, g), targ = target(e, g); + auto src = source(e, g), targ = target(e, g); std::cout << "(" << name[get(vertex_id, src)] << "," << name[get(vertex_id, targ)] << ") "; } @@ -43,11 +39,9 @@ template struct exercise_vertex { // Write out the incoming edges std::cout << "\tin-edges: "; - typename graph_traits::in_edge_iterator in_i, in_end; - for (boost::tie(in_i, in_end) = in_edges(v, g); in_i != in_end; ++in_i) + for (const auto& e : make_range_pair(in_edges(v, g))) { - e = *in_i; - Vertex src = source(e, g), targ = target(e, g); + auto src = source(e, g), targ = target(e, g); std::cout << "(" << name[get(vertex_id, src)] << "," << name[get(vertex_id, targ)] << ") "; } @@ -55,9 +49,8 @@ template struct exercise_vertex { // Write out all adjacent vertices std::cout << "\tadjacent vertices: "; - typename graph_traits::adjacency_iterator ai, ai_end; - for (boost::tie(ai,ai_end) = adjacent_vertices(v, g); ai != ai_end; ++ai) - std::cout << name[get(vertex_id, *ai)] << " "; + for (const auto& vertex : make_range_pair(adjacent_vertices(v, g))) + std::cout << name[get(vertex_id, vertex)] << " "; std::cout << std::endl; } Graph& g; @@ -67,9 +60,9 @@ template struct exercise_vertex { int main(int,char*[]) { - // create a typedef for the Graph type - typedef adjacency_list > Graph; + // create an alias for the Graph type + using Graph = adjacency_list>; // Make convenient labels for the vertices enum { A, B, C, D, E, N }; @@ -77,7 +70,7 @@ int main(int,char*[]) const char name[] = "ABCDE"; // writing out the edges in the graph - typedef std::pair Edge; + using Edge = std::pair; Edge edge_array[] = { Edge(A,B), Edge(A,D), Edge(C,A), Edge(D,C), Edge(C,E), Edge(B,D), Edge(D,E), }; @@ -90,10 +83,9 @@ int main(int,char*[]) #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 // VC++ can't handle the iterator constructor Graph g(num_vertices); - property_map::type weightmap = get(edge_weight, g); + auto weightmap = get(edge_weight, g); for (std::size_t j = 0; j < num_edges; ++j) { - graph_traits::edge_descriptor e; bool inserted; - boost::tie(e, inserted) = add_edge(edge_array[j].first, edge_array[j].second, g); + auto [e, inserted] = add_edge(edge_array[j].first, edge_array[j].second, g); weightmap[e] = transmission_delay[j]; } #else @@ -101,23 +93,18 @@ int main(int,char*[]) transmission_delay, num_vertices); #endif - boost::property_map::type - vertex_id = get(vertex_index, g); - boost::property_map::type - trans_delay = get(edge_weight, g); + auto vertex_id = get(vertex_index, g); + auto trans_delay = get(edge_weight, g); std::cout << "vertices(g) = "; - typedef graph_traits::vertex_iterator vertex_iter; - std::pair vp; - for (vp = vertices(g); vp.first != vp.second; ++vp.first) - std::cout << name[get(vertex_id, *vp.first)] << " "; + for (const auto& vertex : make_range_pair(vertices(g))) + std::cout << name[get(vertex_id, vertex)] << " "; std::cout << std::endl; std::cout << "edges(g) = "; - graph_traits::edge_iterator ei, ei_end; - for (boost::tie(ei,ei_end) = edges(g); ei != ei_end; ++ei) - std::cout << "(" << name[get(vertex_id, source(*ei, g))] - << "," << name[get(vertex_id, target(*ei, g))] << ") "; + for (const auto& edge : make_range_pair(edges(g))) + std::cout << "(" << name[get(vertex_id, source(edge, g))] + << "," << name[get(vertex_id, target(edge, g))] << ") "; std::cout << std::endl; std::for_each(vertices(g).first, vertices(g).second, diff --git a/example/r_c_shortest_paths_example.cpp b/example/r_c_shortest_paths_example.cpp index 518b90c6..2fa4d5f5 100644 --- a/example/r_c_shortest_paths_example.cpp +++ b/example/r_c_shortest_paths_example.cpp @@ -39,12 +39,11 @@ struct SPPRC_Example_Graph_Arc_Prop int time; }; -typedef adjacency_list - SPPRC_Example_Graph; + SPPRC_Example_Graph_Arc_Prop>; // data structures for spp without resource constraints: // ResourceContainer model @@ -164,7 +163,7 @@ class ref_spptw const SPPRC_Example_Graph_Vert_Prop& vert_prop = get( vertex_bundle, g )[target( ed, g )]; new_cont.cost = old_cont.cost + arc_prop.cost; - int& i_time = new_cont.time; + auto& i_time = new_cont.time; i_time = old_cont.time + arc_prop.time; i_time < vert_prop.eat ? i_time = vert_prop.eat : 0; return i_time <= vert_prop.lat ? true : false; @@ -244,7 +243,7 @@ int main() std::vector ::edge_descriptor> > + ::edge_descriptor>> opt_solutions; std::vector pareto_opt_rcs_no_rc; @@ -261,7 +260,7 @@ int main() dominance_no_res_cont(), std::allocator >(), + >(), default_r_c_shortest_paths_visitor() ); std::cout << "SPP without resource constraints:" << std::endl; @@ -281,7 +280,7 @@ int main() // spptw std::vector ::edge_descriptor> > + ::edge_descriptor>> opt_solutions_spptw; std::vector pareto_opt_rcs_spptw; @@ -298,7 +297,7 @@ int main() dominance_spptw(), std::allocator >(), + >(), default_r_c_shortest_paths_visitor() ); std::cout << "SPP with time windows:" << std::endl; diff --git a/example/range_pair.hpp b/example/range_pair.hpp new file mode 100644 index 00000000..864760cc --- /dev/null +++ b/example/range_pair.hpp @@ -0,0 +1,48 @@ +//======================================================================= +// Copyright 2016 Murray Cumming +// Authors: Murray Cumming +// +// Distributed under 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) +//======================================================================= + +#ifndef BOOST_GRAPH_EDGE_LIST_HPP +#define BOOST_GRAPH_EDGE_LIST_HPP + +// Inspired by: +// https://cplusplusmusings.wordpress.com/2013/04/14/range-based-for-loops-and-pairs-of-iterators/ +template +class range_pair +{ +public: + explicit range_pair(T_Pair pair) + : pair_(pair) + {} + + range_pair(const range_pair& src) = default; + range_pair& operator=(const range_pair& src) = default; + + range_pair(range_pair&& src) = default; + range_pair& operator=(range_pair&& src) = default; + + typename T_Pair::first_type + begin() const + { return pair_.first; } + + typename T_Pair::second_type + end() const + { return pair_.second; } + +private: + T_Pair pair_; +}; + +template +range_pair make_range_pair(T_Pair pair) +{ + return range_pair(pair); +} + +#endif //BOOST_GRAPH_EDGE_LIST_HPP + diff --git a/example/reachable-loop-head.cpp b/example/reachable-loop-head.cpp index 2183eab1..d26c2f0c 100644 --- a/example/reachable-loop-head.cpp +++ b/example/reachable-loop-head.cpp @@ -12,6 +12,7 @@ #include #include #include +#include "range_pair.hpp" int main(int argc, char *argv[]) @@ -24,24 +25,22 @@ main(int argc, char *argv[]) using namespace boost; GraphvizDigraph g; read_graphviz(argv[1], g); - graph_traits < GraphvizDigraph >::vertex_descriptor loop_head = 1; - typedef color_traits < default_color_type > Color; + graph_traits::vertex_descriptor loop_head = 1; + using Color = color_traits; - std::vector < default_color_type > + std::vector reachable_from_head(num_vertices(g), Color::white()); default_color_type c; depth_first_visit(g, loop_head, default_dfs_visitor(), make_iterator_property_map(reachable_from_head.begin(), get(vertex_index, g), c)); - property_map::type - vattr_map = get(vertex_attribute, g); + auto vattr_map = get(vertex_attribute, g); - graph_traits < GraphvizDigraph >::vertex_iterator i, i_end; - for (boost::tie(i, i_end) = vertices(g); i != i_end; ++i) - if (reachable_from_head[*i] != Color::white()) { - vattr_map[*i]["color"] = "gray"; - vattr_map[*i]["style"] = "filled"; + for (const auto& vertex : make_range_pair(vertices(g))) + if (reachable_from_head[vertex] != Color::white()) { + vattr_map[vertex]["color"] = "gray"; + vattr_map[vertex]["style"] = "filled"; } std::ofstream loops_out(argv[2]); @@ -51,24 +50,21 @@ main(int argc, char *argv[]) << "size=\"3,3\"\n" << "ratio=\"fill\"\n" << "shape=\"box\"\n"; - graph_traits::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { - loops_out << *vi << "["; - for (std::map::iterator ai = vattr_map[*vi].begin(); - ai != vattr_map[*vi].end(); ++ai) { + for (const auto& vertex : make_range_pair(vertices(g))) { + loops_out << vertex << "["; + for (auto ai = vattr_map[vertex].begin(); + ai != vattr_map[vertex].end(); ++ai) { loops_out << ai->first << "=" << ai->second; - if (next(ai) != vattr_map[*vi].end()) + if (next(ai) != vattr_map[vertex].end()) loops_out << ", "; } loops_out<< "]"; } - property_map::type - eattr_map = get(edge_attribute, g); - graph_traits::edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) { - loops_out << source(*ei, g) << " -> " << target(*ei, g) << "["; - std::map& attr_map = eattr_map[*ei]; - for (std::map::iterator eai = attr_map.begin(); + auto eattr_map = get(edge_attribute, g); + for (const auto& edge : make_range_pair(edges(g))) { + loops_out << source(edge, g) << " -> " << target(edge, g) << "["; + auto& attr_map = eattr_map[edge]; + for (auto eai = attr_map.begin(); eai != attr_map.end(); ++eai) { loops_out << eai->first << "=" << eai->second; if (next(eai) != attr_map.end()) diff --git a/example/reachable-loop-tail.cpp b/example/reachable-loop-tail.cpp index a723d143..615a2a05 100644 --- a/example/reachable-loop-tail.cpp +++ b/example/reachable-loop-tail.cpp @@ -26,18 +26,18 @@ main(int argc, char *argv[]) GraphvizDigraph g_in; read_graphviz(argv[1], g_in); - typedef adjacency_list < vecS, vecS, bidirectionalS, + using Graph = adjacency_list < vecS, vecS, bidirectionalS, GraphvizVertexProperty, - GraphvizEdgeProperty, GraphvizGraphProperty > Graph; + GraphvizEdgeProperty, GraphvizGraphProperty >; Graph g; copy_graph(g_in, g); - graph_traits < GraphvizDigraph >::vertex_descriptor loop_tail = 6; - typedef color_traits < default_color_type > Color; + graph_traits::vertex_descriptor loop_tail = 6; + using Color = color_traits; default_color_type c; - std::vector < default_color_type > reachable_to_tail(num_vertices(g)); - reverse_graph < Graph > reverse_g(g); + std::vector reachable_to_tail(num_vertices(g)); + reverse_graph reverse_g(g); depth_first_visit(reverse_g, loop_tail, default_dfs_visitor(), make_iterator_property_map(reachable_to_tail.begin(), get(vertex_index, g), c)); @@ -47,20 +47,18 @@ main(int argc, char *argv[]) << " graph [ratio=\"fill\",size=\"3,3\"];\n" << " node [shape=\"box\"];\n" << " edge [style=\"bold\"];\n"; - property_map::type - vattr_map = get(vertex_attribute, g); - graph_traits < GraphvizDigraph >::vertex_iterator i, i_end; - for (boost::tie(i, i_end) = vertices(g_in); i != i_end; ++i) { - loops_out << *i << "[label=\"" << vattr_map[*i]["label"] + auto vattr_map = get(vertex_attribute, g); + for (const auto& vertex : make_range_pair(vertices(g_in))) { + loops_out << vertex << "[label=\"" << vattr_map[vertex]["label"] << "\""; - if (reachable_to_tail[*i] != Color::white()) { + if (reachable_to_tail[vertex] != Color::white()) { loops_out << ", color=\"gray\", style=\"filled\""; } loops_out << "]\n"; } - graph_traits < GraphvizDigraph >::edge_iterator e, e_end; - for (boost::tie(e, e_end) = edges(g_in); e != e_end; ++e) - loops_out << source(*e, g) << " -> " << target(*e, g) << ";\n"; + + for (const auto& edge : make_range_pair(edges(g_in))) + loops_out << source(edge, g) << " -> " << target(edge, g) << ";\n"; loops_out << "}\n"; return EXIT_SUCCESS; } diff --git a/example/read_graphviz.cpp b/example/read_graphviz.cpp index 8750e9dc..0c2f0477 100644 --- a/example/read_graphviz.cpp +++ b/example/read_graphviz.cpp @@ -16,34 +16,30 @@ #include using namespace boost; -using namespace std; int main() { // Vertex properties - typedef property < vertex_name_t, std::string, - property < vertex_color_t, float > > vertex_p; + using vertex_p = property>; // Edge properties - typedef property < edge_weight_t, double > edge_p; + using edge_p = property; // Graph properties - typedef property < graph_name_t, std::string > graph_p; + using graph_p = property; // adjacency_list-based type - typedef adjacency_list < vecS, vecS, directedS, - vertex_p, edge_p, graph_p > graph_t; + using graph_t = adjacency_list < vecS, vecS, directedS, + vertex_p, edge_p, graph_p >; // Construct an empty graph and prepare the dynamic_property_maps. graph_t graph(0); dynamic_properties dp; - property_map::type name = - get(vertex_name, graph); + auto name = get(vertex_name, graph); dp.property("node_id",name); - property_map::type mass = - get(vertex_color, graph); + auto mass = get(vertex_color, graph); dp.property("mass",mass); - property_map::type weight = - get(edge_weight, graph); + auto weight = get(edge_weight, graph); dp.property("weight",weight); // Use ref_property_map to turn a graph property into a property map diff --git a/example/read_write_dimacs-eg.cpp b/example/read_write_dimacs-eg.cpp index 64ac43b7..32c5263b 100644 --- a/example/read_write_dimacs-eg.cpp +++ b/example/read_write_dimacs-eg.cpp @@ -35,6 +35,7 @@ #include #include #include +#include "range_pair.hpp" /************************************* @@ -61,25 +62,21 @@ struct zero_edge_capacity{ int main() { using namespace boost; - typedef adjacency_list_traits < vecS, vecS, directedS > Traits; - typedef adjacency_list < vecS, vecS, directedS, - no_property, - property < edge_capacity_t, long, - property < edge_reverse_t, Traits::edge_descriptor > > > Graph; - - typedef graph_traits::out_edge_iterator out_edge_iterator; - typedef graph_traits::edge_descriptor edge_descriptor; - typedef graph_traits::vertex_descriptor vertex_descriptor; + using Traits = adjacency_list_traits; + using Graph = adjacency_list>>; + + using vertex_descriptor = graph_traits::vertex_descriptor; Graph g; - typedef property_map < Graph, edge_capacity_t >::type tCapMap; - typedef tCapMap::value_type tCapMapValue; - - typedef property_map < Graph, edge_reverse_t >::type tRevEdgeMap; + using tCapMap = property_map::type; + using tCapMapValue = tCapMap::value_type; - tCapMap capacity = get(edge_capacity, g); - tRevEdgeMap rev = get(edge_reverse, g); + auto capacity = get(edge_capacity, g); + auto rev = get(edge_reverse, g); vertex_descriptor s, t; /*reading the graph from stdin*/ @@ -89,21 +86,17 @@ int main() tCapMapValue augmented_flow = 0; //we take the source node and check for each outgoing edge e which has a target(p) if we can augment that path - out_edge_iterator oei,oe_end; - for(boost::tie(oei, oe_end) = out_edges(s, g); oei != oe_end; ++oei){ - edge_descriptor from_source = *oei; - vertex_descriptor v = target(from_source, g); - edge_descriptor to_sink; - bool is_there; - boost::tie(to_sink, is_there) = edge(v, t, g); + for(const auto& from_source : make_range_pair(out_edges(s, g))){ + auto v = target(from_source, g); + auto [to_sink, is_there] = edge(v, t, g); if( is_there ){ if( get(capacity, to_sink) > get(capacity, from_source) ){ - tCapMapValue to_augment = get(capacity, from_source); + auto to_augment = get(capacity, from_source); capacity[from_source] = 0; capacity[to_sink] -= to_augment; augmented_flow += to_augment; }else{ - tCapMapValue to_augment = get(capacity, to_sink); + auto to_augment = get(capacity, to_sink); capacity[to_sink] = 0; capacity[from_source] -= to_augment; augmented_flow += to_augment; diff --git a/example/remove_edge_if_bidir.cpp b/example/remove_edge_if_bidir.cpp index 9b7ef69f..d32dd112 100644 --- a/example/remove_edge_if_bidir.cpp +++ b/example/remove_edge_if_bidir.cpp @@ -11,6 +11,7 @@ #include #include #include +#include "range_pair.hpp" /* Sample output: @@ -40,14 +41,14 @@ using namespace boost; -typedef adjacency_list > Graph; +using Graph = adjacency_list>; struct has_weight_greater_than { has_weight_greater_than(int w_, Graph& g_) : w(w_), g(g_) { } bool operator()(graph_traits::edge_descriptor e) { #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 - property_map::type weight = get(edge_weight, g); + auto weight = get(edge_weight, g); return get(weight, e) > w; #else // This version of get() breaks VC++ @@ -61,28 +62,26 @@ struct has_weight_greater_than { int main() { - typedef std::pair Edge; - Edge edge_array[6] = { Edge(0,3), Edge(0,2), Edge(0, 3), + using Edge = std::pair; + Edge edge_array[] = { Edge(0,3), Edge(0,2), Edge(0, 3), Edge(1,3), Edge(2, 0), Edge(3, 2) }; #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 Graph g(4); - for (std::size_t j = 0; j < 6; ++j) - add_edge(edge_array[j].first, edge_array[j].second, g); + for (const auto& edge : edge_array) + add_edge(edge.first, edge.second, g); #else Graph g(edge_array, edge_array + 6, 4); #endif - property_map::type - weight = get(edge_weight, g); + auto weight = get(edge_weight, g); int w = 0; - graph_traits::edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) - weight[*ei] = ++w; + for (const auto& edge : make_range_pair(edges(g))) + weight[edge] = ++w; - property_map::type indexmap = get(vertex_index, g); + auto indexmap = get(vertex_index, g); std::cout << "original graph:" << std::endl; print_graph(g, indexmap); diff --git a/example/remove_edge_if_dir.cpp b/example/remove_edge_if_dir.cpp index 6b6f76ed..830ae7dd 100644 --- a/example/remove_edge_if_dir.cpp +++ b/example/remove_edge_if_dir.cpp @@ -36,26 +36,19 @@ using namespace boost; -typedef adjacency_list Graph; +using Graph = adjacency_list; int main() { - typedef std::pair Edge; - Edge edges[6] = { Edge(0,3), Edge(0,2), Edge(0, 3), + using Edge = std::pair; + const auto edges = { Edge(0,3), Edge(0,2), Edge(0, 3), Edge(1,3), Edge(2, 0), Edge(3, 2) }; -#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 - // VC++ can't handle iterator constructor - Graph g(4); - for (std::size_t j = 0; j < 6; ++j) - add_edge(edges[j].first, edges[j].second, g); -#else - Graph g(edges, edges + 6, 4); -#endif + Graph g(std::begin(edges), std::end(edges), 4); std::cout << "original graph:" << std::endl; print_graph(g, get(vertex_index, g)); diff --git a/example/remove_edge_if_undir.cpp b/example/remove_edge_if_undir.cpp index 3fc5b571..868cd5d7 100644 --- a/example/remove_edge_if_undir.cpp +++ b/example/remove_edge_if_undir.cpp @@ -11,6 +11,7 @@ #include #include #include +#include "range_pair.hpp" using namespace boost; @@ -39,14 +40,14 @@ using namespace boost; */ -typedef adjacency_list > Graph; +using Graph = adjacency_list>; struct has_weight_greater_than { has_weight_greater_than(int w_, Graph& g_) : w(w_), g(g_) { } bool operator()(graph_traits::edge_descriptor e) { #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 - property_map::type weight = get(edge_weight, g); + auto weight = get(edge_weight, g); return get(weight, e) > w; #else // This version of get breaks VC++ @@ -60,26 +61,24 @@ struct has_weight_greater_than { int main() { - typedef std::pair Edge; - Edge edge_array[5] = { Edge(0, 3), Edge(0, 3), + using Edge = std::pair; + Edge edge_array[] = { Edge(0, 3), Edge(0, 3), Edge(1, 3), Edge(2, 0), Edge(3, 2) }; #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 Graph g(4); - for (std::size_t j = 0; j < 5; ++j) - add_edge(edge_array[j].first, edge_array[j].second, g); + for (const auto& edge : edge_array) + add_edge(edge.first, edge.second, g); #else Graph g(edge_array, edge_array + 5, 4); #endif - property_map::type - weight = get(edge_weight, g); + auto weight = get(edge_weight, g); int w = 0; - graph_traits::edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) - weight[*ei] = ++w; + for (const auto& edge : make_range_pair(edges(g))) + weight[edge] = ++w; std::cout << "original graph:" << std::endl; print_graph(g, get(vertex_index, g)); diff --git a/example/reverse_graph.cpp b/example/reverse_graph.cpp index 9ccf1875..cffe24c3 100644 --- a/example/reverse_graph.cpp +++ b/example/reverse_graph.cpp @@ -20,7 +20,7 @@ int main() { using namespace boost; - typedef adjacency_list < vecS, vecS, bidirectionalS > Graph; + using Graph = adjacency_list; Graph G(5); add_edge(0, 2, G); diff --git a/example/roget_components.cpp b/example/roget_components.cpp index 8a34f22e..a1c97ea6 100644 --- a/example/roget_components.cpp +++ b/example/roget_components.cpp @@ -11,6 +11,7 @@ #include #include #include +#include "range_pair.hpp" #define specs(v) \ (filename ? index_map[v] : v->cat_no) << " " << v->name @@ -18,8 +19,7 @@ int main(int argc, char* argv[]) { using namespace boost; - Graph* g; - typedef graph_traits::vertex_descriptor vertex_t; + using vertex_t = graph_traits::vertex_descriptor; unsigned long n = 0; unsigned long d = 0; unsigned long p = 0; @@ -40,7 +40,7 @@ int main(int argc, char* argv[]) } } - g = (filename ? restore_graph(filename) : roget(n, d, p, s)); + auto g = (filename ? restore_graph(filename) : roget(n, d, p, s)); if (g == NULL) { fprintf(stderr, "Sorry, can't create the graph! (error code %ld)\n", panic_code); @@ -56,37 +56,34 @@ int main(int argc, char* argv[]) // a separate field for marking colors, so we use the w field. std::vector comp(num_vertices(g)); - property_map::type - index_map = get(vertex_index, g); + auto index_map = get(vertex_index, g); - property_map >::type - root = get(v_property(), g); + auto root = get(v_property(), g); - int num_comp = strong_components + auto num_comp = strong_components (g, make_iterator_property_map(comp.begin(), index_map), root_map(root). discover_time_map(get(z_property(), g)). color_map(get(w_property(), g))); - std::vector< std::vector > strong_comp(num_comp); + std::vector> strong_comp(num_comp); // First add representative vertices to each component's list - graph_traits::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) - if (root[*vi] == *vi) - strong_comp[comp[index_map[*vi]]].push_back(*vi); + for (const auto& vertex : make_range_pair(vertices(g))) + if (root[vertex] == vertex) + strong_comp[comp[index_map[vertex]]].emplace_back(vertex); // Then add the other vertices of the component - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) - if (root[*vi] != *vi) - strong_comp[comp[index_map[*vi]]].push_back(*vi); + for (const auto& vertex : make_range_pair(vertices(g))) + if (root[vertex] != vertex) + strong_comp[comp[index_map[vertex]]].emplace_back(vertex); // We do not print out the "from" and "to" information as Knuth // does because we no longer have easy access to that information // from outside the algorithm. for (c = 0; c < num_comp; ++c) { - vertex_t v = strong_comp[c].front(); + auto v = strong_comp[c].front(); std::cout << "Strong component `" << specs(v) << "'"; if (strong_comp[c].size() > 1) { std::cout << " also includes:\n"; @@ -113,13 +110,12 @@ int main(int argc, char* argv[]) // We go in reverse order just to mimic the output ordering in // Knuth's version. for (c = num_comp - 1; c >= 0; --c) { - vertex_t u = strong_comp[c][0]; + auto u = strong_comp[c][0]; for (i = 0; i < strong_comp[c].size(); ++i) { - vertex_t v = strong_comp[c][i]; - graph_traits::out_edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = out_edges(v, g); ei != ei_end; ++ei) { - vertex_t x = target(*ei, g); - int comp_x = comp[index_map[x]]; + auto v = strong_comp[c][i]; + for (const auto& edge : make_range_pair(out_edges(v, g))) { + auto x = target(edge, g); + auto comp_x = comp[index_map[x]]; if (comp_x != c && mark[comp_x] != c) { mark[comp_x] = c; vertex_t w = strong_comp[comp_x][0]; diff --git a/example/scaled_closeness_centrality.cpp b/example/scaled_closeness_centrality.cpp index a2ade80c..30dfdf16 100644 --- a/example/scaled_closeness_centrality.cpp +++ b/example/scaled_closeness_centrality.cpp @@ -13,8 +13,8 @@ #include #include #include "helper.hpp" +#include "range_pair.hpp" -using namespace std; using namespace boost; // This template struct provides a generic version of a "scaling" @@ -26,11 +26,11 @@ using namespace boost; template > + typename Divide = std::divides> struct scaled_closeness_measure { - typedef Distance distance_type; - typedef Result result_type; + using distance_type = Distance; + using result_type = Result; Result operator ()(Distance d, const Graph& g) { @@ -51,29 +51,29 @@ struct Actor }; // Declare the graph type and its vertex and edge types. -typedef undirected_graph Graph; -typedef graph_traits::vertex_descriptor Vertex; -typedef graph_traits::edge_descriptor Edge; +using Graph = undirected_graph; +using Vertex = graph_traits::vertex_descriptor; +using Edge = graph_traits::edge_descriptor; // The name map provides an abstract accessor for the names of // each vertex. This is used during graph creation. -typedef property_map::type NameMap; +using NameMap = property_map::type; // Declare a matrix type and its corresponding property map that // will contain the distances between each pair of vertices. -typedef exterior_vertex_property DistanceProperty; -typedef DistanceProperty::matrix_type DistanceMatrix; -typedef DistanceProperty::matrix_map_type DistanceMatrixMap; +using DistanceProperty = exterior_vertex_property; +using DistanceMatrix = DistanceProperty::matrix_type; +using DistanceMatrixMap = DistanceProperty::matrix_map_type; // Declare the weight map so that each edge returns the same value. -typedef constant_property_map WeightMap; +using WeightMap = constant_property_map; // Declare a container and its corresponding property map that // will contain the resulting closeness centralities of each // vertex in the graph. -typedef boost::exterior_vertex_property ClosenessProperty; -typedef ClosenessProperty::container_type ClosenessContainer; -typedef ClosenessProperty::map_type ClosenessMap; +using ClosenessProperty = boost::exterior_vertex_property; +using ClosenessContainer = ClosenessProperty::container_type; +using ClosenessMap = ClosenessProperty::map_type; int main(int argc, char *argv[]) @@ -84,7 +84,7 @@ main(int argc, char *argv[]) NameMap nm(get(&Actor::name, g)); // Read the graph from standard input. - read_graph(g, nm, cin); + read_graph(g, nm, std::cin); // Compute the distances between all pairs of vertices using // the Floyd-Warshall algorithm. Note that the weight map is @@ -103,10 +103,9 @@ main(int argc, char *argv[]) all_closeness_centralities(g, dm, cm, m); // Print the scaled closeness centrality of each vertex. - graph_traits::vertex_iterator i, end; - for(boost::tie(i, end) = vertices(g); i != end; ++i) { - cout << setw(12) << setiosflags(ios::left) - << g[*i].name << get(cm, *i) << endl; + for(const auto& vertex : make_range_pair(vertices(g))) { + std::cout << std::setw(12) << std::setiosflags(std::ios::left) + << g[vertex].name << get(cm, vertex) << std::endl; } return 0; diff --git a/example/scc.cpp b/example/scc.cpp index e34fe582..86c6f8cf 100644 --- a/example/scc.cpp +++ b/example/scc.cpp @@ -19,21 +19,19 @@ main() GraphvizDigraph g; read_graphviz("figs/scc.dot", g); - typedef graph_traits < GraphvizDigraph >::vertex_descriptor vertex_t; - std::map < vertex_t, int >component; + using vertex_t = graph_traits::vertex_descriptor; + std::map component; strong_components(g, make_assoc_property_map(component)); - property_map < GraphvizDigraph, vertex_attribute_t >::type - vertex_attr_map = get(vertex_attribute, g); + auto vertex_attr_map = get(vertex_attribute, g); std::string color[] = { "white", "gray", "black", "lightgray"}; - graph_traits < GraphvizDigraph >::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { - vertex_attr_map[*vi]["color"] = color[component[*vi]]; - vertex_attr_map[*vi]["style"] = "filled"; - if (vertex_attr_map[*vi]["color"] == "black") - vertex_attr_map[*vi]["fontcolor"] = "white"; + for (const auto& vertex : make_range_pair(vertices(g))) { + vertex_attr_map[vertex]["color"] = color[component[vertex]]; + vertex_attr_map[vertex]["style"] = "filled"; + if (vertex_attr_map[vertex]["color"] == "black") + vertex_attr_map[vertex]["fontcolor"] = "white"; } write_graphviz("figs/scc-out.dot", g); diff --git a/example/simple_planarity_test.cpp b/example/simple_planarity_test.cpp index 525c6701..c2f2f89d 100644 --- a/example/simple_planarity_test.cpp +++ b/example/simple_planarity_test.cpp @@ -18,11 +18,11 @@ int main(int argc, char** argv) using namespace boost; - typedef adjacency_list - > graph; + >; graph K_4(4); add_edge(0, 1, K_4); diff --git a/example/sloan_ordering.cpp b/example/sloan_ordering.cpp index e53e3552..d6270ff4 100644 --- a/example/sloan_ordering.cpp +++ b/example/sloan_ordering.cpp @@ -19,10 +19,7 @@ #include #include #include - - -using std::cout; -using std::endl; +#include "range_pair.hpp" /* Sample Output @@ -63,17 +60,16 @@ using std::endl; int main(int , char* []) { - cout << endl; - cout << "#####################################" << endl; - cout << "### First light of sloan-ordering ###" << endl; - cout << "#####################################" << endl << endl; + std::cout << std::endl; + std::cout << "#####################################" << std::endl; + std::cout << "### First light of sloan-ordering ###" << std::endl; + std::cout << "#####################################" << std::endl << std::endl; using namespace boost; - using namespace std; //Defining the graph type - typedef adjacency_list< + using Graph = adjacency_list< setS, vecS, undirectedS, @@ -85,14 +81,14 @@ int main(int , char* []) int, property< vertex_priority_t, - double > > > > Graph; + double>>>>; - typedef graph_traits::vertex_descriptor Vertex; - typedef graph_traits::vertices_size_type size_type; + using Vertex = graph_traits::vertex_descriptor; + using size_type = graph_traits::vertices_size_type; - typedef std::pair Pair; + using Pair = std::pair; - Pair edges[14] = { Pair(0,3), //a-d + Pair edges[] = { Pair(0,3), //a-d Pair(0,5), //a-f Pair(1,2), //b-c Pair(1,4), //b-e @@ -110,19 +106,16 @@ int main(int , char* []) //Creating a graph and adding the edges from above into it Graph G(10); - for (int i = 0; i < 14; ++i) - add_edge(edges[i].first, edges[i].second, G); - - //Creating two iterators over the vertices - graph_traits::vertex_iterator ui, ui_end; + for (const auto& edge : edges) + add_edge(edge.first, edge.second, G); //Creating a property_map with the degrees of the degrees of each vertex - property_map::type deg = get(vertex_degree, G); - for (boost::tie(ui, ui_end) = vertices(G); ui != ui_end; ++ui) - deg[*ui] = degree(*ui, G); + auto deg = get(vertex_degree, G); + for (const auto& vertex : make_range_pair(vertices(G))) + deg[vertex] = degree(vertex, G); //Creating a property_map for the indices of a vertex - property_map::type index_map = get(vertex_index, G); + auto index_map = get(vertex_index, G); std::cout << "original bandwidth: " << bandwidth(G) << std::endl; std::cout << "original profile: " << profile(G) << std::endl; @@ -139,16 +132,16 @@ int main(int , char* []) { //Setting the start node - Vertex s = vertex(0, G); + auto s = vertex(0, G); int ecc; //defining a variable for the pseudoperipheral radius //Calculating the pseudoeperipheral node and radius - Vertex e = pseudo_peripheral_pair(G, s, ecc, get(vertex_color, G), get(vertex_degree, G) ); + auto e = pseudo_peripheral_pair(G, s, ecc, get(vertex_color, G), get(vertex_degree, G) ); - cout << endl; - cout << "Starting vertex: " << s << endl; - cout << "Pseudoperipheral vertex: " << e << endl; - cout << "Pseudoperipheral radius: " << ecc << endl << endl; + std::cout << std::endl; + std::cout << "Starting vertex: " << s << std::endl; + std::cout << "Pseudoperipheral vertex: " << e << std::endl; + std::cout << "Pseudoperipheral radius: " << ecc << std::endl << std::endl; @@ -156,13 +149,12 @@ int main(int , char* []) sloan_ordering(G, s, e, sloan_order.begin(), get(vertex_color, G), get(vertex_degree, G), get(vertex_priority, G)); - cout << "Sloan ordering starting at: " << s << endl; - cout << " "; + std::cout << "Sloan ordering starting at: " << s << std::endl; + std::cout << " "; - for (std::vector::const_iterator i = sloan_order.begin(); - i != sloan_order.end(); ++i) - cout << index_map[*i] << " "; - cout << endl; + for (const auto& vertex : sloan_order) + std::cout << index_map[vertex] << " "; + std::cout << std::endl; for (size_type c = 0; c != sloan_order.size(); ++c) perm[index_map[sloan_order[c]]] = c; @@ -197,12 +189,11 @@ int main(int , char* []) make_degree_map(G), get(vertex_priority, G) ); - cout << endl << "Sloan ordering without a start-vertex:" << endl; - cout << " "; - for (std::vector::const_iterator i=sloan_order.begin(); - i != sloan_order.end(); ++i) - cout << index_map[*i] << " "; - cout << endl; + std::cout << std::endl << "Sloan ordering without a start-vertex:" << std::endl; + std::cout << " "; + for (const auto& vertex : sloan_order) + std::cout << index_map[vertex] << " "; + std::cout << std::endl; for (size_type c = 0; c != sloan_order.size(); ++c) perm[index_map[sloan_order[c]]] = c; @@ -225,10 +216,10 @@ int main(int , char* []) - cout << endl; - cout << "###############################" << endl; - cout << "### sloan-ordering finished ###" << endl; - cout << "###############################" << endl << endl; + std::cout << std::endl; + std::cout << "###############################" << std::endl; + std::cout << "### sloan-ordering finished ###" << std::endl; + std::cout << "###############################" << std::endl << std::endl; return 0; } diff --git a/example/stoer_wagner.cpp b/example/stoer_wagner.cpp index 09f13006..eb28edd8 100644 --- a/example/stoer_wagner.cpp +++ b/example/stoer_wagner.cpp @@ -22,13 +22,11 @@ struct edge_t // A graphic of the min-cut is available at int main() -{ - using namespace std; - - typedef boost::adjacency_list > undirected_graph; - typedef boost::property_map::type weight_map_type; - typedef boost::property_traits::value_type weight_type; +{ + using undirected_graph = boost::adjacency_list>; + using weight_map_type = boost::property_map::type; + using weight_type = boost::property_traits::value_type; // define the 16 edges of the graph. {3, 4} means an undirected edge between vertices 3 and 4. edge_t edges[] = {{3, 4}, {3, 6}, {3, 5}, {0, 4}, {0, 1}, {0, 6}, {0, 7}, @@ -40,32 +38,32 @@ int main() // construct the graph object. 8 is the number of vertices, which are numbered from 0 // through 7, and 16 is the number of edges. - undirected_graph g(edges, edges + 16, ws, 8, 16); + undirected_graph g(std::begin(edges), std::end(edges), ws, 8, 16); // define a property map, `parities`, that will store a boolean value for each vertex. // Vertices that have the same parity after `stoer_wagner_min_cut` runs are on the same side of the min-cut. BOOST_AUTO(parities, boost::make_one_bit_color_map(num_vertices(g), get(boost::vertex_index, g))); // run the Stoer-Wagner algorithm to obtain the min-cut weight. `parities` is also filled in. - int w = boost::stoer_wagner_min_cut(g, get(boost::edge_weight, g), boost::parity_map(parities)); + auto w = boost::stoer_wagner_min_cut(g, get(boost::edge_weight, g), boost::parity_map(parities)); - cout << "The min-cut weight of G is " << w << ".\n" << endl; + std::cout << "The min-cut weight of G is " << w << ".\n" << std::endl; assert(w == 7); - cout << "One set of vertices consists of:" << endl; + std::cout << "One set of vertices consists of:" << std::endl; size_t i; for (i = 0; i < num_vertices(g); ++i) { if (get(parities, i)) - cout << i << endl; + std::cout << i << std::endl; } - cout << endl; + std::cout << std::endl; - cout << "The other set of vertices consists of:" << endl; + std::cout << "The other set of vertices consists of:" << std::endl; for (i = 0; i < num_vertices(g); ++i) { if (!get(parities, i)) - cout << i << endl; + std::cout << i << std::endl; } - cout << endl; + std::cout << std::endl; return EXIT_SUCCESS; } diff --git a/example/straight_line_drawing.cpp b/example/straight_line_drawing.cpp index 15aa8296..77644f7a 100644 --- a/example/straight_line_drawing.cpp +++ b/example/straight_line_drawing.cpp @@ -31,23 +31,16 @@ struct coord_t int main(int argc, char** argv) { - typedef adjacency_list - < vecS, - vecS, - undirectedS, - property - > graph; - - + using graph = adjacency_list>; //Define the storage type for the planar embedding - typedef std::vector< std::vector< graph_traits::edge_descriptor > > - embedding_storage_t; - typedef boost::iterator_property_map + using embedding_storage_t = std::vector::edge_descriptor>>; + using embedding_t = boost::iterator_property_map < embedding_storage_t::iterator, property_map::type - > - embedding_t; + >; @@ -94,12 +87,11 @@ int main(int argc, char** argv) //Set up a property map to hold the mapping from vertices to coord_t's - typedef std::vector< coord_t > straight_line_drawing_storage_t; - typedef boost::iterator_property_map + using straight_line_drawing_storage_t = std::vector; + using straight_line_drawing_t = boost::iterator_property_map < straight_line_drawing_storage_t::iterator, property_map::type - > - straight_line_drawing_t; + >; straight_line_drawing_storage_t straight_line_drawing_storage (num_vertices(g)); @@ -121,11 +113,10 @@ int main(int argc, char** argv) std::cout << "The straight line drawing is: " << std::endl; - graph_traits::vertex_iterator vi, vi_end; - for(boost::tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi) + for(const auto& vertex : make_range_pair(vertices(g))) { - coord_t coord(get(straight_line_drawing,*vi)); - std::cout << *vi << " -> (" << coord.x << ", " << coord.y << ")" + coord_t coord(get(straight_line_drawing,vertex)); + std::cout << vertex << " -> (" << coord.x << ", " << coord.y << ")" << std::endl; } diff --git a/example/strong-components.cpp b/example/strong-components.cpp index 251b7166..b2525280 100644 --- a/example/strong-components.cpp +++ b/example/strong-components.cpp @@ -15,7 +15,7 @@ int main() { using namespace boost; - typedef adjacency_list < vecS, vecS, directedS > Graph; + using Graph = adjacency_list; const int N = 6; Graph G(N); add_edge(0, 1, G); @@ -28,12 +28,11 @@ main() add_edge(5, 2, G); std::vector c(N); - int num = strong_components + auto num = strong_components (G, make_iterator_property_map(c.begin(), get(vertex_index, G), c[0])); std::cout << "Total number of components: " << num << std::endl; - std::vector < int >::iterator i; - for (i = c.begin(); i != c.end(); ++i) + for (auto i = c.begin(); i != c.end(); ++i) std::cout << "Vertex " << i - c.begin() << " is in component " << *i << std::endl; return EXIT_SUCCESS; diff --git a/example/strong_components.cpp b/example/strong_components.cpp index 089e3ef9..011f0b91 100644 --- a/example/strong_components.cpp +++ b/example/strong_components.cpp @@ -54,12 +54,12 @@ int main(int, char*[]) print_graph(G, name); std::cout << std::endl; - typedef graph_traits >::vertex_descriptor Vertex; + using Vertex = graph_traits>::vertex_descriptor; std::vector component(num_vertices(G)), discover_time(num_vertices(G)); std::vector color(num_vertices(G)); std::vector root(num_vertices(G)); - int num = strong_components(G, make_iterator_property_map(component.begin(), get(vertex_index, G)), + auto num = strong_components(G, make_iterator_property_map(component.begin(), get(vertex_index, G)), root_map(make_iterator_property_map(root.begin(), get(vertex_index, G))). color_map(make_iterator_property_map(color.begin(), get(vertex_index, G))). discover_time_map(make_iterator_property_map(discover_time.begin(), get(vertex_index, G)))); diff --git a/example/subgraph.cpp b/example/subgraph.cpp index 289ae54d..a4f66775 100644 --- a/example/subgraph.cpp +++ b/example/subgraph.cpp @@ -37,19 +37,20 @@ #include #include #include +#include "range_pair.hpp" int main(int,char*[]) { using namespace boost; - typedef subgraph< adjacency_list, property > > Graph; + using Graph = subgraph, property>>; const int N = 6; Graph G0(N); enum { A, B, C, D, E, F}; // for conveniently refering to vertices in G0 - Graph& G1 = G0.create_subgraph(); - Graph& G2 = G0.create_subgraph(); + auto& G1 = G0.create_subgraph(); + auto& G2 = G0.create_subgraph(); enum { A1, B1, C1 }; // for conveniently refering to vertices in G1 enum { A2, B2 }; // for conveniently refering to vertices in G2 @@ -74,12 +75,11 @@ int main(int,char*[]) print_edges2(G0, get(vertex_index, G0), get(edge_index, G0)); std::cout << std::endl; - Graph::children_iterator ci, ci_end; int num = 1; - for (boost::tie(ci, ci_end) = G0.children(); ci != ci_end; ++ci) { + for (const auto& vertex : make_range_pair(G0.children())) { std::cout << "G" << num++ << ":" << std::endl; - print_graph(*ci, get(vertex_index, *ci)); - print_edges2(*ci, get(vertex_index, *ci), get(edge_index, *ci)); + print_graph(vertex, get(vertex_index, vertex)); + print_edges2(vertex, get(vertex_index, vertex), get(edge_index, vertex)); std::cout << std::endl; } diff --git a/example/subgraph_properties.cpp b/example/subgraph_properties.cpp index 613d53a3..bf5e74f7 100644 --- a/example/subgraph_properties.cpp +++ b/example/subgraph_properties.cpp @@ -38,18 +38,18 @@ int main(int,char*[]) { using namespace boost; - //typedef adjacency_list_traits Traits;// Does nothing? - typedef property< vertex_color_t, int, - property< vertex_name_t, std::string > > VertexProperty; + //using Traits = adjacency_list_traits;// Does nothing? + using VertexProperty = property>; - typedef subgraph< adjacency_list< vecS, vecS, directedS, - VertexProperty, property > > Graph; + using Graph = subgraph>>; const int N = 6; Graph G0(N); enum { A, B, C, D, E, F}; // for conveniently refering to vertices in G0 - property_map::type name = get(vertex_name_t(), G0); + auto name = get(vertex_name_t(), G0); name[A] = "A"; name[B] = "B"; name[C] = "C"; @@ -57,14 +57,14 @@ int main(int,char*[]) name[E] = "E"; name[F] = "F"; - Graph& G1 = G0.create_subgraph(); + auto& G1 = G0.create_subgraph(); enum { A1, B1, C1 }; // for conveniently refering to vertices in G1 add_vertex(C, G1); // global vertex C becomes local A1 for G1 add_vertex(E, G1); // global vertex E becomes local B1 for G1 add_vertex(F, G1); // global vertex F becomes local C1 for G1 - property_map::type name1 = get(vertex_name_t(), G1); + auto name1 = get(vertex_name_t(), G1); name1[A1] = "A1"; std::cout << std::endl << "After initializing properties for G1:" << std::endl; @@ -84,13 +84,13 @@ int main(int,char*[]) std::cout << " G0[F]= " << boost::get(vertex_name, G0, vertex(F, G0)) << std::endl;// prints: "G0[F]= F" std::cout << " G1[C1]= " << boost::get(vertex_name, G1, vertex(C1, G1)) << std::endl;// prints: "G1[C1]= C1" - Graph& G2 = G0.create_subgraph(); + auto& G2 = G0.create_subgraph(); enum { A2, B2 }; // for conveniently refering to vertices in G2 add_vertex(A, G2); // global vertex A becomes local A2 for G2 add_vertex(C, G2); // global vertex C becomes local B2 for G2 - property_map::type name2 = get(vertex_name_t(), G2); + auto name2 = get(vertex_name_t(), G2); name2[A2] = "A2"; std::cout << std::endl << std::endl << "After initializing properties for G2:" << std::endl; diff --git a/example/successive_shortest_path_nonnegative_weights_example.cpp b/example/successive_shortest_path_nonnegative_weights_example.cpp index 0edc3710..22442ae8 100644 --- a/example/successive_shortest_path_nonnegative_weights_example.cpp +++ b/example/successive_shortest_path_nonnegative_weights_example.cpp @@ -20,7 +20,7 @@ int main() { boost::successive_shortest_path_nonnegative_weights(g, s, t); - int cost = boost::find_flow_cost(g); + auto cost = boost::find_flow_cost(g); assert(cost == 29); return 0; diff --git a/example/tiernan_girth_circumference.cpp b/example/tiernan_girth_circumference.cpp index a277a3fd..f33d5cad 100644 --- a/example/tiernan_girth_circumference.cpp +++ b/example/tiernan_girth_circumference.cpp @@ -12,28 +12,26 @@ #include "helper.hpp" -using namespace std; using namespace boost; // Declare the graph type and its vertex and edge types. -typedef directed_graph<> Graph; -typedef graph_traits::vertex_descriptor Vertex; -typedef graph_traits::edge_descriptor Edge; +using Graph = directed_graph<>; +using Vertex = graph_traits::vertex_descriptor; +using Edge = graph_traits::edge_descriptor; int main(int argc, char *argv[]) { // Create the graph and read it from standard input. Graph g; - read_graph(g, cin); + read_graph(g, std::cin); // Compute the girth and circumference simulataneously - size_t girth, circ; - boost::tie(girth, circ) = tiernan_girth_and_circumference(g); + auto [girth, circ] = tiernan_girth_and_circumference(g); // Print the result - cout << "girth: " << girth << endl; - cout << "circumference: " << circ << endl; + std::cout << "girth: " << girth << std::endl; + std::cout << "circumference: " << circ << std::endl; return 0; } diff --git a/example/tiernan_print_cycles.cpp b/example/tiernan_print_cycles.cpp index c4375615..0e08e9d6 100644 --- a/example/tiernan_print_cycles.cpp +++ b/example/tiernan_print_cycles.cpp @@ -12,7 +12,6 @@ #include "helper.hpp" -using namespace std; using namespace boost; // The cycle_printer is a visitor that will print the path that comprises @@ -31,33 +30,31 @@ struct cycle_printer { // Get the property map containing the vertex indices // so we can print them. - typedef typename property_map::const_type IndexMap; - IndexMap indices = get(vertex_index, g); + auto indices = get(vertex_index, g); // Iterate over path printing each vertex that forms the cycle. - typename Path::const_iterator i, end = p.end(); - for(i = p.begin(); i != end; ++i) { - os << get(indices, *i) << " "; + for(const auto& vertex : p) { + os << get(indices, vertex) << " "; } - os << endl; + os << std::endl; } OutputStream& os; }; // Declare the graph type and its vertex and edge types. -typedef directed_graph<> Graph; -typedef graph_traits::vertex_descriptor Vertex; -typedef graph_traits::edge_descriptor Edge; +using Graph = directed_graph<>; +using Vertex = graph_traits::vertex_descriptor; +using Edge = graph_traits::edge_descriptor; int main(int argc, char *argv[]) { // Create the graph and read it from standard input. Graph g; - read_graph(g, cin); + read_graph(g, std::cin); // Instantiate the visitor for printing cycles - cycle_printer vis(cout); + cycle_printer vis(std::cout); // Use the Tiernan algorithm to visit all cycles, printing them // as they are found. diff --git a/example/topo-sort-file-dep.cpp b/example/topo-sort-file-dep.cpp index 98cca5f0..2544523c 100644 --- a/example/topo-sort-file-dep.cpp +++ b/example/topo-sort-file-dep.cpp @@ -11,36 +11,36 @@ #include #include #include +#include "range_pair.hpp" using namespace boost; namespace std { - template < typename T > - std::istream & operator >> (std::istream & in, std::pair < T, T > &p) + template + std::istream & operator >> (std::istream & in, std::pair &p) { in >> p.first >> p.second; return in; } } -typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list +using file_dep_graph = adjacency_list file_dep_graph; +>; -typedef graph_traits < file_dep_graph >::vertex_descriptor vertex_t; -typedef graph_traits < file_dep_graph >::edge_descriptor edge_t; +using vertex_t = graph_traits::vertex_descriptor; +using edge_t = graph_traits::edge_descriptor; void topo_sort_dfs(const file_dep_graph & g, vertex_t u, vertex_t * &topo_order, int *mark) { mark[u] = 1; // 1 means visited, 0 means not yet visited - graph_traits < file_dep_graph >::adjacency_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = adjacent_vertices(u, g); vi != vi_end; ++vi) - if (mark[*vi] == 0) - topo_sort_dfs(g, *vi, topo_order, mark); + for (const auto& vertex : make_range_pair(adjacent_vertices(u, g))) + if (mark[vertex] == 0) + topo_sort_dfs(g, vertex, topo_order, mark); *--topo_order = u; } @@ -48,11 +48,10 @@ topo_sort_dfs(const file_dep_graph & g, vertex_t u, vertex_t * &topo_order, void topo_sort(const file_dep_graph & g, vertex_t * topo_order) { - std::vector < int >mark(num_vertices(g), 0); - graph_traits < file_dep_graph >::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) - if (mark[*vi] == 0) - topo_sort_dfs(g, *vi, topo_order, &mark[0]); + std::vectormark(num_vertices(g), 0); + for (const auto& vertex : make_range_pair(vertices(g))) + if (mark[vertex] == 0) + topo_sort_dfs(g, vertex, topo_order, &mark[0]); } @@ -60,30 +59,28 @@ int main() { std::ifstream file_in("makefile-dependencies.dat"); - typedef graph_traits < file_dep_graph >::vertices_size_type size_type; + using size_type = graph_traits::vertices_size_type; size_type n_vertices; file_in >> n_vertices; // read in number of vertices - std::istream_iterator < std::pair < size_type, size_type > > + std::istream_iterator> input_begin(file_in), input_end; #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 // VC++ can't handle the iterator constructor file_dep_graph g(n_vertices); while (input_begin != input_end) { - size_type i, j; - boost::tie(i, j) = *input_begin++; + auto [i, j] = *input_begin++; add_edge(i, j, g); } #else file_dep_graph g(input_begin, input_end, n_vertices); #endif - std::vector < std::string > name(num_vertices(g)); + std::vector name(num_vertices(g)); std::ifstream name_in("makefile-target-names.dat"); - graph_traits < file_dep_graph >::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) - name_in >> name[*vi]; + for (const auto& vertex : make_range_pair(vertices(g))) + name_in >> name[vertex]; - std::vector < vertex_t > order(num_vertices(g)); + std::vector order(num_vertices(g)); topo_sort(g, &order[0] + num_vertices(g)); for (size_type i = 0; i < num_vertices(g); ++i) std::cout << name[order[i]] << std::endl; diff --git a/example/topo-sort-file-dep2.cpp b/example/topo-sort-file-dep2.cpp index 5d9f2098..616d5e4e 100644 --- a/example/topo-sort-file-dep2.cpp +++ b/example/topo-sort-file-dep2.cpp @@ -11,14 +11,15 @@ #include #include #include +#include "range_pair.hpp" using namespace boost; namespace std { - template < typename T > + template std::istream & - operator >> (std::istream & in, std::pair < T, T > &p) + operator >> (std::istream & in, std::pair &p) { in >> p.first >> p.second; return @@ -26,69 +27,67 @@ namespace std } } -typedef adjacency_list < +using file_dep_graph = adjacency_list < listS, // Store out-edges of each vertex in a std::list vecS, // Store vertex set in a std::vector directedS // The file dependency graph is directed - > file_dep_graph; + >; -typedef graph_traits ::vertex_descriptor vertex_t; -typedef graph_traits ::edge_descriptor edge_t; +using vertex_t = graph_traits ::vertex_descriptor; +using edge_t = graph_traits ::edge_descriptor; -template < typename Visitor > void +template void dfs_v1(const file_dep_graph & g, vertex_t u, default_color_type * color, Visitor vis) { color[u] = gray_color; vis.discover_vertex(u, g); - graph_traits < file_dep_graph >::out_edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei) { - if (color[target(*ei, g)] == white_color) { - vis.tree_edge(*ei, g); - dfs_v1(g, target(*ei, g), color, vis); - } else if (color[target(*ei, g)] == gray_color) - vis.back_edge(*ei, g); + for (const auto& edge : make_range_pair(out_edges(u, g))) { + if (color[target(edge, g)] == white_color) { + vis.tree_edge(edge, g); + dfs_v1(g, target(edge, g), color, vis); + } else if (color[target(edge, g)] == gray_color) + vis.back_edge(edge, g); else - vis.forward_or_cross_edge(*ei, g); + vis.forward_or_cross_edge(edge, g); } color[u] = black_color; vis.finish_vertex(u, g); } -template < typename Visitor > void +template void generic_dfs_v1(const file_dep_graph & g, Visitor vis) { - std::vector < default_color_type > color(num_vertices(g), white_color); - graph_traits < file_dep_graph >::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { - if (color[*vi] == white_color) - dfs_v1(g, *vi, &color[0], vis); + std::vector color(num_vertices(g), white_color); + for (const auto& vertex : make_range_pair(vertices(g))) { + if (color[vertex] == white_color) + dfs_v1(g, vertex, &color[0], vis); } } struct dfs_visitor_default { - template < typename V, typename G > void + template void discover_vertex(V, const G &) { } - template < typename E, typename G > void + template void tree_edge(E, const G &) { } - template < typename E, typename G > void + template void back_edge(E, const G &) { } - template < typename E, typename G > void + template void forward_or_cross_edge(E, const G &) { } - template < typename V, typename G > void + template void finish_vertex(V, const G &) { } @@ -119,31 +118,29 @@ int main() { std::ifstream file_in("makefile-dependencies.dat"); - typedef graph_traits::vertices_size_type size_type; + using size_type = graph_traits::vertices_size_type; size_type n_vertices; file_in >> n_vertices; // read in number of vertices - std::istream_iterator < std::pair < size_type, - size_type > >input_begin(file_in), input_end; + std::istream_iterator> input_begin(file_in), input_end; #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 // VC++ can't handle the iterator constructor file_dep_graph g(n_vertices); while (input_begin != input_end) { - size_type i, j; - boost::tie(i, j) = *input_begin++; + auto [i, j] = *input_begin++; add_edge(i, j, g); } #else file_dep_graph g(input_begin, input_end, n_vertices); #endif - std::vector < std::string > name(num_vertices(g)); + std::vector name(num_vertices(g)); std::ifstream name_in("makefile-target-names.dat"); - graph_traits < file_dep_graph >::vertex_iterator vi, vi_end; - for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) - name_in >> name[*vi]; + for (const auto& vertex : make_range_pair(vertices(g))) + name_in >> name[vertex]; - std::vector < vertex_t > order(num_vertices(g)); + std::vector order(num_vertices(g)); topo_sort(g, &order[0] + num_vertices(g)); for (size_type i = 0; i < num_vertices(g); ++i) std::cout << name[order[i]] << std::endl; diff --git a/example/topo-sort-with-leda.cpp b/example/topo-sort-with-leda.cpp index cd24b76e..11a99110 100644 --- a/example/topo-sort-with-leda.cpp +++ b/example/topo-sort-with-leda.cpp @@ -17,10 +17,10 @@ int main() { using namespace boost; - typedef GRAPH < std::string, char >graph_t; + using graph_t = GRAPH; graph_t leda_g; - typedef graph_traits < graph_t >::vertex_descriptor vertex_t; - std::vector < vertex_t > vert(7); + using vertex_t = graph_traits::vertex_descriptor; + std::vector vert(7); vert[0] = add_vertex(std::string("pick up kids from school"), leda_g); vert[1] = add_vertex(std::string("buy groceries (and snacks)"), leda_g); vert[2] = add_vertex(std::string("get cash at ATM"), leda_g); @@ -38,17 +38,16 @@ main() add_edge(vert[4], vert[6], leda_g); add_edge(vert[5], vert[6], leda_g); - std::vector < vertex_t > topo_order; - node_array < default_color_type > color_array(leda_g); + std::vector topo_order; + node_array color_array(leda_g); topological_sort(leda_g, std::back_inserter(topo_order), color_map(make_leda_node_property_map(color_array))); std::reverse(topo_order.begin(), topo_order.end()); int n = 1; - for (std::vector < vertex_t >::iterator i = topo_order.begin(); - i != topo_order.end(); ++i, ++n) - std::cout << n << ": " << leda_g[*i] << std::endl; + for (const auto& vertex : topo_order) + std::cout << n << ": " << leda_g[vertex] << std::endl; return EXIT_SUCCESS; } diff --git a/example/topo-sort-with-sgb.cpp b/example/topo-sort-with-sgb.cpp index 9b0ea8e5..311a32f7 100644 --- a/example/topo-sort-with-sgb.cpp +++ b/example/topo-sort-with-sgb.cpp @@ -16,7 +16,7 @@ main() { using namespace boost; const int n_vertices = 7; - Graph *sgb_g = gb_new_graph(n_vertices); + auto sgb_g = gb_new_graph(n_vertices); const char *tasks[] = { "pick up kids from school", @@ -37,12 +37,12 @@ main() gb_new_arc(sgb_g->vertices + 4, sgb_g->vertices + 6, 0); gb_new_arc(sgb_g->vertices + 5, sgb_g->vertices + 6, 0); - typedef graph_traits < Graph * >::vertex_descriptor vertex_t; - std::vector < vertex_t > topo_order; + using vertex_t = graph_traits::vertex_descriptor; + std::vector topo_order; topological_sort(sgb_g, std::back_inserter(topo_order), vertex_index_map(get(vertex_index, sgb_g))); int n = 1; - for (std::vector < vertex_t >::reverse_iterator i = topo_order.rbegin(); + for (auto i = topo_order.rbegin(); i != topo_order.rend(); ++i, ++n) std::cout << n << ": " << tasks[get(vertex_index, sgb_g)[*i]] << std::endl; diff --git a/example/topo-sort1.cpp b/example/topo-sort1.cpp index 90c98a2d..79903503 100644 --- a/example/topo-sort1.cpp +++ b/example/topo-sort1.cpp @@ -25,26 +25,23 @@ main() "pick up kids from soccer", "eat dinner" }; - const int n_tasks = sizeof(tasks) / sizeof(char *); - std::vector < std::list < int > > g(n_tasks); - g[0].push_back(3); - g[1].push_back(3); - g[1].push_back(4); - g[2].push_back(1); - g[3].push_back(5); - g[4].push_back(6); - g[5].push_back(6); + std::vector> g = { + {3}, + {3, 4}, + {1}, + {5}, + {6}, + {6}, + {}}; - std::deque < int >topo_order; + std::deque topo_order; topological_sort(g, std::front_inserter(topo_order), vertex_index_map(identity_property_map())); - int n = 1; - for (std::deque < int >::iterator i = topo_order.begin(); - i != topo_order.end(); ++i, ++n) - std::cout << tasks[*i] << std::endl; + for (const auto& vertex : topo_order) + std::cout << tasks[vertex] << std::endl; return EXIT_SUCCESS; } diff --git a/example/topo-sort2.cpp b/example/topo-sort2.cpp index 4663181e..2d004fea 100644 --- a/example/topo-sort2.cpp +++ b/example/topo-sort2.cpp @@ -26,7 +26,7 @@ main() }; const int n_tasks = sizeof(tasks) / sizeof(char *); - adjacency_list < listS, vecS, directedS > g(n_tasks); + adjacency_list g(n_tasks); add_edge(0, 3, g); add_edge(1, 3, g); @@ -36,15 +36,13 @@ main() add_edge(4, 6, g); add_edge(5, 6, g); - std::deque < int >topo_order; + std::deque topo_order; topological_sort(g, std::front_inserter(topo_order), vertex_index_map(identity_property_map())); - int n = 1; - for (std::deque < int >::iterator i = topo_order.begin(); - i != topo_order.end(); ++i, ++n) - std::cout << tasks[*i] << std::endl; + for (const auto& vertex : topo_order) + std::cout << tasks[vertex] << std::endl; return EXIT_SUCCESS; } diff --git a/example/topo_sort.cpp b/example/topo_sort.cpp index 124db6a2..1a5c048d 100644 --- a/example/topo_sort.cpp +++ b/example/topo_sort.cpp @@ -16,7 +16,7 @@ #include -typedef std::pair Pair; +using Pair = std::pair; /* Topological sort example @@ -40,31 +40,28 @@ main(int , char* []) /* Topological sort will need to color the graph. Here we use an internal decorator, so we "property" the color to the graph. */ - typedef adjacency_list > Graph; - - typedef boost::graph_traits::vertex_descriptor Vertex; - Pair edges[6] = { Pair(0,1), Pair(2,4), - Pair(2,5), - Pair(0,3), Pair(1,4), - Pair(4,3) }; -#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 - // VC++ can't handle the iterator constructor - Graph G(6); - for (std::size_t j = 0; j < 6; ++j) - add_edge(edges[j].first, edges[j].second, G); -#else - Graph G(edges, edges + 6, 6); -#endif - - boost::property_map::type id = get(vertex_index, G); - - typedef std::vector< Vertex > container; + using Graph = adjacency_list>; + + using Vertex = boost::graph_traits::vertex_descriptor; + + const auto edges = { + Pair(0, 1), + Pair(2, 4), + Pair(2, 5), + Pair(0, 3), + Pair(1, 4), + Pair(4, 3) }; + Graph G(std::begin(edges), std::end(edges), 6 /* vertices count */); + + auto id = get(vertex_index, G); + + using container = std::vector; container c; topological_sort(G, std::back_inserter(c)); std::cout << "A topological ordering: "; - for (container::reverse_iterator ii = c.rbegin(); + for (auto ii = c.rbegin(); ii != c.rend(); ++ii) std::cout << id[*ii] << " "; std::cout << std::endl; diff --git a/example/transitive_closure.cpp b/example/transitive_closure.cpp index 2b99cdb9..2f30160e 100644 --- a/example/transitive_closure.cpp +++ b/example/transitive_closure.cpp @@ -18,12 +18,12 @@ int main(int, char *[]) { using namespace boost; - typedef property < vertex_name_t, char >Name; - typedef property < vertex_index_t, std::size_t, Name > Index; - typedef adjacency_list < listS, listS, directedS, Index > graph_t; - typedef graph_traits < graph_t >::vertex_descriptor vertex_t; + using Name = property; + using Index = property; + using graph_t = adjacency_list; + using vertex_t = graph_traits::vertex_descriptor; graph_t G; - std::vector < vertex_t > verts(4); + std::vector verts(4); for (int i = 0; i < 4; ++i) verts[i] = add_vertex(Index(i, Name('a' + i)), G); add_edge(verts[1], verts[2], G); diff --git a/example/transpose-example.cpp b/example/transpose-example.cpp index 7378dd19..5003abd8 100644 --- a/example/transpose-example.cpp +++ b/example/transpose-example.cpp @@ -10,29 +10,28 @@ #include #include #include +#include "range_pair.hpp" int main() { using namespace boost; - typedef adjacency_list < vecS, vecS, bidirectionalS, - property < vertex_name_t, char > > graph_t; + using graph_t = adjacency_list>; enum { a, b, c, d, e, f, g, N }; graph_t G(N); - property_map < graph_t, vertex_name_t >::type - name_map = get(vertex_name, G); + auto name_map = get(vertex_name, G); char name = 'a'; - graph_traits < graph_t >::vertex_iterator v, v_end; - for (boost::tie(v, v_end) = vertices(G); v != v_end; ++v, ++name) - name_map[*v] = name; + for (const auto& vertex : make_range_pair(vertices(G))) + name_map[vertex] = name; - typedef std::pair < int, int >E; + using E = std::pair; E edge_array[] = { E(a, c), E(a, d), E(b, a), E(b, d), E(c, f), E(d, c), E(d, e), E(d, f), E(e, b), E(e, g), E(f, e), E(f, g) }; - for (int i = 0; i < 12; ++i) - add_edge(edge_array[i].first, edge_array[i].second, G); + for (const auto& edge : edge_array) + add_edge(edge.first, edge.second, G); print_graph(G, name_map); std::cout << std::endl; @@ -42,8 +41,7 @@ main() print_graph(G_T, name_map); - graph_traits < graph_t >::edge_iterator ei, ei_end; - for (boost::tie(ei, ei_end) = edges(G); ei != ei_end; ++ei) - assert(edge(target(*ei, G), source(*ei, G), G_T).second == true); + for (const auto& e : make_range_pair(edges(G))) + assert(edge(target(e, G), source(e, G), G_T).second == true); return 0; } diff --git a/example/two_graphs_common_spanning_trees.cpp b/example/two_graphs_common_spanning_trees.cpp index 2fa85179..1671a5c1 100644 --- a/example/two_graphs_common_spanning_trees.cpp +++ b/example/two_graphs_common_spanning_trees.cpp @@ -14,10 +14,7 @@ #include #include - -using namespace std; - -typedef +using Graph = boost::adjacency_list < boost::vecS, // OutEdgeList @@ -27,52 +24,45 @@ boost::adjacency_list boost::no_property, // EdgeProperties boost::no_property, // GraphProperties boost::listS // EdgeList - > -Graph -; + >; -typedef -boost::graph_traits::vertex_descriptor -vertex_descriptor; +using vertex_descriptor = +boost::graph_traits::vertex_descriptor; -typedef -boost::graph_traits::edge_descriptor -edge_descriptor; +using edge_descriptor = +boost::graph_traits::edge_descriptor; -typedef -boost::graph_traits::vertex_iterator -vertex_iterator; +using vertex_iterator = +boost::graph_traits::vertex_iterator; -typedef -boost::graph_traits::edge_iterator -edge_iterator; +using edge_iterator = +boost::graph_traits::edge_iterator; int main(int argc, char **argv) { Graph iG, vG; - vector< edge_descriptor > iG_o; - vector< edge_descriptor > vG_o; - - iG_o.push_back(boost::add_edge(0, 1, iG).first); - iG_o.push_back(boost::add_edge(0, 2, iG).first); - iG_o.push_back(boost::add_edge(0, 3, iG).first); - iG_o.push_back(boost::add_edge(0, 4, iG).first); - iG_o.push_back(boost::add_edge(1, 2, iG).first); - iG_o.push_back(boost::add_edge(3, 4, iG).first); - - vG_o.push_back(boost::add_edge(1, 2, vG).first); - vG_o.push_back(boost::add_edge(2, 0, vG).first); - vG_o.push_back(boost::add_edge(2, 3, vG).first); - vG_o.push_back(boost::add_edge(4, 3, vG).first); - vG_o.push_back(boost::add_edge(0, 3, vG).first); - vG_o.push_back(boost::add_edge(0, 4, vG).first); - - vector inL(iG_o.size(), false); - - std::vector< std::vector > coll; + std::vector iG_o = { + boost::add_edge(0, 1, iG).first, + boost::add_edge(0, 2, iG).first, + boost::add_edge(0, 3, iG).first, + boost::add_edge(0, 4, iG).first, + boost::add_edge(1, 2, iG).first, + boost::add_edge(3, 4, iG).first}; + + std::vector vG_o= { + boost::add_edge(1, 2, vG).first, + boost::add_edge(2, 0, vG).first, + boost::add_edge(2, 3, vG).first, + boost::add_edge(4, 3, vG).first, + boost::add_edge(0, 3, vG).first, + boost::add_edge(0, 4, vG).first}; + + std::vector inL(iG_o.size(), false); + + std::vector> coll; boost::tree_collector< - std::vector< std::vector >, + std::vector>, std::vector > tree_collector(coll); boost::two_graphs_common_spanning_trees @@ -84,11 +74,12 @@ int main(int argc, char **argv) tree_collector, inL ); - - std::vector< std::vector >::iterator it; - for(it = coll.begin(); it != coll.end(); ++it) { - // Here you can play with the trees that the algorithm has found. + + // Here you can play with the trees that the algorithm has found. + /* + for(const auto& vec : coll) { } + */ return 0; } diff --git a/example/undirected_adjacency_list.cpp b/example/undirected_adjacency_list.cpp index 49d31202..8328eccc 100644 --- a/example/undirected_adjacency_list.cpp +++ b/example/undirected_adjacency_list.cpp @@ -8,51 +8,46 @@ #include #include #include +#include "range_pair.hpp" + using namespace boost; -template < typename UndirectedGraph > void +template void undirected_graph_demo1() { const int V = 3; UndirectedGraph undigraph(V); - typename graph_traits < UndirectedGraph >::vertex_descriptor zero, one, two; - typename graph_traits < UndirectedGraph >::out_edge_iterator out, out_end; - typename graph_traits < UndirectedGraph >::in_edge_iterator in, in_end; - zero = vertex(0, undigraph); - one = vertex(1, undigraph); - two = vertex(2, undigraph); + auto zero = vertex(0, undigraph); + auto one = vertex(1, undigraph); + auto two = vertex(2, undigraph); add_edge(zero, one, undigraph); add_edge(zero, two, undigraph); add_edge(one, two, undigraph); std::cout << "out_edges(0):"; - for (boost::tie(out, out_end) = out_edges(zero, undigraph); out != out_end; ++out) - std::cout << ' ' << *out; + for (const auto& edge : make_range_pair(out_edges(zero, undigraph))) + std::cout << ' ' << edge; std::cout << std::endl << "in_edges(0):"; - for (boost::tie(in, in_end) = in_edges(zero, undigraph); in != in_end; ++in) - std::cout << ' ' << *in; + for (const auto& edge : make_range_pair(in_edges(zero, undigraph))) + std::cout << ' ' << edge; std::cout << std::endl; } -template < typename DirectedGraph > void +template void directed_graph_demo() { const int V = 2; DirectedGraph digraph(V); - typename graph_traits < DirectedGraph >::vertex_descriptor u, v; - typedef typename DirectedGraph::edge_property_type Weight; - typename property_map < DirectedGraph, edge_weight_t >::type - weight = get(edge_weight, digraph); - typename graph_traits < DirectedGraph >::edge_descriptor e1, e2; - bool found; + using Weight = typename DirectedGraph::edge_property_type; + auto weight = get(edge_weight, digraph); - u = vertex(0, digraph); - v = vertex(1, digraph); + auto u = vertex(0, digraph); + auto v = vertex(1, digraph); add_edge(u, v, Weight(1.2), digraph); add_edge(v, u, Weight(2.4), digraph); - boost::tie(e1, found) = edge(u, v, digraph); - boost::tie(e2, found) = edge(v, u, digraph); + auto [e1, found1] = edge(u, v, digraph); + auto [e2, found2] = edge(v, u, digraph); std::cout << "in a directed graph is "; #ifdef __GNUC__ // no boolalpha @@ -65,23 +60,19 @@ directed_graph_demo() std::cout << "weight[(v,u)] = " << get(weight, e2) << std::endl; } -template < typename UndirectedGraph > void +template void undirected_graph_demo2() { const int V = 2; UndirectedGraph undigraph(V); - typename graph_traits < UndirectedGraph >::vertex_descriptor u, v; - typedef typename UndirectedGraph::edge_property_type Weight; - typename property_map < UndirectedGraph, edge_weight_t >::type - weight = get(edge_weight, undigraph); - typename graph_traits < UndirectedGraph >::edge_descriptor e1, e2; - bool found; + using Weight = typename UndirectedGraph::edge_property_type; + auto weight = get(edge_weight, undigraph); - u = vertex(0, undigraph); - v = vertex(1, undigraph); + auto u = vertex(0, undigraph); + auto v = vertex(1, undigraph); add_edge(u, v, Weight(3.1), undigraph); - boost::tie(e1, found) = edge(u, v, undigraph); - boost::tie(e2, found) = edge(v, u, undigraph); + auto [e1, found1] = edge(u, v, undigraph); + auto [e2, found2] = edge(v, u, undigraph); std::cout << "in an undirected graph is "; #ifdef __GNUC__ std::cout << "(u,v) == (v,u) ? " << (e1 == e2) << std::endl; @@ -93,25 +84,23 @@ undirected_graph_demo2() std::cout << "weight[(v,u)] = " << get(weight, e2) << std::endl; std::cout << "the edges incident to v: "; - typename boost::graph_traits::out_edge_iterator e, e_end; - typename boost::graph_traits::vertex_descriptor - s = vertex(0, undigraph); - for (boost::tie(e, e_end) = out_edges(s, undigraph); e != e_end; ++e) - std::cout << "(" << source(*e, undigraph) - << "," << target(*e, undigraph) << ")" << std::endl; + auto s = vertex(0, undigraph); + for (const auto& edge : make_range_pair(out_edges(s, undigraph))) + std::cout << "(" << source(edge, undigraph) + << "," << target(edge, undigraph) << ")" << std::endl; } int main() { - typedef property < edge_weight_t, double >Weight; - typedef adjacency_list < vecS, vecS, undirectedS, - no_property, Weight > UndirectedGraph; - typedef adjacency_list < vecS, vecS, directedS, - no_property, Weight > DirectedGraph; - undirected_graph_demo1 < UndirectedGraph > (); - directed_graph_demo < DirectedGraph > (); - undirected_graph_demo2 < UndirectedGraph > (); + using Weight = property < edge_weight_t, double >; + using UndirectedGraph = adjacency_list < vecS, vecS, undirectedS, + no_property, Weight >; + using DirectedGraph = adjacency_list < vecS, vecS, directedS, + no_property, Weight >; + undirected_graph_demo1 (); + directed_graph_demo (); + undirected_graph_demo2 (); return 0; } diff --git a/example/undirected_dfs.cpp b/example/undirected_dfs.cpp index bc314d39..fffac592 100644 --- a/example/undirected_dfs.cpp +++ b/example/undirected_dfs.cpp @@ -45,10 +45,10 @@ struct detect_loops : public boost::dfs_visitor<> int main(int, char*[]) { using namespace boost; - typedef adjacency_list< vecS, vecS, undirectedS, + using graph_t = adjacency_list< vecS, vecS, undirectedS, no_property, - property > graph_t; - typedef graph_traits::vertex_descriptor vertex_t; + property>; + using vertex_t = graph_traits::vertex_descriptor; const std::size_t N = sizeof(name)/sizeof(std::string); graph_t g(N); diff --git a/example/undirected_graph.cpp b/example/undirected_graph.cpp index d926c7b5..d75ad94d 100644 --- a/example/undirected_graph.cpp +++ b/example/undirected_graph.cpp @@ -10,7 +10,7 @@ #include #include -typedef boost::undirected_graph Graph; +using Graph = boost::undirected_graph; int main(int,char*[]) { @@ -18,9 +18,9 @@ int main(int,char*[]) Graph g; // Add vertices - boost::graph_traits::vertex_descriptor v0 = g.add_vertex(); - boost::graph_traits::vertex_descriptor v1 = g.add_vertex(); - boost::graph_traits::vertex_descriptor v2 = g.add_vertex(); + auto v0 = g.add_vertex(); + auto v1 = g.add_vertex(); + auto v2 = g.add_vertex(); // Add edges g.add_edge(v0, v1); diff --git a/example/vector_as_graph.cpp b/example/vector_as_graph.cpp index ccf6abeb..b62dd7e7 100644 --- a/example/vector_as_graph.cpp +++ b/example/vector_as_graph.cpp @@ -20,20 +20,18 @@ int main() { enum - { r, s, t, u, v, w, x, y, N }; + { r, s, t, u, v, w, x, y }; char name[] = "rstuvwxy"; - typedef std::vector < std::list < int > > Graph; - Graph g(N); - g[r].push_back(v); - g[s].push_back(r); - g[s].push_back(r); - g[s].push_back(w); - g[t].push_back(x); - g[u].push_back(t); - g[w].push_back(t); - g[w].push_back(x); - g[x].push_back(y); - g[y].push_back(u); + using Graph = std::vector>; + Graph g = { + {v}, //r + {r, r, w}, //s + {x}, //t + {t}, //u + {}, //v + {t, x}, //w + {y}, //x + {u}}; //y boost::print_graph(g, name); return 0; } diff --git a/example/vertex-name-property.cpp b/example/vertex-name-property.cpp index c1216f04..9024a0bb 100644 --- a/example/vertex-name-property.cpp +++ b/example/vertex-name-property.cpp @@ -13,14 +13,14 @@ using namespace boost; -template < typename Graph, typename VertexNamePropertyMap > void +template void read_graph_file(std::istream & graph_in, std::istream & name_in, Graph & g, VertexNamePropertyMap name_map) { - typedef typename graph_traits < Graph >::vertices_size_type size_type; + using size_type = typename graph_traits::vertices_size_type; size_type n_vertices; - typename graph_traits < Graph >::vertex_descriptor u; - typename property_traits < VertexNamePropertyMap >::value_type name; + typename graph_traits::vertex_descriptor u; + typename property_traits::value_type name; graph_in >> n_vertices; // read in number of vertices for (size_type i = 0; i < n_vertices; ++i) { // Add n vertices to the graph @@ -40,11 +40,11 @@ read_graph_file(std::istream & graph_in, std::istream & name_in, int main() { - typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list + using graph_type = adjacency_list < listS, // Store out-edges of each vertex in a std::list vecS, // Store vertex set in a std::vector directedS, // The graph is directed - property < vertex_name_t, std::string > // Add a vertex property - >graph_type; + property // Add a vertex property + >; graph_type g; // use default constructor to create empty graph const char* dep_file_name = "makefile-dependencies.dat"; @@ -62,15 +62,14 @@ main() } // Obtain internal property map from the graph - property_map < graph_type, vertex_name_t >::type name_map = - get(vertex_name, g); + auto name_map = get(vertex_name, g); read_graph_file(file_in, name_in, g, name_map); // Create storage for last modified times - std::vector < time_t > last_mod_vec(num_vertices(g)); + std::vector last_mod_vec(num_vertices(g)); // Create nickname for the property map type - typedef iterator_property_map < std::vector < time_t >::iterator, - property_map < graph_type, vertex_index_t >::type, time_t, time_t& > iter_map_t; + using iter_map_t = iterator_property_map < std::vector::iterator, + property_map::type, time_t, time_t&>; // Create last modified time property map iter_map_t mod_time_map(last_mod_vec.begin(), get(vertex_index, g)); diff --git a/example/vertex_basics.cpp b/example/vertex_basics.cpp index 8d2053fb..d77ca407 100644 --- a/example/vertex_basics.cpp +++ b/example/vertex_basics.cpp @@ -11,8 +11,8 @@ #include #include #include +#include "range_pair.hpp" -using namespace std; using namespace boost; @@ -59,17 +59,16 @@ template struct print_edge { print_edge(Graph& g) : G(g) { } - typedef typename boost::graph_traits::edge_descriptor Edge; - typedef typename boost::graph_traits::vertex_descriptor Vertex; + using Edge = typename boost::graph_traits::edge_descriptor; + using Vertex = typename boost::graph_traits::vertex_descriptor; void operator()(Edge e) const { - typename boost::property_map::type - id = get(vertex_index, G); + auto id = get(vertex_index, G); - Vertex src = source(e, G); - Vertex targ = target(e, G); + auto src = source(e, G); + auto targ = target(e, G); - cout << "(" << id[src] << "," << id[targ] << ") "; + std::cout << "(" << id[src] << "," << id[targ] << ") "; } Graph& G; @@ -79,12 +78,11 @@ template struct print_index { print_index(Graph& g) : G(g){ } - typedef typename boost::graph_traits::vertex_descriptor Vertex; + using Vertex = typename boost::graph_traits::vertex_descriptor; void operator()(Vertex c) const { - typename boost::property_map::type - id = get(vertex_index, G); - cout << id[c] << " "; + auto id = get(vertex_index, G); + std::cout << id[c] << " "; } Graph& G; @@ -93,33 +91,32 @@ struct print_index { template struct exercise_vertex { - typedef typename boost::graph_traits::vertex_descriptor Vertex; + using Vertex = typename boost::graph_traits::vertex_descriptor; exercise_vertex(Graph& _g) : g(_g) { } void operator()(Vertex v) const { - typename boost::property_map::type - id = get(vertex_index, g); + auto id = get(vertex_index, g); - cout << "vertex id: " << id[v] << endl; + std::cout << "vertex id: " << id[v] << std::endl; - cout << "out-edges: "; - for_each(out_edges(v, g).first, out_edges(v,g).second, + std::cout << "out-edges: "; + std::for_each(out_edges(v, g).first, out_edges(v,g).second, print_edge(g)); - cout << endl; + std::cout << std::endl; - cout << "in-edges: "; - for_each(in_edges(v, g).first, in_edges(v,g).second, + std::cout << "in-edges: "; + std::for_each(in_edges(v, g).first, in_edges(v,g).second, print_edge(g)); - cout << endl; + std::cout << std::endl; - cout << "adjacent vertices: "; - for_each(adjacent_vertices(v,g).first, + std::cout << "adjacent vertices: "; + std::for_each(adjacent_vertices(v,g).first, adjacent_vertices(v,g).second, print_index(g)); - cout << endl << endl; + std::cout << std::endl << std::endl; } Graph& g; @@ -129,30 +126,28 @@ struct exercise_vertex { int main() { - typedef adjacency_list MyGraphType; + using MyGraphType = adjacency_list; - typedef pair Pair; - Pair edge_array[11] = { Pair(0,1), Pair(0,2), Pair(0,3), Pair(0,4), + using Pair = std::pair; + Pair edge_array[] = { Pair(0,1), Pair(0,2), Pair(0,3), Pair(0,4), Pair(2,0), Pair(3,0), Pair(2,4), Pair(3,1), Pair(3,4), Pair(4,0), Pair(4,1) }; /* Construct a graph using the edge_array*/ MyGraphType g(5); - for (int i=0; i<11; ++i) - add_edge(edge_array[i].first, edge_array[i].second, g); + for (const auto& edge : edge_array) + add_edge(edge.first, edge.second, g); - boost::property_map::type - id = get(vertex_index, g); + auto id = get(vertex_index, g); - cout << "vertices(g) = "; - boost::graph_traits::vertex_iterator vi; - for (vi = vertices(g).first; vi != vertices(g).second; ++vi) - std::cout << id[*vi] << " "; + std::cout << "vertices(g) = "; + for (const auto& vertex : make_range_pair(vertices(g))) + std::cout << id[vertex] << " "; std::cout << std::endl; /* Use the STL for_each algorithm to "exercise" all of the vertices in the graph */ - for_each(vertices(g).first, vertices(g).second, + std::for_each(vertices(g).first, vertices(g).second, exercise_vertex(g)); return 0; diff --git a/example/vf2_sub_graph_iso_example.cpp b/example/vf2_sub_graph_iso_example.cpp index 8e17d2a7..c6b1010c 100644 --- a/example/vf2_sub_graph_iso_example.cpp +++ b/example/vf2_sub_graph_iso_example.cpp @@ -13,7 +13,7 @@ using namespace boost; int main() { - typedef adjacency_list graph_type; + using graph_type = adjacency_list; // Build graph1 int num_vertices1 = 8; graph_type graph1(num_vertices1); diff --git a/example/vf2_sub_graph_iso_multi_example.cpp b/example/vf2_sub_graph_iso_multi_example.cpp index 6f9e34a2..1a71c600 100644 --- a/example/vf2_sub_graph_iso_multi_example.cpp +++ b/example/vf2_sub_graph_iso_multi_example.cpp @@ -11,11 +11,11 @@ using namespace boost; int main() { - typedef property edge_property; - typedef property > vertex_property; + using edge_property = property; + using vertex_property = property>; // Using a vecS graphs => the index maps are implicit. - typedef adjacency_list graph_type; + using graph_type = adjacency_list; // Build graph1 graph_type graph1; @@ -65,14 +65,9 @@ int main() { add_edge(5, 0, edge_property('s'), graph2); // create predicates - typedef property_map::type vertex_name_map_t; - typedef property_map_equivalent vertex_comp_t; - vertex_comp_t vertex_comp = + auto vertex_comp = make_property_map_equivalent(get(vertex_name, graph1), get(vertex_name, graph2)); - - typedef property_map::type edge_name_map_t; - typedef property_map_equivalent edge_comp_t; - edge_comp_t edge_comp = + auto edge_comp = make_property_map_equivalent(get(edge_name, graph1), get(edge_name, graph2)); // Create callback diff --git a/example/visitor.cpp b/example/visitor.cpp index cd5b4f5a..ae2f259e 100644 --- a/example/visitor.cpp +++ b/example/visitor.cpp @@ -45,11 +45,10 @@ #include using namespace boost; -using namespace std; template -struct edge_printer : public base_visitor > { - typedef Tag event_filter; +struct edge_printer : public base_visitor> { + using event_filter = Tag; edge_printer(std::string edge_t) : m_edge_type(edge_t) { } template void operator()(Edge e, Graph& G) { @@ -69,34 +68,28 @@ main(int, char*[]) using namespace boost; - typedef adjacency_list<> Graph; - typedef std::pair E; - E edges[] = { E(0, 2), + using Graph = adjacency_list<>; + using E = std::pair; + const auto edges = { E(0, 2), E(1, 1), E(1, 3), E(2, 1), E(2, 3), E(3, 1), E(3, 4), E(4, 0), E(4, 1) }; -#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 - Graph G(5); - for (std::size_t j = 0; j < sizeof(edges)/sizeof(E); ++j) - add_edge(edges[j].first, edges[j].second, G); -#else - Graph G(edges, edges + sizeof(edges)/sizeof(E), 5); -#endif + Graph G(std::begin(edges), std::end(edges), 5); - typedef boost::graph_traits::vertices_size_type size_type; + using size_type = boost::graph_traits::vertices_size_type; std::vector d(num_vertices(G)); std::vector f(num_vertices(G)); - cout << "DFS categorized directed graph" << endl; + std::cout << "DFS categorized directed graph" << std::endl; depth_first_search(G, visitor(make_dfs_visitor( make_list(print_edge("tree", on_tree_edge()), print_edge("back", on_back_edge()), print_edge("forward or cross", on_forward_or_cross_edge()) )))); - cout << endl << "BFS categorized directed graph" << endl; + std::cout << std::endl << "BFS categorized directed graph" << std::endl; boost::breadth_first_search (G, vertex(0, G), visitor(make_bfs_visitor( std::make_pair(print_edge("tree", on_tree_edge()), diff --git a/example/write_graphviz.cpp b/example/write_graphviz.cpp index 890f08cf..313b9275 100644 --- a/example/write_graphviz.cpp +++ b/example/write_graphviz.cpp @@ -25,7 +25,7 @@ const char* name[] = { "dax.h", "yow.h", "boz.h", "zow.h", "foo.cpp", int main(int,char*[]) { - typedef std::pair Edge; + using Edge = std::pair; Edge used_by[] = { Edge(dax_h, foo_cpp), Edge(dax_h, bar_cpp), Edge(dax_h, yow_h), Edge(yow_h, bar_cpp), Edge(yow_h, zag_cpp), @@ -48,10 +48,10 @@ int main(int,char*[]) using namespace boost; - typedef adjacency_list< vecS, vecS, directedS, - property< vertex_color_t, default_color_type >, - property< edge_weight_t, int > - > Graph; + using Graph = adjacency_list, + property + >; Graph g(used_by, used_by + nedges, weights, N); write_graphviz(std::cout, g, make_label_writer(name)); diff --git a/include/boost/graph/astar_search.hpp b/include/boost/graph/astar_search.hpp index 435ccf03..1e4fa34d 100644 --- a/include/boost/graph/astar_search.hpp +++ b/include/boost/graph/astar_search.hpp @@ -325,7 +325,7 @@ namespace boost { bool decreased = relax(e, g, weight, predecessor, distance, combine, compare); - Distance w_d = combine(get(distance, v), e_weight); + combine(get(distance, v), e_weight); if (decreased) { vis.edge_relaxed(e, g); Distance w_rank = combine(get(distance, w), h(w)); diff --git a/include/boost/graph/bc_clustering.hpp b/include/boost/graph/bc_clustering.hpp index beba4d8e..71deee6f 100644 --- a/include/boost/graph/bc_clustering.hpp +++ b/include/boost/graph/bc_clustering.hpp @@ -112,8 +112,6 @@ betweenness_centrality_clustering(MutableGraph& g, Done done, { typedef typename property_traits::value_type centrality_type; - typedef typename graph_traits::edge_iterator edge_iterator; - typedef typename graph_traits::edge_descriptor edge_descriptor; if (has_no_edges(g)) return; @@ -126,8 +124,8 @@ betweenness_centrality_clustering(MutableGraph& g, Done done, brandes_betweenness_centrality(g, edge_centrality_map(edge_centrality) .vertex_index_map(vertex_index)); - std::pair edges_iters = edges(g); - edge_descriptor e = *max_element(edges_iters.first, edges_iters.second, cmp); + auto edges_iters = edges(g); + auto e = *max_element(edges_iters.first, edges_iters.second, cmp); is_done = done(get(edge_centrality, e), e, g); if (!is_done) remove_edge(e, g); } while (!is_done && !has_no_edges(g));