Browse Source

update: enhance codec interface

master
Dnomd343 2 years ago
parent
commit
ffedc8f259
  1. 68
      src/klotski/common_code/common_code.cc
  2. 6
      src/klotski/common_code/common_code.h
  3. 74
      src/klotski/raw_code/raw_code.cc
  4. 6
      src/klotski/raw_code/raw_code.h
  5. 66
      src/klotski/short_code/short_code.cc
  6. 8
      src/klotski/short_code/short_code.h

68
src/klotski/common_code/common_code.cc

@ -2,47 +2,59 @@
#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());
}
};
uint64_t CommonCode::unwrap() const { template<>
return code; // raw uint64_t code struct equal_to<klotski::CommonCode> {
bool operator()(const klotski::CommonCode &c1, const klotski::CommonCode &c2) const {
return c1.unwrap() == c2.unwrap();
}
};
} }
bool CommonCode::valid() const { namespace klotski {
return CommonCode::check(code); bool CommonCode::operator==(const CommonCode &common_code) const {
} return this->code == common_code.code;
}
CommonCode CommonCode::create(uint64_t common_code) { std::ostream& operator<<(std::ostream &out, const CommonCode &self) {
return CommonCode(common_code); // create from uint64_t char str[10];
sprintf(str, "%09lX", self.code);
out << str;
return out;
}
} }
CommonCode CommonCode::unsafe_create(uint64_t common_code) { // create without check namespace klotski {
auto common = CommonCode(); // init directly bool CommonCode::valid() const {
common.code = common_code; return CommonCode::check(code);
return common; }
}
CommonCode::CommonCode(uint64_t common_code) { CommonCode CommonCode::create(uint64_t common_code) {
if (!CommonCode::check(common_code)) { // check input common code return CommonCode(common_code); // create from uint64_t
throw std::invalid_argument("invalid common code");
} }
code = common_code;
}
bool CommonCode::operator==(const CommonCode &common_code) const { CommonCode CommonCode::unsafe_create(uint64_t common_code) { // create without check
return this->code == common_code.code; auto tmp = CommonCode(); // init directly
} tmp.code = common_code;
return tmp;
}
namespace klotski { CommonCode::CommonCode(uint64_t common_code) {
std::ostream &operator<<(std::ostream &out, const CommonCode &self) { if (!CommonCode::check(common_code)) { // check input common code
char str[10]; throw std::invalid_argument("invalid common code");
sprintf(str, "%09lX", self.code); }
out << str; code = common_code;
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);

74
src/klotski/raw_code/raw_code.cc

@ -2,43 +2,32 @@
#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 template<>
auto raw = RawCode(); // init directly struct equal_to<klotski::RawCode> {
raw.code = raw_code; bool operator()(const klotski::RawCode &c1, const klotski::RawCode &c2) const {
return raw; return c1.unwrap() == c2.unwrap();
}
};
} }
RawCode::RawCode(uint64_t raw_code) { namespace klotski {
if (!RawCode::check(raw_code)) { // check input raw code bool RawCode::operator==(const RawCode &raw_code) const {
throw std::invalid_argument("invalid raw code"); return this->code == raw_code.code;
} }
code = raw_code;
}
bool RawCode::operator==(const RawCode &raw_code) const {
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[] = {
/// 0x0 1x2 2x1 1x1 2x2 b101 b110 fill /// 0x0 1x2 2x1 1x1 2x2 b101 b110 fill
'.', '~', '|', '*', '@', '?', '?', '+' '.', '~', '|', '*', '@', '?', '?', '+'
}; };
sprintf(code, "%015lX", self.code); // code length -> 15 sprintf(code, "%015lX", self.code); // code length -> 15
out << code << '\n'; out << code << '\n';
@ -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);

66
src/klotski/short_code/short_code.cc

@ -1,42 +1,62 @@
#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());
}
};
uint32_t ShortCode::unwrap() const { template<>
return code; // raw uint32_t code struct equal_to<klotski::ShortCode> {
bool operator()(const klotski::ShortCode &c1, const klotski::ShortCode &c2) const {
return c1.unwrap() == c2.unwrap();
}
};
} }
bool ShortCode::valid() const { namespace klotski {
return ShortCode::check(code); bool ShortCode::operator==(const ShortCode &short_code) const {
} return this->code == short_code.code;
}
ShortCode ShortCode::create(uint32_t short_code) { std::ostream &operator<<(std::ostream &out, const ShortCode &self) {
return ShortCode(short_code); out << self.to_string() << "(" << self.code << ")"; // short code info
return out;
}
} }
ShortCode::ShortCode(uint32_t short_code) { namespace klotski {
if (!ShortCode::check(short_code)) { // check input short code bool ShortCode::valid() const {
throw std::invalid_argument("invalid short code"); return ShortCode::check(code);
} }
code = short_code;
}
bool ShortCode::check(uint32_t short_code) { ShortCode ShortCode::create(uint32_t short_code) {
return short_code < SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1) return ShortCode(short_code);
} }
bool ShortCode::operator==(const ShortCode &short_code) const { ShortCode ShortCode::unsafe_create(uint32_t short_code) { // create without check
return this->code == short_code.code; auto tmp = ShortCode(); // init directly
} tmp.code = short_code;
return tmp;
}
namespace klotski { ShortCode::ShortCode(uint32_t short_code) {
std::ostream &operator<<(std::ostream &out, const ShortCode &self) { if (!ShortCode::check(short_code)) { // check input short code
out << self.to_string() << "(" << self.code << ")"; // short code info throw std::invalid_argument("invalid short code");
return out; }
code = short_code;
} }
} }
bool klotski::ShortCode::check(uint32_t short_code) {
return short_code < SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1)
}
using klotski::ShortCode;
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