From 0bc80d89e5d001dc035e946a7f796a627e45cc34 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 19 Oct 2024 10:21:43 +0800 Subject: [PATCH] feat: hash support of klotski code --- src/core/common_code/internal/common_code.inl | 15 +++++++++++++++ src/core/main.cc | 18 ++++++++++++++++++ src/core/raw_code/internal/raw_code.inl | 15 +++++++++++++++ src/core/short_code/internal/short_code.inl | 15 +++++++++++++++ 4 files changed, 63 insertions(+) diff --git a/src/core/common_code/internal/common_code.inl b/src/core/common_code/internal/common_code.inl index 3fb3b25..f80f3b6 100644 --- a/src/core/common_code/internal/common_code.inl +++ b/src/core/common_code/internal/common_code.inl @@ -138,3 +138,18 @@ constexpr auto operator<=>(const CommonCode &lhs, const CommonCode &rhs) { // ----------------------------------------------------------------------------------------- // } // namespace klotski::codec + +// ----------------------------------------------------------------------------------------- // + +namespace std { + +template <> +struct std::hash { + constexpr std::size_t operator()(const klotski::codec::CommonCode &c) const noexcept { + return std::hash{}(c.unwrap()); + } +}; + +} // namespace std + +// ----------------------------------------------------------------------------------------- // diff --git a/src/core/main.cc b/src/core/main.cc index d26256d..fbf5365 100644 --- a/src/core/main.cc +++ b/src/core/main.cc @@ -32,6 +32,7 @@ using klotski::cases::GroupCases; using klotski::cases::GroupUnion; using klotski::cases::TYPE_ID_LIMIT; +using klotski::cases::ALL_CASES_NUM_; using klotski::codec::SHORT_CODE_LIMIT; int main() { @@ -42,6 +43,23 @@ int main() { const auto start = std::chrono::system_clock::now(); + ShortCode::speed_up(true); + + std::unordered_set data_r; + std::unordered_set data_s; + std::unordered_set data_c; + data_r.reserve(ALL_CASES_NUM_); + data_s.reserve(ALL_CASES_NUM_); + data_c.reserve(ALL_CASES_NUM_); + for (auto code : AllCases::instance().fetch().codes()) { + data_c.emplace(code); + data_r.emplace(code.to_raw_code()); + data_s.emplace(code.to_short_code()); + } + std::cout << data_r.size() << std::endl; + std::cout << data_s.size() << std::endl; + std::cout << data_c.size() << std::endl; + // const auto common_code = CommonCode::unsafe_create(0x1A9BF0C00); // const auto group = Group::from_common_code(common_code); diff --git a/src/core/raw_code/internal/raw_code.inl b/src/core/raw_code/internal/raw_code.inl index b530e15..17d6756 100644 --- a/src/core/raw_code/internal/raw_code.inl +++ b/src/core/raw_code/internal/raw_code.inl @@ -94,3 +94,18 @@ constexpr auto operator<=>(const RawCode &lhs, const RawCode &rhs) { // ----------------------------------------------------------------------------------------- // } // namespace klotski::codec + +// ----------------------------------------------------------------------------------------- // + +namespace std { + +template <> +struct std::hash { + constexpr std::size_t operator()(const klotski::codec::RawCode &r) const noexcept { + return std::hash{}(r.unwrap()); + } +}; + +} // namespace std + +// ----------------------------------------------------------------------------------------- // diff --git a/src/core/short_code/internal/short_code.inl b/src/core/short_code/internal/short_code.inl index c0b031b..1546092 100644 --- a/src/core/short_code/internal/short_code.inl +++ b/src/core/short_code/internal/short_code.inl @@ -116,3 +116,18 @@ constexpr auto operator<=>(const ShortCode &lhs, const ShortCode &rhs) { // ----------------------------------------------------------------------------------------- // } // namespace klotski::codec + +// ----------------------------------------------------------------------------------------- // + +namespace std { + +template <> +struct std::hash { + constexpr std::size_t operator()(const klotski::codec::ShortCode &s) const noexcept { + return std::hash{}(s.unwrap()); + } +}; + +} // namespace std + +// ----------------------------------------------------------------------------------------- //