From c95aa1edd554a40211a374df77e7ac8753e7baa4 Mon Sep 17 00:00:00 2001 From: Aleksey Lobanov Date: Thu, 26 Apr 2018 01:17:15 +0300 Subject: [PATCH] No class extending if size of it more than needed --- main.cpp | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/main.cpp b/main.cpp index 5df8e9e..34529ff 100644 --- a/main.cpp +++ b/main.cpp @@ -272,6 +272,12 @@ pair>, bool> extend_function_class( const set>& base_class, size_t max_size ) { + if ( base_class.size() >= max_size ) + return make_pair( + set>(base_class.begin(), base_class.end()), + false + ); + FiniteFunction identical_x(string("000 111 222")); FiniteFunction identical_y(string("012 012 012")); auto FiniteFunctionHasher = [](const FiniteFunction &f) -> uint32_t { @@ -335,7 +341,7 @@ bool is_bad_class( size_t total_possible_functions; atomic completed_tasks; - +atomic tasks_to_extend; // количество тасков, которые не обработаны list< set> > shared_function_classes; mutex shared_functions_mutex; @@ -375,10 +381,10 @@ void do_work() { task.current, get_math_coeff(task.current_max_coeff) ); - //cout << "k=" << task.current_max_coeff << endl; processed_task_mutex.lock(); processed_task_list.push_back(task); processed_task_mutex.unlock(); + --tasks_to_extend; } } @@ -389,13 +395,34 @@ void process_task_lists() { std::chrono::milliseconds SLEEP_TIME(10); while ( completed_tasks < total_possible_functions ) { + if ( tasks_to_extend ) { + // подождём, пока не закончатся таски в очереди + std::this_thread::sleep_for(SLEEP_TIME); + continue; + } + task_mutex.lock(); + task_list.shrink_to_fit(); + task_mutex.unlock(); + // опустошим выполненные таски processed_task_mutex.lock(); - vector local_processed_task_list = processed_task_list; + vector local_processed_tasks = processed_task_list; processed_task_list.clear(); + processed_task_list.shrink_to_fit(); processed_task_mutex.unlock(); - for ( auto&& task: local_processed_task_list ) { + cout << "sorting finished of "<< local_processed_tasks.size() << endl; + // Обеспечим увеличение размеров, чтобы не было проблем со включением одного + // в другое + sort( + local_processed_tasks.begin(), + local_processed_tasks.end(), + [](const FunctionTask& a, const FunctionTask& b) { + return a.current.size() < b.current.size(); + } + ); + + for ( auto&& task: local_processed_tasks ) { if ( !task.is_finished ) { if ( is_bad_class(task.current, bad_functions) ) { //cout << "bad class" << endl; @@ -404,6 +431,7 @@ void process_task_lists() { } else { ++task.current_max_coeff; task_mutex.lock(); + ++tasks_to_extend; task_list.push_back(task); task_mutex.unlock(); } @@ -453,7 +481,7 @@ void process_task_lists() { } } } - local_processed_task_list.clear(); + local_processed_tasks.clear(); // Поспим, чтобы не работать слишком часто std::this_thread::sleep_for(SLEEP_TIME); @@ -506,7 +534,7 @@ int main() { cout << "Total funcs in list " << task_list.size() << " functions" << endl; total_possible_functions = task_list.size(); - + tasks_to_extend = task_list.size(); vector< thread > thread_pool; thread task_processer(process_task_lists);