From 39c348090bd83746a4d953b679a12c395bae6e4c Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Wed, 3 Jan 2018 19:56:04 -0600 Subject: [PATCH] Add CountIf method to IdList to simplify some call sites. NFC. This also changes GetNumConstraints to return size_t. --- src/dsc.h | 5 +++++ src/group.cpp | 10 ++-------- src/sketch.h | 2 +- src/textscreens.cpp | 6 +----- 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/dsc.h b/src/dsc.h index 2e8a812c..56c61674 100644 --- a/src/dsc.h +++ b/src/dsc.h @@ -400,6 +400,11 @@ public: const T *begin() const { return &elem[0]; } const T *end() const { return &elem[n]; } + template + size_t CountIf(F &&predicate) const { + return std::count_if(begin(), end(), std::forward(predicate)); + } + void ClearTags() { int i; for(i = 0; i < n; i++) { diff --git a/src/group.cpp b/src/group.cpp index bddd2e4e..3d1f978a 100644 --- a/src/group.cpp +++ b/src/group.cpp @@ -49,14 +49,8 @@ bool Group::IsVisible() { return true; } -int Group::GetNumConstraints(void) { - int num = 0; - for(int i = 0; i < SK.constraint.n; i++) { - Constraint *c = &SK.constraint.elem[i]; - if(c->group.v != h.v) continue; - num++; - } - return num; +size_t Group::GetNumConstraints(void) { + return SK.constraint.CountIf([&](Constraint const & c) { return c.group.v == h.v; }); } Vector Group::ExtrusionGetVector() { diff --git a/src/sketch.h b/src/sketch.h index cc5c0d23..1657bb93 100644 --- a/src/sketch.h +++ b/src/sketch.h @@ -264,7 +264,7 @@ public: void AddEq(IdList *l, Expr *expr, int index); void GenerateEquations(IdList *l); bool IsVisible(); - int GetNumConstraints(); + size_t GetNumConstraints(); Vector ExtrusionGetVector(); void ExtrusionForceVectorTo(const Vector &v); diff --git a/src/textscreens.cpp b/src/textscreens.cpp index a0e2e9a9..fa8ab1d1 100644 --- a/src/textscreens.cpp +++ b/src/textscreens.cpp @@ -698,16 +698,12 @@ void TextWindow::EditControlDone(std::string s) { g->valA = ev; if(g->type == Group::Type::ROTATE) { - int i, c = 0; - for(i = 0; i < SK.constraint.n; i++) { - if(SK.constraint.elem[i].group.v == g->h.v) c++; - } // If the group does not contain any constraints, then // set the numerical guess to space the copies uniformly // over one rotation. Don't touch the guess if we're // already constrained, because that would break // convergence. - if(c == 0) { + if(g->GetNumConstraints() == 0) { double copies = (g->skipFirst) ? (ev + 1) : ev; SK.GetParam(g->h.param(3))->val = PI/(2*copies); }