Browse Source

update: remove unneeded functions

master
Dnomd343 2 years ago
parent
commit
0c49bd05ef
  1. 51
      src/klotski_core/raw_code/mirror.cc
  2. 17
      src/klotski_core/raw_code/raw_code.h
  3. 34
      src/klotski_core/short_code/convert.cc
  4. 26
      src/klotski_core/short_code/serialize.cc
  5. 12
      src/klotski_core/short_code/short_code.cc
  6. 14
      src/klotski_core/short_code/short_code.h

51
src/klotski_core/raw_code/mirror.cc

@ -2,37 +2,17 @@
using klotski::RawCode; using klotski::RawCode;
/// Static mirror functions /// Mirror convert functions
bool RawCode::is_vertical_mirror(RawCode &&raw_code) { RawCode RawCode::to_vertical_mirror() const {
return raw_code.is_vertical_mirror();
}
bool RawCode::is_horizontal_mirror(RawCode &&raw_code) {
return raw_code.is_horizontal_mirror();
}
bool RawCode::is_vertical_mirror(const RawCode &raw_code) {
return raw_code.is_vertical_mirror();
}
bool RawCode::is_horizontal_mirror(const RawCode &raw_code) { // TODO: vertical mirror convert
return raw_code.is_horizontal_mirror();
}
RawCode RawCode::to_vertical_mirror(RawCode &&raw_code) {
return raw_code.to_vertical_mirror();
} }
RawCode RawCode::to_horizontal_mirror(RawCode &&raw_code) { RawCode RawCode::to_horizontal_mirror() const {
return raw_code.to_horizontal_mirror();
}
RawCode RawCode::to_vertical_mirror(const RawCode &raw_code) { // TODO: horizontal mirror convert
return raw_code.to_vertical_mirror();
}
RawCode RawCode::to_horizontal_mirror(const RawCode &raw_code) {
return raw_code.to_horizontal_mirror();
} }
/// Mirror check functions /// Mirror check functions
@ -48,15 +28,26 @@ bool RawCode::is_horizontal_mirror() const {
} }
/// Mirror convert functions bool RawCode::is_vertical_mirror(RawCode &&raw_code) const {
RawCode RawCode::to_vertical_mirror() const {
// TODO: vertical mirror convert // TODO: vertical mirror check
} }
RawCode RawCode::to_horizontal_mirror() const { bool RawCode::is_vertical_mirror(const RawCode &raw_code) const {
// TODO: horizontal mirror convert // TODO: vertical mirror check
}
bool RawCode::is_horizontal_mirror(RawCode &&raw_code) const {
// TODO: horizontal mirror check
}
bool RawCode::is_horizontal_mirror(const RawCode &raw_code) const {
// TODO: horizontal mirror check
} }

17
src/klotski_core/raw_code/raw_code.h

@ -91,20 +91,15 @@ namespace klotski {
static RawCode from_common_code(const std::string &common_code); static RawCode from_common_code(const std::string &common_code);
/// Mirror functions /// Mirror functions
bool is_vertical_mirror() const;
bool is_horizontal_mirror() const;
RawCode to_vertical_mirror() const; RawCode to_vertical_mirror() const;
RawCode to_horizontal_mirror() const; RawCode to_horizontal_mirror() const;
static bool is_vertical_mirror(RawCode &&raw_code); bool is_vertical_mirror() const; // whether vertically symmetrical
static bool is_horizontal_mirror(RawCode &&raw_code); bool is_horizontal_mirror() const; // whether horizontally symmetrical
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); bool is_vertical_mirror(RawCode &&raw_code) const; // whether vertically symmetric to another
static RawCode to_horizontal_mirror(RawCode &&raw_code); bool is_vertical_mirror(const RawCode &raw_code) const;
static RawCode to_vertical_mirror(const RawCode &raw_code); bool is_horizontal_mirror(RawCode &&raw_code) const; // whether horizontally symmetric to another
static RawCode to_horizontal_mirror(const RawCode &raw_code); bool is_horizontal_mirror(const RawCode &raw_code) const;
}; };
} }

34
src/klotski_core/short_code/convert.cc

@ -10,7 +10,7 @@ using klotski::ShortCode;
using klotski::CommonCode; using klotski::CommonCode;
/// ShortCode to CommonCode /// ShortCode to CommonCode
CommonCode ShortCode::to_common_code() const { // convert to common code CommonCode ShortCode::to_common_code() const {
if (ShortCode::mode() == ShortCode::NORMAL) { if (ShortCode::mode() == ShortCode::NORMAL) {
return CommonCode::unsafe_create(tiny_decode(code)); // normal mode return CommonCode::unsafe_create(tiny_decode(code)); // normal mode
} }
@ -18,10 +18,34 @@ CommonCode ShortCode::to_common_code() const { // convert to common code
} }
/// CommonCode to ShortCode /// CommonCode to ShortCode
ShortCode::ShortCode(CommonCode &&common_code) {
if (ShortCode::mode() == ShortCode::NORMAL) {
code = tiny_encode(common_code.unwrap()); // normal mode
} else {
code = fast_encode(common_code.unwrap()); // fast mode
}
}
ShortCode::ShortCode(const CommonCode &common_code) {
if (ShortCode::mode() == ShortCode::NORMAL) {
code = tiny_encode(common_code.unwrap()); // normal mode
} else {
code = fast_encode(common_code.unwrap()); // fast mode
}
}
ShortCode ShortCode::from_common_code(uint64_t common_code) { ShortCode ShortCode::from_common_code(uint64_t common_code) {
return ShortCode(CommonCode(common_code)); return ShortCode(CommonCode(common_code));
} }
ShortCode ShortCode::from_common_code(CommonCode &&common_code) {
return ShortCode(std::forward<CommonCode>(common_code));
}
ShortCode ShortCode::from_common_code(std::string &&common_code) {
return ShortCode(std::forward<std::string>(common_code));
}
ShortCode ShortCode::from_common_code(const CommonCode &common_code) { ShortCode ShortCode::from_common_code(const CommonCode &common_code) {
return ShortCode(common_code); return ShortCode(common_code);
} }
@ -30,14 +54,6 @@ ShortCode ShortCode::from_common_code(const std::string &common_code) {
return ShortCode(CommonCode(common_code)); return ShortCode(CommonCode(common_code));
} }
ShortCode::ShortCode(const CommonCode &common_code) { // convert from common code
if (ShortCode::mode() == ShortCode::NORMAL) {
code = tiny_encode(common_code.unwrap()); // normal mode
} else {
code = fast_encode(common_code.unwrap()); // fast mode
}
}
/// NOTE: ensure that input common code is valid! /// NOTE: ensure that input common code is valid!
uint32_t ShortCode::fast_encode(uint64_t common_code) { // common code --> short code uint32_t ShortCode::fast_encode(uint64_t common_code) { // common code --> short code
auto head = common_code >> 32; // head index auto head = common_code >> 32; // head index

26
src/klotski_core/short_code/serialize.cc

@ -4,10 +4,7 @@
using klotski::ShortCode; using klotski::ShortCode;
using klotski::ShortCodeException; using klotski::ShortCodeException;
ShortCode ShortCode::from_string(const std::string &short_code) { /// ShortCode to String
return ShortCode(short_code); // convert from string
}
std::string ShortCode::to_string() const { // encode as 5-bits string std::string ShortCode::to_string() const { // encode as 5-bits string
uint32_t short_code = code; uint32_t short_code = code;
char result[6]; // short code length 5 char result[6]; // short code length 5
@ -19,7 +16,24 @@ std::string ShortCode::to_string() const { // encode as 5-bits string
return result; return result;
} }
ShortCode::ShortCode(const std::string &short_code) { // 5-bits string decode /// String to ShortCode
ShortCode::ShortCode(std::string &&short_code) {
code = string_decode(short_code);
}
ShortCode::ShortCode(const std::string &short_code) {
code = string_decode(short_code);
}
ShortCode ShortCode::from_string(std::string &&short_code) {
return ShortCode(short_code);
}
ShortCode ShortCode::from_string(const std::string &short_code) {
return ShortCode(short_code);
}
uint32_t ShortCode::string_decode(const std::string &short_code) { // 5-bits string decode
if (short_code.length() != 5) { // check string length if (short_code.length() != 5) { // check string length
throw ShortCodeException("short code should length 5"); throw ShortCodeException("short code should length 5");
} }
@ -40,5 +54,5 @@ ShortCode::ShortCode(const std::string &short_code) { // 5-bits string decode
if (!ShortCode::check(result)) { // check converted short code if (!ShortCode::check(result)) { // check converted short code
throw ShortCodeException("short code invalid"); throw ShortCodeException("short code invalid");
} }
code = result; // apply convert result return result; // apply convert result
} }

12
src/klotski_core/short_code/short_code.cc

@ -18,6 +18,10 @@ namespace std {
} }
namespace klotski { namespace klotski {
bool ShortCode::operator==(uint32_t short_code) const {
return this->code == short_code;
}
bool ShortCode::operator==(const ShortCode &short_code) const { bool ShortCode::operator==(const ShortCode &short_code) const {
return this->code == short_code.code; return this->code == short_code.code;
} }
@ -51,15 +55,15 @@ namespace klotski {
} }
} }
bool klotski::ShortCode::check(uint32_t short_code) {
return short_code < SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1)
}
using klotski::ShortCode; 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;
bool ShortCode::check(uint32_t short_code) {
return short_code < klotski::SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1)
}
ShortCode::Mode ShortCode::mode() { // ensure speed up enabled and return current mode ShortCode::Mode ShortCode::mode() { // ensure speed up enabled and return current mode
if (fast_mode_available) { if (fast_mode_available) {
return ShortCode::FAST; // fast mode already enabled return ShortCode::FAST; // fast mode already enabled

14
src/klotski_core/short_code/short_code.h

@ -53,11 +53,14 @@
#include <ostream> #include <ostream>
#include <stdexcept> #include <stdexcept>
#include <utility> #include <utility>
#include "all_cases.h"
#include "common_code.h" #include "common_code.h"
namespace klotski { namespace klotski {
class CommonCode; // import for convert interface class CommonCode; // import for convert interface
const uint32_t SHORT_CODE_LIMIT = klotski::ALL_CASES_SIZE_SUM;
class ShortCodeException : public std::runtime_error { class ShortCodeException : public std::runtime_error {
public: public:
ShortCodeException() : std::runtime_error("invalid short code") {} ShortCodeException() : std::runtime_error("invalid short code") {}
@ -73,15 +76,16 @@ namespace klotski {
uint32_t code; uint32_t code;
ShortCode() = default; // unsafe initialize ShortCode() = default; // unsafe initialize
static Mode mode(); static inline Mode mode();
static bool fast_mode_available; static bool fast_mode_available;
static bool normal_mode_available; static bool normal_mode_available;
static uint64_t fast_decode(uint32_t short_code); // short code -> common code 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 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 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 uint32_t tiny_encode(uint64_t common_code); // common code -> short code
static const uint32_t SHORT_CODE_LIMIT = 29334498; static uint32_t string_decode(const std::string &short_code); // string -> short code
public: public:
/// ShortCode validity check /// ShortCode validity check
@ -109,12 +113,6 @@ namespace klotski {
explicit ShortCode(const std::string &short_code); explicit ShortCode(const std::string &short_code);
explicit ShortCode(const CommonCode &common_code); explicit ShortCode(const CommonCode &common_code);
ShortCode(uint32_t short_code, Mode mode) : ShortCode(short_code) { speed_up(mode); }
ShortCode(const std::string &short_code, Mode mode) : ShortCode(short_code) { speed_up(mode); }
ShortCode(const CommonCode &common_code, Mode mode) : ShortCode(common_code) { speed_up(mode); }
ShortCode(std::string &&short_code, Mode mode) : ShortCode(std::forward<std::string>(short_code)) { speed_up(mode); }
ShortCode(CommonCode &&common_code, Mode mode) : ShortCode(std::forward<CommonCode>(common_code)) { speed_up(mode); }
/// Static initialization /// Static 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 unsafe_create(uint32_t short_code);

Loading…
Cancel
Save