Skip to content

Commit

Permalink
examples: C++11: Use auto
Browse files Browse the repository at this point in the history
  • Loading branch information
murraycu committed Jan 6, 2025
1 parent ce1daeb commit 9ed5d90
Show file tree
Hide file tree
Showing 107 changed files with 284 additions and 419 deletions.
8 changes: 2 additions & 6 deletions example/accum-compile-times.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,8 @@ int main(int argc, const char** argv)
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(argc >= 3 ? argv[2] : "makefile-target-names.dat");
std::ifstream compile_cost_in(
Expand Down
20 changes: 8 additions & 12 deletions example/actor_clustering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ void load_actor_graph(std::istream& in, ActorGraph& g)
// Map from the actor numbers on this line to the actor vertices
typedef tokenizer< char_separator< char > > Tok;
Tok tok(line, char_separator< char >(" "));
for (Tok::iterator id = tok.begin(); id != tok.end(); ++id)
for (const auto& id : tok)
{
int actor_id = lexical_cast< int >(*id);
std::map< int, Vertex >::iterator v = actors.find(actor_id);
auto actor_id = lexical_cast< int >(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);
}
Expand All @@ -68,11 +68,9 @@ void load_actor_graph(std::istream& in, ActorGraph& g)
}
}

for (std::vector< Vertex >::iterator i = actors_in_movie.begin();
i != actors_in_movie.end(); ++i)
for (auto i = actors_in_movie.begin(); i != actors_in_movie.end(); ++i)
{
for (std::vector< Vertex >::iterator j = i + 1;
j != actors_in_movie.end(); ++j)
for (auto j = i + 1; j != actors_in_movie.end(); ++j)
{
if (!edge(*i, *j, g).second)
add_edge(*i, *j, g);
Expand All @@ -86,16 +84,14 @@ std::ostream& 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< Graph >::vertex_iterator vertex_iterator;
for (vertex_iterator v = vertices(g).first; v != vertices(g).second; ++v)
for (auto v = vertices(g).first; v != vertices(g).second; ++v)
{
out << get(vertex_index, *v) + 1 << " \"" << get(vertex_name, *v)
<< "\"\n";
}

out << "*Edges\n";
typedef typename graph_traits< Graph >::edge_iterator edge_iterator;
for (edge_iterator e = edges(g).first; e != edges(g).second; ++e)
for (auto 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!
Expand Down
6 changes: 2 additions & 4 deletions example/adjacency_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@ int main(int, char*[])
const int V = 5;
Graph g(V);

property_map< Graph, std::size_t VertexProperties::* >::type id
= get(&VertexProperties::index, g);
property_map< Graph, std::string EdgeProperties::* >::type name
= get(&EdgeProperties::name, g);
auto id = get(&VertexProperties::index, g);
auto name = get(&EdgeProperties::name, g);

boost::graph_traits< Graph >::vertex_iterator vi, viend;
int vnum = 0;
Expand Down
16 changes: 8 additions & 8 deletions example/astar-cities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ class distance_heuristic : public astar_heuristic< Graph, CostType >
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);
}

Expand Down Expand Up @@ -174,7 +174,7 @@ 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;
Expand All @@ -185,9 +185,9 @@ int main(int argc, char** argv)
}

// pick random start/goal
boost::mt19937 gen(std::time(0));
vertex start = random_vertex(g, gen);
vertex goal = random_vertex(g, gen);
boost::mt19937 gen(time(0));
auto start = random_vertex(g, gen);
auto goal = random_vertex(g, gen);

cout << "Start vertex: " << name[start] << endl;
cout << "Goal vertex: " << name[goal] << endl;
Expand Down Expand Up @@ -215,15 +215,15 @@ int main(int argc, char** argv)
catch (found_goal fg)
{ // found a path to the goal
list< vertex > shortest_path;
for (vertex v = goal;; v = p[v])
for (auto v = goal;; v = p[v])
{
shortest_path.push_front(v);
if (p[v] == v)
break;
}
cout << "Shortest path from " << name[start] << " to " << name[goal]
<< ": ";
list< vertex >::iterator spi = shortest_path.begin();
auto spi = shortest_path.begin();
cout << name[start];
for (++spi; spi != shortest_path.end(); ++spi)
cout << " -> " << name[*spi];
Expand Down
16 changes: 8 additions & 8 deletions example/astar_maze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ bool maze::solve()
dist_map distance;
boost::associative_property_map< dist_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);

Expand All @@ -218,7 +218,7 @@ bool maze::solve()
{
// 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];
Expand Down Expand Up @@ -285,9 +285,9 @@ std::size_t random_int(std::size_t a, std::size_t b)
// Generate a maze with a random assignment of barriers.
void random_maze(maze& m)
{
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)
Expand All @@ -297,7 +297,7 @@ void random_maze(maze& m)
// 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.
Expand All @@ -310,7 +310,7 @@ void random_maze(maze& m)
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;
Expand Down
11 changes: 4 additions & 7 deletions example/bellman-example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ template < typename Graph, typename ParentMap > struct edge_writer
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
Expand Down Expand Up @@ -74,8 +73,7 @@ int main()
Graph g(edge_array, edge_array + n_edges, N);
#endif
graph_traits< Graph >::edge_iterator ei, ei_end;
property_map< Graph, int EdgeProperties::* >::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];
Expand Down Expand Up @@ -115,9 +113,8 @@ int main()
{
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);
auto e = *ei;
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] << "[label=\""
Expand Down
2 changes: 1 addition & 1 deletion example/bfs-example2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ int main()
Size time = 0;
typedef property_map< graph_t, std::size_t VertexProps::* >::type
dtime_map_t;
dtime_map_t dtime_map = get(&VertexProps::discover_time, g);
auto dtime_map = get(&VertexProps::discover_time, g);
bfs_time_visitor< dtime_map_t > vis(dtime_map, time);
breadth_first_search(
g, vertex(s, g), color_map(get(&VertexProps::color, g)).visitor(vis));
Expand Down
17 changes: 8 additions & 9 deletions example/bfs-name-printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ template < typename Graph, typename VertexNameMap, typename TransDelayMap >
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< Graph >::edge_descriptor ed;
Expand Down Expand Up @@ -78,13 +77,13 @@ int main()
typedef adjacency_list< listS, vecS, directedS, VP, EP > graph_t;
graph_t g;

property_map< graph_t, char VP::* >::type name_map = get(&VP::name, g);
property_map< graph_t, double EP::* >::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< graph_t, char VP::* >::type VertexNameMap;
graph_traits< graph_t >::vertex_descriptor a = *vertices(g).first;
auto a = *vertices(g).first;
bfs_name_printer< VertexNameMap > vis(name_map);
std::cout << "BFS vertex discover order: ";
breadth_first_search(g, a, visitor(vis));
Expand Down
2 changes: 1 addition & 1 deletion example/bfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
boost::visitor(boost::make_bfs_visitor(
Expand Down
2 changes: 1 addition & 1 deletion example/bfs_neighbor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
boost::visitor(boost::make_neighbor_bfs_visitor(
Expand Down
5 changes: 2 additions & 3 deletions example/biconnected_components.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ int 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< vertex_t > art_points;
Expand Down
14 changes: 7 additions & 7 deletions example/boost_web_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ class calc_distance_visitor : public boost::bfs_visitor<>
void tree_edge(
typename boost::graph_traits< Graph >::edge_descriptor e, Graph& g)
{
typename boost::graph_traits< Graph >::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;
}

Expand Down Expand Up @@ -95,9 +94,8 @@ int main(int argc, const char** argv)
NameVertexMap name2vertex;
Graph g;

typedef property_map< Graph, vertex_name_t >::type NameMap;
NameMap node_name = get(vertex_name, g);
property_map< Graph, edge_name_t >::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.
Expand All @@ -113,7 +111,7 @@ int main(int argc, const char** argv)
bool inserted;
Vertex u, v;

std::list< std::string >::iterator i = line_toks.begin();
auto i = line_toks.begin();

boost::tie(pos, inserted)
= name2vertex.insert(std::make_pair(*i, Vertex()));
Expand Down Expand Up @@ -210,6 +208,8 @@ int main(int argc, const char** argv)
// the tree nodes in the order that we want to print out:
// a directory-structure like format.
std::vector< size_type > dfs_distances(num_vertices(g), 0);

using NameMap = property_map< Graph, vertex_name_t >::type;
print_tree_visitor< NameMap, size_type* > tree_printer(
node_name, &dfs_distances[0]);
for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
Expand Down
10 changes: 4 additions & 6 deletions example/boykov_kolmogorov-eg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,15 @@ int main()
Graph;

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< default_color_type > color(num_vertices(g));
std::vector< long > 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;
Expand Down
2 changes: 1 addition & 1 deletion example/bron_kerbosch_clique_number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int main(int argc, char* argv[])
read_graph(g, cin);

// Use the Bron-Kerbosch algorithm to find all cliques, and
size_t c = bron_kerbosch_clique_number(g);
auto c = bron_kerbosch_clique_number(g);
cout << "clique number: " << c << endl;

return 0;
Expand Down
Loading

0 comments on commit 9ed5d90

Please sign in to comment.