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/raw_code.cc
  6. 20
      src/klotski_core/raw_code/raw_code.h
  7. 7
      src/klotski_core/short_code/convert.cc
  8. 4
      src/klotski_core/short_code/short_code.cc
  9. 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;
bool CommonCode::valid() const noexcept {
return CommonCode::check(code_);
}
CommonCode CommonCode::create(uint64_t common_code) {
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,
/// which are `00` `01` `10` `11`. Arrange them according to their position and
/// 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
/// order to be compatible with all cases, the length of this part of the code
@ -52,6 +53,7 @@
/// CommonCode = 0x4FEA13400 -> "4FEA134"
#include <string>
#include <vector>
#include <cstdint>
#include <ostream>
#include <stdexcept>
@ -62,6 +64,11 @@ namespace klotski {
class RawCode;
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 {
public:
@ -79,8 +86,8 @@ class CommonCode {
public:
/// Validity check
bool valid() const noexcept;
static bool check(uint64_t common_code) noexcept;
bool valid() const noexcept { return check(code_); }
/// Operators of CommonCode
constexpr explicit operator uint64_t() const noexcept { return code_; }
@ -101,7 +108,7 @@ public:
explicit CommonCode(const ShortCode &short_code) noexcept;
explicit CommonCode(const std::string &common_code);
/// Static initialization
/// CommonCode initializations
static CommonCode create(uint64_t common_code);
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(const ShortCode &short_code) noexcept;
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==(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 == c2); }
} // namespace klotski

14
src/klotski_core/common_code/convert.cc

@ -4,6 +4,10 @@ using klotski::RawCode;
using klotski::ShortCode;
using klotski::CommonCode;
using klotski::RawCodes;
using klotski::ShortCode;
using klotski::CommonCodes;
/// -------------------------- CommonCode to RawCode --------------------------
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) {
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"
using klotski::RawCode;
using klotski::CommonCode;
using klotski::RawCodeExp;
using klotski::CommonCode;
using klotski::CommonCodes;
/// -------------------------- RawCode to CommonCode --------------------------
@ -43,6 +44,12 @@ RawCode RawCode::from_common_code(const std::string &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 -----------------------------
/// NOTE: ensure that input raw code is valid!

4
src/klotski_core/raw_code/raw_code.cc

@ -3,10 +3,6 @@
namespace klotski {
bool RawCode::valid() const noexcept {
return RawCode::check(code_);
}
RawCode RawCode::create(uint64_t raw_code) {
return RawCode(raw_code);
}

20
src/klotski_core/raw_code/raw_code.h

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

7
src/klotski_core/short_code/convert.cc

@ -6,6 +6,7 @@
#include "range_prefix_offset.h"
using klotski::ShortCode;
using klotski::ShortCodes;
using klotski::CommonCode;
/// ------------------------- ShortCode to CommonCode -------------------------
@ -57,6 +58,12 @@ ShortCode ShortCode::from_common_code(const std::string &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 -----------------------------
/// 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::normal_mode_available_ = false;
bool ShortCode::valid() const noexcept {
return ShortCode::check(code_);
}
ShortCode ShortCode::create(uint32_t 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.
#include <string>
#include <vector>
#include <cstdint>
#include <ostream>
#include <utility>
@ -58,8 +59,12 @@
namespace klotski {
class ShortCode;
class CommonCode;
typedef std::vector<ShortCode> ShortCodes;
typedef std::vector<CommonCode> CommonCodes;
const uint32_t SHORT_CODE_LIMIT = 29334498;
class ShortCodeExp : public std::runtime_error {
@ -97,8 +102,8 @@ private:
public:
/// Validity check
bool valid() const noexcept;
static bool check(uint32_t short_code) noexcept;
bool valid() const noexcept { return check(code_); }
/// ShortCode convert mode
static void speed_up(Mode mode); // {} -> {NORMAL} -> {FAST}
@ -119,7 +124,7 @@ public:
explicit ShortCode(const std::string &short_code);
explicit ShortCode(const CommonCode &common_code) noexcept;
/// Static initialization
/// ShortCode initializations
static ShortCode create(uint32_t short_code);
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(const CommonCode &common_code) noexcept;
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==(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 == s2); }
} // namespace klotski

Loading…
Cancel
Save