Browse Source

feat: search group cases

legacy
Dnomd343 2 years ago
parent
commit
b8bb0d2ef5
  1. 7
      src/klotski_core/ffi/tmain.cc
  2. 43
      src/klotski_core/group/group.cc
  3. 8
      src/klotski_core/group/group.h

7
src/klotski_core/ffi/tmain.cc

@ -56,11 +56,14 @@ void tmain() {
// Group::demo(RawCode::from_common_code(0x1A9BF0C00));
// Group::demo(RawCode::from_common_code(0x4FEA13400));
// std::cout << Group::demo(RawCode::from_common_code(0x1A9BF0C00)) << std::endl;
// auto ret = Group::group_cases(RawCode::from_common_code(0x1A9BF0C00));
// std::cout << ret.size() << std::endl;
// std::cout << ret[0] << std::endl;
// std::cout << ret[1] << std::endl;
auto tmp = RawCode::from_common_code(0x1A9BF0C00);
for (int i = 0; i < 100; ++i) {
Group::demo(tmp);
Group::group_cases(tmp);
}
std::cout << ((clock() - start) * 1000 / CLOCKS_PER_SEC) << "ms" << std::endl;

43
src/klotski_core/group/group.cc

@ -1,4 +1,5 @@
#include <queue>
#include <vector>
#include "core.h"
#include "group.h"
#include "common.h"
@ -46,51 +47,37 @@ Group::block_num_t Group::block_num(const CommonCode &common_code) {
return result;
}
uint32_t Group::demo(const RawCode &seed) {
std::vector<RawCode> Group::group_cases(const RawCode &seed) {
std::queue<uint64_t> cache;
// uint32_t index = 0;
// std::vector<uint64_t> temp;
// temp.reserve(65535 * 8);
absl::flat_hash_map<uint64_t, uint64_t> cases;
cases.reserve(65535 * 8);
absl::flat_hash_map<uint64_t, uint64_t> cases; // <code, mask>
cases.reserve(max_group_size(seed));
cases.emplace(seed.unwrap(), 0); // without mask
cache.emplace(seed.unwrap());
// temp.emplace_back(seed.unwrap());
auto core = Core(
[&cases, &cache](auto &&code, auto &&mask) {
[&cache, &cases](auto &&code, auto &&mask) { // callback function
auto current = cases.find(code);
if (current != cases.end()) {
current->second |= mask; // update mask info
current->second |= mask; // update mask
return;
}
cases.emplace(code, mask);
cache.emplace(code);
// temp.emplace_back(code);
}
);
while (!cache.empty()) {
// while (index != temp.size()) {
while (!cache.empty()) { // until BFS without elements
core.next_cases(cache.front(), cases.find(cache.front())->second);
// core.next_cases(temp[index], cases.find(temp[index])->second);
cache.pop();
// ++index;
cache.pop(); // case dequeue
}
return cases.size();
auto result = std::vector<RawCode>();
result.reserve(cases.size());
for (auto &&raw_code : cases) { // export group cases
result.emplace_back(RawCode::unsafe_create(raw_code.first));
}
return result;
}
} // namespace klotski

8
src/klotski_core/group/group.h

@ -15,10 +15,16 @@ public:
uint8_t n_2x1 = 0;
};
// static block_num_t block_num(uint32_t type_id);
static block_num_t block_num(const RawCode &raw_code);
static block_num_t block_num(const CommonCode &common_code);
static uint32_t demo(const RawCode &seed);
// static uint32_t max_group_size(uint32_t type_id);
static uint32_t max_group_size(const RawCode &raw_code) {
return 65535 * 8;
};
static std::vector<RawCode> group_cases(const RawCode &seed);
};

Loading…
Cancel
Save