perf. 使用ranges及views及<=>及其他一些c++20优化及一般优化

This commit is contained in:
lhhxxxxx
2022-08-02 01:48:16 +08:00
parent e55e1567ae
commit 396acc47d4
35 changed files with 322 additions and 308 deletions

View File

@@ -206,17 +206,17 @@ size_t asst::InfrastProductionTask::opers_detect()
//--cur_available_num;
continue;
}
auto find_iter = std::find_if(
m_all_available_opers.cbegin(), m_all_available_opers.cend(),
[&](const infrast::Oper& oper) -> bool {
if (oper.skills != cur_oper.skills) {
return false;
}
// 有可能是同一个干员比一下hash
int dist = HashImageAnalyzer::hamming(cur_oper.face_hash, oper.face_hash);
Log.debug("opers_detect hash dist |", dist);
return dist < face_hash_thres;
});
auto find_iter = std::ranges::find_if(std::as_const(m_all_available_opers)
,
[&](const infrast::Oper& oper) -> bool {
if (oper.skills != cur_oper.skills) {
return false;
}
// 有可能是同一个干员比一下hash
int dist = HashImageAnalyzer::hamming(cur_oper.face_hash, oper.face_hash);
Log.debug("opers_detect hash dist |", dist);
return dist < face_hash_thres;
});
// 如果两个的hash距离过小则认为是同一个干员不进行插入
if (find_iter != m_all_available_opers.cend()) {
continue;
@@ -244,10 +244,10 @@ bool asst::InfrastProductionTask::optimal_calc()
std::vector<infrast::SkillsComb> optimal_combs;
optimal_combs.reserve(cur_max_num_of_opers);
double max_efficient = 0;
std::sort(all_available_combs.begin(), all_available_combs.end(),
[&](const infrast::SkillsComb& lhs, const infrast::SkillsComb& rhs) -> bool {
return lhs.efficient.at(m_product) > rhs.efficient.at(m_product);
});
std::ranges::sort(all_available_combs,
[&](const infrast::SkillsComb& lhs, const infrast::SkillsComb& rhs) -> bool {
return lhs.efficient.at(m_product) > rhs.efficient.at(m_product);
});
for (const auto& comb : all_available_combs) {
std::string skill_str;
@@ -329,11 +329,11 @@ bool asst::InfrastProductionTask::optimal_calc()
// necessary里的技能一个都不能少
// TODO necessary暂时没做hash校验。因为没有需要比hash的necessary干员
for (const infrast::SkillsComb& nec_skills : group.necessary) {
auto find_iter = std::find_if(
cur_available_opers.cbegin(), cur_available_opers.cend(),
[&](const infrast::SkillsComb& arg) -> bool {
return arg == nec_skills;
});
auto find_iter = std::ranges::find_if(std::as_const(cur_available_opers)
,
[&](const infrast::SkillsComb& arg) -> bool {
return arg == nec_skills;
});
if (find_iter == cur_available_opers.cend()) {
group_unavailable = true;
break;
@@ -360,11 +360,11 @@ bool asst::InfrastProductionTask::optimal_calc()
}
}
std::sort(optional.begin(), optional.end(),
[&](const infrast::SkillsComb& lhs,
const infrast::SkillsComb& rhs) -> bool {
return lhs.efficient.at(m_product) > rhs.efficient.at(m_product);
});
std::ranges::sort(optional,
[&](const infrast::SkillsComb& lhs,
const infrast::SkillsComb& rhs) -> bool {
return lhs.efficient.at(m_product) > rhs.efficient.at(m_product);
});
// 可能有多个干员有同样的技能,所以这里需要循环找同一个技能,直到找不到为止
for (const infrast::SkillsComb& opt : optional) {
@@ -389,8 +389,8 @@ bool asst::InfrastProductionTask::optimal_calc()
continue;
}
std::string name = name_analyzer.get_result().front().text;
hash_matched = std::find(
opt.name_filter.cbegin(), opt.name_filter.cend(), name)
hash_matched = std::ranges::find(opt.name_filter
, name)
!= opt.name_filter.cend();
}
if (!hash_matched) {
@@ -496,37 +496,37 @@ bool asst::InfrastProductionTask::opers_choose()
auto cur_all_opers = oper_analyzer.get_result();
Log.trace("before mood filter, opers size:", cur_all_opers.size());
// 小于心情阈值的干员则不可用
auto remove_iter = std::remove_if(cur_all_opers.begin(), cur_all_opers.end(),
[&](const infrast::Oper& rhs) -> bool {
return rhs.mood_ratio < m_mood_threshold;
});
auto remove_iter = std::ranges::remove_if(cur_all_opers,
[&](const infrast::Oper& rhs) -> bool {
return rhs.mood_ratio < m_mood_threshold;
}).begin();
cur_all_opers.erase(remove_iter, cur_all_opers.end());
Log.trace("after mood filter, opers size:", cur_all_opers.size());
for (auto opt_iter = m_optimal_combs.begin(); opt_iter != m_optimal_combs.end();) {
Log.trace("to find", opt_iter->skills.begin()->names.front());
auto find_iter = std::find_if(
cur_all_opers.cbegin(), cur_all_opers.cend(),
[&](const infrast::Oper& lhs) -> bool {
if (lhs.skills != opt_iter->skills) {
return false;
}
if (opt_iter->name_filter.empty()) {
return true;
}
else {
OcrWithPreprocessImageAnalyzer name_analyzer(lhs.name_img);
name_analyzer.set_replace(
Task.get<OcrTaskInfo>("CharsNameOcrReplace")->replace_map);
Log.trace("Analyze name filter");
if (!name_analyzer.analyze()) {
return false;
}
std::string name = name_analyzer.get_result().front().text;
return std::find(
opt_iter->name_filter.cbegin(), opt_iter->name_filter.cend(), name)
!= opt_iter->name_filter.cend();
}
});
auto find_iter = std::ranges::find_if(std::as_const(cur_all_opers)
,
[&](const infrast::Oper& lhs) -> bool {
if (lhs.skills != opt_iter->skills) {
return false;
}
if (opt_iter->name_filter.empty()) {
return true;
}
else {
OcrWithPreprocessImageAnalyzer name_analyzer(lhs.name_img);
name_analyzer.set_replace(
Task.get<OcrTaskInfo>("CharsNameOcrReplace")->replace_map);
Log.trace("Analyze name filter");
if (!name_analyzer.analyze()) {
return false;
}
std::string name = name_analyzer.get_result().front().text;
return std::ranges::find(std::as_const(opt_iter->name_filter)
, name)
!= opt_iter->name_filter.cend();
}
});
if (find_iter == cur_all_opers.cend()) {
++opt_iter;
@@ -547,13 +547,13 @@ bool asst::InfrastProductionTask::opers_choose()
m_ctrler->click(find_iter->rect);
}
{
auto avlb_iter = std::find_if(
m_all_available_opers.cbegin(), m_all_available_opers.cend(),
[&](const infrast::Oper& lhs) -> bool {
int dist = HashImageAnalyzer::hamming(lhs.face_hash, find_iter->face_hash);
Log.debug("opers_choose | face hash dist", dist);
return dist < face_hash_thres;
}
auto avlb_iter = std::ranges::find_if(std::as_const(m_all_available_opers)
,
[&](const infrast::Oper& lhs) -> bool {
int dist = HashImageAnalyzer::hamming(lhs.face_hash, find_iter->face_hash);
Log.debug("opers_choose | face hash dist", dist);
return dist < face_hash_thres;
}
);
if (avlb_iter != m_all_available_opers.cend()) {
m_all_available_opers.erase(avlb_iter);