Skip to content

Commit

Permalink
use C++11 range-based for (part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
wojdyr committed Aug 7, 2022
1 parent d1ede74 commit f594847
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 144 deletions.
4 changes: 2 additions & 2 deletions fityk/LMfit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ vector<double> LMfit::get_covariance_matrix(const vector<Data*>& datas)

invert_matrix(alpha, na_);

v_foreach (int, i, undef)
alpha[(*i)*na_ + (*i)] = 0.;
for (int i : undef)
alpha[i*na_ + i] = 0.;

#if USE_LONG_DOUBLE
return vector<double>(alpha.begin(), alpha.end());
Expand Down
53 changes: 27 additions & 26 deletions fityk/fit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ namespace fityk {
int count_points(const vector<Data*>& datas)
{
int n = 0;
v_foreach (Data*, i, datas)
n += (*i)->get_n();
for (const Data* data : datas)
n += data->get_n();
return n;
}

Expand Down Expand Up @@ -93,17 +93,17 @@ vector<double> Fit::get_standard_errors(const vector<Data*>& datas)
vector<double> Fit::get_confidence_limits(const vector<Data*>& datas,
double level_percent)
{
vector<double> v = get_standard_errors(datas);
vector<double> stderrors = get_standard_errors(datas);
int dof = get_dof(datas);
double level = 1. - level_percent / 100.;
// If Fityk run under valgrind gives
// Error in function boost::math::erfc_inv<e>(e, e): Overflow Error
// see VALGRIND_WORKAROUND above.
boost::math::students_t dist(dof);
double t = boost::math::quantile(boost::math::complement(dist, level/2));
vm_foreach (double, i, v)
*i *= t;
return v;
for (double& x : stderrors)
x *= t;
return stderrors;
}

string Fit::get_cov_info(const vector<Data*>& datas)
Expand Down Expand Up @@ -132,8 +132,8 @@ int Fit::compute_deviates(const vector<realt> &A, double *deviates)
++evaluations_;
F_->mgr.use_external_parameters(A); //that's the only side-effect
int ntot = 0;
v_foreach (Data*, i, fitted_datas_)
ntot += compute_deviates_for_data(*i, deviates + ntot);
for (const Data* data : fitted_datas_)
ntot += compute_deviates_for_data(data, deviates + ntot);
return ntot;
}

Expand All @@ -156,8 +156,8 @@ realt Fit::compute_wssr(const vector<realt> &A,
{
realt wssr = 0;
F_->mgr.use_external_parameters(A); //that's the only side-effect
v_foreach (Data*, i, datas) {
wssr += compute_wssr_for_data(*i, weigthed);
for (const Data* data : datas) {
wssr += compute_wssr_for_data(data, weigthed);
}
++evaluations_;
return wssr;
Expand Down Expand Up @@ -189,8 +189,8 @@ realt Fit::compute_r_squared(const vector<realt> &A,
{
realt sum_err = 0, sum_tot = 0, se = 0, st = 0;
F_->mgr.use_external_parameters(A);
v_foreach (Data*, i, datas) {
compute_r_squared_for_data(*i, &se, &st);
for (const Data* data : datas) {
compute_r_squared_for_data(data, &se, &st);
sum_err += se;
sum_tot += st;
}
Expand Down Expand Up @@ -240,8 +240,8 @@ void Fit::compute_derivatives(const vector<realt> &A,
fill(beta.begin(), beta.end(), 0.0);

F_->mgr.use_external_parameters(A);
v_foreach (Data*, i, datas) {
compute_derivatives_for(*i, alpha, beta);
for (const Data* data : datas) {
compute_derivatives_for(data, alpha, beta);
}
// filling second half of alpha[]
for (int j = 1; j < na_; j++)
Expand Down Expand Up @@ -297,8 +297,8 @@ void Fit::compute_derivatives_mp(const vector<realt> &A,
++evaluations_;
F_->mgr.use_external_parameters(A);
int ntot = 0;
v_foreach (Data*, i, datas) {
ntot += compute_derivatives_mp_for(*i, ntot, derivs, deviates);
for (const Data* data : datas) {
ntot += compute_derivatives_mp_for(data, ntot, derivs, deviates);
}
}

Expand Down Expand Up @@ -330,8 +330,8 @@ realt Fit::compute_wssr_gradient(const vector<realt> &A,
F_->mgr.use_external_parameters(A);
realt wssr = 0.;
fill(grad, grad+na_, 0.0);
v_foreach (Data*, i, datas)
wssr += compute_wssr_gradient_for(*i, grad);
for (const Data* data : datas)
wssr += compute_wssr_gradient_for(data, grad);
return wssr;
}

Expand Down Expand Up @@ -448,8 +448,8 @@ void Fit::update_par_usage(const vector<Data*>& datas)
par_usage_ = vector<bool>(na_, false);
for (int idx = 0; idx < na_; ++idx) {
int var_idx = F_->mgr.gpos_to_vpos(idx);
v_foreach (Data*, i, datas) {
if ((*i)->model()->is_dependent_on_var(var_idx)) {
for (const Data* data : datas) {
if (data->model()->is_dependent_on_var(var_idx)) {
par_usage_[idx] = true;
break; //go to next idx
}
Expand Down Expand Up @@ -499,9 +499,10 @@ void Fit::output_tried_parameters(const vector<realt>& a)
{
const SettingsMgr *sm = F_->settings_mgr();
string s = "Trying ( ";
s.reserve(s.size() + a.size() * 12); // rough guess
v_foreach (realt, j, a)
s += sm->format_double(*j) + (j+1 == a.end() ? " )" : ", ");
for (const realt& j : a) {
s += sm->format_double(j);
s += &j != &a.back() ? ", " : " )";
}
F_->ui()->mesg(s);
}

Expand Down Expand Up @@ -587,9 +588,9 @@ FitManager::~FitManager()

Fit* FitManager::get_method(const string& name) const
{
v_foreach(Fit*, i, methods_)
if ((*i)->name == name)
return *i;
for(Fit* method : methods_)
if (method->name == name)
return method;
throw ExecuteError("fitting method `" + name + "' not available.");
return NULL; // avoid compiler warning
}
Expand Down
22 changes: 13 additions & 9 deletions fityk/func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ void Function::do_precomputations(const vector<Variable*> &variables)
for (int i = 0; i < used_vars_.get_count(); ++i) {
const Variable *v = variables[used_vars_.get_idx(i)];
av_[i] = v->value();
v_foreach (Variable::ParMult, j, v->recursive_derivatives())
multi_.push_back(Multi(i, *j));
for (const Variable::ParMult& pm : v->recursive_derivatives())
multi_.push_back(Multi(i, pm));
}
this->more_precomputations();
}

void Function::erased_parameter(int k)
{
vm_foreach (Multi, i, multi_)
if (i->p > k)
-- i->p;
for (Multi& m : multi_)
if (m.p > k)
-- m.p;
}


Expand Down Expand Up @@ -98,8 +98,8 @@ void Function::calculate_value_deriv(const vector<realt> &x,
int Function::max_param_pos() const
{
int n = 0;
v_foreach (Multi, j, multi_)
n = max(j->p + 1, n);
for (const Multi& m : multi_)
n = max(m.p + 1, n);
return n;
}

Expand All @@ -126,8 +126,12 @@ bool Function::get_ibreadth(realt* a) const
string Function::get_basic_assignment() const
{
string r = "%" + name + " = " + tp_->name + "(";
v_foreach (string, i, used_vars_.names())
r += (i == used_vars_.names().begin() ? "$" : ", $") + *i;
bool first = true;
for (const string& name : used_vars_.names()) {
r += (first ? "$" : ", $");
r += name;
first = false;
}
r += ")";
return r;
}
Expand Down
94 changes: 46 additions & 48 deletions fityk/mgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ Variable* make_compound_variable(const string &name, VMData* vd,

// re-index variables
vector<string> used_vars;
vm_foreach (int, i, vd->get_mutable_code()) {
std::vector<int>& code = vd->get_mutable_code();
for (auto i = code.begin(); i != code.end(); ++i) {
if (*i == OP_SYMBOL) {
++i;
const string& vname = all_variables[*i]->name;
Expand All @@ -106,8 +107,9 @@ Variable* make_compound_variable(const string &name, VMData* vd,
used_vars.push_back(vname);
}
*i = idx;
} else if (VMData::has_idx(*i))
} else if (VMData::has_idx(*i)) {
++i;
}
}

vector<OpTree*> op_trees = prepare_ast_with_der(*vd, used_vars.size());
Expand Down Expand Up @@ -219,26 +221,23 @@ ModelManager::get_variable_references(const string &name) const
{
int idx = find_variable_nr(name);
vector<string> refs;
v_foreach (Variable*, i, variables_)
if ((*i)->used_vars().has_idx(idx))
refs.push_back("$" + (*i)->name);
v_foreach (Function*, i, functions_)
for (int j = 0; j < (*i)->used_vars().get_count(); ++j)
if ((*i)->used_vars().get_idx(j) == idx)
refs.push_back("%" + (*i)->name + "." + (*i)->get_param(j));
for (const Variable* var : variables_)
if (var->used_vars().has_idx(idx))
refs.push_back("$" + var->name);
for (const Function* func : functions_)
for (int j = 0; j < func->used_vars().get_count(); ++j)
if (func->used_vars().get_idx(j) == idx)
refs.push_back("%" + func->name + "." + func->get_param(j));
return refs;
}

// set indices corresponding to variable names in all functions and variables
void ModelManager::reindex_all()
{
for (vector<Variable*>::iterator i = variables_.begin();
i != variables_.end(); ++i)
(*i)->set_var_idx(variables_);
for (vector<Function*>::iterator i = functions_.begin();
i != functions_.end(); ++i) {
(*i)->update_var_indices(variables_);
}
for (Variable* var : variables_)
var->set_var_idx(variables_);
for (Function* func : functions_)
func->update_var_indices(variables_);
}

void ModelManager::remove_unreferred()
Expand All @@ -264,12 +263,10 @@ void ModelManager::remove_unreferred()
if (del) {
parameters_.erase(parameters_.begin() + i);
// take care about parameter indices in variables and functions
for (vector<Variable*>::iterator j = variables_.begin();
j != variables_.end(); ++j)
(*j)->erased_parameter(i);
for (vector<Function*>::iterator j = functions_.begin();
j != functions_.end(); ++j)
(*j)->erased_parameter(i);
for (Variable* var : variables_)
var->erased_parameter(i);
for (Function* func : functions_)
func->erased_parameter(i);
}
}
}
Expand Down Expand Up @@ -320,8 +317,8 @@ int ModelManager::copy_and_add_variable(const string& newname,
vars.push_back(varmap.find(v_idx)->second);
}
vector<OpTree*> new_op_trees;
v_foreach (OpTree*, i, orig->get_op_trees())
new_op_trees.push_back((*i)->clone());
for (const OpTree* tree : orig->get_op_trees())
new_op_trees.push_back(tree->clone());
var = new Variable(newname, vars, new_op_trees);
}
var->domain = orig->domain;
Expand Down Expand Up @@ -353,15 +350,15 @@ void ModelManager::delete_variables(const vector<string> &names)

set<int> nn;
// find indices of variables_, expanding wildcards
v_foreach (string, i, names) {
if (i->find('*') == string::npos) {
int k = find_variable_nr(*i);
for (const string& name : names) {
if (name.find('*') == string::npos) {
int k = find_variable_nr(name);
if (k == -1)
throw ExecuteError("undefined variable: $" + *i);
throw ExecuteError("undefined variable: $" + name);
nn.insert(k);
} else
for (size_t j = 0; j != variables_.size(); ++j)
if (match_glob(variables_[j]->name.c_str(), i->c_str()))
if (match_glob(variables_[j]->name.c_str(), name.c_str()))
nn.insert(j);
}

Expand Down Expand Up @@ -393,16 +390,17 @@ void ModelManager::delete_funcs(const vector<string>& names)

set<int> nn;
// find indices of functions, expanding wildcards
v_foreach (string, i, names) {
if (i->find('*') == string::npos) {
int k = find_function_nr(*i);
for (const string& name : names) {
if (name.find('*') == string::npos) {
int k = find_function_nr(name);
if (k == -1)
throw ExecuteError("undefined function: %" + *i);
throw ExecuteError("undefined function: %" + name);
nn.insert(k);
} else
} else {
for (size_t j = 0; j != functions_.size(); ++j)
if (match_glob(functions_[j]->name.c_str(), i->c_str()))
if (match_glob(functions_[j]->name.c_str(), name.c_str()))
nn.insert(j);
}
}

// Delete functions. The descending index order is needed by .erase().
Expand All @@ -418,9 +416,9 @@ void ModelManager::delete_funcs(const vector<string>& names)

bool ModelManager::is_function_referred(int n) const
{
v_foreach (Model*, i, models_) {
if (contains_element((*i)->get_ff().idx, n)
|| contains_element((*i)->get_zz().idx, n))
for (const Model* model : models_) {
if (contains_element(model->get_ff().idx, n)
|| contains_element(model->get_zz().idx, n))
return true;
}
return false;
Expand Down Expand Up @@ -489,10 +487,10 @@ void ModelManager::use_parameters()

void ModelManager::use_external_parameters(const vector<realt> &ext_param)
{
vm_foreach (Variable*, i, variables_)
(*i)->recalculate(variables_, ext_param);
vm_foreach (Function*, i, functions_)
(*i)->do_precomputations(variables_);
for (Variable* var : variables_)
var->recalculate(variables_, ext_param);
for (Function* func : functions_)
func->do_precomputations(variables_);
}

void ModelManager::put_new_parameters(const vector<realt> &aa)
Expand All @@ -512,9 +510,9 @@ int ModelManager::assign_func(const string &name, Tplate::Ptr tp,
{
assert(tp);
vector<string> varnames;
vm_foreach (VMData*, j, args) {
int idx = (*j)->single_symbol() ? (*j)->code()[1]
: make_variable(next_var_name(), *j);
for (VMData* vm : args) {
int idx = vm->single_symbol() ? vm->code()[1]
: make_variable(next_var_name(), vm);
varnames.push_back(variables_[idx]->name);
}
Function *func = (*tp->create)(ctx_->get_settings(), name, tp, varnames);
Expand Down Expand Up @@ -676,10 +674,10 @@ vector<string> ModelManager::share_par_cmd(const string& par, bool share)
int nr = find_variable_nr(varname);
if (share) {
vector<double> values;
v_foreach (Function*, i, functions_) {
int idx = index_of_element((*i)->tp()->fargs, par);
for (const Function* func : functions_) {
int idx = index_of_element(func->tp()->fargs, par);
if (idx != -1)
values.push_back((*i)->av()[idx]);
values.push_back(func->av()[idx]);
}
if (values.empty())
return cmds;
Expand Down
Loading

0 comments on commit f594847

Please sign in to comment.