Browse Source

update: using `Layout` instead of `CommonCode`

master
Dnomd343 16 hours from now
parent
commit
84de126c13
  1. 2
      src/core_ffi/rust_ffi/CMakeLists.txt
  2. 2
      src/core_ffi/rust_ffi/Cargo.lock
  3. 2
      src/core_ffi/rust_ffi/Cargo.toml
  4. 28
      src/core_ffi/rust_ffi/adapter/layout.cc
  5. 4
      src/core_ffi/rust_ffi/adapter/short_code.cc
  6. 4
      src/core_ffi/rust_ffi/build.rs
  7. 16
      src/core_ffi/rust_ffi/examples/demo.rs
  8. 4
      src/core_ffi/rust_ffi/include/interface.h
  9. 0
      src/core_ffi/rust_ffi/include/layout.h
  10. 54
      src/core_ffi/rust_ffi/src/bridge.rs
  11. 2
      src/core_ffi/rust_ffi/src/lib.rs

2
src/core_ffi/rust_ffi/CMakeLists.txt

@ -5,8 +5,8 @@ set(CMAKE_CXX_STANDARD 23)
if (KLSK_DEV_MODE) if (KLSK_DEV_MODE)
set(KLSK_RUST_FFI_SRC set(KLSK_RUST_FFI_SRC
adapter/layout.cc
adapter/short_code.cc adapter/short_code.cc
adapter/common_code.cc
target/cxxbridge/rust_ffi/src/bridge.rs.cc target/cxxbridge/rust_ffi/src/bridge.rs.cc
) )
add_library(klotski_rs STATIC ${KLSK_RUST_FFI_SRC}) # just for IDE highlight add_library(klotski_rs STATIC ${KLSK_RUST_FFI_SRC}) # just for IDE highlight

2
src/core_ffi/rust_ffi/Cargo.lock

@ -76,7 +76,7 @@ dependencies = [
[[package]] [[package]]
name = "klotski" name = "klotski"
version = "0.0.1" version = "0.0.2"
dependencies = [ dependencies = [
"cmake", "cmake",
"cxx", "cxx",

2
src/core_ffi/rust_ffi/Cargo.toml

@ -1,6 +1,6 @@
[package] [package]
name = "klotski" name = "klotski"
version = "0.0.1" version = "0.0.2"
edition = "2021" edition = "2021"
authors = ["Dnomd343 <dnomd343@gmail.com>"] authors = ["Dnomd343 <dnomd343@gmail.com>"]
description = "Klotski multifunctional engine with high performance" description = "Klotski multifunctional engine with high performance"

28
src/core_ffi/rust_ffi/adapter/common_code.cc → 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 <mover/mover.h> #include <mover/mover.h>
#include <common_code/common_code.h> #include <common_code/common_code.h>
@ -8,11 +8,11 @@ using klotski::codec::CommonCode;
using klotski::mover::MaskMover; using klotski::mover::MaskMover;
using klotski::ffi::RsLayout;
using klotski::ffi::RsShortCode; using klotski::ffi::RsShortCode;
using klotski::ffi::RsCommonCode;
// TODO: it seems that cxx.rs not support `std::optional` // 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()}; const std::string_view sv {s.data(), s.length()};
if (const auto ret = CommonCode::from_string(sv); ret.has_value()) { if (const auto ret = CommonCode::from_string(sv); ret.has_value()) {
return ret.value().unwrap(); 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 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); 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(); 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); 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()}; 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(); 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(); 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()}; 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()}; return {CommonCode::unsafe_create(code).to_horizontal_mirror().unwrap()};
} }
rust::Vec<RsCommonCode> RsCommonCode::next_cases() const noexcept { rust::Vec<RsLayout> RsLayout::next_cases() const noexcept {
std::vector<CommonCode> result; std::vector<CommonCode> result;
auto mover = MaskMover([&result](const RawCode code, uint64_t) { auto mover = MaskMover([&result](const RawCode code, uint64_t) {
result.emplace_back(code.to_common_code()); result.emplace_back(code.to_common_code());
}); });
mover.next_cases(CommonCode::unsafe_create(code).to_raw_code(), 0); mover.next_cases(CommonCode::unsafe_create(code).to_raw_code(), 0);
rust::Vec<RsCommonCode> vec; rust::Vec<RsLayout> vec;
for (auto x : result) { for (auto x : result) {
vec.emplace_back(RsCommonCode(x.unwrap())); vec.emplace_back(RsLayout(x.unwrap()));
} }
return vec; return vec;
} }

4
src/core_ffi/rust_ffi/adapter/short_code.cc

@ -4,8 +4,8 @@
using klotski::codec::ShortCode; using klotski::codec::ShortCode;
using klotski::ffi::RsLayout;
using klotski::ffi::RsShortCode; using klotski::ffi::RsShortCode;
using klotski::ffi::RsCommonCode;
bool klotski::ffi::short_code_check(const uint32_t val) { bool klotski::ffi::short_code_check(const uint32_t val) {
return ShortCode::check(val); return ShortCode::check(val);
@ -23,7 +23,7 @@ rust::String RsShortCode::to_string() const noexcept {
return ShortCode::unsafe_create(code).to_string(); 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()}; return {ShortCode::unsafe_create(code).to_common_code().unwrap()};
} }

4
src/core_ffi/rust_ffi/build.rs

@ -22,8 +22,8 @@ fn main() {
CFG.include_prefix = "rust_ffi"; CFG.include_prefix = "rust_ffi";
cxx_build::bridge("src/bridge.rs") cxx_build::bridge("src/bridge.rs")
.file("adapter/layout.cc")
.file("adapter/short_code.cc") .file("adapter/short_code.cc")
.file("adapter/common_code.cc")
.include("klotski/src/core") .include("klotski/src/core")
.flag("-std=c++23") .flag("-std=c++23")
.flag("-fno-rtti") .flag("-fno-rtti")
@ -32,6 +32,6 @@ fn main() {
} }
println!("cargo:rerun-if-changed=src/bridge.rs"); 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/short_code.cc");
println!("cargo:rerun-if-changed=adapter/common_code.cc");
} }

16
src/core_ffi/rust_ffi/examples/demo.rs

@ -1,5 +1,5 @@
use klotski::Layout;
use klotski::ShortCode; use klotski::ShortCode;
use klotski::CommonCode;
fn short_code_demo() { fn short_code_demo() {
ShortCode::speed_up(false); ShortCode::speed_up(false);
@ -13,14 +13,14 @@ fn short_code_demo() {
println!("code: {:?}", code.unwrap()); println!("code: {:?}", code.unwrap());
println!("string: {}", code.to_string()); println!("string: {}", code.to_string());
println!("common_code: {:?}", code.to_common_code()); println!("layout: {:?}", code.to_layout());
} }
fn common_code_demo() { fn layout_demo() {
assert!(CommonCode::check(0x1A9BF0C00)); assert!(Layout::check(0x1A9BF0C00));
let code = CommonCode::create(0x1A9BF0C00).unwrap(); let code = Layout::create(0x1A9BF0C00).unwrap();
assert_eq!(code, CommonCode::from_string("1A9BF0C").unwrap()); assert_eq!(code, Layout::from_string("1A9BF0C").unwrap());
assert_eq!(code, CommonCode::create(0x1A9BF0C00).unwrap()); assert_eq!(code, Layout::create(0x1A9BF0C00).unwrap());
println!("code: {:?}", code.unwrap()); println!("code: {:?}", code.unwrap());
@ -42,6 +42,6 @@ fn main() {
println!("----------------------------------------------------------------"); println!("----------------------------------------------------------------");
short_code_demo(); short_code_demo();
println!("----------------------------------------------------------------"); println!("----------------------------------------------------------------");
common_code_demo(); layout_demo();
println!("----------------------------------------------------------------"); println!("----------------------------------------------------------------");
} }

4
src/core_ffi/rust_ffi/include/interface.h

@ -6,11 +6,11 @@ namespace klotski::ffi {
bool short_code_check(uint32_t val); 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); 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); void short_code_speed_up(bool fast_mode);

0
src/core_ffi/rust_ffi/include/common_code.h → src/core_ffi/rust_ffi/include/layout.h

54
src/core_ffi/rust_ffi/src/bridge.rs

@ -1,7 +1,7 @@
#[cxx::bridge(namespace = "klotski::ffi")] #[cxx::bridge(namespace = "klotski::ffi")]
mod ffi { mod ffi {
#[derive(Debug)] #[derive(Debug)]
struct RsCommonCode { struct RsLayout {
code: u64, code: u64,
} }
@ -11,37 +11,37 @@ mod ffi {
} }
unsafe extern "C++" { unsafe extern "C++" {
include!("rust_ffi/include/common_code.h"); include!("rust_ffi/include/layout.h");
/// only for internal use /// only for internal use
fn common_code_check(val: u64) -> bool; fn layout_check(val: u64) -> bool;
/// only for internal use /// only for internal use
fn common_code_from_str(s: &str) -> u64; fn layout_from_str(s: &str) -> u64;
/// Convert CommonCode to string form. /// Convert Layout to string form.
fn to_string(self: &RsCommonCode) -> String; fn to_string(self: &RsLayout) -> String;
/// Convert CommonCode to shorten string form. /// Convert Layout to shorten string form.
fn to_shorten_string(self: &RsCommonCode) -> String; fn to_shorten_string(self: &RsLayout) -> String;
/// Convert CommonCode to ShortCode. /// Convert Layout to ShortCode.
fn to_short_code(self: &RsCommonCode) -> RsShortCode; fn to_short_code(self: &RsLayout) -> RsShortCode;
/// Whether the layout is vertically symmetrical. /// 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. /// 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. /// 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. /// 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. /// Obtain all next cases in Layout.
fn next_cases(self: &RsCommonCode) -> Vec<RsCommonCode>; fn next_cases(self: &RsLayout) -> Vec<RsLayout>;
} }
unsafe extern "C++" { unsafe extern "C++" {
@ -56,29 +56,29 @@ mod ffi {
/// Convert ShortCode to string form. /// Convert ShortCode to string form.
fn to_string(self: &RsShortCode) -> String; fn to_string(self: &RsShortCode) -> String;
/// Convert ShortCode to CommonCode. /// Convert ShortCode to Layout.
fn to_common_code(self: &RsShortCode) -> RsCommonCode; fn to_layout(self: &RsShortCode) -> RsLayout;
/// Build the conversion index for ShortCode. /// Build the conversion index for ShortCode.
fn short_code_speed_up(fast_mode: bool); fn short_code_speed_up(fast_mode: bool);
} }
} }
pub use ffi::RsLayout as Layout;
pub use ffi::RsShortCode as ShortCode; pub use ffi::RsShortCode as ShortCode;
pub use ffi::RsCommonCode as CommonCode;
impl CommonCode { impl Layout {
/// Check the validity of the original CommonCode. /// Check the validity of the original Layout.
pub fn check(code: u64) -> bool { 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 { pub fn unsafe_create(code: u64) -> Self {
Self { code } Self { code }
} }
/// Create CommonCode with validity check. /// Create Layout with validity check.
pub fn create(code: u64) -> Option<Self> { pub fn create(code: u64) -> Option<Self> {
if Self::check(code) { if Self::check(code) {
return Some(Self::unsafe_create(code)); return Some(Self::unsafe_create(code));
@ -91,9 +91,9 @@ impl CommonCode {
self.code self.code
} }
/// Create CommonCode from string form. /// Create Layout from string form.
pub fn from_string(s: &str) -> Option<Self> { pub fn from_string(s: &str) -> Option<Self> {
let val = ffi::common_code_from_str(s); let val = ffi::layout_from_str(s);
if val < 0x10_FFFF_FFFF { if val < 0x10_FFFF_FFFF {
return Some(Self::unsafe_create(val)); 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 { fn eq(&self, other: &Self) -> bool {
self.code == other.code self.code == other.code
} }

2
src/core_ffi/rust_ffi/src/lib.rs

@ -1,4 +1,4 @@
mod bridge; mod bridge;
pub use bridge::Layout;
pub use bridge::ShortCode; pub use bridge::ShortCode;
pub use bridge::CommonCode;

Loading…
Cancel
Save