diff --git a/klotski/common_code.cc b/klotski/common_code.cc index 75b9fbd..83374ae 100644 --- a/klotski/common_code.cc +++ b/klotski/common_code.cc @@ -1,6 +1,27 @@ +#include #include "common.h" #include "common_code.h" +inline uint8_t last_zero_num(uint32_t bin) { // get last zero number + bin ^= (bin - 1); + return __builtin_popcount(bin >> 1); +} + +std::string CommonCode::code_to_string(uint64_t common_code, bool shorten) { + if (!CommonCode::check(common_code)) { + throw std::invalid_argument("invalid common code"); + } + char result[10]; + sprintf(result, "%09lX", common_code); + if (shorten) { // remove `0` after common code + if (common_code == 0x000000000) { + return "0"; // special case + } + result[9 - last_zero_num(common_code) / 4] = '\0'; // truncate string + } + return result; +} + bool CommonCode::check(uint64_t common_code) { uint32_t head = common_code >> 32; if (head >= 16 || (head & 0b11) == 0b11) { // check 2x2 block address diff --git a/klotski/common_code.h b/klotski/common_code.h index 9a2005c..833adc8 100644 --- a/klotski/common_code.h +++ b/klotski/common_code.h @@ -1,8 +1,11 @@ #pragma once +#include #include class CommonCode { public: static bool check(uint64_t common_code); +// static uint64_t code_from_string(const std::string &common_code); + static std::string code_to_string(uint64_t common_code, bool shorten = false); }; diff --git a/klotski/main.cc b/klotski/main.cc index 6af547e..b73f139 100644 --- a/klotski/main.cc +++ b/klotski/main.cc @@ -1,35 +1,39 @@ #include #include "all_cases.h" #include "short_code.h" -//#include "common_code.h" +#include "common_code.h" int main() { // auto a = AllCases(); // auto a = AllCases(AllCases::Build::BASIC_RANGES); // auto a = AllCases(AllCases::Build::ALL_CASES); -// + // std::cout << "start getting basic ranges" << std::endl; // std::cout << "basic range: " << a.get_basic_ranges()->size() << std::endl; -// + // std::cout << "start getting all cases" << std::endl; // for (const auto &temp : *a.get_all_cases()) { // std::cout << " " << temp.size() << std::endl; // } - auto s = ShortCode(); +// auto s = ShortCode(); // auto s = ShortCode(ShortCode::Mode::NORMAL); // auto s = ShortCode(ShortCode::Mode::FAST); // s.speed_up(ShortCode::Mode::NORMAL); // s.speed_up(ShortCode::Mode::FAST); - printf("%d\n", s.zip_short_code(0x6EC0F8800)); - printf("%09lX\n", s.unzip_short_code(14323231)); +// printf("%d\n", s.zip_short_code(0x6EC0F8800)); +// printf("%09lX\n", s.unzip_short_code(14323231)); + +// std::cout << ShortCode::code_to_string(14323231) << std::endl; +// std::cout << ShortCode::code_from_string("EP4HZ") << std::endl; + - std::cout << ShortCode::code_to_string(14323231) << std::endl; - std::cout << ShortCode::code_from_string("EP4HZ") << std::endl; + std::cout << CommonCode::code_to_string(0x4FEA13400, true) << std::endl; + std::cout << CommonCode::code_to_string(0x4FEA13400) << std::endl; return 0; }