Currently moving from Gitlab to Gitea at https://labs.phundrak.com. Some features may not work anymore on this Gitlab instance.

less raw for-loops

parent 0aa81fde
......@@ -294,9 +294,8 @@ void ImageManipulator::get_color_set()
thread_list.push_back(
std::thread(&ImageManipulator::threaded_get_color, this, h + i));
}
for (auto& th : thread_list) {
th.join();
}
std::for_each(thread_list.begin(), thread_list.end(),
[](auto& th) { th.join(); });
}
colors_.shrink_to_fit();
}
......@@ -339,12 +338,11 @@ void ImageManipulator::draw_square(cv::Mat& t_img,
int const t_size,
cv::Scalar const& t_color) const
{
auto points = std::make_unique<cv::Point[]>(4);
points[0] = t_top_left;
points[1] = cv::Point{t_top_left.x, t_top_left.y + t_size};
points[2] = cv::Point{t_top_left.x + t_size, t_top_left.y + t_size};
points[3] = cv::Point{t_top_left.x + t_size, t_top_left.y};
fillConvexPoly(t_img, points.get(), 4, t_color);
std::array<cv::Point, 4> points
= {t_top_left, cv::Point{t_top_left.x, t_top_left.y + t_size},
cv::Point{t_top_left.x + t_size, t_top_left.y + t_size},
cv::Point{t_top_left.x + t_size, t_top_left.y}};
fillConvexPoly(t_img, points.data(), 4, t_color);
}
/**
......@@ -406,8 +404,6 @@ void ImageManipulator::method1()
void ImageManipulator::method2()
{
spdlog::debug("Beginning method2, initial difference: {}", diff_);
spdlog::debug("nb_total_iter: {}, nb_remaining_iter: {}", total_iterations_,
remaining_iter_);
spdlog::debug("Running on {} threads", thread_nbr);
get_color_set();
spdlog::debug("{} colors detected", colors_.size());
......@@ -421,8 +417,6 @@ void ImageManipulator::method2()
void ImageManipulator::method3()
{
spdlog::debug("Beginning method3, initial difference: {}", diff_);
spdlog::debug("nb_total_iter: {}, nb_remaining_iter: {}", total_iterations_,
remaining_iter_);
spdlog::debug("Running on {} threads", thread_nbr);
get_color_set();
spdlog::debug("{} colors detected", colors_.size());
......@@ -440,8 +434,6 @@ void ImageManipulator::method3()
void ImageManipulator::method4(bool const t_controlled_size)
{
spdlog::debug("Beginning method4, initial difference: {}", diff_);
spdlog::debug("nb_total_iter: {}, nb_remaining_iter: {}", total_iterations_,
remaining_iter_);
spdlog::debug("Running on {} threads", thread_nbr);
get_color_set();
spdlog::debug("{} colors detected", colors_.size());
......@@ -449,16 +441,19 @@ void ImageManipulator::method4(bool const t_controlled_size)
std::vector<std::future<std::optional<std::pair<cv::Mat, double>>>>
results{};
std::vector<std::pair<cv::Mat, double>> values{};
// launch asynchronously candidate image generation
for (size_t i = 0; i < thread_nbr; ++i) {
results.push_back(std::async(std::launch::async,
&ImageManipulator::create_candidate, this,
t_controlled_size));
}
for (auto& elem : results) {
if (auto res = elem.get(); res.has_value() && res->second < diff_) {
// if candidate is a success, store it
std::for_each(results.begin(), results.end(), [&values, this](auto& elem) {
if (auto res = elem.get(); res.has_value() && res->second < this->diff_) {
values.push_back(*res);
}
}
});
// apply best candidate
if (values.size() > 0) {
auto const pos
= std::min_element(std::begin(values), std::end(values),
......@@ -488,14 +483,13 @@ void ImageManipulator::method5(bool const t_controlled_size,
spdlog::debug("{} tiles", tiles.size());
std::vector<std::thread> thread_list{};
for (auto& row : tiles) {
for (auto& tile : row) {
std::for_each(tiles.begin(), tiles.end(), [&](auto& row) {
std::for_each(row.begin(), row.end(), [&](auto& tile) {
thread_list.emplace_back(
[&]() { tile.exec_method(t_submethod, t_controlled_size); });
}
}
for (auto& th : thread_list) {
th.join();
}
});
});
std::for_each(thread_list.begin(), thread_list.end(),
[](auto& th) { th.join(); });
merge_tiles(tiles);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment