From 0c49bd05ef677ef6fac11eb199f2c7c838ea9a26 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 24 Feb 2023 17:02:01 +0800 Subject: [PATCH] update: remove unneeded functions --- src/klotski_core/raw_code/mirror.cc | 51 ++++++++++------------- src/klotski_core/raw_code/raw_code.h | 17 +++----- src/klotski_core/short_code/convert.cc | 34 +++++++++++---- src/klotski_core/short_code/serialize.cc | 26 +++++++++--- src/klotski_core/short_code/short_code.cc | 12 ++++-- src/klotski_core/short_code/short_code.h | 14 +++---- 6 files changed, 86 insertions(+), 68 deletions(-) diff --git a/src/klotski_core/raw_code/mirror.cc b/src/klotski_core/raw_code/mirror.cc index d419383..76173bd 100644 --- a/src/klotski_core/raw_code/mirror.cc +++ b/src/klotski_core/raw_code/mirror.cc @@ -2,37 +2,17 @@ using klotski::RawCode; -/// Static mirror functions -bool RawCode::is_vertical_mirror(RawCode &&raw_code) { - return raw_code.is_vertical_mirror(); -} - -bool RawCode::is_horizontal_mirror(RawCode &&raw_code) { - return raw_code.is_horizontal_mirror(); -} - -bool RawCode::is_vertical_mirror(const RawCode &raw_code) { - return raw_code.is_vertical_mirror(); -} +/// Mirror convert functions +RawCode RawCode::to_vertical_mirror() const { -bool RawCode::is_horizontal_mirror(const RawCode &raw_code) { - return raw_code.is_horizontal_mirror(); -} + // TODO: vertical mirror convert -RawCode RawCode::to_vertical_mirror(RawCode &&raw_code) { - return raw_code.to_vertical_mirror(); } -RawCode RawCode::to_horizontal_mirror(RawCode &&raw_code) { - return raw_code.to_horizontal_mirror(); -} +RawCode RawCode::to_horizontal_mirror() const { -RawCode RawCode::to_vertical_mirror(const RawCode &raw_code) { - return raw_code.to_vertical_mirror(); -} + // TODO: horizontal mirror convert -RawCode RawCode::to_horizontal_mirror(const RawCode &raw_code) { - return raw_code.to_horizontal_mirror(); } /// Mirror check functions @@ -48,15 +28,26 @@ bool RawCode::is_horizontal_mirror() const { } -/// Mirror convert functions -RawCode RawCode::to_vertical_mirror() const { +bool RawCode::is_vertical_mirror(RawCode &&raw_code) const { - // TODO: vertical mirror convert + // TODO: vertical mirror check } -RawCode RawCode::to_horizontal_mirror() const { +bool RawCode::is_vertical_mirror(const RawCode &raw_code) const { - // TODO: horizontal mirror convert + // TODO: vertical mirror check + +} + +bool RawCode::is_horizontal_mirror(RawCode &&raw_code) const { + + // TODO: horizontal mirror check + +} + +bool RawCode::is_horizontal_mirror(const RawCode &raw_code) const { + + // TODO: horizontal mirror check } diff --git a/src/klotski_core/raw_code/raw_code.h b/src/klotski_core/raw_code/raw_code.h index 1f4e797..230eff1 100644 --- a/src/klotski_core/raw_code/raw_code.h +++ b/src/klotski_core/raw_code/raw_code.h @@ -91,20 +91,15 @@ namespace klotski { static RawCode from_common_code(const std::string &common_code); /// Mirror functions - bool is_vertical_mirror() const; - bool is_horizontal_mirror() const; - RawCode to_vertical_mirror() const; RawCode to_horizontal_mirror() const; - static bool is_vertical_mirror(RawCode &&raw_code); - static bool is_horizontal_mirror(RawCode &&raw_code); - static bool is_vertical_mirror(const RawCode &raw_code); - static bool is_horizontal_mirror(const RawCode &raw_code); + bool is_vertical_mirror() const; // whether vertically symmetrical + bool is_horizontal_mirror() const; // whether horizontally symmetrical - static RawCode to_vertical_mirror(RawCode &&raw_code); - static RawCode to_horizontal_mirror(RawCode &&raw_code); - static RawCode to_vertical_mirror(const RawCode &raw_code); - static RawCode to_horizontal_mirror(const RawCode &raw_code); + bool is_vertical_mirror(RawCode &&raw_code) const; // whether vertically symmetric to another + bool is_vertical_mirror(const RawCode &raw_code) const; + bool is_horizontal_mirror(RawCode &&raw_code) const; // whether horizontally symmetric to another + bool is_horizontal_mirror(const RawCode &raw_code) const; }; } diff --git a/src/klotski_core/short_code/convert.cc b/src/klotski_core/short_code/convert.cc index 992c74b..598e28a 100644 --- a/src/klotski_core/short_code/convert.cc +++ b/src/klotski_core/short_code/convert.cc @@ -10,7 +10,7 @@ using klotski::ShortCode; using klotski::CommonCode; /// ShortCode to CommonCode -CommonCode ShortCode::to_common_code() const { // convert to common code +CommonCode ShortCode::to_common_code() const { if (ShortCode::mode() == ShortCode::NORMAL) { return CommonCode::unsafe_create(tiny_decode(code)); // normal mode } @@ -18,10 +18,34 @@ CommonCode ShortCode::to_common_code() const { // convert to common code } /// CommonCode to ShortCode +ShortCode::ShortCode(CommonCode &&common_code) { + if (ShortCode::mode() == ShortCode::NORMAL) { + code = tiny_encode(common_code.unwrap()); // normal mode + } else { + code = fast_encode(common_code.unwrap()); // fast mode + } +} + +ShortCode::ShortCode(const CommonCode &common_code) { + if (ShortCode::mode() == ShortCode::NORMAL) { + code = tiny_encode(common_code.unwrap()); // normal mode + } else { + code = fast_encode(common_code.unwrap()); // fast mode + } +} + ShortCode ShortCode::from_common_code(uint64_t common_code) { return ShortCode(CommonCode(common_code)); } +ShortCode ShortCode::from_common_code(CommonCode &&common_code) { + return ShortCode(std::forward(common_code)); +} + +ShortCode ShortCode::from_common_code(std::string &&common_code) { + return ShortCode(std::forward(common_code)); +} + ShortCode ShortCode::from_common_code(const CommonCode &common_code) { return ShortCode(common_code); } @@ -30,14 +54,6 @@ ShortCode ShortCode::from_common_code(const std::string &common_code) { return ShortCode(CommonCode(common_code)); } -ShortCode::ShortCode(const CommonCode &common_code) { // convert from common code - if (ShortCode::mode() == ShortCode::NORMAL) { - code = tiny_encode(common_code.unwrap()); // normal mode - } else { - code = fast_encode(common_code.unwrap()); // fast mode - } -} - /// NOTE: ensure that input common code is valid! uint32_t ShortCode::fast_encode(uint64_t common_code) { // common code --> short code auto head = common_code >> 32; // head index diff --git a/src/klotski_core/short_code/serialize.cc b/src/klotski_core/short_code/serialize.cc index 26e107d..428ff99 100644 --- a/src/klotski_core/short_code/serialize.cc +++ b/src/klotski_core/short_code/serialize.cc @@ -4,10 +4,7 @@ using klotski::ShortCode; using klotski::ShortCodeException; -ShortCode ShortCode::from_string(const std::string &short_code) { - return ShortCode(short_code); // convert from string -} - +/// ShortCode to String std::string ShortCode::to_string() const { // encode as 5-bits string uint32_t short_code = code; char result[6]; // short code length 5 @@ -19,7 +16,24 @@ std::string ShortCode::to_string() const { // encode as 5-bits string return result; } -ShortCode::ShortCode(const std::string &short_code) { // 5-bits string decode +/// String to ShortCode +ShortCode::ShortCode(std::string &&short_code) { + code = string_decode(short_code); +} + +ShortCode::ShortCode(const std::string &short_code) { + code = string_decode(short_code); +} + +ShortCode ShortCode::from_string(std::string &&short_code) { + return ShortCode(short_code); +} + +ShortCode ShortCode::from_string(const std::string &short_code) { + return ShortCode(short_code); +} + +uint32_t ShortCode::string_decode(const std::string &short_code) { // 5-bits string decode if (short_code.length() != 5) { // check string length throw ShortCodeException("short code should length 5"); } @@ -40,5 +54,5 @@ ShortCode::ShortCode(const std::string &short_code) { // 5-bits string decode if (!ShortCode::check(result)) { // check converted short code throw ShortCodeException("short code invalid"); } - code = result; // apply convert result + return result; // apply convert result } diff --git a/src/klotski_core/short_code/short_code.cc b/src/klotski_core/short_code/short_code.cc index 029cfbb..fcb67d0 100644 --- a/src/klotski_core/short_code/short_code.cc +++ b/src/klotski_core/short_code/short_code.cc @@ -18,6 +18,10 @@ namespace std { } namespace klotski { + bool ShortCode::operator==(uint32_t short_code) const { + return this->code == short_code; + } + bool ShortCode::operator==(const ShortCode &short_code) const { return this->code == short_code.code; } @@ -51,15 +55,15 @@ namespace klotski { } } -bool klotski::ShortCode::check(uint32_t short_code) { - return short_code < SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1) -} - using klotski::ShortCode; bool ShortCode::fast_mode_available = false; bool ShortCode::normal_mode_available = false; +bool ShortCode::check(uint32_t short_code) { + return short_code < klotski::SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1) +} + ShortCode::Mode ShortCode::mode() { // ensure speed up enabled and return current mode if (fast_mode_available) { return ShortCode::FAST; // fast mode already enabled diff --git a/src/klotski_core/short_code/short_code.h b/src/klotski_core/short_code/short_code.h index de84df0..9ba17ea 100644 --- a/src/klotski_core/short_code/short_code.h +++ b/src/klotski_core/short_code/short_code.h @@ -53,11 +53,14 @@ #include #include #include +#include "all_cases.h" #include "common_code.h" namespace klotski { class CommonCode; // import for convert interface + const uint32_t SHORT_CODE_LIMIT = klotski::ALL_CASES_SIZE_SUM; + class ShortCodeException : public std::runtime_error { public: ShortCodeException() : std::runtime_error("invalid short code") {} @@ -73,15 +76,16 @@ namespace klotski { uint32_t code; ShortCode() = default; // unsafe initialize - static Mode mode(); + static inline Mode mode(); static bool fast_mode_available; static bool normal_mode_available; + static uint64_t fast_decode(uint32_t short_code); // short code -> common code static uint32_t fast_encode(uint64_t common_code); // common code -> short code static uint64_t tiny_decode(uint32_t short_code); // short code -> common code static uint32_t tiny_encode(uint64_t common_code); // common code -> short code - static const uint32_t SHORT_CODE_LIMIT = 29334498; + static uint32_t string_decode(const std::string &short_code); // string -> short code public: /// ShortCode validity check @@ -109,12 +113,6 @@ namespace klotski { explicit ShortCode(const std::string &short_code); explicit ShortCode(const CommonCode &common_code); - ShortCode(uint32_t short_code, Mode mode) : ShortCode(short_code) { speed_up(mode); } - ShortCode(const std::string &short_code, Mode mode) : ShortCode(short_code) { speed_up(mode); } - ShortCode(const CommonCode &common_code, Mode mode) : ShortCode(common_code) { speed_up(mode); } - ShortCode(std::string &&short_code, Mode mode) : ShortCode(std::forward(short_code)) { speed_up(mode); } - ShortCode(CommonCode &&common_code, Mode mode) : ShortCode(std::forward(common_code)) { speed_up(mode); } - /// Static initialization static ShortCode create(uint32_t short_code); static ShortCode unsafe_create(uint32_t short_code);