From cf1eb0250707fe271d1ce41c2cedb162051beb1c Mon Sep 17 00:00:00 2001 From: Phuntsok Drak-pa Date: Thu, 25 Apr 2019 02:53:53 +0200 Subject: [PATCH] less raw for-loops --- src/methods.cc | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/src/methods.cc b/src/methods.cc index 4cc9f1a..ada5a0e 100644 --- a/src/methods.cc +++ b/src/methods.cc @@ -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(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 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>>> results{}; std::vector> 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 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); }