140 lines
4.3 KiB
C++
140 lines
4.3 KiB
C++
#ifndef R8VERITAS
|
|
#define R8VERITAS
|
|
|
|
#include <array>
|
|
#include <bit>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <ostream>
|
|
#include <set>
|
|
#include <stack>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <unordered_set>
|
|
#include <utility>
|
|
|
|
#define CELL_SIZE 5
|
|
#define CELL_SIZE_BITWIDTH std::bit_width(unsigned(CELL_SIZE))
|
|
|
|
#define INS_SIZE (CELL_SIZE + 1)
|
|
// mask and opcode
|
|
#define INS_BYTECODE_SIZE (INS_SIZE + CELL_SIZE_BITWIDTH)
|
|
// mask,opcode and sacrifice
|
|
|
|
namespace r8veritas {
|
|
using cell = uint8_t;
|
|
using stat = std::array<r8veritas::cell, (1 << CELL_SIZE)>;
|
|
using stat_ref = const stat *;
|
|
using truthtable = std::pair<r8veritas::stat_ref, r8veritas::stat_ref>;
|
|
using op = std::function<r8veritas::cell(r8veritas::cell)>;
|
|
using index = size_t;
|
|
using influence = size_t;
|
|
using ins_body = size_t;
|
|
using ins_bytecode = size_t;
|
|
/*
|
|
using ins_body = std::bitset<INS_SIZE>;
|
|
using ins_bytecode = std::bitset<INS_BYTECODE_SIZE>;
|
|
*/
|
|
|
|
enum ins_opcode {
|
|
SET1 = 0,
|
|
FLIP = 1,
|
|
};
|
|
|
|
struct ins {
|
|
r8veritas::ins_body bc;
|
|
r8veritas::index sacrifice;
|
|
operator const ins_bytecode() const {
|
|
return (bc << CELL_SIZE_BITWIDTH) | sacrifice;
|
|
}
|
|
};
|
|
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_ref, r8veritas::ins_bytecode>>;
|
|
|
|
inline std::array<r8veritas::influence, CELL_SIZE> all_influences;
|
|
|
|
inline const std::set<r8veritas::ins> all_ins =
|
|
[]() -> std::set<r8veritas::ins> {
|
|
std::set<r8veritas::ins> s;
|
|
for (r8veritas::index sacrifice_it = 0; sacrifice_it < CELL_SIZE;
|
|
sacrifice_it++) {
|
|
for (r8veritas::index bc_it = 0; bc_it < (1 << INS_SIZE); bc_it++) {
|
|
auto tmp = bc_it & (~(1 << (sacrifice_it + 1)));
|
|
// sacrifice shouldn't be operated,
|
|
// which means that bit in bc's mask can't be 1.
|
|
// so here I forcefully change it to 0,
|
|
// and use set to ensure it won't duplicate.
|
|
// for here's the initialization part,it's acceptable
|
|
s.insert({((tmp == 0) ? 1 : tmp), sacrifice_it});
|
|
// both bc == 0b0 and bc == 0b1 mean erasing sacrifice,so here I skip 0b0
|
|
}
|
|
}
|
|
return s;
|
|
}();
|
|
|
|
inline bool debug_output = false;
|
|
|
|
} // namespace r8veritas
|
|
|
|
namespace std {
|
|
// Overload hash to make some unordered_maps work.
|
|
|
|
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);
|
|
// Magic,from Boost's hash_combine.
|
|
// An explanation from MCMC here: https://zhuanlan.zhihu.com/p/574573421
|
|
}
|
|
return h;
|
|
}
|
|
};
|
|
|
|
template <> struct hash<pair<r8veritas::stat_ref, r8veritas::ins_bytecode>> {
|
|
size_t operator()(
|
|
const pair<r8veritas::stat_ref, r8veritas::ins_bytecode> &p) const {
|
|
size_t h = hash<r8veritas::stat_ref>{}(p.first) + 0x9e3779b9;
|
|
// Yep,magic again.
|
|
h ^= hash<r8veritas::ins_bytecode>{}(p.second) + 0x9e3779b9 + (h << 6) +
|
|
(h >> 2);
|
|
return h;
|
|
}
|
|
};
|
|
|
|
} // namespace std
|
|
|
|
#define OPCODE_SET1 (static_cast<int>(r8veritas::ins_opcode::SET1))
|
|
#define OPCODE_FLIP (static_cast<int>(r8veritas::ins_opcode::FLIP))
|
|
|
|
r8veritas::truthtable generate_truthtable(const r8veritas::op &operation);
|
|
r8veritas::stat_ref operate_ins(const r8veritas::stat_ref &last_stat,
|
|
const r8veritas::ins &ins);
|
|
|
|
inline std::unordered_map<
|
|
r8veritas::stat_ref,
|
|
std::pair<r8veritas::stat_ref, r8veritas::ins_bytecode>>
|
|
visited_op;
|
|
|
|
inline std::unordered_set<r8veritas::stat> stat_pool;
|
|
inline std::array<std::string, CELL_SIZE> nicknames;
|
|
inline std::array<std::string, CELL_SIZE> tp_paths;
|
|
|
|
std::ostream &operator<<(std::ostream &os, const r8veritas::stat &s);
|
|
|
|
r8veritas::truthtable
|
|
compress_stat_space(const r8veritas::truthtable &raw_truthtable);
|
|
// raw means input must be 0 ~ (1<<CELL_SIZE),
|
|
// which makes reaching the output of after-xor input easier.
|
|
|
|
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_ref &s);
|
|
std::ostream &operator<<(std::ostream &os, const r8veritas::ins &s);
|
|
void initialize_nicknames();
|
|
void initialize_tp_paths();
|
|
#endif |