forked from starcells/R8VeritasCC
use stat_ref and stat_pool instead of stat
This commit is contained in:
1 parent
f42aca2e4f
commit
d441982dad
3 files changed
+48
-46
No files matched your search
+23
-28
@@ -6,13 +6,12 @@
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <ostream>
|
||||
#include <queue>
|
||||
#include <set>
|
||||
#include <stack>
|
||||
#include <unordered_map>
|
||||
#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)
|
||||
@@ -24,7 +23,8 @@ namespace r8veritas {
|
||||
// using cell = std::bitset<CELL_SIZE>;
|
||||
using cell = size_t;
|
||||
using stat = std::array<r8veritas::cell, (1 << CELL_SIZE)>;
|
||||
using truthtable = std::pair<r8veritas::stat, r8veritas::stat>;
|
||||
using stat_ref = const stat *;
|
||||
using truthtable = std::pair<const r8veritas::stat *, const r8veritas::stat *>;
|
||||
using op = std::function<r8veritas::cell(r8veritas::cell)>;
|
||||
using index = size_t;
|
||||
using influence = size_t;
|
||||
@@ -35,6 +35,8 @@ using ins_body = std::bitset<INS_SIZE>;
|
||||
using ins_bytecode = std::bitset<INS_BYTECODE_SIZE>;
|
||||
*/
|
||||
|
||||
static std::set<stat> stat_pool;
|
||||
|
||||
enum ins_opcode {
|
||||
SET1 = 0,
|
||||
FLIP = 1,
|
||||
@@ -50,7 +52,8 @@ using ins = struct ins {
|
||||
inline r8veritas::ins to_ins(const r8veritas::ins_bytecode &bc) {
|
||||
return {bc >> CELL_SIZE_BITWIDTH, bc & ((1 << CELL_SIZE_BITWIDTH) - 1)};
|
||||
}
|
||||
using path = std::stack<std::pair<r8veritas::stat, r8veritas::ins_bytecode>>;
|
||||
using path =
|
||||
std::stack<std::pair<r8veritas::stat_ref, r8veritas::ins_bytecode>>;
|
||||
|
||||
static std::array<r8veritas::influence, CELL_SIZE> all_influences;
|
||||
|
||||
@@ -75,33 +78,23 @@ static const std::set<r8veritas::ins> all_ins =
|
||||
|
||||
} // namespace r8veritas
|
||||
|
||||
inline r8veritas::stat generate_empty_stat() {
|
||||
inline r8veritas::stat_ref generate_empty_stat() {
|
||||
r8veritas::stat arr;
|
||||
for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) {
|
||||
arr[i] = 0;
|
||||
}
|
||||
return (arr);
|
||||
auto tmp_ptr = r8veritas::stat_pool.insert(std::move(arr)).first;
|
||||
return &(*tmp_ptr);
|
||||
}
|
||||
|
||||
namespace std {
|
||||
|
||||
// Overload hash to make some unordered_maps work.
|
||||
template <> struct hash<r8veritas::stat> {
|
||||
// Magic,from Boost's hash_combine.
|
||||
// An explanation from MCMC here: https://zhuanlan.zhihu.com/p/574573421
|
||||
size_t operator()(const r8veritas::stat &p) const {
|
||||
size_t h = 0;
|
||||
for (auto iter : p) {
|
||||
h ^= hash<r8veritas::cell>{}(iter) + 0x9e3779b9 + (h << 6) + (h >> 2);
|
||||
}
|
||||
return (h);
|
||||
}
|
||||
};
|
||||
template <> struct hash<pair<r8veritas::stat, r8veritas::ins_bytecode>> {
|
||||
template <> struct hash<pair<r8veritas::stat_ref, r8veritas::ins_bytecode>> {
|
||||
// Yep,magic again.
|
||||
size_t
|
||||
operator()(const pair<r8veritas::stat, r8veritas::ins_bytecode> &p) const {
|
||||
size_t h = hash<r8veritas::stat>{}(p.first) + 0x9e3779b9;
|
||||
size_t operator()(
|
||||
const pair<r8veritas::stat_ref, r8veritas::ins_bytecode> &p) const {
|
||||
size_t h = hash<r8veritas::stat_ref>{}(p.first) + 0x9e3779b9;
|
||||
h ^= hash<r8veritas::ins_bytecode>{}(p.second) + 0x9e3779b9 + (h << 6) +
|
||||
(h >> 2);
|
||||
return h;
|
||||
@@ -114,16 +107,18 @@ template <> struct hash<pair<r8veritas::stat, r8veritas::ins_bytecode>> {
|
||||
#define OPCODE_FLIP (static_cast<int>(r8veritas::ins_opcode::FLIP))
|
||||
|
||||
r8veritas::truthtable generate_truthtable(const r8veritas::op &operation);
|
||||
std::pair<r8veritas::stat, bool> operate_ins(const r8veritas::stat &last_stat,
|
||||
const r8veritas::ins &ins);
|
||||
std::pair<r8veritas::stat_ref, bool>
|
||||
operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins);
|
||||
// true means that {stat,ins} has been operated.
|
||||
|
||||
static std::unordered_map<std::pair<r8veritas::stat, r8veritas::ins_bytecode>,
|
||||
r8veritas::stat>
|
||||
static std::unordered_map<
|
||||
std::pair<r8veritas::stat_ref, r8veritas::ins_bytecode>,
|
||||
r8veritas::stat_ref>
|
||||
visited_stat;
|
||||
|
||||
static std::unordered_map<r8veritas::stat,
|
||||
std::pair<r8veritas::stat, r8veritas::ins_bytecode>>
|
||||
static std::unordered_map<
|
||||
r8veritas::stat_ref,
|
||||
std::pair<r8veritas::stat_ref, r8veritas::ins_bytecode>>
|
||||
visited_op;
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const r8veritas::stat &s);
|
||||
@@ -135,7 +130,7 @@ compress_stat_space(const r8veritas::truthtable &raw_truthtable);
|
||||
|
||||
bool bfs(const r8veritas::truthtable &truthtable, r8veritas::index layer = 1);
|
||||
r8veritas::path track_path(const r8veritas::truthtable &truthtable);
|
||||
std::ostream &operator<<(std::ostream &os, const r8veritas::stat &s);
|
||||
std::ostream &operator<<(std::ostream &os, const r8veritas::stat_ref &s);
|
||||
std::ostream &operator<<(std::ostream &os, const r8veritas::ins &s);
|
||||
|
||||
#endif
|
||||
+24
-17
@@ -7,20 +7,26 @@
|
||||
#include <queue>
|
||||
#include <utility>
|
||||
|
||||
// WARNING:ST WILL BE DESTROYED
|
||||
r8veritas::stat_ref save_stat(r8veritas::stat &&st) {
|
||||
return &(*r8veritas::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();
|
||||
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));
|
||||
return (r8veritas::truthtable(save_stat(std::move(inp)),
|
||||
save_stat(std::move(out))));
|
||||
}
|
||||
|
||||
std::pair<r8veritas::stat, bool> operate_ins(const r8veritas::stat &last_stat,
|
||||
const r8veritas::ins &ins) {
|
||||
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;
|
||||
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};
|
||||
@@ -39,13 +45,14 @@ std::pair<r8veritas::stat, bool> operate_ins(const r8veritas::stat &last_stat,
|
||||
}
|
||||
}
|
||||
}
|
||||
visited_stat.insert(std::pair(key, next_stat));
|
||||
visited_op.insert(std::pair(next_stat, std::move(key)));
|
||||
return {next_stat, false};
|
||||
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 &s) {
|
||||
for (auto i : s) {
|
||||
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);
|
||||
@@ -79,7 +86,7 @@ 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))[j] != (*(raw_truthtable.second))[j ^ (1 << i)]) {
|
||||
influences[i]++;
|
||||
}
|
||||
}
|
||||
@@ -93,7 +100,7 @@ compress_stat_space(const r8veritas::truthtable &raw_truthtable) {
|
||||
return (new_truthtable);
|
||||
}
|
||||
|
||||
static std::queue<r8veritas::stat> stats;
|
||||
static std::queue<r8veritas::stat_ref> stats;
|
||||
bool bfs(const r8veritas::truthtable &truthtable, r8veritas::index layer) {
|
||||
|
||||
if (layer == 1) {
|
||||
@@ -106,13 +113,13 @@ bool bfs(const r8veritas::truthtable &truthtable, r8veritas::index layer) {
|
||||
|
||||
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;
|
||||
r8veritas::stat_ref tmp_stat_ref;
|
||||
std::pair<r8veritas::stat_ref, bool> tmp_ans;
|
||||
auto operated = false;
|
||||
for (; i > 0; i--) {
|
||||
tmp_stat = stats.front();
|
||||
tmp_stat_ref = stats.front();
|
||||
for (auto j : r8veritas::all_ins) {
|
||||
tmp_ans = operate_ins(tmp_stat, j);
|
||||
tmp_ans = operate_ins(tmp_stat_ref, j);
|
||||
if (tmp_ans.first == truthtable.second) {
|
||||
return (true);
|
||||
}
|
||||
|
||||
+1
-1
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user