mirror of https://github.com/dnomd343/klotski.git
				
				
			
				 16 changed files with 359 additions and 324 deletions
			
			
		| @ -0,0 +1,56 @@ | |||||
|  | #pragma once | ||||
|  | 
 | ||||
|  | namespace klotski { | ||||
|  | namespace codec { | ||||
|  | 
 | ||||
|  | /// Get the original 64-bit code.
 | ||||
|  | inline uint64_t CommonCode::unwrap() const noexcept { | ||||
|  |     return code_; | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// Implicit conversion to 64-bit code.
 | ||||
|  | inline CommonCode::operator uint64_t() const noexcept { | ||||
|  |     return code_; | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// Equality comparison between CommonCode and numbers.
 | ||||
|  | inline bool operator==(CommonCode c1, uint64_t c2) noexcept { | ||||
|  |     return c1.unwrap() == c2; | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// CommonCode equal comparison implement.
 | ||||
|  | inline bool operator==(CommonCode c1, CommonCode c2) noexcept { | ||||
|  |     return c1.unwrap() == c2.unwrap(); | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// CommonCode less than comparison implement.
 | ||||
|  | inline bool operator<(CommonCode c1, CommonCode c2) noexcept { | ||||
|  |     return c1.unwrap() < c2.unwrap(); | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// CommonCode greater than comparison implement.
 | ||||
|  | inline bool operator>(CommonCode c1, CommonCode c2) noexcept { | ||||
|  |     return c1.unwrap() > c2.unwrap(); | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// CommonCode create without any check.
 | ||||
|  | inline CommonCode CommonCode::unsafe_create(uint64_t common_code) noexcept { | ||||
|  |     return *reinterpret_cast<CommonCode*>(&common_code); // init directly
 | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// CommonCode create with valid check.
 | ||||
|  | inline std::optional<CommonCode> CommonCode::create(uint64_t common_code) noexcept { | ||||
|  |     if (!CommonCode::check(common_code)) { | ||||
|  |         return std::nullopt; // invalid common code
 | ||||
|  |     } | ||||
|  |     return CommonCode::unsafe_create(common_code); | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// Output string encoding of CommonCode.
 | ||||
|  | inline std::ostream& operator<<(std::ostream &out, CommonCode self) { | ||||
|  |     out << CommonCode::string_encode(self.code_); | ||||
|  |     return out; | ||||
|  | } | ||||
|  | 
 | ||||
|  | } // namespace codec
 | ||||
|  | } // namespace klotski
 | ||||
| @ -0,0 +1,70 @@ | |||||
|  | #pragma once | ||||
|  | 
 | ||||
|  | namespace klotski { | ||||
|  | namespace codec { | ||||
|  | 
 | ||||
|  | /// Get the original 64-bit code.
 | ||||
|  | inline uint64_t RawCode::unwrap() const noexcept { | ||||
|  |     return code_; | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// Implicit conversion to 64-bit code.
 | ||||
|  | inline RawCode::operator uint64_t() const noexcept { | ||||
|  |     return code_; | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// Equality comparison between RawCode and numbers.
 | ||||
|  | inline bool operator==(RawCode r1, uint64_t r2) noexcept { | ||||
|  |     return r1.unwrap() == r2; | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// RawCode equal comparison implement.
 | ||||
|  | inline bool operator==(RawCode r1, RawCode r2) noexcept { | ||||
|  |     return r1.unwrap() == r2.unwrap(); | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// RawCode create without any check.
 | ||||
|  | inline RawCode RawCode::unsafe_create(uint64_t raw_code) noexcept { | ||||
|  |     return *reinterpret_cast<RawCode*>(&raw_code); // init directly
 | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// RawCode create with valid check.
 | ||||
|  | inline std::optional<RawCode> RawCode::create(uint64_t raw_code) noexcept { | ||||
|  |     if (!RawCode::check(raw_code)) { | ||||
|  |         return std::nullopt; // invalid raw code
 | ||||
|  |     } | ||||
|  |     return RawCode::unsafe_create(raw_code); | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// Calculate vertically symmetrical case.
 | ||||
|  | inline RawCode RawCode::to_vertical_mirror() const noexcept { | ||||
|  |     return RawCode::unsafe_create(get_vertical_mirror(code_)); | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// Calculate horizontally symmetrical case.
 | ||||
|  | inline RawCode RawCode::to_horizontal_mirror() const noexcept { | ||||
|  |     return RawCode::unsafe_create(get_horizontal_mirror(code_)); | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// Determine whether the case is vertically symmetrical.
 | ||||
|  | inline bool RawCode::is_vertical_mirror() const noexcept { | ||||
|  |     return check_vertical_mirror(code_); | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// Determine whether the case is horizontally symmetrical.
 | ||||
|  | inline bool RawCode::is_horizontal_mirror() const noexcept { | ||||
|  |     return check_horizontal_mirror(code_); | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// Determine whether two cases are vertically symmetrical to each other.
 | ||||
|  | inline bool RawCode::is_vertical_mirror(RawCode raw_code) const noexcept { | ||||
|  |     return raw_code.code_ == get_vertical_mirror(code_); | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// Determine whether two cases are horizontally symmetrical to each other.
 | ||||
|  | inline bool RawCode::is_horizontal_mirror(RawCode raw_code) const noexcept { | ||||
|  |     return raw_code.code_ == get_horizontal_mirror(code_); | ||||
|  | } | ||||
|  | 
 | ||||
|  | } // namespace codec
 | ||||
|  | } // namespace klotski
 | ||||
| @ -0,0 +1,56 @@ | |||||
|  | #pragma once | ||||
|  | 
 | ||||
|  | namespace klotski { | ||||
|  | namespace codec { | ||||
|  | 
 | ||||
|  | /// Get the original 32-bit code.
 | ||||
|  | inline uint32_t ShortCode::unwrap() const noexcept { | ||||
|  |     return code_; | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// Implicit conversion to 32-bit code.
 | ||||
|  | inline ShortCode::operator uint32_t() const noexcept { | ||||
|  |     return code_; | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// Equality comparison between ShortCode and numbers.
 | ||||
|  | inline bool operator==(ShortCode s1, uint32_t s2) noexcept { | ||||
|  |     return s1.unwrap() == s2; | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// ShortCode equal comparison implement.
 | ||||
|  | inline bool operator==(ShortCode s1, ShortCode s2) noexcept { | ||||
|  |     return s1.unwrap() == s2.unwrap(); | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// ShortCode less than comparison implement.
 | ||||
|  | inline bool operator<(ShortCode s1, ShortCode s2) noexcept { | ||||
|  |     return s1.unwrap() < s2.unwrap(); | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// ShortCode greater than comparison implement.
 | ||||
|  | inline bool operator>(ShortCode s1, ShortCode s2) noexcept { | ||||
|  |     return s1.unwrap() > s2.unwrap(); | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// ShortCode create without any check.
 | ||||
|  | inline ShortCode ShortCode::unsafe_create(uint32_t short_code) noexcept { | ||||
|  |     return *reinterpret_cast<ShortCode*>(&short_code); // init directly
 | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// ShortCode create with valid check.
 | ||||
|  | inline std::optional<ShortCode> ShortCode::create(uint32_t short_code) noexcept { | ||||
|  |     if (!ShortCode::check(short_code)) { | ||||
|  |         return std::nullopt; // invalid short code
 | ||||
|  |     } | ||||
|  |     return ShortCode::unsafe_create(short_code); | ||||
|  | } | ||||
|  | 
 | ||||
|  | /// Output string encoding of ShortCode.
 | ||||
|  | inline std::ostream& operator<<(std::ostream &out, ShortCode self) { | ||||
|  |     out << ShortCode::string_encode(self.code_); | ||||
|  |     return out; | ||||
|  | } | ||||
|  | 
 | ||||
|  | } // namespace codec
 | ||||
|  | } // namespace klotski
 | ||||
| @ -1,18 +0,0 @@ | |||||
| #pragma once |  | ||||
| 
 |  | ||||
| /// This is the head index, the offset (0 ~ 29334498) in all cases is obtained
 |  | ||||
| /// according to the `head` (0 ~ 15). In other words, the short code range can be
 |  | ||||
| /// obtained according to the position of the 2x2 block.
 |  | ||||
| 
 |  | ||||
| #include <cstdint> |  | ||||
| 
 |  | ||||
| namespace klotski { |  | ||||
| 
 |  | ||||
| const uint32_t ALL_CASES_OFFSET[16] = { |  | ||||
|            0,  2942906,  5203298,  8146204, |  | ||||
|      8146204, 10468254, 12345199, 14667249, |  | ||||
|     14667249, 16989299, 18866244, 21188294, |  | ||||
|     21188294, 24131200, 26391592, 29334498, |  | ||||
| }; |  | ||||
| 
 |  | ||||
| } // namespace klotski
 |  | ||||
					Loading…
					
					
				
		Reference in new issue