use visited_cell to decrease stats

This commit is contained in:
starcells committed 2026-07-25 00:46:46 +08:00
1 parent 8f0cc306b5
commit 39ff890215
3 files changed
+41 -20

No files matched your search

+13 -6
View File
@@ -12,7 +12,7 @@
#include <unordered_set>
#include <utility>
#define CELL_SIZE 8
#define CELL_SIZE 5
#define CELL_SIZE_BITWIDTH std::bit_width(unsigned(CELL_SIZE))
#define INS_SIZE (CELL_SIZE + 1)
@@ -23,7 +23,13 @@
namespace r8veritas {
// using cell = std::bitset<CELL_SIZE>;
using cell = size_t;
using stat = std::array<r8veritas::cell, (1 << CELL_SIZE)>;
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_ref = const stat *;
using truthtable = std::pair<const r8veritas::stat *, const r8veritas::stat *>;
using op = std::function<r8veritas::cell(r8veritas::cell)>;
@@ -80,9 +86,9 @@ namespace std {
template <> struct hash<r8veritas::stat> {
size_t operator()(const r8veritas::stat &s) const {
size_t h=0;
for(auto i:s){
h ^= hash<r8veritas::cell>{}(i) + 0x9e3779b9 + (h<<6) + (h>>2);
size_t h = 0;
for (auto i : s.datas) {
h ^= hash<r8veritas::cell>{}(i) + 0x9e3779b9 + (h << 6) + (h >> 2);
}
return h;
}
@@ -107,7 +113,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);
operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins,bool is_initialization = false);
// true means that {stat,ins} has been operated.
static std::unordered_map<
@@ -133,4 +139,5 @@ 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
+27 -13
View File
@@ -10,7 +10,7 @@
r8veritas::stat_ref generate_empty_stat() {
r8veritas::stat arr;
for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) {
arr[i] = 0;
arr.datas[i] = 0;
}
auto tmp_ptr = stat_pool.insert(std::move(arr)).first;
return &(*tmp_ptr);
@@ -23,17 +23,20 @@ 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[i] = i;
out[i] = operation(r8veritas::cell(i));
inp.datas[i] = i;
out.datas[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) {
operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins,
bool is_initialization) {
auto key = std::pair(last_stat, r8veritas::ins_bytecode(ins));
r8veritas::stat next_stat = *last_stat;
auto it = visited_stat.find(key);
@@ -45,15 +48,22 @@ 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[i] & (1 << ins.sacrifice)) != 0) {
next_stat[i] ^= (1 << ins.sacrifice);
if ((next_stat.datas[i] & (1 << ins.sacrifice)) != 0) {
next_stat.datas[i] ^= (1 << ins.sacrifice);
if (opcode == OPCODE_SET1) {
next_stat[i] |= cell_mask;
next_stat.datas[i] |= cell_mask;
} else if (opcode == OPCODE_FLIP) {
next_stat[i] ^= cell_mask;
next_stat.datas[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)));
@@ -61,7 +71,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) {
for (auto i : (*s).datas) {
os << std::bitset<CELL_SIZE>(i).to_string() << " ";
}
return (os);
@@ -95,8 +105,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))[j] !=
(*(raw_truthtable.second))[j ^ (1 << i)]) {
if ((*(raw_truthtable.second)).datas[j] !=
(*(raw_truthtable.second)).datas[j ^ (1 << i)]) {
influences[i]++;
}
}
@@ -104,7 +114,8 @@ 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}).first;
new_truthtable.first =
operate_ins(new_truthtable.first, {0, i}, true).first;
}
}
return (new_truthtable);
@@ -129,7 +140,10 @@ bool bfs(const r8veritas::truthtable &truthtable, r8veritas::index layer) {
for (; i > 0; i--) {
tmp_stat_ref = stats.front();
for (auto j : r8veritas::all_ins) {
tmp_ans = operate_ins(tmp_stat_ref, j);
if (((1 << (j.sacrifice)) & (tmp_stat_ref->unvisited_cell)) == 0) {
tmp_ans = operate_ins(tmp_stat_ref, j);
} else
continue;
if (tmp_ans.first == truthtable.second) {
return (true);
}
+1 -1
View File
@@ -21,7 +21,7 @@ r8veritas::cell func_full_add(r8veritas::cell inp) {
}
int main() {
auto tab = generate_truthtable(func_full_add);
auto tab = generate_truthtable(func_and);
auto new_tab = compress_stat_space(tab);
if (bfs(new_tab)) {
r8veritas::path p = track_path(tab);