Browse Source

remove: legacy group impl

legacy
Dnomd343 2 months ago
parent
commit
a0c495326d
  1. 55
      src/core/group/group.h
  2. 103
      src/core/group/internal/group.cc
  3. 78
      src/core/group/internal/group.inl
  4. 32
      src/core/group/internal/group_cases.cc
  5. 4
      src/core/group/internal/group_pro.cc
  6. 30
      src/core/group/internal/group_union.inl
  7. 2
      src/core/main.cc
  8. 22
      src/core_test/cases/group.cc
  9. 32
      src/core_test/cases/group_union.cc
  10. 2
      src/core_test/cases/helper/group_impl.cc
  11. 2
      src/core_test/helper/internal/group.cc
  12. 4
      src/core_test/helper/internal/parallel.cc
  13. 4
      src/core_test/helper/parallel.h

55
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<codec::RawCode> RawCodes;
typedef std::vector<codec::CommonCode> 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<Group> groups() const;
// [[nodiscard]] std::vector<Group> groups() const;
/// Get the group instance with the specified group id.
[[nodiscard]] std::optional<Group> group(uint32_t group_id) const;
// [[nodiscard]] std::optional<Group> 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<Group> create(uint32_t type_id, uint32_t group_id);
// static constexpr std::optional<Group> 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<codec::RawCode> extend(codec::RawCode raw_code, uint32_t reserve = 0);
};
std::vector<codec::RawCode> Group_extend(codec::RawCode raw_code, uint32_t reserve = 0);
class GroupCasesPro {
public:

103
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<RawCode> Group::extend(RawCode raw_code, uint32_t reserve) {
std::vector<RawCode> klotski::cases::Group_extend(RawCode raw_code, uint32_t reserve) {
std::vector<RawCode> codes;
absl::flat_hash_map<uint64_t, uint64_t> cases; // <code, mask>
reserve = reserve ? reserve : GroupUnion::from_raw_code(raw_code).max_group_size();
@ -36,52 +37,52 @@ std::vector<RawCode> 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<uint32_t>(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<std::vector>(); // 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<uint32_t>(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<std::vector>(); // 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
//}

78
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<Group>(static_cast<uint64_t>(group_id) << 32 | type_id);
}
constexpr std::optional<Group> 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<Group>(static_cast<uint64_t>(group_id) << 32 | type_id);
//}
//
//constexpr std::optional<Group> 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;
//}
// ----------------------------------------------------------------------------------------- //

32
src/core/group/internal/group_cases.cc

@ -34,9 +34,9 @@ std::vector<RangesUnion> 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<tmp_t> 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;
}

4
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<uint64_t, GroupPro> 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<std::vector>(); // TODO: search min_element directly

30
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<Group> 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<std::vector>();
}
inline std::optional<Group> 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<Group> 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<std::vector>();
//}
//inline std::optional<Group> 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

2
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;

22
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());
//
// }
}
}

32
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<std::vector>();
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<std::vector>();
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<std::vector>();
// 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();

2
src/core_test/cases/helper/group_impl.cc

@ -7,7 +7,7 @@
/// Extend ordered Group from the specified CommonCode seed.
static std::vector<CommonCode> 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<CommonCode> common_codes {raw_codes.begin(), raw_codes.end()};
std::ranges::sort(common_codes.begin(), common_codes.end());
return common_codes;

2
src/core_test/helper/internal/group.cc

@ -53,7 +53,7 @@ const std::vector<CommonCode>& helper::group_union_cases(const uint32_t type_id)
/// Extend ordered Group from the specified CommonCode seed.
static std::vector<CommonCode> 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<CommonCode> common_codes {raw_codes.begin(), raw_codes.end()};
std::ranges::sort(common_codes.begin(), common_codes.end());
return common_codes;

4
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<void(Group)> &&func) {
//void helper::group_parallel(std::function<void(Group)> &&func) {
// TODO: spawn all Groups
}
//}
void helper::type_id_parallel(std::function<void(uint32_t type_id)> &&func) {

4
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<void(Group group)> &&func);
//void group_parallel(std::function<void(Group group)> &&func);
/// Spawn all valid type_ids in parallel.
void type_id_parallel(std::function<void(uint32_t type_id)> &&func);

Loading…
Cancel
Save