From a70f7669309e12bb7b506d34cad8b565f92f97d1 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 29 Sep 2023 16:19:24 +0800 Subject: [PATCH] feat: support all cases data export --- src/core/ffi/all_cases.cc | 23 ++++++++++++++----- src/core/ffi/klotski.h | 22 +++++++++++++----- src/main.c | 47 ++++++++++++++++++++++++++------------- 3 files changed, 65 insertions(+), 27 deletions(-) diff --git a/src/core/ffi/all_cases.cc b/src/core/ffi/all_cases.cc index 384394e..db44a53 100644 --- a/src/core/ffi/all_cases.cc +++ b/src/core/ffi/all_cases.cc @@ -1,12 +1,11 @@ #include "klotski.h" #include "all_cases.h" -#include -#include - using klotski::cases::AllCases; using klotski::cases::BasicRanges; +using klotski::cases::ALL_CASES_NUM; + void all_cases_prebuild() { BasicRanges::Instance().Build(); } @@ -18,7 +17,7 @@ void all_cases_prebuild_async(executor_t executor, notifier_t callback) { }, (void*)callback); } -int is_all_cases_prebuild_available() { +int all_cases_prebuild_available() { if (BasicRanges::Instance().IsAvailable()) { return KLOTSKI_TRUE; } else { @@ -60,10 +59,24 @@ void all_cases_build_parallel_async(executor_t executor, notifier_t callback) { }, std::move(all_done)); } -int is_all_cases_available() { +int all_cases_available() { if (AllCases::Instance().IsAvailable()) { return KLOTSKI_TRUE; } else { return KLOTSKI_FALSE; } } + +int all_cases_num(int head) { + if (head < 0 || head > 15) { + return -1; + } + return ALL_CASES_NUM[head]; +} + +const klotski_u32* all_cases_export(int head) { + if (all_cases_num(head) < 0) { + return nullptr; + } + return AllCases::Instance().Fetch()[head].data(); +} diff --git a/src/core/ffi/klotski.h b/src/core/ffi/klotski.h index bd6ebf6..8488a76 100644 --- a/src/core/ffi/klotski.h +++ b/src/core/ffi/klotski.h @@ -15,6 +15,10 @@ typedef void (*notifier_t)(); typedef void (*executor_t)(void (*fn)(void*), void *arg); +typedef uint32_t klotski_u32; + +// -------------------------------- klotski all cases --------------------------------- + #ifdef __cplusplus extern "C" { #endif @@ -30,7 +34,7 @@ EXTERN void all_cases_prebuild_async(executor_t executor, notifier_t callback); /// Returns the pre-build status of all_cases without any block, value is 0 if /// not completed, non-0 otherwise. -EXTERN int is_all_cases_prebuild_available(); +EXTERN int all_cases_prebuild_available(); /// Perform the build of all_cases, it is blocking, and will return directly /// if completed. @@ -54,13 +58,19 @@ EXTERN void all_cases_build_parallel_async(executor_t executor, notifier_t callb /// Returns whether the all_cases is ready, 0 means not completed, non-0 means /// the data is ready. -EXTERN int is_all_cases_available(); +EXTERN int all_cases_available(); + +/// Returns the number of all_cases corresponding to head. Head is an integer +/// between 0 and 15. When head is invalid, a value less than 0 is returned. +/// Note that when head is `4n-1`, 0 will be returned. +EXTERN int all_cases_num(int head); -//extern const uint32_t ALL_CASES_SIZE; -//EXTERN_FUNC void export_all_cases(uint64_t *buffer); +/// Returns the starting pointer of all_cases data corresponding to head, the +/// number of data is obtained by all_cases_num. When head is invalid, a NULL +/// pointer will be returned. Note that when head is `4n-1`, since the number +/// is 0, the returned pointer is still a NULL pointer. +EXTERN const klotski_u32* all_cases_export(int head); -//extern const uint32_t BASIC_RANGES_SIZE; -//EXTERN_FUNC void export_basic_ranges(uint32_t *buffer); #ifdef __cplusplus } #endif diff --git a/src/main.c b/src/main.c index 7aa155a..89fdefb 100644 --- a/src/main.c +++ b/src/main.c @@ -10,7 +10,7 @@ typedef struct { } pthread_wrapper_t; void* pthread_wrapper(void *arg) { - printf("wrapper at %ld\n", pthread_self()); + printf("wrapper at %p\n", pthread_self()); pthread_wrapper_t *kk = (pthread_wrapper_t*)arg; kk->fn(kk->arg); return NULL; @@ -33,7 +33,7 @@ void executor(void(*fn)(void*), void *arg) { } int main() { -// printf("prebuild available -> %d\n", is_all_cases_prebuild_available()); +// printf("prebuild available -> %d\n", all_cases_prebuild_available()); // // printf("prebuild begin\n"); // all_cases_prebuild(); @@ -42,38 +42,53 @@ int main() { // printf("prebuild begin\n"); // all_cases_prebuild_async(executor, callback); // printf("prebuild func exited\n"); -// printf("prebuild available -> %d\n", is_all_cases_prebuild_available()); +// printf("prebuild available -> %d\n", all_cases_prebuild_available()); // printf("start sleep 3s\n"); // sleep(3); // -// printf("prebuild available -> %d\n", is_all_cases_prebuild_available()); +// printf("prebuild available -> %d\n", all_cases_prebuild_available()); - printf("build available -> %d\n", is_all_cases_available()); - +// printf("build available -> %d\n", all_cases_available()); +// // printf("build begin\n"); // all_cases_build(); // printf("build complete\n"); - +// // printf("build begin\n"); // all_cases_build_async(executor, callback); // printf("build func exited\n"); -// printf("build available -> %d\n", is_all_cases_available()); +// printf("build available -> %d\n", all_cases_available()); // printf("start sleep 3s\n"); // sleep(3); - +// // printf("build begin\n"); // all_cases_build_parallel(executor); // printf("build complete\n"); +// +// printf("build begin\n"); +// all_cases_build_parallel_async(executor, callback); +// printf("build func exited\n"); +// printf("build available -> %d\n", all_cases_available()); +// printf("start sleep 3s\n"); +// sleep(3); +// +// printf("build available -> %d\n", all_cases_available()); + - printf("build begin\n"); - all_cases_build_parallel_async(executor, callback); - printf("build func exited\n"); - printf("build available -> %d\n", is_all_cases_available()); - printf("start sleep 3s\n"); - sleep(3); +// int i = -2; +// for (; i < 18; ++i) { +// printf("all cases %d -> %d (%p)\n", i, all_cases_num(i), all_cases_export(i)); +// } - printf("build available -> %d\n", is_all_cases_available()); + int i = 0; + for (; i < 16; ++i) { + int j = 0; + const klotski_u32 *ranges = all_cases_export(i); + for (; j < all_cases_num(i); ++j) { + printf("%X%08X\n", i, ranges[j]); + } + } return 0; }