diff --git a/include/r8veritas.hh b/include/r8veritas.hh index 5363f69..01467d2 100644 --- a/include/r8veritas.hh +++ b/include/r8veritas.hh @@ -6,13 +6,12 @@ #include #include #include -#include #include #include #include #include -#define CELL_SIZE 5 +#define CELL_SIZE 8 #define CELL_SIZE_BITWIDTH std::bit_width(unsigned(CELL_SIZE)) #define INS_SIZE (CELL_SIZE + 1) @@ -24,7 +23,8 @@ namespace r8veritas { // using cell = std::bitset; using cell = size_t; using stat = std::array; -using truthtable = std::pair; +using stat_ref = const stat *; +using truthtable = std::pair; using op = std::function; using index = size_t; using influence = size_t; @@ -35,6 +35,8 @@ using ins_body = std::bitset; using ins_bytecode = std::bitset; */ +static std::set stat_pool; + enum ins_opcode { SET1 = 0, FLIP = 1, @@ -50,7 +52,8 @@ using ins = struct ins { inline r8veritas::ins to_ins(const r8veritas::ins_bytecode &bc) { return {bc >> CELL_SIZE_BITWIDTH, bc & ((1 << CELL_SIZE_BITWIDTH) - 1)}; } -using path = std::stack>; +using path = + std::stack>; static std::array all_influences; @@ -75,33 +78,23 @@ static const std::set all_ins = } // namespace r8veritas -inline r8veritas::stat generate_empty_stat() { +inline r8veritas::stat_ref generate_empty_stat() { r8veritas::stat arr; for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) { arr[i] = 0; } - return (arr); + auto tmp_ptr = r8veritas::stat_pool.insert(std::move(arr)).first; + return &(*tmp_ptr); } namespace std { // Overload hash to make some unordered_maps work. -template <> struct hash { - // Magic,from Boost's hash_combine. - // An explanation from MCMC here: https://zhuanlan.zhihu.com/p/574573421 - size_t operator()(const r8veritas::stat &p) const { - size_t h = 0; - for (auto iter : p) { - h ^= hash{}(iter) + 0x9e3779b9 + (h << 6) + (h >> 2); - } - return (h); - } -}; -template <> struct hash> { +template <> struct hash> { // Yep,magic again. - size_t - operator()(const pair &p) const { - size_t h = hash{}(p.first) + 0x9e3779b9; + size_t operator()( + const pair &p) const { + size_t h = hash{}(p.first) + 0x9e3779b9; h ^= hash{}(p.second) + 0x9e3779b9 + (h << 6) + (h >> 2); return h; @@ -114,16 +107,18 @@ template <> struct hash> { #define OPCODE_FLIP (static_cast(r8veritas::ins_opcode::FLIP)) r8veritas::truthtable generate_truthtable(const r8veritas::op &operation); -std::pair operate_ins(const r8veritas::stat &last_stat, - const r8veritas::ins &ins); +std::pair +operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins); // true means that {stat,ins} has been operated. -static std::unordered_map, - r8veritas::stat> +static std::unordered_map< + std::pair, + r8veritas::stat_ref> visited_stat; -static std::unordered_map> +static std::unordered_map< + r8veritas::stat_ref, + std::pair> visited_op; std::ostream &operator<<(std::ostream &os, const r8veritas::stat &s); @@ -135,7 +130,7 @@ compress_stat_space(const r8veritas::truthtable &raw_truthtable); bool bfs(const r8veritas::truthtable &truthtable, r8veritas::index layer = 1); r8veritas::path track_path(const r8veritas::truthtable &truthtable); -std::ostream &operator<<(std::ostream &os, const r8veritas::stat &s); +std::ostream &operator<<(std::ostream &os, const r8veritas::stat_ref &s); std::ostream &operator<<(std::ostream &os, const r8veritas::ins &s); #endif \ No newline at end of file diff --git a/src/r8veritas.cc b/src/r8veritas.cc index 1b4e775..8a15055 100644 --- a/src/r8veritas.cc +++ b/src/r8veritas.cc @@ -7,20 +7,26 @@ #include #include +// WARNING:ST WILL BE DESTROYED +r8veritas::stat_ref save_stat(r8veritas::stat &&st) { + return &(*r8veritas::stat_pool.insert(std::move(st)).first); +} + r8veritas::truthtable generate_truthtable(const r8veritas::op &operation) { - r8veritas::stat inp = generate_empty_stat(); - r8veritas::stat out = generate_empty_stat(); + r8veritas::stat inp = *generate_empty_stat(); + r8veritas::stat out = *generate_empty_stat(); for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) { inp[i] = i; out[i] = operation(r8veritas::cell(i)); } - return (r8veritas::truthtable(inp, out)); + return (r8veritas::truthtable(save_stat(std::move(inp)), + save_stat(std::move(out)))); } -std::pair operate_ins(const r8veritas::stat &last_stat, - const r8veritas::ins &ins) { +std::pair +operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins) { auto key = std::pair(last_stat, r8veritas::ins_bytecode(ins)); - r8veritas::stat next_stat = last_stat; + r8veritas::stat next_stat = *last_stat; auto it = visited_stat.find(key); if (it != visited_stat.end()) { // foud in visited_stat return {it->second, true}; @@ -39,13 +45,14 @@ std::pair operate_ins(const r8veritas::stat &last_stat, } } } - visited_stat.insert(std::pair(key, next_stat)); - visited_op.insert(std::pair(next_stat, std::move(key))); - return {next_stat, false}; + r8veritas::stat_ref tmp_next_stat_ref = save_stat(std::move(next_stat)); + visited_stat.insert(std::pair(key, tmp_next_stat_ref)); + visited_op.insert(std::pair(tmp_next_stat_ref, std::move(key))); + return {tmp_next_stat_ref, false}; } -std::ostream &operator<<(std::ostream &os, const r8veritas::stat &s) { - for (auto i : s) { +std::ostream &operator<<(std::ostream &os, const r8veritas::stat_ref &s) { + for (auto i : *s) { os << std::bitset(i).to_string() << " "; } return (os); @@ -79,7 +86,7 @@ compress_stat_space(const r8veritas::truthtable &raw_truthtable) { for (r8veritas::index i = 0; i < CELL_SIZE; i++) { influences[i] = 0; for (r8veritas::index j = 0; j < (1 << CELL_SIZE); j++) { - if (raw_truthtable.second[j] != raw_truthtable.second[j ^ (1 << i)]) { + if ((*(raw_truthtable.second))[j] != (*(raw_truthtable.second))[j ^ (1 << i)]) { influences[i]++; } } @@ -93,7 +100,7 @@ compress_stat_space(const r8veritas::truthtable &raw_truthtable) { return (new_truthtable); } -static std::queue stats; +static std::queue stats; bool bfs(const r8veritas::truthtable &truthtable, r8veritas::index layer) { if (layer == 1) { @@ -106,13 +113,13 @@ bool bfs(const r8veritas::truthtable &truthtable, r8veritas::index layer) { r8veritas::index i = stats.size(); std::cout << "LAYER=" << layer << ",stats_size=" << i << std::endl; - r8veritas::stat tmp_stat; - std::pair tmp_ans; + r8veritas::stat_ref tmp_stat_ref; + std::pair tmp_ans; auto operated = false; for (; i > 0; i--) { - tmp_stat = stats.front(); + tmp_stat_ref = stats.front(); for (auto j : r8veritas::all_ins) { - tmp_ans = operate_ins(tmp_stat, j); + tmp_ans = operate_ins(tmp_stat_ref, j); if (tmp_ans.first == truthtable.second) { return (true); } diff --git a/tests/r8_test.cc b/tests/r8_test.cc index 99a73d3..9a3d7ef 100644 --- a/tests/r8_test.cc +++ b/tests/r8_test.cc @@ -21,7 +21,7 @@ r8veritas::cell func_full_add(r8veritas::cell inp) { } int main() { - auto tab = generate_truthtable(func_and); + auto tab = generate_truthtable(func_full_add); auto new_tab = compress_stat_space(tab); if (bfs(new_tab)) { r8veritas::path p = track_path(tab);