Add CountIf method to IdList to simplify some call sites. NFC.

This also changes GetNumConstraints to return size_t.
pull/420/head
Ryan Pavlik 2018-01-03 19:56:04 -06:00 committed by whitequark
parent 43c9cba7dd
commit 39c348090b
4 changed files with 9 additions and 14 deletions

View File

@ -400,6 +400,11 @@ public:
const T *begin() const { return &elem[0]; } const T *begin() const { return &elem[0]; }
const T *end() const { return &elem[n]; } const T *end() const { return &elem[n]; }
template<typename F>
size_t CountIf(F &&predicate) const {
return std::count_if(begin(), end(), std::forward<F&&>(predicate));
}
void ClearTags() { void ClearTags() {
int i; int i;
for(i = 0; i < n; i++) { for(i = 0; i < n; i++) {

View File

@ -49,14 +49,8 @@ bool Group::IsVisible() {
return true; return true;
} }
int Group::GetNumConstraints(void) { size_t Group::GetNumConstraints(void) {
int num = 0; return SK.constraint.CountIf([&](Constraint const & c) { return c.group.v == h.v; });
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;
} }
Vector Group::ExtrusionGetVector() { Vector Group::ExtrusionGetVector() {

View File

@ -264,7 +264,7 @@ public:
void AddEq(IdList<Equation,hEquation> *l, Expr *expr, int index); void AddEq(IdList<Equation,hEquation> *l, Expr *expr, int index);
void GenerateEquations(IdList<Equation,hEquation> *l); void GenerateEquations(IdList<Equation,hEquation> *l);
bool IsVisible(); bool IsVisible();
int GetNumConstraints(); size_t GetNumConstraints();
Vector ExtrusionGetVector(); Vector ExtrusionGetVector();
void ExtrusionForceVectorTo(const Vector &v); void ExtrusionForceVectorTo(const Vector &v);

View File

@ -698,16 +698,12 @@ void TextWindow::EditControlDone(std::string s) {
g->valA = ev; g->valA = ev;
if(g->type == Group::Type::ROTATE) { 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 // If the group does not contain any constraints, then
// set the numerical guess to space the copies uniformly // set the numerical guess to space the copies uniformly
// over one rotation. Don't touch the guess if we're // over one rotation. Don't touch the guess if we're
// already constrained, because that would break // already constrained, because that would break
// convergence. // convergence.
if(c == 0) { if(g->GetNumConstraints() == 0) {
double copies = (g->skipFirst) ? (ev + 1) : ev; double copies = (g->skipFirst) ? (ev + 1) : ev;
SK.GetParam(g->h.param(3))->val = PI/(2*copies); SK.GetParam(g->h.param(3))->val = PI/(2*copies);
} }