Skip to content

Commit

Permalink
Some micro optimization of hashmap to use lookup and insert/modify
Browse files Browse the repository at this point in the history
  • Loading branch information
metab0t committed Aug 30, 2024
1 parent 81949ea commit 7cb6bcf
Showing 1 changed file with 18 additions and 28 deletions.
46 changes: 18 additions & 28 deletions lib/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,14 +329,12 @@ void ExprBuilder::_add_quadratic_term(IndexT i, IndexT j, CoeffT coeff)
{
std::swap(i, j);
}
auto it = quadratic_terms.find({i, j});
if (it == quadratic_terms.end())
VariablePair vp{i, j};
auto ret = quadratic_terms.emplace(vp, coeff);
if (!ret.second)
{
quadratic_terms.insert({{i, j}, coeff});
}
else
{
it->second += coeff;
auto &iter = ret.first;
iter->second += coeff;
}
}
void ExprBuilder::_set_quadratic_coef(IndexT i, IndexT j, CoeffT coeff)
Expand All @@ -345,14 +343,12 @@ void ExprBuilder::_set_quadratic_coef(IndexT i, IndexT j, CoeffT coeff)
{
std::swap(i, j);
}
auto it = quadratic_terms.find({i, j});
if (it == quadratic_terms.end())
{
quadratic_terms.insert({{i, j}, coeff});
}
else
VariablePair vp{i, j};
auto ret = quadratic_terms.emplace(vp, coeff);
if (!ret.second)
{
it->second = coeff;
auto &iter = ret.first;
iter->second = coeff;
}
}
void ExprBuilder::add_quadratic_term(const VariableIndex &i, const VariableIndex &j, CoeffT coeff)
Expand All @@ -366,26 +362,20 @@ void ExprBuilder::set_quadratic_coef(const VariableIndex &i, const VariableIndex

void ExprBuilder::_add_affine_term(IndexT i, CoeffT coeff)
{
auto it = affine_terms.find(i);
if (it == affine_terms.end())
auto ret = affine_terms.emplace(i, coeff);
if (!ret.second)
{
affine_terms.insert({i, coeff});
}
else
{
it->second += coeff;
auto &iter = ret.first;
iter->second += coeff;
}
}
void ExprBuilder::_set_affine_coef(IndexT i, CoeffT coeff)
{
auto it = affine_terms.find(i);
if (it == affine_terms.end())
{
affine_terms.insert({i, coeff});
}
else
auto ret = affine_terms.emplace(i, coeff);
if (!ret.second)
{
it->second = coeff;
auto &iter = ret.first;
iter->second = coeff;
}
}
void ExprBuilder::add_affine_term(const VariableIndex &i, CoeffT coeff)
Expand Down

0 comments on commit 7cb6bcf

Please sign in to comment.