Browse Source

feat: parse and obtain group info

legacy
Dnomd343 3 months ago
parent
commit
43ff39afde
  1. 40
      src/core/group/group.h
  2. 97
      src/core/group/internal/group_cases.cc
  3. 24
      src/core/group/internal/group_cases.inl
  4. 10
      src/core/main.cc

40
src/core/group/group.h

@ -201,13 +201,14 @@ public:
class GroupCases { class GroupCases {
public: public:
// TODO: rename as Info and changed as class // TODO: rename as Info and changed as class
struct info_t { class Info {
public:
uint16_t type_id; uint16_t type_id;
uint16_t group_id; uint16_t group_id;
uint32_t case_id; uint32_t case_id;
#ifndef KLSK_NDEBUG #ifndef KLSK_NDEBUG
friend std::ostream& operator<<(std::ostream &out, info_t self) { friend std::ostream& operator<<(std::ostream &out, Info self) {
out << std::format("{}-{}-{}", self.type_id, self.group_id, self.case_id); out << std::format("{}-{}-{}", self.type_id, self.group_id, self.case_id);
return out; return out;
} }
@ -219,24 +220,24 @@ public:
// ------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------- //
/// Execute the build process. /// Execute the build process.
void build(); static void build();
/// Execute the build process without blocking. /// Execute the build process without blocking.
void build_async(Executor &&executor, Notifier &&callback); static void build_async(Executor &&executor, Notifier &&callback);
// ------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------- //
/// Parse CommonCode from group info. /// Parse CommonCode from group info.
codec::CommonCode parse(info_t info); // codec::CommonCode parse(Info info);
/// Get group info from RawCode. /// Get group info from RawCode.
info_t group_info(codec::RawCode raw_code); // Info get_info(codec::RawCode raw_code);
/// Get group info from short code. /// Get group info from short code.
info_t group_info(codec::ShortCode short_code); // Info get_info(codec::ShortCode short_code);
/// Get group info from common code. /// Get group info from common code.
info_t group_info(codec::CommonCode common_code); // Info get_info(codec::CommonCode common_code);
// ------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------- //
@ -247,13 +248,26 @@ private:
KLSK_INSTANCE(GroupCases) KLSK_INSTANCE(GroupCases)
public: public:
// fast api // ------------------------------------------------------------------------------------- //
static info_t to_info_t(codec::ShortCode short_code);
static codec::CommonCode from_info_t(info_t info); /// Parse group info into CommonCode.
static codec::CommonCode tiny_parse(Info info);
/// Obtain group info from CommonCode.
static Info tiny_obtain(codec::CommonCode common_code);
// ------------------------------------------------------------------------------------- //
/// Quickly parse group info into CommonCode.
static codec::CommonCode fast_parse(Info info);
// TODO: add to_info_t(CommonCode) interface /// Quickly obtain group info from ShortCode.
static Info fast_obtain(codec::ShortCode short_code);
// TODO: tiny api /// Quickly obtain group info from CommonCode.
static Info fast_obtain(codec::CommonCode common_code);
// ------------------------------------------------------------------------------------- //
}; };
} // namespace klotski::cases } // namespace klotski::cases

97
src/core/group/internal/group_cases.cc

@ -3,20 +3,15 @@
#include "group/group.h" #include "group/group.h"
using klotski::cases::GroupCases;
using klotski::codec::ShortCode; using klotski::codec::ShortCode;
using klotski::codec::CommonCode; using klotski::codec::CommonCode;
using klotski::cases::RangesUnion; using klotski::cases::GroupCases;
using klotski::cases::GroupUnion; using klotski::cases::GroupUnion;
using klotski::cases::RangesUnion;
using klotski::cases::ALL_GROUP_NUM; using klotski::cases::ALL_GROUP_NUM;
using klotski::cases::TYPE_ID_LIMIT; using klotski::cases::TYPE_ID_LIMIT;
using klotski::cases::ALL_CASES_NUM_; using klotski::cases::ALL_CASES_NUM_;
struct tmp_t { struct tmp_t {
@ -28,8 +23,10 @@ static_assert(sizeof(tmp_t) == 4);
// TODO: we need multi-thread support (Executor) // TODO: we need multi-thread support (Executor)
std::vector<RangesUnion> build_ranges_unions() { static std::vector<tmp_t> *rev_data = nullptr;
static std::vector<RangesUnion> *ru_data = nullptr;
std::vector<RangesUnion> build_ranges_unions() {
std::vector<RangesUnion> unions; std::vector<RangesUnion> unions;
unions.reserve(ALL_GROUP_NUM); unions.reserve(ALL_GROUP_NUM);
@ -41,37 +38,10 @@ std::vector<RangesUnion> build_ranges_unions() {
unions.emplace_back(group.cases()); unions.emplace_back(group.cases());
} }
} }
return unions; return unions;
} }
CommonCode GroupCases::from_info_t(info_t info) {
// TODO: build as static class member (ptr)
static std::vector<RangesUnion> data = build_ranges_unions();
auto flat_id = GROUP_OFFSET[info.type_id] + info.group_id;
auto &cases = data[flat_id];
// TODO: make offset table for perf
uint64_t head = 0;
for (;;) {
if (info.case_id >= cases[head].size()) {
info.case_id -= cases[head].size();
++head;
} else {
break;
}
}
auto range = cases[head][info.case_id];
return CommonCode::unsafe_create(head << 32 | range);
}
static std::vector<tmp_t> build_tmp_data() { static std::vector<tmp_t> build_tmp_data() {
std::vector<tmp_t> data; std::vector<tmp_t> data;
data.resize(ALL_CASES_NUM_); data.resize(ALL_CASES_NUM_);
ShortCode::speed_up(true); ShortCode::speed_up(true);
@ -89,27 +59,68 @@ static std::vector<tmp_t> build_tmp_data() {
.group_id = group_id, .group_id = group_id,
.case_id = case_id, .case_id = case_id,
}; };
} }
} }
} }
return data; return data;
}
void GroupCases::build() {
static auto data_1 = build_ranges_unions();
static auto data_2 = build_tmp_data();
ru_data = &data_1;
rev_data = &data_2;
} }
GroupCases::info_t GroupCases::to_info_t(codec::ShortCode short_code) { CommonCode GroupCases::fast_parse(Info info) {
auto flat_id = GROUP_OFFSET[info.type_id] + info.group_id;
auto &cases = (*ru_data)[flat_id];
// TODO: make offset table for perf
uint64_t head = 0;
for (;;) {
if (info.case_id >= cases[head].size()) {
info.case_id -= cases[head].size();
++head;
} else {
break;
}
}
auto range = cases[head][info.case_id];
return CommonCode::unsafe_create(head << 32 | range);
}
// TODO: build as static class member (ptr) GroupCases::Info GroupCases::fast_obtain(codec::ShortCode short_code) {
static auto data = build_tmp_data();
uint16_t type_id = GroupUnion::from_short_code(short_code).unwrap(); // NOTE: need to convert as CommonCode uint16_t type_id = GroupUnion::from_short_code(short_code).unwrap(); // NOTE: need to convert as CommonCode
uint16_t group_id = data[short_code.unwrap()].group_id; uint16_t group_id = (*rev_data)[short_code.unwrap()].group_id;
auto case_id = data[short_code.unwrap()].case_id; auto case_id = (*rev_data)[short_code.unwrap()].case_id;
return info_t { return Info {
.type_id = type_id, .type_id = type_id,
.group_id = group_id, .group_id = group_id,
.case_id = case_id, .case_id = case_id,
}; };
} }
GroupCases::Info GroupCases::fast_obtain(codec::CommonCode common_code) {
return fast_obtain(common_code.to_short_code());
}
void GroupCases::build_async(klotski::Executor &&executor, klotski::Notifier &&callback) {
}
klotski::codec::CommonCode GroupCases::tiny_parse(klotski::cases::GroupCases::Info info) {
// TODO: tiny parse process
return CommonCode::unsafe_create(0);
}
GroupCases::Info GroupCases::tiny_obtain(codec::CommonCode common_code) {
// TODO: tiny obtain process
return Info {};
}

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

@ -2,28 +2,4 @@
namespace klotski::cases { namespace klotski::cases {
inline void GroupCases::build() {
}
inline void GroupCases::build_async(Executor &&executor, Notifier &&callback) {
}
inline codec::CommonCode GroupCases::parse(GroupCases::info_t info) {
return codec::CommonCode::unsafe_create(0);
}
inline GroupCases::info_t GroupCases::group_info(codec::RawCode raw_code) {
return info_t {};
}
inline GroupCases::info_t GroupCases::group_info(codec::ShortCode short_code) {
return info_t {};
}
inline GroupCases::info_t GroupCases::group_info(codec::CommonCode common_code) {
return info_t {};
}
} // namespace klotski::cases } // namespace klotski::cases

10
src/core/main.cc

@ -43,11 +43,13 @@ int main() {
// const auto common_code = CommonCode::unsafe_create(0x1A9BF0C00); // const auto common_code = CommonCode::unsafe_create(0x1A9BF0C00);
// const auto group = Group::from_common_code(common_code); // const auto group = Group::from_common_code(common_code);
std::cout << GroupCases::from_info_t({169, 1, 7472}) << std::endl; GroupCases::build();
std::cout << GroupCases::from_info_t({164, 0, 30833}) << std::endl;
std::cout << GroupCases::to_info_t(CommonCode::unsafe_create(0x1A9BF0C00).to_short_code()) << std::endl; std::cout << GroupCases::fast_parse({169, 1, 7472}) << std::endl;
std::cout << GroupCases::to_info_t(CommonCode::unsafe_create(0x4FEA13400).to_short_code()) << std::endl; std::cout << GroupCases::fast_parse({164, 0, 30833}) << std::endl;
std::cout << GroupCases::fast_obtain(CommonCode::unsafe_create(0x1A9BF0C00)) << std::endl;
std::cout << GroupCases::fast_obtain(CommonCode::unsafe_create(0x4FEA13400)) << std::endl;
std::cerr << std::chrono::system_clock::now() - start << std::endl; std::cerr << std::chrono::system_clock::now() - start << std::endl;

Loading…
Cancel
Save