Browse Source

feat: codec batch convert in vector

legacy
Dnomd343 2 years ago
parent
commit
edd5186454
  1. 4
      src/klotski_core/common_code/common_code.cc
  2. 26
      src/klotski_core/common_code/common_code.h
  3. 14
      src/klotski_core/common_code/convert.cc
  4. 9
      src/klotski_core/raw_code/convert.cc
  5. 4
      src/klotski_core/raw_code/mirror.cc
  6. 6
      src/klotski_core/raw_code/raw_code.cc
  7. 20
      src/klotski_core/raw_code/raw_code.h
  8. 7
      src/klotski_core/short_code/convert.cc
  9. 4
      src/klotski_core/short_code/short_code.cc
  10. 22
      src/klotski_core/short_code/short_code.h

4
src/klotski_core/common_code/common_code.cc

@ -5,10 +5,6 @@ namespace klotski {
using Common::range_reverse; using Common::range_reverse;
bool CommonCode::valid() const noexcept {
return CommonCode::check(code_);
}
CommonCode CommonCode::create(uint64_t common_code) { CommonCode CommonCode::create(uint64_t common_code) {
return CommonCode(common_code); // create from uint64_t return CommonCode(common_code); // create from uint64_t
} }

26
src/klotski_core/common_code/common_code.h

@ -17,8 +17,9 @@
/// namely `space`, `1x2`, `2x1`, `1x1`. Each of them is represented by 2-bit, /// namely `space`, `1x2`, `2x1`, `1x1`. Each of them is represented by 2-bit,
/// which are `00` `01` `10` `11`. Arrange them according to their position and /// which are `00` `01` `10` `11`. Arrange them according to their position and
/// size, and we can get a binary sequence. /// size, and we can get a binary sequence.
/// 2x2 -> # # | 2x1 -> # | 1x2 -> # # | 1x1 -> # ///
/// # # | # | | /// ( 2x2 -> # # ) | ( 2x1 -> # ) | ( 1x2 -> # # ) | ( 1x1 -> # )
/// ( # # ) | ( # ) | |
/// This sequence can have up to 16 blocks, aka 32-bit in length. Therefore, in /// This sequence can have up to 16 blocks, aka 32-bit in length. Therefore, in
/// order to be compatible with all cases, the length of this part of the code /// order to be compatible with all cases, the length of this part of the code
@ -52,6 +53,7 @@
/// CommonCode = 0x4FEA13400 -> "4FEA134" /// CommonCode = 0x4FEA13400 -> "4FEA134"
#include <string> #include <string>
#include <vector>
#include <cstdint> #include <cstdint>
#include <ostream> #include <ostream>
#include <stdexcept> #include <stdexcept>
@ -62,6 +64,11 @@ namespace klotski {
class RawCode; class RawCode;
class ShortCode; class ShortCode;
class CommonCode;
typedef std::vector<RawCode> RawCodes;
typedef std::vector<ShortCode> ShortCodes;
typedef std::vector<CommonCode> CommonCodes;
class CommonCodeExp : public std::runtime_error { class CommonCodeExp : public std::runtime_error {
public: public:
@ -79,8 +86,8 @@ class CommonCode {
public: public:
/// Validity check /// Validity check
bool valid() const noexcept;
static bool check(uint64_t common_code) noexcept; static bool check(uint64_t common_code) noexcept;
bool valid() const noexcept { return check(code_); }
/// Operators of CommonCode /// Operators of CommonCode
constexpr explicit operator uint64_t() const noexcept { return code_; } constexpr explicit operator uint64_t() const noexcept { return code_; }
@ -101,7 +108,7 @@ public:
explicit CommonCode(const ShortCode &short_code) noexcept; explicit CommonCode(const ShortCode &short_code) noexcept;
explicit CommonCode(const std::string &common_code); explicit CommonCode(const std::string &common_code);
/// Static initialization /// CommonCode initializations
static CommonCode create(uint64_t common_code); static CommonCode create(uint64_t common_code);
static CommonCode unsafe_create(uint64_t common_code) noexcept; static CommonCode unsafe_create(uint64_t common_code) noexcept;
@ -117,17 +124,22 @@ public:
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) noexcept; 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);
/// Batch conversions
static CommonCodes convert(const RawCodes &raw_codes) noexcept;
static CommonCodes convert(const ShortCodes &short_codes) noexcept;
}; };
/// Compare implements
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!=(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; }
inline bool operator!=(const CommonCode &c1, uint64_t c2) noexcept { return c1.unwrap() != c2; } inline bool operator!=(uint64_t c1, const CommonCode &c2) noexcept { return !(c1 == c2); }
inline bool operator!=(const CommonCode &c1, uint64_t c2) noexcept { return !(c1 == c2); }
inline bool operator<(const CommonCode &c1, const CommonCode &c2) noexcept { return c1.unwrap() < c2.unwrap(); } inline bool operator<(const CommonCode &c1, const CommonCode &c2) noexcept { return c1.unwrap() < c2.unwrap(); }
inline bool operator>(const CommonCode &c1, const CommonCode &c2) noexcept { return c1.unwrap() > c2.unwrap(); } inline bool operator>(const CommonCode &c1, const CommonCode &c2) noexcept { return c1.unwrap() > c2.unwrap(); }
inline bool operator==(const CommonCode &c1, const CommonCode &c2) noexcept { return c1.unwrap() == c2.unwrap(); } inline bool operator==(const CommonCode &c1, const CommonCode &c2) noexcept { return c1.unwrap() == c2.unwrap(); }
inline bool operator!=(const CommonCode &c1, const CommonCode &c2) noexcept { return c1.unwrap() != c2.unwrap(); } inline bool operator!=(const CommonCode &c1, const CommonCode &c2) noexcept { return !(c1 == c2); }
} // namespace klotski } // namespace klotski

14
src/klotski_core/common_code/convert.cc

@ -4,6 +4,10 @@ using klotski::RawCode;
using klotski::ShortCode; using klotski::ShortCode;
using klotski::CommonCode; using klotski::CommonCode;
using klotski::RawCodes;
using klotski::ShortCode;
using klotski::CommonCodes;
/// -------------------------- CommonCode to RawCode -------------------------- /// -------------------------- CommonCode to RawCode --------------------------
RawCode CommonCode::to_raw_code() const noexcept { RawCode CommonCode::to_raw_code() const noexcept {
@ -67,3 +71,13 @@ CommonCode CommonCode::from_short_code(const ShortCode &short_code) noexcept {
CommonCode CommonCode::from_short_code(const std::string &short_code) { CommonCode CommonCode::from_short_code(const std::string &short_code) {
return ShortCode(short_code).to_common_code(); return ShortCode(short_code).to_common_code();
} }
/// ---------------------------- Batch conversions ----------------------------
CommonCodes CommonCode::convert(const RawCodes &raw_codes) noexcept {
return {raw_codes.begin(), raw_codes.end()};
}
CommonCodes CommonCode::convert(const ShortCodes &short_codes) noexcept {
return {short_codes.begin(), short_codes.end()};
}

9
src/klotski_core/raw_code/convert.cc

@ -2,8 +2,9 @@
#include "raw_code.h" #include "raw_code.h"
using klotski::RawCode; using klotski::RawCode;
using klotski::CommonCode;
using klotski::RawCodeExp; using klotski::RawCodeExp;
using klotski::CommonCode;
using klotski::CommonCodes;
/// -------------------------- RawCode to CommonCode -------------------------- /// -------------------------- RawCode to CommonCode --------------------------
@ -43,6 +44,12 @@ RawCode RawCode::from_common_code(const std::string &common_code) {
return RawCode(CommonCode(common_code)); return RawCode(CommonCode(common_code));
} }
/// ---------------------------- Batch conversion -----------------------------
CommonCodes RawCode::convert(const RawCodes &raw_codes) noexcept {
return {raw_codes.begin(), raw_codes.end()};
}
/// ----------------------------- Basic Functions ----------------------------- /// ----------------------------- Basic Functions -----------------------------
/// NOTE: ensure that input raw code is valid! /// NOTE: ensure that input raw code is valid!

4
src/klotski_core/raw_code/mirror.cc

@ -41,7 +41,7 @@ bool RawCode::is_horizontal_mirror(const RawCode &raw_code) const noexcept {
/// ----------------------------- Basic Functions ----------------------------- /// ----------------------------- Basic Functions -----------------------------
/// MASK_MIRROR_H1 | MASK_MIRROR_H2 /// MASK_MIRROR_H1 | MASK_MIRROR_H2
/// 111 000 000 000 | 000 111 000 000 /// 111 000 000 000 | 000 111 000 000
/// 111 000 000 000 | 000 111 000 000 /// 111 000 000 000 | 000 111 000 000
/// 111 000 000 000 | 000 111 000 000 /// 111 000 000 000 | 000 111 000 000
@ -51,7 +51,7 @@ bool RawCode::is_horizontal_mirror(const RawCode &raw_code) const noexcept {
constexpr uint64_t MASK_MIRROR_H1 = 0x0'007'007'007'007'007; constexpr uint64_t MASK_MIRROR_H1 = 0x0'007'007'007'007'007;
constexpr uint64_t MASK_MIRROR_H2 = 0x0'038'038'038'038'038; constexpr uint64_t MASK_MIRROR_H2 = 0x0'038'038'038'038'038;
/// MASK_MIRROR_V1 | MASK_MIRROR_V2 | MASK_MIRROR_V3 /// MASK_MIRROR_V1 | MASK_MIRROR_V2 | MASK_MIRROR_V3
/// 111 111 111 111 | 000 000 000 000 | 000 000 000 000 /// 111 111 111 111 | 000 000 000 000 | 000 000 000 000
/// 000 000 000 000 | 111 111 111 111 | 000 000 000 000 /// 000 000 000 000 | 111 111 111 111 | 000 000 000 000
/// 000 000 000 000 | 000 000 000 000 | 111 111 111 111 /// 000 000 000 000 | 000 000 000 000 | 111 111 111 111

6
src/klotski_core/raw_code/raw_code.cc

@ -3,10 +3,6 @@
namespace klotski { namespace klotski {
bool RawCode::valid() const noexcept {
return RawCode::check(code_);
}
RawCode RawCode::create(uint64_t raw_code) { RawCode RawCode::create(uint64_t raw_code) {
return RawCode(raw_code); return RawCode(raw_code);
} }
@ -27,7 +23,7 @@ RawCode::RawCode(uint64_t raw_code) {
std::ostream& operator<<(std::ostream &out, const RawCode &self) { std::ostream& operator<<(std::ostream &out, const RawCode &self) {
char code[16]; char code[16];
char dump_map[] = { char dump_map[] = {
/// 0x0 1x2 2x1 1x1 2x2 b101 b110 fill /// 0x0 1x2 2x1 1x1 2x2 b101 b110 fill
'.', '~', '|', '*', '@', '?', '?', '+' '.', '~', '|', '*', '@', '?', '?', '+'
}; };
sprintf(code, "%015lX", self.code_); // code length -> 15 sprintf(code, "%015lX", self.code_); // code length -> 15

20
src/klotski_core/raw_code/raw_code.h

@ -38,6 +38,7 @@
/// => 0x0E58'FC85'FFEB'C4DB /// => 0x0E58'FC85'FFEB'C4DB
#include <string> #include <string>
#include <vector>
#include <cstdint> #include <cstdint>
#include <ostream> #include <ostream>
#include <stdexcept> #include <stdexcept>
@ -45,8 +46,12 @@
namespace klotski { namespace klotski {
class RawCode;
class CommonCode; class CommonCode;
typedef std::vector<RawCode> RawCodes;
typedef std::vector<CommonCode> CommonCodes;
class RawCodeExp : public std::runtime_error { class RawCodeExp : public std::runtime_error {
public: public:
RawCodeExp() : std::runtime_error("invalid raw code") {} RawCodeExp() : std::runtime_error("invalid raw code") {}
@ -69,8 +74,8 @@ class RawCode {
public: public:
/// Validity check /// Validity check
bool valid() const noexcept;
static bool check(uint64_t raw_code) noexcept; static bool check(uint64_t raw_code) noexcept;
bool valid() const noexcept { return check(code_); }
/// Operators of RawCode /// Operators of RawCode
constexpr explicit operator uint64_t() const noexcept { return code_; } constexpr explicit operator uint64_t() const noexcept { return code_; }
@ -85,7 +90,7 @@ public:
explicit RawCode(CommonCode &&common_code) noexcept; explicit RawCode(CommonCode &&common_code) noexcept;
explicit RawCode(const CommonCode &common_code) noexcept; explicit RawCode(const CommonCode &common_code) noexcept;
/// Static initialization /// RawCode initializations
static RawCode create(uint64_t raw_code); static RawCode create(uint64_t raw_code);
static RawCode unsafe_create(uint64_t raw_code) noexcept; static RawCode unsafe_create(uint64_t raw_code) noexcept;
@ -95,6 +100,9 @@ public:
static RawCode from_common_code(const CommonCode &common_code) noexcept; static RawCode from_common_code(const CommonCode &common_code) noexcept;
static RawCode from_common_code(const std::string &common_code); static RawCode from_common_code(const std::string &common_code);
/// Batch conversion
static CommonCodes convert(const RawCodes &raw_codes) noexcept;
/// Mirror functions /// Mirror functions
RawCode to_vertical_mirror() const noexcept; RawCode to_vertical_mirror() const noexcept;
RawCode to_horizontal_mirror() const noexcept; RawCode to_horizontal_mirror() const noexcept;
@ -108,12 +116,14 @@ public:
bool is_horizontal_mirror(const RawCode &raw_code) const noexcept; bool is_horizontal_mirror(const RawCode &raw_code) const noexcept;
}; };
/// Compare implements
inline bool operator==(uint64_t r1, const RawCode &r2) noexcept { return r1 == r2.unwrap(); } inline bool operator==(uint64_t r1, const RawCode &r2) noexcept { return r1 == r2.unwrap(); }
inline bool operator!=(uint64_t r1, const RawCode &r2) noexcept { return r1 != r2.unwrap(); }
inline bool operator==(const RawCode &r1, uint64_t r2) noexcept { return r1.unwrap() == r2; } inline bool operator==(const RawCode &r1, uint64_t r2) noexcept { return r1.unwrap() == r2; }
inline bool operator!=(const RawCode &r1, uint64_t r2) noexcept { return r1.unwrap() != r2; }
inline bool operator==(const RawCode &r1, const RawCode &r2) noexcept { return r1.unwrap() == r2.unwrap(); } inline bool operator==(const RawCode &r1, const RawCode &r2) noexcept { return r1.unwrap() == r2.unwrap(); }
inline bool operator!=(const RawCode &r1, const RawCode &r2) noexcept { return r1.unwrap() != r2.unwrap(); }
inline bool operator!=(uint64_t r1, const RawCode &r2) noexcept { return !(r1 == r2); }
inline bool operator!=(const RawCode &r1, uint64_t r2) noexcept { return !(r1 == r2); }
inline bool operator!=(const RawCode &r1, const RawCode &r2) noexcept { return !(r1 == r2); }
} // namespace klotski } // namespace klotski

7
src/klotski_core/short_code/convert.cc

@ -6,6 +6,7 @@
#include "range_prefix_offset.h" #include "range_prefix_offset.h"
using klotski::ShortCode; using klotski::ShortCode;
using klotski::ShortCodes;
using klotski::CommonCode; using klotski::CommonCode;
/// ------------------------- ShortCode to CommonCode ------------------------- /// ------------------------- ShortCode to CommonCode -------------------------
@ -57,6 +58,12 @@ ShortCode ShortCode::from_common_code(const std::string &common_code) {
return ShortCode(CommonCode(common_code)); return ShortCode(CommonCode(common_code));
} }
/// ---------------------------- Batch conversion -----------------------------
ShortCodes ShortCode::convert(const CommonCodes &common_codes) noexcept {
return {common_codes.begin(), common_codes.end()};
}
/// ----------------------------- Basic Functions ----------------------------- /// ----------------------------- Basic Functions -----------------------------
/// NOTE: ensure that input common code is valid! /// NOTE: ensure that input common code is valid!

4
src/klotski_core/short_code/short_code.cc

@ -6,10 +6,6 @@ namespace klotski {
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::valid() const noexcept {
return ShortCode::check(code_);
}
ShortCode ShortCode::create(uint32_t short_code) { ShortCode ShortCode::create(uint32_t short_code) {
return ShortCode(short_code); return ShortCode(short_code);
} }

22
src/klotski_core/short_code/short_code.h

@ -49,6 +49,7 @@
/// while the latter is almost impossible to complete by the human brain. /// while the latter is almost impossible to complete by the human brain.
#include <string> #include <string>
#include <vector>
#include <cstdint> #include <cstdint>
#include <ostream> #include <ostream>
#include <utility> #include <utility>
@ -58,8 +59,12 @@
namespace klotski { namespace klotski {
class ShortCode;
class CommonCode; class CommonCode;
typedef std::vector<ShortCode> ShortCodes;
typedef std::vector<CommonCode> CommonCodes;
const uint32_t SHORT_CODE_LIMIT = 29334498; const uint32_t SHORT_CODE_LIMIT = 29334498;
class ShortCodeExp : public std::runtime_error { class ShortCodeExp : public std::runtime_error {
@ -97,8 +102,8 @@ private:
public: public:
/// Validity check /// Validity check
bool valid() const noexcept;
static bool check(uint32_t short_code) noexcept; static bool check(uint32_t short_code) noexcept;
bool valid() const noexcept { return check(code_); }
/// ShortCode convert mode /// ShortCode convert mode
static void speed_up(Mode mode); // {} -> {NORMAL} -> {FAST} static void speed_up(Mode mode); // {} -> {NORMAL} -> {FAST}
@ -119,7 +124,7 @@ public:
explicit ShortCode(const std::string &short_code); explicit ShortCode(const std::string &short_code);
explicit ShortCode(const CommonCode &common_code) noexcept; explicit ShortCode(const CommonCode &common_code) noexcept;
/// Static initialization /// ShortCode initializations
static ShortCode create(uint32_t short_code); static ShortCode create(uint32_t short_code);
static ShortCode unsafe_create(uint32_t short_code) noexcept; static ShortCode unsafe_create(uint32_t short_code) noexcept;
@ -131,14 +136,21 @@ public:
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) noexcept; 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);
/// Batch conversion
static ShortCodes convert(const CommonCodes &common_codes) noexcept;
}; };
/// Compare implements
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!=(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; }
inline bool operator!=(const ShortCode &s1, uint32_t s2) noexcept { return s1.unwrap() != s2; } inline bool operator!=(uint32_t s1, const ShortCode &s2) noexcept { return !(s1 == s2); }
inline bool operator!=(const ShortCode &s1, uint32_t s2) noexcept { return !(s1 == s2); }
inline bool operator<(const ShortCode &s1, const ShortCode &s2) noexcept { return s1.unwrap() < s2.unwrap(); }
inline bool operator>(const ShortCode &s1, const ShortCode &s2) noexcept { return s1.unwrap() > s2.unwrap(); }
inline bool operator==(const ShortCode &s1, const ShortCode &s2) noexcept { return s1.unwrap() == s2.unwrap(); } inline bool operator==(const ShortCode &s1, const ShortCode &s2) noexcept { return s1.unwrap() == s2.unwrap(); }
inline bool operator!=(const ShortCode &s1, const ShortCode &s2) noexcept { return s1.unwrap() != s2.unwrap(); } inline bool operator!=(const ShortCode &s1, const ShortCode &s2) noexcept { return !(s1 == s2); }
} // namespace klotski } // namespace klotski

Loading…
Cancel
Save