From a98401f67a0646ffc874edb5d9803a87eef32d2b Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 12 Jan 2023 13:50:32 +0800 Subject: [PATCH] feat: try template on class non-static function --- src/core/core.cc | 27 ++++++++++++++++++++------- src/core/core.h | 8 +++++++- src/fast_cal/fast_cal.cc | 15 ++++++++++----- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/core/core.cc b/src/core/core.cc index e154935..bad45a3 100644 --- a/src/core/core.cc +++ b/src/core/core.cc @@ -60,7 +60,9 @@ cache_insert(next_case); //////////////////////////////////////// -inline void Core::cache_insert(Core::cache_t &next_case) { // try to insert into cache +//inline void Core::cache_insert(Core::cache_t &next_case) { // try to insert into cache +template +inline void Core::cache_insert(Core::cache_t &next_case) { // try to insert into cache auto *p = cache; for (; p < cache + cache_size; ++p) { if (p->code == next_case.code) { @@ -71,7 +73,9 @@ inline void Core::cache_insert(Core::cache_t &next_case) { // try to insert into ++cache_size; } -void Core::move_1x1(uint64_t code, int addr) { // try to move target 1x1 block +//void Core::move_1x1(uint64_t code, int addr) { // try to move target 1x1 block +template +void Core::move_1x1(uint64_t code, int addr) { // try to move target 1x1 block BFS_INIT while (!BFS_STOP) { // bfs search process BFS_LOAD @@ -90,7 +94,9 @@ 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 Core::move_1x2(uint64_t code, int addr) { // try to move target 1x2 block +template +void Core::move_1x2(uint64_t code, int addr) { // try to move target 1x2 block BFS_INIT while (!BFS_STOP) { // bfs search process BFS_LOAD @@ -109,7 +115,9 @@ 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 Core::move_2x1(uint64_t code, int addr) { // try to move target 2x1 block +template +void Core::move_2x1(uint64_t code, int addr) { // try to move target 2x1 block BFS_INIT while (!BFS_STOP) { // bfs search process BFS_LOAD @@ -128,7 +136,9 @@ 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 Core::move_2x2(uint64_t code, int addr) { // try to move target 2x2 block +template +void Core::move_2x2(uint64_t code, int addr) { // try to move target 2x2 block BFS_INIT while (!BFS_STOP) { // bfs search process BFS_LOAD @@ -147,7 +157,9 @@ void Core::move_2x2(uint64_t code, int addr) { // try to move target 2x2 block } } -void Core::next_step(uint64_t code, uint64_t mask) { // search next step cases +//void Core::next_step(uint64_t code, uint64_t mask) { // search next step cases +template +void Core::next_step(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; @@ -173,8 +185,9 @@ void Core::next_step(uint64_t code, uint64_t mask) { // search next step cases // TODO: try to send multi-items data for (int i = 1; i < cache_size; ++i) { + (src_class->*release)(cache[i].code, cache[i].mask); // release(cache[i].code, cache[i].mask); // release next cases - release_next(cache[i].code, cache[i].mask); // release next cases +// release_next(cache[i].code, cache[i].mask); // release next cases } cache_size = 1; // reset cache size diff --git a/src/core/core.h b/src/core/core.h index 11a0d41..d63319a 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -8,10 +8,16 @@ #define DOWN (+4 * 3) #define RIGHT (+1 * 3) +class FastCal; + +template class Core { public: -// typedef void (*release_t)(uint64_t, uint64_t); + typedef void (T::*release_t)(uint64_t, uint64_t); + + T *src_class; + release_t release; std::function release_next; diff --git a/src/fast_cal/fast_cal.cc b/src/fast_cal/fast_cal.cc index acd7e5c..1811e3c 100644 --- a/src/fast_cal/fast_cal.cc +++ b/src/fast_cal/fast_cal.cc @@ -10,8 +10,13 @@ void FastCal::fast_cal(uint64_t code) { std::cout << RawCode(code).dump_case() << std::endl; - auto core = Core(); - core.release_next = std::bind(&FastCal::add_new_case, this, std::placeholders::_1, std::placeholders::_2); +// auto core = Core(); +// core.release_next = std::bind(&FastCal::add_new_case, this, std::placeholders::_1, std::placeholders::_2); + + auto core = Core(); + + core.src_class = this; + core.release = &FastCal::add_new_case; cases.empty(); cache.empty(); @@ -56,9 +61,9 @@ void FastCal::add_new_case(uint64_t code, uint64_t mask) { } cases[code] = fast_cal_t { - .code = code, - .mask = mask, - .last = cache.front(), + .code = code, + .mask = mask, + .last = cache.front(), };; cache.emplace(&cases[code]);