use remembered_stat

This commit is contained in:
starcells committed 2026-07-25 01:08:13 +08:00
1 parent 39ff890215
commit edc2c63f14
3 files changed
+22 -40

No files matched your search

+4 -11
View File
@@ -12,7 +12,7 @@
#include <unordered_set>
#include <utility>
#define CELL_SIZE 5
#define CELL_SIZE 8
#define CELL_SIZE_BITWIDTH std::bit_width(unsigned(CELL_SIZE))
#define INS_SIZE (CELL_SIZE + 1)
@@ -23,13 +23,7 @@
namespace r8veritas {
// using cell = std::bitset<CELL_SIZE>;
using cell = size_t;
using stat = struct stat {
std::array<r8veritas::cell, (1 << CELL_SIZE)> datas;
r8veritas::cell unvisited_cell = 0;
bool operator==(const r8veritas::stat &s) const {
return (this->datas == s.datas);
}
};
using stat = std::array<r8veritas::cell, (1 << CELL_SIZE)>;
using stat_ref = const stat *;
using truthtable = std::pair<const r8veritas::stat *, const r8veritas::stat *>;
using op = std::function<r8veritas::cell(r8veritas::cell)>;
@@ -87,7 +81,7 @@ namespace std {
template <> struct hash<r8veritas::stat> {
size_t operator()(const r8veritas::stat &s) const {
size_t h = 0;
for (auto i : s.datas) {
for (auto i : s) {
h ^= hash<r8veritas::cell>{}(i) + 0x9e3779b9 + (h << 6) + (h >> 2);
}
return h;
@@ -113,7 +107,7 @@ template <> struct hash<pair<r8veritas::stat_ref, r8veritas::ins_bytecode>> {
r8veritas::truthtable generate_truthtable(const r8veritas::op &operation);
std::pair<r8veritas::stat_ref, bool>
operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins,bool is_initialization = false);
operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins);
// true means that {stat,ins} has been operated.
static std::unordered_map<
@@ -139,5 +133,4 @@ std::ostream &operator<<(std::ostream &os, const r8veritas::stat_ref &s);
std::ostream &operator<<(std::ostream &os, const r8veritas::ins &s);
static std::unordered_set<r8veritas::stat> stat_pool;
#endif
+17 -28
View File
@@ -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();
+1 -1
View File
@@ -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);