From fe6f83e3a52c83e56c2a72450d21090058b41e08 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 9 Apr 2023 16:36:01 +0800 Subject: [PATCH] feat: type id convert --- src/klotski_core/ffi/tmain.cc | 6 ++- src/klotski_core/group/group.cc | 68 +++++++++++++------------------- src/klotski_core/group/group.h | 13 ++++++ src/klotski_core/group/type_id.h | 2 +- 4 files changed, 46 insertions(+), 43 deletions(-) diff --git a/src/klotski_core/ffi/tmain.cc b/src/klotski_core/ffi/tmain.cc index 2ded2ca..21821d7 100644 --- a/src/klotski_core/ffi/tmain.cc +++ b/src/klotski_core/ffi/tmain.cc @@ -23,7 +23,11 @@ using klotski::Benchmark; void tmain() { - Group::block_num(123); + auto r = Group::block_num(123); + + std::cout << (int)r.n_1x1 << " " << (int)r.n_1x2 << " " << (int)r.n_2x1 << std::endl; + + std::cout << Group::type_id(r) << std::endl; // Group::block_num(CommonCode(0x1A9BF0C00)); // Group::block_num(RawCode::from_common_code(0x1A9BF0C00)); diff --git a/src/klotski_core/group/group.cc b/src/klotski_core/group/group.cc index ae10ee4..54647d6 100644 --- a/src/klotski_core/group/group.cc +++ b/src/klotski_core/group/group.cc @@ -12,47 +12,22 @@ namespace klotski { using Common::range_reverse; Group::block_num_t Group::block_num(uint32_t type_id) { - - // jiang_num (n_x2x) - // bing_num (n_1x1) - // style_num (n_2x1) - - auto tids = std::vector(); - - uint32_t count = 0; - for (int n_x2x = 0; n_x2x <= 7; ++n_x2x) - for (int n_2x1 = 0; n_2x1 <= n_x2x; ++n_2x1) - for (int n_1x1 = 0; n_1x1 <= (14 - n_x2x * 2); ++n_1x1) { - ++count; - - uint32_t tid = (n_x2x << 8) | (n_2x1 << 4) | n_1x1; - tids.emplace_back(tid); - - std::cout << n_x2x << " " << n_1x1 << " " << n_2x1 << "(" << tid << ")" << std::endl; - } - -// std::cout << count << std::endl; - -// for (int i = 0; i < tids.size(); ++i) { -// -// printf("% 5d,", tids[i]); -// -// if ((i & 0b111) == 0b111) { -// printf("\n"); -// } - -// std::cout << tid << std::endl; -// } - - -// generate(generate_t { // generate target ranges -// .n1 = 16 - n * 2 - n_1x1, /// space -> 00 -// .n2 = n - n_2x1, /// 1x2 -> 01 -// .n3 = n_2x1, /// 2x1 -> 10 -// .n4 = n_1x1, /// 1x1 -> 11 -// }); - - return Group::block_num_t(); + auto flag = TYPE_ID_INDEX[type_id]; + uint8_t n_x2x = flag >> 8; + uint8_t n_2x1 = (flag >> 4) & 0b1111; + +// std::cout << (int)n_x2x << std::endl; +// std::cout << (int)n_2x1 << std::endl; +// std::cout << flag << std::endl; + // 1046 -> 0100 0001 0110 + // 1024+0016+0006 + // 4 1 6 + + return block_num_t { + .n_1x1 = static_cast(flag & 0b1111), + .n_1x2 = static_cast(n_x2x - n_2x1), + .n_2x1 = n_2x1, + }; } Group::block_num_t Group::block_num(const RawCode &raw_code) { @@ -124,7 +99,18 @@ std::vector Group::group_cases(const RawCode &seed) { return result; } +uint32_t Group::type_id(const block_num_t &block_num) { + auto n_x2x = block_num.n_1x2 + block_num.n_2x1; + auto flag = (n_x2x << 8) | (block_num.n_2x1 << 4) | block_num.n_1x1; + std::cout << flag << std::endl; + +// std::cout << << std::endl; + + // search in TYPE_ID_INDEX + + return std::lower_bound(TYPE_ID_INDEX, TYPE_ID_INDEX + 204, flag) - TYPE_ID_INDEX; +} } // namespace klotski diff --git a/src/klotski_core/group/group.h b/src/klotski_core/group/group.h index d5fcd29..2881e27 100644 --- a/src/klotski_core/group/group.h +++ b/src/klotski_core/group/group.h @@ -15,10 +15,15 @@ public: uint8_t n_2x1 = 0; }; +// static uint32_t type_id(const RawCode &raw_code); + static uint32_t type_id(const block_num_t &block_num); +// static uint32_t type_id(const CommonCode &common_code); + static block_num_t block_num(uint32_t type_id); static block_num_t block_num(const RawCode &raw_code); static block_num_t block_num(const CommonCode &common_code); + // static uint32_t max_group_size(uint32_t type_id); static uint32_t max_group_size(const RawCode &raw_code) { return 65535 * 8; @@ -28,4 +33,12 @@ public: }; +inline bool operator==(const Group::block_num_t &b1, const Group::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 Group::block_num_t &b1, const Group::block_num_t &b2) { + return (b1.n_1x1 != b2.n_1x1) || (b1.n_1x2 != b2.n_1x2) || (b1.n_2x1 != b2.n_2x1); +} + } diff --git a/src/klotski_core/group/type_id.h b/src/klotski_core/group/type_id.h index 2cdd7a8..a92b7d5 100644 --- a/src/klotski_core/group/type_id.h +++ b/src/klotski_core/group/type_id.h @@ -2,7 +2,7 @@ namespace klotski { -const uint16_t TYPE_ID[204] = { +const uint16_t TYPE_ID_INDEX[204] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 256, 257, 258, 259, 260, 261, 262, 263, 264,