From fb70d6cc6192e849c74a186b031ccd0225098704 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 26 Oct 2024 10:34:37 +0800 Subject: [PATCH] feat: demo of fast calculate class --- src/core/fast_cal/internal/demo.cc | 117 ++++++++++++++++++++++++++--- src/core/main.cc | 1 + 2 files changed, 106 insertions(+), 12 deletions(-) diff --git a/src/core/fast_cal/internal/demo.cc b/src/core/fast_cal/internal/demo.cc index 27be62d..528f97f 100644 --- a/src/core/fast_cal/internal/demo.cc +++ b/src/core/fast_cal/internal/demo.cc @@ -21,38 +21,130 @@ struct data_t { 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 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 codes_; + absl::flat_hash_map cases_; // +}; RawCode FastCal_demo(RawCode raw_code) { - std::queue cache; + + FCDemo fc {raw_code}; + return fc.DoCal(); + +// std::queue cache; + std::vector codes; absl::flat_hash_map cases; // auto reserve = GroupUnion::from_raw_code(raw_code).max_group_size(); + codes.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()) { match->second.mask |= mask; // update mask return; } cases.emplace(code, data_t { .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 - while (!cache.empty()) { - if (((cache.front().unwrap() >> 39) & 0b111) == 0b100) { + while (offset != codes.size()) { + auto curr = codes[offset].unwrap(); + core.next_cases(curr, cases.find(curr)->second.mask); + ++offset; + + if (stop_flag) { 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 path; // auto code = cache.front().unwrap(); // while (true) { @@ -68,6 +160,7 @@ RawCode FastCal_demo(RawCode raw_code) { // } // std::cout << path.size() << std::endl; - return cache.front(); + return codes[offset]; +// return cache.front(); } diff --git a/src/core/main.cc b/src/core/main.cc index 87f5dc3..e20cd45 100644 --- a/src/core/main.cc +++ b/src/core/main.cc @@ -48,6 +48,7 @@ int main() { // fc.solve(); // klotski::cases::Group_extend(code); FastCal_demo(code); + break; } // for (int i = 0; i < 10000000; ++i) {