From 93241e6cc45f4d5173cff7150404c01bfb113c4d Mon Sep 17 00:00:00 2001 From: Daniel Milroy Date: Fri, 10 Feb 2023 14:33:58 -0800 Subject: [PATCH] policies: replace O(n) accumulate op --- resource/policies/dfu_match_multilevel_id.hpp | 6 ++-- .../policies/dfu_match_multilevel_id_impl.hpp | 29 +++++++------------ 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/resource/policies/dfu_match_multilevel_id.hpp b/resource/policies/dfu_match_multilevel_id.hpp index 7f4611f80..99e694c0e 100644 --- a/resource/policies/dfu_match_multilevel_id.hpp +++ b/resource/policies/dfu_match_multilevel_id.hpp @@ -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; @@ -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 m_multilevel_scores; + int64_t m_multilevel_scores = 0; std::unordered_map m_multilevel_factors; }; diff --git a/resource/policies/dfu_match_multilevel_id_impl.hpp b/resource/policies/dfu_match_multilevel_id_impl.hpp index 97585daa1..b6e369880 100644 --- a/resource/policies/dfu_match_multilevel_id_impl.hpp +++ b/resource/policies/dfu_match_multilevel_id_impl.hpp @@ -36,9 +36,9 @@ multilevel_id_t::score_factor_t::score_factor_t (const std::string &type, template int64_t multilevel_id_t::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; @@ -61,15 +61,8 @@ int64_t multilevel_id_t::score_factor_t::calc_factor ( errno = EOVERFLOW; return -1; } - factor = mul + tie; - return factor; -} - -template -int64_t multilevel_id_t::calc_multilevel_scores () const -{ - return std::accumulate (m_multilevel_scores.begin (), - m_multilevel_scores.end (), 0); + m_factor = mul + tie; + return m_factor; } @@ -138,7 +131,7 @@ int multilevel_id_t::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; } @@ -168,7 +161,7 @@ int multilevel_id_t::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; @@ -185,7 +178,6 @@ int multilevel_id_t::dom_finish_vtx ( { int64_t score = MATCH_MET; int64_t overall; - unsigned prefix; for (auto &resource : resources) { if (resource.type != g[u].type) @@ -204,11 +196,12 @@ int multilevel_id_t::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;