Browse Source

feat: support `std::hash` and `std::equal_to` trait for `backtrack_t`

legacy
Dnomd343 2 years ago
parent
commit
dd9be678ea
  1. 61
      src/analyse/backtrack.cc
  2. 1
      src/fast_cal/fast_cal.h

61
src/analyse/backtrack.cc

@ -7,66 +7,33 @@
// TODO: using const RawCode& instead of uint64_t // TODO: using const RawCode& instead of uint64_t
struct backtrack_hash { namespace std {
// template <class T1, class T2> template<>
// std::size_t operator()(const std::pair<T1, T2> &v) const { struct hash<Analyse::backtrack_t> {
// return std::hash<T1>()(v.size());
// }
std::size_t operator()(const Analyse::backtrack_t &b) const { std::size_t operator()(const Analyse::backtrack_t &b) const {
std::cout << "hash -> " << b.code << std::endl; // std::cout << "get hash: " << b.code << std::endl;
return std::hash<uint64_t>()(b.code);
auto hash_ret = std::hash<uint64_t>()(b.code);
std::cout << "hash ret -> " << hash_ret << std::endl;
return b.code;
} }
};
template<>
struct equal_to<Analyse::backtrack_t> {
bool operator()(const Analyse::backtrack_t &b1, const Analyse::backtrack_t &b2) const {
// std::cout << "get eq: " << b1.code << " ? " << b2.code << std::endl;
return b1.code == b2.code;
}
}; };
}
void Analyse::backtrack_demo(uint64_t code) { void Analyse::backtrack_demo(uint64_t code) {
// std::cout << RawCode(code) << std::endl;
// TODO: confirm code exist // TODO: confirm code exist
std::cout << cases[code].step << std::endl; std::cout << cases[code].step << std::endl;
// std::vector<std::unordered_set<backtrack_t>> dat; // std::vector<std::unordered_set<backtrack_t>> dat;
auto b_hash = [](const backtrack_t &b) { std::unordered_set<backtrack_t> test;
auto hash_dat = std::hash<uint64_t>()(b.code);
std::cout << "get hash: " << b.code << " -> " << hash_dat << std::endl;
return hash_dat;
};
auto b_eq = [](const backtrack_t &b1, const backtrack_t &b2) {
std::cout << "get eq: " << b1.code << " ? " << b2.code << std::endl;
return b1.code == b2.code;
};
std::unordered_set<backtrack_t, decltype(b_hash), decltype(b_eq)> test(0, b_hash, b_eq);
// test.emplace(backtrack_t {
// .code = 123,
// .layer_num = 0,
// .last = std::list<backtrack_t*>{},
// .next = std::list<backtrack_t*>{},
// });
//
// test.emplace(backtrack_t {
// .code = 233,
// .layer_num = 1,
// .last = std::list<backtrack_t*>{},
// .next = std::list<backtrack_t*>{},
// });
//
// test.emplace(backtrack_t {
// .code = 343,
// .layer_num = 2,
// .last = std::list<backtrack_t*>{},
// .next = std::list<backtrack_t*>{},
// });
for (uint32_t i = 0; i < 20; ++i) { for (uint32_t i = 0; i < 20; ++i) {
test.emplace(backtrack_t { test.emplace(backtrack_t {

1
src/fast_cal/fast_cal.h

@ -8,6 +8,7 @@
#include "core.h" #include "core.h"
#include "raw_code.h" #include "raw_code.h"
// TODO: using prime number
const uint32_t FC_MAP_RESERVE = 65536 * 8; const uint32_t FC_MAP_RESERVE = 65536 * 8;
/// FastCal not found -> return invalid raw code /// FastCal not found -> return invalid raw code

Loading…
Cancel
Save