From 827fa270e39d7b4b7bcfbc69db97df999b1021bf Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 24 Feb 2023 19:56:48 +0800 Subject: [PATCH] perf: mirror interface of RawCode --- src/klotski_core/all_cases/all_cases.h | 2 +- src/klotski_core/all_cases/basic_ranges.h | 2 +- src/klotski_core/raw_code/convert.cc | 4 +- src/klotski_core/raw_code/mirror.cc | 57 +++++++++++------------ src/klotski_core/raw_code/raw_code.cc | 14 +++--- src/klotski_core/raw_code/raw_code.h | 11 +++-- src/klotski_core/short_code/serialize.cc | 2 +- src/klotski_core/short_code/short_code.h | 10 ++-- test/basic/all_cases.cc | 1 - 9 files changed, 53 insertions(+), 50 deletions(-) diff --git a/src/klotski_core/all_cases/all_cases.h b/src/klotski_core/all_cases/all_cases.h index 8a94bd2..d1ec817 100644 --- a/src/klotski_core/all_cases/all_cases.h +++ b/src/klotski_core/all_cases/all_cases.h @@ -31,7 +31,7 @@ #include "basic_ranges.h" namespace klotski { - /// all cases count -> memory pre-allocated + /// all cases count const uint32_t ALL_CASES_SIZE[16] = { 2942906, 2260392, 2942906, 0, 2322050, 1876945, 2322050, 0, diff --git a/src/klotski_core/all_cases/basic_ranges.h b/src/klotski_core/all_cases/basic_ranges.h index 3b06d6c..3276ab8 100644 --- a/src/klotski_core/all_cases/basic_ranges.h +++ b/src/klotski_core/all_cases/basic_ranges.h @@ -31,7 +31,7 @@ #include namespace klotski { - /// basic ranges count -> memory pre-allocated + /// basic ranges count const uint32_t BASIC_RANGES_SIZE = 7311921; class BasicRanges { diff --git a/src/klotski_core/raw_code/convert.cc b/src/klotski_core/raw_code/convert.cc index cd5c841..dbfbf41 100644 --- a/src/klotski_core/raw_code/convert.cc +++ b/src/klotski_core/raw_code/convert.cc @@ -30,7 +30,9 @@ RawCode RawCode::from_common_code(CommonCode &&common_code) { } RawCode RawCode::from_common_code(std::string &&common_code) { - return RawCode(std::forward(CommonCode(common_code))); + return RawCode(std::forward( + CommonCode(std::forward(common_code)) + )); } RawCode RawCode::from_common_code(const CommonCode &common_code) { diff --git a/src/klotski_core/raw_code/mirror.cc b/src/klotski_core/raw_code/mirror.cc index 46b5d1f..16e942c 100644 --- a/src/klotski_core/raw_code/mirror.cc +++ b/src/klotski_core/raw_code/mirror.cc @@ -2,7 +2,8 @@ using klotski::RawCode; -/// Mirror convert functions +/// ----------------------------- Mirror Convert ------------------------------ + RawCode RawCode::to_vertical_mirror() const { return RawCode::unsafe_create(vertical_mirror(code)); } @@ -11,50 +12,34 @@ RawCode RawCode::to_horizontal_mirror() const { return RawCode::unsafe_create(horizontal_mirror(code)); } -/// Mirror check functions -bool RawCode::is_vertical_mirror() const { - - // TODO: vertical mirror check +/// ------------------------------ Mirror Check ------------------------------- - return false; +bool RawCode::is_vertical_mirror() const { + return vertical_mirror_check(code); } bool RawCode::is_horizontal_mirror() const { - - // TODO: horizontal mirror check - - return false; + return horizontal_mirror_check(code); } bool RawCode::is_vertical_mirror(RawCode &&raw_code) const { - - // TODO: vertical mirror check - - return false; -} - -bool RawCode::is_vertical_mirror(const RawCode &raw_code) const { - - // TODO: vertical mirror check - - return false; + return raw_code.unwrap() == vertical_mirror(code); } bool RawCode::is_horizontal_mirror(RawCode &&raw_code) const { + return raw_code.unwrap() == horizontal_mirror(code); +} - // TODO: horizontal mirror check - - return false; +bool RawCode::is_vertical_mirror(const RawCode &raw_code) const { + return raw_code.unwrap() == vertical_mirror(code); } bool RawCode::is_horizontal_mirror(const RawCode &raw_code) const { - - // TODO: horizontal mirror check - - return false; + return raw_code.unwrap() == horizontal_mirror(code); } -/// Basic mirror convert +/// ----------------------------- Basic Functions ----------------------------- + uint64_t RawCode::vertical_mirror(uint64_t raw_code) { // TODO: vertical mirror convert @@ -68,3 +53,17 @@ uint64_t RawCode::horizontal_mirror(uint64_t raw_code) { return 0; } + +bool RawCode::vertical_mirror_check(uint64_t raw_code) { + + // TODO: whether self vertical mirror + + return false; +} + +bool RawCode::horizontal_mirror_check(uint64_t raw_code) { + + // TODO: whether self horizontal mirror + + return false; +} diff --git a/src/klotski_core/raw_code/raw_code.cc b/src/klotski_core/raw_code/raw_code.cc index 65e2247..845643b 100644 --- a/src/klotski_core/raw_code/raw_code.cc +++ b/src/klotski_core/raw_code/raw_code.cc @@ -68,19 +68,19 @@ namespace klotski { } bool RawCode::check(uint64_t raw_code) { // check whether raw code is valid - /// MASK_1x2 MASK_2x1 MASK_2x2 - /// 000 100 000 000 000 000 000 000 000 100 000 000 - /// 000 000 000 000 100 000 000 000 100 100 000 000 - /// ... ... ... - constexpr uint64_t MASK_1x1 = ~B_1x1 & 0b111; /// 0b100 + /// MASK_1x1 | MASK_1x2 | MASK_2x1 | MASK_2x2 + /// 100 000 000 000 | 000 100 000 000 | 000 000 000 000 | 000 100 000 000 + /// 000 000 000 000 | 000 000 000 000 | 100 000 000 000 | 100 100 000 000 + /// ... | ... | ... | ... + /// + constexpr uint64_t MASK_1x1 = ~B_1x1 & 0b111; constexpr uint64_t MASK_1x2 = MASK_1x1 << 3; constexpr uint64_t MASK_2x1 = MASK_1x1 << 12; constexpr uint64_t MASK_2x2 = MASK_1x1 << 3 | MASK_1x1 << 12 | MASK_1x1 << 15; - - /// high 4-bits check if (raw_code >> 60) { return false; // high 4-bits must be zero } + /// check each block int head_num = 0, space_num = 0; // statistics for space and 2x2 number for (int addr = 0; addr < 20; ++addr, raw_code >>= 3) { diff --git a/src/klotski_core/raw_code/raw_code.h b/src/klotski_core/raw_code/raw_code.h index b17c65d..ad34e07 100644 --- a/src/klotski_core/raw_code/raw_code.h +++ b/src/klotski_core/raw_code/raw_code.h @@ -57,11 +57,14 @@ namespace klotski { uint64_t code; RawCode() = default; // unsafe initialize - static uint64_t compact(uint64_t raw_code); // raw code -> common code - static uint64_t extract(uint64_t common_code); // common code -> raw code + static inline uint64_t compact(uint64_t raw_code); // raw code -> common code + static inline uint64_t extract(uint64_t common_code); // common code -> raw code - static uint64_t vertical_mirror(uint64_t raw_code); // to vertical mirror - static uint64_t horizontal_mirror(uint64_t raw_code); // to horizontal mirror + static inline uint64_t vertical_mirror(uint64_t raw_code); // to vertical mirror + static inline uint64_t horizontal_mirror(uint64_t raw_code); // to horizontal mirror + + static inline bool vertical_mirror_check(uint64_t raw_code); // check vertical mirror + static inline bool horizontal_mirror_check(uint64_t raw_code); // check horizontal mirror public: /// RawCode validity check diff --git a/src/klotski_core/short_code/serialize.cc b/src/klotski_core/short_code/serialize.cc index 23519c1..132d937 100644 --- a/src/klotski_core/short_code/serialize.cc +++ b/src/klotski_core/short_code/serialize.cc @@ -21,7 +21,7 @@ ShortCode::ShortCode(const std::string &short_code) { } ShortCode ShortCode::from_string(std::string &&short_code) { - return ShortCode(short_code); + return ShortCode(std::forward(short_code)); } ShortCode ShortCode::from_string(const std::string &short_code) { diff --git a/src/klotski_core/short_code/short_code.h b/src/klotski_core/short_code/short_code.h index 67c959c..6769047 100644 --- a/src/klotski_core/short_code/short_code.h +++ b/src/klotski_core/short_code/short_code.h @@ -76,14 +76,14 @@ namespace klotski { uint32_t code; ShortCode() = default; // unsafe initialize - static inline Mode mode(); + static 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 inline uint64_t fast_decode(uint32_t short_code); // short code -> common code + static inline uint32_t fast_encode(uint64_t common_code); // common code -> short code + static inline uint64_t tiny_decode(uint32_t short_code); // short code -> common code + static inline uint32_t tiny_encode(uint64_t common_code); // common code -> short code static inline std::string string_encode(uint32_t short_code); // short code -> string static inline uint32_t string_decode(const std::string &short_code); // string -> short code diff --git a/test/basic/all_cases.cc b/test/basic/all_cases.cc index 952d55d..f741605 100644 --- a/test/basic/all_cases.cc +++ b/test/basic/all_cases.cc @@ -1,5 +1,4 @@ #include -#include #include "md5.h" #include "all_cases.h" #include "gtest/gtest.h"