From: Daniel Arndt Date: Sun, 6 Jan 2019 19:18:32 +0000 (+0100) Subject: Use range-based for loop in source/dofs X-Git-Tag: v9.1.0-rc1~442^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f3a70a60c38effdad42d3254f981ff121bedaa65;p=dealii.git Use range-based for loop in source/dofs --- diff --git a/source/dofs/block_info.cc b/source/dofs/block_info.cc index bc0c3990a9..3e597031db 100644 --- a/source/dofs/block_info.cc +++ b/source/dofs/block_info.cc @@ -42,10 +42,8 @@ BlockInfo::initialize(const DoFHandler &dof, if (!active_only && dof.has_level_dofs()) { std::vector> sizes( - dof.get_triangulation().n_levels()); - - for (unsigned int i = 0; i < sizes.size(); ++i) - sizes[i].resize(dof.get_fe().n_blocks()); + dof.get_triangulation().n_levels(), + std::vector(dof.get_fe().n_blocks())); MGTools::count_dofs_per_block(dof, sizes); levels.resize(sizes.size()); diff --git a/source/dofs/dof_handler.cc b/source/dofs/dof_handler.cc index 81b173e18c..03603ee5a0 100644 --- a/source/dofs/dof_handler.cc +++ b/source/dofs/dof_handler.cc @@ -1372,8 +1372,8 @@ DoFHandler::renumber_dofs( Assert(*p == i, ExcNewNumbersNotConsecutive(i)); } else - for (types::global_dof_index i = 0; i < new_numbers.size(); ++i) - Assert(new_numbers[i] < n_dofs(), + for (const auto new_number : new_numbers) + Assert(new_number < n_dofs(), ExcMessage( "New DoF index is not less than the total number of dofs.")); #endif @@ -1411,8 +1411,8 @@ DoFHandler::renumber_dofs( Assert(*p == i, ExcNewNumbersNotConsecutive(i)); } else - for (types::global_dof_index i = 0; i < new_numbers.size(); ++i) - Assert(new_numbers[i] < n_dofs(level), + for (const auto new_number : new_numbers) + Assert(new_number < n_dofs(level), ExcMessage( "New DoF index is not less than the total number of dofs.")); #endif diff --git a/source/dofs/dof_handler_policy.cc b/source/dofs/dof_handler_policy.cc index 48cfdee471..ab9a3f2336 100644 --- a/source/dofs/dof_handler_policy.cc +++ b/source/dofs/dof_handler_policy.cc @@ -676,7 +676,7 @@ namespace internal DoFIdentities &identities = *vertex_dof_identities[most_dominating_fe_index] [other_fe_index]; - for (unsigned int i = 0; i < identities.size(); ++i) + for (const auto &identity : identities) { const types::global_dof_index master_dof_index = dealii::internal::DoFAccessorImplementation:: @@ -684,14 +684,14 @@ namespace internal dof_handler, vertex_index, most_dominating_fe_index, - identities[i].first); + identity.first); const types::global_dof_index slave_dof_index = dealii::internal::DoFAccessorImplementation:: Implementation::get_vertex_dof_index( dof_handler, vertex_index, other_fe_index, - identities[i].second); + identity.second); // on subdomain boundaries, we will // encounter invalid DoFs on ghost cells, @@ -1036,16 +1036,15 @@ namespace internal DoFIdentities &identities = *line_dof_identities[most_dominating_fe_index] [other_fe_index]; - for (unsigned int i = 0; i < identities.size(); - ++i) + for (const auto &identity : identities) { const types::global_dof_index master_dof_index = line->dof_index( - identities[i].first, + identity.first, most_dominating_fe_index); const types::global_dof_index slave_dof_index = - line->dof_index(identities[i].second, + line->dof_index(identity.second, other_fe_index); // on subdomain boundaries, we will @@ -1197,13 +1196,13 @@ namespace internal DoFIdentities &identities = *quad_dof_identities[most_dominating_fe_index] [other_fe_index]; - for (unsigned int i = 0; i < identities.size(); ++i) + for (const auto &identity : identities) { const types::global_dof_index master_dof_index = - quad->dof_index(identities[i].first, + quad->dof_index(identity.first, most_dominating_fe_index); const types::global_dof_index slave_dof_index = - quad->dof_index(identities[i].second, + quad->dof_index(identity.second, other_fe_index); // we only store an identity if we are about to @@ -1363,12 +1362,13 @@ namespace internal new_dof_indices[p.first] = new_dof_indices[p.second]; } - for (unsigned int i = 0; i < new_dof_indices.size(); ++i) + for (const types::global_dof_index new_dof_index : new_dof_indices) { - Assert(new_dof_indices[i] != enumeration_dof_index, + (void)new_dof_index; + Assert(new_dof_index != enumeration_dof_index, ExcInternalError()); - Assert(new_dof_indices[i] < next_free_dof || - new_dof_indices[i] == numbers::invalid_dof_index, + Assert(new_dof_index < next_free_dof || + new_dof_index == numbers::invalid_dof_index, ExcInternalError()); } @@ -1516,8 +1516,7 @@ namespace internal DoFIdentities &identities = *vertex_dof_identities[most_dominating_fe_index] [other_fe_index]; - for (unsigned int i = 0; i < identities.size(); - ++i) + for (const auto &identity : identities) { const types::global_dof_index master_dof_index = dealii::internal:: @@ -1526,7 +1525,7 @@ namespace internal dof_handler, vertex_index, most_dominating_fe_index, - identities[i].first); + identity.first); const types::global_dof_index slave_dof_index = dealii::internal:: DoFAccessorImplementation:: @@ -1534,7 +1533,7 @@ namespace internal dof_handler, vertex_index, other_fe_index, - identities[i].second); + identity.second); // check if we are on an interface between // a locally owned and a ghost cell on which @@ -1558,7 +1557,7 @@ namespace internal dof_handler, vertex_index, other_fe_index, - identities[i].second, + identity.second, master_dof_index); } } @@ -1796,16 +1795,15 @@ namespace internal DoFIdentities &identities = *line_dof_identities[most_dominating_fe_index] [other_fe_index]; - for (unsigned int i = 0; i < identities.size(); - ++i) + for (const auto &identity : identities) { const types::global_dof_index master_dof_index = line->dof_index( - identities[i].first, + identity.first, most_dominating_fe_index); const types::global_dof_index slave_dof_index = - line->dof_index(identities[i].second, + line->dof_index(identity.second, other_fe_index); // check if we are on an interface between @@ -1824,7 +1822,7 @@ namespace internal numbers::invalid_dof_index) && (master_dof_index != numbers::invalid_dof_index)) - line->set_dof_index(identities[i].second, + line->set_dof_index(identity.second, master_dof_index, other_fe_index); } @@ -1940,13 +1938,13 @@ namespace internal DoFIdentities &identities = *quad_dof_identities[most_dominating_fe_index] [other_fe_index]; - for (unsigned int i = 0; i < identities.size(); ++i) + for (const auto &identity : identities) { const types::global_dof_index master_dof_index = - quad->dof_index(identities[i].first, + quad->dof_index(identity.first, most_dominating_fe_index); const types::global_dof_index slave_dof_index = - quad->dof_index(identities[i].second, + quad->dof_index(identity.second, other_fe_index); // check if we are on an interface between @@ -1964,7 +1962,7 @@ namespace internal numbers::invalid_dof_index) && (master_dof_index != numbers::invalid_dof_index)) - quad->set_dof_index(identities[i].second, + quad->set_dof_index(identity.second, master_dof_index, other_fe_index); } @@ -2092,7 +2090,7 @@ namespace internal { std::vector local_dof_indices; - for (auto cell : dof_handler.active_cell_iterators()) + for (const auto &cell : dof_handler.active_cell_iterators()) if (cell->is_ghost() && (cell->subdomain_id() < subdomain_id)) { // we found a neighboring ghost cell whose subdomain @@ -2103,7 +2101,7 @@ namespace internal // the interface) local_dof_indices.resize(cell->get_fe().dofs_per_cell); cell->get_dof_indices(local_dof_indices); - for (auto &local_dof_index : local_dof_indices) + for (const auto &local_dof_index : local_dof_indices) if (local_dof_index != numbers::invalid_dof_index) renumbering[local_dof_index] = numbers::invalid_dof_index; } @@ -4552,12 +4550,9 @@ namespace internal using cellmap_t = std::map>; cellmap_t neighbor_cell_list; - for (std::set::iterator it = - level_ghost_owners.begin(); - it != level_ghost_owners.end(); - ++it) + for (const auto level_ghost_owner : level_ghost_owners) neighbor_cell_list.insert( - std::make_pair(*it, CellDataTransferBuffer())); + std::make_pair(level_ghost_owner, CellDataTransferBuffer())); for (typename DoFHandlerType::level_cell_iterator cell = dof_handler.begin(0); @@ -5185,7 +5180,7 @@ namespace internal // mark all cells that either have to send data (locally // owned cells that are adjacent to ghost neighbors in some // way) or receive data (all ghost cells) via the user flags - for (auto cell : dof_handler->active_cell_iterators()) + for (const auto &cell : dof_handler->active_cell_iterators()) if (cell->is_locally_owned()) { for (unsigned int v = 0; @@ -5236,7 +5231,7 @@ namespace internal // at this point, we must have taken care of the data transfer // on all cells we had previously marked. verify this # ifdef DEBUG - for (auto cell : dof_handler->active_cell_iterators()) + for (const auto &cell : dof_handler->active_cell_iterators()) Assert(cell->user_flag_set() == false, ExcInternalError()); # endif @@ -5248,7 +5243,7 @@ namespace internal { std::vector local_dof_indices; - for (auto cell : dof_handler->active_cell_iterators()) + for (const auto &cell : dof_handler->active_cell_iterators()) if (!cell->is_artificial()) { local_dof_indices.resize(cell->get_fe().dofs_per_cell); @@ -5373,12 +5368,9 @@ namespace internal // TODO: make this code simpler with the new constructors of // NumberCache make indices consecutive level_number_cache.n_locally_owned_dofs = 0; - for (std::vector::iterator it = - renumbering.begin(); - it != renumbering.end(); - ++it) - if (*it != numbers::invalid_dof_index) - *it = level_number_cache.n_locally_owned_dofs++; + for (types::global_dof_index &index : renumbering) + if (index != numbers::invalid_dof_index) + index = level_number_cache.n_locally_owned_dofs++; //* 3. communicate local dofcount and shift ids to make // them unique @@ -5400,12 +5392,9 @@ namespace internal level_number_cache.n_locally_owned_dofs_per_processor.begin() + triangulation->locally_owned_subdomain(), static_cast(0)); - for (std::vector::iterator it = - renumbering.begin(); - it != renumbering.end(); - ++it) - if (*it != numbers::invalid_dof_index) - (*it) += shift; + for (types::global_dof_index &index : renumbering) + if (index != numbers::invalid_dof_index) + index += shift; // now re-enumerate all dofs to this shifted and condensed // numbering form. we renumber some dofs as invalid, so @@ -5669,7 +5658,7 @@ namespace internal triangulation->clear_user_flags(); // mark all own cells for transfer - for (auto cell : dof_handler->active_cell_iterators()) + for (const auto &cell : dof_handler->active_cell_iterators()) if (!cell->is_artificial()) cell->set_user_flag(); diff --git a/source/dofs/dof_renumbering.cc b/source/dofs/dof_renumbering.cc index 577e70657a..93b308b6a2 100644 --- a/source/dofs/dof_renumbering.cc +++ b/source/dofs/dof_renumbering.cc @@ -447,17 +447,17 @@ namespace DoFRenumbering DoFTools::extract_locally_active_dofs(dof_handler, locally_active_dofs); bool needs_locally_active = false; - for (unsigned int i = 0; i < starting_indices.size(); ++i) + for (const auto starting_index : starting_indices) { if ((needs_locally_active == /* previously already set to */ true) || - (locally_owned_dofs.is_element(starting_indices[i]) == false)) + (locally_owned_dofs.is_element(starting_index) == false)) { Assert( - locally_active_dofs.is_element(starting_indices[i]), + locally_active_dofs.is_element(starting_index), ExcMessage( "You specified global degree of freedom " + - Utilities::to_string(starting_indices[i]) + + Utilities::to_string(starting_index) + " as a starting index, but this index is not among the " "locally active ones on this processor, as required " "for this function.")); @@ -589,8 +589,8 @@ namespace DoFRenumbering // above, we ended up with new_indices only containing the local // indices of the locally-owned DoFs. so that's where we get the // indices - for (std::size_t i = 0; i < new_indices.size(); ++i) - new_indices[i] = locally_owned_dofs.nth_index_in_set(new_indices[i]); + for (types::global_dof_index &new_index : new_indices) + new_index = locally_owned_dofs.nth_index_in_set(new_index); } } @@ -736,11 +736,11 @@ namespace DoFRenumbering ExcDimensionMismatch(component_order.size(), fe_collection.n_components())); - for (unsigned int i = 0; i < component_order.size(); ++i) - Assert(component_order[i] < fe_collection.n_components(), - ExcIndexRange(component_order[i], - 0, - fe_collection.n_components())); + for (const unsigned int component : component_order) + { + (void)component; + AssertIndexRange(component, fe_collection.n_components()); + } // vector to hold the dof indices on // the cell we visit at a time diff --git a/source/dofs/dof_tools.cc b/source/dofs/dof_tools.cc index 3ce175aeb2..9019292628 100644 --- a/source/dofs/dof_tools.cc +++ b/source/dofs/dof_tools.cc @@ -883,14 +883,11 @@ namespace DoFTools // achieve this by extending halo_dofs with DoFs to which // halo_dofs are constrained. std::set extra_halo; - for (std::set::iterator it = - dofs_with_support_on_halo_cells.begin(); - it != dofs_with_support_on_halo_cells.end(); - ++it) + for (const auto dof : dofs_with_support_on_halo_cells) // if halo DoF is constrained, add all DoFs to which it's constrained // because after resolving constraints, the support of the DoFs that // constrain the current DoF will extend to the halo cells. - if (const auto *line_ptr = cm.get_constraint_entries(*it)) + if (const auto *line_ptr = cm.get_constraint_entries(dof)) { const unsigned int line_size = line_ptr->size(); for (unsigned int j = 0; j < line_size; ++j) @@ -947,7 +944,7 @@ namespace DoFTools // this function is similar to the make_sparsity_pattern function, // see there for more information - for (auto cell : dof_handler.active_cell_iterators()) + for (const auto &cell : dof_handler.active_cell_iterators()) if (!cell->is_artificial()) { for (unsigned int face = 0; @@ -993,7 +990,7 @@ namespace DoFTools const FiniteElement &fe = dof_handler.get_fe(); - for (auto cell : dof_handler.active_cell_iterators()) + for (const auto &cell : dof_handler.active_cell_iterators()) if (!cell->is_artificial()) for (unsigned int f = 0; f < GeometryInfo::faces_per_cell; ++f) { @@ -1123,12 +1120,9 @@ namespace DoFTools dof_indices.resize(cell->get_fe().dofs_per_cell); cell->get_dof_indices(dof_indices); - for (std::vector::iterator it = - dof_indices.begin(); - it != dof_indices.end(); - ++it) - if (!dof_set.is_element(*it)) - global_dof_indices.insert(*it); + for (const types::global_dof_index dof_index : dof_indices) + if (!dof_set.is_element(dof_index)) + global_dof_indices.insert(dof_index); } dof_set.add_indices(global_dof_indices.begin(), global_dof_indices.end()); @@ -1165,9 +1159,9 @@ namespace DoFTools { dof_indices.resize(cell->get_fe().dofs_per_cell); cell->get_dof_indices(dof_indices); - for (unsigned int i = 0; i < dof_indices.size(); ++i) - if (!dof_set.is_element(dof_indices[i])) - dofs_on_ghosts.push_back(dof_indices[i]); + for (const auto dof_index : dof_indices) + if (!dof_set.is_element(dof_index)) + dofs_on_ghosts.push_back(dof_index); } // sort, compress out duplicates, fill into index set @@ -1213,9 +1207,9 @@ namespace DoFTools dof_indices.resize(cell->get_fe().dofs_per_cell); cell->get_mg_dof_indices(dof_indices); - for (unsigned int i = 0; i < dof_indices.size(); ++i) - if (!dof_set.is_element(dof_indices[i])) - dofs_on_ghosts.push_back(dof_indices[i]); + for (const auto dof_index : dof_indices) + if (!dof_set.is_element(dof_index)) + dofs_on_ghosts.push_back(dof_index); } // sort, compress out duplicates, fill into index set @@ -1291,12 +1285,8 @@ namespace DoFTools std::vector(n_selected_dofs, false)); // Loop over all owned cells and ask the element for the constant modes - - typename DoFHandlerType::active_cell_iterator cell = - dof_handler.begin_active(), - endc = dof_handler.end(); std::vector dof_indices; - for (; cell != endc; ++cell) + for (const auto &cell : dof_handler.active_cell_iterators()) if (cell->is_locally_owned()) { dof_indices.resize(cell->get_fe().dofs_per_cell); @@ -1309,16 +1299,12 @@ namespace DoFTools locally_owned_dofs.index_within_set(dof_indices[i]); const unsigned int comp = dofs_by_component[loc_index]; if (component_mask[comp]) - for (unsigned int j = 0; - j < constant_mode_to_component_translation[comp].size(); - ++j) - constant_modes - [constant_mode_to_component_translation[comp][j].first] - [component_numbering[loc_index]] = - element_constant_modes[cell->active_fe_index()]( - constant_mode_to_component_translation[comp][j] - .second, - i); + for (auto &indices : + constant_mode_to_component_translation[comp]) + constant_modes[indices + .first][component_numbering[loc_index]] = + element_constant_modes[cell->active_fe_index()]( + indices.second, i); } } } @@ -1500,11 +1486,9 @@ namespace DoFTools local_dof_indices.resize(cell->get_fe().dofs_per_cell); cell->get_dof_indices(local_dof_indices); - for (std::vector::iterator it = - local_dof_indices.begin(); - it != local_dof_indices.end(); - ++it) - subdomain_halo_global_dof_indices.insert(*it); + for (const types::global_dof_index local_dof_index : + local_dof_indices) + subdomain_halo_global_dof_indices.insert(local_dof_index); } dof_set[subdomain_id].add_indices( @@ -2499,8 +2483,8 @@ namespace DoFTools } else { - for (types::global_dof_index j = 0; j < indices.size(); ++j) - block_list.add(0, indices[j]); + for (const auto index : indices) + block_list.add(0, index); } } } @@ -2735,8 +2719,8 @@ namespace DoFTools } else { - for (unsigned int j = 0; j < indices.size(); ++j) - block_list.add(block, indices[j]); + for (const auto index : indices) + block_list.add(block, index); } } } diff --git a/source/dofs/dof_tools_constraints.cc b/source/dofs/dof_tools_constraints.cc index e3571730af..6550f7adc3 100644 --- a/source/dofs/dof_tools_constraints.cc +++ b/source/dofs/dof_tools_constraints.cc @@ -1212,10 +1212,13 @@ namespace DoFTools subface->get_dof_indices(slave_dofs, subface_fe_index); - for (unsigned int i = 0; i < slave_dofs.size(); ++i) - Assert(slave_dofs[i] != - numbers::invalid_dof_index, - ExcInternalError()); + for (const types::global_dof_index slave_dof : + slave_dofs) + { + (void)slave_dof; + Assert(slave_dof != numbers::invalid_dof_index, + ExcInternalError()); + } // Now create the element constraint for this // subface. @@ -3507,14 +3510,14 @@ namespace DoFTools // enter those dofs into the list that match the component // signature. - for (unsigned int i = 0; i < face_dofs.size(); ++i) + for (const types::global_dof_index face_dof : face_dofs) { // Find out if a dof has a contribution in this component, // and if so, add it to the list const std::vector::iterator it_index_on_cell = std::find(cell_dofs.begin(), cell_dofs.end(), - face_dofs[i]); + face_dof); Assert(it_index_on_cell != cell_dofs.end(), ExcInvalidIterator()); const unsigned int index_on_cell = @@ -3530,7 +3533,7 @@ namespace DoFTools } if (nonzero) - zero_boundary_constraints.add_line(face_dofs[i]); + zero_boundary_constraints.add_line(face_dof); } } } diff --git a/source/dofs/dof_tools_sparsity.cc b/source/dofs/dof_tools_sparsity.cc index fbef24e408..0856bbe681 100644 --- a/source/dofs/dof_tools_sparsity.cc +++ b/source/dofs/dof_tools_sparsity.cc @@ -343,12 +343,9 @@ namespace DoFTools if (sparsity.n_rows() != 0) { types::global_dof_index max_element = 0; - for (std::vector::const_iterator i = - dof_to_boundary_mapping.begin(); - i != dof_to_boundary_mapping.end(); - ++i) - if ((*i != numbers::invalid_dof_index) && (*i > max_element)) - max_element = *i; + for (const types::global_dof_index index : dof_to_boundary_mapping) + if ((index != numbers::invalid_dof_index) && (index > max_element)) + max_element = index; AssertDimension(max_element, sparsity.n_rows() - 1); }; #endif @@ -447,12 +444,9 @@ namespace DoFTools if (sparsity.n_rows() != 0) { types::global_dof_index max_element = 0; - for (std::vector::const_iterator i = - dof_to_boundary_mapping.begin(); - i != dof_to_boundary_mapping.end(); - ++i) - if ((*i != numbers::invalid_dof_index) && (*i > max_element)) - max_element = *i; + for (const types::global_dof_index index : dof_to_boundary_mapping) + if ((index != numbers::invalid_dof_index) && (index > max_element)) + max_element = index; AssertDimension(max_element, sparsity.n_rows() - 1); }; #endif