geodynamics / aspect

A parallel, extensible finite element code to simulate convection in both 2D and 3D models.
https://aspect.geodynamics.org/
Other
227 stars 237 forks source link

C++11: Use range-based for loops #3774

Open bangerth opened 4 years ago

bangerth commented 4 years ago

We have a lot of places of the form

      typename DoFHandler<dim>::active_cell_iterator cell = mesh_deformation_dof_handler.begin_active(),
                                                     endc= mesh_deformation_dof_handler.end();
      for (; cell!=endc; ++cell)
        ...

But using C++11 features, we can more easily write this as

for (const auto &cell : this->get_dof_handler().active_cell_iterators())

Similarly, there are plenty of places of the form (or in many variations -- searching for for... ::const_iterator will yield many)

      for (typename std::list<std::unique_ptr<Interface<dim> > >::const_iterator
           p = mesh_refinement_objects.begin();
           p != mesh_refinement_objects.end(); ++p)

that could now be written as

  for (const auto p : mesh_refinement_objects)

All of these might make the code simpler to read.

pmbremner commented 2 years ago

@bangerth I see you have two merged pull requests related to this. Has this issue been addressed?

bangerth commented 2 years ago

It seems to me like the majority of places has been converted. But there are a few places one could still touch:

> egrep -n -r 'for.*typename' source/
source/postprocess/heating_statistics.cc:100:            for (typename std::list<std::unique_ptr<HeatingModel::Interface<dim>>>::const_iterator
source/postprocess/heating_statistics.cc:122:      for (typename std::list<std::unique_ptr<HeatingModel::Interface<dim>>>::const_iterator
source/postprocess/visualization.cc:1176:              for (typename std::list<typename aspect::internal::Plugins::PluginList<VisualizationPostprocessors::Interface<dim>>::PluginInfo>::const_iterator
source/postprocess/visualization/heating.cc:153:        for (typename std::list<std::unique_ptr<HeatingModel::Interface<dim>>>::const_iterator
source/postprocess/interface.cc:234:          for (typename std::list<typename aspect::internal::Plugins::PluginList<Interface<dim>>::PluginInfo>::const_iterator
source/simulator/core.cc:1626:            for (typename Triangulation<dim>::active_cell_iterator
source/simulator/core.cc:1641:          for (typename Triangulation<dim>::active_cell_iterator
source/termination_criteria/interface.cc:87:      for (typename std::list<std::unique_ptr<Interface<dim>>>::const_iterator
source/particle/interpolator/bilinear_least_squares.cc:115:        for (typename ParticleHandler<dim>::particle_iterator particle = particle_range.begin();
source/particle/interpolator/bilinear_least_squares.cc:195:                for (typename std::vector<Point<dim>>::const_iterator itr = positions.begin(); itr != positions.end(); ++itr, ++positions_index)
source/particle/interpolator/nearest_neighbor.cc:76:                for (typename ParticleHandler<dim>::particle_iterator particle = particle_range.begin();
source/particle/interpolator/quadratic_least_squares.cc:102:        for (typename ParticleHandler<dim>::particle_iterator particle = particle_range.begin();
source/particle/interpolator/quadratic_least_squares.cc:161:        for (typename std::vector<Point<dim>>::const_iterator itr = positions.begin(); itr != positions.end(); ++itr, ++index_positions)
source/particle/integrator/euler.cc:46:        for (typename ParticleHandler<dim>::particle_iterator it = begin_particle;
source/particle/integrator/rk_2.cc:73:        for (typename ParticleHandler<dim>::particle_iterator it = begin_particle;
source/particle/integrator/rk_4.cc:78:        for (typename ParticleHandler<dim>::particle_iterator it = begin_particle;
source/particle/property/interface.cc:413:        for (typename std::list<std::unique_ptr<Interface<dim>>>::const_iterator
source/particle/property/interface.cc:562:        for (typename std::list<std::unique_ptr<Interface<dim>>>::const_iterator
source/particle/property/interface.cc:752:                for (typename std::list<typename aspect::internal::Plugins::PluginList<aspect::Particle::Property::Interface<dim>>::PluginInfo>::const_iterator
source/particle/world.cc:465:      for (typename ParticleHandler<dim>::particle_iterator it = begin_particle; it!=end_particle; ++it)
source/geometry_model/two_merged_chunks.cc:125:      for (typename Triangulation<dim>::active_cell_iterator
source/mesh_refinement/interface.cc:156:      for (typename std::list<std::unique_ptr<Interface<dim>>>::const_iterator

@pmbremner Feel free to convert these in whatever large or small increment you feel like.