use remembered_stat
This commit is contained in:
1 parent
39ff890215
commit
edc2c63f14
3 files changed
+22
-40
No files matched your search
+17
-28
@@ -5,12 +5,13 @@
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <queue>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
|
||||
r8veritas::stat_ref generate_empty_stat() {
|
||||
r8veritas::stat arr;
|
||||
for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) {
|
||||
arr.datas[i] = 0;
|
||||
arr[i] = 0;
|
||||
}
|
||||
auto tmp_ptr = stat_pool.insert(std::move(arr)).first;
|
||||
return &(*tmp_ptr);
|
||||
@@ -23,20 +24,17 @@ r8veritas::stat_ref save_stat(r8veritas::stat &&st) {
|
||||
|
||||
r8veritas::truthtable generate_truthtable(const r8veritas::op &operation) {
|
||||
r8veritas::stat inp = *generate_empty_stat();
|
||||
inp.unvisited_cell = (1 << CELL_SIZE) - 1;
|
||||
r8veritas::stat out = *generate_empty_stat();
|
||||
out.unvisited_cell = 0;
|
||||
for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) {
|
||||
inp.datas[i] = i;
|
||||
out.datas[i] = operation(r8veritas::cell(i));
|
||||
inp[i] = i;
|
||||
out[i] = operation(r8veritas::cell(i));
|
||||
}
|
||||
return (r8veritas::truthtable(save_stat(std::move(inp)),
|
||||
save_stat(std::move(out))));
|
||||
}
|
||||
|
||||
std::pair<r8veritas::stat_ref, bool>
|
||||
operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins,
|
||||
bool is_initialization) {
|
||||
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;
|
||||
auto it = visited_stat.find(key);
|
||||
@@ -48,22 +46,15 @@ operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins,
|
||||
r8veritas::cell cell_mask = (ins.bc >> 1);
|
||||
|
||||
for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) {
|
||||
if ((next_stat.datas[i] & (1 << ins.sacrifice)) != 0) {
|
||||
next_stat.datas[i] ^= (1 << ins.sacrifice);
|
||||
if ((next_stat[i] & (1 << ins.sacrifice)) != 0) {
|
||||
next_stat[i] ^= (1 << ins.sacrifice);
|
||||
if (opcode == OPCODE_SET1) {
|
||||
next_stat.datas[i] |= cell_mask;
|
||||
next_stat[i] |= cell_mask;
|
||||
} else if (opcode == OPCODE_FLIP) {
|
||||
next_stat.datas[i] ^= cell_mask;
|
||||
next_stat[i] ^= cell_mask;
|
||||
}
|
||||
}
|
||||
}
|
||||
next_stat.unvisited_cell = last_stat->unvisited_cell;
|
||||
if (!is_initialization) {
|
||||
next_stat.unvisited_cell &= ~(1 << ins.sacrifice);
|
||||
next_stat.unvisited_cell &= ~(ins.bc >> 1);
|
||||
} else {
|
||||
next_stat.unvisited_cell ^= ~(1 << ins.sacrifice);
|
||||
}
|
||||
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)));
|
||||
@@ -71,7 +62,7 @@ operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins,
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const r8veritas::stat_ref &s) {
|
||||
for (auto i : (*s).datas) {
|
||||
for (auto i : *s) {
|
||||
os << std::bitset<CELL_SIZE>(i).to_string() << " ";
|
||||
}
|
||||
return (os);
|
||||
@@ -105,8 +96,8 @@ 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)).datas[j] !=
|
||||
(*(raw_truthtable.second)).datas[j ^ (1 << i)]) {
|
||||
if ((*(raw_truthtable.second))[j] !=
|
||||
(*(raw_truthtable.second))[j ^ (1 << i)]) {
|
||||
influences[i]++;
|
||||
}
|
||||
}
|
||||
@@ -114,14 +105,14 @@ compress_stat_space(const r8veritas::truthtable &raw_truthtable) {
|
||||
}
|
||||
for (r8veritas::index i = 0; i < CELL_SIZE; i++) {
|
||||
if (influences[i] == 0) {
|
||||
new_truthtable.first =
|
||||
operate_ins(new_truthtable.first, {0, i}, true).first;
|
||||
new_truthtable.first = operate_ins(new_truthtable.first, {0, i}).first;
|
||||
}
|
||||
}
|
||||
return (new_truthtable);
|
||||
}
|
||||
|
||||
static std::queue<r8veritas::stat_ref> stats;
|
||||
static std::unordered_set<r8veritas::stat_ref> remembered_stats;
|
||||
bool bfs(const r8veritas::truthtable &truthtable, r8veritas::index layer) {
|
||||
|
||||
if (layer == 1) {
|
||||
@@ -140,15 +131,13 @@ bool bfs(const r8veritas::truthtable &truthtable, r8veritas::index layer) {
|
||||
for (; i > 0; i--) {
|
||||
tmp_stat_ref = stats.front();
|
||||
for (auto j : r8veritas::all_ins) {
|
||||
if (((1 << (j.sacrifice)) & (tmp_stat_ref->unvisited_cell)) == 0) {
|
||||
tmp_ans = operate_ins(tmp_stat_ref, j);
|
||||
} else
|
||||
continue;
|
||||
tmp_ans = operate_ins(tmp_stat_ref, j);
|
||||
if (tmp_ans.first == truthtable.second) {
|
||||
return (true);
|
||||
}
|
||||
if (!(tmp_ans.second)) {
|
||||
if (!((tmp_ans.second) || remembered_stats.contains(tmp_ans.first))) {
|
||||
stats.push(tmp_ans.first);
|
||||
remembered_stats.insert(tmp_ans.first);
|
||||
}
|
||||
}
|
||||
stats.pop();
|
||||
|
||||
Reference in new issue
Block a user