Browse Source

feat: conversion of GroupCases

master
Dnomd343 2 weeks ago
parent
commit
1c1c068117
  1. 1
      src/core/CMakeLists.txt
  2. 34
      src/core/group/group.h
  3. 3
      src/core/group/internal/group.cc
  4. 89
      src/core/group/internal/group_cases.cc
  5. 65
      src/core/main.cc
  6. 4
      src/core/ranges/ranges.h

1
src/core/CMakeLists.txt

@ -24,6 +24,7 @@ set(KLOTSKI_CORE_SRC
group/internal/group_union.cc
group/internal/group.cc
group/internal/group_cases.cc
ranges/internal/spawn.cc
ranges/internal/ranges.cc

34
src/core/group/group.h

@ -173,10 +173,13 @@ public:
// ------------------------------------------------------------------------------------- //
/// Create Group from RawCode.
static Group from_raw_code(codec::RawCode raw_code);
/// Create Group from ShortCode.
static Group from_short_code(codec::ShortCode short_code);
/// Create Group from CommonCode.
static Group from_common_code(codec::CommonCode common_code);
// ------------------------------------------------------------------------------------- //
@ -191,32 +194,39 @@ public:
static std::vector<codec::RawCode> extend(codec::RawCode raw_code, uint32_t reserve = 0);
};
class GroupCase {
class GroupCases {
public:
struct info_t {
uint16_t type_id;
uint16_t group_id;
uint32_t group_index;
uint32_t case_id;
// TODO: should we keep it valid? (convert without check)
};
// TODO: mark as instance.
void build();
/// Build group cases accelerated index.
static void speed_up();
void build_async(Executor &&executor, Notifier &&notifier);
/// Get the CommonCode using the group info.
static codec::CommonCode parse(const info_t &info);
// static codec::CommonCode parse(const info_t &info);
/// Get group info according to specified case.
static info_t encode(const codec::RawCode &raw_code);
static info_t encode(const codec::CommonCode &common_code);
// static info_t encode(const codec::RawCode &raw_code);
// static info_t encode(const codec::CommonCode &common_code);
static info_t to_info_t(codec::ShortCode short_code);
static codec::CommonCode from_info_t(info_t info);
private:
static bool available_;
static std::mutex building_;
bool available_ = false;
std::mutex building_ {};
// static codec::CommonCode fast_decode(const info_t &info);
// static info_t fast_encode(const codec::CommonCode &common_code);
static codec::CommonCode fast_decode(const info_t &info);
static info_t fast_encode(const codec::CommonCode &common_code);
KLSK_INSTANCE(GroupCases)
};
} // namespace klotski::cases

3
src/core/group/internal/group.cc

@ -36,6 +36,9 @@ std::vector<RawCode> Group::extend(RawCode raw_code, uint32_t reserve) {
}
RangesUnion Group::cases() const {
// TODO: add white list for single-group unions
auto seed = CommonCode::unsafe_create(GROUP_SEED[flat_id()]);
// std::cout << seed << std::endl;

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

@ -0,0 +1,89 @@
#include <iostream>
#include "group/group.h"
using klotski::cases::GroupCases;
using klotski::codec::CommonCode;
using klotski::cases::RangesUnion;
struct tmp_t {
uint32_t group_id : 12;
uint32_t case_id : 20;
};
static_assert(sizeof(tmp_t) == 4);
// TODO: we need multi-thread support (Executor)
std::vector<RangesUnion> build_ranges_unions() {
std::vector<RangesUnion> unions;
unions.reserve(25422);
// auto group_union = klotski::cases::GroupUnion::unsafe_create(169);
// TODO: add white list for single-group unions
for (uint32_t type_id = 0; type_id < klotski::cases::TYPE_ID_LIMIT; ++type_id) {
auto group_union = klotski::cases::GroupUnion::unsafe_create(type_id);
for (auto group : group_union.groups()) {
unions.emplace_back(group.cases());
}
}
std::cout << unions.size() << std::endl;
return unions;
}
CommonCode GroupCases::from_info_t(info_t info) {
std::vector<RangesUnion> data = build_ranges_unions();
// TODO: build as static data
// TODO: loop [0, 25422) -> get RangesUnion of all flat_ids
auto flat_id = GROUP_OFFSET[info.type_id] + info.group_id;
std::cout << "flat_id = " << flat_id << std::endl;
// auto codes = data[flat_id].codes();
// std::stable_sort(codes.begin(), codes.end());
// std::cout << codes[info.case_id] << std::endl;
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;
}
}
std::cout << "head = " << head << std::endl;
std::cout << info.case_id << std::endl;
// TODO: need sort in `Group::cases`
std::stable_sort(cases[head].begin(), cases[head].end());
std::cout << CommonCode::unsafe_create(head << 32 | cases[1][1909]) << std::endl;
return CommonCode::unsafe_create(0);
}
GroupCases::info_t GroupCases::to_info_t(codec::ShortCode short_code) {
std::vector<tmp_t> data;
// TODO: build as static data
// TODO: loop from `std::vector<RangesUnion>`
return {};
}

65
src/core/main.cc

@ -26,6 +26,10 @@ using klotski::codec::ShortCode;
using klotski::codec::CommonCode;
using klotski::cases::GroupUnion;
using klotski::cases::Group;
using klotski::cases::GroupCases;
using klotski::cases::GroupUnion;
using klotski::codec::SHORT_CODE_LIMIT;
int main() {
@ -36,69 +40,12 @@ int main() {
const auto start = std::chrono::system_clock::now();
// auto kk = AllCases::instance().fetch()[1];
// for (auto &x : kk) {
// x >>= 20;
// }
// std::cout << kk.size() << std::endl;
// for (int i = 0; i < 4096; ++i) {
// auto pp = std::ranges::lower_bound(kk.begin(), kk.end(), i);
// std::cout << pp - kk.begin() << std::endl;
// }
// std::unordered_set<uint32_t> tmp;
// for (auto x : kk) {
// tmp.emplace(x);
// }
// std::cout << tmp.size() << std::endl;
// ShortCode::speed_up(true);
// ShortCode::speed_up(false);
// auto show_with_index = [](uint32_t index) {
// uint64_t head = 1;
// uint32_t range = BasicRanges::instance().fetch()[index];
// std::cout << CommonCode::unsafe_create(head << 32 | range) << std::endl;
// };
//
// for (int offset = -29; offset < 42; ++offset) {
// std::cout << "offset = " << offset << " | ";
// show_with_index(4273474 + offset);
// }
GroupCases::from_info_t({169, 1, 7472});
auto short_code = ShortCode::unsafe_create(4091296);
std::cout << short_code.to_common_code() << std::endl;
auto common_code = CommonCode::unsafe_create(0x1A9BF0C00);
std::cout << common_code.to_short_code().unwrap() << std::endl;
// auto kk = BasicRanges::instance().fetch();
// for (auto &x : kk) {
// x >>= 20;
// }
// std::cout << kk.size() << std::endl;
// for (int i = 0; i < 4096; ++i) {
// auto pp = std::ranges::lower_bound(kk.begin(), kk.end(), i);
// std::cout << pp - kk.begin() << std::endl;
// }
// GroupCases::to_info_t(CommonCode::unsafe_create(0x1A9BF0C00).to_short_code());
std::cerr << std::chrono::system_clock::now() - start << std::endl;
// auto raw_code = RawCode::from_common_code(0x1A9BF0C00)->unwrap();
// auto ret = klotski::cases::group_extend_from_seed(raw_code);
//
// std::cout << ret.size() << std::endl;
// 1 A9BF0C00 -> 10 10 10 01 10 11 11 11 00 00 11 000000
// auto ret = klotski::cases::spawn_ranges(2, 1, 4, 4);
// std::cout << ret.size() << std::endl;
// auto kk = GroupUnion::create(123).value();
// std::cout << kk.size() << std::endl;
// std::cout << kk.group_num() << std::endl;
// std::cout << kk.max_group_size() << std::endl;
// auto core = Core([](const uint64_t code, uint64_t) {
// std::cout << RawCode::unsafe_create(code);
// std::cout << std::endl;

4
src/core/ranges/ranges.h

@ -1,3 +1,7 @@
/// Klotski Engine by Dnomd343 @2024
// TODO: A stable Ranges or RangesUnion must be ordered and non-repeating.
#pragma once
#include <array>

Loading…
Cancel
Save