Browse Source

feat: demo of fast calculate class

legacy
Dnomd343 2 months ago
parent
commit
fb70d6cc61
  1. 117
      src/core/fast_cal/internal/demo.cc
  2. 1
      src/core/main.cc

117
src/core/fast_cal/internal/demo.cc

@ -21,38 +21,130 @@ struct data_t {
uint64_t back; uint64_t back;
}; };
// TODO: try using `std::vector` + offset instead of `std::queue` // TODO: try wrapper as custom `std::queue`
class FCDemo {
public:
explicit FCDemo(RawCode raw_code) {
auto reserve = GroupUnion::from_raw_code(raw_code).max_group_size();
codes_.reserve(reserve);
cases_.reserve(reserve);
codes_.emplace_back(raw_code);
cases_.emplace(raw_code, data_t {0, 0}); // without mask
}
RawCode DoCal() {
// bool stop_flag = false;
uint64_t result = 0;
auto core = MaskMover([this, &result](uint64_t code, uint64_t mask) {
if (const auto match = cases_.find(code); match != cases_.end()) {
match->second.mask |= mask; // update mask
return;
}
cases_.emplace(code, data_t {
.mask = mask,
.back = codes_[offset_].unwrap(),
});
codes_.emplace_back(RawCode::unsafe_create(code));
if (((code >> 39) & 0b111) == 0b100) {
// stop_flag = true;
result = code;
}
});
while (offset_ != codes_.size()) {
auto curr = codes_[offset_].unwrap();
core.next_cases(curr, cases_.find(curr)->second.mask);
++offset_;
if (result != 0) {
break;
}
}
// std::vector<RawCode> path;
// auto code = result;
// while (true) {
// if (code == 0) {
// break;
// }
// path.emplace_back(RawCode::unsafe_create(code));
// code = cases_.find(code)->second.back;
// }
// std::reverse(path.begin(), path.end());
// for (auto step : path) {
// std::cout << step << std::endl;
// }
// std::cout << path.size() << std::endl;
return codes_[offset_];
}
private:
uint64_t offset_ {0};
std::vector<RawCode> codes_;
absl::flat_hash_map<uint64_t, data_t> cases_; // <code, mask>
};
RawCode FastCal_demo(RawCode raw_code) { RawCode FastCal_demo(RawCode raw_code) {
std::queue<RawCode> cache;
FCDemo fc {raw_code};
return fc.DoCal();
// std::queue<RawCode> cache;
std::vector<RawCode> codes;
absl::flat_hash_map<uint64_t, data_t> cases; // <code, mask> absl::flat_hash_map<uint64_t, data_t> cases; // <code, mask>
auto reserve = GroupUnion::from_raw_code(raw_code).max_group_size(); auto reserve = GroupUnion::from_raw_code(raw_code).max_group_size();
codes.reserve(reserve);
cases.reserve(reserve); cases.reserve(reserve);
auto core = MaskMover([&cache, &cases](uint64_t code, uint64_t mask) { uint64_t offset = 0;
bool stop_flag = false;
auto core = MaskMover([&codes, &cases, &offset, &stop_flag](uint64_t code, uint64_t mask) {
if (const auto match = cases.find(code); match != cases.end()) { if (const auto match = cases.find(code); match != cases.end()) {
match->second.mask |= mask; // update mask match->second.mask |= mask; // update mask
return; return;
} }
cases.emplace(code, data_t { cases.emplace(code, data_t {
.mask = mask, .mask = mask,
.back = cache.front().unwrap(), .back = codes[offset].unwrap(),
// .back = cache.front().unwrap(),
}); });
cache.emplace(RawCode::unsafe_create(code)); codes.emplace_back(RawCode::unsafe_create(code));
// cache.emplace(RawCode::unsafe_create(code));
if (((code >> 39) & 0b111) == 0b100) {
stop_flag = true;
}
}); });
cache.emplace(raw_code); codes.emplace_back(raw_code);
// cache.emplace(raw_code);
cases.emplace(raw_code, data_t {0, 0}); // without mask cases.emplace(raw_code, data_t {0, 0}); // without mask
while (!cache.empty()) { while (offset != codes.size()) {
if (((cache.front().unwrap() >> 39) & 0b111) == 0b100) { auto curr = codes[offset].unwrap();
core.next_cases(curr, cases.find(curr)->second.mask);
++offset;
if (stop_flag) {
break; break;
} }
uint64_t curr = cache.front().unwrap();
core.next_cases(curr, cases.find(curr)->second.mask);
cache.pop();
} }
// while (!cache.empty()) {
// if (((cache.front().unwrap() >> 39) & 0b111) == 0b100) {
// break;
// }
// uint64_t curr = cache.front().unwrap();
// core.next_cases(curr, cases.find(curr)->second.mask);
// cache.pop();
// if (stop_flag) {
// break;
// }
// }
// std::vector<RawCode> path; // std::vector<RawCode> path;
// auto code = cache.front().unwrap(); // auto code = cache.front().unwrap();
// while (true) { // while (true) {
@ -68,6 +160,7 @@ RawCode FastCal_demo(RawCode raw_code) {
// } // }
// std::cout << path.size() << std::endl; // std::cout << path.size() << std::endl;
return cache.front(); return codes[offset];
// return cache.front();
} }

1
src/core/main.cc

@ -48,6 +48,7 @@ int main() {
// fc.solve(); // fc.solve();
// klotski::cases::Group_extend(code); // klotski::cases::Group_extend(code);
FastCal_demo(code); FastCal_demo(code);
break;
} }
// for (int i = 0; i < 10000000; ++i) { // for (int i = 0; i < 10000000; ++i) {

Loading…
Cancel
Save