From eeaf00abf683c96a5bfcf6444af301a4bdba41e8 Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Wed, 9 Jan 2019 10:20:19 +0100 Subject: [PATCH] Use ranged-based for loop in source/numerics --- source/numerics/data_out_stack.cc | 44 +++++++------- source/numerics/histogram.cc | 8 +-- source/numerics/matrix_tools_once.cc | 79 ++++++++++---------------- source/numerics/point_value_history.cc | 10 ++-- source/numerics/time_dependent.cc | 8 +-- 5 files changed, 61 insertions(+), 88 deletions(-) diff --git a/source/numerics/data_out_stack.cc b/source/numerics/data_out_stack.cc index 5d51002780..336ddeba53 100644 --- a/source/numerics/data_out_stack.cc +++ b/source/numerics/data_out_stack.cc @@ -112,23 +112,16 @@ DataOutStack::declare_data_vector( // also make sure that no name is // used twice - for (std::vector::const_iterator name = names.begin(); - name != names.end(); - ++name) + for (const auto &name : names) { - for (typename std::vector::const_iterator data_set = - dof_data.begin(); - data_set != dof_data.end(); - ++data_set) - for (unsigned int i = 0; i < data_set->names.size(); ++i) - Assert(*name != data_set->names[i], ExcNameAlreadyUsed(*name)); - - for (typename std::vector::const_iterator data_set = - cell_data.begin(); - data_set != cell_data.end(); - ++data_set) - for (unsigned int i = 0; i < data_set->names.size(); ++i) - Assert(*name != data_set->names[i], ExcNameAlreadyUsed(*name)); + (void)name; + for (const auto &data_set : dof_data) + for (const auto &data_set_name : data_set.names) + Assert(name != data_set_name, ExcNameAlreadyUsed(name)); + + for (const auto &data_set : cell_data) + for (const auto &data_set_name : data_set.names) + Assert(name != data_set_name, ExcNameAlreadyUsed(name)); } switch (vector_type) @@ -198,15 +191,18 @@ DataOutStack::add_data_vector( (names.size() == dof_handler->get_fe(0).n_components())), Exceptions::DataOutImplementation::ExcInvalidNumberOfNames( names.size(), dof_handler->get_fe(0).n_components())); - for (unsigned int i = 0; i < names.size(); ++i) - Assert(names[i].find_first_not_of("abcdefghijklmnopqrstuvwxyz" + for (const auto &name : names) + { + (void)name; + Assert(name.find_first_not_of("abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789_<>()") == std::string::npos, + Exceptions::DataOutImplementation::ExcInvalidCharacter( + name, + name.find_first_not_of("abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "0123456789_<>()") == std::string::npos, - Exceptions::DataOutImplementation::ExcInvalidCharacter( - names[i], - names[i].find_first_not_of("abcdefghijklmnopqrstuvwxyz" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "0123456789_<>()"))); + "0123456789_<>()"))); + } if (vec.size() == dof_handler->n_dofs()) { diff --git a/source/numerics/histogram.cc b/source/numerics/histogram.cc index a5dd3f2deb..51dfcfb54c 100644 --- a/source/numerics/histogram.cc +++ b/source/numerics/histogram.cc @@ -233,11 +233,9 @@ Histogram::write_gnuplot(std::ostream &out) const // one data set is available if (intervals.size() == 1) { - for (unsigned int n = 0; n < intervals[0].size(); ++n) - out << intervals[0][n].left_point << ' ' << intervals[0][n].content - << std::endl - << intervals[0][n].right_point << ' ' << intervals[0][n].content - << std::endl; + for (const auto &interval : intervals[0]) + out << interval.left_point << ' ' << interval.content << std::endl + << interval.right_point << ' ' << interval.content << std::endl; } else // otherwise create a whole 3d plot diff --git a/source/numerics/matrix_tools_once.cc b/source/numerics/matrix_tools_once.cc index aded4396a4..21efb2f41f 100644 --- a/source/numerics/matrix_tools_once.cc +++ b/source/numerics/matrix_tools_once.cc @@ -106,13 +106,10 @@ namespace MatrixTools // figure out which rows of the matrix we // have to eliminate on this processor std::vector constrained_rows; - for (std::map::const_iterator - dof = boundary_values.begin(); - dof != boundary_values.end(); - ++dof) - if ((dof->first >= local_range.first) && - (dof->first < local_range.second)) - constrained_rows.push_back(dof->first); + for (const auto &boundary_value : boundary_values) + if ((boundary_value.first >= local_range.first) && + (boundary_value.first < local_range.second)) + constrained_rows.push_back(boundary_value.first); // then eliminate these rows and set // their diagonal entry to what we have @@ -129,22 +126,19 @@ namespace MatrixTools std::vector indices; std::vector solution_values; - for (std::map::const_iterator - dof = boundary_values.begin(); - dof != boundary_values.end(); - ++dof) - if ((dof->first >= local_range.first) && - (dof->first < local_range.second)) + for (const auto &boundary_value : boundary_values) + if ((boundary_value.first >= local_range.first) && + (boundary_value.first < local_range.second)) { - indices.push_back(dof->first); - solution_values.push_back(dof->second); + indices.push_back(boundary_value.first); + solution_values.push_back(boundary_value.second); } solution.set(indices, solution_values); // now also set appropriate values for // the rhs - for (unsigned int i = 0; i < solution_values.size(); ++i) - solution_values[i] *= average_nonzero_diagonal_entry; + for (double &solution_value : solution_values) + solution_value *= average_nonzero_diagonal_entry; right_hand_side.set(indices, solution_values); } @@ -187,20 +181,17 @@ namespace MatrixTools { int block = 0; dealii::types::global_dof_index offset = 0; - for (std::map::const_iterator dof = - boundary_values.begin(); - dof != boundary_values.end(); - ++dof) + for (const auto &boundary_value : boundary_values) { - if (dof->first >= matrix.block(block, 0).m() + offset) + if (boundary_value.first >= matrix.block(block, 0).m() + offset) { offset += matrix.block(block, 0).m(); block++; } - const types::global_dof_index index = dof->first - offset; + const types::global_dof_index index = boundary_value.first - offset; block_boundary_values[block].insert( - std::pair(index, - dof->second)); + std::pair( + index, boundary_value.second)); } } @@ -294,14 +285,10 @@ namespace MatrixTools // figure out which rows of the matrix we // have to eliminate on this processor std::vector constrained_rows; - for (std::map::const_iterator dof = - boundary_values.begin(); - dof != boundary_values.end(); - ++dof) - if ((dof->first >= local_range.first) && - (dof->first < local_range.second)) - constrained_rows.push_back(dof->first); + for (const auto &boundary_value : boundary_values) + if ((boundary_value.first >= local_range.first) && + (boundary_value.first < local_range.second)) + constrained_rows.push_back(boundary_value.first); // then eliminate these rows and // set their diagonal entry to @@ -314,16 +301,12 @@ namespace MatrixTools std::vector indices; std::vector solution_values; - for (std::map::const_iterator dof = - boundary_values.begin(); - dof != boundary_values.end(); - ++dof) - if ((dof->first >= local_range.first) && - (dof->first < local_range.second)) + for (const auto &boundary_value : boundary_values) + if ((boundary_value.first >= local_range.first) && + (boundary_value.first < local_range.second)) { - indices.push_back(dof->first); - solution_values.push_back(dof->second); + indices.push_back(boundary_value.first); + solution_values.push_back(boundary_value.second); } solution.set(indices, solution_values); @@ -380,20 +363,18 @@ namespace MatrixTools { int block = 0; types::global_dof_index offset = 0; - for (std::map::const_iterator - dof = boundary_values.begin(); - dof != boundary_values.end(); - ++dof) + for (const auto &boundary_value : boundary_values) { - if (dof->first >= matrix.block(block, 0).m() + offset) + if (boundary_value.first >= matrix.block(block, 0).m() + offset) { offset += matrix.block(block, 0).m(); block++; } - const types::global_dof_index index = dof->first - offset; + const types::global_dof_index index = + boundary_value.first - offset; block_boundary_values[block].insert( std::pair( - index, dof->second)); + index, boundary_value.second)); } } diff --git a/source/numerics/point_value_history.cc b/source/numerics/point_value_history.cc index 8c3cbdeda9..58231c2cb1 100644 --- a/source/numerics/point_value_history.cc +++ b/source/numerics/point_value_history.cc @@ -1157,10 +1157,9 @@ PointValueHistory::write_gnuplot( { AssertThrow(names.size() == n_stored, ExcDimensionMismatch(names.size(), n_stored)); - for (unsigned int component = 0; component < names.size(); - component++) + for (const auto &name : names) { - to_gnuplot << "<" << names[component] << "> "; + to_gnuplot << "<" << name << "> "; } } else @@ -1432,10 +1431,9 @@ PointValueHistory::status(std::ostream &out) // add names, if available if (component_names->second.size() > 0) { - for (unsigned int name = 0; name < component_names->second.size(); - name++) + for (const auto &name : component_names->second) { - out << "<" << component_names->second[name] << "> "; + out << "<" << name << "> "; } out << "\n"; } diff --git a/source/numerics/time_dependent.cc b/source/numerics/time_dependent.cc index 9ec4a97ebc..58264342d1 100644 --- a/source/numerics/time_dependent.cc +++ b/source/numerics/time_dependent.cc @@ -218,8 +218,8 @@ TimeDependent::start_sweep(const unsigned int s) timesteps[step]->set_sweep_no(sweep_no); } - for (unsigned int step = 0; step < timesteps.size(); ++step) - timesteps[step]->start_sweep(); + for (const auto ×tep : timesteps) + timestep->start_sweep(); } @@ -255,8 +255,8 @@ TimeDependent::memory_consumption() const MemoryConsumption::memory_consumption(sweep_no) + sizeof(timestepping_data_primal) + sizeof(timestepping_data_dual) + sizeof(timestepping_data_postprocess)); - for (unsigned int i = 0; i < timesteps.size(); ++i) - mem += MemoryConsumption::memory_consumption(*timesteps[i]); + for (const auto ×tep : timesteps) + mem += MemoryConsumption::memory_consumption(*timestep); return mem; } -- 2.39.5