From: Arnaud Giersch Date: Wed, 26 Feb 2020 21:12:26 +0000 (+0100) Subject: [sonar] Change raw for-loops to range for-loops. X-Git-Tag: v3.26~869 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/40dc178ab282a23a717380eedcfc0336134a1dc5 [sonar] Change raw for-loops to range for-loops. --- diff --git a/examples/s4u/platform-properties/s4u-platform-properties.cpp b/examples/s4u/platform-properties/s4u-platform-properties.cpp index 1720303ef4..57d47ccdc9 100644 --- a/examples/s4u/platform-properties/s4u-platform-properties.cpp +++ b/examples/s4u/platform-properties/s4u-platform-properties.cpp @@ -119,8 +119,8 @@ int main(int argc, char* argv[]) XBT_INFO("There are %zu hosts in the environment", totalHosts); std::vector hosts = e.get_all_hosts(); - for (unsigned int i = 0; i < hosts.size(); i++) - XBT_INFO("Host '%s' runs at %.0f flops/s", hosts[i]->get_cname(), hosts[i]->get_speed()); + for (simgrid::s4u::Host const* host : hosts) + XBT_INFO("Host '%s' runs at %.0f flops/s", host->get_cname(), host->get_speed()); e.load_deployment(argv[2]); e.run(); diff --git a/src/kernel/routing/FatTreeZone.cpp b/src/kernel/routing/FatTreeZone.cpp index 46d7449dbd..820dfba65f 100644 --- a/src/kernel/routing/FatTreeZone.cpp +++ b/src/kernel/routing/FatTreeZone.cpp @@ -30,11 +30,11 @@ FatTreeZone::FatTreeZone(NetZoneImpl* father, const std::string& name, resource: FatTreeZone::~FatTreeZone() { - for (unsigned int i = 0; i < this->nodes_.size(); i++) { - delete this->nodes_[i]; + for (FatTreeNode const* node : this->nodes_) { + delete node; } - for (unsigned int i = 0; i < this->links_.size(); i++) { - delete this->links_[i]; + for (FatTreeLink const* link : this->links_) { + delete link; } } @@ -149,8 +149,8 @@ void FatTreeZone::seal() msgBuffer.str(""); msgBuffer << "Nodes are : "; - for (unsigned int i = 0; i < this->nodes_.size(); i++) { - msgBuffer << this->nodes_[i]->id << "(" << this->nodes_[i]->level << "," << this->nodes_[i]->position << ") "; + for (FatTreeNode const* node : this->nodes_) { + msgBuffer << node->id << "(" << node->level << "," << node->position << ") "; } XBT_DEBUG("%s", msgBuffer.str().c_str()); } @@ -169,8 +169,8 @@ void FatTreeZone::seal() if (XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) { std::stringstream msgBuffer; msgBuffer << "Links are : "; - for (unsigned int i = 0; i < this->links_.size(); i++) { - msgBuffer << "(" << this->links_[i]->up_node_->id << "," << this->links_[i]->down_node_->id << ") "; + for (FatTreeLink const* link : this->links_) { + msgBuffer << "(" << link->up_node_->id << "," << link->down_node_->id << ") "; } XBT_DEBUG("%s", msgBuffer.str().c_str()); } @@ -380,11 +380,11 @@ void FatTreeZone::parse_specific_arguments(ClusterCreationArgs* cluster) " levels but the child count vector (the first one) contains " + std::to_string(tmp.size()) + " levels."); - for (size_t i = 0; i < tmp.size(); i++) { + for (std::string const& level : tmp) { try { - this->num_children_per_node_.push_back(std::stoi(tmp[i])); + this->num_children_per_node_.push_back(std::stoi(level)); } catch (const std::invalid_argument&) { - surf_parse_error(std::string("Invalid child count: ") + tmp[i]); + surf_parse_error(std::string("Invalid child count: ") + level); } } @@ -393,11 +393,11 @@ void FatTreeZone::parse_specific_arguments(ClusterCreationArgs* cluster) surf_parse_assert(tmp.size() == this->levels_, std::string("You specified ") + std::to_string(this->levels_) + " levels but the parent count vector (the second one) contains " + std::to_string(tmp.size()) + " levels."); - for (size_t i = 0; i < tmp.size(); i++) { + for (std::string const& parent : tmp) { try { - this->num_parents_per_node_.push_back(std::stoi(tmp[i])); + this->num_parents_per_node_.push_back(std::stoi(parent)); } catch (const std::invalid_argument&) { - surf_parse_error(std::string("Invalid parent count: ") + tmp[i]); + surf_parse_error(std::string("Invalid parent count: ") + parent); } } @@ -406,11 +406,11 @@ void FatTreeZone::parse_specific_arguments(ClusterCreationArgs* cluster) surf_parse_assert(tmp.size() == this->levels_, std::string("You specified ") + std::to_string(this->levels_) + " levels but the port count vector (the third one) contains " + std::to_string(tmp.size()) + " levels."); - for (size_t i = 0; i < tmp.size(); i++) { + for (std::string const& port : tmp) { try { - this->num_port_lower_level_.push_back(std::stoi(tmp[i])); + this->num_port_lower_level_.push_back(std::stoi(port)); } catch (const std::invalid_argument&) { - throw std::invalid_argument(std::string("Invalid lower level port number:") + tmp[i]); + throw std::invalid_argument(std::string("Invalid lower level port number:") + port); } } this->cluster_ = cluster; @@ -423,16 +423,16 @@ void FatTreeZone::generate_dot_file(const std::string& filename) const xbt_assert(file.is_open(), "Unable to open file %s", filename.c_str()); file << "graph AsClusterFatTree {\n"; - for (unsigned int i = 0; i < this->nodes_.size(); i++) { - file << this->nodes_[i]->id; - if (this->nodes_[i]->id < 0) + for (FatTreeNode const* node : this->nodes_) { + file << node->id; + if (node->id < 0) file << " [shape=circle];\n"; else file << " [shape=hexagon];\n"; } - for (unsigned int i = 0; i < this->links_.size(); i++) { - file << this->links_[i]->down_node_->id << " -- " << this->links_[i]->up_node_->id << ";\n"; + for (FatTreeLink const* link : this->links_) { + file << link->down_node_->id << " -- " << link->up_node_->id << ";\n"; } file << "}"; file.close(); diff --git a/src/plugins/host_energy.cpp b/src/plugins/host_energy.cpp index 1f4ba650f1..b68edc8aee 100644 --- a/src/plugins/host_energy.cpp +++ b/src/plugins/host_energy.cpp @@ -512,15 +512,13 @@ static void on_host_destruction(simgrid::s4u::Host const& host) static void on_simulation_end() { - std::vector hosts = simgrid::s4u::Engine::get_instance()->get_all_hosts(); - double total_energy = 0.0; // Total energy consumption (whole platform) double used_hosts_energy = 0.0; // Energy consumed by hosts that computed something - for (size_t i = 0; i < hosts.size(); i++) { - if (dynamic_cast(hosts[i]) == nullptr) { // Ignore virtual machines - double energy = hosts[i]->extension()->get_consumed_energy(); + for (simgrid::s4u::Host const* host : simgrid::s4u::Engine::get_instance()->get_all_hosts()) { + if (dynamic_cast(host) == nullptr) { // Ignore virtual machines + double energy = host->extension()->get_consumed_energy(); total_energy += energy; - if (hosts[i]->extension()->host_was_used_) + if (host->extension()->host_was_used_) used_hosts_energy += energy; } } diff --git a/src/surf/network_ib.cpp b/src/surf/network_ib.cpp index 70782d1115..90cb7daabd 100644 --- a/src/surf/network_ib.cpp +++ b/src/surf/network_ib.cpp @@ -116,11 +116,11 @@ void NetworkIBModel::computeIBfactors(IBNode* root) double num_comm_out = root->ActiveCommsUp.size(); double max_penalty_out = 0.0; // first, compute all outbound penalties to get their max - for (std::vector::iterator it = root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) { + for (ActiveComm const* comm : root->ActiveCommsUp) { double my_penalty_out = 1.0; if (num_comm_out != 1) { - if ((*it)->destination->nbActiveCommsDown > 2) // number of comms sent to the receiving node + if (comm->destination->nbActiveCommsDown > 2) // number of comms sent to the receiving node my_penalty_out = num_comm_out * Bs * ys; else my_penalty_out = num_comm_out * Bs; @@ -129,30 +129,30 @@ void NetworkIBModel::computeIBfactors(IBNode* root) max_penalty_out = std::max(max_penalty_out, my_penalty_out); } - for (std::vector::iterator it = root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) { + for (ActiveComm* comm : root->ActiveCommsUp) { // compute inbound penalty double my_penalty_in = 1.0; - int nb_comms = (*it)->destination->nbActiveCommsDown; // total number of incoming comms + int nb_comms = comm->destination->nbActiveCommsDown; // total number of incoming comms if (nb_comms != 1) - my_penalty_in = ((*it)->destination->ActiveCommsDown)[root] // number of comm sent to dest by root node - * Be * (*it)->destination->ActiveCommsDown.size(); // number of different nodes sending to dest + my_penalty_in = (comm->destination->ActiveCommsDown)[root] // number of comm sent to dest by root node + * Be * comm->destination->ActiveCommsDown.size(); // number of different nodes sending to dest double penalty = std::max(my_penalty_in, max_penalty_out); - double rate_before_update = (*it)->action->get_bound(); + double rate_before_update = comm->action->get_bound(); // save initial rate of the action - if ((*it)->init_rate == -1) - (*it)->init_rate = rate_before_update; + if (comm->init_rate == -1) + comm->init_rate = rate_before_update; - double penalized_bw = num_comm_out ? (*it)->init_rate / penalty : (*it)->init_rate; + double penalized_bw = num_comm_out ? comm->init_rate / penalty : comm->init_rate; if (not double_equals(penalized_bw, rate_before_update, sg_surf_precision)) { XBT_DEBUG("%d->%d action %p penalty updated : bw now %f, before %f , initial rate %f", root->id, - (*it)->destination->id, (*it)->action, penalized_bw, (*it)->action->get_bound(), (*it)->init_rate); - get_maxmin_system()->update_variable_bound((*it)->action->get_variable(), penalized_bw); + comm->destination->id, comm->action, penalized_bw, comm->action->get_bound(), comm->init_rate); + get_maxmin_system()->update_variable_bound(comm->action->get_variable(), penalized_bw); } else { - XBT_DEBUG("%d->%d action %p penalty not updated : bw %f, initial rate %f", root->id, (*it)->destination->id, - (*it)->action, penalized_bw, (*it)->init_rate); + XBT_DEBUG("%d->%d action %p penalty not updated : bw %f, initial rate %f", root->id, comm->destination->id, + comm->action, penalized_bw, comm->init_rate); } } XBT_DEBUG("Finished computing IB penalties"); @@ -164,13 +164,13 @@ void NetworkIBModel::updateIBfactors_rec(IBNode* root, std::vector& update XBT_DEBUG("IB - Updating rec %d", root->id); computeIBfactors(root); updatedlist[root->id] = true; - for (std::vector::iterator it = root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) { - if (not updatedlist[(*it)->destination->id]) - updateIBfactors_rec((*it)->destination, updatedlist); + for (ActiveComm const* comm : root->ActiveCommsUp) { + if (not updatedlist[comm->destination->id]) + updateIBfactors_rec(comm->destination, updatedlist); } - for (std::map::iterator it = root->ActiveCommsDown.begin(); it != root->ActiveCommsDown.end(); ++it) { - if (not updatedlist[it->first->id]) - updateIBfactors_rec(it->first, updatedlist); + for (std::map::value_type const& comm : root->ActiveCommsDown) { + if (not updatedlist[comm.first->id]) + updateIBfactors_rec(comm.first, updatedlist); } } } @@ -187,12 +187,12 @@ void NetworkIBModel::updateIBfactors(NetworkAction* action, IBNode* from, IBNode to->ActiveCommsDown[from] -= 1; to->nbActiveCommsDown--; - for (std::vector::iterator it = from->ActiveCommsUp.begin(); it != from->ActiveCommsUp.end(); ++it) { - if ((*it)->action == action) { - delete *it; - from->ActiveCommsUp.erase(it); - break; - } + std::vector::iterator it = + std::find_if(begin(from->ActiveCommsUp), end(from->ActiveCommsUp), + [action](const ActiveComm* comm) { return comm->action == action; }); + if (it != std::end(from->ActiveCommsUp)) { + delete *it; + from->ActiveCommsUp.erase(it); } action->unref(); } else { diff --git a/teshsuite/s4u/activity-lifecycle/testing_comm.cpp b/teshsuite/s4u/activity-lifecycle/testing_comm.cpp index 9d4dddd496..c40a344f03 100644 --- a/teshsuite/s4u/activity-lifecycle/testing_comm.cpp +++ b/teshsuite/s4u/activity-lifecycle/testing_comm.cpp @@ -24,8 +24,8 @@ static void test_link_off_helper(double delay) simgrid::s4u::ActorPtr receiver = simgrid::s4u::Actor::create("receiver", all_hosts[1], [&start]() { assert_exit(true, 9); double milestone[5] = {0.5, 3.5, 4.5, 7.5, 9.0}; - for (int i = 0; i < 5; i++) - milestone[i] += start; + for (double& m : milestone) + m += start; for (int i = 0; i < 4; i++) { simgrid::s4u::this_actor::sleep_until(milestone[i]); REQUIRE_NETWORK_FAILURE({ @@ -40,8 +40,8 @@ static void test_link_off_helper(double delay) assert_exit(true, 9); int data = 42; double milestone[5] = {1.5, 2.5, 5.5, 6.5, 9.0}; - for (int i = 0; i < 5; i++) - milestone[i] += start; + for (double& m : milestone) + m += start; for (int i = 0; i < 2; i++) { simgrid::s4u::this_actor::sleep_until(milestone[i]); XBT_VERB("dsend(%c)", 'A' + i); diff --git a/teshsuite/xbt/parallel_log_crashtest/parallel_log_crashtest.cpp b/teshsuite/xbt/parallel_log_crashtest/parallel_log_crashtest.cpp index b894023e66..1ea8857de0 100644 --- a/teshsuite/xbt/parallel_log_crashtest/parallel_log_crashtest.cpp +++ b/teshsuite/xbt/parallel_log_crashtest/parallel_log_crashtest.cpp @@ -34,13 +34,13 @@ int main(int argc, char* argv[]) std::thread crashers[crasher_amount]; /* spawn threads */ - for (int i = 0; i < crasher_amount; i++) { - crashers[i] = std::thread(crasher_thread, i); - } + int id = 0; + for (std::thread& thr : crashers) + thr = std::thread(crasher_thread, id++); /* wait for them */ - for (int i = 0; i < crasher_amount; i++) - crashers[i].join(); + for (std::thread& thr : crashers) + thr.join(); return 0; }