Browse Source

feat: build single group

master
Dnomd343 2 years ago
parent
commit
5c710620af
  1. 12
      src/klotski_core/ffi/tmain.cc
  2. 23
      src/klotski_core/group/build_cases.cc
  3. 3
      src/klotski_core/group/group.h
  4. 4
      test/group/build_cases.cc

12
src/klotski_core/ffi/tmain.cc

@ -33,6 +33,18 @@ using klotski::TYPE_ID_LIMIT;
void tmain() {
// printf("tmain start\n");
auto ret = Group::build_group(123, 0);
std::cout << ret.size() << std::endl;
std::cout << ret[0] << std::endl;
std::cout << ret[1] << std::endl;
std::cout << ret[2] << std::endl;
std::cout << ret[3] << std::endl;
return;
// struct group_info_t {
// uint16_t type_id;
// uint16_t group_id;

23
src/klotski_core/group/build_cases.cc

@ -74,17 +74,34 @@ std::vector<RawCode> Group::group_cases(const RawCode &seed) {
return result;
}
std::vector<CommonCode> Group::build_group(uint32_t type_id, uint32_t group_id) {
uint32_t group_num = 0;
auto all_cases = Group::all_cases(type_id); // load all cases of type_id
std::set<CommonCode> cases(all_cases.begin(), all_cases.end());
while (!cases.empty()) {
if (group_id == group_num) { // found target group
auto group = group_cases(cases.begin()->to_raw_code());
return {group.begin(), group.end()};
}
for (auto &&tmp : group_cases(cases.begin()->to_raw_code())) {
cases.erase(tmp.to_common_code()); // remove from global union
}
++group_num;
}
return {}; // group_id out of range
}
std::vector<std::vector<CommonCode>> Group::build_groups(uint32_t type_id) {
auto all_cases = Group::all_cases(type_id);
std::vector<std::vector<CommonCode>> groups;
auto min = std::min_element(all_cases.begin(), all_cases.end()); // search min CommonCode
auto first_group = Group::group_cases(min->to_raw_code()); // expand the first group
auto first_group = group_cases(min->to_raw_code()); // expand the first group
groups.emplace_back(first_group.begin(), first_group.end());
if (first_group.size() == all_cases.size()) { // only contains one group
return groups;
}
// TODO: do not insert the elements in first group
std::set<CommonCode> cases(all_cases.begin(), all_cases.end());
for (auto &&tmp : groups[0]) {
cases.erase(tmp); // remove elements in first group
@ -92,7 +109,7 @@ std::vector<std::vector<CommonCode>> Group::build_groups(uint32_t type_id) {
while (!cases.empty()) {
groups.emplace_back(); // create empty vector
auto current_group = groups.end() - 1; // insert into latest
for (auto &&tmp : Group::group_cases(cases.begin()->to_raw_code())) {
for (auto &&tmp : group_cases(cases.begin()->to_raw_code())) {
auto common_code = tmp.to_common_code();
current_group->emplace_back(common_code); // insert into current group
cases.erase(common_code); // remove from global union

3
src/klotski_core/group/group.h

@ -40,6 +40,9 @@ public:
/// Calculate all groups in the specified type_id.
static std::vector<std::vector<CommonCode>> build_groups(uint32_t type_id);
/// Calculate the specified group using type_id and group_id.
static std::vector<CommonCode> build_group(uint32_t type_id, uint32_t group_id);
/// ---------------------------- xxxxxxxxxxxxxxxxx ----------------------------

4
test/group/build_cases.cc

@ -69,6 +69,10 @@ TEST(Group, group_cases) {
EXPECT_EQ(all_cases, AllCases::release()); // verify all released cases
}
TEST(Group, build_group) {
}
TEST(Group, build_groups) {
struct group_info_t {
uint16_t group_id;

Loading…
Cancel
Save