Files
R8VeritasCC/src/r8veritas.cc
T
2026-07-24 20:43:34 +08:00

113 lines
3.3 KiB
C++

#include "r8veritas.hh"
#include <array>
#include <bitset>
#include <iostream>
#include <queue>
#include <utility>
r8veritas::truthtable generate_truthtable(const r8veritas::op &operation) {
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));
}
std::pair<r8veritas::stat, bool> operate_ins(const r8veritas::stat &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);
if (it != visited_stat.end()) { // foud <last_stat,ins> in visited_stat
return {it->second, true};
}
int opcode = ins.bc & 1;
r8veritas::cell cell_mask = (ins.bc >> 1);
for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) {
if ((next_stat[i] & (1 << ins.sacrifice)) != 0) {
next_stat[i] ^= (1 << ins.sacrifice);
if (opcode == OPCODE_SET1) {
next_stat[i] |= cell_mask;
} else if (opcode == OPCODE_FLIP) {
next_stat[i] ^= cell_mask;
}
}
}
visited_stat.insert_or_assign(key, next_stat);
visited_op.insert_or_assign(next_stat, std::move(key));
return {next_stat, false};
}
std::ostream &operator<<(std::ostream &os, const r8veritas::stat &s) {
for (auto i : s) {
os << std::bitset<CELL_SIZE>(i).to_string() << " ";
}
return (os);
}
r8veritas::truthtable
compress_stat_space(const r8veritas::truthtable &raw_truthtable) {
std::array<r8veritas::influence, CELL_SIZE> influences;
r8veritas::truthtable new_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)]) {
influences[i]++;
}
}
influences[i] >>= 1;
}
for (r8veritas::index i = 0; i < CELL_SIZE; i++) {
if (influences[i] == 0) {
new_truthtable.first = operate_ins(new_truthtable.first, {0, i}).first;
}
}
return (new_truthtable);
}
static std::queue<r8veritas::stat> stats;
bool bfs(const r8veritas::truthtable &truthtable, r8veritas::index layer) {
if (layer == 1) {
stats.push(truthtable.first);
}
if (stats.empty()) {
return (false);
}
r8veritas::index i = stats.size();
std::cout << "LAYER=" << layer << ",stats_size=" << i << std::endl;
r8veritas::stat tmp_stat;
std::pair<r8veritas::stat, bool> tmp_ans;
auto operated = false;
for (; i > 0; i--) {
tmp_stat = stats.front();
for (auto j : r8veritas::all_ins) {
tmp_ans = operate_ins(tmp_stat, j);
if (tmp_ans.first == truthtable.second) {
return (true);
}
if (!(tmp_ans.second)) {
stats.push(tmp_ans.first);
}
}
stats.pop();
}
return (bfs(truthtable, layer + 1));
}
r8veritas::path track_path(const r8veritas::truthtable &truthtable) {
r8veritas::path p;
for (auto tmp_stat = truthtable.second; tmp_stat != truthtable.first;) {
p.push(visited_op[tmp_stat]);
tmp_stat = p.top().first;
std::cout << tmp_stat << ";" << p.top().second << std::endl;
}
return p;
}