Browse Source

feat: spawn Group from cases

master
Dnomd343 2 months ago
parent
commit
ce91b0810a
  1. 21
      src/core/group/group.h
  2. 20
      src/core/group/internal/group.cc
  3. 32
      src/core/group/internal/group.inl
  4. 8
      src/core/group/internal/group_union.inl
  5. 42
      src/core/main.cc

21
src/core/group/group.h

@ -145,6 +145,8 @@ private:
// ------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------- //
}; };
// TODO: add debug output
class Group { class Group {
public: public:
Group() = delete; Group() = delete;
@ -152,25 +154,25 @@ public:
// ------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------- //
/// Get the original type id. /// Get the original type id.
[[nodiscard]] uint32_t type_id() const; [[nodiscard]] constexpr uint32_t type_id() const;
/// Get the original group id. /// Get the original group id.
[[nodiscard]] uint32_t group_id() const; [[nodiscard]] constexpr uint32_t group_id() const;
/// Create Group without any check. /// Create Group without any check.
static 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. /// Create Group with validity check.
static 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 the number of cases contained. /// Get all cases under current group.
[[nodiscard]] uint32_t size() const;
/// Get all cases under the current group.
[[nodiscard]] RangesUnion cases() const; [[nodiscard]] RangesUnion cases() const;
/// Get the number of klotski cases contained.
[[nodiscard]] constexpr uint32_t size() const;
// ------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------- //
/// Create Group from RawCode. /// Create Group from RawCode.
@ -188,7 +190,8 @@ private:
uint32_t type_id_; uint32_t type_id_;
uint32_t group_id_; uint32_t group_id_;
[[nodiscard]] uint32_t flat_id() const; /// Tiled merge of type_id and group_id.
[[nodiscard]] constexpr uint32_t flat_id() const;
public: public:
/// Spawn all the unsorted codes of the current group. /// Spawn all the unsorted codes of the current group.

20
src/core/group/internal/group.cc

@ -38,6 +38,7 @@ std::vector<RawCode> Group::extend(RawCode raw_code, uint32_t reserve) {
RangesUnion Group::cases() const { RangesUnion Group::cases() const {
// TODO: add white list for single-group unions // TODO: add white list for single-group unions
// return GroupUnion::cases directly
auto seed = CommonCode::unsafe_create(GROUP_SEED[flat_id()]); auto seed = CommonCode::unsafe_create(GROUP_SEED[flat_id()]);
@ -60,3 +61,22 @@ RangesUnion Group::cases() const {
return data; 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
}

32
src/core/group/internal/group.inl

@ -4,31 +4,47 @@
namespace klotski::cases { namespace klotski::cases {
inline uint32_t Group::size() const { // ----------------------------------------------------------------------------------------- //
constexpr uint32_t Group::size() const {
return GROUP_SIZE[flat_id()]; return GROUP_SIZE[flat_id()];
} }
inline uint32_t Group::flat_id() const { 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_; return GROUP_OFFSET[type_id_] + group_id_;
} }
inline uint32_t Group::type_id() const { // ----------------------------------------------------------------------------------------- //
return type_id_;
inline Group Group::from_common_code(codec::CommonCode common_code) {
return from_raw_code(common_code.to_raw_code());
} }
inline uint32_t Group::group_id() const { inline Group Group::from_short_code(codec::ShortCode short_code) {
return group_id_; return from_common_code(short_code.to_common_code());
} }
inline Group Group::unsafe_create(const uint32_t type_id, const uint32_t group_id) { // ----------------------------------------------------------------------------------------- //
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); return std::bit_cast<Group>(static_cast<uint64_t>(group_id) << 32 | type_id);
} }
inline std::optional<Group> Group::create(const uint32_t type_id, const uint32_t group_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]) { if (type_id < TYPE_ID_LIMIT && group_id < GROUP_NUM[type_id]) {
return unsafe_create(type_id, group_id); return unsafe_create(type_id, group_id);
} }
return std::nullopt; return std::nullopt;
} }
// ----------------------------------------------------------------------------------------- //
} // namespace klotski::cases } // namespace klotski::cases

8
src/core/group/internal/group_union.inl

@ -6,7 +6,7 @@
namespace klotski::cases { namespace klotski::cases {
// ------------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------------- //
constexpr uint32_t GroupUnion::unwrap() const { constexpr uint32_t GroupUnion::unwrap() const {
return type_id_; return type_id_;
@ -23,7 +23,7 @@ constexpr std::optional<GroupUnion> GroupUnion::create(const uint32_t type_id) {
return std::nullopt; return std::nullopt;
} }
// ------------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------------- //
constexpr uint32_t GroupUnion::size() const { constexpr uint32_t GroupUnion::size() const {
return GROUP_UNION_SIZE[type_id_]; return GROUP_UNION_SIZE[type_id_];
@ -53,7 +53,7 @@ inline std::optional<Group> GroupUnion::group(const uint32_t group_id) const {
return std::nullopt; return std::nullopt;
} }
// ------------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------------- //
inline GroupUnion GroupUnion::from_raw_code(const codec::RawCode raw_code) { inline GroupUnion GroupUnion::from_raw_code(const codec::RawCode raw_code) {
return unsafe_create(type_id(raw_code)); return unsafe_create(type_id(raw_code));
@ -67,6 +67,6 @@ inline GroupUnion GroupUnion::from_common_code(const codec::CommonCode common_co
return unsafe_create(type_id(common_code)); return unsafe_create(type_id(common_code));
} }
// ------------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------------- //
} // namespace klotski::cases } // namespace klotski::cases

42
src/core/main.cc

@ -40,48 +40,10 @@ int main() {
const auto start = std::chrono::system_clock::now(); const auto start = std::chrono::system_clock::now();
// auto ret = CommonCode::is_mirror(0x1A9BF0C00); const auto common_code = CommonCode::unsafe_create(0x1A9BF0C00);
// auto ret = CommonCode::is_mirror(0x4FEA13400); const auto group = Group::from_common_code(common_code);
// auto ret = CommonCode::is_mirror(0x100AA0300);
// auto ret = CommonCode::is_mirror(0x5000A0000);
// std::cout << "ret = " << ret << std::endl;
// auto raw_code = CommonCode::unsafe_create(0x1A9BF0C00).to_raw_code();
// std::cout << raw_code.is_horizontal_mirror() << std::endl;
for (auto common_code : AllCases::instance().fetch().codes()) {
auto raw_code = common_code.to_raw_code();
// bool r1 = raw_code.is_horizontal_mirror();
// bool r2 = common_code.is_horizontal_mirror();
//
// if (r1 != r2) {
// std::cout << "found invalid" << std::endl;
// std::cout << common_code << std::endl;
// break;
// }
//
// // if (raw_code.is_vertical_mirror()) {
// // std::cout << "found invalid" << std::endl;
// // break;
// // }
// auto rr = raw_code.to_vertical_mirror();
// auto cc = common_code.to_vertical_mirror();
auto rr = raw_code.to_horizontal_mirror();
auto cc = common_code.to_horizontal_mirror();
if (cc.to_raw_code() != rr) {
std::cout << "found invalid" << std::endl;
std::cout << common_code << std::endl;
break;
}
}
// GroupCases::from_info_t({169, 1, 7472}); // GroupCases::from_info_t({169, 1, 7472});
// GroupCases::to_info_t(CommonCode::unsafe_create(0x1A9BF0C00).to_short_code()); // GroupCases::to_info_t(CommonCode::unsafe_create(0x1A9BF0C00).to_short_code());
std::cerr << std::chrono::system_clock::now() - start << std::endl; std::cerr << std::chrono::system_clock::now() - start << std::endl;

Loading…
Cancel
Save