Skip to content

Commit

Permalink
policies: replace O(n) accumulate op
Browse files Browse the repository at this point in the history
  • Loading branch information
milroy committed Feb 10, 2023
1 parent 0e7ffa3 commit 93241e6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
6 changes: 3 additions & 3 deletions resource/policies/dfu_match_multilevel_id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ class multilevel_id_t : public dfu_match_cb_t
unsigned int add_by,
unsigned int multiply_by);
score_factor_t (const score_factor_t &o) = default;
int64_t calc_factor (int64_t base_factor, int64_t break_tie) const;
int64_t calc_factor (int64_t base_factor, int64_t break_tie);
int64_t m_factor = 0;

private:
std::string m_type;
Expand All @@ -116,11 +117,10 @@ class multilevel_id_t : public dfu_match_cb_t
};

void set_base_factor (const std::string &type, unsigned int id);
int64_t calc_multilevel_scores () const;

FOLD m_comp;
unsigned m_stop_on_k_matches = 0;
std::vector<int64_t> m_multilevel_scores;
int64_t m_multilevel_scores = 0;
std::unordered_map<std::string, score_factor_t> m_multilevel_factors;
};

Expand Down
29 changes: 11 additions & 18 deletions resource/policies/dfu_match_multilevel_id_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ multilevel_id_t<FOLD>::score_factor_t::score_factor_t (const std::string &type,
template<typename FOLD>
int64_t multilevel_id_t<FOLD>::score_factor_t::calc_factor (
int64_t base_factor,
int64_t break_tie) const
int64_t break_tie)
{
int64_t add, mul, tie, factor;
int64_t add, mul, tie;
if (base_factor < 0 || break_tie < 0) {
errno = EINVAL;
return -1;
Expand All @@ -61,15 +61,8 @@ int64_t multilevel_id_t<FOLD>::score_factor_t::calc_factor (
errno = EOVERFLOW;
return -1;
}
factor = mul + tie;
return factor;
}

template<typename FOLD>
int64_t multilevel_id_t<FOLD>::calc_multilevel_scores () const
{
return std::accumulate (m_multilevel_scores.begin (),
m_multilevel_scores.end (), 0);
m_factor = mul + tie;
return m_factor;
}


Expand Down Expand Up @@ -138,7 +131,7 @@ int multilevel_id_t<FOLD>::dom_finish_graph (
dfu.choose_accum_best_k (subsystem, type, count, m_comp);
}
dfu.set_overall_score (score);
m_multilevel_scores.clear ();
m_multilevel_scores = 0;
return (score == MATCH_MET)? 0 : -1;
}

Expand Down Expand Up @@ -168,7 +161,7 @@ int multilevel_id_t<FOLD>::dom_discover_vtx (
int64_t factor = f.calc_factor (g[u].id+1, g[u].uniq_id);
if (factor < 0)
return factor;
m_multilevel_scores.push_back (factor);
m_multilevel_scores += factor;
}
incr ();
return 0;
Expand All @@ -185,7 +178,6 @@ int multilevel_id_t<FOLD>::dom_finish_vtx (
{
int64_t score = MATCH_MET;
int64_t overall;
unsigned prefix;

for (auto &resource : resources) {
if (resource.type != g[u].type)
Expand All @@ -204,11 +196,12 @@ int multilevel_id_t<FOLD>::dom_finish_vtx (
}
}

if (m_multilevel_factors.find (g[u].type) != m_multilevel_factors.end ())
m_multilevel_scores.pop_back ();
auto it = m_multilevel_factors.find (g[u].type);
if (it != m_multilevel_factors.end ())
m_multilevel_scores -= it->second.m_factor;

prefix = calc_multilevel_scores ();
overall = (score == MATCH_MET)? (score + prefix + g[u].id + 1) : score;
overall = (score == MATCH_MET)? (score + m_multilevel_scores
+ g[u].id + 1) : score;
dfu.set_overall_score (overall);
decr ();
return (score == MATCH_MET)? 0 : -1;
Expand Down

0 comments on commit 93241e6

Please sign in to comment.