From 0c90647f6921e9c4519ee3b6e774a12767bd3d9f Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 8 Jan 2023 13:02:07 +0800 Subject: [PATCH] update: common code support --- klotski/common_code.h | 11 --- src/CMakeLists.txt | 10 ++- src/common_code/CMakeLists.txt | 4 ++ {klotski => src/common_code}/common_code.cc | 6 +- src/common_code/common_code.h | 11 +++ src/main.cc | 80 ++++++++++++--------- 6 files changed, 70 insertions(+), 52 deletions(-) delete mode 100644 klotski/common_code.h create mode 100644 src/common_code/CMakeLists.txt rename {klotski => src/common_code}/common_code.cc (91%) create mode 100644 src/common_code/common_code.h diff --git a/klotski/common_code.h b/klotski/common_code.h deleted file mode 100644 index 430dcea..0000000 --- a/klotski/common_code.h +++ /dev/null @@ -1,11 +0,0 @@ -#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/src/CMakeLists.txt b/src/CMakeLists.txt index afb42c1..5cc8d8c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,10 +1,14 @@ cmake_minimum_required(VERSION 3.0) -include_directories(${PROJECT_SOURCE_DIR}/src/common) -include_directories(${PROJECT_SOURCE_DIR}/src/all_cases) +include_directories(common) +include_directories(all_cases) +include_directories(common_code) + add_subdirectory(common) add_subdirectory(all_cases) +add_subdirectory(common_code) add_executable(klotski main.cc) -target_link_libraries(klotski common all_cases) +target_link_libraries(klotski all_cases) +target_link_libraries(klotski common_code) diff --git a/src/common_code/CMakeLists.txt b/src/common_code/CMakeLists.txt new file mode 100644 index 0000000..5b861e0 --- /dev/null +++ b/src/common_code/CMakeLists.txt @@ -0,0 +1,4 @@ +cmake_minimum_required(VERSION 3.0) + +add_library(common_code common_code.cc) +target_link_libraries(common_code common) diff --git a/klotski/common_code.cc b/src/common_code/common_code.cc similarity index 91% rename from klotski/common_code.cc rename to src/common_code/common_code.cc index b6593b7..9408f27 100644 --- a/klotski/common_code.cc +++ b/src/common_code/common_code.cc @@ -8,7 +8,7 @@ inline uint8_t last_zero_num(uint32_t bin) { // get last zero number return __builtin_popcount(bin >> 1); } -std::string CommonCode::code_to_string(uint64_t common_code, bool shorten) { +std::string CommonCode::to_string(uint64_t common_code, bool shorten) { if (!CommonCode::check(common_code)) { throw std::invalid_argument("invalid common code"); } @@ -23,7 +23,7 @@ std::string CommonCode::code_to_string(uint64_t common_code, bool shorten) { return result; } -uint64_t CommonCode::code_from_string(const std::string &common_code) { +uint64_t CommonCode::from_string(const std::string &common_code) { if (common_code.length() > 9 || common_code.length() == 0) { throw std::invalid_argument("common code format error"); } @@ -44,7 +44,7 @@ uint64_t CommonCode::code_from_string(const std::string &common_code) { return result << (9 - common_code.length()) * 4; // low-bits fill with zero } -bool CommonCode::check(uint64_t common_code) { +bool CommonCode::check(uint64_t common_code) { // check whether common code is valid uint32_t head = common_code >> 32; if (head >= 16 || (head & 0b11) == 0b11) { // check 2x2 block address return false; // invalid common code diff --git a/src/common_code/common_code.h b/src/common_code/common_code.h new file mode 100644 index 0000000..614886a --- /dev/null +++ b/src/common_code/common_code.h @@ -0,0 +1,11 @@ +#pragma once + +#include +#include + +class CommonCode { +public: + static bool check(uint64_t common_code); + static uint64_t from_string(const std::string &common_code); + static std::string to_string(uint64_t common_code, bool shorten = false); +}; diff --git a/src/main.cc b/src/main.cc index 0cf39c6..b6c7339 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,47 +1,57 @@ #include #include "all_cases.h" #include "basic_ranges.h" +#include "common_code.h" -void get_status() { - switch (BasicRanges::basic_ranges_status()) { - case BasicRanges::NO_INIT: - std::cout << "basic ranges no init" << std::endl; - break; - case BasicRanges::BUILDING: - std::cout << "basic ranges building" << std::endl; - break; - case BasicRanges::AVAILABLE: - std::cout << "basic ranges available" << std::endl; - break; - } - - switch (AllCases::all_cases_status()) { - case AllCases::NO_INIT: - std::cout << "all cases no init" << std::endl; - break; - case AllCases::BUILDING: - std::cout << "all cases building" << std::endl; - break; - case AllCases::AVAILABLE: - std::cout << "all cases available" << std::endl; - break; - } -} +//void get_status() { +// switch (BasicRanges::basic_ranges_status()) { +// case BasicRanges::NO_INIT: +// std::cout << "basic ranges no init" << std::endl; +// break; +// case BasicRanges::BUILDING: +// std::cout << "basic ranges building" << std::endl; +// break; +// case BasicRanges::AVAILABLE: +// std::cout << "basic ranges available" << std::endl; +// break; +// } +// +// switch (AllCases::all_cases_status()) { +// case AllCases::NO_INIT: +// std::cout << "all cases no init" << std::endl; +// break; +// case AllCases::BUILDING: +// std::cout << "all cases building" << std::endl; +// break; +// case AllCases::AVAILABLE: +// std::cout << "all cases available" << std::endl; +// break; +// } +//} int main() { - get_status(); - BasicRanges::build_basic_ranges(); - get_status(); - AllCases::build_all_cases(); - get_status(); +// get_status(); +// BasicRanges::build_basic_ranges(); +// get_status(); +// AllCases::build_all_cases(); +// get_status(); +// +// for (auto const &all_case : *AllCases::get_all_cases()) { +// std::cout << " " << all_case.size() << std::endl; +// } +// +// std::cout << BasicRanges::get_basic_ranges() << std::endl; +// std::cout << AllCases::get_basic_ranges() << std::endl; + - for (auto const &all_case : *AllCases::get_all_cases()) { - std::cout << " " << all_case.size() << std::endl; - } + std::cout << CommonCode::check(0x123456789) << std::endl; + std::cout << CommonCode::check(0x4FEA13400) << std::endl; - std::cout << BasicRanges::get_basic_ranges() << std::endl; - std::cout << AllCases::get_basic_ranges() << std::endl; + // TODO: should we return a CommonCode object like String::new(...) in rust? + printf("%09lX\n", CommonCode::from_string("1A9bF0c0")); + std::cout << CommonCode::to_string(0x1A9BF0C00) << std::endl; + std::cout << CommonCode::to_string(0x1A9BF0C00, true) << std::endl; return 0; }