From 8d6b1bba448f01e41c8d77a4d7abc04dc1deb61f Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 28 Feb 2023 14:47:00 +0800 Subject: [PATCH] feat: RawCode mirror functions of rust codec --- src/cli/src/main.rs | 9 ++- src/rust_ffi/src/codec/ffi.rs | 8 +- src/rust_ffi/src/codec/raw_code.rs | 116 ++++++++++++++++++++++++++++- 3 files changed, 127 insertions(+), 6 deletions(-) diff --git a/src/cli/src/main.rs b/src/cli/src/main.rs index d4ac734..27a3b67 100644 --- a/src/cli/src/main.rs +++ b/src/cli/src/main.rs @@ -31,5 +31,12 @@ fn main() { // println!("{}", klotski_ffi::common_code_from_string("1A9BF0C").unwrap()); // println!("{}", klotski_ffi::common_code_from_string("1A9BF0C00").unwrap()); - klotski_ffi::demo(); + // klotski_ffi::demo(); + + // let r = klotski_ffi::RawCode::from(0x0_E58_FC8_5FF_EBC_4DB).unwrap(); // 4FEA13400 + // let r = klotski_ffi::RawCode::from(0x0_EDB_5FF_EBC_5C8_E58).unwrap(); // 8346AFC00 + // let r = klotski_ffi::RawCode::from(0x0_0F9_1CF_FFA_F17_6DA).unwrap(); // 6BFA47000 + let r = klotski_ffi::RawCode::from(0x0_603_EDF_5CA_FFF_5E2).unwrap(); // 1A9BF0C00 + + println!("{} => {}", r, r.to_common_code()); } diff --git a/src/rust_ffi/src/codec/ffi.rs b/src/rust_ffi/src/codec/ffi.rs index 9208ebd..1c65a20 100644 --- a/src/rust_ffi/src/codec/ffi.rs +++ b/src/rust_ffi/src/codec/ffi.rs @@ -204,28 +204,28 @@ pub fn to_horizontal_mirror(raw_code: u64) -> Result { } } -#[allow(dead_code)] +#[inline] pub fn is_vertical_mirror_unsafe(raw_code: u64) -> bool { unsafe { Core::is_vertical_mirror_unsafe(raw_code) } } -#[allow(dead_code)] +#[inline] pub fn is_horizontal_mirror_unsafe(raw_code: u64) -> bool { unsafe { Core::is_horizontal_mirror_unsafe(raw_code) } } -#[allow(dead_code)] +#[inline] pub fn to_vertical_mirror_unsafe(raw_code: u64) -> u64 { unsafe { Core::to_vertical_mirror_unsafe(raw_code) } } -#[allow(dead_code)] +#[inline] pub fn to_horizontal_mirror_unsafe(raw_code: u64) -> u64 { unsafe { Core::to_horizontal_mirror_unsafe(raw_code) diff --git a/src/rust_ffi/src/codec/raw_code.rs b/src/rust_ffi/src/codec/raw_code.rs index 64d13fb..05c3890 100644 --- a/src/rust_ffi/src/codec/raw_code.rs +++ b/src/rust_ffi/src/codec/raw_code.rs @@ -114,7 +114,121 @@ impl RawCode { } impl RawCode { + /// Determine whether two layouts are mirrored vertically. + /// # Example + /// ``` + /// use klotski_ffi::RawCode; + /// + /// let r1 = RawCode::from(0x0_E58_FC8_5FF_EBC_4DB).unwrap(); // 4FEA13400 + /// let r2 = RawCode::from(0x0_EDB_5FF_EBC_5C8_E58).unwrap(); // 8346AFC00 + /// + /// println!("is vertical mirror: {}", RawCode::is_vertical_mirror(&r1, &r2)); + /// ``` + #[inline] + pub fn is_vertical_mirror(r1: &RawCode, r2: &RawCode) -> bool { + r1 == &r2.to_vertical_mirror() + } - // TODO: mirror function + /// Determine whether two layouts are mirrored horizontally. + /// # Example + /// ``` + /// use klotski_ffi::RawCode; + /// + /// let r1 = RawCode::from(0x0_E58_FC8_5FF_EBC_4DB).unwrap(); // 4FEA13400 + /// let r2 = RawCode::from(0x0_0F9_1CF_FFA_F17_6DA).unwrap(); // 6BFA47000 + /// + /// println!("is horizontal mirror: {}", RawCode::is_horizontal_mirror(&r1, &r2)); + /// ``` + #[inline] + pub fn is_horizontal_mirror(r1: &RawCode, r2: &RawCode) -> bool { + r1 == &r2.to_horizontal_mirror() + } + /// Determine whether the current layout is vertically mirrored with another layout. + /// # Example + /// ``` + /// use klotski_ffi::RawCode; + /// + /// let r1 = RawCode::from(0x0_E58_FC8_5FF_EBC_4DB).unwrap(); // 4FEA13400 + /// let r2 = RawCode::from(0x0_EDB_5FF_EBC_5C8_E58).unwrap(); // 8346AFC00 + /// + /// println!("is vertical mirror: {}", r1.is_vertical_mirror_with(&r2)); + /// ``` + #[inline] + pub fn is_vertical_mirror_with(&self, raw_code: &RawCode) -> bool { + self == &raw_code.to_vertical_mirror() + } + + /// Determine whether the current layout is horizontally mirrored with another layout. + /// # Example + /// ``` + /// use klotski_ffi::RawCode; + /// + /// let r1 = RawCode::from(0x0_E58_FC8_5FF_EBC_4DB).unwrap(); // 4FEA13400 + /// let r2 = RawCode::from(0x0_0F9_1CF_FFA_F17_6DA).unwrap(); // 6BFA47000 + /// + /// println!("is horizontal mirror: {}", r1.is_horizontal_mirror_with(&r2)); + /// ``` + #[inline] + pub fn is_horizontal_mirror_with(&self, raw_code: &RawCode) -> bool { + self == &raw_code.to_horizontal_mirror() + } + + /// Determine whether the layout is vertically symmetrical. + /// # Example + /// ``` + /// use klotski_ffi::RawCode; + /// + /// let r = RawCode::from(0x0_603_EDF_5CA_FFF_5E2).unwrap(); // 1A9BF0C00 + /// + /// println!("is vertically symmetrical: {}", r.is_self_vertical_mirror()); + /// ``` + #[inline] + pub fn is_self_vertical_mirror(&self) -> bool { + codec_ffi::is_vertical_mirror_unsafe(self.code) + } + + /// Determine whether the layout is horizontally symmetrical. + /// # Example + /// ``` + /// use klotski_ffi::RawCode; + /// + /// let r = RawCode::from(0x0_603_EDF_5CA_FFF_5E2).unwrap(); // 1A9BF0C00 + /// + /// println!("is horizontally symmetrical: {}", r.is_self_horizontal_mirror()); + /// ``` + #[inline] + pub fn is_self_horizontal_mirror(&self) -> bool { + codec_ffi::is_horizontal_mirror_unsafe(self.code) + } + + /// Get vertical mirroring of the layout. + /// # Example + /// ``` + /// use klotski_ffi::RawCode; + /// + /// let r = RawCode::from(0x0_E58_FC8_5FF_EBC_4DB).unwrap(); // 4FEA13400 + /// println!("vertical mirror: {}", r.to_vertical_mirror()); + /// ``` + #[inline] + pub fn to_vertical_mirror(&self) -> RawCode { + RawCode::new( + codec_ffi::to_vertical_mirror_unsafe(self.code) + ) + } + + /// Get horizontally mirroring of the layout. + /// # Example + /// ``` + /// use klotski_ffi::RawCode; + /// + /// let r = RawCode::from(0x0_E58_FC8_5FF_EBC_4DB).unwrap(); // 4FEA13400 + /// println!("horizontal mirror: {}", r.to_horizontal_mirror()); + /// ``` + #[inline] + pub fn to_horizontal_mirror(&self) -> RawCode { + RawCode::new( + codec_ffi::to_horizontal_mirror_unsafe(self.code) + ) + } }