Browse Source

feat: framework of Analyse module

legacy
Dnomd343 2 years ago
parent
commit
3a1d0a4d7e
  1. 3
      src/CMakeLists.txt
  2. 4
      src/analyse/CMakeLists.txt
  3. 49
      src/analyse/analyse.cc
  4. 27
      src/analyse/analyse.h
  5. 8
      src/main.cc

3
src/CMakeLists.txt

@ -11,6 +11,7 @@ include_directories(short_code)
include_directories(common_code)
include_directories(fast_cal)
include_directories(analyse)
################################
@ -23,6 +24,7 @@ add_subdirectory(short_code)
add_subdirectory(common_code)
add_subdirectory(fast_cal)
add_subdirectory(analyse)
################################
@ -39,6 +41,7 @@ target_link_libraries(klotski short_code)
target_link_libraries(klotski common_code)
target_link_libraries(klotski fast_cal)
target_link_libraries(klotski analyse)
target_link_libraries(klotski pthread)

4
src/analyse/CMakeLists.txt

@ -0,0 +1,4 @@
cmake_minimum_required(VERSION 3.0)
add_library(analyse analyse.cc)
target_link_libraries(analyse core)

49
src/analyse/analyse.cc

@ -0,0 +1,49 @@
#include "core.h"
#include "analyse.h"
#include <iostream>
Core Analyse::new_core() {
return Core(
[this](auto &&code, auto &&mask) { // lambda as function pointer
new_case(std::forward<decltype(code)>(code), std::forward<decltype(mask)>(mask));
}
);
}
void Analyse::start_analyse(uint64_t code) {
auto core = new_core();
cases.empty();
cache.empty();
cases.reserve(65536);
cache.emplace(&cases.emplace(code, analyse_t {
.code = code,
.mask = 0,
}).first->second);
while (!cache.empty()) {
core.next_step(cache.front()->code, cache.front()->mask);
cache.pop();
}
std::cout << "size: " << cases.size() << std::endl;
}
void Analyse::new_case(uint64_t code, uint64_t mask) {
auto current = cases.find(code);
if (current != cases.end()) { // find existed case
current->second.mask |= mask; // update mask info
return;
}
cache.emplace(&cases.emplace(code, analyse_t {
.code = code,
.mask = mask,
}).first->second);
}

27
src/analyse/analyse.h

@ -0,0 +1,27 @@
#pragma once
#include <queue>
#include <cstdint>
#include <unordered_map>
class Analyse {
public:
struct analyse_t {
uint64_t code;
uint64_t mask;
// int step;
// TODO: try set / unordered_set
// std::list<analyse_t*> src;
};
std::queue<analyse_t*> cache;
std::unordered_map<uint64_t, analyse_t> cases;
inline Core new_core();
void start_analyse(uint64_t code);
void new_case(uint64_t code, uint64_t mask);
};

8
src/main.cc

@ -6,6 +6,7 @@
#include "short_code.h"
#include "raw_code.h"
#include "fast_cal.h"
#include "analyse.h"
//#include "core_demo.h"
@ -196,8 +197,11 @@ int main() {
auto raw_code = CommonCode("1a9bf0c").to_raw_code().unwrap();
// std::cout << fast_cal(raw_code) << std::endl;
auto f = FastCal();
f.fast_cal(raw_code);
// auto f = FastCal();
// f.fast_cal(raw_code);
auto a = Analyse();
a.start_analyse(raw_code);
// int sum = 0;
// for (auto const &raw_code : all_cases_raw) {

Loading…
Cancel
Save