Browse Source

refactor: using `mover` instead of `core`

legacy
Dnomd343 2 months ago
parent
commit
2b44799d1e
  1. 2
      src/core/CMakeLists.txt
  2. 6
      src/core/fast_cal/fast_cal.h
  3. 6
      src/core/fast_cal/internal/cal_core.cc
  4. 7
      src/core/group/internal/group.cc
  5. 2
      src/core/group/internal/group_union.cc
  6. 4
      src/core/main.cc
  7. 16
      src/core/mover/internal/mover.cc
  8. 10
      src/core/mover/mover.h
  9. 10
      src/core_test/CMakeLists.txt
  10. 2
      src/core_test/cases/helper/group_impl.cc
  11. 2
      src/core_test/helper/internal/group.cc
  12. 12
      src/core_test/mover/mover.cc

2
src/core/CMakeLists.txt

@ -18,7 +18,7 @@ set(KLOTSKI_CORE_SRC
short_code/internal/convert.cc
short_code/internal/serialize.cc
core/internal/core.cc
mover/internal/mover.cc
fast_cal/internal/cal_core.cc
fast_cal/internal/fast_cal.cc

6
src/core/fast_cal/fast_cal.h

@ -10,11 +10,11 @@
#include <functional>
#include <unordered_map>
#include "core/core.h"
#include "mover/mover.h"
#include "raw_code/raw_code.h"
using klotski::core::Core;
using klotski::codec::RawCode;
using klotski::mover::MaskMover;
// TODO: using prime number
const uint32_t FC_MAP_RESERVE = 65536 * 8;
@ -60,6 +60,6 @@ private:
std::queue<fast_cal_t*> cache;
std::unordered_map<uint64_t, fast_cal_t> cases;
inline Core init(uint64_t code);
inline MaskMover init(uint64_t code);
void new_case(uint64_t code, uint64_t mask);
};

6
src/core/fast_cal/internal/cal_core.cc

@ -1,6 +1,6 @@
#include "fast_cal/fast_cal.h"
Core FastCal::init(uint64_t code) { // initialize process
MaskMover FastCal::init(uint64_t code) { // initialize process
/// reset working data
cases.clear();
cases.reserve(FC_MAP_RESERVE); // hashmap pre-reserve
@ -13,8 +13,8 @@ Core FastCal::init(uint64_t code) { // initialize process
.last = nullptr, // without parent node
}).first->second);
/// import klotski core
return Core(
/// import klotski mover
return MaskMover(
[this](auto &&code, auto &&mask) { // lambda as function pointer
new_case(std::forward<decltype(code)>(code), std::forward<decltype(mask)>(mask));
}

7
src/core/group/internal/group.cc

@ -1,14 +1,15 @@
#include <absl/container/flat_hash_map.h>
#include "core/core.h"
#include "mover/mover.h"
#include "group/group.h"
using klotski::core::Core;
using klotski::cases::Group;
using klotski::codec::RawCode;
using klotski::codec::CommonCode;
using klotski::cases::RangesUnion;
using klotski::mover::MaskMover;
std::vector<RawCode> Group::extend(RawCode raw_code, uint32_t reserve) {
std::vector<RawCode> codes;
absl::flat_hash_map<uint64_t, uint64_t> cases; // <code, mask>
@ -16,7 +17,7 @@ std::vector<RawCode> Group::extend(RawCode raw_code, uint32_t reserve) {
codes.reserve(reserve);
cases.reserve(reserve);
auto core = Core([&codes, &cases](uint64_t code, uint64_t mask) {
auto core = MaskMover([&codes, &cases](uint64_t code, uint64_t mask) {
if (const auto match = cases.find(code); match != cases.end()) {
match->second |= mask; // update mask
return;

2
src/core/group/internal/group_union.cc

@ -1,4 +1,4 @@
#include "core/core.h"
#include "mover/mover.h"
#include "group/group.h"
#include "constant/group_union.h"

4
src/core/main.cc

@ -6,8 +6,8 @@
#include <unordered_set>
#include <ranges/ranges.h>
#include "core/core.h"
#include "group/group.h"
#include "mover/mover.h"
#include "raw_code/raw_code.h"
#include "fast_cal/fast_cal.h"
#include "all_cases/all_cases.h"
@ -16,7 +16,7 @@
#include "../../third_party/thread-pool/include/BS_thread_pool.hpp"
using klotski::core::Core;
using klotski::mover::MaskMover;
using klotski::cases::AllCases;
using klotski::cases::BasicRanges;

16
src/core/core/internal/core.cc → src/core/mover/internal/mover.cc

@ -1,4 +1,4 @@
#include "core/core.h"
#include "mover/mover.h"
#include "utils/common.h"
/// block move direction
@ -73,9 +73,9 @@
///////////////////////////////////////////////
using klotski::core::Core;
using klotski::mover::MaskMover;
inline void Core::cache_insert(cache_t next_case) { // try to insert into cache
inline void MaskMover::cache_insert(cache_t next_case) { // try to insert into cache
auto *cache_ptr = cache_;
for (; cache_ptr < cache_ + cache_size_; ++cache_ptr) {
if (cache_ptr->code == next_case.code) {
@ -86,7 +86,7 @@ inline void Core::cache_insert(cache_t next_case) { // try to insert into cache
++cache_size_;
}
void Core::move_1x1(uint64_t code, int addr) { // try to move target 1x1 block
void MaskMover::move_1x1(uint64_t code, int addr) { // try to move target 1x1 block
BFS_INIT
while (!BFS_STOP) { // bfs search process
BFS_LOAD
@ -105,7 +105,7 @@ void Core::move_1x1(uint64_t code, int addr) { // try to move target 1x1 block
}
}
void Core::move_1x2(uint64_t code, int addr) { // try to move target 1x2 block
void MaskMover::move_1x2(uint64_t code, int addr) { // try to move target 1x2 block
BFS_INIT
while (!BFS_STOP) { // bfs search process
BFS_LOAD
@ -124,7 +124,7 @@ void Core::move_1x2(uint64_t code, int addr) { // try to move target 1x2 block
}
}
void Core::move_2x1(uint64_t code, int addr) { // try to move target 2x1 block
void MaskMover::move_2x1(uint64_t code, int addr) { // try to move target 2x1 block
BFS_INIT
while (!BFS_STOP) { // bfs search process
BFS_LOAD
@ -143,7 +143,7 @@ void Core::move_2x1(uint64_t code, int addr) { // try to move target 2x1 block
}
}
void Core::move_2x2(uint64_t code, int addr) { // try to move target 2x2 block
void MaskMover::move_2x2(uint64_t code, int addr) { // try to move target 2x2 block
BFS_INIT
while (!BFS_STOP) { // bfs search process
BFS_LOAD
@ -162,7 +162,7 @@ void Core::move_2x2(uint64_t code, int addr) { // try to move target 2x2 block
}
}
void Core::next_cases(uint64_t code, uint64_t mask) { // search next step cases
void MaskMover::next_cases(uint64_t code, uint64_t mask) { // search next step cases
cache_[0].filter = 0; // without filter
cache_[0].code = code; // bfs root code
auto range = code | mask;

10
src/core/core/core.h → src/core/mover/mover.h

@ -52,7 +52,7 @@
/// be obtained at the same time, which marks the moved block as `111`, which
/// will speed up subsequent calculations. The generated layout will be
/// inserted into the cache, and after the BFS search for each block is
/// completed, the core will use the callback function to output these
/// completed, the mover will use the callback function to output these
/// results.
#pragma once
@ -61,20 +61,20 @@
#include <utility>
#include <functional>
namespace klotski::core {
namespace klotski::mover {
// TODO: new version without mask
// TODO: allow wrap as a function directly
class Core {
class MaskMover {
public:
/// Release with code and mask
typedef std::function<void(uint64_t, uint64_t)> release_t;
/// Core interface
void next_cases(uint64_t code, uint64_t mask);
explicit Core(release_t release_func) : release_(std::move(release_func)) {}
explicit MaskMover(release_t release_func) : release_(std::move(release_func)) {}
private:
struct cache_t {
@ -95,4 +95,4 @@ private:
inline void cache_insert(cache_t next_case);
};
} // namespace klotski::core
} // namespace klotski::mover

10
src/core_test/CMakeLists.txt

@ -62,12 +62,12 @@ add_test(NAME klotski_codec COMMAND test_klotski_codec)
# ------------------------------------------------------------------------------------ #
set(KLSK_TEST_CORE_SRC
core/core.cc
set(KLSK_TEST_MOVER_SRC
mover/mover.cc
)
add_executable(test_klotski_core ${KLSK_TEST_CORE_SRC})
target_link_libraries(test_klotski_core PRIVATE ${KLSK_TEST_DEPS})
add_test(NAME klotski_core COMMAND test_klotski_core)
add_executable(test_klotski_mover ${KLSK_TEST_MOVER_SRC})
target_link_libraries(test_klotski_mover PRIVATE ${KLSK_TEST_DEPS} test_helper)
add_test(NAME klotski_mover COMMAND test_klotski_mover)
# ------------------------------------------------------------------------------------ #

2
src/core_test/cases/helper/group_impl.cc

@ -6,7 +6,7 @@
/// Extend ordered Group from the specified CommonCode seed.
static std::vector<CommonCode> extend_cases(CommonCode seed) {
// TODO: using inner build process -> only allow calling klotski::core
// TODO: using inner build process -> only allow calling klotski::mover
auto raw_codes = klotski::cases::Group::extend(seed.to_raw_code());
std::vector<CommonCode> common_codes {raw_codes.begin(), raw_codes.end()};
std::ranges::sort(common_codes.begin(), common_codes.end());

2
src/core_test/helper/internal/group.cc

@ -52,7 +52,7 @@ const std::vector<CommonCode>& helper::group_union_cases(const uint32_t type_id)
/// Extend ordered Group from the specified CommonCode seed.
static std::vector<CommonCode> extend_cases(CommonCode seed) {
// TODO: using inner build process -> only allow calling klotski::core
// TODO: using inner build process -> only allow calling klotski::mover
auto raw_codes = klotski::cases::Group::extend(seed.to_raw_code());
std::vector<CommonCode> common_codes {raw_codes.begin(), raw_codes.end()};
std::ranges::sort(common_codes.begin(), common_codes.end());

12
src/core_test/core/core.cc → src/core_test/mover/mover.cc

@ -5,7 +5,7 @@
#include <numeric>
#include <ranges>
#include "core/core.h"
#include "mover/mover.h"
#include "utils/common.h"
#include "all_cases/all_cases.h"
#include "common_code/common_code.h"
@ -19,7 +19,7 @@ constexpr auto NEXT_CASES_XXH3 = std::to_array<uint64_t>({
0x4a7599e1bdbffbb3, 0xb3cf1fdea988466a, 0x21226a4f692e1892, 0x2d06800538d394c2,
});
using klotski::core::Core;
using klotski::mover::MaskMover;
using klotski::cases::AllCases;
using klotski::codec::CommonCode;
@ -54,7 +54,7 @@ TEST(Core, core) {
std::vector<uint64_t> codes;
codes.reserve(402258220);
auto core = Core([&codes](uint64_t ret, uint64_t) {
auto core = MaskMover([&codes](uint64_t ret, uint64_t) {
codes.emplace_back(klotski::codec::RawCode::unsafe_create(ret).to_common_code().unwrap());
});
@ -82,7 +82,7 @@ TEST(Core, mask) {
uint64_t src;
auto core = Core([&src](uint64_t ret, uint64_t mask) {
auto core = MaskMover([&src](uint64_t ret, uint64_t mask) {
EXPECT_EQ(std::popcount(mask), 3);
@ -184,7 +184,7 @@ TEST(Core, next_cases) {
std::vector<CommonCode> result {};
std::vector<CommonCode> tmp;
auto core = Core([&tmp](uint64_t ret, uint64_t mask) {
auto core = MaskMover([&tmp](uint64_t ret, uint64_t mask) {
tmp.emplace_back(CommonCode::from_raw_code(ret).value());
});
@ -212,6 +212,6 @@ TEST(Core, next_cases) {
// std::cout << std::hex << hash::xxh3(result) << std::endl;
EXPECT_EQ(hash::xxh3(result), NEXT_CASES_XXH3[0]);
EXPECT_EQ(helper::xxh3(result), NEXT_CASES_XXH3[0]);
}
Loading…
Cancel
Save