Browse Source

update: add `noexcept` flag

master
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 {
public:
static void build();
static enum Status status();
static Status status();
static const std::vector<uint32_t> (&fetch())[16];
private:

2
src/klotski_core/all_cases/basic_ranges.h

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

10
src/klotski_core/common_code/common_code.cc

@ -20,11 +20,11 @@ namespace std {
}
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;
}
bool CommonCode::operator!=(const CommonCode &common_code) const {
bool CommonCode::operator!=(const CommonCode &common_code) const noexcept {
return this->code != common_code.code;
}
@ -37,7 +37,7 @@ namespace klotski {
}
namespace klotski {
bool CommonCode::valid() const {
bool CommonCode::valid() const noexcept {
return CommonCode::check(code);
}
@ -45,7 +45,7 @@ namespace klotski {
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
tmp.code = common_code;
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
/// 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

44
src/klotski_core/common_code/common_code.h

@ -74,54 +74,54 @@ namespace klotski {
CommonCode() = default; // unsafe initialize
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:
/// CommonCode validity check
bool valid() const;
static bool check(uint64_t common_code);
bool valid() const noexcept;
static bool check(uint64_t common_code) noexcept;
/// Operators of CommonCode
bool operator==(const CommonCode &common_code) const;
bool operator!=(const CommonCode &common_code) const;
constexpr explicit operator uint64_t() const { return code; }
bool operator==(const CommonCode &common_code) const noexcept;
bool operator!=(const CommonCode &common_code) const noexcept;
constexpr explicit operator uint64_t() const noexcept { return code; }
friend std::ostream& operator<<(std::ostream &out, const CommonCode &self);
/// Export functions
RawCode to_raw_code() const;
ShortCode to_short_code() const;
RawCode to_raw_code() const noexcept;
ShortCode to_short_code() const noexcept;
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
explicit CommonCode(uint64_t common_code);
explicit CommonCode(RawCode &&raw_code);
explicit CommonCode(ShortCode &&short_code);
explicit CommonCode(RawCode &&raw_code) noexcept;
explicit CommonCode(ShortCode &&short_code) noexcept;
explicit CommonCode(std::string &&common_code);
explicit CommonCode(const RawCode &raw_code);
explicit CommonCode(const ShortCode &short_code);
explicit CommonCode(const RawCode &raw_code) noexcept;
explicit CommonCode(const ShortCode &short_code) noexcept;
explicit CommonCode(const std::string &common_code);
/// Static initialization
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(const std::string &common_code);
static CommonCode from_raw_code(uint64_t raw_code);
static CommonCode from_raw_code(RawCode &&raw_code);
static CommonCode from_raw_code(const RawCode &raw_code);
static CommonCode from_raw_code(RawCode &&raw_code) noexcept;
static CommonCode from_raw_code(const RawCode &raw_code) noexcept;
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(const ShortCode &short_code);
static CommonCode from_short_code(const ShortCode &short_code) noexcept;
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) { 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) { return c1.unwrap() != c2; }
inline bool operator==(uint64_t c1, const CommonCode &c2) noexcept { 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) noexcept { 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 --------------------------
RawCode CommonCode::to_raw_code() const {
RawCode CommonCode::to_raw_code() const noexcept {
return RawCode(*this); // convert to raw code
}
/// ------------------------- CommonCode to ShortCode -------------------------
ShortCode CommonCode::to_short_code() const {
ShortCode CommonCode::to_short_code() const noexcept {
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();
}
CommonCode CommonCode::from_raw_code(RawCode &&raw_code) {
CommonCode CommonCode::from_raw_code(RawCode &&raw_code) noexcept {
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();
}
CommonCode::CommonCode(RawCode &&raw_code) {
CommonCode::CommonCode(RawCode &&raw_code) noexcept {
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
}
/// ------------------------- 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
}
CommonCode::CommonCode(const ShortCode &short_code) {
CommonCode::CommonCode(const ShortCode &short_code) noexcept {
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();
}
CommonCode CommonCode::from_short_code(ShortCode &&short_code) {
CommonCode CommonCode::from_short_code(ShortCode &&short_code) noexcept {
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();
}
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();
}

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);
}
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
sprintf(result, "%09lX", 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 BASIC_RANGES_SIZE = klotski::BASIC_RANGES_SIZE;
void all_cases_build() {
void all_cases_build() noexcept {
AllCases::build();
}
void basic_ranges_build() {
void basic_ranges_build() noexcept {
BasicRanges::build();
}
bool is_all_cases_available() {
bool is_all_cases_available() noexcept {
return AllCases::status() == AllCases::AVAILABLE;
}
bool is_basic_ranges_available() {
bool is_basic_ranges_available() noexcept {
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 (const auto &range : AllCases::fetch()[head]) {
*(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();
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);
}
void short_code_enable_fast() {
void short_code_enable_fast() noexcept {
ShortCode::speed_up(ShortCode::FAST);
}
bool is_short_code_available() {
bool is_short_code_available() noexcept {
if (BasicRanges::status() != BasicRanges::AVAILABLE) {
return false;
}
@ -28,7 +28,7 @@ bool is_short_code_available() {
return true;
}
bool is_short_code_available_fast() {
bool is_short_code_available_fast() noexcept {
if (AllCases::status() != AllCases::AVAILABLE) {
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);
}
bool short_code_check(uint32_t short_code) {
bool short_code_check(uint32_t short_code) noexcept {
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);
}
///////////////////////////////////////////////////////////////////////////////
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)) {
return false;
}
@ -62,7 +62,7 @@ bool raw_code_to_short_code(uint64_t raw_code, uint32_t *short_code) {
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)) {
return false;
}
@ -72,7 +72,7 @@ bool short_code_to_raw_code(uint32_t short_code, uint64_t *raw_code) {
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)) {
return false;
}
@ -82,7 +82,7 @@ bool raw_code_to_common_code(uint64_t raw_code, uint64_t *common_code) {
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)) {
return false;
}
@ -92,7 +92,7 @@ bool common_code_to_raw_code(uint64_t common_code, uint64_t *raw_code) {
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)) {
return false;
}
@ -102,7 +102,7 @@ bool short_code_to_common_code(uint32_t short_code, uint64_t *common_code) {
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)) {
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)
.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)
.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();
}
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();
}
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();
}
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();
}
///////////////////////////////////////////////////////////////////////////////
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)) {
return false;
}
@ -150,7 +150,7 @@ bool is_vertical_mirror(uint64_t raw_code, bool *result) {
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)) {
return false;
}
@ -158,7 +158,7 @@ bool is_horizontal_mirror(uint64_t raw_code, bool *result) {
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)) {
return false;
}
@ -166,7 +166,7 @@ bool to_vertical_mirror(uint64_t raw_code, uint64_t *result) {
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)) {
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();
}
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();
}
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();
}
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();
}
@ -196,7 +196,7 @@ uint64_t to_horizontal_mirror_unsafe(uint64_t raw_code) {
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)) {
return false;
}
@ -205,7 +205,7 @@ bool short_code_to_string(uint32_t short_code, char short_code_str[]) {
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 {
*short_code = ShortCode::from_string(short_code_str).unwrap();
} 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;
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)) {
return false;
}
@ -227,7 +227,7 @@ bool common_code_to_string(uint64_t common_code, char common_code_str[]) {
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)) {
return false;
}
@ -236,7 +236,7 @@ bool common_code_to_string_shorten(uint64_t common_code, char common_code_str[])
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 {
*common_code = CommonCode::from_string(common_code_str).unwrap();
} catch (...) {

24
src/klotski_core/ffi/metadata.cc

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

116
src/klotski_core/klotski.h

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

18
src/klotski_core/short_code/convert.cc

@ -10,7 +10,7 @@ using klotski::CommonCode;
/// ------------------------- ShortCode to CommonCode -------------------------
CommonCode ShortCode::to_common_code() const {
CommonCode ShortCode::to_common_code() const noexcept {
if (ShortCode::mode() == ShortCode::NORMAL) {
return CommonCode::unsafe_create(tiny_decode(code)); // normal mode
}
@ -19,7 +19,7 @@ CommonCode ShortCode::to_common_code() const {
/// ------------------------- ShortCode to CommonCode -------------------------
ShortCode::ShortCode(CommonCode &&common_code) {
ShortCode::ShortCode(CommonCode &&common_code) noexcept {
if (ShortCode::mode() == ShortCode::NORMAL) {
code = tiny_encode(common_code.unwrap()); // normal mode
} 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) {
code = tiny_encode(common_code.unwrap()); // normal mode
} else {
@ -39,7 +39,7 @@ ShortCode ShortCode::from_common_code(uint64_t 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));
}
@ -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);
}
@ -60,7 +60,7 @@ ShortCode ShortCode::from_common_code(const std::string &common_code) {
/// ----------------------------- Basic Functions -----------------------------
/// 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
const auto &ranges = AllCases::fetch()[head]; // available ranges
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!
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
ALL_CASES_OFFSET, ALL_CASES_OFFSET + 16, short_code
) - 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!
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
uint32_t head = common_code >> 32;
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!
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
auto offset = std::upper_bound( // binary search
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 ---------------------------
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);
}
@ -30,7 +30,7 @@ ShortCode ShortCode::from_string(const std::string &short_code) {
/// ----------------------------- 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
result[5] = '\0'; // string ending flag
for (int n = 0; n < 5; ++n) {

10
src/klotski_core/short_code/short_code.cc

@ -23,11 +23,11 @@ namespace std {
}
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;
}
bool ShortCode::operator!=(const ShortCode &short_code) const {
bool ShortCode::operator!=(const ShortCode &short_code) const noexcept {
return this->code != short_code.code;
}
@ -38,7 +38,7 @@ namespace klotski {
}
namespace klotski {
bool ShortCode::valid() const {
bool ShortCode::valid() const noexcept {
return ShortCode::check(code);
}
@ -46,7 +46,7 @@ namespace klotski {
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
tmp.code = short_code;
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)
}

44
src/klotski_core/short_code/short_code.h

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

4
src/klotski_core/utils/common.h

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

Loading…
Cancel
Save