Browse Source

update: using `klotski::group` namespace

master
Dnomd343 3 weeks ago
parent
commit
e3b5987d26
  1. 2
      src/core/all_cases/internal/basic_ranges.cc
  2. 4
      src/core/fast_cal/internal/fast_cal.inl
  3. 80
      src/core/group/group.h
  4. 4
      src/core/group/internal/constant/group.h
  5. 4
      src/core/group/internal/constant/group_union.h
  6. 7
      src/core/group/internal/extend.cc
  7. 6
      src/core/group/internal/group.cc
  8. 4
      src/core/group/internal/group.inl
  9. 12
      src/core/group/internal/group_cases.cc
  10. 8
      src/core/group/internal/group_cases.inl
  11. 7
      src/core/group/internal/group_union.cc
  12. 4
      src/core/group/internal/group_union.inl
  13. 15
      src/core/main.cc
  14. 4
      src/core_test/cases/basic_ranges.cc
  15. 4
      src/core_test/cases/group_legacy.cc
  16. 4
      src/core_test/cases/group_pro.cc
  17. 8
      src/core_test/cases/group_union.cc
  18. 2
      src/core_test/cases/helper/cases.h
  19. 2
      src/core_test/cases/helper/group_impl.cc
  20. 2
      src/core_test/helper/internal/group.cc
  21. 6
      src/core_test/helper/internal/parallel.cc
  22. 6
      src/core_test/helper/parallel.h

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

4
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<size_t>(reserve * 1.56));
cases_.emplace(code, info_t {0, nil}); // without mask
}

80
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<Group>;
#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<Group>;
/// 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> 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<GroupUnion>);
static_assert(std::is_standard_layout_v<GroupCases::CaseInfo>);
static_assert(std::is_trivially_copyable_v<GroupCases::CaseInfo>);
} // 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<GroupCases::CaseInfo>);
namespace std {
template <>
struct std::hash<klotski::cases::Group> {
constexpr std::size_t operator()(const klotski::cases::Group &g) const noexcept {
struct std::hash<klotski::group::Group> {
constexpr std::size_t operator()(const klotski::group::Group &g) const noexcept {
// TODO: perf hash alg
return std::hash<uint64_t>{}(g.type_id() ^ g.pattern_id() ^ (int)g.toward());
}
};
template <>
struct std::hash<klotski::cases::GroupUnion> {
constexpr std::size_t operator()(const klotski::cases::GroupUnion &gu) const noexcept {
struct std::hash<klotski::group::GroupUnion> {
constexpr std::size_t operator()(const klotski::group::GroupUnion &gu) const noexcept {
return std::hash<uint32_t>{}(gu.unwrap());
}
};
// TODO: add `std::hash` for CaseInfo
} // namespace std
// ----------------------------------------------------------------------------------------- //

4
src/core/group/internal/constant/group.h

@ -2,7 +2,7 @@
#include <array>
namespace klotski::cases {
namespace klotski::group {
// seed(36) + size(20) + type(3)
@ -16,4 +16,4 @@ constexpr auto GROUP_DATA = std::to_array<uint64_t>({
#include "group.inc"
});
} // namespace klotski::cases
} // namespace klotski::group

4
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<uint16_t>({
@ -187,4 +187,4 @@ constexpr auto GROUP_UNION_CASES_NUM = std::to_array<std::tuple<uint32_t, uint32
{60 , 40 , 28 , 16 }, {42 , 42 , 15 , 15 },
});
} // namespace klotski::cases
} // namespace klotski::group

7
src/core/group/internal/extend.cc

@ -1,21 +1,18 @@
// #include <absl/container/flat_hash_map.h>
#include <parallel_hashmap/phmap.h>
#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<RawCode> klotski::cases::Group_extend(RawCode raw_code, uint32_t reserve) {
std::vector<RawCode> klotski::group::Group_extend(RawCode raw_code, uint32_t reserve) {
std::vector<RawCode> codes;
phmap::flat_hash_map<uint64_t, uint64_t> cases; // <code, mask>
// reserve = reserve ? reserve : GroupUnion::from_raw_code(raw_code).max_group_size();

6
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 {

4
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

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

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

@ -2,7 +2,7 @@
#include <format>
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> GroupCases::CaseInfo::create(klotski::cases::Group group, uint32_t case_id) {
inline std::optional<GroupCases::CaseInfo> 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

7
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])

4
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

15
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();

4
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_)

4
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

4
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) {

8
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()); \

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

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::group::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::group::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;

6
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<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) {

6
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<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