Browse Source

update: FastCal cases hashmap pre-reserve

master
Dnomd343 2 years ago
parent
commit
4ab9ed667d
  1. 10
      src/fast_cal/cal_core.cc
  2. 18
      src/fast_cal/fast_cal.h
  3. 8
      src/main.cc

10
src/fast_cal/cal_core.cc

@ -2,13 +2,11 @@
#include "fast_cal.h"
Core FastCal::init(uint64_t code) { // initialize process
/// clear working data
/// reset working data
cases.clear();
cases.reserve(FC_MAP_RESERVE); // hashmap pre-reserve
std::queue<fast_cal_t*>{}.swap(cache);
// TODO: test the speed without hashmap reserve
cases.reserve(65536);
/// insert root node
cache.emplace(&cases.emplace(code, fast_cal_t {
.code = code,
@ -39,7 +37,7 @@ void FastCal::new_case(uint64_t code, uint64_t mask) {
}
/// found first matched target
RawCode FastCal::target(RawCode code, const check_t &match) {
RawCode FastCal::target(RawCode code, const match_t &match) {
auto core = init((uint64_t)code);
/// start bfs search
while (!cache.empty()) {
@ -53,7 +51,7 @@ RawCode FastCal::target(RawCode code, const check_t &match) {
}
/// found multi-targets matched in first same layer
std::vector<RawCode> FastCal::target_multi(RawCode code, const check_t &match) {
std::vector<RawCode> FastCal::target_multi(RawCode code, const match_t &match) {
auto core = init((uint64_t)code);
auto layer_end = cache.back();
std::vector<RawCode> matched; // matched list

18
src/fast_cal/fast_cal.h

@ -6,27 +6,25 @@
#include <unordered_map>
#include "raw_code.h"
const uint32_t FC_MAP_RESERVE = 65536;
/// FastCal not found -> return invalid raw code
const RawCode FC_NOT_FOUND = RawCode::unsafe_create(0);
class FastCal {
public:
const static auto NOT_FOUND = (uint64_t)0;
typedef std::function<bool(uint64_t)> check_t;
// const static RawCode *test = (RawCode*)nullptr;
typedef std::function<bool(uint64_t)> match_t;
/// xxx_multi only search until same layer
// TODO: shall we use RawCode instead of uint64_t?
/// BFS search functions
RawCode solve(RawCode code);
RawCode target(RawCode code, const check_t &match);
std::vector<RawCode> furthest(RawCode code);
std::vector<RawCode> solve_multi(RawCode code);
std::vector<RawCode> target_multi(RawCode code, const check_t &match);
RawCode target(RawCode code, const match_t &match);
std::vector<RawCode> target_multi(RawCode code, const match_t &match);
// TODO: search / search_multi / resolve / resolve_multi
// TODO: static furthest function

8
src/main.cc

@ -15,20 +15,20 @@
int main() {
// BasicRanges::build();
BasicRanges::build();
// std::cout << "wait 3s" << std::endl;
// sleep(3);
// std::cout << "start benchmark" << std::endl;
std::cout << "start benchmark" << std::endl;
auto start_time = clock();
// AllCases::build();
auto f = FastCal();
// auto ret = f.solve((uint64_t)RawCode::from_common_code("1a9bf0c"));
// auto ret = f.solve(RawCode::from_common_code("1a9bf0c"));
auto ret = f.solve(RawCode::from_common_code("1aaef0c"));
auto ret = f.solve(RawCode::from_common_code("1a9bf0c"));
// auto ret = f.solve(RawCode::from_common_code("1aaef0c"));
if (ret == FC_NOT_FOUND) {
std::cout << "not found" << std::endl;

Loading…
Cancel
Save