Browse Source

update: adjust hash impl of codec

legacy
Dnomd343 2 weeks ago
parent
commit
31dc23174b
  1. 7
      src/core/common_code/common_code.h
  2. 19
      src/core/common_code/internal/common_code.inl
  3. 7
      src/core/main.cc
  4. 24
      src/core/raw_code/internal/check.inl
  5. 35
      src/core/raw_code/internal/raw_code.inl
  6. 7
      src/core/raw_code/raw_code.h
  7. 15
      src/core/short_code/internal/short_code.inl
  8. 13
      src/core/short_code/short_code.h

7
src/core/common_code/common_code.h

@ -201,3 +201,10 @@ static_assert(std::is_trivially_copyable_v<CommonCode>);
#include "internal/common_code.inl" #include "internal/common_code.inl"
#include "internal/mirror.inl" #include "internal/mirror.inl"
#include "internal/check.inl" #include "internal/check.inl"
template <>
struct std::hash<klotski::codec::CommonCode> {
constexpr std::size_t operator()(const klotski::codec::CommonCode &c) const noexcept {
return std::hash<uint64_t>{}(c.unwrap());
}
};

19
src/core/common_code/internal/common_code.inl

@ -53,6 +53,8 @@ inline ShortCode CommonCode::to_short_code() const {
return ShortCode(*this); return ShortCode(*this);
} }
// ----------------------------------------------------------------------------------------- //
inline std::string CommonCode::to_string(const bool shorten) const { inline std::string CommonCode::to_string(const bool shorten) const {
if (!shorten) { if (!shorten) {
return string_encode(code_); // with full length return string_encode(code_); // with full length
@ -60,8 +62,6 @@ inline std::string CommonCode::to_string(const bool shorten) const {
return string_encode_shorten(code_); // without trailing zero return string_encode_shorten(code_); // without trailing zero
} }
// ----------------------------------------------------------------------------------------- //
inline std::optional<CommonCode> CommonCode::from_string(const std::string_view common_code) { inline std::optional<CommonCode> CommonCode::from_string(const std::string_view common_code) {
return string_decode(common_code).transform(unsafe_create); return string_decode(common_code).transform(unsafe_create);
} }
@ -138,18 +138,3 @@ constexpr auto operator<=>(const CommonCode &lhs, const CommonCode &rhs) {
// ----------------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------------- //
} // namespace klotski::codec } // namespace klotski::codec
// ----------------------------------------------------------------------------------------- //
namespace std {
template <>
struct hash<klotski::codec::CommonCode> {
constexpr std::size_t operator()(const klotski::codec::CommonCode &c) const noexcept {
return std::hash<uint64_t>{}(c.unwrap());
}
};
} // namespace std
// ----------------------------------------------------------------------------------------- //

7
src/core/main.cc

@ -79,6 +79,13 @@ int main() {
static_assert(raw_code.to_vertical_mirror() == 0xFFF5E2FCF4DA603); static_assert(raw_code.to_vertical_mirror() == 0xFFF5E2FCF4DA603);
static_assert(raw_code.to_horizontal_mirror() == 0x603EDF5CAFFF5E2); static_assert(raw_code.to_horizontal_mirror() == 0x603EDF5CAFFF5E2);
static_assert(ShortCode::check(4091296));
constexpr auto short_code = ShortCode::unsafe_create(4091296);
static_assert(short_code.unwrap() == 4091296);
static_assert(static_cast<uint32_t>(short_code) == 4091296);
static_assert(short_code == ShortCode::create(4091296)->unwrap());
// const auto code = CommonCode::unsafe_create(0x1A9BF0C00).to_raw_code(); // const auto code = CommonCode::unsafe_create(0x1A9BF0C00).to_raw_code();
// const auto code = CommonCode::unsafe_create(0x4FEA13400).to_raw_code(); // const auto code = CommonCode::unsafe_create(0x4FEA13400).to_raw_code();
// FastCal fc {code}; // FastCal fc {code};

24
src/core/raw_code/internal/check.inl

@ -1,33 +1,9 @@
#pragma once #pragma once
#ifndef KLSK_NDEBUG
// TODO: only for debug output -> move to other header
#include <format>
#include <ostream>
#endif
#include "utils/common.h" #include "utils/common.h"
namespace klotski::codec { namespace klotski::codec {
#ifndef KLSK_NDEBUG
inline std::ostream& operator<<(std::ostream &out, const RawCode self) {
constexpr auto char_map = std::to_array({
'.', // space
'~', '|', // 1x2 | 2x1
'*', '@', // 1x1 | 2x2
'?', '?', // unknown
'+', // fill
});
out << std::format("{:015X}\n", self.code_);
for (int addr = 0; addr < 60; addr += 3) {
out << char_map[(self.code_ >> addr) & 0b111];
out << &" "[(addr & 0b11) == 0b01] << &"\n"[(addr & 0b11) != 0b01];
}
return out;
}
#endif
constexpr bool RawCode::check(uint64_t raw_code) { constexpr bool RawCode::check(uint64_t raw_code) {
/// MASK_1x1 | MASK_1x2 | MASK_2x1 | MASK_2x2 /// MASK_1x1 | MASK_1x2 | MASK_2x1 | MASK_2x2
/// 100 000 000 000 | 000 100 000 000 | 000 000 000 000 | 000 100 000 000 /// 100 000 000 000 | 000 100 000 000 | 000 000 000 000 | 000 100 000 000

35
src/core/raw_code/internal/raw_code.inl

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <format>
#include "common_code/common_code.h" #include "common_code/common_code.h"
namespace klotski::codec { namespace klotski::codec {
@ -93,19 +95,28 @@ constexpr auto operator<=>(const RawCode &lhs, const RawCode &rhs) {
// ----------------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------------- //
} // namespace klotski::codec #ifndef KLSK_NDEBUG
inline std::ostream& operator<<(std::ostream &out, const RawCode self) {
// ----------------------------------------------------------------------------------------- // auto show = [code = self.code_](const int offset) {
constexpr auto char_map = std::to_array({
namespace std { '.', // space
'~', '|', // 1x2 | 2x1
template <> '*', '@', // 1x1 | 2x2
struct hash<klotski::codec::RawCode> { '?', '?', // unknown
constexpr std::size_t operator()(const klotski::codec::RawCode &r) const noexcept { '+', // fill
return std::hash<uint64_t>{}(r.unwrap()); });
} return char_map[(code >> (offset * 3)) & 0b111];
}; };
} // namespace std out << std::format("{:015X}\n", self.code_);
for (int offset = 0; offset < 20; offset += 4) {
out << std::format("{} {} {} {}\n",
show(offset), show(offset + 1), show(offset + 2), show(offset + 3));
}
return out;
}
#endif
// ----------------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------------- //
} // namespace klotski::codec

7
src/core/raw_code/raw_code.h

@ -179,3 +179,10 @@ static_assert(std::is_trivially_copyable_v<RawCode>);
#include "internal/convert.inl" #include "internal/convert.inl"
#include "internal/mirror.inl" #include "internal/mirror.inl"
#include "internal/check.inl" #include "internal/check.inl"
template <>
struct std::hash<klotski::codec::RawCode> {
constexpr std::size_t operator()(const klotski::codec::RawCode &r) const noexcept {
return std::hash<uint64_t>{}(r.unwrap());
}
};

15
src/core/short_code/internal/short_code.inl

@ -117,18 +117,3 @@ constexpr auto operator<=>(const ShortCode &lhs, const ShortCode &rhs) {
// ----------------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------------- //
} // namespace klotski::codec } // namespace klotski::codec
// ----------------------------------------------------------------------------------------- //
namespace std {
template <>
struct hash<klotski::codec::ShortCode> {
constexpr std::size_t operator()(const klotski::codec::ShortCode &s) const noexcept {
return std::hash<uint32_t>{}(s.unwrap());
}
};
} // namespace std
// ----------------------------------------------------------------------------------------- //

13
src/core/short_code/short_code.h

@ -91,15 +91,15 @@ public:
// ------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------- //
/// Build the conversion index.
static void speed_up(bool fast_mode);
/// Explicit conversion to u32 code. /// Explicit conversion to u32 code.
explicit constexpr operator uint32_t() const; explicit constexpr operator uint32_t() const;
/// Check the validity of the original ShortCode. /// Check the validity of the original ShortCode.
static constexpr bool check(uint32_t short_code); static constexpr bool check(uint32_t short_code);
/// Build the conversion index for ShortCode.
static void speed_up(bool fast_mode = false);
#ifndef KLSK_NDEBUG #ifndef KLSK_NDEBUG
/// Output string encoding of ShortCode only for debug. /// Output string encoding of ShortCode only for debug.
friend std::ostream& operator<<(std::ostream &out, ShortCode self); friend std::ostream& operator<<(std::ostream &out, ShortCode self);
@ -195,3 +195,10 @@ static_assert(std::is_trivially_copyable_v<ShortCode>);
#include "internal/short_code.inl" #include "internal/short_code.inl"
#include "internal/serialize.inl" #include "internal/serialize.inl"
#include "internal/convert.inl" #include "internal/convert.inl"
template <>
struct std::hash<klotski::codec::ShortCode> {
constexpr std::size_t operator()(const klotski::codec::ShortCode &s) const noexcept {
return std::hash<uint32_t>{}(s.unwrap());
}
};

Loading…
Cancel
Save