Browse Source

perf: mirror interface of RawCode

master
Dnomd343 2 years ago
parent
commit
827fa270e3
  1. 2
      src/klotski_core/all_cases/all_cases.h
  2. 2
      src/klotski_core/all_cases/basic_ranges.h
  3. 4
      src/klotski_core/raw_code/convert.cc
  4. 57
      src/klotski_core/raw_code/mirror.cc
  5. 14
      src/klotski_core/raw_code/raw_code.cc
  6. 11
      src/klotski_core/raw_code/raw_code.h
  7. 2
      src/klotski_core/short_code/serialize.cc
  8. 10
      src/klotski_core/short_code/short_code.h
  9. 1
      test/basic/all_cases.cc

2
src/klotski_core/all_cases/all_cases.h

@ -31,7 +31,7 @@
#include "basic_ranges.h"
namespace klotski {
/// all cases count -> memory pre-allocated
/// all cases count
const uint32_t ALL_CASES_SIZE[16] = {
2942906, 2260392, 2942906, 0,
2322050, 1876945, 2322050, 0,

2
src/klotski_core/all_cases/basic_ranges.h

@ -31,7 +31,7 @@
#include <cstdint>
namespace klotski {
/// basic ranges count -> memory pre-allocated
/// basic ranges count
const uint32_t BASIC_RANGES_SIZE = 7311921;
class BasicRanges {

4
src/klotski_core/raw_code/convert.cc

@ -30,7 +30,9 @@ RawCode RawCode::from_common_code(CommonCode &&common_code) {
}
RawCode RawCode::from_common_code(std::string &&common_code) {
return RawCode(std::forward<CommonCode>(CommonCode(common_code)));
return RawCode(std::forward<CommonCode>(
CommonCode(std::forward<std::string>(common_code))
));
}
RawCode RawCode::from_common_code(const CommonCode &common_code) {

57
src/klotski_core/raw_code/mirror.cc

@ -2,7 +2,8 @@
using klotski::RawCode;
/// Mirror convert functions
/// ----------------------------- Mirror Convert ------------------------------
RawCode RawCode::to_vertical_mirror() const {
return RawCode::unsafe_create(vertical_mirror(code));
}
@ -11,50 +12,34 @@ RawCode RawCode::to_horizontal_mirror() const {
return RawCode::unsafe_create(horizontal_mirror(code));
}
/// Mirror check functions
bool RawCode::is_vertical_mirror() const {
// TODO: vertical mirror check
/// ------------------------------ Mirror Check -------------------------------
return false;
bool RawCode::is_vertical_mirror() const {
return vertical_mirror_check(code);
}
bool RawCode::is_horizontal_mirror() const {
// TODO: horizontal mirror check
return false;
return horizontal_mirror_check(code);
}
bool RawCode::is_vertical_mirror(RawCode &&raw_code) const {
// TODO: vertical mirror check
return false;
}
bool RawCode::is_vertical_mirror(const RawCode &raw_code) const {
// TODO: vertical mirror check
return false;
return raw_code.unwrap() == vertical_mirror(code);
}
bool RawCode::is_horizontal_mirror(RawCode &&raw_code) const {
return raw_code.unwrap() == horizontal_mirror(code);
}
// TODO: horizontal mirror check
return false;
bool RawCode::is_vertical_mirror(const RawCode &raw_code) const {
return raw_code.unwrap() == vertical_mirror(code);
}
bool RawCode::is_horizontal_mirror(const RawCode &raw_code) const {
// TODO: horizontal mirror check
return false;
return raw_code.unwrap() == horizontal_mirror(code);
}
/// Basic mirror convert
/// ----------------------------- Basic Functions -----------------------------
uint64_t RawCode::vertical_mirror(uint64_t raw_code) {
// TODO: vertical mirror convert
@ -68,3 +53,17 @@ uint64_t RawCode::horizontal_mirror(uint64_t raw_code) {
return 0;
}
bool RawCode::vertical_mirror_check(uint64_t raw_code) {
// TODO: whether self vertical mirror
return false;
}
bool RawCode::horizontal_mirror_check(uint64_t raw_code) {
// TODO: whether self horizontal mirror
return false;
}

14
src/klotski_core/raw_code/raw_code.cc

@ -68,19 +68,19 @@ namespace klotski {
}
bool RawCode::check(uint64_t raw_code) { // check whether raw code is valid
/// MASK_1x2 MASK_2x1 MASK_2x2
/// 000 100 000 000 000 000 000 000 000 100 000 000
/// 000 000 000 000 100 000 000 000 100 100 000 000
/// ... ... ...
constexpr uint64_t MASK_1x1 = ~B_1x1 & 0b111; /// 0b100
/// MASK_1x1 | MASK_1x2 | MASK_2x1 | MASK_2x2
/// 100 000 000 000 | 000 100 000 000 | 000 000 000 000 | 000 100 000 000
/// 000 000 000 000 | 000 000 000 000 | 100 000 000 000 | 100 100 000 000
/// ... | ... | ... | ...
///
constexpr uint64_t MASK_1x1 = ~B_1x1 & 0b111;
constexpr uint64_t MASK_1x2 = MASK_1x1 << 3;
constexpr uint64_t MASK_2x1 = MASK_1x1 << 12;
constexpr uint64_t MASK_2x2 = MASK_1x1 << 3 | MASK_1x1 << 12 | MASK_1x1 << 15;
/// high 4-bits check
if (raw_code >> 60) {
return false; // high 4-bits must be zero
}
/// check each block
int head_num = 0, space_num = 0; // statistics for space and 2x2 number
for (int addr = 0; addr < 20; ++addr, raw_code >>= 3) {

11
src/klotski_core/raw_code/raw_code.h

@ -57,11 +57,14 @@ namespace klotski {
uint64_t code;
RawCode() = default; // unsafe initialize
static uint64_t compact(uint64_t raw_code); // raw code -> common code
static uint64_t extract(uint64_t common_code); // common code -> raw code
static inline uint64_t compact(uint64_t raw_code); // raw code -> common code
static inline uint64_t extract(uint64_t common_code); // common code -> raw code
static uint64_t vertical_mirror(uint64_t raw_code); // to vertical mirror
static uint64_t horizontal_mirror(uint64_t raw_code); // to horizontal mirror
static inline uint64_t vertical_mirror(uint64_t raw_code); // to vertical mirror
static inline uint64_t horizontal_mirror(uint64_t raw_code); // to horizontal mirror
static inline bool vertical_mirror_check(uint64_t raw_code); // check vertical mirror
static inline bool horizontal_mirror_check(uint64_t raw_code); // check horizontal mirror
public:
/// RawCode validity check

2
src/klotski_core/short_code/serialize.cc

@ -21,7 +21,7 @@ ShortCode::ShortCode(const std::string &short_code) {
}
ShortCode ShortCode::from_string(std::string &&short_code) {
return ShortCode(short_code);
return ShortCode(std::forward<std::string>(short_code));
}
ShortCode ShortCode::from_string(const std::string &short_code) {

10
src/klotski_core/short_code/short_code.h

@ -76,14 +76,14 @@ namespace klotski {
uint32_t code;
ShortCode() = default; // unsafe initialize
static inline Mode mode();
static Mode mode();
static bool fast_mode_available;
static bool normal_mode_available;
static uint64_t fast_decode(uint32_t short_code); // short code -> common code
static uint32_t fast_encode(uint64_t common_code); // common code -> short code
static uint64_t tiny_decode(uint32_t short_code); // short code -> common code
static uint32_t tiny_encode(uint64_t common_code); // common code -> short code
static 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 std::string string_encode(uint32_t short_code); // short code -> string
static inline uint32_t string_decode(const std::string &short_code); // string -> short code

1
test/basic/all_cases.cc

@ -1,5 +1,4 @@
#include <thread>
#include <cstdint>
#include "md5.h"
#include "all_cases.h"
#include "gtest/gtest.h"

Loading…
Cancel
Save