From a51386a8901b7251eb34877f67a6c969d42c3416 Mon Sep 17 00:00:00 2001 From: Aleksey Lobanov Date: Fri, 20 Mar 2020 14:53:01 +0300 Subject: [PATCH] [debug] Trying to save filling state --- main.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 69 insertions(+), 9 deletions(-) diff --git a/main.cpp b/main.cpp index 216c3ed..3e52774 100644 --- a/main.cpp +++ b/main.cpp @@ -12,8 +12,8 @@ using namespace std; -typedef uint16_t Storage; -const size_t ARGS_COUNT = 2; +typedef uint32_t Storage; +const size_t ARGS_COUNT = 5; const size_t FUNCTION_LEN = 1ll << ARGS_COUNT; const size_t FUNCTIONS_COUNT = 1ll << FUNCTION_LEN; typedef Function MyFunction; @@ -182,12 +182,20 @@ vector get_linear_components() { res.push_back(MyFunction("0101010101010101")); // f = x_4 function_formulas[res.back().value()] = "x_4"; } else if constexpr ( ARGS_COUNT == 5 ) { + res.push_back(MyFunction("00000000000000000000000000000000")); // f = 0 + function_formulas[res.back().value()] = "0"; res.push_back(MyFunction("11111111111111111111111111111111")); // f = 1 + function_formulas[res.back().value()] = "1"; res.push_back(MyFunction("00000000000000001111111111111111")); // f = x_1 + function_formulas[res.back().value()] = "x_1"; res.push_back(MyFunction("00000000111111110000000011111111")); // f = x_2 + function_formulas[res.back().value()] = "x_2"; res.push_back(MyFunction("00001111000011110000111100001111")); // f = x_3 + function_formulas[res.back().value()] = "x_3"; res.push_back(MyFunction("00110011001100110011001100110011")); // f = x_4 + function_formulas[res.back().value()] = "x_4"; res.push_back(MyFunction("01010101010101010101010101010101")); // f = x_5 + function_formulas[res.back().value()] = "x_5"; } else { assert (("bad args_count", false)); } @@ -227,9 +235,11 @@ vector get_all_monomials(const vector &linear_combinatio for (auto el_second: res) { bool is_added_now = res.insert(el_first and el_second).second; if ( is_added_now ) { - Storage cur_value = (el_first and el_second).value(); - function_formulas[cur_value] = preprocess_factor(function_formulas[el_first.value()]) - + " * " + preprocess_factor(function_formulas[el_second.value()]); + if constexpr ( ARGS_COUNT < 5 ) { + Storage cur_value = (el_first and el_second).value(); + function_formulas[cur_value] = preprocess_factor(function_formulas[el_first.value()]) + + " * " + preprocess_factor(function_formulas[el_second.value()]); + } } is_added = is_added or is_added_now; } @@ -241,6 +251,41 @@ string preprocess_monom(string monom) { return monom; } +inline bool exists_test0 (const std::string& name) { + ifstream f(name.c_str()); + return f.good(); +} + +string get_out_file_name(size_t rank_ind) { + return string("base_" + to_string(ARGS_COUNT) + "_rank_" + to_string(rank_ind) + ".txt"); +} + + +size_t recover_ranks(vector< vector >& ranks, vector& used_map, size_t& functions_remains) { + for (size_t rank_ind = 2; true; ++rank_ind) { + ifstream f_in(get_out_file_name(rank_ind).c_str(), std::ios::binary); + if ( not f_in.good() ) + return rank_ind; + ranks.push_back(vector()); + while ( not f_in.eof() ) { + Storage function_value; + f_in >> function_value; + MyFunction f(function_value); + used_map.at(f.value()) = rank_ind; + --functions_remains; + ranks.at(rank_ind).push_back(f); + } + } +} + +void save_rank(size_t rank_ind, vector& rank_items) { + ofstream f_out(get_out_file_name(rank_ind).c_str(), ios::binary); + for (auto f: rank_items) { + Storage function_value = f.value(); + f_out << reinterpret_cast(&function_value), sizeof(function_value); + } +} + void fill_ranks(vector monomials) { vector used_map(FUNCTIONS_COUNT, 0); size_t functions_remains = FUNCTIONS_COUNT; @@ -255,7 +300,8 @@ void fill_ranks(vector monomials) { --functions_remains; used_map.at(el.value()) = 1; } - size_t total_ranks = 1; + size_t total_ranks = recover_ranks(ranks, used_map, functions_remains); + cout << " total_ranks = " << total_ranks << endl; for (total_ranks = 1; functions_remains; ++total_ranks) { ranks.push_back(vector()); cout << "rank index = " << total_ranks << " remains: " << functions_remains << endl; @@ -265,9 +311,11 @@ void fill_ranks(vector monomials) { if ( used_map[res_el.value()] == 0 ) { --functions_remains; - function_formulas[res_el.value()] = - preprocess_monom(function_formulas[el_first.value()]) + - " + " + preprocess_monom(function_formulas[el_second.value()]); + if constexpr ( ARGS_COUNT < 5 ) { + function_formulas[res_el.value()] = + preprocess_monom(function_formulas[el_first.value()]) + + " + " + preprocess_monom(function_formulas[el_second.value()]); + } used_map.at(res_el.value()) = total_ranks; ranks.at(total_ranks).push_back(res_el); @@ -277,6 +325,17 @@ void fill_ranks(vector monomials) { if ( total_ranks - 1 > 1 ) ranks.at(total_ranks - 1).clear(); // больше не нужен cout << "size for rank " << total_ranks << " is " << ranks.at(total_ranks).size() << endl; + save_rank(total_ranks, ranks.at(total_ranks)); + } + if ( ranks.at(ranks.size() - 1).size() != 0 ) { + ranks.push_back(vector()); + total_ranks += 2; + } + cout << "ranks=" << total_ranks << endl; + for (size_t i = 0; i < ranks.size(); ++i) { + cout << "i=" << i << endl; + for (auto v: ranks.at(i)) + cout << " " << v.string() << endl; } auto possible_tranformations = get_good_matrices(); @@ -306,6 +365,7 @@ void fill_ranks(vector monomials) { used_map[marked_function.value()] = 0; ++total_functions; } + cout << "cur_rank - 1 =" << cur_rank - 1 << endl; ranks.at(cur_rank - 1).push_back(current_fn); } if ( total_functions != FUNCTIONS_COUNT)