diff --git a/src/core/group/group.h b/src/core/group/group.h index a3ba5e2..dc0210f 100644 --- a/src/core/group/group.h +++ b/src/core/group/group.h @@ -74,11 +74,12 @@ namespace klotski::cases { constexpr uint32_t TYPE_ID_LIMIT = 203; constexpr uint32_t ALL_GROUP_NUM = 25422; +constexpr uint32_t ALL_PATTERN_NUM = 6577; typedef std::vector RawCodes; typedef std::vector CommonCodes; -class Group; +//class Group; class GroupPro; // TODO: add constexpr @@ -123,10 +124,10 @@ public: [[nodiscard]] RangesUnion cases() const; /// Get all groups under the current type id. - [[nodiscard]] std::vector groups() const; +// [[nodiscard]] std::vector groups() const; /// Get the group instance with the specified group id. - [[nodiscard]] std::optional group(uint32_t group_id) const; +// [[nodiscard]] std::optional group(uint32_t group_id) const; // ------------------------------------------------------------------------------------- // @@ -222,12 +223,19 @@ public: // ------------------------------------------------------------------------------------- // + /// Obtain the symmetry type of the group. [[nodiscard]] constexpr MirrorType mirror_type() const; + /// 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 GroupPro to_vertical_mirror() const; + + /// Obtain the horizontally symmetrical klotski group. [[nodiscard]] constexpr GroupPro to_horizontal_mirror() const; // ------------------------------------------------------------------------------------- // @@ -247,62 +255,61 @@ private: [[nodiscard]] constexpr uint32_t flat_id() const; }; -class Group { -public: - Group() = delete; +//class Group { +//public: +// Group() = delete; // ------------------------------------------------------------------------------------- // /// Get the original type id. - [[nodiscard]] constexpr uint32_t type_id() const; +// [[nodiscard]] constexpr uint32_t type_id() const; /// Get the original group id. - [[nodiscard]] constexpr uint32_t group_id() const; +// [[nodiscard]] constexpr uint32_t group_id() const; /// Create Group without any check. - static constexpr Group unsafe_create(uint32_t type_id, uint32_t group_id); +// static constexpr Group unsafe_create(uint32_t type_id, uint32_t group_id); /// Create Group with validity check. - static constexpr std::optional create(uint32_t type_id, uint32_t group_id); +// static constexpr std::optional create(uint32_t type_id, uint32_t group_id); // ------------------------------------------------------------------------------------- // /// Get all cases under current group. - [[nodiscard]] RangesUnion cases() const; +// [[nodiscard]] RangesUnion cases() const; /// Get the number of klotski cases contained. - [[nodiscard]] constexpr uint32_t size() const; +// [[nodiscard]] constexpr uint32_t size() const; // ------------------------------------------------------------------------------------- // /// Create Group from RawCode. - static Group from_raw_code(codec::RawCode raw_code); +// static Group from_raw_code(codec::RawCode raw_code); /// Create Group from ShortCode. - static Group from_short_code(codec::ShortCode short_code); +// static Group from_short_code(codec::ShortCode short_code); /// Create Group from CommonCode. - static Group from_common_code(codec::CommonCode common_code); +// static Group from_common_code(codec::CommonCode common_code); // ------------------------------------------------------------------------------------- // // TODO: add `is_xxx_mirror` interface - [[nodiscard]] constexpr Group to_vertical_mirror() const; +// [[nodiscard]] constexpr Group to_vertical_mirror() const; - [[nodiscard]] constexpr Group to_horizontal_mirror() const; +// [[nodiscard]] constexpr Group to_horizontal_mirror() const; -private: - uint32_t type_id_; - uint32_t group_id_; +//private: +// uint32_t type_id_; +// uint32_t group_id_; /// Tiled merge of type_id and group_id. - [[nodiscard]] constexpr uint32_t flat_id() const; +// [[nodiscard]] constexpr uint32_t flat_id() const; +//}; -public: - /// Spawn all the unsorted codes of the current group. - static std::vector extend(codec::RawCode raw_code, uint32_t reserve = 0); -}; +/// Spawn all the unsorted codes of the current group. +std::vector Group_extend(codec::RawCode raw_code, uint32_t reserve = 0); class GroupCasesPro { public: diff --git a/src/core/group/internal/group.cc b/src/core/group/internal/group.cc index c5ea6a6..00bfa55 100644 --- a/src/core/group/internal/group.cc +++ b/src/core/group/internal/group.cc @@ -3,14 +3,15 @@ #include "mover/mover.h" #include "group/group.h" -using klotski::cases::Group; +//using klotski::cases::Group; using klotski::codec::RawCode; using klotski::codec::CommonCode; using klotski::cases::RangesUnion; using klotski::mover::MaskMover; +using klotski::cases::GroupUnion; -std::vector Group::extend(RawCode raw_code, uint32_t reserve) { +std::vector klotski::cases::Group_extend(RawCode raw_code, uint32_t reserve) { std::vector codes; absl::flat_hash_map cases; // reserve = reserve ? reserve : GroupUnion::from_raw_code(raw_code).max_group_size(); @@ -36,52 +37,52 @@ std::vector Group::extend(RawCode raw_code, uint32_t reserve) { return codes; } -RangesUnion Group::cases() const { - - // TODO: add white list for single-group unions - // return GroupUnion::cases directly - - auto seed = CommonCode::unsafe_create(GROUP_SEED[flat_id()]); - - // std::cout << seed << std::endl; - - auto codes = extend(seed.to_raw_code(), size()); - - // std::cout << codes.size() << std::endl; - - // TODO: how to reserve - - RangesUnion data; - - for (auto raw_code : codes) { - auto common_code = raw_code.to_common_code().unwrap(); - data[common_code >> 32].emplace_back(static_cast(common_code)); - } - - // TODO: do sort process - - for (int head = 0; head < 16; ++head) { - std::stable_sort(data[head].begin(), data[head].end()); - } - - return data; -} - -Group Group::from_raw_code(codec::RawCode raw_code) { - - auto raw_codes = extend(raw_code); - - auto common_codes = raw_codes | std::views::transform([](const RawCode r) { - return r.to_common_code(); - }) | std::ranges::to(); // TODO: search min_element directly - - auto seed = std::min_element(common_codes.begin(), common_codes.end()); - - std::cout << *seed << std::endl; - - // TODO: search type_id / group_id from map - auto flat_id = std::find(GROUP_SEED.begin(), GROUP_SEED.end(), *seed) - GROUP_SEED.begin(); - std::cout << flat_id << std::endl; - - return Group::unsafe_create(0, 0); // TODO: only for compile -} +//RangesUnion Group::cases() const { +// +// // TODO: add white list for single-group unions +// // return GroupUnion::cases directly +// +// auto seed = CommonCode::unsafe_create(GROUP_SEED[flat_id()]); +// +// // std::cout << seed << std::endl; +// +// auto codes = Group_extend(seed.to_raw_code(), size()); +// +// // std::cout << codes.size() << std::endl; +// +// // TODO: how to reserve +// +// RangesUnion data; +// +// for (auto raw_code : codes) { +// auto common_code = raw_code.to_common_code().unwrap(); +// data[common_code >> 32].emplace_back(static_cast(common_code)); +// } +// +// // TODO: do sort process +// +// for (int head = 0; head < 16; ++head) { +// std::stable_sort(data[head].begin(), data[head].end()); +// } +// +// return data; +//} + +//Group Group::from_raw_code(codec::RawCode raw_code) { +// +// auto raw_codes = Group_extend(raw_code); +// +// auto common_codes = raw_codes | std::views::transform([](const RawCode r) { +// return r.to_common_code(); +// }) | std::ranges::to(); // TODO: search min_element directly +// +// auto seed = std::min_element(common_codes.begin(), common_codes.end()); +// +// std::cout << *seed << std::endl; +// +// // TODO: search type_id / group_id from map +// auto flat_id = std::find(GROUP_SEED.begin(), GROUP_SEED.end(), *seed) - GROUP_SEED.begin(); +// std::cout << flat_id << std::endl; +// +// return Group::unsafe_create(0, 0); // TODO: only for compile +//} diff --git a/src/core/group/internal/group.inl b/src/core/group/internal/group.inl index 0e57cfb..63b3aa5 100644 --- a/src/core/group/internal/group.inl +++ b/src/core/group/internal/group.inl @@ -6,54 +6,54 @@ namespace klotski::cases { // ----------------------------------------------------------------------------------------- // -constexpr uint32_t Group::size() const { - return GROUP_SIZE[flat_id()]; -} - -constexpr uint32_t Group::type_id() const { - return type_id_; -} - -constexpr uint32_t Group::group_id() const { - return group_id_; -} - -constexpr uint32_t Group::flat_id() const { - return GROUP_OFFSET[type_id_] + group_id_; -} +//constexpr uint32_t Group::size() const { +// return GROUP_SIZE[flat_id()]; +//} +// +//constexpr uint32_t Group::type_id() const { +// return type_id_; +//} +// +//constexpr uint32_t Group::group_id() const { +// return group_id_; +//} +// +//constexpr uint32_t Group::flat_id() const { +// return GROUP_OFFSET[type_id_] + group_id_; +//} // ----------------------------------------------------------------------------------------- // -constexpr Group Group::to_vertical_mirror() const { - return Group::unsafe_create(type_id_, GROUP_MIRROR_V[flat_id()]); -} - -constexpr Group Group::to_horizontal_mirror() const { - return Group::unsafe_create(type_id_, GROUP_MIRROR_H[flat_id()]); -} +//constexpr Group Group::to_vertical_mirror() const { +// return Group::unsafe_create(type_id_, GROUP_MIRROR_V[flat_id()]); +//} +// +//constexpr Group Group::to_horizontal_mirror() const { +// return Group::unsafe_create(type_id_, GROUP_MIRROR_H[flat_id()]); +//} // ----------------------------------------------------------------------------------------- // -inline Group Group::from_common_code(codec::CommonCode common_code) { - return from_raw_code(common_code.to_raw_code()); -} - -inline Group Group::from_short_code(codec::ShortCode short_code) { - return from_common_code(short_code.to_common_code()); -} +//inline Group Group::from_common_code(codec::CommonCode common_code) { +// return from_raw_code(common_code.to_raw_code()); +//} +// +//inline Group Group::from_short_code(codec::ShortCode short_code) { +// return from_common_code(short_code.to_common_code()); +//} // ----------------------------------------------------------------------------------------- // -constexpr Group Group::unsafe_create(const uint32_t type_id, const uint32_t group_id) { - return std::bit_cast(static_cast(group_id) << 32 | type_id); -} - -constexpr std::optional Group::create(const uint32_t type_id, const uint32_t group_id) { - if (type_id < TYPE_ID_LIMIT && group_id < GROUP_NUM[type_id]) { - return unsafe_create(type_id, group_id); - } - return std::nullopt; -} +//constexpr Group Group::unsafe_create(const uint32_t type_id, const uint32_t group_id) { +// return std::bit_cast(static_cast(group_id) << 32 | type_id); +//} +// +//constexpr std::optional Group::create(const uint32_t type_id, const uint32_t group_id) { +// if (type_id < TYPE_ID_LIMIT && group_id < GROUP_NUM[type_id]) { +// return unsafe_create(type_id, group_id); +// } +// return std::nullopt; +//} // ----------------------------------------------------------------------------------------- // diff --git a/src/core/group/internal/group_cases.cc b/src/core/group/internal/group_cases.cc index f4dbb24..99688f8 100644 --- a/src/core/group/internal/group_cases.cc +++ b/src/core/group/internal/group_cases.cc @@ -34,9 +34,9 @@ std::vector build_ranges_unions() { for (uint32_t type_id = 0; type_id < TYPE_ID_LIMIT; ++type_id) { auto group_union = GroupUnion::unsafe_create(type_id); - for (auto group : group_union.groups()) { - unions.emplace_back(group.cases()); - } +// for (auto group : group_union.groups()) { +// unions.emplace_back(group.cases()); +// } } return unions; } @@ -48,19 +48,19 @@ static std::vector build_tmp_data() { for (uint32_t type_id = 0; type_id < TYPE_ID_LIMIT; ++type_id) { auto group_union = GroupUnion::unsafe_create(type_id); - for (auto group : group_union.groups()) { - uint32_t group_id = group.group_id(); - - auto codes = group.cases().codes(); - for (uint32_t case_id = 0; case_id < codes.size(); ++case_id) { - auto short_code = codes[case_id].to_short_code(); - - data[short_code.unwrap()] = tmp_t { - .group_id = group_id, - .case_id = case_id, - }; - } - } +// for (auto group : group_union.groups()) { +// uint32_t group_id = group.group_id(); +// +// auto codes = group.cases().codes(); +// for (uint32_t case_id = 0; case_id < codes.size(); ++case_id) { +// auto short_code = codes[case_id].to_short_code(); +// +// data[short_code.unwrap()] = tmp_t { +// .group_id = group_id, +// .case_id = case_id, +// }; +// } +// } } return data; } diff --git a/src/core/group/internal/group_pro.cc b/src/core/group/internal/group_pro.cc index 1add55e..691ed3a 100644 --- a/src/core/group/internal/group_pro.cc +++ b/src/core/group/internal/group_pro.cc @@ -24,7 +24,7 @@ RangesUnion GroupPro::cases() const { seed = seed.to_vertical_mirror().to_horizontal_mirror(); } - auto codes = Group::extend(seed.to_raw_code(), size()); + auto codes = Group_extend(seed.to_raw_code(), size()); RangesUnion data; for (auto raw_code : codes) { @@ -56,7 +56,7 @@ static std::unordered_map build_map_data() { GroupPro GroupPro::from_raw_code(codec::RawCode raw_code) { static auto map_data = build_map_data(); - auto raw_codes = Group::extend(raw_code); + auto raw_codes = Group_extend(raw_code); auto common_codes = raw_codes | std::views::transform([](const RawCode r) { return r.to_common_code(); }) | std::ranges::to(); // TODO: search min_element directly diff --git a/src/core/group/internal/group_union.inl b/src/core/group/internal/group_union.inl index 91d4101..e49ee49 100644 --- a/src/core/group/internal/group_union.inl +++ b/src/core/group/internal/group_union.inl @@ -37,21 +37,21 @@ constexpr uint32_t GroupUnion::max_group_size() const { return MAX_GROUP_SIZE[type_id_]; } -inline std::vector GroupUnion::groups() const { - auto build = [this](const uint32_t group_id) { - return Group::unsafe_create(type_id_, group_id); - }; - return std::views::iota(0U, group_num()) - | std::views::transform(build) - | std::ranges::to(); -} - -inline std::optional GroupUnion::group(const uint32_t group_id) const { - if (group_id < group_num()) { - return Group::unsafe_create(type_id_, group_id); - } - return std::nullopt; -} +//inline std::vector GroupUnion::groups() const { +// auto build = [this](const uint32_t group_id) { +// return Group::unsafe_create(type_id_, group_id); +// }; +// return std::views::iota(0U, group_num()) +// | std::views::transform(build) +// | std::ranges::to(); +//} + +//inline std::optional GroupUnion::group(const uint32_t group_id) const { +// if (group_id < group_num()) { +// return Group::unsafe_create(type_id_, group_id); +// } +// return std::nullopt; +//} // ----------------------------------------------------------------------------------------- // // TODO: new interface diff --git a/src/core/main.cc b/src/core/main.cc index 5468f08..aa08b0b 100644 --- a/src/core/main.cc +++ b/src/core/main.cc @@ -26,7 +26,7 @@ using klotski::codec::ShortCode; using klotski::codec::CommonCode; using klotski::cases::GroupUnion; -using klotski::cases::Group; +//using klotski::cases::Group; using klotski::cases::GroupPro; using klotski::cases::GroupCases; using klotski::cases::GroupUnion; diff --git a/src/core_test/cases/group.cc b/src/core_test/cases/group.cc index dd515d6..38a4d3c 100644 --- a/src/core_test/cases/group.cc +++ b/src/core_test/cases/group.cc @@ -3,7 +3,7 @@ #include "group/group.h" #include "helper/cases.h" -using klotski::cases::Group; +//using klotski::cases::Group; using klotski::cases::GroupUnion; using klotski::cases::TYPE_ID_LIMIT; @@ -17,16 +17,16 @@ TEST(Group, cases) { for (uint32_t type_id = 0; type_id < TYPE_ID_LIMIT; ++type_id) { auto group_union = GroupUnion::unsafe_create(type_id); - for (auto group : group_union.groups()) { - - auto &expect = group_cases(type_id, group.group_id()); - auto codes = group.cases().codes(); - - EXPECT_EQ(codes, expect); - - EXPECT_EQ(codes.size(), group.size()); - - } +// for (auto group : group_union.groups()) { +// +// auto &expect = group_cases(type_id, group.group_id()); +// auto codes = group.cases().codes(); +// +// EXPECT_EQ(codes, expect); +// +// EXPECT_EQ(codes.size(), group.size()); +// +// } } } diff --git a/src/core_test/cases/group_union.cc b/src/core_test/cases/group_union.cc index 03b464a..8c6f56a 100644 --- a/src/core_test/cases/group_union.cc +++ b/src/core_test/cases/group_union.cc @@ -15,7 +15,7 @@ using klotski::codec::ShortCode; -using klotski::cases::Group; +//using klotski::cases::Group; using klotski::cases::GroupPro; using klotski::cases::GroupUnion; @@ -42,25 +42,25 @@ TEST(GroupUnion, basic) { EXPECT_FALSE(GroupUnion::create(TYPE_ID_LIMIT).has_value()); GROUP_UNION_PARALLEL({ - const auto groups = group_union.groups(); + const auto groups = group_union.groups_pro(); EXPECT_EQ(groups.size(), group_union.group_num()); auto get_type_id = [](const auto g) { return g.type_id(); }; const auto type_ids = groups | std::views::transform(get_type_id) | std::ranges::to(); EXPECT_REPEAT(type_ids, group_union.unwrap()); - auto get_group_id = [](const auto g) { return g.group_id(); }; - const auto group_ids = groups | std::views::transform(get_group_id) | std::ranges::to(); - EXPECT_IOTA(group_ids); - - for (const auto g : group_union.groups()) { - auto type_id = g.type_id(); - auto group_id = g.group_id(); - EXPECT_TRUE(group_union.group(group_id).has_value()); - EXPECT_EQ(group_union.group(group_id)->type_id(), type_id); - EXPECT_EQ(group_union.group(group_id)->group_id(), group_id); - } - EXPECT_FALSE(group_union.group(group_union.group_num()).has_value()); +// auto get_group_id = [](const auto g) { return g.group_id(); }; +// const auto group_ids = groups | std::views::transform(get_group_id) | std::ranges::to(); +// EXPECT_IOTA(group_ids); + +// for (const auto g : group_union.groups()) { +// auto type_id = g.type_id(); +// auto group_id = g.group_id(); +// EXPECT_TRUE(group_union.group(group_id).has_value()); +// EXPECT_EQ(group_union.group(group_id)->type_id(), type_id); +// EXPECT_EQ(group_union.group(group_id)->group_id(), group_id); +// } +// EXPECT_FALSE(group_union.group(group_union.group_num()).has_value()); }); } @@ -88,7 +88,7 @@ TEST(GroupUnion, values) { EXPECT_EQ(group_union.group_num(), group_num(type_id)); auto get_group_size = [](auto g) { return g.size(); }; - const auto sizes = group_union.groups() | std::views::transform(get_group_size); + const auto sizes = group_union.groups_pro() | std::views::transform(get_group_size); EXPECT_EQ(group_union.max_group_size(), *std::ranges::max_element(sizes)); }); } @@ -104,7 +104,7 @@ TEST(GroupUnion, values_pro) { EXPECT_EQ(group_union.pattern_num(), helper::group_union_pattern_num(type_id)); auto get_group_size = [](auto g) { return g.size(); }; - const auto sizes = group_union.groups() | std::views::transform(get_group_size); + const auto sizes = group_union.groups_pro() | std::views::transform(get_group_size); EXPECT_EQ(group_union.max_group_size(), *std::ranges::max_element(sizes)); auto groups = group_union.groups_pro(); diff --git a/src/core_test/cases/helper/group_impl.cc b/src/core_test/cases/helper/group_impl.cc index f5aa338..a8b59f4 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::cases::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 989ad24..c6a375a 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::cases::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 e965a6c..37c6bf5 100644 --- a/src/core_test/helper/internal/parallel.cc +++ b/src/core_test/helper/internal/parallel.cc @@ -9,9 +9,9 @@ using klotski::cases::AllCases; using klotski::cases::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 982237d..a80e5da 100644 --- a/src/core_test/helper/parallel.h +++ b/src/core_test/helper/parallel.h @@ -9,7 +9,7 @@ #include "short_code/short_code.h" #include "common_code/common_code.h" -using klotski::cases::Group; +//using klotski::cases::Group; using klotski::cases::GroupUnion; using klotski::codec::RawCode; @@ -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);