Skip to content

Commit a52140a

Browse files
authored
Merge pull request #2426 from fwesselm/rawLoops1
Simplify some for loops (part 1)
2 parents 7946c89 + 7c2b025 commit a52140a

File tree

12 files changed

+32
-43
lines changed

12 files changed

+32
-43
lines changed

highs/io/FilereaderEms.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,10 @@ HighsStatus FilereaderEms::writeModelToFile(const HighsOptions& options,
269269
f << "names" << std::endl;
270270

271271
f << "columns" << std::endl;
272-
for (size_t i = 0; i < lp.col_names_.size(); i++)
273-
f << lp.col_names_[i] << std::endl;
272+
for (const auto& name : lp.col_names_) f << name << std::endl;
274273

275274
f << "rows" << std::endl;
276-
for (size_t i = 0; i < lp.row_names_.size(); i++)
277-
f << lp.row_names_[i] << std::endl;
275+
for (const auto& name : lp.row_names_) f << name << std::endl;
278276
}
279277

280278
// todo: integer variables.

highs/io/FilereaderLp.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ FilereaderRetcode FilereaderLp::readModelFromFile(const HighsOptions& options,
7070
//
7171
lp.offset_ = m.objective->offset;
7272
lp.col_cost_.resize(lp.num_col_, 0.0);
73-
for (size_t i = 0; i < m.objective->linterms.size(); i++) {
74-
std::shared_ptr<LinTerm> lt = m.objective->linterms[i];
73+
for (const auto& lt : m.objective->linterms) {
7574
lp.col_cost_[varindex[lt->var->name]] = lt->coef;
7675
}
7776

@@ -177,8 +176,7 @@ FilereaderRetcode FilereaderLp::readModelFromFile(const HighsOptions& options,
177176
// column 0, so have to clear this before pushing back start
178177
lp.a_matrix_.start_.clear();
179178
assert((int)lp.a_matrix_.start_.size() == 0);
180-
for (HighsInt i = 0; i < lp.num_col_; i++) {
181-
std::shared_ptr<Variable> var = m.variables[i];
179+
for (const auto& var : m.variables) {
182180
lp.a_matrix_.start_.push_back(nz);
183181
for (size_t j = 0; j < consofvarmap_index[var].size(); j++) {
184182
double value = consofvarmap_value[var][j];

highs/io/HMpsFF.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ FreeFormatParserReturnCode HMpsFF::loadProblem(
108108

109109
// Only set up lp.integrality_ if non-continuous
110110
bool is_mip = false;
111-
for (size_t iCol = 0; iCol < col_integrality.size(); iCol++) {
112-
if (col_integrality[iCol] != HighsVarType::kContinuous) {
111+
for (const auto& var_type : col_integrality) {
112+
if (var_type != HighsVarType::kContinuous) {
113113
is_mip = true;
114114
break;
115115
}

highs/lp_data/HighsIis.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,8 @@ HighsStatus HighsIis::getData(const HighsLp& lp, const HighsOptions& options,
204204
if (this->compute(to_lp, options) != HighsStatus::kOk)
205205
return HighsStatus::kError;
206206
// Indirect the values into the original LP
207-
for (HighsInt iCol = 0; iCol < HighsInt(this->col_index_.size()); iCol++)
208-
this->col_index_[iCol] = from_col[this->col_index_[iCol]];
209-
for (HighsInt iRow = 0; iRow < HighsInt(this->row_index_.size()); iRow++)
210-
this->row_index_[iRow] = from_row[this->row_index_[iRow]];
207+
for (HighsInt& colindex : this->col_index_) colindex = from_col[colindex];
208+
for (HighsInt& rowindex : this->row_index_) rowindex = from_row[rowindex];
211209
if (kIisDevReport) this->report("On exit", lp);
212210
return HighsStatus::kOk;
213211
}

highs/lp_data/HighsInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class HighsInfo : public HighsInfoStruct {
169169

170170
private:
171171
void deleteRecords() {
172-
for (size_t i = 0; i < records.size(); i++) delete records[i];
172+
for (auto record : records) delete record;
173173
}
174174

175175
void initRecords() {

highs/lp_data/HighsInterface.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,7 @@ HighsStatus Highs::formStandardFormLp() {
308308
}
309309
}
310310
// Now add the slack variables
311-
for (HighsInt iX = 0; iX < HighsInt(slack_ix.size()); iX++) {
312-
HighsInt iRow = slack_ix[iX];
311+
for (HighsInt iRow : slack_ix) {
313312
this->standard_form_cost_.push_back(0);
314313
if (iRow > 0) {
315314
this->standard_form_matrix_.index_.push_back(iRow - 1);
@@ -3312,11 +3311,11 @@ HighsStatus Highs::computeIllConditioning(
33123311
ss.str().c_str());
33133312
}
33143313
} else {
3315-
for (HighsInt iX = 0; iX < HighsInt(ill_conditioning.record.size()); iX++) {
3314+
for (const auto& rec : ill_conditioning.record) {
33163315
ss.str(std::string());
33173316
bool newline = false;
3318-
double multiplier = ill_conditioning.record[iX].multiplier;
3319-
HighsInt iCol = basic_var[ill_conditioning.record[iX].index];
3317+
double multiplier = rec.multiplier;
3318+
HighsInt iCol = basic_var[rec.index];
33203319
if (iCol < incumbent_lp.num_col_) {
33213320
std::string col_name = has_col_names ? incumbent_lp.col_names_[iCol]
33223321
: "C" + std::to_string(iCol);

highs/lp_data/HighsLpUtils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,9 +788,9 @@ bool costScaleOk(const vector<double>& cost, const HighsInt cost_scale,
788788
const double infinite_cost) {
789789
if (!cost_scale) return true;
790790
double cost_scale_value = std::pow(2, cost_scale);
791-
for (HighsInt iCol = 0; iCol < HighsInt(cost.size()); iCol++)
792-
if (std::abs(cost[iCol]) < kHighsInf &&
793-
std::abs(cost[iCol] * cost_scale_value) > infinite_cost)
791+
for (double c : cost)
792+
if (std::abs(c) < kHighsInf &&
793+
std::abs(c * cost_scale_value) > infinite_cost)
794794
return false;
795795
return true;
796796
}

highs/lp_data/HighsOptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1553,7 +1553,7 @@ class HighsOptions : public HighsOptionsStruct {
15531553
}
15541554

15551555
void deleteRecords() {
1556-
for (size_t i = 0; i < records.size(); i++) delete records[i];
1556+
for (auto record : records) delete record;
15571557
}
15581558

15591559
public:

highs/lp_data/HighsSolution.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,8 +1772,8 @@ void reportLpKktFailures(const HighsLp& lp, const HighsOptions& options,
17721772
}
17731773

17741774
bool HighsSolution::hasUndefined() const {
1775-
for (HighsInt iCol = 0; iCol < HighsInt(this->col_value.size()); iCol++)
1776-
if (this->col_value[iCol] == kHighsUndefined) return true;
1775+
for (double value : this->col_value)
1776+
if (value == kHighsUndefined) return true;
17771777
return false;
17781778
}
17791779

highs/mip/HighsDomain.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2924,8 +2924,7 @@ bool HighsDomain::ConflictSet::resolveLinearGeq(HighsCDouble M, double Mupper,
29242924
resolvedDomainChanges.clear();
29252925
double covered = double(M - Mupper);
29262926
if (covered > 0) {
2927-
for (HighsInt k = 0; k < (HighsInt)resolveBuffer.size(); ++k) {
2928-
ResolveCandidate& reasonDomchg = resolveBuffer[k];
2927+
for (const auto& reasonDomchg : resolveBuffer) {
29292928
LocalDomChg locdomchg;
29302929
locdomchg.pos = reasonDomchg.boundPos;
29312930
locdomchg.domchg = localdom.domchgstack_[reasonDomchg.boundPos];
@@ -3034,8 +3033,7 @@ bool HighsDomain::ConflictSet::resolveLinearLeq(HighsCDouble M, double Mlower,
30343033
resolvedDomainChanges.clear();
30353034
double covered = double(M - Mlower);
30363035
if (covered < 0) {
3037-
for (HighsInt k = 0; k < (HighsInt)resolveBuffer.size(); ++k) {
3038-
ResolveCandidate& reasonDomchg = resolveBuffer[k];
3036+
for (const auto& reasonDomchg : resolveBuffer) {
30393037
LocalDomChg locdomchg;
30403038
locdomchg.pos = reasonDomchg.boundPos;
30413039
locdomchg.domchg = localdom.domchgstack_[reasonDomchg.boundPos];

0 commit comments

Comments
 (0)