From edd51864544414505f19b58091e45f14a592c5fb Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 29 Apr 2023 02:47:27 +0800 Subject: [PATCH] feat: codec batch convert in vector --- src/klotski_core/common_code/common_code.cc | 4 ---- src/klotski_core/common_code/common_code.h | 26 +++++++++++++++------ src/klotski_core/common_code/convert.cc | 14 +++++++++++ src/klotski_core/raw_code/convert.cc | 9 ++++++- src/klotski_core/raw_code/mirror.cc | 4 ++-- src/klotski_core/raw_code/raw_code.cc | 6 +---- src/klotski_core/raw_code/raw_code.h | 20 ++++++++++++---- src/klotski_core/short_code/convert.cc | 7 ++++++ src/klotski_core/short_code/short_code.cc | 4 ---- src/klotski_core/short_code/short_code.h | 22 +++++++++++++---- 10 files changed, 83 insertions(+), 33 deletions(-) diff --git a/src/klotski_core/common_code/common_code.cc b/src/klotski_core/common_code/common_code.cc index 4736822..05a1cbc 100644 --- a/src/klotski_core/common_code/common_code.cc +++ b/src/klotski_core/common_code/common_code.cc @@ -5,10 +5,6 @@ namespace klotski { using Common::range_reverse; -bool CommonCode::valid() const noexcept { - return CommonCode::check(code_); -} - CommonCode CommonCode::create(uint64_t common_code) { return CommonCode(common_code); // create from uint64_t } diff --git a/src/klotski_core/common_code/common_code.h b/src/klotski_core/common_code/common_code.h index 09da66f..aa2f0f4 100644 --- a/src/klotski_core/common_code/common_code.h +++ b/src/klotski_core/common_code/common_code.h @@ -17,8 +17,9 @@ /// namely `space`, `1x2`, `2x1`, `1x1`. Each of them is represented by 2-bit, /// which are `00` `01` `10` `11`. Arrange them according to their position and /// size, and we can get a binary sequence. -/// 2x2 -> # # | 2x1 -> # | 1x2 -> # # | 1x1 -> # -/// # # | # | | +/// +/// ( 2x2 -> # # ) | ( 2x1 -> # ) | ( 1x2 -> # # ) | ( 1x1 -> # ) +/// ( # # ) | ( # ) | | /// This sequence can have up to 16 blocks, aka 32-bit in length. Therefore, in /// order to be compatible with all cases, the length of this part of the code @@ -52,6 +53,7 @@ /// CommonCode = 0x4FEA13400 -> "4FEA134" #include +#include #include #include #include @@ -62,6 +64,11 @@ namespace klotski { class RawCode; class ShortCode; +class CommonCode; + +typedef std::vector RawCodes; +typedef std::vector ShortCodes; +typedef std::vector CommonCodes; class CommonCodeExp : public std::runtime_error { public: @@ -79,8 +86,8 @@ class CommonCode { public: /// Validity check - bool valid() const noexcept; static bool check(uint64_t common_code) noexcept; + bool valid() const noexcept { return check(code_); } /// Operators of CommonCode constexpr explicit operator uint64_t() const noexcept { return code_; } @@ -101,7 +108,7 @@ public: explicit CommonCode(const ShortCode &short_code) noexcept; explicit CommonCode(const std::string &common_code); - /// Static initialization + /// CommonCode initializations static CommonCode create(uint64_t common_code); static CommonCode unsafe_create(uint64_t common_code) noexcept; @@ -117,17 +124,22 @@ public: static CommonCode from_short_code(std::string &&short_code); static CommonCode from_short_code(const ShortCode &short_code) noexcept; static CommonCode from_short_code(const std::string &short_code); + + /// Batch conversions + static CommonCodes convert(const RawCodes &raw_codes) noexcept; + static CommonCodes convert(const ShortCodes &short_codes) noexcept; }; +/// Compare implements inline bool operator==(uint64_t c1, const CommonCode &c2) noexcept { return c1 == c2.unwrap(); } -inline bool operator!=(uint64_t c1, const CommonCode &c2) noexcept { return c1 != c2.unwrap(); } inline bool operator==(const CommonCode &c1, uint64_t c2) noexcept { return c1.unwrap() == c2; } -inline bool operator!=(const CommonCode &c1, uint64_t c2) noexcept { return c1.unwrap() != c2; } +inline bool operator!=(uint64_t c1, const CommonCode &c2) noexcept { return !(c1 == c2); } +inline bool operator!=(const CommonCode &c1, uint64_t c2) noexcept { return !(c1 == c2); } inline bool operator<(const CommonCode &c1, const CommonCode &c2) noexcept { return c1.unwrap() < c2.unwrap(); } inline bool operator>(const CommonCode &c1, const CommonCode &c2) noexcept { return c1.unwrap() > c2.unwrap(); } inline bool operator==(const CommonCode &c1, const CommonCode &c2) noexcept { return c1.unwrap() == c2.unwrap(); } -inline bool operator!=(const CommonCode &c1, const CommonCode &c2) noexcept { return c1.unwrap() != c2.unwrap(); } +inline bool operator!=(const CommonCode &c1, const CommonCode &c2) noexcept { return !(c1 == c2); } } // namespace klotski diff --git a/src/klotski_core/common_code/convert.cc b/src/klotski_core/common_code/convert.cc index 340c521..c323643 100644 --- a/src/klotski_core/common_code/convert.cc +++ b/src/klotski_core/common_code/convert.cc @@ -4,6 +4,10 @@ using klotski::RawCode; using klotski::ShortCode; using klotski::CommonCode; +using klotski::RawCodes; +using klotski::ShortCode; +using klotski::CommonCodes; + /// -------------------------- CommonCode to RawCode -------------------------- RawCode CommonCode::to_raw_code() const noexcept { @@ -67,3 +71,13 @@ CommonCode CommonCode::from_short_code(const ShortCode &short_code) noexcept { CommonCode CommonCode::from_short_code(const std::string &short_code) { return ShortCode(short_code).to_common_code(); } + +/// ---------------------------- Batch conversions ---------------------------- + +CommonCodes CommonCode::convert(const RawCodes &raw_codes) noexcept { + return {raw_codes.begin(), raw_codes.end()}; +} + +CommonCodes CommonCode::convert(const ShortCodes &short_codes) noexcept { + return {short_codes.begin(), short_codes.end()}; +} diff --git a/src/klotski_core/raw_code/convert.cc b/src/klotski_core/raw_code/convert.cc index 9bdc2b2..1492961 100644 --- a/src/klotski_core/raw_code/convert.cc +++ b/src/klotski_core/raw_code/convert.cc @@ -2,8 +2,9 @@ #include "raw_code.h" using klotski::RawCode; -using klotski::CommonCode; using klotski::RawCodeExp; +using klotski::CommonCode; +using klotski::CommonCodes; /// -------------------------- RawCode to CommonCode -------------------------- @@ -43,6 +44,12 @@ RawCode RawCode::from_common_code(const std::string &common_code) { return RawCode(CommonCode(common_code)); } +/// ---------------------------- Batch conversion ----------------------------- + +CommonCodes RawCode::convert(const RawCodes &raw_codes) noexcept { + return {raw_codes.begin(), raw_codes.end()}; +} + /// ----------------------------- Basic Functions ----------------------------- /// NOTE: ensure that input raw code is valid! diff --git a/src/klotski_core/raw_code/mirror.cc b/src/klotski_core/raw_code/mirror.cc index 77a92bc..455d93e 100644 --- a/src/klotski_core/raw_code/mirror.cc +++ b/src/klotski_core/raw_code/mirror.cc @@ -41,7 +41,7 @@ bool RawCode::is_horizontal_mirror(const RawCode &raw_code) const noexcept { /// ----------------------------- Basic Functions ----------------------------- -/// MASK_MIRROR_H1 | MASK_MIRROR_H2 +/// MASK_MIRROR_H1 | MASK_MIRROR_H2 /// 111 000 000 000 | 000 111 000 000 /// 111 000 000 000 | 000 111 000 000 /// 111 000 000 000 | 000 111 000 000 @@ -51,7 +51,7 @@ bool RawCode::is_horizontal_mirror(const RawCode &raw_code) const noexcept { constexpr uint64_t MASK_MIRROR_H1 = 0x0'007'007'007'007'007; constexpr uint64_t MASK_MIRROR_H2 = 0x0'038'038'038'038'038; -/// MASK_MIRROR_V1 | MASK_MIRROR_V2 | MASK_MIRROR_V3 +/// MASK_MIRROR_V1 | MASK_MIRROR_V2 | MASK_MIRROR_V3 /// 111 111 111 111 | 000 000 000 000 | 000 000 000 000 /// 000 000 000 000 | 111 111 111 111 | 000 000 000 000 /// 000 000 000 000 | 000 000 000 000 | 111 111 111 111 diff --git a/src/klotski_core/raw_code/raw_code.cc b/src/klotski_core/raw_code/raw_code.cc index 14d596c..47991b0 100644 --- a/src/klotski_core/raw_code/raw_code.cc +++ b/src/klotski_core/raw_code/raw_code.cc @@ -3,10 +3,6 @@ namespace klotski { -bool RawCode::valid() const noexcept { - return RawCode::check(code_); -} - RawCode RawCode::create(uint64_t raw_code) { return RawCode(raw_code); } @@ -27,7 +23,7 @@ RawCode::RawCode(uint64_t raw_code) { std::ostream& operator<<(std::ostream &out, const RawCode &self) { char code[16]; char dump_map[] = { - /// 0x0 1x2 2x1 1x1 2x2 b101 b110 fill + /// 0x0 1x2 2x1 1x1 2x2 b101 b110 fill '.', '~', '|', '*', '@', '?', '?', '+' }; sprintf(code, "%015lX", self.code_); // code length -> 15 diff --git a/src/klotski_core/raw_code/raw_code.h b/src/klotski_core/raw_code/raw_code.h index bd88e5c..9df594f 100644 --- a/src/klotski_core/raw_code/raw_code.h +++ b/src/klotski_core/raw_code/raw_code.h @@ -38,6 +38,7 @@ /// => 0x0E58'FC85'FFEB'C4DB #include +#include #include #include #include @@ -45,8 +46,12 @@ namespace klotski { +class RawCode; class CommonCode; +typedef std::vector RawCodes; +typedef std::vector CommonCodes; + class RawCodeExp : public std::runtime_error { public: RawCodeExp() : std::runtime_error("invalid raw code") {} @@ -69,8 +74,8 @@ class RawCode { public: /// Validity check - bool valid() const noexcept; static bool check(uint64_t raw_code) noexcept; + bool valid() const noexcept { return check(code_); } /// Operators of RawCode constexpr explicit operator uint64_t() const noexcept { return code_; } @@ -85,7 +90,7 @@ public: explicit RawCode(CommonCode &&common_code) noexcept; explicit RawCode(const CommonCode &common_code) noexcept; - /// Static initialization + /// RawCode initializations static RawCode create(uint64_t raw_code); static RawCode unsafe_create(uint64_t raw_code) noexcept; @@ -95,6 +100,9 @@ public: static RawCode from_common_code(const CommonCode &common_code) noexcept; static RawCode from_common_code(const std::string &common_code); + /// Batch conversion + static CommonCodes convert(const RawCodes &raw_codes) noexcept; + /// Mirror functions RawCode to_vertical_mirror() const noexcept; RawCode to_horizontal_mirror() const noexcept; @@ -108,12 +116,14 @@ public: bool is_horizontal_mirror(const RawCode &raw_code) const noexcept; }; +/// Compare implements inline bool operator==(uint64_t r1, const RawCode &r2) noexcept { return r1 == r2.unwrap(); } -inline bool operator!=(uint64_t r1, const RawCode &r2) noexcept { return r1 != r2.unwrap(); } inline bool operator==(const RawCode &r1, uint64_t r2) noexcept { return r1.unwrap() == r2; } -inline bool operator!=(const RawCode &r1, uint64_t r2) noexcept { return r1.unwrap() != r2; } inline bool operator==(const RawCode &r1, const RawCode &r2) noexcept { return r1.unwrap() == r2.unwrap(); } -inline bool operator!=(const RawCode &r1, const RawCode &r2) noexcept { return r1.unwrap() != r2.unwrap(); } + +inline bool operator!=(uint64_t r1, const RawCode &r2) noexcept { return !(r1 == r2); } +inline bool operator!=(const RawCode &r1, uint64_t r2) noexcept { return !(r1 == r2); } +inline bool operator!=(const RawCode &r1, const RawCode &r2) noexcept { return !(r1 == r2); } } // namespace klotski diff --git a/src/klotski_core/short_code/convert.cc b/src/klotski_core/short_code/convert.cc index 1fac5ad..d030e11 100644 --- a/src/klotski_core/short_code/convert.cc +++ b/src/klotski_core/short_code/convert.cc @@ -6,6 +6,7 @@ #include "range_prefix_offset.h" using klotski::ShortCode; +using klotski::ShortCodes; using klotski::CommonCode; /// ------------------------- ShortCode to CommonCode ------------------------- @@ -57,6 +58,12 @@ ShortCode ShortCode::from_common_code(const std::string &common_code) { return ShortCode(CommonCode(common_code)); } +/// ---------------------------- Batch conversion ----------------------------- + +ShortCodes ShortCode::convert(const CommonCodes &common_codes) noexcept { + return {common_codes.begin(), common_codes.end()}; +} + /// ----------------------------- Basic Functions ----------------------------- /// NOTE: ensure that input common code is valid! diff --git a/src/klotski_core/short_code/short_code.cc b/src/klotski_core/short_code/short_code.cc index b744bc9..ee68fdf 100644 --- a/src/klotski_core/short_code/short_code.cc +++ b/src/klotski_core/short_code/short_code.cc @@ -6,10 +6,6 @@ namespace klotski { bool ShortCode::fast_mode_available_ = false; bool ShortCode::normal_mode_available_ = false; -bool ShortCode::valid() const noexcept { - return ShortCode::check(code_); -} - ShortCode ShortCode::create(uint32_t short_code) { return ShortCode(short_code); } diff --git a/src/klotski_core/short_code/short_code.h b/src/klotski_core/short_code/short_code.h index da139be..159fd3f 100644 --- a/src/klotski_core/short_code/short_code.h +++ b/src/klotski_core/short_code/short_code.h @@ -49,6 +49,7 @@ /// while the latter is almost impossible to complete by the human brain. #include +#include #include #include #include @@ -58,8 +59,12 @@ namespace klotski { +class ShortCode; class CommonCode; +typedef std::vector ShortCodes; +typedef std::vector CommonCodes; + const uint32_t SHORT_CODE_LIMIT = 29334498; class ShortCodeExp : public std::runtime_error { @@ -97,8 +102,8 @@ private: public: /// Validity check - bool valid() const noexcept; static bool check(uint32_t short_code) noexcept; + bool valid() const noexcept { return check(code_); } /// ShortCode convert mode static void speed_up(Mode mode); // {} -> {NORMAL} -> {FAST} @@ -119,7 +124,7 @@ public: explicit ShortCode(const std::string &short_code); explicit ShortCode(const CommonCode &common_code) noexcept; - /// Static initialization + /// ShortCode initializations static ShortCode create(uint32_t short_code); static ShortCode unsafe_create(uint32_t short_code) noexcept; @@ -131,14 +136,21 @@ public: static ShortCode from_common_code(std::string &&common_code); static ShortCode from_common_code(const CommonCode &common_code) noexcept; static ShortCode from_common_code(const std::string &common_code); + + /// Batch conversion + static ShortCodes convert(const CommonCodes &common_codes) noexcept; }; +/// Compare implements inline bool operator==(uint32_t s1, const ShortCode &s2) noexcept { return s1 == s2.unwrap(); } -inline bool operator!=(uint32_t s1, const ShortCode &s2) noexcept { return s1 != s2.unwrap(); } inline bool operator==(const ShortCode &s1, uint32_t s2) noexcept { return s1.unwrap() == s2; } -inline bool operator!=(const ShortCode &s1, uint32_t s2) noexcept { return s1.unwrap() != s2; } +inline bool operator!=(uint32_t s1, const ShortCode &s2) noexcept { return !(s1 == s2); } +inline bool operator!=(const ShortCode &s1, uint32_t s2) noexcept { return !(s1 == s2); } + +inline bool operator<(const ShortCode &s1, const ShortCode &s2) noexcept { return s1.unwrap() < s2.unwrap(); } +inline bool operator>(const ShortCode &s1, const ShortCode &s2) noexcept { return s1.unwrap() > s2.unwrap(); } inline bool operator==(const ShortCode &s1, const ShortCode &s2) noexcept { return s1.unwrap() == s2.unwrap(); } -inline bool operator!=(const ShortCode &s1, const ShortCode &s2) noexcept { return s1.unwrap() != s2.unwrap(); } +inline bool operator!=(const ShortCode &s1, const ShortCode &s2) noexcept { return !(s1 == s2); } } // namespace klotski