186 lines
5.1 KiB
C++
186 lines
5.1 KiB
C++
#include "r8veritas.hh"
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <bitset>
|
|
#include <iostream>
|
|
#include <ostream>
|
|
#include <queue>
|
|
#include <string>
|
|
#include <unordered_set>
|
|
#include <utility>
|
|
void initialize_nicknames() {
|
|
for (r8veritas::index i = 0; i < CELL_SIZE; i++) {
|
|
nicknames[i] = "%" + std::to_string(i + 1);
|
|
}
|
|
return;
|
|
}
|
|
|
|
void initialize_tp_paths() {
|
|
for (r8veritas::index i = 0; i < CELL_SIZE; i++) {
|
|
tp_paths[i] = ".tp " + nicknames[i];
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
r8veritas::stat_ref generate_empty_stat() {
|
|
r8veritas::stat arr;
|
|
for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) {
|
|
arr[i] = 0;
|
|
}
|
|
auto tmp_ptr = stat_pool.insert(std::move(arr)).first;
|
|
return &(*tmp_ptr);
|
|
}
|
|
|
|
// WARNING:ST WILL BE DESTROYED
|
|
r8veritas::stat_ref save_stat(r8veritas::stat &&st) {
|
|
return &(*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();
|
|
for (r8veritas::index i = 0; i < (1 << CELL_SIZE); 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) {
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
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_ref &s) {
|
|
for (auto i : *s) {
|
|
os << std::bitset<CELL_SIZE>(i).to_string() << " ";
|
|
}
|
|
return (os);
|
|
}
|
|
|
|
std::ostream &operator<<(std::ostream &os, const r8veritas::ins &s) {
|
|
|
|
auto str = std::bitset<INS_SIZE>(s.bc).to_string();
|
|
os << tp_paths[s.sacrifice] << std::endl
|
|
<< "biz" << std::endl
|
|
<< "\tflb" << std::endl;
|
|
/*
|
|
.tp xxx
|
|
biz
|
|
flb
|
|
*/
|
|
std::reverse(&str[0], &str[CELL_SIZE]);
|
|
if ((s.bc != 0b0) && (s.bc != 0b1)) {
|
|
for (r8veritas::index i = 0; i < str.length() - 1; i++) {
|
|
if (str[i] == '1') {
|
|
os << "\t" << tp_paths[i] << std::endl;
|
|
/*
|
|
.tp yyy
|
|
*/
|
|
if (str[CELL_SIZE] == '1')
|
|
os << "\tflb" << std::endl;
|
|
else
|
|
os << "\tset_1" << std::endl;
|
|
}
|
|
}
|
|
os << "\t" << tp_paths[s.sacrifice] << std::endl;
|
|
}
|
|
os << "rnz" << std::endl;
|
|
/*
|
|
rnz
|
|
*/
|
|
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_ref> stats;
|
|
static std::unordered_set<r8veritas::stat_ref> remembered_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_ref tmp_stat_ref;
|
|
std::pair<r8veritas::stat_ref, bool> tmp_ans;
|
|
auto operated = false;
|
|
for (; i > 0; i--) {
|
|
tmp_stat_ref = stats.front();
|
|
for (auto j : r8veritas::all_ins) {
|
|
tmp_ans = operate_ins(tmp_stat_ref, j);
|
|
if (tmp_ans.first == truthtable.second) {
|
|
while (!stats.empty())
|
|
stats.pop();
|
|
remembered_stats.clear();
|
|
return (true);
|
|
}
|
|
if (!remembered_stats.contains(tmp_ans.first)) {
|
|
stats.push(tmp_ans.first);
|
|
remembered_stats.insert(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;
|
|
}
|
|
return p;
|
|
} |