From 84de126c134a05bcd1f04364055889fdf45f8468 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 28 Dec 2024 09:54:36 +0800 Subject: [PATCH] update: using `Layout` instead of `CommonCode` --- src/core_ffi/rust_ffi/CMakeLists.txt | 2 +- src/core_ffi/rust_ffi/Cargo.lock | 2 +- src/core_ffi/rust_ffi/Cargo.toml | 2 +- .../adapter/{common_code.cc => layout.cc} | 28 +++++----- src/core_ffi/rust_ffi/adapter/short_code.cc | 4 +- src/core_ffi/rust_ffi/build.rs | 4 +- src/core_ffi/rust_ffi/examples/demo.rs | 16 +++--- src/core_ffi/rust_ffi/include/interface.h | 4 +- .../include/{common_code.h => layout.h} | 0 src/core_ffi/rust_ffi/src/bridge.rs | 54 +++++++++---------- src/core_ffi/rust_ffi/src/lib.rs | 2 +- 11 files changed, 59 insertions(+), 59 deletions(-) rename src/core_ffi/rust_ffi/adapter/{common_code.cc => layout.cc} (62%) rename src/core_ffi/rust_ffi/include/{common_code.h => layout.h} (100%) diff --git a/src/core_ffi/rust_ffi/CMakeLists.txt b/src/core_ffi/rust_ffi/CMakeLists.txt index cf98055..01c0a94 100644 --- a/src/core_ffi/rust_ffi/CMakeLists.txt +++ b/src/core_ffi/rust_ffi/CMakeLists.txt @@ -5,8 +5,8 @@ set(CMAKE_CXX_STANDARD 23) if (KLSK_DEV_MODE) set(KLSK_RUST_FFI_SRC + adapter/layout.cc adapter/short_code.cc - adapter/common_code.cc target/cxxbridge/rust_ffi/src/bridge.rs.cc ) add_library(klotski_rs STATIC ${KLSK_RUST_FFI_SRC}) # just for IDE highlight diff --git a/src/core_ffi/rust_ffi/Cargo.lock b/src/core_ffi/rust_ffi/Cargo.lock index 50e45cf..9434056 100644 --- a/src/core_ffi/rust_ffi/Cargo.lock +++ b/src/core_ffi/rust_ffi/Cargo.lock @@ -76,7 +76,7 @@ dependencies = [ [[package]] name = "klotski" -version = "0.0.1" +version = "0.0.2" dependencies = [ "cmake", "cxx", diff --git a/src/core_ffi/rust_ffi/Cargo.toml b/src/core_ffi/rust_ffi/Cargo.toml index c7e7b9d..bebf5d8 100644 --- a/src/core_ffi/rust_ffi/Cargo.toml +++ b/src/core_ffi/rust_ffi/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "klotski" -version = "0.0.1" +version = "0.0.2" edition = "2021" authors = ["Dnomd343 "] description = "Klotski multifunctional engine with high performance" diff --git a/src/core_ffi/rust_ffi/adapter/common_code.cc b/src/core_ffi/rust_ffi/adapter/layout.cc similarity index 62% rename from src/core_ffi/rust_ffi/adapter/common_code.cc rename to src/core_ffi/rust_ffi/adapter/layout.cc index 973689a..8364aed 100644 --- a/src/core_ffi/rust_ffi/adapter/common_code.cc +++ b/src/core_ffi/rust_ffi/adapter/layout.cc @@ -1,4 +1,4 @@ -#include "rust_ffi/include/common_code.h" +#include "rust_ffi/include/layout.h" #include #include @@ -8,11 +8,11 @@ using klotski::codec::CommonCode; using klotski::mover::MaskMover; +using klotski::ffi::RsLayout; using klotski::ffi::RsShortCode; -using klotski::ffi::RsCommonCode; // TODO: it seems that cxx.rs not support `std::optional` -uint64_t klotski::ffi::common_code_from_str(const rust::Str s) { +uint64_t klotski::ffi::layout_from_str(const rust::Str s) { const std::string_view sv {s.data(), s.length()}; if (const auto ret = CommonCode::from_string(sv); ret.has_value()) { return ret.value().unwrap(); @@ -20,48 +20,48 @@ uint64_t klotski::ffi::common_code_from_str(const rust::Str s) { return 0x10FFFFFFFF; // return invalid value for now } -bool klotski::ffi::common_code_check(const uint64_t val) { +bool klotski::ffi::layout_check(const uint64_t val) { return CommonCode::check(val); } -rust::String RsCommonCode::to_string() const noexcept { +rust::String RsLayout::to_string() const noexcept { return CommonCode::unsafe_create(code).to_string(); } -rust::String RsCommonCode::to_shorten_string() const noexcept { +rust::String RsLayout::to_shorten_string() const noexcept { return CommonCode::unsafe_create(code).to_string(true); } -RsShortCode RsCommonCode::to_short_code() const noexcept { +RsShortCode RsLayout::to_short_code() const noexcept { return {CommonCode::unsafe_create(code).to_short_code().unwrap()}; } -bool RsCommonCode::is_vertical_mirror() const noexcept { +bool RsLayout::is_vertical_mirror() const noexcept { return CommonCode::unsafe_create(code).is_vertical_mirror(); } -bool RsCommonCode::is_horizontal_mirror() const noexcept { +bool RsLayout::is_horizontal_mirror() const noexcept { return CommonCode::unsafe_create(code).is_horizontal_mirror(); } -RsCommonCode RsCommonCode::to_vertical_mirror() const noexcept { +RsLayout RsLayout::to_vertical_mirror() const noexcept { return {CommonCode::unsafe_create(code).to_vertical_mirror().unwrap()}; } -RsCommonCode RsCommonCode::to_horizontal_mirror() const noexcept { +RsLayout RsLayout::to_horizontal_mirror() const noexcept { return {CommonCode::unsafe_create(code).to_horizontal_mirror().unwrap()}; } -rust::Vec RsCommonCode::next_cases() const noexcept { +rust::Vec RsLayout::next_cases() const noexcept { std::vector result; auto mover = MaskMover([&result](const RawCode code, uint64_t) { result.emplace_back(code.to_common_code()); }); mover.next_cases(CommonCode::unsafe_create(code).to_raw_code(), 0); - rust::Vec vec; + rust::Vec vec; for (auto x : result) { - vec.emplace_back(RsCommonCode(x.unwrap())); + vec.emplace_back(RsLayout(x.unwrap())); } return vec; } diff --git a/src/core_ffi/rust_ffi/adapter/short_code.cc b/src/core_ffi/rust_ffi/adapter/short_code.cc index 45b0836..827bd35 100644 --- a/src/core_ffi/rust_ffi/adapter/short_code.cc +++ b/src/core_ffi/rust_ffi/adapter/short_code.cc @@ -4,8 +4,8 @@ using klotski::codec::ShortCode; +using klotski::ffi::RsLayout; using klotski::ffi::RsShortCode; -using klotski::ffi::RsCommonCode; bool klotski::ffi::short_code_check(const uint32_t val) { return ShortCode::check(val); @@ -23,7 +23,7 @@ rust::String RsShortCode::to_string() const noexcept { return ShortCode::unsafe_create(code).to_string(); } -RsCommonCode RsShortCode::to_common_code() const noexcept { +RsLayout RsShortCode::to_layout() const noexcept { return {ShortCode::unsafe_create(code).to_common_code().unwrap()}; } diff --git a/src/core_ffi/rust_ffi/build.rs b/src/core_ffi/rust_ffi/build.rs index e4e5af7..ca70170 100644 --- a/src/core_ffi/rust_ffi/build.rs +++ b/src/core_ffi/rust_ffi/build.rs @@ -22,8 +22,8 @@ fn main() { CFG.include_prefix = "rust_ffi"; cxx_build::bridge("src/bridge.rs") + .file("adapter/layout.cc") .file("adapter/short_code.cc") - .file("adapter/common_code.cc") .include("klotski/src/core") .flag("-std=c++23") .flag("-fno-rtti") @@ -32,6 +32,6 @@ fn main() { } println!("cargo:rerun-if-changed=src/bridge.rs"); + println!("cargo:rerun-if-changed=adapter/layout.cc"); println!("cargo:rerun-if-changed=adapter/short_code.cc"); - println!("cargo:rerun-if-changed=adapter/common_code.cc"); } diff --git a/src/core_ffi/rust_ffi/examples/demo.rs b/src/core_ffi/rust_ffi/examples/demo.rs index 645fbfc..4277a2a 100644 --- a/src/core_ffi/rust_ffi/examples/demo.rs +++ b/src/core_ffi/rust_ffi/examples/demo.rs @@ -1,5 +1,5 @@ +use klotski::Layout; use klotski::ShortCode; -use klotski::CommonCode; fn short_code_demo() { ShortCode::speed_up(false); @@ -13,14 +13,14 @@ fn short_code_demo() { println!("code: {:?}", code.unwrap()); println!("string: {}", code.to_string()); - println!("common_code: {:?}", code.to_common_code()); + println!("layout: {:?}", code.to_layout()); } -fn common_code_demo() { - assert!(CommonCode::check(0x1A9BF0C00)); - let code = CommonCode::create(0x1A9BF0C00).unwrap(); - assert_eq!(code, CommonCode::from_string("1A9BF0C").unwrap()); - assert_eq!(code, CommonCode::create(0x1A9BF0C00).unwrap()); +fn layout_demo() { + assert!(Layout::check(0x1A9BF0C00)); + let code = Layout::create(0x1A9BF0C00).unwrap(); + assert_eq!(code, Layout::from_string("1A9BF0C").unwrap()); + assert_eq!(code, Layout::create(0x1A9BF0C00).unwrap()); println!("code: {:?}", code.unwrap()); @@ -42,6 +42,6 @@ fn main() { println!("----------------------------------------------------------------"); short_code_demo(); println!("----------------------------------------------------------------"); - common_code_demo(); + layout_demo(); println!("----------------------------------------------------------------"); } diff --git a/src/core_ffi/rust_ffi/include/interface.h b/src/core_ffi/rust_ffi/include/interface.h index 35362e8..db4e8e4 100644 --- a/src/core_ffi/rust_ffi/include/interface.h +++ b/src/core_ffi/rust_ffi/include/interface.h @@ -6,11 +6,11 @@ namespace klotski::ffi { bool short_code_check(uint32_t val); -bool common_code_check(uint64_t val); +bool layout_check(uint64_t val); uint32_t short_code_from_str(rust::Str s); -uint64_t common_code_from_str(rust::Str s); +uint64_t layout_from_str(rust::Str s); void short_code_speed_up(bool fast_mode); diff --git a/src/core_ffi/rust_ffi/include/common_code.h b/src/core_ffi/rust_ffi/include/layout.h similarity index 100% rename from src/core_ffi/rust_ffi/include/common_code.h rename to src/core_ffi/rust_ffi/include/layout.h diff --git a/src/core_ffi/rust_ffi/src/bridge.rs b/src/core_ffi/rust_ffi/src/bridge.rs index f0d5f8e..f897f29 100644 --- a/src/core_ffi/rust_ffi/src/bridge.rs +++ b/src/core_ffi/rust_ffi/src/bridge.rs @@ -1,7 +1,7 @@ #[cxx::bridge(namespace = "klotski::ffi")] mod ffi { #[derive(Debug)] - struct RsCommonCode { + struct RsLayout { code: u64, } @@ -11,37 +11,37 @@ mod ffi { } unsafe extern "C++" { - include!("rust_ffi/include/common_code.h"); + include!("rust_ffi/include/layout.h"); /// only for internal use - fn common_code_check(val: u64) -> bool; + fn layout_check(val: u64) -> bool; /// only for internal use - fn common_code_from_str(s: &str) -> u64; + fn layout_from_str(s: &str) -> u64; - /// Convert CommonCode to string form. - fn to_string(self: &RsCommonCode) -> String; + /// Convert Layout to string form. + fn to_string(self: &RsLayout) -> String; - /// Convert CommonCode to shorten string form. - fn to_shorten_string(self: &RsCommonCode) -> String; + /// Convert Layout to shorten string form. + fn to_shorten_string(self: &RsLayout) -> String; - /// Convert CommonCode to ShortCode. - fn to_short_code(self: &RsCommonCode) -> RsShortCode; + /// Convert Layout to ShortCode. + fn to_short_code(self: &RsLayout) -> RsShortCode; /// Whether the layout is vertically symmetrical. - fn is_vertical_mirror(self: &RsCommonCode) -> bool; + fn is_vertical_mirror(self: &RsLayout) -> bool; /// Whether the layout is horizontally symmetrical. - fn is_horizontal_mirror(self: &RsCommonCode) -> bool; + fn is_horizontal_mirror(self: &RsLayout) -> bool; /// Calculate the vertically symmetrical klotski layout. - fn to_vertical_mirror(self: &RsCommonCode) -> RsCommonCode; + fn to_vertical_mirror(self: &RsLayout) -> RsLayout; /// Calculate the horizontally symmetrical klotski layout. - fn to_horizontal_mirror(self: &RsCommonCode) -> RsCommonCode; + fn to_horizontal_mirror(self: &RsLayout) -> RsLayout; - /// Obtain all next cases in CommonCode. - fn next_cases(self: &RsCommonCode) -> Vec; + /// Obtain all next cases in Layout. + fn next_cases(self: &RsLayout) -> Vec; } unsafe extern "C++" { @@ -56,29 +56,29 @@ mod ffi { /// Convert ShortCode to string form. fn to_string(self: &RsShortCode) -> String; - /// Convert ShortCode to CommonCode. - fn to_common_code(self: &RsShortCode) -> RsCommonCode; + /// Convert ShortCode to Layout. + fn to_layout(self: &RsShortCode) -> RsLayout; /// Build the conversion index for ShortCode. fn short_code_speed_up(fast_mode: bool); } } +pub use ffi::RsLayout as Layout; pub use ffi::RsShortCode as ShortCode; -pub use ffi::RsCommonCode as CommonCode; -impl CommonCode { - /// Check the validity of the original CommonCode. +impl Layout { + /// Check the validity of the original Layout. pub fn check(code: u64) -> bool { - ffi::common_code_check(code) + ffi::layout_check(code) } - /// Create CommonCode without any check. + /// Create Layout without any check. pub fn unsafe_create(code: u64) -> Self { Self { code } } - /// Create CommonCode with validity check. + /// Create Layout with validity check. pub fn create(code: u64) -> Option { if Self::check(code) { return Some(Self::unsafe_create(code)); @@ -91,9 +91,9 @@ impl CommonCode { self.code } - /// Create CommonCode from string form. + /// Create Layout from string form. pub fn from_string(s: &str) -> Option { - let val = ffi::common_code_from_str(s); + let val = ffi::layout_from_str(s); if val < 0x10_FFFF_FFFF { return Some(Self::unsafe_create(val)); } @@ -139,7 +139,7 @@ impl ShortCode { } } -impl PartialEq for CommonCode { +impl PartialEq for Layout { fn eq(&self, other: &Self) -> bool { self.code == other.code } diff --git a/src/core_ffi/rust_ffi/src/lib.rs b/src/core_ffi/rust_ffi/src/lib.rs index 9a4a73b..9f0a1f7 100644 --- a/src/core_ffi/rust_ffi/src/lib.rs +++ b/src/core_ffi/rust_ffi/src/lib.rs @@ -1,4 +1,4 @@ mod bridge; +pub use bridge::Layout; pub use bridge::ShortCode; -pub use bridge::CommonCode;