mirror of https://github.com/dnomd343/klotski.git
Dnomd343
2 years ago
6 changed files with 86 additions and 48 deletions
@ -1,8 +1,29 @@ |
|||
#include "klotski.h" |
|||
#include "gtest/gtest.h" |
|||
#include "common_code.h" |
|||
#include "global_utils.h" |
|||
|
|||
using klotski::CommonCode; |
|||
|
|||
std::vector<uint64_t> common_code_search(uint64_t start, uint64_t end) { |
|||
std::vector<uint64_t> ret; |
|||
for (uint64_t common_code = start; common_code < end; ++common_code) { |
|||
if (CommonCode::check(common_code)) { |
|||
ret.emplace_back(common_code); // valid common code
|
|||
} |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
|
|||
TEST(GLOBAL, common_code) { |
|||
|
|||
// std::cout << "demo ok" << std::endl;
|
|||
// auto ranges = range_split(0, 100, 30);
|
|||
// auto ranges = range_split(0, 91, 30);
|
|||
// auto ranges = range_split(0, 89, 30);
|
|||
auto ranges = range_split(0, 90, 30); |
|||
|
|||
for (const auto &r : ranges) { |
|||
printf("[%lu, %lu)\n", r.first, r.second); |
|||
} |
|||
|
|||
} |
|||
|
@ -0,0 +1,29 @@ |
|||
#include "global_utils.h" |
|||
|
|||
template <typename Func, typename ...Args> |
|||
auto TinyPool::submit(Func &&func, Args &&...args) -> std::future<decltype(func(args...))> { |
|||
std::function<decltype(func(args...))()> wrap_func = std::bind( |
|||
std::forward<Func>(func), std::forward<Args>(args)... |
|||
); |
|||
auto func_ptr = std::make_shared< |
|||
std::packaged_task<decltype(func(args...))()> |
|||
>(wrap_func); |
|||
tiny_pool_submit(pool, TinyPool::wrap_c_func, (void*)( |
|||
new std::function<void()> ( |
|||
[func_ptr]() { (*func_ptr)(); } |
|||
) |
|||
)); |
|||
return func_ptr->get_future(); |
|||
} |
|||
|
|||
range_split_t range_split(uint64_t start, uint64_t end, uint64_t size) { |
|||
uint64_t rear = end - (end - start) % size; // (rear - start) % size == 0
|
|||
range_split_t ranges; |
|||
for (uint64_t i = start; i < rear; i += size) { |
|||
ranges.emplace_back(i, i + size); |
|||
} |
|||
if (rear != end) { |
|||
ranges.emplace_back(rear, end); |
|||
} |
|||
return ranges; |
|||
} |
@ -0,0 +1,27 @@ |
|||
#include <future> |
|||
#include <vector> |
|||
#include <functional> |
|||
#include "tiny_pool.h" |
|||
|
|||
class TinyPool { // OOP for tiny_thread_pool
|
|||
pool_t *pool; |
|||
static void wrap_c_func(void *func) { // wrap std::function as c-style function ptr
|
|||
auto func_ptr = static_cast<std::function<void()>*>(func); |
|||
(*func_ptr)(); |
|||
delete func_ptr; |
|||
} |
|||
|
|||
public: |
|||
void boot() { tiny_pool_boot(pool); } |
|||
void join() { tiny_pool_join(pool); } |
|||
void kill() { tiny_pool_kill(pool); } |
|||
void detach() { tiny_pool_detach(pool); } |
|||
explicit TinyPool(uint32_t size) { pool = tiny_pool_create(size); } |
|||
|
|||
template <typename Func, typename ...Args> |
|||
auto submit(Func &&func, Args &&...args) -> std::future<decltype(func(args...))>; |
|||
}; |
|||
|
|||
typedef std::vector<std::pair<uint64_t, uint64_t>> range_split_t; |
|||
|
|||
range_split_t range_split(uint64_t start, uint64_t end, uint64_t size); |
@ -1,37 +0,0 @@ |
|||
#include <future> |
|||
#include <functional> |
|||
#include "tiny_pool.h" |
|||
|
|||
class TinyPool { // OOP for tiny_thread_pool
|
|||
pool_t *pool; |
|||
static void wrap_c_func(void *func) { // wrap std::function as c-style function ptr
|
|||
(*static_cast<std::function<void()>*>(func))(); |
|||
free(func); |
|||
} |
|||
|
|||
public: |
|||
void boot() { tiny_pool_boot(pool); } |
|||
void join() { tiny_pool_join(pool); } |
|||
void kill() { tiny_pool_kill(pool); } |
|||
void detach() { tiny_pool_detach(pool); } |
|||
explicit TinyPool(uint32_t size) { pool = tiny_pool_create(size); } |
|||
|
|||
template <typename Func, typename ...Args> |
|||
auto submit(Func &&func, Args &&...args) -> std::future<decltype(func(args...))>; |
|||
}; |
|||
|
|||
template <typename Func, typename ...Args> |
|||
auto TinyPool::submit(Func &&func, Args &&...args) -> std::future<decltype(func(args...))> { |
|||
std::function<decltype(func(args...))()> wrap_func = std::bind( |
|||
std::forward<Func>(func), std::forward<Args>(args)... |
|||
); |
|||
auto func_ptr = std::make_shared< |
|||
std::packaged_task<decltype(func(args...))()> |
|||
>(wrap_func); |
|||
tiny_pool_submit(pool, TinyPool::wrap_c_func, (void*)( |
|||
new std::function<void()> ( |
|||
[func_ptr]() { (*func_ptr)(); } |
|||
) |
|||
)); |
|||
return func_ptr->get_future(); |
|||
} |
@ -1 +1 @@ |
|||
Subproject commit 77351b1aace75c17b5163ea1d2940f665d7ed400 |
|||
Subproject commit 216c55476c45f9c2b8b6567ea2bf1aa643d95c0a |
Loading…
Reference in new issue