Browse Source

perf: cal group size

legacy
Dnomd343 2 years ago
parent
commit
6d5595b8b3
  1. 35
      src/klotski_core/group/build_cases.cc

35
src/klotski_core/group/build_cases.cc

@ -13,11 +13,7 @@ using Common::check_range;
using Common::range_reverse;
uint32_t Group::group_size(const GroupId &group_id) {
return group_cases(group_seed(group_id)).size();
}
uint32_t Group::group_size(const RawCode &raw_code) {
return group_cases(raw_code).size();
return group_size(group_seed(group_id));
}
uint32_t Group::group_size(const CommonCode &common_code) {
@ -67,12 +63,35 @@ std::vector<CommonCode> Group::all_cases(const TypeId &type_id) {
return all_cases;
}
uint32_t Group::group_size(const RawCode &raw_code) {
std::queue<uint64_t> cache({raw_code.unwrap()});
absl::flat_hash_map<uint64_t, uint64_t> cases; // <code, mask>
cases.reserve(Group::group_max_size(raw_code));
cases.emplace(raw_code.unwrap(), 0b0); // without mask
auto core = Core(
[&cache, &cases](auto &&code, auto &&mask) { // callback function
auto current = cases.find(code);
if (current != cases.end()) {
current->second |= mask; // update mask
return;
}
cases.emplace(code, mask);
cache.emplace(code);
}
);
while (!cache.empty()) { // until BFS without elements
core.next_cases(cache.front(), cases.find(cache.front())->second);
cache.pop(); // case dequeue
}
return cases.size();
}
std::vector<RawCode> Group::group_cases(const RawCode &raw_code) {
std::queue<uint64_t> cache;
std::queue<uint64_t> cache({raw_code.unwrap()});
absl::flat_hash_map<uint64_t, uint64_t> cases; // <code, mask>
cases.reserve(Group::group_max_size(raw_code));
cases.emplace(raw_code.unwrap(), 0); // without mask
cache.emplace(raw_code.unwrap());
cases.emplace(raw_code.unwrap(), 0b0); // without mask
auto core = Core(
[&cache, &cases](auto &&code, auto &&mask) { // callback function

Loading…
Cancel
Save