Browse Source

update: enhance codec interface

legacy
Dnomd343 2 years ago
parent
commit
ffedc8f259
  1. 50
      src/klotski/common_code/common_code.cc
  2. 6
      src/klotski/common_code/common_code.h
  3. 60
      src/klotski/raw_code/raw_code.cc
  4. 6
      src/klotski/raw_code/raw_code.h
  5. 48
      src/klotski/short_code/short_code.cc
  6. 8
      src/klotski/short_code/short_code.h

50
src/klotski/common_code/common_code.cc

@ -2,12 +2,36 @@
#include "common.h" #include "common.h"
#include "common_code.h" #include "common_code.h"
using klotski::CommonCode; namespace std {
template<>
struct hash<klotski::CommonCode> {
std::size_t operator()(const klotski::CommonCode &c) const {
return std::hash<uint64_t>()(c.unwrap());
}
};
template<>
struct equal_to<klotski::CommonCode> {
bool operator()(const klotski::CommonCode &c1, const klotski::CommonCode &c2) const {
return c1.unwrap() == c2.unwrap();
}
};
}
namespace klotski {
bool CommonCode::operator==(const CommonCode &common_code) const {
return this->code == common_code.code;
}
uint64_t CommonCode::unwrap() const { std::ostream& operator<<(std::ostream &out, const CommonCode &self) {
return code; // raw uint64_t code char str[10];
sprintf(str, "%09lX", self.code);
out << str;
return out;
}
} }
namespace klotski {
bool CommonCode::valid() const { bool CommonCode::valid() const {
return CommonCode::check(code); return CommonCode::check(code);
} }
@ -17,9 +41,9 @@ CommonCode CommonCode::create(uint64_t common_code) {
} }
CommonCode CommonCode::unsafe_create(uint64_t common_code) { // create without check CommonCode CommonCode::unsafe_create(uint64_t common_code) { // create without check
auto common = CommonCode(); // init directly auto tmp = CommonCode(); // init directly
common.code = common_code; tmp.code = common_code;
return common; return tmp;
} }
CommonCode::CommonCode(uint64_t common_code) { CommonCode::CommonCode(uint64_t common_code) {
@ -28,21 +52,9 @@ CommonCode::CommonCode(uint64_t common_code) {
} }
code = common_code; code = common_code;
} }
bool CommonCode::operator==(const CommonCode &common_code) const {
return this->code == common_code.code;
}
namespace klotski {
std::ostream &operator<<(std::ostream &out, const CommonCode &self) {
char str[10];
sprintf(str, "%09lX", self.code);
out << str;
return out;
}
} }
bool CommonCode::check(uint64_t common_code) { // whether common code is valid bool klotski::CommonCode::check(uint64_t common_code) { // whether common code is valid
/// M_1x1 M_1x2 M_2x1 M_2x2 /// M_1x1 M_1x2 M_2x1 M_2x2
/// 1 0 0 0 1 1 0 0 1 0 0 0 1 1 0 0 /// 1 0 0 0 1 1 0 0 1 0 0 0 1 1 0 0
/// 0 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 /// 0 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0

6
src/klotski/common_code/common_code.h

@ -13,19 +13,19 @@ namespace klotski {
class CommonCode { class CommonCode {
public: public:
bool valid() const; inline bool valid() const;
static bool check(uint64_t common_code); static bool check(uint64_t common_code);
/// Operators of CommonCode /// Operators of CommonCode
explicit operator uint64_t() const { return code; } constexpr operator uint64_t() const { return code; }
bool operator==(const CommonCode &common_code) const; bool operator==(const CommonCode &common_code) const;
friend std::ostream& operator<<(std::ostream &out, const CommonCode &self); friend std::ostream& operator<<(std::ostream &out, const CommonCode &self);
/// Export functions /// Export functions
uint64_t unwrap() const;
RawCode to_raw_code() const; RawCode to_raw_code() const;
ShortCode to_short_code() const; ShortCode to_short_code() const;
std::string to_string(bool shorten = false) const; std::string to_string(bool shorten = false) const;
constexpr uint64_t unwrap() const { return code; }
/// CommonCode constructors /// CommonCode constructors
explicit CommonCode(uint64_t common_code); explicit CommonCode(uint64_t common_code);

60
src/klotski/raw_code/raw_code.cc

@ -2,38 +2,27 @@
#include "common.h" #include "common.h"
#include "raw_code.h" #include "raw_code.h"
using klotski::RawCode; namespace std {
template<>
uint64_t RawCode::unwrap() const { struct hash<klotski::RawCode> {
return code; // raw uint64_t code std::size_t operator()(const klotski::RawCode &c) const {
} return std::hash<uint64_t>()(c.unwrap());
bool RawCode::valid() const {
return RawCode::check(code);
}
RawCode RawCode::create(uint64_t raw_code) {
return RawCode(raw_code);
}
RawCode RawCode::unsafe_create(uint64_t raw_code) { // create without check
auto raw = RawCode(); // init directly
raw.code = raw_code;
return raw;
} }
};
RawCode::RawCode(uint64_t raw_code) { template<>
if (!RawCode::check(raw_code)) { // check input raw code struct equal_to<klotski::RawCode> {
throw std::invalid_argument("invalid raw code"); bool operator()(const klotski::RawCode &c1, const klotski::RawCode &c2) const {
return c1.unwrap() == c2.unwrap();
} }
code = raw_code; };
} }
namespace klotski {
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;
} }
namespace klotski {
std::ostream& operator<<(std::ostream &out, const RawCode &self) { std::ostream& operator<<(std::ostream &out, const RawCode &self) {
char code[16]; char code[16];
char dump_map[] = { char dump_map[] = {
@ -50,7 +39,30 @@ namespace klotski {
} }
} }
bool RawCode::check(uint64_t raw_code) { // check whether raw code is valid namespace klotski {
bool RawCode::valid() const {
return RawCode::check(code);
}
RawCode RawCode::create(uint64_t raw_code) {
return RawCode(raw_code);
}
RawCode RawCode::unsafe_create(uint64_t raw_code) { // create without check
auto tmp = RawCode(); // init directly
tmp.code = raw_code;
return tmp;
}
RawCode::RawCode(uint64_t raw_code) {
if (!RawCode::check(raw_code)) { // check input raw code
throw std::invalid_argument("invalid raw code");
}
code = raw_code;
}
}
bool klotski::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

6
src/klotski/raw_code/raw_code.h

@ -45,17 +45,17 @@ namespace klotski {
class RawCode { class RawCode {
public: public:
bool valid() const; inline 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==(const RawCode &raw_code) const; bool operator==(const RawCode &raw_code) const;
explicit operator uint64_t() const { return code; } constexpr 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);
/// Export functions /// Export functions
uint64_t unwrap() const;
CommonCode to_common_code() const; CommonCode to_common_code() const;
constexpr uint64_t unwrap() const { return code; }
/// RawCode constructors /// RawCode constructors
explicit RawCode(uint64_t raw_code); explicit RawCode(uint64_t raw_code);

48
src/klotski/short_code/short_code.cc

@ -1,12 +1,34 @@
#include "all_cases.h" #include "all_cases.h"
#include "short_code.h" #include "short_code.h"
using klotski::ShortCode; namespace std {
template<>
struct hash<klotski::ShortCode> {
std::size_t operator()(const klotski::ShortCode &c) const {
return std::hash<uint64_t>()(c.unwrap());
}
};
template<>
struct equal_to<klotski::ShortCode> {
bool operator()(const klotski::ShortCode &c1, const klotski::ShortCode &c2) const {
return c1.unwrap() == c2.unwrap();
}
};
}
uint32_t ShortCode::unwrap() const { namespace klotski {
return code; // raw uint32_t code bool ShortCode::operator==(const ShortCode &short_code) const {
return this->code == short_code.code;
} }
std::ostream &operator<<(std::ostream &out, const ShortCode &self) {
out << self.to_string() << "(" << self.code << ")"; // short code info
return out;
}
}
namespace klotski {
bool ShortCode::valid() const { bool ShortCode::valid() const {
return ShortCode::check(code); return ShortCode::check(code);
} }
@ -15,27 +37,25 @@ ShortCode ShortCode::create(uint32_t short_code) {
return ShortCode(short_code); return ShortCode(short_code);
} }
ShortCode ShortCode::unsafe_create(uint32_t short_code) { // create without check
auto tmp = ShortCode(); // init directly
tmp.code = short_code;
return tmp;
}
ShortCode::ShortCode(uint32_t short_code) { ShortCode::ShortCode(uint32_t short_code) {
if (!ShortCode::check(short_code)) { // check input short code if (!ShortCode::check(short_code)) { // check input short code
throw std::invalid_argument("invalid short code"); throw std::invalid_argument("invalid short code");
} }
code = short_code; code = short_code;
} }
bool ShortCode::check(uint32_t short_code) {
return short_code < SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1)
} }
bool ShortCode::operator==(const ShortCode &short_code) const { bool klotski::ShortCode::check(uint32_t short_code) {
return this->code == short_code.code; return short_code < SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1)
} }
namespace klotski { using klotski::ShortCode;
std::ostream &operator<<(std::ostream &out, const ShortCode &self) {
out << self.to_string() << "(" << self.code << ")"; // short code info
return out;
}
}
bool ShortCode::fast_mode_available = false; bool ShortCode::fast_mode_available = false;
bool ShortCode::normal_mode_available = false; bool ShortCode::normal_mode_available = false;

8
src/klotski/short_code/short_code.h

@ -13,19 +13,19 @@ namespace klotski {
public: public:
enum Mode {NORMAL, FAST}; enum Mode {NORMAL, FAST};
bool valid() const; inline bool valid() const;
static void speed_up(enum Mode mode); static void speed_up(enum Mode mode);
static bool check(uint32_t short_code); static bool check(uint32_t short_code);
/// Operators of ShortCode /// Operators of ShortCode
explicit operator uint32_t() const { return code; } constexpr operator uint32_t() const { return code; }
bool operator==(const ShortCode &short_code) const; bool operator==(const ShortCode &short_code) const;
friend std::ostream& operator<<(std::ostream &out, const ShortCode &self); friend std::ostream& operator<<(std::ostream &out, const ShortCode &self);
/// Export functions /// Export functions
uint32_t unwrap() const;
std::string to_string() const; std::string to_string() const;
CommonCode to_common_code() const; CommonCode to_common_code() const;
constexpr uint32_t unwrap() const { return code; }
/// ShortCode constructors /// ShortCode constructors
explicit ShortCode(uint32_t short_code); explicit ShortCode(uint32_t short_code);
@ -38,6 +38,7 @@ namespace klotski {
/// Rust-style initialization /// Rust-style initialization
static ShortCode create(uint32_t short_code); static ShortCode create(uint32_t short_code);
static ShortCode unsafe_create(uint32_t short_code);
static ShortCode from_string(const std::string &short_code); static ShortCode from_string(const std::string &short_code);
static ShortCode from_common_code(uint64_t common_code); static ShortCode from_common_code(uint64_t common_code);
@ -50,6 +51,7 @@ namespace klotski {
static bool normal_mode_available; static bool normal_mode_available;
static const uint32_t SHORT_CODE_LIMIT = 29334498; static const uint32_t SHORT_CODE_LIMIT = 29334498;
ShortCode() = default; // unsafe initialize
static enum Mode mode(); static enum Mode mode();
static uint64_t fast_decode(uint32_t short_code); static uint64_t fast_decode(uint32_t short_code);
static uint32_t fast_encode(uint64_t common_code); static uint32_t fast_encode(uint64_t common_code);

Loading…
Cancel
Save