Skip to content

Commit 89aedac

Browse files
authored
Merge pull request #2458 from fwesselm/rawLoops2
Replace some more "raw" by range-based loops
2 parents 02662c3 + 21f9964 commit 89aedac

File tree

7 files changed

+102
-107
lines changed

7 files changed

+102
-107
lines changed

check/TestCAPI.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,6 @@ void fullApi() {
617617

618618
// Define all column names to be different
619619
for (HighsInt iCol = 0; iCol < num_col; iCol++) {
620-
const char suffix = iCol + '0';
621620
char name[5]; // 3 chars prefix, 1 char iCol, 1 char 0-terminator
622621
sprintf(name, "%s%" HIGHSINT_FORMAT "", col_prefix, iCol);
623622
const char* name_p = name;
@@ -650,7 +649,6 @@ void fullApi() {
650649

651650
// Define all row names to be different
652651
for (HighsInt iRow = 0; iRow < num_row; iRow++) {
653-
const char suffix = iRow + '0';
654652
char name[5]; // 3 chars prefix, 1 char iCol, 1 char 0-terminator
655653
sprintf(name, "%s%" HIGHSINT_FORMAT "", row_prefix, iRow);
656654
const char* name_p = name;

highs/simplex/HEkk.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,11 +3156,10 @@ bool HEkk::isBadBasisChange(const SimplexAlgorithm algorithm,
31563156
} else {
31573157
// Look to see whether this basis change is in the list of bad
31583158
// ones
3159-
for (HighsInt iX = 0; iX < (HighsInt)bad_basis_change_.size(); iX++) {
3160-
if (bad_basis_change_[iX].variable_out == variable_out &&
3161-
bad_basis_change_[iX].variable_in == variable_in &&
3162-
bad_basis_change_[iX].row_out == row_out) {
3163-
bad_basis_change_[iX].taboo = true;
3159+
for (auto& change : bad_basis_change_) {
3160+
if (change.variable_out == variable_out &&
3161+
change.variable_in == variable_in && change.row_out == row_out) {
3162+
change.taboo = true;
31643163
return true;
31653164
}
31663165
}
@@ -3946,24 +3945,23 @@ HighsInt HEkk::addBadBasisChange(const HighsInt row_out,
39463945
}
39473946

39483947
void HEkk::clearBadBasisChangeTabooFlag() {
3949-
for (HighsInt iX = 0; iX < (HighsInt)bad_basis_change_.size(); iX++)
3950-
bad_basis_change_[iX].taboo = false;
3948+
for (auto& change : bad_basis_change_) change.taboo = false;
39513949
}
39523950

39533951
bool HEkk::tabooBadBasisChange() const {
3954-
for (HighsInt iX = 0; iX < (HighsInt)bad_basis_change_.size(); iX++) {
3955-
if (bad_basis_change_[iX].taboo) return true;
3952+
for (const auto& change : bad_basis_change_) {
3953+
if (change.taboo) return true;
39563954
}
39573955
return false;
39583956
}
39593957

39603958
void HEkk::applyTabooRowOut(vector<double>& values,
39613959
const double overwrite_with) {
3962-
assert((HighsInt)values.size() >= lp_.num_row_);
3963-
for (HighsInt iX = 0; iX < (HighsInt)bad_basis_change_.size(); iX++) {
3964-
if (bad_basis_change_[iX].taboo) {
3965-
HighsInt iRow = bad_basis_change_[iX].row_out;
3966-
bad_basis_change_[iX].save_value = values[iRow];
3960+
assert(values.size() >= static_cast<size_t>(lp_.num_row_));
3961+
for (auto& change : bad_basis_change_) {
3962+
if (change.taboo) {
3963+
HighsInt iRow = change.row_out;
3964+
change.save_value = values[iRow];
39673965
values[iRow] = overwrite_with;
39683966
}
39693967
}
@@ -3982,11 +3980,12 @@ void HEkk::unapplyTabooRowOut(vector<double>& values) {
39823980

39833981
void HEkk::applyTabooVariableIn(vector<double>& values,
39843982
const double overwrite_with) {
3985-
assert((HighsInt)values.size() >= lp_.num_col_ + lp_.num_row_);
3986-
for (HighsInt iX = 0; iX < (HighsInt)bad_basis_change_.size(); iX++) {
3987-
if (bad_basis_change_[iX].taboo) {
3988-
HighsInt iCol = bad_basis_change_[iX].variable_in;
3989-
bad_basis_change_[iX].save_value = values[iCol];
3983+
assert(values.size() >=
3984+
static_cast<size_t>(lp_.num_col_) + static_cast<size_t>(lp_.num_row_));
3985+
for (auto& change : bad_basis_change_) {
3986+
if (change.taboo) {
3987+
HighsInt iCol = change.variable_in;
3988+
change.save_value = values[iCol];
39903989
values[iCol] = overwrite_with;
39913990
}
39923991
}

highs/simplex/HEkkDualMulti.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -947,8 +947,8 @@ void HEkkDual::majorRollback() {
947947
ekk_instance_.updateMatrix(finish->variable_out, finish->variable_in);
948948

949949
// 3. Roll back flips
950-
for (unsigned i = 0; i < finish->flipList.size(); i++) {
951-
ekk_instance_.flipBound(finish->flipList[i]);
950+
for (HighsInt var : finish->flipList) {
951+
ekk_instance_.flipBound(var);
952952
}
953953

954954
// 4. Roll back cost

highs/simplex/HEkkPrimal.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,9 +1214,9 @@ void HEkkPrimal::phase1ChooseRow() {
12141214
pdqsort(ph1SorterR.begin(), ph1SorterR.end());
12151215
double dMaxTheta = ph1SorterR[0].first;
12161216
double dGradient = fabs(theta_dual);
1217-
for (size_t i = 0; i < ph1SorterR.size(); i++) {
1218-
double dMyTheta = ph1SorterR[i].first;
1219-
HighsInt index = ph1SorterR[i].second;
1217+
for (const auto& candidate : ph1SorterR) {
1218+
double dMyTheta = candidate.first;
1219+
HighsInt index = candidate.second;
12201220
HighsInt iRow = index >= 0 ? index : index + num_row;
12211221
dGradient -= fabs(col_aq.array[iRow]);
12221222
// Stop when the gradient start to decrease

highs/test_kkt/KktCh2.cpp

Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -74,35 +74,35 @@ void KktChStep::addChange(int type, HighsInt row, HighsInt col, double valC,
7474
case 171: // new bounds from doubleton equation, retrieve old ones
7575
upd = rLowers.top();
7676
rLowers.pop();
77-
for (size_t i = 0; i < upd.size(); i++) {
78-
HighsInt ind = get<0>(upd[i]);
79-
RrowLower[ind] = get<1>(upd[i]);
77+
for (const auto& bound : upd) {
78+
HighsInt ind = get<0>(bound);
79+
RrowLower[ind] = get<1>(bound);
8080
}
8181
upd = rUppers.top();
8282
rUppers.pop();
83-
for (size_t i = 0; i < upd.size(); i++) {
84-
HighsInt ind = get<0>(upd[i]);
85-
RrowUpper[ind] = get<1>(upd[i]);
83+
for (const auto& bound : upd) {
84+
HighsInt ind = get<0>(bound);
85+
RrowUpper[ind] = get<1>(bound);
8686
}
8787
break;
8888
case 1: // row singleton
8989
upd = cLowers.top();
9090
cLowers.pop();
91-
for (size_t i = 0; i < upd.size(); i++) {
92-
HighsInt ind = get<0>(upd[i]);
93-
RcolLower[ind] = get<1>(upd[i]);
91+
for (const auto& bound : upd) {
92+
HighsInt ind = get<0>(bound);
93+
RcolLower[ind] = get<1>(bound);
9494
}
9595
upd = cUppers.top();
9696
cUppers.pop();
97-
for (size_t i = 0; i < upd.size(); i++) {
98-
HighsInt ind = get<0>(upd[i]);
99-
RcolUpper[ind] = get<1>(upd[i]);
97+
for (const auto& bound : upd) {
98+
HighsInt ind = get<0>(bound);
99+
RcolUpper[ind] = get<1>(bound);
100100
}
101101
upd = costs.top();
102102
costs.pop();
103-
for (size_t i = 0; i < upd.size(); i++) {
104-
HighsInt ind = get<0>(upd[i]);
105-
RcolCost[ind] = get<1>(upd[i]);
103+
for (const auto& cost : upd) {
104+
HighsInt ind = get<0>(cost);
105+
RcolCost[ind] = get<1>(cost);
106106
}
107107
break;
108108
case 2: // each variable at forcing row: rowDual is cost here
@@ -111,146 +111,146 @@ void KktChStep::addChange(int type, HighsInt row, HighsInt col, double valC,
111111
case 22: //
112112
upd = rLowers.top();
113113
rLowers.pop();
114-
for (size_t i = 0; i < upd.size(); i++) {
115-
HighsInt ind = get<0>(upd[i]);
116-
RrowLower[ind] = get<1>(upd[i]);
114+
for (const auto& bound : upd) {
115+
HighsInt ind = get<0>(bound);
116+
RrowLower[ind] = get<1>(bound);
117117
}
118118
upd = rUppers.top();
119119
rUppers.pop();
120-
for (size_t i = 0; i < upd.size(); i++) {
121-
HighsInt ind = get<0>(upd[i]);
122-
RrowUpper[ind] = get<1>(upd[i]);
120+
for (const auto& bound : upd) {
121+
HighsInt ind = get<0>(bound);
122+
RrowUpper[ind] = get<1>(bound);
123123
}
124124
break;
125125
case 3: // the row that is forcing
126126
if (valC != 0) {
127127
upd = rLowers.top();
128128
rLowers.pop();
129-
for (size_t i = 0; i < upd.size(); i++) {
130-
HighsInt ind = get<0>(upd[i]);
131-
RrowLower[ind] = get<1>(upd[i]);
129+
for (const auto& bound : upd) {
130+
HighsInt ind = get<0>(bound);
131+
RrowLower[ind] = get<1>(bound);
132132
}
133133
upd = rUppers.top();
134134
rUppers.pop();
135-
for (size_t i = 0; i < upd.size(); i++) {
136-
HighsInt ind = get<0>(upd[i]);
137-
RrowUpper[ind] = get<1>(upd[i]);
135+
for (const auto& bound : upd) {
136+
HighsInt ind = get<0>(bound);
137+
RrowUpper[ind] = get<1>(bound);
138138
}
139139
}
140140
break;
141141
case 4: // implied free column singleton (also from duplicate row)
142142
upd = costs.top();
143143
costs.pop();
144-
for (size_t i = 0; i < upd.size(); i++) {
145-
HighsInt ind = get<0>(upd[i]);
146-
RcolCost[ind] = get<1>(upd[i]);
144+
for (const auto& cost : upd) {
145+
HighsInt ind = get<0>(cost);
146+
RcolCost[ind] = get<1>(cost);
147147
}
148148
break;
149149
case 5: // doubleton eq with singleton col
150150
upd = cLowers.top();
151151
cLowers.pop();
152-
for (size_t i = 0; i < upd.size(); i++) {
153-
HighsInt ind = get<0>(upd[i]);
154-
RcolLower[ind] = get<1>(upd[i]);
152+
for (const auto& bound : upd) {
153+
HighsInt ind = get<0>(bound);
154+
RcolLower[ind] = get<1>(bound);
155155
}
156156
upd = cUppers.top();
157157
cUppers.pop();
158-
for (size_t i = 0; i < upd.size(); i++) {
159-
HighsInt ind = get<0>(upd[i]);
160-
RcolUpper[ind] = get<1>(upd[i]);
158+
for (const auto& bound : upd) {
159+
HighsInt ind = get<0>(bound);
160+
RcolUpper[ind] = get<1>(bound);
161161
}
162162
upd = costs.top();
163163
costs.pop();
164-
for (size_t i = 0; i < upd.size(); i++) {
165-
HighsInt ind = get<0>(upd[i]);
166-
RcolCost[ind] = get<1>(upd[i]);
164+
for (const auto& cost : upd) {
165+
HighsInt ind = get<0>(cost);
166+
RcolCost[ind] = get<1>(cost);
167167
}
168168
break;
169169
case 17: { // doubleton equation
170170
upd = cLowers.top();
171171
cLowers.pop();
172-
for (size_t i = 0; i < upd.size(); i++) {
173-
HighsInt ind = get<0>(upd[i]);
174-
RcolLower[ind] = get<1>(upd[i]);
172+
for (const auto& bound : upd) {
173+
HighsInt ind = get<0>(bound);
174+
RcolLower[ind] = get<1>(bound);
175175
}
176176
upd = cUppers.top();
177177
cUppers.pop();
178-
for (size_t i = 0; i < upd.size(); i++) {
179-
HighsInt ind = get<0>(upd[i]);
180-
RcolUpper[ind] = get<1>(upd[i]);
178+
for (const auto& bound : upd) {
179+
HighsInt ind = get<0>(bound);
180+
RcolUpper[ind] = get<1>(bound);
181181
}
182182
upd = costs.top();
183183
costs.pop();
184-
for (size_t i = 0; i < upd.size(); i++) {
185-
HighsInt ind = get<0>(upd[i]);
186-
RcolCost[ind] = get<1>(upd[i]);
184+
for (const auto& cost : upd) {
185+
HighsInt ind = get<0>(cost);
186+
RcolCost[ind] = get<1>(cost);
187187
}
188188
break;
189189
}
190190
case 6: // empty column, dominated column or weakly dominated
191191
if (valC != 0) {
192192
upd = rLowers.top();
193193
rLowers.pop();
194-
for (size_t i = 0; i < upd.size(); i++) {
195-
HighsInt ind = get<0>(upd[i]);
196-
RrowLower[ind] = get<1>(upd[i]);
194+
for (const auto& bound : upd) {
195+
HighsInt ind = get<0>(bound);
196+
RrowLower[ind] = get<1>(bound);
197197
}
198198
upd = rUppers.top();
199199
rUppers.pop();
200-
for (size_t i = 0; i < upd.size(); i++) {
201-
HighsInt ind = get<0>(upd[i]);
202-
RrowUpper[ind] = get<1>(upd[i]);
200+
for (const auto& bound : upd) {
201+
HighsInt ind = get<0>(bound);
202+
RrowUpper[ind] = get<1>(bound);
203203
}
204204
}
205205
break;
206206
case 7: // fixed variable
207207
if (valC != 0) {
208208
upd = rLowers.top();
209209
rLowers.pop();
210-
for (size_t i = 0; i < upd.size(); i++) {
211-
HighsInt ind = get<0>(upd[i]);
212-
RrowLower[ind] = get<1>(upd[i]);
210+
for (const auto& bound : upd) {
211+
HighsInt ind = get<0>(bound);
212+
RrowLower[ind] = get<1>(bound);
213213
}
214214
upd = rUppers.top();
215215
rUppers.pop();
216-
for (size_t i = 0; i < upd.size(); i++) {
217-
HighsInt ind = get<0>(upd[i]);
218-
RrowUpper[ind] = get<1>(upd[i]);
216+
for (const auto& bound : upd) {
217+
HighsInt ind = get<0>(bound);
218+
RrowUpper[ind] = get<1>(bound);
219219
}
220220
}
221221
break;
222222
case 11: // empty row from duplicate rows
223223
upd = rLowers.top();
224224
rLowers.pop();
225-
for (size_t i = 0; i < upd.size(); i++) {
226-
HighsInt ind = get<0>(upd[i]);
227-
RrowLower[ind] = get<1>(upd[i]);
225+
for (const auto& bound : upd) {
226+
HighsInt ind = get<0>(bound);
227+
RrowLower[ind] = get<1>(bound);
228228
}
229229
upd = rUppers.top();
230230
rUppers.pop();
231-
for (size_t i = 0; i < upd.size(); i++) {
232-
HighsInt ind = get<0>(upd[i]);
233-
RrowUpper[ind] = get<1>(upd[i]);
231+
for (const auto& bound : upd) {
232+
HighsInt ind = get<0>(bound);
233+
RrowUpper[ind] = get<1>(bound);
234234
}
235235
break;
236236
case 12: // doubleton eq from duplicate rows;
237237
upd = cLowers.top();
238238
cLowers.pop();
239-
for (size_t i = 0; i < upd.size(); i++) {
240-
HighsInt ind = get<0>(upd[i]);
241-
RcolLower[ind] = get<1>(upd[i]);
239+
for (const auto& bound : upd) {
240+
HighsInt ind = get<0>(bound);
241+
RcolLower[ind] = get<1>(bound);
242242
}
243243
upd = cUppers.top();
244244
cUppers.pop();
245-
for (size_t i = 0; i < upd.size(); i++) {
246-
HighsInt ind = get<0>(upd[i]);
247-
RcolUpper[ind] = get<1>(upd[i]);
245+
for (const auto& bound : upd) {
246+
HighsInt ind = get<0>(bound);
247+
RcolUpper[ind] = get<1>(bound);
248248
}
249249
upd = costs.top();
250250
costs.pop();
251-
for (size_t i = 0; i < upd.size(); i++) {
252-
HighsInt ind = get<0>(upd[i]);
253-
RcolCost[ind] = get<1>(upd[i]);
251+
for (const auto& cost : upd) {
252+
HighsInt ind = get<0>(cost);
253+
RcolCost[ind] = get<1>(cost);
254254
}
255255
break;
256256
case 121: //

highs/util/HFactor.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,8 +2120,7 @@ void HFactor::updateCFT(HVector* aq, HVector* ep, HighsInt* iRow
21202120
dwork[iRow[cp]] = 0;
21212121
HighsInt u_countX = t_start[cp];
21222122
HighsInt u_startX = u_countX;
2123-
for (unsigned i = 0; i < iwork.size(); i++) {
2124-
HighsInt index = iwork[i];
2123+
for (HighsInt index : iwork) {
21252124
double value = dwork[index];
21262125
dwork[index] = 0; // This effectively removes all duplication
21272126
if (fabs(value) > kHighsTiny) {
@@ -2181,8 +2180,7 @@ void HFactor::updateCFT(HVector* aq, HVector* ep, HighsInt* iRow
21812180
// 7. Store BTRAN result to FT elimination, update logic helper
21822181
dwork[iRow[cp]] = 0;
21832182
double pivot_multiplier = -p_value[cp];
2184-
for (unsigned i = 0; i < iwork.size(); i++) {
2185-
HighsInt index = iwork[i];
2183+
for (HighsInt index : iwork) {
21862184
double value = dwork[index];
21872185
dwork[index] = 0;
21882186
if (fabs(value) > kHighsTiny) {

highs/util/HVectorBase.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ void HVectorBase<Real>::tight() {
8787
HighsInt totalCount = 0;
8888
using std::abs;
8989
if (count < 0) {
90-
for (size_t my_index = 0; my_index < array.size(); my_index++)
91-
if (abs(array[my_index]) < kHighsTiny) array[my_index] = 0;
90+
for (auto& val : array)
91+
if (abs(val) < kHighsTiny) val = 0;
9292
} else {
9393
for (HighsInt i = 0; i < count; i++) {
9494
const HighsInt my_index = index[i];

0 commit comments

Comments
 (0)