Browse Source

update: adjust the class name

legacy
Dnomd343 2 years ago
parent
commit
cdfbf9a93b
  1. 14
      src/klotski_core/group/basic_id.cc
  2. 24
      src/klotski_core/group/build_cases.cc
  3. 53
      src/klotski_core/group/group.h
  4. 9
      src/klotski_core/group/group_info.cc
  5. 8
      src/klotski_core/group/seeds.cc

14
src/klotski_core/group/basic_id.cc

@ -83,28 +83,28 @@ TypeId::block_num_t TypeId::block_num(const CommonCode &common_code) noexcept {
return result;
}
/// ---------------------------------------- Group ID -----------------------------------------
/// ------------------------------------------ Group ------------------------------------------
GroupId::GroupId(uint32_t type_id, uint32_t group_id) : type_id_(type_id) {
Group::Group(uint32_t type_id, uint32_t group_id) : type_id_(type_id) {
if (group_id >= TYPE_ID_GROUP_NUM[type_id]) {
throw std::invalid_argument("group id overflow");
}
group_id_ = group_id;
}
GroupId::GroupId(const TypeId &type_id, uint32_t group_id) : type_id_(type_id) {
Group::Group(const TypeId &type_id, uint32_t group_id) : type_id_(type_id) {
if (group_id >= TYPE_ID_GROUP_NUM[type_id.unwrap()]) {
throw std::invalid_argument("group id overflow");
}
group_id_ = group_id;
}
GroupId::GroupId(const RawCode &raw_code) noexcept : type_id_(TypeId(raw_code)) {
group_id_ = group_id(type_id_.unwrap(), GroupId::seed(raw_code));
Group::Group(const RawCode &raw_code) noexcept : type_id_(TypeId(raw_code)) {
group_id_ = group_id(type_id_.unwrap(), Group::seed(raw_code));
}
GroupId::GroupId(const CommonCode &common_code) noexcept : type_id_(TypeId(common_code)) {
group_id_ = group_id(type_id_.unwrap(), GroupId::seed(common_code));
Group::Group(const CommonCode &common_code) noexcept : type_id_(TypeId(common_code)) {
group_id_ = group_id(type_id_.unwrap(), Group::seed(common_code));
}
} // namespace klotski

24
src/klotski_core/group/build_cases.cc

@ -4,8 +4,8 @@
#include "common.h"
#include "type_id.h"
#include "common_code.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/btree_set.h"
#include "absl/container/flat_hash_map.h"
namespace klotski {
@ -49,7 +49,7 @@ std::vector<CommonCode> TypeId::cases() const noexcept {
}
std::vector<std::vector<CommonCode>> TypeId::groups() const noexcept {
auto all_cases = cases();
auto all_cases = TypeId::cases();
std::vector<std::vector<CommonCode>> groups;
auto min = std::min_element(all_cases.begin(), all_cases.end()); // search min CommonCode
@ -78,23 +78,17 @@ std::vector<std::vector<CommonCode>> TypeId::groups() const noexcept {
return groups;
}
/// ---------------------------------------- Group ID -----------------------------------------
/// --------------------------------------- Group Size ----------------------------------------
uint32_t GroupId::size() const noexcept {
uint32_t Group::size() const noexcept {
return size(seed());
}
uint32_t GroupId::size(const CommonCode &common_code) noexcept {
uint32_t Group::size(const CommonCode &common_code) noexcept {
return size(common_code.to_raw_code());
}
std::vector<RawCode> GroupId::cases() const noexcept {
// auto cases = Group::cases(seed());
// return {cases.begin(), cases.end()};
return Group::cases(seed());
}
uint32_t GroupId::size(const RawCode &raw_code) noexcept {
uint32_t Group::size(const RawCode &raw_code) noexcept {
std::queue<uint64_t> cache({raw_code.unwrap()});
absl::flat_hash_map<uint64_t, uint64_t> cases; // <code, mask>
cases.reserve(TypeId::group_max_size(raw_code));
@ -118,7 +112,11 @@ uint32_t GroupId::size(const RawCode &raw_code) noexcept {
return cases.size();
}
/// ------------------------------------------ Group ------------------------------------------
/// --------------------------------------- Group Cases ---------------------------------------
std::vector<RawCode> Group::cases() const noexcept {
return cases(seed());
}
std::vector<RawCode> Group::cases(const CommonCode &common_code) noexcept {
return cases(common_code.to_raw_code());

53
src/klotski_core/group/group.h

@ -59,19 +59,19 @@ public:
std::vector<std::vector<CommonCode>> groups() const noexcept;
};
/// ---------------------------------------- Group ID -----------------------------------------
/// ------------------------------------------ Group ------------------------------------------
class GroupId {
class Group {
TypeId type_id_;
uint32_t group_id_;
static uint32_t group_id(uint32_t type_id, const CommonCode &seed) noexcept;
public:
GroupId(uint32_t type_id, uint32_t group_id);
GroupId(const TypeId &type_id, uint32_t group_id);
explicit GroupId(const RawCode &raw_code) noexcept;
explicit GroupId(const CommonCode &common_code) noexcept;
Group(uint32_t type_id, uint32_t group_id);
Group(const TypeId &type_id, uint32_t group_id);
explicit Group(const RawCode &raw_code) noexcept;
explicit Group(const CommonCode &common_code) noexcept;
/// Release raw type_id / group_id value.
constexpr uint32_t unwrap() const noexcept { return group_id_; }
@ -82,18 +82,20 @@ public:
static uint32_t size(const RawCode &raw_code) noexcept;
static uint32_t size(const CommonCode &common_code) noexcept;
/// Get the minimum CommonCode of the current group.
/// Get the minimum CommonCode.
CommonCode seed() const noexcept;
static CommonCode seed(const RawCode &raw_code) noexcept;
static CommonCode seed(const CommonCode &common_code) noexcept;
/// Calculate the specified group.
/// Calculate the current group.
std::vector<RawCode> cases() const noexcept;
static std::vector<RawCode> cases(const RawCode &raw_code) noexcept;
static std::vector<RawCode> cases(const CommonCode &common_code) noexcept;
};
/// ------------------------------------------ Group ------------------------------------------
/// --------------------------------------- Group Case ----------------------------------------
class Group {
class GroupCase {
public:
struct info_t {
uint16_t type_id;
@ -101,16 +103,12 @@ public:
uint32_t group_index;
};
/// Search for all derivatives that a case can produce.
static std::vector<RawCode> cases(const RawCode &raw_code) noexcept;
static std::vector<RawCode> cases(const CommonCode &common_code) noexcept;
/// Get the CommonCode using the group info.
static CommonCode parse(const info_t &info);
/// Get group info according to specified case.
static info_t info(const RawCode &raw_code);
static info_t info(const CommonCode &common_code);
/// Get the CommonCode according to the group info.
static CommonCode resolve(const GroupId &group_id, uint32_t group_index);
static info_t encode(const RawCode &raw_code) noexcept;
static info_t encode(const CommonCode &common_code) noexcept;
};
/// ---------------------------------------- Operators ----------------------------------------
@ -119,24 +117,21 @@ inline bool operator==(const TypeId &t1, const TypeId &t2) {
return t1.unwrap() == t2.unwrap();
}
inline bool operator!=(const TypeId &t1, const TypeId &t2) {
return t1.unwrap() != t2.unwrap();
}
inline bool operator==(const GroupId &g1, const GroupId &g2) {
inline bool operator==(const Group &g1, const Group &g2) {
return g1.type_id() == g2.type_id() && g1.unwrap() == g2.unwrap();
}
inline bool operator!=(const GroupId &g1, const GroupId &g2) {
return g1.type_id() != g2.type_id() || g1.unwrap() != g2.unwrap();
}
inline bool operator==(const TypeId::block_num_t &b1, const TypeId::block_num_t &b2) {
return (b1.n_1x1 == b2.n_1x1) && (b1.n_1x2 == b2.n_1x2) && (b1.n_2x1 == b2.n_2x1);
}
inline bool operator!=(const TypeId::block_num_t &b1, const TypeId::block_num_t &b2) {
return (b1.n_1x1 != b2.n_1x1) || (b1.n_1x2 != b2.n_1x2) || (b1.n_2x1 != b2.n_2x1);
inline bool operator==(const GroupCase::info_t &i1, const GroupCase::info_t &i2) {
return (i1.type_id == i2.type_id) && (i1.group_id == i2.group_id) && (i1.group_index == i2.group_index);
}
inline bool operator!=(const Group &g1, const Group &g2) { return !(g1 == g2); }
inline bool operator!=(const TypeId &t1, const TypeId &t2) { return !(t1 == t2); }
inline bool operator!=(const GroupCase::info_t &i1, const GroupCase::info_t &i2) { return !(i1 == i2); }
inline bool operator!=(const TypeId::block_num_t &b1, const TypeId::block_num_t &b2) { return !(b1 == b2); }
} // namespace klotski

9
src/klotski_core/group/group_info.cc

@ -33,22 +33,23 @@ uint32_t TypeId::group_max_size(const CommonCode &common_code) noexcept {
return TypeId(common_code).group_max_size();
}
/// ------------------------------------------ Group ------------------------------------------
/// --------------------------------------- Group Case ----------------------------------------
Group::info_t Group::info(const RawCode &raw_code) {
GroupCase::info_t GroupCase::encode(const RawCode &raw_code) noexcept {
// TODO: function body
return {};
}
Group::info_t Group::info(const CommonCode &common_code) {
GroupCase::info_t GroupCase::encode(const CommonCode &common_code) noexcept {
// TODO: function body
return {};
}
CommonCode Group::resolve(const GroupId &group_id, uint32_t group_index) {
CommonCode GroupCase::parse(const info_t &info) {
// TODO: check group index

8
src/klotski_core/group/seeds.cc

@ -12,23 +12,23 @@ std::vector<CommonCode> TypeId::seeds() const noexcept {
return {offset, offset + TYPE_ID_GROUP_NUM[type_id_]};
}
CommonCode GroupId::seed() const noexcept { // group_id -> seed
CommonCode Group::seed() const noexcept { // group_id -> seed
auto offset = TYPE_ID_OFFSET[type_id_.unwrap()]; // type id offset
auto index = offset + GROUP_SEEDS_INDEX[offset + group_id_];
return CommonCode::unsafe_create(GROUP_SEEDS[index]);
}
CommonCode GroupId::seed(const RawCode &raw_code) noexcept {
CommonCode Group::seed(const RawCode &raw_code) noexcept {
auto cases = Group::cases(raw_code);
std::vector<CommonCode> group(cases.begin(), cases.end());
return *std::min_element(group.begin(), group.end());
}
CommonCode GroupId::seed(const CommonCode &common_code) noexcept {
CommonCode Group::seed(const CommonCode &common_code) noexcept {
return seed(common_code.to_raw_code());
}
uint32_t GroupId::group_id(uint32_t type_id, const CommonCode &seed) noexcept { // seed -> group_id
uint32_t Group::group_id(uint32_t type_id, const CommonCode &seed) noexcept { // seed -> group_id
auto start = GROUP_SEEDS + TYPE_ID_OFFSET[type_id];
auto end = start + TYPE_ID_GROUP_NUM[type_id];
auto index = std::lower_bound(start, end, seed.unwrap()) - GROUP_SEEDS;

Loading…
Cancel
Save