Skip to content

Commit 86f20cc

Browse files
rpavlikwhitequark
authored andcommitted
Convert many loops to range-for or std algorithms. NFC.
Also add comments about indexing and when we don't use range-for.
1 parent 97c8cb7 commit 86f20cc

16 files changed

+257
-287
lines changed

src/bsp.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,8 @@ SBsp2 *SBsp2::Alloc() { return (SBsp2 *)AllocTemporary(sizeof(SBsp2)); }
1212
SBsp3 *SBsp3::Alloc() { return (SBsp3 *)AllocTemporary(sizeof(SBsp3)); }
1313

1414
SBsp3 *SBsp3::FromMesh(const SMesh *m) {
15-
SBsp3 *bsp3 = NULL;
16-
int i;
17-
1815
SMesh mc = {};
19-
for(i = 0; i < m->l.n; i++) {
20-
mc.AddTriangle(&(m->l.elem[i]));
21-
}
16+
for(auto const &elt : m->l) { mc.AddTriangle(&elt); }
2217

2318
srand(0); // Let's be deterministic, at least!
2419
int n = mc.l.n;
@@ -28,10 +23,8 @@ SBsp3 *SBsp3::FromMesh(const SMesh *m) {
2823
swap(mc.l.elem[k], mc.l.elem[n]);
2924
}
3025

31-
for(i = 0; i < mc.l.n; i++) {
32-
bsp3 = InsertOrCreate(bsp3, &(mc.l.elem[i]), NULL);
33-
}
34-
26+
SBsp3 *bsp3 = NULL;
27+
for(auto &elt : mc.l) { bsp3 = InsertOrCreate(bsp3, &elt, NULL); }
3528
mc.Clear();
3629
return bsp3;
3730
}

src/constraint.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ std::string Constraint::DescriptionString() const {
5959
void Constraint::DeleteAllConstraintsFor(Constraint::Type type, hEntity entityA, hEntity ptA)
6060
{
6161
SK.constraint.ClearTags();
62-
for(int i = 0; i < SK.constraint.n; i++) {
63-
ConstraintBase *ct = &(SK.constraint.elem[i]);
62+
for(auto &constraint : SK.constraint) {
63+
ConstraintBase *ct = &constraint;
6464
if(ct->type != type) continue;
6565

6666
if(ct->entityA != entityA) continue;

src/dsc.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,7 @@ class IdList {
434434
}
435435

436436
void ClearTags() {
437-
int i;
438-
for(i = 0; i < n; i++) {
439-
elem[i].tag = 0;
440-
}
437+
for(auto &elt : *this) { elt.tag = 0; }
441438
}
442439

443440
void Tag(H h, int tag) {

src/entity.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -922,19 +922,16 @@ void EntityBase::GenerateEquations(IdList<Equation,hEquation> *l) const {
922922
// If the two endpoints of the arc are constrained coincident
923923
// (to make a complete circle), then our distance constraint
924924
// would be redundant and therefore overconstrain things.
925-
int i;
926-
for(i = 0; i < SK.constraint.n; i++) {
927-
ConstraintBase *c = &(SK.constraint.elem[i]);
928-
if(c->group != group) continue;
929-
if(c->type != Constraint::Type::POINTS_COINCIDENT) continue;
930-
931-
if((c->ptA == point[1] && c->ptB == point[2]) ||
932-
(c->ptA == point[2] && c->ptB == point[1]))
933-
{
934-
break;
935-
}
925+
auto it = std::find_if(SK.constraint.begin(), SK.constraint.end(),
926+
[&](ConstraintBase const &con) {
927+
return (con.group == group) &&
928+
(con.type == Constraint::Type::POINTS_COINCIDENT) &&
929+
((con.ptA == point[1] && con.ptB == point[2]) ||
930+
(con.ptA == point[2] && con.ptB == point[1]));
931+
});
932+
if(it != SK.constraint.end()) {
933+
break;
936934
}
937-
if(i < SK.constraint.n) break;
938935

939936
Expr *ra = Constraint::Distance(workplane, point[0], point[1]);
940937
Expr *rb = Constraint::Distance(workplane, point[0], point[2]);

src/export.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ void SolveSpaceUI::ExportSectionTo(const Platform::Path &filename) {
9696
bl.CullIdenticalBeziers(/*both=*/true);
9797

9898
// Collect lines and beziers with custom style & export.
99-
int i;
100-
for(i = 0; i < SK.entity.n; i++) {
101-
Entity *e = &(SK.entity.elem[i]);
99+
for(auto &ent : SK.entity) {
100+
Entity *e = &ent;
102101
if (!e->IsVisible()) continue;
103102
if (e->style.v < Style::FIRST_CUSTOM) continue;
104103
if (!Style::Exportable(e->style.v)) continue;
@@ -186,7 +185,6 @@ class GetEdgesCanvas : public Canvas {
186185
};
187186

188187
void SolveSpaceUI::ExportViewOrWireframeTo(const Platform::Path &filename, bool exportWireframe) {
189-
int i;
190188
SEdgeList edges = {};
191189
SBezierList beziers = {};
192190

@@ -206,8 +204,8 @@ void SolveSpaceUI::ExportViewOrWireframeTo(const Platform::Path &filename, bool
206204
sm = NULL;
207205
}
208206

209-
for(i = 0; i < SK.entity.n; i++) {
210-
Entity *e = &(SK.entity.elem[i]);
207+
for(auto &entity : SK.entity) {
208+
Entity *e = &entity;
211209
if(!e->IsVisible()) continue;
212210
if(e->construction) continue;
213211

src/exportvector.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,8 +1027,9 @@ void SvgFileWriter::StartFile() {
10271027
double sw = max(ptMax.x - ptMin.x, ptMax.y - ptMin.y) / 1000;
10281028
fprintf(f, "stroke-width:%f;\r\n", sw);
10291029
fprintf(f, "}\r\n");
1030-
for(int i = 0; i < SK.style.n; i++) {
1031-
Style *s = &SK.style.elem[i];
1030+
for(auto &style : SK.style) {
1031+
Style *s = &style;
1032+
10321033
RgbaColor strokeRgb = Style::Color(s->h, /*forExport=*/true);
10331034
StipplePattern pattern = Style::PatternType(s->h);
10341035
double stippleScale = Style::StippleScaleMm(s->h);

src/file.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ void SolveSpaceUI::ClearExisting() {
2020
UndoClearStack(&redo);
2121
UndoClearStack(&undo);
2222

23-
for(int i = 0; i < SK.groupOrder.n; i++) {
24-
Group *g = SK.GetGroup(SK.groupOrder.elem[i]);
23+
for(hGroup hg : SK.groupOrder) {
24+
Group *g = SK.GetGroup(hg);
2525
g->Clear();
2626
}
2727

@@ -301,39 +301,39 @@ bool SolveSpaceUI::SaveToFile(const Platform::Path &filename) {
301301
fprintf(fh, "%s\n\n\n", VERSION_STRING);
302302

303303
int i, j;
304-
for(i = 0; i < SK.group.n; i++) {
305-
sv.g = SK.group.elem[i];
304+
for(auto &g : SK.group) {
305+
sv.g = g;
306306
SaveUsingTable(filename, 'g');
307307
fprintf(fh, "AddGroup\n\n");
308308
}
309309

310-
for(i = 0; i < SK.param.n; i++) {
311-
sv.p = SK.param.elem[i];
310+
for(auto &p : SK.param) {
311+
sv.p = p;
312312
SaveUsingTable(filename, 'p');
313313
fprintf(fh, "AddParam\n\n");
314314
}
315315

316-
for(i = 0; i < SK.request.n; i++) {
317-
sv.r = SK.request.elem[i];
316+
for(auto &r : SK.request) {
317+
sv.r = r;
318318
SaveUsingTable(filename, 'r');
319319
fprintf(fh, "AddRequest\n\n");
320320
}
321321

322-
for(i = 0; i < SK.entity.n; i++) {
323-
(SK.entity.elem[i]).CalculateNumerical(/*forExport=*/true);
324-
sv.e = SK.entity.elem[i];
322+
for(auto &e : SK.entity) {
323+
e.CalculateNumerical(/*forExport=*/true);
324+
sv.e = e;
325325
SaveUsingTable(filename, 'e');
326326
fprintf(fh, "AddEntity\n\n");
327327
}
328328

329-
for(i = 0; i < SK.constraint.n; i++) {
330-
sv.c = SK.constraint.elem[i];
329+
for(auto &c : SK.constraint) {
330+
sv.c = c;
331331
SaveUsingTable(filename, 'c');
332332
fprintf(fh, "AddConstraint\n\n");
333333
}
334334

335-
for(i = 0; i < SK.style.n; i++) {
336-
sv.s = SK.style.elem[i];
335+
for(auto &s : SK.style) {
336+
sv.s = s;
337337
if(sv.s.h.v >= Style::FIRST_CUSTOM) {
338338
SaveUsingTable(filename, 's');
339339
fprintf(fh, "AddStyle\n\n");

0 commit comments

Comments
 (0)