examples: C++11: Use auto. · murraycu/graph@bfaddb5 (original) (raw)
108 files changed
lines changed
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -69,12 +69,10 @@ main() | ||
| 69 | 69 | file_dep_graph2 g(input_begin, input_end, n_vertices); |
| 70 | 70 | #endif |
| 71 | 71 | |
| 72 | -typedef property_map < file_dep_graph2, vertex_name_t >::type name_map_t; | |
| 73 | -typedef property_map < file_dep_graph2, vertex_compile_cost_t >::type | |
| 74 | -compile_cost_map_t; | |
| 75 | - | |
| 76 | -name_map_t name_map = get(vertex_name, g); | |
| 77 | -compile_cost_map_t compile_cost_map = get(vertex_compile_cost, g); | |
| 72 | +auto name_map = get(vertex_name, g); | |
| 73 | +auto compile_cost_map = get(vertex_compile_cost, g); | |
| 74 | +auto distance_map = get(vertex_distance, g); | |
| 75 | +auto color_map = get(vertex_color, g); | |
| 78 | 76 | |
| 79 | 77 | std::ifstream name_in("makefile-target-names.dat"); |
| 80 | 78 | std::ifstream compile_cost_in("target-compile-costs.dat"); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -50,21 +50,21 @@ void load_actor_graph(std::istream& in, ActorGraph& g) | ||
| 50 | 50 | // Map from the actor numbers on this line to the actor vertices |
| 51 | 51 | typedef tokenizer<char_separator<char> > Tok; |
| 52 | 52 | Tok tok(line, char_separator<char>(" ")); |
| 53 | -for (Tok::iterator id = tok.begin(); id != tok.end(); ++id) { | |
| 54 | -int actor_id = lexical_cast<int>(*id); | |
| 55 | -std::map<int, Vertex>::iterator v = actors.find(actor_id); | |
| 53 | +for (const auto& id : tok) { | |
| 54 | +auto actor_id = lexical_cast<int>(id); | |
| 55 | +auto v = actors.find(actor_id); | |
| 56 | 56 | if (v == actors.end()) { |
| 57 | -Vertex new_vertex = add_vertex(Actor(actor_id), g); | |
| 57 | +auto new_vertex = add_vertex(Actor(actor_id), g); | |
| 58 | 58 | actors[actor_id] = new_vertex; |
| 59 | 59 | actors_in_movie.push_back(new_vertex); |
| 60 | 60 | } else { |
| 61 | 61 | actors_in_movie.push_back(v->second); |
| 62 | 62 | } |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | -for (std::vector::iterator i = actors_in_movie.begin(); | |
| 65 | +for (auto i = actors_in_movie.begin(); | |
| 66 | 66 | i != actors_in_movie.end(); ++i) { |
| 67 | -for (std::vector::iterator j = i + 1; | |
| 67 | +for (auto j = i + 1; | |
| 68 | 68 | j != actors_in_movie.end(); ++j) { |
| 69 | 69 | if (!edge(*i, *j, g).second) add_edge(*i, *j, g); |
| 70 | 70 | } |
| @@ -78,14 +78,12 @@ write_pajek_graph(std::ostream& out, const Graph& g, | ||
| 78 | 78 | VertexIndexMap vertex_index, VertexNameMap vertex_name) |
| 79 | 79 | { |
| 80 | 80 | out << "*Vertices " << num_vertices(g) << '\n'; |
| 81 | -typedef typename graph_traits::vertex_iterator vertex_iterator; | |
| 82 | -for (vertex_iterator v = vertices(g).first; v != vertices(g).second; ++v) { | |
| 81 | +for (auto v = vertices(g).first; v != vertices(g).second; ++v) { | |
| 83 | 82 | out << get(vertex_index, *v)+1 << " \"" << get(vertex_name, *v) << "\"\n"; |
| 84 | 83 | } |
| 85 | 84 | |
| 86 | 85 | out << "*Edges\n"; |
| 87 | -typedef typename graph_traits::edge_iterator edge_iterator; | |
| 88 | -for (edge_iterator e = edges(g).first; e != edges(g).second; ++e) { | |
| 86 | +for (auto e = edges(g).first; e != edges(g).second; ++e) { | |
| 89 | 87 | out << get(vertex_index, source(*e, g))+1 << ' ' |
| 90 | 88 | << get(vertex_index, target(*e, g))+1 << " 1.0\n"; // HACK! |
| 91 | 89 | } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -59,10 +59,8 @@ int main(int , char* []) | ||
| 59 | 59 | const int V = 5; |
| 60 | 60 | Graph g(V); |
| 61 | 61 | |
| 62 | - property_map<Graph, std::size_t VertexProperties::*>::type | |
| 63 | - id = get(&VertexProperties::index, g); | |
| 64 | - property_map<Graph, std::string EdgeProperties::*>::type | |
| 65 | - name = get(&EdgeProperties::name, g); | |
| 62 | +auto id = get(&VertexProperties::index, g); | |
| 63 | +auto name = get(&EdgeProperties::name, g); | |
| 66 | 64 | |
| 67 | 65 | boost::graph_traits::vertex_iterator vi, viend; |
| 68 | 66 | int vnum = 0; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -81,8 +81,8 @@ class distance_heuristic : public astar_heuristic<Graph, CostType> | ||
| 81 | 81 | : m_location(l), m_goal(goal) {} |
| 82 | 82 | CostType operator()(Vertex u) |
| 83 | 83 | { |
| 84 | -CostType dx = m_location[m_goal].x - m_location[u].x; | |
| 85 | -CostType dy = m_location[m_goal].y - m_location[u].y; | |
| 84 | +auto dx = m_location[m_goal].x - m_location[u].x; | |
| 85 | +auto dy = m_location[m_goal].y - m_location[u].y; | |
| 86 | 86 | return ::sqrt(dx * dx + dy * dy); |
| 87 | 87 | } |
| 88 | 88 | private: |
| @@ -160,7 +160,7 @@ int main(int argc, char **argv) | ||
| 160 | 160 | |
| 161 | 161 | // create graph |
| 162 | 162 | mygraph_t g(N); |
| 163 | -WeightMap weightmap = get(edge_weight, g); | |
| 163 | +auto weightmap = get(edge_weight, g); | |
| 164 | 164 | for(std::size_t j = 0; j < num_edges; ++j) { |
| 165 | 165 | edge_descriptor e; bool inserted; |
| 166 | 166 | boost::tie(e, inserted) = add_edge(edge_array[j].first, |
| @@ -171,8 +171,8 @@ int main(int argc, char **argv) | ||
| 171 | 171 | |
| 172 | 172 | // pick random start/goal |
| 173 | 173 | boost::mt19937 gen(time(0)); |
| 174 | -vertex start = random_vertex(g, gen); | |
| 175 | -vertex goal = random_vertex(g, gen); | |
| 174 | +auto start = random_vertex(g, gen); | |
| 175 | +auto goal = random_vertex(g, gen); | |
| 176 | 176 | |
| 177 | 177 | |
| 178 | 178 | cout << "Start vertex: " << name[start] << endl; |
| @@ -202,14 +202,14 @@ int main(int argc, char **argv) | ||
| 202 | 202 | |
| 203 | 203 | } catch(found_goal fg) { // found a path to the goal |
| 204 | 204 | list shortest_path; |
| 205 | -for(vertex v = goal;; v = p[v]) { | |
| 205 | +for(auto v = goal;; v = p[v]) { | |
| 206 | 206 | shortest_path.push_front(v); |
| 207 | 207 | if(p[v] == v) |
| 208 | 208 | break; |
| 209 | 209 | } |
| 210 | 210 | cout << "Shortest path from " << name[start] << " to " |
| 211 | 211 | << name[goal] << ": "; |
| 212 | -list::iterator spi = shortest_path.begin(); | |
| 212 | +auto spi = shortest_path.begin(); | |
| 213 | 213 | cout << name[start]; |
| 214 | 214 | for(++spi; spi != shortest_path.end(); ++spi) |
| 215 | 215 | cout << " -> " << name[*spi]; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -178,8 +178,8 @@ bool maze::solve() { | ||
| 178 | 178 | dist_map distance; |
| 179 | 179 | boost::associative_property_map<dist_map> dist_pmap(distance); |
| 180 | 180 | |
| 181 | -vertex_descriptor s = source(); | |
| 182 | -vertex_descriptor g = goal(); | |
| 181 | +auto s = source(); | |
| 182 | +auto g = goal(); | |
| 183 | 183 | euclidean_heuristic heuristic(g); |
| 184 | 184 | astar_goal_visitor visitor(g); |
| 185 | 185 | |
| @@ -192,7 +192,7 @@ bool maze::solve() { | ||
| 192 | 192 | } catch(found_goal fg) { |
| 193 | 193 | // Walk backwards from the goal through the predecessor chain adding |
| 194 | 194 | // vertices to the solution path. |
| 195 | -for (vertex_descriptor u = g; u != s; u = predecessor[u]) | |
| 195 | +for (auto u = g; u != s; u = predecessor[u]) | |
| 196 | 196 | m_solution.insert(u); |
| 197 | 197 | m_solution.insert(s); |
| 198 | 198 | m_solution_length = distance[g]; |
| @@ -256,9 +256,9 @@ std::size_t random_int(std::size_t a, std::size_t b) { | ||
| 256 | 256 | // Generate a maze with a random assignment of barriers. |
| 257 | 257 | maze random_maze(std::size_t x, std::size_t y) { |
| 258 | 258 | maze m(x, y); |
| 259 | -vertices_size_type n = num_vertices(m.m_grid); | |
| 260 | -vertex_descriptor s = m.source(); | |
| 261 | -vertex_descriptor g = m.goal(); | |
| 259 | +auto n = num_vertices(m.m_grid); | |
| 260 | +auto s = m.source(); | |
| 261 | +auto g = m.goal(); | |
| 262 | 262 | // One quarter of the cells in the maze should be barriers. |
| 263 | 263 | int barriers = n/4; |
| 264 | 264 | while (barriers > 0) { |
| @@ -267,7 +267,7 @@ maze random_maze(std::size_t x, std::size_t y) { | ||
| 267 | 267 | // Walls range up to one quarter the dimension length in this direction. |
| 268 | 268 | vertices_size_type wall = random_int(1, m.length(direction)/4); |
| 269 | 269 | // Create the wall while decrementing the total barrier count. |
| 270 | -vertex_descriptor u = vertex(random_int(0, n-1), m.m_grid); | |
| 270 | +auto u = vertex(random_int(0, n-1), m.m_grid); | |
| 271 | 271 | while (wall) { |
| 272 | 272 | // Start and goal spaces should never be barriers. |
| 273 | 273 | if (u != s && u != g) { |
| @@ -277,7 +277,7 @@ maze random_maze(std::size_t x, std::size_t y) { | ||
| 277 | 277 | barriers--; |
| 278 | 278 | } |
| 279 | 279 | } |
| 280 | -vertex_descriptor v = m.m_grid.next(u, direction); | |
| 280 | +auto v = m.m_grid.next(u, direction); | |
| 281 | 281 | // Stop creating this wall if we reached the maze's edge. |
| 282 | 282 | if (u == v) |
| 283 | 283 | break; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -27,8 +27,7 @@ struct edge_writer | ||
| 27 | 27 | void operator() (std::ostream & out, const Edge & e) const |
| 28 | 28 | { |
| 29 | 29 | out << "[label=\"" << get(edge_weight, m_g, e) << "\""; |
| 30 | -typename graph_traits < Graph >::vertex_descriptor | |
| 31 | - u = source(e, m_g), v = target(e, m_g); | |
| 30 | +auto u = source(e, m_g), v = target(e, m_g); | |
| 32 | 31 | if (m_parent[v] == u) |
| 33 | 32 | out << ", color=\"black\""; |
| 34 | 33 | else |
| @@ -71,8 +70,7 @@ main() | ||
| 71 | 70 | Graph g(edge_array, edge_array + n_edges, N); |
| 72 | 71 | #endif |
| 73 | 72 | graph_traits < Graph >::edge_iterator ei, ei_end; |
| 74 | - property_map<Graph, int EdgeProperties::*>::type | |
| 75 | - weight_pmap = get(&EdgeProperties::weight, g); | |
| 73 | +auto weight_pmap = get(&EdgeProperties::weight, g); | |
| 76 | 74 | int i = 0; |
| 77 | 75 | for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei, ++i) |
| 78 | 76 | weight_pmap[*ei] = weight[i]; |
| @@ -109,9 +107,8 @@ main() | ||
| 109 | 107 | |
| 110 | 108 | { |
| 111 | 109 | for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) { |
| 112 | - graph_traits < Graph >::edge_descriptor e = *ei; | |
| 113 | - graph_traits < Graph >::vertex_descriptor | |
| 114 | - u = source(e, g), v = target(e, g); | |
| 110 | +auto e = *ei; | |
| 111 | +auto u = source(e, g), v = target(e, g); | |
| 115 | 112 | // VC++ doesn't like the 3-argument get function, so here |
| 116 | 113 | // we workaround by using 2-nested get()'s. |
| 117 | 114 | dot_file << name[u] << " -> " << name[v] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -69,7 +69,7 @@ main() | ||
| 69 | 69 | |
| 70 | 70 | Size time = 0; |
| 71 | 71 | typedef property_map<graph_t, std::size_t VertexProps::*>::type dtime_map_t; |
| 72 | -dtime_map_t dtime_map = get(&VertexProps::discover_time, g); | |
| 72 | +auto dtime_map = get(&VertexProps::discover_time, g); | |
| 73 | 73 | bfs_time_visitor < dtime_map_t > vis(dtime_map, time); |
| 74 | 74 | breadth_first_search(g, vertex(s, g), color_map(get(&VertexProps::color, g)). |
| 75 | 75 | visitor(vis)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -17,16 +17,15 @@ void | ||
| 17 | 17 | build_router_network(Graph & g, VertexNameMap name_map, |
| 18 | 18 | TransDelayMap delay_map) |
| 19 | 19 | { |
| 20 | -typename graph_traits < Graph >::vertex_descriptor a, b, c, d, e; | |
| 21 | - a = add_vertex(g); | |
| 20 | +auto a = add_vertex(g); | |
| 22 | 21 | name_map[a] = 'a'; |
| 23 | - b = add_vertex(g); | |
| 22 | +auto b = add_vertex(g); | |
| 24 | 23 | name_map[b] = 'b'; |
| 25 | - c = add_vertex(g); | |
| 24 | +auto c = add_vertex(g); | |
| 26 | 25 | name_map[c] = 'c'; |
| 27 | - d = add_vertex(g); | |
| 26 | +auto d = add_vertex(g); | |
| 28 | 27 | name_map[d] = 'd'; |
| 29 | - e = add_vertex(g); | |
| 28 | +auto e = add_vertex(g); | |
| 30 | 29 | name_map[e] = 'e'; |
| 31 | 30 | |
| 32 | 31 | typename graph_traits::edge_descriptor ed; |
| @@ -79,13 +78,13 @@ main() | ||
| 79 | 78 | typedef adjacency_list < listS, vecS, directedS, VP, EP> graph_t; |
| 80 | 79 | graph_t g; |
| 81 | 80 | |
| 82 | -property_map<graph_t, char VP::*>::type name_map = get(&VP::name, g); | |
| 83 | -property_map<graph_t, double EP::*>::type delay_map = get(&EP::weight, g); | |
| 81 | +auto name_map = get(&VP::name, g); | |
| 82 | +auto delay_map = get(&EP::weight, g); | |
| 84 | 83 | |
| 85 | 84 | build_router_network(g, name_map, delay_map); |
| 86 | 85 | |
| 87 | 86 | typedef property_map<graph_t, char VP::*>::type VertexNameMap; |
| 88 | -graph_traits<graph_t>::vertex_descriptor a = *vertices(g).first; | |
| 87 | +auto a = *vertices(g).first; | |
| 89 | 88 | bfs_name_printer vis(name_map); |
| 90 | 89 | std::cout << "BFS vertex discover order: "; |
| 91 | 90 | breadth_first_search(g, a, visitor(vis)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -129,7 +129,7 @@ int main(int , char* []) | ||
| 129 | 129 | std::fill_n(d, 5, 0); |
| 130 | 130 | |
| 131 | 131 | // The source vertex |
| 132 | -Vertex s = *(boost::vertices(G).first); | |
| 132 | +auto s = *(boost::vertices(G).first); | |
| 133 | 133 | p[s] = s; |
| 134 | 134 | boost::breadth_first_search |
| 135 | 135 | (G, s, |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -123,7 +123,7 @@ int main(int , char* []) | ||
| 123 | 123 | std::fill_n(d, 5, 0); |
| 124 | 124 | |
| 125 | 125 | // The source vertex |
| 126 | -Vertex s = *(boost::vertices(G).first); | |
| 126 | +auto s = *(boost::vertices(G).first); | |
| 127 | 127 | p[s] = s; |
| 128 | 128 | boost::neighbor_breadth_first_search |
| 129 | 129 | (G, s, |