From e007047fb9a6ceb555d13d125f3269f247d2880d Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 22 Feb 2023 17:18:42 +0800 Subject: [PATCH] update: RawCode support rvalue --- src/klotski_core/raw_code/convert.cc | 27 +++++++++++++------ src/klotski_core/raw_code/raw_code.cc | 8 +++++- src/klotski_core/raw_code/raw_code.h | 37 ++++++++++++++++++++------- 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/src/klotski_core/raw_code/convert.cc b/src/klotski_core/raw_code/convert.cc index d99a452..a0434e2 100644 --- a/src/klotski_core/raw_code/convert.cc +++ b/src/klotski_core/raw_code/convert.cc @@ -1,4 +1,3 @@ -#include #include "common.h" #include "raw_code.h" @@ -12,20 +11,32 @@ CommonCode RawCode::to_common_code() const { } /// CommonCode to RawCode -RawCode RawCode::from_common_code(const CommonCode &common_code) { - return RawCode(common_code); // load from common code +RawCode::RawCode(CommonCode &&common_code) { + code = RawCode::extract(common_code.unwrap()); +} + +RawCode::RawCode(const CommonCode &common_code) { + code = RawCode::extract(common_code.unwrap()); } RawCode RawCode::from_common_code(uint64_t common_code) { - return RawCode(CommonCode(common_code)); // load from common code + return RawCode(CommonCode(common_code)); } -RawCode RawCode::from_common_code(const std::string &common_code) { - return RawCode(CommonCode(common_code)); // load from common code +RawCode RawCode::from_common_code(CommonCode &&common_code) { + return RawCode(std::forward(common_code)); } -RawCode::RawCode(const CommonCode &common_code) { - code = RawCode::extract(common_code.unwrap()); // load from common code +RawCode RawCode::from_common_code(std::string &&common_code) { + return RawCode(std::forward(CommonCode(common_code))); +} + +RawCode RawCode::from_common_code(const CommonCode &common_code) { + return RawCode(common_code); +} + +RawCode RawCode::from_common_code(const std::string &common_code) { + return RawCode(CommonCode(common_code)); } /// NOTE: ensure that input raw code is valid! diff --git a/src/klotski_core/raw_code/raw_code.cc b/src/klotski_core/raw_code/raw_code.cc index aec594d..65e2247 100644 --- a/src/klotski_core/raw_code/raw_code.cc +++ b/src/klotski_core/raw_code/raw_code.cc @@ -1,6 +1,8 @@ #include "common.h" #include "raw_code.h" +using klotski::RawCode; + namespace std { template<> struct hash { @@ -18,6 +20,10 @@ namespace std { } namespace klotski { + bool RawCode::operator==(uint64_t raw_code) const { + return this->code == raw_code; + } + bool RawCode::operator==(const RawCode &raw_code) const { return this->code == raw_code.code; } @@ -61,7 +67,7 @@ namespace klotski { } } -bool klotski::RawCode::check(uint64_t raw_code) { // check whether raw code is valid +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 diff --git a/src/klotski_core/raw_code/raw_code.h b/src/klotski_core/raw_code/raw_code.h index 0bb5d54..1e0b1a5 100644 --- a/src/klotski_core/raw_code/raw_code.h +++ b/src/klotski_core/raw_code/raw_code.h @@ -54,11 +54,19 @@ namespace klotski { }; class RawCode { + 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 + public: + /// RawCode validity check bool valid() const; static bool check(uint64_t raw_code); /// Operators of RawCode + bool operator==(uint64_t raw_code) const; bool operator==(const RawCode &raw_code) const; constexpr explicit operator uint64_t() const { return code; } friend std::ostream& operator<<(std::ostream &out, const RawCode &self); @@ -69,23 +77,34 @@ namespace klotski { /// RawCode constructors explicit RawCode(uint64_t raw_code); + explicit RawCode(CommonCode &&common_code); explicit RawCode(const CommonCode &common_code); - /// Rust-style initialization + /// Static initialization static RawCode create(uint64_t raw_code); static RawCode unsafe_create(uint64_t raw_code); static RawCode from_common_code(uint64_t common_code); + static RawCode from_common_code(CommonCode &&common_code); + static RawCode from_common_code(std::string &&common_code); static RawCode from_common_code(const CommonCode &common_code); static RawCode from_common_code(const std::string &common_code); - // TODO: mirror functions - - private: - uint64_t code; - RawCode() = default; // unsafe initialize - - static uint64_t compact(uint64_t raw_code); - static uint64_t extract(uint64_t 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); +// +// 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); }; }