Browse Source

update: RawCode support rvalue

master
Dnomd343 2 years ago
parent
commit
e007047fb9
  1. 27
      src/klotski_core/raw_code/convert.cc
  2. 8
      src/klotski_core/raw_code/raw_code.cc
  3. 37
      src/klotski_core/raw_code/raw_code.h

27
src/klotski_core/raw_code/convert.cc

@ -1,4 +1,3 @@
#include <stdexcept>
#include "common.h" #include "common.h"
#include "raw_code.h" #include "raw_code.h"
@ -12,20 +11,32 @@ CommonCode RawCode::to_common_code() const {
} }
/// CommonCode to RawCode /// CommonCode to RawCode
RawCode RawCode::from_common_code(const CommonCode &common_code) { RawCode::RawCode(CommonCode &&common_code) {
return RawCode(common_code); // load from 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) { 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) { RawCode RawCode::from_common_code(CommonCode &&common_code) {
return RawCode(CommonCode(common_code)); // load from common code return RawCode(std::forward<CommonCode>(common_code));
} }
RawCode::RawCode(const CommonCode &common_code) { RawCode RawCode::from_common_code(std::string &&common_code) {
code = RawCode::extract(common_code.unwrap()); // load from common code return RawCode(std::forward<CommonCode>(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! /// NOTE: ensure that input raw code is valid!

8
src/klotski_core/raw_code/raw_code.cc

@ -1,6 +1,8 @@
#include "common.h" #include "common.h"
#include "raw_code.h" #include "raw_code.h"
using klotski::RawCode;
namespace std { namespace std {
template<> template<>
struct hash<klotski::RawCode> { struct hash<klotski::RawCode> {
@ -18,6 +20,10 @@ namespace std {
} }
namespace klotski { namespace klotski {
bool RawCode::operator==(uint64_t raw_code) const {
return this->code == raw_code;
}
bool RawCode::operator==(const RawCode &raw_code) const { bool RawCode::operator==(const RawCode &raw_code) const {
return this->code == raw_code.code; 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 /// MASK_1x2 MASK_2x1 MASK_2x2
/// 000 100 000 000 000 000 000 000 000 100 000 000 /// 000 100 000 000 000 000 000 000 000 100 000 000
/// 000 000 000 000 100 000 000 000 100 100 000 000 /// 000 000 000 000 100 000 000 000 100 100 000 000

37
src/klotski_core/raw_code/raw_code.h

@ -54,11 +54,19 @@ namespace klotski {
}; };
class RawCode { 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: public:
/// RawCode validity check
bool valid() const; bool valid() const;
static bool check(uint64_t raw_code); static bool check(uint64_t raw_code);
/// Operators of RawCode /// Operators of RawCode
bool operator==(uint64_t raw_code) const;
bool operator==(const RawCode &raw_code) const; bool operator==(const RawCode &raw_code) const;
constexpr explicit operator uint64_t() const { return code; } constexpr explicit operator uint64_t() const { return code; }
friend std::ostream& operator<<(std::ostream &out, const RawCode &self); friend std::ostream& operator<<(std::ostream &out, const RawCode &self);
@ -69,23 +77,34 @@ namespace klotski {
/// RawCode constructors /// RawCode constructors
explicit RawCode(uint64_t raw_code); explicit RawCode(uint64_t raw_code);
explicit RawCode(CommonCode &&common_code);
explicit RawCode(const CommonCode &common_code); explicit RawCode(const CommonCode &common_code);
/// Rust-style initialization /// Static initialization
static RawCode create(uint64_t raw_code); static RawCode create(uint64_t raw_code);
static RawCode unsafe_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(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 CommonCode &common_code);
static RawCode from_common_code(const std::string &common_code); static RawCode from_common_code(const std::string &common_code);
// TODO: mirror functions /// Mirror functions
// bool is_vertical_mirror() const;
private: // bool is_horizontal_mirror() const;
uint64_t code; //
RawCode() = default; // unsafe initialize // RawCode to_vertical_mirror() const;
// RawCode to_horizontal_mirror() const;
static uint64_t compact(uint64_t raw_code); //
static uint64_t extract(uint64_t common_code); // 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);
}; };
} }

Loading…
Cancel
Save