From e3b5987d26bf7ee8b5fd36490390018b4ba9b3e4 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 27 Oct 2024 17:55:27 +0800 Subject: [PATCH] update: using `klotski::group` namespace --- src/core/all_cases/internal/basic_ranges.cc | 2 +- src/core/fast_cal/internal/fast_cal.inl | 4 +- src/core/group/group.h | 80 +++++++++++-------- src/core/group/internal/constant/group.h | 4 +- .../group/internal/constant/group_union.h | 4 +- src/core/group/internal/extend.cc | 7 +- src/core/group/internal/group.cc | 6 +- src/core/group/internal/group.inl | 4 +- src/core/group/internal/group_cases.cc | 12 +-- src/core/group/internal/group_cases.inl | 8 +- src/core/group/internal/group_union.cc | 7 +- src/core/group/internal/group_union.inl | 4 +- src/core/main.cc | 15 ++-- src/core_test/cases/basic_ranges.cc | 4 +- src/core_test/cases/group_legacy.cc | 4 +- src/core_test/cases/group_pro.cc | 4 +- src/core_test/cases/group_union.cc | 8 +- src/core_test/cases/helper/cases.h | 2 +- src/core_test/cases/helper/group_impl.cc | 2 +- src/core_test/helper/internal/group.cc | 2 +- src/core_test/helper/internal/parallel.cc | 6 +- src/core_test/helper/parallel.h | 6 +- 22 files changed, 105 insertions(+), 90 deletions(-) diff --git a/src/core/all_cases/internal/basic_ranges.cc b/src/core/all_cases/internal/basic_ranges.cc index e194aec..c79d256 100644 --- a/src/core/all_cases/internal/basic_ranges.cc +++ b/src/core/all_cases/internal/basic_ranges.cc @@ -6,7 +6,7 @@ using klotski::cases::Ranges; using klotski::cases::BasicRanges; -using klotski::cases::TYPE_ID_LIMIT; +using klotski::group::TYPE_ID_LIMIT; using RangesIter = Ranges::iterator ; using RangeType = std::tuple ; diff --git a/src/core/fast_cal/internal/fast_cal.inl b/src/core/fast_cal/internal/fast_cal.inl index 0173f0b..3107747 100644 --- a/src/core/fast_cal/internal/fast_cal.inl +++ b/src/core/fast_cal/internal/fast_cal.inl @@ -5,8 +5,8 @@ namespace klotski::fast_cal { inline FastCal::FastCal(const codec::RawCode code) - : seeker_({code}, cases::GroupUnion::from_raw_code(code).max_group_size()) { - const auto reserve = cases::GroupUnion::from_raw_code(code).max_group_size(); + : seeker_({code}, group::GroupUnion::from_raw_code(code).max_group_size()) { + const auto reserve = group::GroupUnion::from_raw_code(code).max_group_size(); cases_.reserve(static_cast(reserve * 1.56)); cases_.emplace(code, info_t {0, nil}); // without mask } diff --git a/src/core/group/group.h b/src/core/group/group.h index 773c825..ecd76cb 100644 --- a/src/core/group/group.h +++ b/src/core/group/group.h @@ -70,25 +70,21 @@ #include "short_code/short_code.h" #include "common_code/common_code.h" -namespace klotski::cases { +namespace klotski::group { +// TODO: move constants to `.inl` file constexpr uint32_t TYPE_ID_LIMIT = 203; -constexpr uint32_t ALL_GROUP_NUM = 25422; -constexpr uint32_t ALL_PATTERN_NUM = 6577; +constexpr uint32_t ALL_GROUP_NUM = 25422; // TODO: from GROUP_NUM +constexpr uint32_t ALL_PATTERN_NUM = 6577; // TODO: from PATTERN_NUM class Group; class GroupUnion { public: - GroupUnion() = delete; - - using Groups = std::vector; - -#ifndef KLSK_NDEBUG - friend std::ostream& operator<<(std::ostream &out, GroupUnion self); -#endif // ------------------------------------------------------------------------------------- // + GroupUnion() = delete; + /// Get the original type id. [[nodiscard]] constexpr uint32_t unwrap() const; @@ -114,12 +110,14 @@ public: // ------------------------------------------------------------------------------------- // - /// Get all cases under the current type id. - [[nodiscard]] RangesUnion cases() const; + using Groups = std::vector; /// Get all groups under the current type id. [[nodiscard]] constexpr Groups groups() const; + /// Get all klotski cases under the current type id. + [[nodiscard]] cases::RangesUnion cases() const; + /// Get the group instance with the specified pattern id. [[nodiscard]] constexpr std::optional groups(uint32_t pattern_id) const; @@ -136,13 +134,18 @@ public: // ------------------------------------------------------------------------------------- // +#ifndef KLSK_NDEBUG + /// Output type_id value only for debug. + friend std::ostream& operator<<(std::ostream &out, GroupUnion self); +#endif + /// Compare the type_id values of two GroupUnion. friend constexpr auto operator==(const GroupUnion &lhs, const GroupUnion &rhs); // ------------------------------------------------------------------------------------- // private: - uint32_t type_id_; + uint32_t type_id_; // TODO: using uint_fast8_t // ------------------------------------------------------------------------------------- // @@ -157,15 +160,9 @@ private: class Group { public: - Group() = delete; - - [[nodiscard]] constexpr std::string to_string() const; - -#ifndef KLSK_NDEBUG - friend std::ostream& operator<<(std::ostream &out, Group self); -#endif // ------------------------------------------------------------------------------------- // + // TODO: enum with uint_fast8_t enum class Toward { A = 0, // baseline B = 1, // horizontal mirror @@ -195,8 +192,13 @@ public: /// Get the original pattern id. [[nodiscard]] constexpr uint32_t pattern_id() const; + /// Get the string form of current group. + [[nodiscard]] constexpr std::string to_string() const; + // ------------------------------------------------------------------------------------- // + Group() = delete; + /// Create Group without any check. static constexpr Group unsafe_create(uint32_t type_id, uint32_t pattern_id, Toward toward); @@ -207,12 +209,12 @@ public: // ------------------------------------------------------------------------------------- // - /// Get all cases under current group. - [[nodiscard]] RangesUnion cases() const; - - /// Get the number of klotski cases contained. + /// Get the number of cases contained. [[nodiscard]] constexpr uint32_t size() const; + /// Get all klotski cases under current group. + [[nodiscard]] cases::RangesUnion cases() const; + // ------------------------------------------------------------------------------------- // /// Create Group from RawCode. @@ -232,24 +234,29 @@ public: /// Whether the group is vertically symmetrical. [[nodiscard]] constexpr bool is_vertical_mirror() const; - /// Whether the group is horizontally symmetrical. - [[nodiscard]] constexpr bool is_horizontal_mirror() const; - /// Obtain the vertically symmetrical klotski group. [[nodiscard]] constexpr Group to_vertical_mirror() const; + /// Whether the group is horizontally symmetrical. + [[nodiscard]] constexpr bool is_horizontal_mirror() const; + /// Obtain the horizontally symmetrical klotski group. [[nodiscard]] constexpr Group to_horizontal_mirror() const; // ------------------------------------------------------------------------------------- // +#ifndef KLSK_NDEBUG + /// Output group info only for debug. + friend std::ostream& operator<<(std::ostream &out, Group self); +#endif + /// Compare the internal values of two Group. friend constexpr auto operator==(const Group &lhs, const Group &rhs); // ------------------------------------------------------------------------------------- // private: - uint32_t type_id_; + uint32_t type_id_; // TODO: using uint_fast8_t Toward toward_; uint32_t pattern_id_; @@ -270,6 +277,7 @@ class GroupCases { public: // ------------------------------------------------------------------------------------- // + // TODO: move to `::klotski::group::CaseInfo` class CaseInfo { public: CaseInfo() = delete; @@ -323,8 +331,12 @@ public: // ------------------------------------------------------------------------------------- // private: + // ------------------------------------------------------------------------------------- // + + /// Whether fast mode is available. static inline bool fast_ {false}; + /// Mutex for protecting critical section. static inline std::mutex busy_ {}; // ------------------------------------------------------------------------------------- // @@ -366,7 +378,7 @@ static_assert(std::is_trivially_copyable_v); static_assert(std::is_standard_layout_v); static_assert(std::is_trivially_copyable_v); -} // namespace klotski::cases +} // namespace klotski::group #include "internal/group_union.inl" #include "internal/group_cases.inl" @@ -377,20 +389,22 @@ static_assert(std::is_trivially_copyable_v); namespace std { template <> -struct std::hash { - constexpr std::size_t operator()(const klotski::cases::Group &g) const noexcept { +struct std::hash { + constexpr std::size_t operator()(const klotski::group::Group &g) const noexcept { // TODO: perf hash alg return std::hash{}(g.type_id() ^ g.pattern_id() ^ (int)g.toward()); } }; template <> -struct std::hash { - constexpr std::size_t operator()(const klotski::cases::GroupUnion &gu) const noexcept { +struct std::hash { + constexpr std::size_t operator()(const klotski::group::GroupUnion &gu) const noexcept { return std::hash{}(gu.unwrap()); } }; +// TODO: add `std::hash` for CaseInfo + } // namespace std // ----------------------------------------------------------------------------------------- // diff --git a/src/core/group/internal/constant/group.h b/src/core/group/internal/constant/group.h index ed9d3e3..e428a61 100644 --- a/src/core/group/internal/constant/group.h +++ b/src/core/group/internal/constant/group.h @@ -2,7 +2,7 @@ #include -namespace klotski::cases { +namespace klotski::group { // seed(36) + size(20) + type(3) @@ -16,4 +16,4 @@ constexpr auto GROUP_DATA = std::to_array({ #include "group.inc" }); -} // namespace klotski::cases +} // namespace klotski::group diff --git a/src/core/group/internal/constant/group_union.h b/src/core/group/internal/constant/group_union.h index c8b4943..ee903f8 100644 --- a/src/core/group/internal/constant/group_union.h +++ b/src/core/group/internal/constant/group_union.h @@ -4,7 +4,7 @@ #include "utils/utility.h" -namespace klotski::cases { +namespace klotski::group { /// The number of groups contained in GroupUnion. constexpr auto GROUP_NUM = std::to_array({ @@ -187,4 +187,4 @@ constexpr auto GROUP_UNION_CASES_NUM = std::to_array - #include #include "mover/mover.h" #include "group/group.h" -//using klotski::cases::Group; using klotski::codec::RawCode; using klotski::codec::CommonCode; using klotski::cases::RangesUnion; using klotski::mover::MaskMover; -using klotski::cases::GroupUnion; +using klotski::group::GroupUnion; // TODO: maybe we can perf with mirror cases -std::vector klotski::cases::Group_extend(RawCode raw_code, uint32_t reserve) { +std::vector klotski::group::Group_extend(RawCode raw_code, uint32_t reserve) { std::vector codes; phmap::flat_hash_map cases; // // reserve = reserve ? reserve : GroupUnion::from_raw_code(raw_code).max_group_size(); diff --git a/src/core/group/internal/group.cc b/src/core/group/internal/group.cc index d36a71e..502d675 100644 --- a/src/core/group/internal/group.cc +++ b/src/core/group/internal/group.cc @@ -5,11 +5,11 @@ using klotski::codec::RawCode; using klotski::codec::CommonCode; -using klotski::cases::Group; +using klotski::group::Group; using klotski::cases::RangesUnion; -using klotski::cases::GROUP_DATA; -using klotski::cases::PATTERN_DATA; +using klotski::group::GROUP_DATA; +using klotski::group::PATTERN_DATA; RangesUnion Group::cases() const { diff --git a/src/core/group/internal/group.inl b/src/core/group/internal/group.inl index f2040d5..96078fe 100644 --- a/src/core/group/internal/group.inl +++ b/src/core/group/internal/group.inl @@ -2,7 +2,7 @@ #include "constant/group.h" -namespace klotski::cases { +namespace klotski::group { constexpr uint32_t Group::type_id() const { return type_id_; @@ -178,4 +178,4 @@ constexpr Group Group::to_horizontal_mirror() const { } } -} // namespace klotski::cases +} // namespace klotski::group diff --git a/src/core/group/internal/group_cases.cc b/src/core/group/internal/group_cases.cc index 24867fd..10c6c5b 100644 --- a/src/core/group/internal/group_cases.cc +++ b/src/core/group/internal/group_cases.cc @@ -7,16 +7,16 @@ using klotski::codec::ShortCode; using klotski::codec::CommonCode; -using klotski::cases::Group; -using klotski::cases::GroupCases; -using klotski::cases::GroupUnion; +using klotski::group::Group; +using klotski::group::GroupCases; +using klotski::group::GroupUnion; using klotski::cases::RangesUnion; -using klotski::cases::ALL_GROUP_NUM; -using klotski::cases::TYPE_ID_LIMIT; +using klotski::group::ALL_GROUP_NUM; +using klotski::group::TYPE_ID_LIMIT; using klotski::cases::ALL_CASES_NUM_; -using klotski::cases::GROUP_DATA; +using klotski::group::GROUP_DATA; struct case_info_t { uint32_t pattern_id : 10; diff --git a/src/core/group/internal/group_cases.inl b/src/core/group/internal/group_cases.inl index 02054f6..b8b181f 100644 --- a/src/core/group/internal/group_cases.inl +++ b/src/core/group/internal/group_cases.inl @@ -2,7 +2,7 @@ #include -namespace klotski::cases { +namespace klotski::group { inline std::ostream& operator<<(std::ostream &out, GroupCases::CaseInfo self) { out << self.to_string(); @@ -21,14 +21,14 @@ constexpr uint32_t GroupCases::CaseInfo::case_id() const { return case_id_; } -inline std::optional GroupCases::CaseInfo::create(klotski::cases::Group group, uint32_t case_id) { +inline std::optional GroupCases::CaseInfo::create(klotski::group::Group group, uint32_t case_id) { if (case_id >= group.size()) { return std::nullopt; } return unsafe_create(group, case_id); } -inline GroupCases::CaseInfo GroupCases::CaseInfo::unsafe_create(klotski::cases::Group group, uint32_t case_id) { +inline GroupCases::CaseInfo GroupCases::CaseInfo::unsafe_create(klotski::group::Group group, uint32_t case_id) { return {group, case_id}; } @@ -67,4 +67,4 @@ inline GroupCases::CaseInfo GroupCases::obtain_info(codec::ShortCode short_code) return tiny_obtain_info(short_code.to_common_code()); } -} // namespace klotski::cases +} // namespace klotski::group diff --git a/src/core/group/internal/group_union.cc b/src/core/group/internal/group_union.cc index 6d807ea..9ecacfe 100644 --- a/src/core/group/internal/group_union.cc +++ b/src/core/group/internal/group_union.cc @@ -2,9 +2,14 @@ #include "group/group.h" #include "constant/group_union.h" +using klotski::cases::Ranges; +using klotski::cases::RangesUnion; + using klotski::codec::RawCode; using klotski::codec::CommonCode; -using klotski::cases::GroupUnion; +using klotski::group::GroupUnion; + +using klotski::cases::BASIC_RANGES_NUM; #define RANGE_DERIVE(HEAD) ranges.derive(HEAD, cases[HEAD]) diff --git a/src/core/group/internal/group_union.inl b/src/core/group/internal/group_union.inl index 8d50620..1a39631 100644 --- a/src/core/group/internal/group_union.inl +++ b/src/core/group/internal/group_union.inl @@ -4,7 +4,7 @@ #include "constant/group_union.h" -namespace klotski::cases { +namespace klotski::group { // ----------------------------------------------------------------------------------------- // @@ -138,4 +138,4 @@ constexpr GroupUnion GroupUnion::from_common_code(const codec::CommonCode common // ----------------------------------------------------------------------------------------- // -} // namespace klotski::cases +} // namespace klotski::group diff --git a/src/core/main.cc b/src/core/main.cc index b28f9cb..44dbc52 100644 --- a/src/core/main.cc +++ b/src/core/main.cc @@ -27,19 +27,18 @@ using klotski::cases::BasicRanges; using klotski::codec::RawCode; using klotski::codec::ShortCode; using klotski::codec::CommonCode; -using klotski::cases::GroupUnion; +using klotski::group::GroupUnion; -using klotski::cases::Group; -using klotski::cases::GroupCases; -using klotski::cases::GroupUnion; +using klotski::group::Group; +using klotski::group::GroupCases; +using klotski::group::GroupUnion; -using klotski::cases::TYPE_ID_LIMIT; +using klotski::group::TYPE_ID_LIMIT; using klotski::cases::ALL_CASES_NUM_; using klotski::codec::SHORT_CODE_LIMIT; -using klotski::cases::PATTERN_DATA; -using klotski::cases::PATTERN_OFFSET; -using klotski::cases::Group_load_factor; +using klotski::group::PATTERN_DATA; +using klotski::group::PATTERN_OFFSET; int main() { // const auto start = clock(); diff --git a/src/core_test/cases/basic_ranges.cc b/src/core_test/cases/basic_ranges.cc index 3028175..b9718e6 100644 --- a/src/core_test/cases/basic_ranges.cc +++ b/src/core_test/cases/basic_ranges.cc @@ -11,8 +11,8 @@ using klotski::cases::Ranges; using klotski::cases::AllCases; using klotski::cases::BasicRanges; -using klotski::cases::BLOCK_NUM; -using klotski::cases::TYPE_ID_LIMIT; +using klotski::group::BLOCK_NUM; +using klotski::group::TYPE_ID_LIMIT; /// Forcibly modify private variables to reset state. EXPOSE_VAR(BasicRanges, bool, available_) diff --git a/src/core_test/cases/group_legacy.cc b/src/core_test/cases/group_legacy.cc index 8975891..aeafc8e 100644 --- a/src/core_test/cases/group_legacy.cc +++ b/src/core_test/cases/group_legacy.cc @@ -4,9 +4,9 @@ #include "helper/cases.h" //using klotski::cases::Group; -using klotski::cases::GroupUnion; +using klotski::group::GroupUnion; -using klotski::cases::TYPE_ID_LIMIT; +using klotski::group::TYPE_ID_LIMIT; // TODO: basic type_id / group_id verify with create / unsafe_create diff --git a/src/core_test/cases/group_pro.cc b/src/core_test/cases/group_pro.cc index 358c42d..b9c4909 100644 --- a/src/core_test/cases/group_pro.cc +++ b/src/core_test/cases/group_pro.cc @@ -7,8 +7,8 @@ #include "helper/parallel.h" -using klotski::cases::Group; -using klotski::cases::GroupUnion; +using klotski::group::Group; +using klotski::group::GroupUnion; TEST(Group, demo) { diff --git a/src/core_test/cases/group_union.cc b/src/core_test/cases/group_union.cc index 63a0979..7ef3876 100644 --- a/src/core_test/cases/group_union.cc +++ b/src/core_test/cases/group_union.cc @@ -15,11 +15,11 @@ using klotski::codec::ShortCode; -using klotski::cases::Group; -using klotski::cases::GroupUnion; +using klotski::group::Group; +using klotski::group::GroupUnion; -using klotski::cases::TYPE_ID_LIMIT; -using klotski::cases::ALL_GROUP_NUM; +using klotski::group::TYPE_ID_LIMIT; +using klotski::group::ALL_GROUP_NUM; #define EXPECT_IOTA(R) \ EXPECT_FALSE(R.empty()); \ diff --git a/src/core_test/cases/helper/cases.h b/src/core_test/cases/helper/cases.h index de79fda..fc87944 100644 --- a/src/core_test/cases/helper/cases.h +++ b/src/core_test/cases/helper/cases.h @@ -14,7 +14,7 @@ using klotski::codec::CommonCode; using klotski::cases::BasicRanges; using klotski::cases::RangesUnion; -using klotski::cases::BLOCK_NUM; +using klotski::group::BLOCK_NUM; using klotski::cases::ALL_CASES_NUM; using klotski::cases::ALL_CASES_NUM_; using klotski::cases::BASIC_RANGES_NUM; diff --git a/src/core_test/cases/helper/group_impl.cc b/src/core_test/cases/helper/group_impl.cc index a8b59f4..8dc5529 100644 --- a/src/core_test/cases/helper/group_impl.cc +++ b/src/core_test/cases/helper/group_impl.cc @@ -7,7 +7,7 @@ /// Extend ordered Group from the specified CommonCode seed. static std::vector extend_cases(CommonCode seed) { // TODO: using inner build process -> only allow calling klotski::mover - auto raw_codes = klotski::cases::Group_extend(seed.to_raw_code()); + auto raw_codes = klotski::group::Group_extend(seed.to_raw_code()); std::vector common_codes {raw_codes.begin(), raw_codes.end()}; std::ranges::sort(common_codes.begin(), common_codes.end()); return common_codes; diff --git a/src/core_test/helper/internal/group.cc b/src/core_test/helper/internal/group.cc index c6a375a..9de368a 100644 --- a/src/core_test/helper/internal/group.cc +++ b/src/core_test/helper/internal/group.cc @@ -53,7 +53,7 @@ const std::vector& helper::group_union_cases(const uint32_t type_id) /// Extend ordered Group from the specified CommonCode seed. static std::vector extend_cases(CommonCode seed) { // TODO: using inner build process -> only allow calling klotski::mover - auto raw_codes = klotski::cases::Group_extend(seed.to_raw_code()); + auto raw_codes = klotski::group::Group_extend(seed.to_raw_code()); std::vector common_codes {raw_codes.begin(), raw_codes.end()}; std::ranges::sort(common_codes.begin(), common_codes.end()); return common_codes; diff --git a/src/core_test/helper/internal/parallel.cc b/src/core_test/helper/internal/parallel.cc index 37c6bf5..6e56731 100644 --- a/src/core_test/helper/internal/parallel.cc +++ b/src/core_test/helper/internal/parallel.cc @@ -6,12 +6,12 @@ #include "all_cases/all_cases.h" using klotski::cases::AllCases; -using klotski::cases::TYPE_ID_LIMIT; +using klotski::group::TYPE_ID_LIMIT; using klotski::cases::ALL_CASES_NUM_; -//void helper::group_parallel(std::function &&func) { +void helper::group_parallel(std::function &&func) { // TODO: spawn all Groups -//} +} void helper::type_id_parallel(std::function &&func) { diff --git a/src/core_test/helper/parallel.h b/src/core_test/helper/parallel.h index a80e5da..4e97c91 100644 --- a/src/core_test/helper/parallel.h +++ b/src/core_test/helper/parallel.h @@ -9,8 +9,8 @@ #include "short_code/short_code.h" #include "common_code/common_code.h" -//using klotski::cases::Group; -using klotski::cases::GroupUnion; +using klotski::group::Group; +using klotski::group::GroupUnion; using klotski::codec::RawCode; using klotski::codec::ShortCode; @@ -21,7 +21,7 @@ namespace helper { // ----------------------------------------------------------------------------------------- // /// Spawn all valid Groups in parallel. -//void group_parallel(std::function &&func); +void group_parallel(std::function &&func); /// Spawn all valid type_ids in parallel. void type_id_parallel(std::function &&func);