Browse Source

update: add `noexcept` flag

legacy
Dnomd343 2 years ago
parent
commit
4403e4e189
  1. 2
      src/klotski_core/all_cases/all_cases.h
  2. 2
      src/klotski_core/all_cases/basic_ranges.h
  3. 10
      src/klotski_core/common_code/common_code.cc
  4. 44
      src/klotski_core/common_code/common_code.h
  5. 20
      src/klotski_core/common_code/convert.cc
  6. 2
      src/klotski_core/common_code/serialize.cc
  7. 12
      src/klotski_core/ffi/all_cases.cc
  8. 64
      src/klotski_core/ffi/codec.cc
  9. 24
      src/klotski_core/ffi/metadata.cc
  10. 116
      src/klotski_core/klotski.h
  11. 18
      src/klotski_core/short_code/convert.cc
  12. 4
      src/klotski_core/short_code/serialize.cc
  13. 10
      src/klotski_core/short_code/short_code.cc
  14. 44
      src/klotski_core/short_code/short_code.h
  15. 4
      src/klotski_core/utils/common.cc
  16. 4
      src/klotski_core/utils/common.h

2
src/klotski_core/all_cases/all_cases.h

@ -45,7 +45,7 @@ namespace klotski {
class AllCases : public BasicRanges { class AllCases : public BasicRanges {
public: public:
static void build(); static void build();
static enum Status status(); static Status status();
static const std::vector<uint32_t> (&fetch())[16]; static const std::vector<uint32_t> (&fetch())[16];
private: private:

2
src/klotski_core/all_cases/basic_ranges.h

@ -42,7 +42,7 @@ namespace klotski {
AVAILABLE, AVAILABLE,
}; };
static void build(); static void build();
static enum Status status(); static Status status();
static const std::vector<uint32_t> &fetch(); static const std::vector<uint32_t> &fetch();
private: private:

10
src/klotski_core/common_code/common_code.cc

@ -20,11 +20,11 @@ namespace std {
} }
namespace klotski { namespace klotski {
bool CommonCode::operator==(const CommonCode &common_code) const { bool CommonCode::operator==(const CommonCode &common_code) const noexcept {
return this->code == common_code.code; return this->code == common_code.code;
} }
bool CommonCode::operator!=(const CommonCode &common_code) const { bool CommonCode::operator!=(const CommonCode &common_code) const noexcept {
return this->code != common_code.code; return this->code != common_code.code;
} }
@ -37,7 +37,7 @@ namespace klotski {
} }
namespace klotski { namespace klotski {
bool CommonCode::valid() const { bool CommonCode::valid() const noexcept {
return CommonCode::check(code); return CommonCode::check(code);
} }
@ -45,7 +45,7 @@ namespace klotski {
return CommonCode(common_code); // create from uint64_t return CommonCode(common_code); // create from uint64_t
} }
CommonCode CommonCode::unsafe_create(uint64_t common_code) { // create without check CommonCode CommonCode::unsafe_create(uint64_t common_code) noexcept { // create without check
auto tmp = CommonCode(); // init directly auto tmp = CommonCode(); // init directly
tmp.code = common_code; tmp.code = common_code;
return tmp; return tmp;
@ -59,7 +59,7 @@ namespace klotski {
} }
} }
bool CommonCode::check(uint64_t common_code) { // whether common code is valid bool CommonCode::check(uint64_t common_code) noexcept { // 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

44
src/klotski_core/common_code/common_code.h

@ -74,54 +74,54 @@ namespace klotski {
CommonCode() = default; // unsafe initialize CommonCode() = default; // unsafe initialize
static inline uint64_t string_decode(const std::string &common_code); static inline uint64_t string_decode(const std::string &common_code);
static inline std::string string_encode(uint64_t common_code, bool shorten); static inline std::string string_encode(uint64_t common_code, bool shorten) noexcept;
public: public:
/// CommonCode validity check /// CommonCode validity check
bool valid() const; bool valid() const noexcept;
static bool check(uint64_t common_code); static bool check(uint64_t common_code) noexcept;
/// Operators of CommonCode /// Operators of CommonCode
bool operator==(const CommonCode &common_code) const; bool operator==(const CommonCode &common_code) const noexcept;
bool operator!=(const CommonCode &common_code) const; bool operator!=(const CommonCode &common_code) const noexcept;
constexpr explicit operator uint64_t() const { return code; } constexpr explicit operator uint64_t() const noexcept { return code; }
friend std::ostream& operator<<(std::ostream &out, const CommonCode &self); friend std::ostream& operator<<(std::ostream &out, const CommonCode &self);
/// Export functions /// Export functions
RawCode to_raw_code() const; RawCode to_raw_code() const noexcept;
ShortCode to_short_code() const; ShortCode to_short_code() const noexcept;
std::string to_string(bool shorten = false) const; std::string to_string(bool shorten = false) const;
constexpr uint64_t unwrap() const { return code; } constexpr uint64_t unwrap() const noexcept { return code; }
/// CommonCode constructors /// CommonCode constructors
explicit CommonCode(uint64_t common_code); explicit CommonCode(uint64_t common_code);
explicit CommonCode(RawCode &&raw_code); explicit CommonCode(RawCode &&raw_code) noexcept;
explicit CommonCode(ShortCode &&short_code); explicit CommonCode(ShortCode &&short_code) noexcept;
explicit CommonCode(std::string &&common_code); explicit CommonCode(std::string &&common_code);
explicit CommonCode(const RawCode &raw_code); explicit CommonCode(const RawCode &raw_code) noexcept;
explicit CommonCode(const ShortCode &short_code); explicit CommonCode(const ShortCode &short_code) noexcept;
explicit CommonCode(const std::string &common_code); explicit CommonCode(const std::string &common_code);
/// Static initialization /// Static initialization
static CommonCode create(uint64_t common_code); static CommonCode create(uint64_t common_code);
static CommonCode unsafe_create(uint64_t common_code); static CommonCode unsafe_create(uint64_t common_code) noexcept;
static CommonCode from_string(std::string &&common_code); static CommonCode from_string(std::string &&common_code);
static CommonCode from_string(const std::string &common_code); static CommonCode from_string(const std::string &common_code);
static CommonCode from_raw_code(uint64_t raw_code); static CommonCode from_raw_code(uint64_t raw_code);
static CommonCode from_raw_code(RawCode &&raw_code); static CommonCode from_raw_code(RawCode &&raw_code) noexcept;
static CommonCode from_raw_code(const RawCode &raw_code); static CommonCode from_raw_code(const RawCode &raw_code) noexcept;
static CommonCode from_short_code(uint32_t short_code); static CommonCode from_short_code(uint32_t short_code);
static CommonCode from_short_code(ShortCode &&short_code); static CommonCode from_short_code(ShortCode &&short_code) noexcept;
static CommonCode from_short_code(std::string &&short_code); static CommonCode from_short_code(std::string &&short_code);
static CommonCode from_short_code(const ShortCode &short_code); static CommonCode from_short_code(const ShortCode &short_code) noexcept;
static CommonCode from_short_code(const std::string &short_code); static CommonCode from_short_code(const std::string &short_code);
}; };
inline bool operator==(uint64_t c1, const CommonCode &c2) { return c1 == c2.unwrap(); } inline bool operator==(uint64_t c1, const CommonCode &c2) noexcept { return c1 == c2.unwrap(); }
inline bool operator!=(uint64_t c1, const CommonCode &c2) { return c1 != c2.unwrap(); } inline bool operator!=(uint64_t c1, const CommonCode &c2) noexcept { return c1 != c2.unwrap(); }
inline bool operator==(const CommonCode &c1, uint64_t c2) { return c1.unwrap() == c2; } inline bool operator==(const CommonCode &c1, uint64_t c2) noexcept { return c1.unwrap() == c2; }
inline bool operator!=(const CommonCode &c1, uint64_t c2) { return c1.unwrap() != c2; } inline bool operator!=(const CommonCode &c1, uint64_t c2) noexcept { return c1.unwrap() != c2; }
} }

20
src/klotski_core/common_code/convert.cc

@ -6,13 +6,13 @@ using klotski::CommonCode;
/// -------------------------- CommonCode to RawCode -------------------------- /// -------------------------- CommonCode to RawCode --------------------------
RawCode CommonCode::to_raw_code() const { RawCode CommonCode::to_raw_code() const noexcept {
return RawCode(*this); // convert to raw code return RawCode(*this); // convert to raw code
} }
/// ------------------------- CommonCode to ShortCode ------------------------- /// ------------------------- CommonCode to ShortCode -------------------------
ShortCode CommonCode::to_short_code() const { ShortCode CommonCode::to_short_code() const noexcept {
return ShortCode(*this); // convert to short code return ShortCode(*this); // convert to short code
} }
@ -22,29 +22,29 @@ CommonCode CommonCode::from_raw_code(uint64_t raw_code) {
return RawCode(raw_code).to_common_code(); return RawCode(raw_code).to_common_code();
} }
CommonCode CommonCode::from_raw_code(RawCode &&raw_code) { CommonCode CommonCode::from_raw_code(RawCode &&raw_code) noexcept {
return raw_code.to_common_code(); return raw_code.to_common_code();
} }
CommonCode CommonCode::from_raw_code(const RawCode &raw_code) { CommonCode CommonCode::from_raw_code(const RawCode &raw_code) noexcept {
return raw_code.to_common_code(); return raw_code.to_common_code();
} }
CommonCode::CommonCode(RawCode &&raw_code) { CommonCode::CommonCode(RawCode &&raw_code) noexcept {
code = raw_code.to_common_code().code; // convert from raw code code = raw_code.to_common_code().code; // convert from raw code
} }
CommonCode::CommonCode(const RawCode &raw_code) { CommonCode::CommonCode(const RawCode &raw_code) noexcept {
code = raw_code.to_common_code().code; // convert from raw code code = raw_code.to_common_code().code; // convert from raw code
} }
/// ------------------------- ShortCode to CommonCode ------------------------- /// ------------------------- ShortCode to CommonCode -------------------------
CommonCode::CommonCode(ShortCode &&short_code) { CommonCode::CommonCode(ShortCode &&short_code) noexcept {
code = short_code.to_common_code().code; // convert from short code code = short_code.to_common_code().code; // convert from short code
} }
CommonCode::CommonCode(const ShortCode &short_code) { CommonCode::CommonCode(const ShortCode &short_code) noexcept {
code = short_code.to_common_code().code; // convert from short code code = short_code.to_common_code().code; // convert from short code
} }
@ -52,7 +52,7 @@ CommonCode CommonCode::from_short_code(uint32_t short_code) {
return ShortCode(short_code).to_common_code(); return ShortCode(short_code).to_common_code();
} }
CommonCode CommonCode::from_short_code(ShortCode &&short_code) { CommonCode CommonCode::from_short_code(ShortCode &&short_code) noexcept {
return short_code.to_common_code(); return short_code.to_common_code();
} }
@ -60,7 +60,7 @@ CommonCode CommonCode::from_short_code(std::string &&short_code) {
return ShortCode(std::forward<std::string>(short_code)).to_common_code(); return ShortCode(std::forward<std::string>(short_code)).to_common_code();
} }
CommonCode CommonCode::from_short_code(const ShortCode &short_code) { CommonCode CommonCode::from_short_code(const ShortCode &short_code) noexcept {
return short_code.to_common_code(); return short_code.to_common_code();
} }

2
src/klotski_core/common_code/serialize.cc

@ -44,7 +44,7 @@ inline uint8_t last_zero_num(uint32_t bin) { // get last zero number
return binary_count(bin >> 1); return binary_count(bin >> 1);
} }
std::string CommonCode::string_encode(uint64_t common_code, bool shorten) { // convert uint64_t code to string std::string CommonCode::string_encode(uint64_t common_code, bool shorten) noexcept { // convert uint64_t code to string
char result[10]; // max length 9-bits char result[10]; // max length 9-bits
sprintf(result, "%09lX", common_code); sprintf(result, "%09lX", common_code);
if (shorten) { // remove `0` after common code if (shorten) { // remove `0` after common code

12
src/klotski_core/ffi/all_cases.cc

@ -8,23 +8,23 @@ using klotski::BasicRanges;
const uint32_t ALL_CASES_SIZE = klotski::ALL_CASES_SIZE_SUM; const uint32_t ALL_CASES_SIZE = klotski::ALL_CASES_SIZE_SUM;
const uint32_t BASIC_RANGES_SIZE = klotski::BASIC_RANGES_SIZE; const uint32_t BASIC_RANGES_SIZE = klotski::BASIC_RANGES_SIZE;
void all_cases_build() { void all_cases_build() noexcept {
AllCases::build(); AllCases::build();
} }
void basic_ranges_build() { void basic_ranges_build() noexcept {
BasicRanges::build(); BasicRanges::build();
} }
bool is_all_cases_available() { bool is_all_cases_available() noexcept {
return AllCases::status() == AllCases::AVAILABLE; return AllCases::status() == AllCases::AVAILABLE;
} }
bool is_basic_ranges_available() { bool is_basic_ranges_available() noexcept {
return BasicRanges::status() == BasicRanges::AVAILABLE; return BasicRanges::status() == BasicRanges::AVAILABLE;
} }
void export_all_cases(uint64_t *buffer) { void export_all_cases(uint64_t *buffer) noexcept {
for (uint64_t head = 0; head < 16; ++head) { for (uint64_t head = 0; head < 16; ++head) {
for (const auto &range : AllCases::fetch()[head]) { for (const auto &range : AllCases::fetch()[head]) {
*(buffer++) = head << 32 | range; *(buffer++) = head << 32 | range;
@ -32,7 +32,7 @@ void export_all_cases(uint64_t *buffer) {
} }
} }
void export_basic_ranges(uint32_t *buffer) { void export_basic_ranges(uint32_t *buffer) noexcept {
auto basic_ranges_ptr = &*BasicRanges::fetch().begin(); auto basic_ranges_ptr = &*BasicRanges::fetch().begin();
memcpy(buffer, basic_ranges_ptr, BASIC_RANGES_SIZE * 4); // 32-bits -> 4-bytes memcpy(buffer, basic_ranges_ptr, BASIC_RANGES_SIZE * 4); // 32-bits -> 4-bytes
} }

64
src/klotski_core/ffi/codec.cc

@ -12,15 +12,15 @@ using klotski::BasicRanges;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void short_code_enable() { void short_code_enable() noexcept {
ShortCode::speed_up(ShortCode::NORMAL); ShortCode::speed_up(ShortCode::NORMAL);
} }
void short_code_enable_fast() { void short_code_enable_fast() noexcept {
ShortCode::speed_up(ShortCode::FAST); ShortCode::speed_up(ShortCode::FAST);
} }
bool is_short_code_available() { bool is_short_code_available() noexcept {
if (BasicRanges::status() != BasicRanges::AVAILABLE) { if (BasicRanges::status() != BasicRanges::AVAILABLE) {
return false; return false;
} }
@ -28,7 +28,7 @@ bool is_short_code_available() {
return true; return true;
} }
bool is_short_code_available_fast() { bool is_short_code_available_fast() noexcept {
if (AllCases::status() != AllCases::AVAILABLE) { if (AllCases::status() != AllCases::AVAILABLE) {
return false; return false;
} }
@ -38,21 +38,21 @@ bool is_short_code_available_fast() {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
bool raw_code_check(uint64_t raw_code) { bool raw_code_check(uint64_t raw_code) noexcept {
return RawCode::check(raw_code); return RawCode::check(raw_code);
} }
bool short_code_check(uint32_t short_code) { bool short_code_check(uint32_t short_code) noexcept {
return ShortCode::check(short_code); return ShortCode::check(short_code);
} }
bool common_code_check(uint64_t common_code) { bool common_code_check(uint64_t common_code) noexcept {
return CommonCode::check(common_code); return CommonCode::check(common_code);
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
bool raw_code_to_short_code(uint64_t raw_code, uint32_t *short_code) { bool raw_code_to_short_code(uint64_t raw_code, uint32_t *short_code) noexcept {
if (!RawCode::check(raw_code)) { if (!RawCode::check(raw_code)) {
return false; return false;
} }
@ -62,7 +62,7 @@ bool raw_code_to_short_code(uint64_t raw_code, uint32_t *short_code) {
return true; return true;
} }
bool short_code_to_raw_code(uint32_t short_code, uint64_t *raw_code) { bool short_code_to_raw_code(uint32_t short_code, uint64_t *raw_code) noexcept {
if (!ShortCode::check(short_code)) { if (!ShortCode::check(short_code)) {
return false; return false;
} }
@ -72,7 +72,7 @@ bool short_code_to_raw_code(uint32_t short_code, uint64_t *raw_code) {
return true; return true;
} }
bool raw_code_to_common_code(uint64_t raw_code, uint64_t *common_code) { bool raw_code_to_common_code(uint64_t raw_code, uint64_t *common_code) noexcept {
if (!RawCode::check(raw_code)) { if (!RawCode::check(raw_code)) {
return false; return false;
} }
@ -82,7 +82,7 @@ bool raw_code_to_common_code(uint64_t raw_code, uint64_t *common_code) {
return true; return true;
} }
bool common_code_to_raw_code(uint64_t common_code, uint64_t *raw_code) { bool common_code_to_raw_code(uint64_t common_code, uint64_t *raw_code) noexcept {
if (!CommonCode::check(common_code)) { if (!CommonCode::check(common_code)) {
return false; return false;
} }
@ -92,7 +92,7 @@ bool common_code_to_raw_code(uint64_t common_code, uint64_t *raw_code) {
return true; return true;
} }
bool short_code_to_common_code(uint32_t short_code, uint64_t *common_code) { bool short_code_to_common_code(uint32_t short_code, uint64_t *common_code) noexcept {
if (!ShortCode::check(short_code)) { if (!ShortCode::check(short_code)) {
return false; return false;
} }
@ -102,7 +102,7 @@ bool short_code_to_common_code(uint32_t short_code, uint64_t *common_code) {
return true; return true;
} }
bool common_code_to_short_code(uint64_t common_code, uint32_t *short_code) { bool common_code_to_short_code(uint64_t common_code, uint32_t *short_code) noexcept {
if (!CommonCode::check(common_code)) { if (!CommonCode::check(common_code)) {
return false; return false;
} }
@ -114,35 +114,35 @@ bool common_code_to_short_code(uint64_t common_code, uint32_t *short_code) {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
uint32_t raw_code_to_short_code_unsafe(uint64_t raw_code) { uint32_t raw_code_to_short_code_unsafe(uint64_t raw_code) noexcept {
return RawCode::unsafe_create(raw_code) return RawCode::unsafe_create(raw_code)
.to_common_code().to_short_code().unwrap(); .to_common_code().to_short_code().unwrap();
} }
uint64_t short_code_to_raw_code_unsafe(uint32_t short_code) { uint64_t short_code_to_raw_code_unsafe(uint32_t short_code) noexcept {
return ShortCode::unsafe_create(short_code) return ShortCode::unsafe_create(short_code)
.to_common_code().to_raw_code().unwrap(); .to_common_code().to_raw_code().unwrap();
} }
uint64_t raw_code_to_common_code_unsafe(uint64_t raw_code) { uint64_t raw_code_to_common_code_unsafe(uint64_t raw_code) noexcept {
return RawCode::unsafe_create(raw_code).to_common_code().unwrap(); return RawCode::unsafe_create(raw_code).to_common_code().unwrap();
} }
uint64_t common_code_to_raw_code_unsafe(uint64_t common_code) { uint64_t common_code_to_raw_code_unsafe(uint64_t common_code) noexcept {
return CommonCode::unsafe_create(common_code).to_raw_code().unwrap(); return CommonCode::unsafe_create(common_code).to_raw_code().unwrap();
} }
uint64_t short_code_to_common_code_unsafe(uint32_t short_code) { uint64_t short_code_to_common_code_unsafe(uint32_t short_code) noexcept {
return ShortCode::unsafe_create(short_code).to_common_code().unwrap(); return ShortCode::unsafe_create(short_code).to_common_code().unwrap();
} }
uint32_t common_code_to_short_code_unsafe(uint64_t common_code) { uint32_t common_code_to_short_code_unsafe(uint64_t common_code) noexcept {
return CommonCode::unsafe_create(common_code).to_short_code().unwrap(); return CommonCode::unsafe_create(common_code).to_short_code().unwrap();
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
bool is_vertical_mirror(uint64_t raw_code, bool *result) { bool is_vertical_mirror(uint64_t raw_code, bool *result) noexcept {
if (!RawCode::check(raw_code)) { if (!RawCode::check(raw_code)) {
return false; return false;
} }
@ -150,7 +150,7 @@ bool is_vertical_mirror(uint64_t raw_code, bool *result) {
return true; return true;
} }
bool is_horizontal_mirror(uint64_t raw_code, bool *result) { bool is_horizontal_mirror(uint64_t raw_code, bool *result) noexcept {
if (!RawCode::check(raw_code)) { if (!RawCode::check(raw_code)) {
return false; return false;
} }
@ -158,7 +158,7 @@ bool is_horizontal_mirror(uint64_t raw_code, bool *result) {
return true; return true;
} }
bool to_vertical_mirror(uint64_t raw_code, uint64_t *result) { bool to_vertical_mirror(uint64_t raw_code, uint64_t *result) noexcept {
if (!RawCode::check(raw_code)) { if (!RawCode::check(raw_code)) {
return false; return false;
} }
@ -166,7 +166,7 @@ bool to_vertical_mirror(uint64_t raw_code, uint64_t *result) {
return true; return true;
} }
bool to_horizontal_mirror(uint64_t raw_code, uint64_t *result) { bool to_horizontal_mirror(uint64_t raw_code, uint64_t *result) noexcept {
if (!RawCode::check(raw_code)) { if (!RawCode::check(raw_code)) {
return false; return false;
} }
@ -176,19 +176,19 @@ bool to_horizontal_mirror(uint64_t raw_code, uint64_t *result) {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
bool is_vertical_mirror_unsafe(uint64_t raw_code) { bool is_vertical_mirror_unsafe(uint64_t raw_code) noexcept {
return RawCode::unsafe_create(raw_code).is_vertical_mirror(); return RawCode::unsafe_create(raw_code).is_vertical_mirror();
} }
bool is_horizontal_mirror_unsafe(uint64_t raw_code) { bool is_horizontal_mirror_unsafe(uint64_t raw_code) noexcept {
return RawCode::unsafe_create(raw_code).is_horizontal_mirror(); return RawCode::unsafe_create(raw_code).is_horizontal_mirror();
} }
uint64_t to_vertical_mirror_unsafe(uint64_t raw_code) { uint64_t to_vertical_mirror_unsafe(uint64_t raw_code) noexcept {
return RawCode::unsafe_create(raw_code).to_vertical_mirror().unwrap(); return RawCode::unsafe_create(raw_code).to_vertical_mirror().unwrap();
} }
uint64_t to_horizontal_mirror_unsafe(uint64_t raw_code) { uint64_t to_horizontal_mirror_unsafe(uint64_t raw_code) noexcept {
return RawCode::unsafe_create(raw_code).to_horizontal_mirror().unwrap(); return RawCode::unsafe_create(raw_code).to_horizontal_mirror().unwrap();
} }
@ -196,7 +196,7 @@ uint64_t to_horizontal_mirror_unsafe(uint64_t raw_code) {
const uint32_t SHORT_CODE_STR_SIZE = 6; const uint32_t SHORT_CODE_STR_SIZE = 6;
bool short_code_to_string(uint32_t short_code, char short_code_str[]) { bool short_code_to_string(uint32_t short_code, char short_code_str[]) noexcept {
if (!ShortCode::check(short_code)) { if (!ShortCode::check(short_code)) {
return false; return false;
} }
@ -205,7 +205,7 @@ bool short_code_to_string(uint32_t short_code, char short_code_str[]) {
return true; return true;
} }
bool short_code_from_string(const char short_code_str[], uint32_t *short_code) { bool short_code_from_string(const char short_code_str[], uint32_t *short_code) noexcept {
try { try {
*short_code = ShortCode::from_string(short_code_str).unwrap(); *short_code = ShortCode::from_string(short_code_str).unwrap();
} catch (...) { } catch (...) {
@ -218,7 +218,7 @@ bool short_code_from_string(const char short_code_str[], uint32_t *short_code) {
const uint32_t COMMON_CODE_STR_SIZE = 10; const uint32_t COMMON_CODE_STR_SIZE = 10;
bool common_code_to_string(uint64_t common_code, char common_code_str[]) { bool common_code_to_string(uint64_t common_code, char common_code_str[]) noexcept {
if (!CommonCode::check(common_code)) { if (!CommonCode::check(common_code)) {
return false; return false;
} }
@ -227,7 +227,7 @@ bool common_code_to_string(uint64_t common_code, char common_code_str[]) {
return true; return true;
} }
bool common_code_to_string_shorten(uint64_t common_code, char common_code_str[]) { bool common_code_to_string_shorten(uint64_t common_code, char common_code_str[]) noexcept {
if (!CommonCode::check(common_code)) { if (!CommonCode::check(common_code)) {
return false; return false;
} }
@ -236,7 +236,7 @@ bool common_code_to_string_shorten(uint64_t common_code, char common_code_str[])
return true; return true;
} }
bool common_code_from_string(const char common_code_str[], uint64_t *common_code) { bool common_code_from_string(const char common_code_str[], uint64_t *common_code) noexcept {
try { try {
*common_code = CommonCode::from_string(common_code_str).unwrap(); *common_code = CommonCode::from_string(common_code_str).unwrap();
} catch (...) { } catch (...) {

24
src/klotski_core/ffi/metadata.cc

@ -1,50 +1,50 @@
#include "klotski.h" #include "klotski.h"
#include "metadata.h" #include "metadata.h"
uint32_t get_version_major() { uint32_t get_version_major() noexcept {
return VERSION_MAJOR; return VERSION_MAJOR;
} }
uint32_t get_version_minor() { uint32_t get_version_minor() noexcept {
return VERSION_MINOR; return VERSION_MINOR;
} }
uint32_t get_version_patch() { uint32_t get_version_patch() noexcept {
return VERSION_PATCH; return VERSION_PATCH;
} }
const char* get_author() { const char* get_author() noexcept {
return AUTHOR; return AUTHOR;
} }
const char* get_git_tag() { const char* get_git_tag() noexcept {
return GIT_TAG_ID; return GIT_TAG_ID;
} }
const char* get_version() { const char* get_version() noexcept {
return VERSION_STR; return VERSION_STR;
} }
const char* get_commit_id() { const char* get_commit_id() noexcept {
return GIT_COMMIT_ID; return GIT_COMMIT_ID;
} }
const char* get_build_time() { const char* get_build_time() noexcept {
return BUILD_TIME; return BUILD_TIME;
} }
const char* get_git_branch() { const char* get_git_branch() noexcept {
return GIT_BRANCH; return GIT_BRANCH;
} }
const char* get_project_url() { const char* get_project_url() noexcept {
return GIT_PROJECT; return GIT_PROJECT;
} }
const char* get_system_info() { const char* get_system_info() noexcept {
return SYSTEM; return SYSTEM;
} }
const char* get_compiler_info() { const char* get_compiler_info() noexcept {
return COMPILER; return COMPILER;
} }

116
src/klotski_core/klotski.h

@ -2,6 +2,7 @@
#ifdef __cplusplus #ifdef __cplusplus
#include <cstdint> #include <cstdint>
#define NOEXCEPT noexcept
#else #else
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
@ -14,7 +15,7 @@
extern "C" { extern "C" {
#endif #endif
// TODO: remove after test // TODO: remove after test
extern void tmain(); EXTERN_FUNC void tmain();
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
@ -24,20 +25,19 @@ extern "C" {
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
// version -> major.minor.patch EXTERN_FUNC uint32_t get_version_major() NOEXCEPT;
extern uint32_t get_version_major(); EXTERN_FUNC uint32_t get_version_minor() NOEXCEPT;
extern uint32_t get_version_minor(); EXTERN_FUNC uint32_t get_version_patch() NOEXCEPT;
extern uint32_t get_version_patch();
EXTERN_FUNC const char* get_author() NOEXCEPT;
extern const char* get_author(); EXTERN_FUNC const char* get_git_tag() NOEXCEPT;
extern const char* get_git_tag(); EXTERN_FUNC const char* get_version() NOEXCEPT;
extern const char* get_version(); EXTERN_FUNC const char* get_commit_id() NOEXCEPT;
extern const char* get_commit_id(); EXTERN_FUNC const char* get_build_time() NOEXCEPT;
extern const char* get_build_time(); EXTERN_FUNC const char* get_git_branch() NOEXCEPT;
extern const char* get_git_branch(); EXTERN_FUNC const char* get_project_url() NOEXCEPT;
extern const char* get_project_url(); EXTERN_FUNC const char* get_system_info() NOEXCEPT;
extern const char* get_system_info(); EXTERN_FUNC const char* get_compiler_info() NOEXCEPT;
extern const char* get_compiler_info();
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
@ -47,16 +47,16 @@ extern "C" {
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
extern void all_cases_build(); EXTERN_FUNC void all_cases_build() NOEXCEPT;
extern void basic_ranges_build(); EXTERN_FUNC void basic_ranges_build() NOEXCEPT;
extern bool is_all_cases_available(); EXTERN_FUNC bool is_all_cases_available() NOEXCEPT;
extern bool is_basic_ranges_available(); EXTERN_FUNC bool is_basic_ranges_available() NOEXCEPT;
extern const uint32_t ALL_CASES_SIZE; extern const uint32_t ALL_CASES_SIZE;
extern void export_all_cases(uint64_t *buffer); EXTERN_FUNC void export_all_cases(uint64_t *buffer) NOEXCEPT;
extern const uint32_t BASIC_RANGES_SIZE; extern const uint32_t BASIC_RANGES_SIZE;
extern void export_basic_ranges(uint32_t *buffer); EXTERN_FUNC void export_basic_ranges(uint32_t *buffer) NOEXCEPT;
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
@ -66,47 +66,47 @@ extern "C" {
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
EXTERN_FUNC void short_code_enable(); EXTERN_FUNC void short_code_enable() NOEXCEPT;
EXTERN_FUNC void short_code_enable_fast(); EXTERN_FUNC void short_code_enable_fast() NOEXCEPT;
EXTERN_FUNC bool is_short_code_available(); EXTERN_FUNC bool is_short_code_available() NOEXCEPT;
EXTERN_FUNC bool is_short_code_available_fast(); EXTERN_FUNC bool is_short_code_available_fast() NOEXCEPT;
EXTERN_FUNC bool raw_code_check(uint64_t raw_code); EXTERN_FUNC bool raw_code_check(uint64_t raw_code) NOEXCEPT;
EXTERN_FUNC bool short_code_check(uint32_t short_code); EXTERN_FUNC bool short_code_check(uint32_t short_code) NOEXCEPT;
EXTERN_FUNC bool common_code_check(uint64_t common_code); EXTERN_FUNC bool common_code_check(uint64_t common_code) NOEXCEPT;
EXTERN_FUNC bool raw_code_to_short_code(uint64_t raw_code, uint32_t *short_code); EXTERN_FUNC bool raw_code_to_short_code(uint64_t raw_code, uint32_t *short_code) NOEXCEPT;
EXTERN_FUNC bool short_code_to_raw_code(uint32_t short_code, uint64_t *raw_code); EXTERN_FUNC bool short_code_to_raw_code(uint32_t short_code, uint64_t *raw_code) NOEXCEPT;
EXTERN_FUNC bool raw_code_to_common_code(uint64_t raw_code, uint64_t *common_code); EXTERN_FUNC bool raw_code_to_common_code(uint64_t raw_code, uint64_t *common_code) NOEXCEPT;
EXTERN_FUNC bool common_code_to_raw_code(uint64_t common_code, uint64_t *raw_code); EXTERN_FUNC bool common_code_to_raw_code(uint64_t common_code, uint64_t *raw_code) NOEXCEPT;
EXTERN_FUNC bool short_code_to_common_code(uint32_t short_code, uint64_t *common_code); EXTERN_FUNC bool short_code_to_common_code(uint32_t short_code, uint64_t *common_code) NOEXCEPT;
EXTERN_FUNC bool common_code_to_short_code(uint64_t common_code, uint32_t *short_code); EXTERN_FUNC bool common_code_to_short_code(uint64_t common_code, uint32_t *short_code) NOEXCEPT;
EXTERN_FUNC uint32_t raw_code_to_short_code_unsafe(uint64_t raw_code); EXTERN_FUNC uint32_t raw_code_to_short_code_unsafe(uint64_t raw_code) NOEXCEPT;
EXTERN_FUNC uint64_t short_code_to_raw_code_unsafe(uint32_t short_code); EXTERN_FUNC uint64_t short_code_to_raw_code_unsafe(uint32_t short_code) NOEXCEPT;
EXTERN_FUNC uint64_t raw_code_to_common_code_unsafe(uint64_t raw_code); EXTERN_FUNC uint64_t raw_code_to_common_code_unsafe(uint64_t raw_code) NOEXCEPT;
EXTERN_FUNC uint64_t common_code_to_raw_code_unsafe(uint64_t common_code); EXTERN_FUNC uint64_t common_code_to_raw_code_unsafe(uint64_t common_code) NOEXCEPT;
EXTERN_FUNC uint64_t short_code_to_common_code_unsafe(uint32_t short_code); EXTERN_FUNC uint64_t short_code_to_common_code_unsafe(uint32_t short_code) NOEXCEPT;
EXTERN_FUNC uint32_t common_code_to_short_code_unsafe(uint64_t common_code); EXTERN_FUNC uint32_t common_code_to_short_code_unsafe(uint64_t common_code) NOEXCEPT;
EXTERN_FUNC bool is_vertical_mirror(uint64_t raw_code, bool *result); EXTERN_FUNC bool is_vertical_mirror(uint64_t raw_code, bool *result) NOEXCEPT;
EXTERN_FUNC bool is_horizontal_mirror(uint64_t raw_code, bool *result); EXTERN_FUNC bool is_horizontal_mirror(uint64_t raw_code, bool *result) NOEXCEPT;
EXTERN_FUNC bool to_vertical_mirror(uint64_t raw_code, uint64_t *result); EXTERN_FUNC bool to_vertical_mirror(uint64_t raw_code, uint64_t *result) NOEXCEPT;
EXTERN_FUNC bool to_horizontal_mirror(uint64_t raw_code, uint64_t *result); EXTERN_FUNC bool to_horizontal_mirror(uint64_t raw_code, uint64_t *result) NOEXCEPT;
EXTERN_FUNC bool is_vertical_mirror_unsafe(uint64_t raw_code); EXTERN_FUNC bool is_vertical_mirror_unsafe(uint64_t raw_code) NOEXCEPT;
EXTERN_FUNC bool is_horizontal_mirror_unsafe(uint64_t raw_code); EXTERN_FUNC bool is_horizontal_mirror_unsafe(uint64_t raw_code) NOEXCEPT;
EXTERN_FUNC uint64_t to_vertical_mirror_unsafe(uint64_t raw_code); EXTERN_FUNC uint64_t to_vertical_mirror_unsafe(uint64_t raw_code) NOEXCEPT;
EXTERN_FUNC uint64_t to_horizontal_mirror_unsafe(uint64_t raw_code); EXTERN_FUNC uint64_t to_horizontal_mirror_unsafe(uint64_t raw_code) NOEXCEPT;
extern const uint32_t SHORT_CODE_STR_SIZE; extern const uint32_t SHORT_CODE_STR_SIZE;
EXTERN_FUNC bool short_code_to_string(uint32_t short_code, char short_code_str[]); EXTERN_FUNC bool short_code_to_string(uint32_t short_code, char short_code_str[]) NOEXCEPT;
EXTERN_FUNC bool short_code_from_string(const char short_code_str[], uint32_t *short_code); EXTERN_FUNC bool short_code_from_string(const char short_code_str[], uint32_t *short_code) NOEXCEPT;
extern const uint32_t COMMON_CODE_STR_SIZE; extern const uint32_t COMMON_CODE_STR_SIZE;
EXTERN_FUNC bool common_code_to_string(uint64_t common_code, char common_code_str[]); EXTERN_FUNC bool common_code_to_string(uint64_t common_code, char common_code_str[]) NOEXCEPT;
EXTERN_FUNC bool common_code_to_string_shorten(uint64_t common_code, char common_code_str[]); EXTERN_FUNC bool common_code_to_string_shorten(uint64_t common_code, char common_code_str[]) NOEXCEPT;
EXTERN_FUNC bool common_code_from_string(const char common_code_str[], uint64_t *common_code); EXTERN_FUNC bool common_code_from_string(const char common_code_str[], uint64_t *common_code) NOEXCEPT;
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

18
src/klotski_core/short_code/convert.cc

@ -10,7 +10,7 @@ using klotski::CommonCode;
/// ------------------------- ShortCode to CommonCode ------------------------- /// ------------------------- ShortCode to CommonCode -------------------------
CommonCode ShortCode::to_common_code() const { CommonCode ShortCode::to_common_code() const noexcept {
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
} }
@ -19,7 +19,7 @@ CommonCode ShortCode::to_common_code() const {
/// ------------------------- ShortCode to CommonCode ------------------------- /// ------------------------- ShortCode to CommonCode -------------------------
ShortCode::ShortCode(CommonCode &&common_code) { ShortCode::ShortCode(CommonCode &&common_code) noexcept {
if (ShortCode::mode() == ShortCode::NORMAL) { if (ShortCode::mode() == ShortCode::NORMAL) {
code = tiny_encode(common_code.unwrap()); // normal mode code = tiny_encode(common_code.unwrap()); // normal mode
} else { } else {
@ -27,7 +27,7 @@ ShortCode::ShortCode(CommonCode &&common_code) {
} }
} }
ShortCode::ShortCode(const CommonCode &common_code) { ShortCode::ShortCode(const CommonCode &common_code) noexcept {
if (ShortCode::mode() == ShortCode::NORMAL) { if (ShortCode::mode() == ShortCode::NORMAL) {
code = tiny_encode(common_code.unwrap()); // normal mode code = tiny_encode(common_code.unwrap()); // normal mode
} else { } else {
@ -39,7 +39,7 @@ 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) { ShortCode ShortCode::from_common_code(CommonCode &&common_code) noexcept {
return ShortCode(std::forward<CommonCode>(common_code)); return ShortCode(std::forward<CommonCode>(common_code));
} }
@ -49,7 +49,7 @@ ShortCode ShortCode::from_common_code(std::string &&common_code) {
)); ));
} }
ShortCode ShortCode::from_common_code(const CommonCode &common_code) { ShortCode ShortCode::from_common_code(const CommonCode &common_code) noexcept {
return ShortCode(common_code); return ShortCode(common_code);
} }
@ -60,7 +60,7 @@ ShortCode ShortCode::from_common_code(const std::string &common_code) {
/// ----------------------------- Basic Functions ----------------------------- /// ----------------------------- Basic Functions -----------------------------
/// 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) noexcept { // common code --> short code
auto head = common_code >> 32; // head index auto head = common_code >> 32; // head index
const auto &ranges = AllCases::fetch()[head]; // available ranges const auto &ranges = AllCases::fetch()[head]; // available ranges
auto offset = std::lower_bound(ranges.begin(), ranges.end(), (uint32_t)common_code) - ranges.begin(); auto offset = std::lower_bound(ranges.begin(), ranges.end(), (uint32_t)common_code) - ranges.begin();
@ -68,7 +68,7 @@ uint32_t ShortCode::fast_encode(uint64_t common_code) { // common code --> short
} }
/// NOTE: ensure that input short code is valid! /// NOTE: ensure that input short code is valid!
uint64_t ShortCode::fast_decode(uint32_t short_code) { // short code --> common code uint64_t ShortCode::fast_decode(uint32_t short_code) noexcept { // short code --> common code
auto offset = std::upper_bound( // using binary search auto offset = std::upper_bound( // using binary search
ALL_CASES_OFFSET, ALL_CASES_OFFSET + 16, short_code ALL_CASES_OFFSET, ALL_CASES_OFFSET + 16, short_code
) - 1; ) - 1;
@ -77,7 +77,7 @@ uint64_t ShortCode::fast_decode(uint32_t short_code) { // short code --> common
} }
/// NOTE: ensure that input common code is valid! /// NOTE: ensure that input common code is valid!
uint32_t ShortCode::tiny_encode(uint64_t common_code) { // common code --> short code uint32_t ShortCode::tiny_encode(uint64_t common_code) noexcept { // common code --> short code
/// load head index and range prefix /// load head index and range prefix
uint32_t head = common_code >> 32; uint32_t head = common_code >> 32;
uint32_t prefix = (common_code >> 20) & 0xFFF; uint32_t prefix = (common_code >> 20) & 0xFFF;
@ -105,7 +105,7 @@ uint32_t ShortCode::tiny_encode(uint64_t common_code) { // common code --> short
} }
/// NOTE: ensure that input short code is valid! /// NOTE: ensure that input short code is valid!
uint64_t ShortCode::tiny_decode(uint32_t short_code) { // short code --> common code uint64_t ShortCode::tiny_decode(uint32_t short_code) noexcept { // short code --> common code
/// match head index /// match head index
auto offset = std::upper_bound( // binary search auto offset = std::upper_bound( // binary search
ALL_CASES_OFFSET, ALL_CASES_OFFSET + 16, short_code ALL_CASES_OFFSET, ALL_CASES_OFFSET + 16, short_code

4
src/klotski_core/short_code/serialize.cc

@ -6,7 +6,7 @@ using klotski::ShortCodeException;
/// --------------------------- ShortCode to String --------------------------- /// --------------------------- ShortCode to String ---------------------------
std::string ShortCode::to_string() const { // encode as 5-bits string std::string ShortCode::to_string() const noexcept { // encode as 5-bits string
return string_encode(code); return string_encode(code);
} }
@ -30,7 +30,7 @@ ShortCode ShortCode::from_string(const std::string &short_code) {
/// ----------------------------- Basic Functions ----------------------------- /// ----------------------------- Basic Functions -----------------------------
std::string klotski::ShortCode::string_encode(uint32_t short_code) { // encode as 5-bits string std::string ShortCode::string_encode(uint32_t short_code) noexcept { // encode as 5-bits string
char result[6]; // short code length 5 char result[6]; // short code length 5
result[5] = '\0'; // string ending flag result[5] = '\0'; // string ending flag
for (int n = 0; n < 5; ++n) { for (int n = 0; n < 5; ++n) {

10
src/klotski_core/short_code/short_code.cc

@ -23,11 +23,11 @@ namespace std {
} }
namespace klotski { namespace klotski {
bool ShortCode::operator==(const ShortCode &short_code) const { bool ShortCode::operator==(const ShortCode &short_code) const noexcept {
return this->code == short_code.code; return this->code == short_code.code;
} }
bool ShortCode::operator!=(const ShortCode &short_code) const { bool ShortCode::operator!=(const ShortCode &short_code) const noexcept {
return this->code != short_code.code; return this->code != short_code.code;
} }
@ -38,7 +38,7 @@ namespace klotski {
} }
namespace klotski { namespace klotski {
bool ShortCode::valid() const { bool ShortCode::valid() const noexcept {
return ShortCode::check(code); return ShortCode::check(code);
} }
@ -46,7 +46,7 @@ namespace klotski {
return ShortCode(short_code); return ShortCode(short_code);
} }
ShortCode ShortCode::unsafe_create(uint32_t short_code) { // create without check ShortCode ShortCode::unsafe_create(uint32_t short_code) noexcept { // create without check
auto tmp = ShortCode(); // init directly auto tmp = ShortCode(); // init directly
tmp.code = short_code; tmp.code = short_code;
return tmp; return tmp;
@ -60,7 +60,7 @@ namespace klotski {
} }
} }
bool ShortCode::check(uint32_t short_code) { bool ShortCode::check(uint32_t short_code) noexcept {
return short_code < klotski::SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1) return short_code < klotski::SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1)
} }

44
src/klotski_core/short_code/short_code.h

@ -80,56 +80,56 @@ namespace klotski {
static bool fast_mode_available; static bool fast_mode_available;
static bool normal_mode_available; static bool normal_mode_available;
static inline uint64_t fast_decode(uint32_t short_code); // short code -> common code static inline uint64_t fast_decode(uint32_t short_code) noexcept; // short code -> common code
static inline uint32_t fast_encode(uint64_t common_code); // common code -> short code static inline uint32_t fast_encode(uint64_t common_code) noexcept; // common code -> short code
static inline uint64_t tiny_decode(uint32_t short_code); // short code -> common code static inline uint64_t tiny_decode(uint32_t short_code) noexcept; // short code -> common code
static inline uint32_t tiny_encode(uint64_t common_code); // common code -> short code static inline uint32_t tiny_encode(uint64_t common_code) noexcept; // common code -> short code
static inline std::string string_encode(uint32_t short_code); // short code -> string static inline std::string string_encode(uint32_t short_code) noexcept; // short code -> string
static inline uint32_t string_decode(const std::string &short_code); // string -> short code static inline uint32_t string_decode(const std::string &short_code); // string -> short code
public: public:
/// ShortCode validity check /// ShortCode validity check
bool valid() const; bool valid() const noexcept;
static bool check(uint32_t short_code); static bool check(uint32_t short_code) noexcept;
/// ShortCode convert mode /// ShortCode convert mode
static void speed_up(Mode mode); static void speed_up(Mode mode);
/// Operators of ShortCode /// Operators of ShortCode
bool operator==(const ShortCode &short_code) const; bool operator==(const ShortCode &short_code) const noexcept;
bool operator!=(const ShortCode &short_code) const; bool operator!=(const ShortCode &short_code) const noexcept;
constexpr explicit operator uint32_t() const { return code; } constexpr explicit operator uint32_t() const noexcept { return code; }
friend std::ostream& operator<<(std::ostream &out, const ShortCode &self); friend std::ostream& operator<<(std::ostream &out, const ShortCode &self);
/// Export functions /// Export functions
std::string to_string() const; std::string to_string() const noexcept;
CommonCode to_common_code() const; CommonCode to_common_code() const noexcept;
constexpr uint32_t unwrap() const { return code; } constexpr uint32_t unwrap() const noexcept { return code; }
/// ShortCode constructors /// ShortCode constructors
explicit ShortCode(uint32_t short_code); explicit ShortCode(uint32_t short_code);
explicit ShortCode(std::string &&short_code); explicit ShortCode(std::string &&short_code);
explicit ShortCode(CommonCode &&common_code); explicit ShortCode(CommonCode &&common_code) noexcept;
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) noexcept;
/// 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) noexcept;
static ShortCode from_string(std::string &&short_code); static ShortCode from_string(std::string &&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);
static ShortCode from_common_code(CommonCode &&common_code); static ShortCode from_common_code(CommonCode &&common_code) noexcept;
static ShortCode from_common_code(std::string &&common_code); static ShortCode from_common_code(std::string &&common_code);
static ShortCode from_common_code(const CommonCode &common_code); static ShortCode from_common_code(const CommonCode &common_code) noexcept;
static ShortCode from_common_code(const std::string &common_code); static ShortCode from_common_code(const std::string &common_code);
}; };
inline bool operator==(uint32_t s1, const ShortCode &s2) { return s1 == s2.unwrap(); } inline bool operator==(uint32_t s1, const ShortCode &s2) noexcept { return s1 == s2.unwrap(); }
inline bool operator!=(uint32_t s1, const ShortCode &s2) { return s1 != s2.unwrap(); } inline bool operator!=(uint32_t s1, const ShortCode &s2) noexcept { return s1 != s2.unwrap(); }
inline bool operator==(const ShortCode &s1, uint32_t s2) { return s1.unwrap() == s2; } inline bool operator==(const ShortCode &s1, uint32_t s2) noexcept { return s1.unwrap() == s2; }
inline bool operator!=(const ShortCode &s1, uint32_t s2) { return s1.unwrap() != s2; } inline bool operator!=(const ShortCode &s1, uint32_t s2) noexcept { return s1.unwrap() != s2; }
} }

4
src/klotski_core/utils/common.cc

@ -2,7 +2,7 @@
using klotski::Common; using klotski::Common;
uint32_t Common::range_reverse(uint32_t bin) { // reverse binary every 2-bits uint32_t Common::range_reverse(uint32_t bin) noexcept { // reverse binary every 2-bits
bin = ((bin << 16) & 0xFFFF0000) | ((bin >> 16) & 0x0000FFFF); bin = ((bin << 16) & 0xFFFF0000) | ((bin >> 16) & 0x0000FFFF);
bin = ((bin << 8) & 0xFF00FF00) | ((bin >> 8) & 0x00FF00FF); bin = ((bin << 8) & 0xFF00FF00) | ((bin >> 8) & 0x00FF00FF);
bin = ((bin << 4) & 0xF0F0F0F0) | ((bin >> 4) & 0x0F0F0F0F); bin = ((bin << 4) & 0xF0F0F0F0) | ((bin >> 4) & 0x0F0F0F0F);
@ -10,7 +10,7 @@ uint32_t Common::range_reverse(uint32_t bin) { // reverse binary every 2-bits
} }
/// WARN: don't check unknown data -> may cause infinite loop /// WARN: don't check unknown data -> may cause infinite loop
uint8_t Common::check_range(uint32_t head, uint32_t range) { // check generated range uint8_t Common::check_range(uint32_t head, uint32_t range) noexcept { // check generated range
/// 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

4
src/klotski_core/utils/common.h

@ -61,7 +61,7 @@
namespace klotski { namespace klotski {
class Common { class Common {
public: public:
static uint32_t range_reverse(uint32_t bin); static uint32_t range_reverse(uint32_t bin) noexcept;
static uint8_t check_range(uint32_t head, uint32_t range); static uint8_t check_range(uint32_t head, uint32_t range) noexcept;
}; };
} }

Loading…
Cancel
Save