Browse Source

update: mark RawCode param as const ref

legacy
Dnomd343 2 years ago
parent
commit
85077dc7f7
  1. 6
      src/fast_cal/cal_core.cc
  2. 31
      src/fast_cal/fast_cal.cc
  3. 24
      src/fast_cal/fast_cal.h
  4. 17
      src/main.cc

6
src/fast_cal/cal_core.cc

@ -37,7 +37,7 @@ void FastCal::new_case(uint64_t code, uint64_t mask) {
}
/// found first matched target
RawCode FastCal::target(RawCode code, const match_t &match) {
RawCode FastCal::target(const RawCode &code, const match_t &match) {
auto core = init((uint64_t)code);
/// start bfs search
while (!cache.empty()) {
@ -51,7 +51,7 @@ RawCode FastCal::target(RawCode code, const match_t &match) {
}
/// found multi-targets matched in first same layer
std::vector<RawCode> FastCal::target_multi(RawCode code, const match_t &match) {
std::vector<RawCode> FastCal::target_multi(const RawCode &code, const match_t &match) {
auto core = init((uint64_t)code);
auto layer_end = cache.back();
std::vector<RawCode> matched; // matched list
@ -73,7 +73,7 @@ std::vector<RawCode> FastCal::target_multi(RawCode code, const match_t &match) {
}
/// found all of the furthest cases
std::vector<RawCode> FastCal::furthest(RawCode code) {
std::vector<RawCode> FastCal::furthest(const RawCode &code) {
auto core = init((uint64_t)code);
auto layer_end = cache.back();
std::vector<RawCode> layer_cases;

31
src/fast_cal/fast_cal.cc

@ -4,21 +4,29 @@
#include "fast_cal.h"
#include "raw_code.h"
/// klotski resolved -> 2x2 block at address 13 (aka 0xD)
auto resolved = [](uint64_t code) {
return ((code >> (3 * 0xD)) & 0b111) == B_2x2; // check 2x2 block address
};
/// klotski resolved -> 2x2 block at address 13 (aka 0xD)
RawCode FastCal::solve(RawCode code) {
RawCode FastCal::solve(const RawCode &code) {
return FastCal::target(code, resolved);
}
std::vector<RawCode> FastCal::solve_multi(RawCode code) {
std::vector<RawCode> FastCal::solve_multi(const RawCode &code) {
return FastCal::target_multi(code, resolved);
}
std::vector<RawCode> FastCal::resolve(const RawCode &start) {
return FastCal::search(start, resolved);
}
std::vector<std::vector<RawCode>> FastCal::resolve_multi(const RawCode &start) {
return FastCal::search_multi(start, resolved);
}
/// backtrack of FastCal tree
int FastCal::step_num(RawCode code) {
int FastCal::step_num(const RawCode &code) {
auto tmp = cases.find((uint64_t)code);
if (tmp == cases.end()) {
return -1; // code not exist
@ -46,15 +54,8 @@ std::vector<RawCode> FastCal::backtrack(const RawCode &code) {
return path;
}
std::vector<RawCode> FastCal::resolve(RawCode start) {
return FastCal::search(start, resolved);
}
std::vector<std::vector<RawCode>> FastCal::resolve_multi(RawCode start) {
return FastCal::search_multi(start, resolved);
}
std::vector<std::vector<RawCode>> FastCal::to_furthest(RawCode start) {
/// static BFS search functions
std::vector<std::vector<RawCode>> FastCal::to_furthest(const RawCode &start) {
auto fc = FastCal();
std::vector<std::vector<RawCode>> result;
for (const auto &furthest : fc.furthest(start)) {
@ -63,7 +64,7 @@ std::vector<std::vector<RawCode>> FastCal::to_furthest(RawCode start) {
return result;
}
std::vector<RawCode> FastCal::search(RawCode start, const match_t &match) {
std::vector<RawCode> FastCal::search(const RawCode &start, const match_t &match) {
auto fc = FastCal();
auto result = fc.target(start, match);
if (result == FC_NOT_FOUND) {
@ -72,7 +73,7 @@ std::vector<RawCode> FastCal::search(RawCode start, const match_t &match) {
return fc.backtrack(result); // backtrack target path
}
std::vector<std::vector<RawCode>> FastCal::search_multi(RawCode start, const match_t &match) {
std::vector<std::vector<RawCode>> FastCal::search_multi(const RawCode &start, const match_t &match) {
auto fc = FastCal();
std::vector<std::vector<RawCode>> result;
for (const auto &target : fc.target_multi(start, match)) {

24
src/fast_cal/fast_cal.h

@ -16,25 +16,23 @@ public:
typedef std::function<bool(uint64_t)> match_t;
/// backtrack functions
int step_num(RawCode code);
int step_num(const RawCode &code);
std::vector<RawCode> backtrack(const RawCode &code);
/// BFS search functions
// TODO: build function with void return -> build total tree
RawCode solve(RawCode code);
std::vector<RawCode> furthest(RawCode code);
std::vector<RawCode> solve_multi(RawCode code);
RawCode target(RawCode code, const match_t &match);
std::vector<RawCode> target_multi(RawCode code, const match_t &match);
RawCode solve(const RawCode &code);
std::vector<RawCode> furthest(const RawCode &code);
std::vector<RawCode> solve_multi(const RawCode &code);
RawCode target(const RawCode &code, const match_t &match);
std::vector<RawCode> target_multi(const RawCode &code, const match_t &match);
/// static BFS search functions
static std::vector<RawCode> resolve(RawCode start);
static std::vector<std::vector<RawCode>> to_furthest(RawCode start);
static std::vector<std::vector<RawCode>> resolve_multi(RawCode start);
static std::vector<RawCode> search(RawCode start, const match_t &match);
static std::vector<std::vector<RawCode>> search_multi(RawCode start, const match_t &match);
// TODO: (RawCode code) -> (const RawCode &code)
static std::vector<RawCode> resolve(const RawCode &start);
static std::vector<std::vector<RawCode>> to_furthest(const RawCode &start);
static std::vector<std::vector<RawCode>> resolve_multi(const RawCode &start);
static std::vector<RawCode> search(const RawCode &start, const match_t &match);
static std::vector<std::vector<RawCode>> search_multi(const RawCode &start, const match_t &match);
private:
struct fast_cal_t {

17
src/main.cc

@ -26,7 +26,6 @@ int main() {
// 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.target(RawCode::from_common_code("1a9bf0c"), [](uint64_t code) {
@ -45,17 +44,17 @@ int main() {
// std::cout << "step number: " << f.step_num(ret) << std::endl;
// }
// for (const auto &c : FastCal::resolve(RawCode::from_common_code("1a9bf0c"))) {
for (const auto &c : FastCal::resolve(RawCode::from_common_code("1a9bf0c"))) {
// std::cout << c << std::endl;
// }
for (const auto &s : FastCal::to_furthest(RawCode::from_common_code("1a9bf0c"))) {
for (const auto &c : s) {
std::cout << c << std::endl;
}
std::cout << "--------------------------------------------" << std::endl;
}
// for (const auto &s : FastCal::to_furthest(RawCode::from_common_code("1a9bf0c"))) {
// for (const auto &c : s) {
// std::cout << c << std::endl;
// }
// std::cout << "--------------------------------------------" << std::endl;
// }
// std::cout << f.step_num(ret) << std::endl;
// auto f = FastCal(RawCode::from_common_code("1a9bf0c").unwrap());

Loading…
Cancel
Save