Browse Source

feat: more interfaces for Layout

master
Dnomd343 1 day from now
parent
commit
538bf03bf3
  1. 38
      src/core_ffi/rust_ffi/adapter/layout.cc
  2. 7
      src/core_ffi/rust_ffi/examples/demo.rs
  3. 30
      src/core_ffi/rust_ffi/src/bridge.rs

38
src/core_ffi/rust_ffi/adapter/layout.cc

@ -1,5 +1,6 @@
#include "rust_ffi/include/layout.h"
#include <group/group.h>
#include <mover/mover.h>
#include <common_code/common_code.h>
@ -8,6 +9,11 @@ using klotski::codec::CommonCode;
using klotski::mover::MaskMover;
using klotski::group::Group;
using klotski::group::BLOCK_NUM;
using klotski::group::GroupCases;
using klotski::group::GroupUnion;
using klotski::ffi::RsLayout;
using klotski::ffi::RsShortCode;
@ -52,6 +58,38 @@ RsLayout RsLayout::to_horizontal_mirror() const noexcept {
return {CommonCode::unsafe_create(code).to_horizontal_mirror().unwrap()};
}
uint8_t RsLayout::n_1x1() const noexcept {
return std::get<2>(BLOCK_NUM[type_id()]);
}
uint8_t RsLayout::n_1x2() const noexcept {
return std::get<0>(BLOCK_NUM[type_id()]) - n_2x1();
}
uint8_t RsLayout::n_2x1() const noexcept {
return std::get<1>(BLOCK_NUM[type_id()]);
}
uint8_t RsLayout::n_2x2() const noexcept {
return 1;
}
uint8_t RsLayout::type_id() const noexcept {
return GroupUnion::from_common_code(CommonCode::unsafe_create(code)).unwrap();
}
uint16_t RsLayout::pattern_id() const noexcept {
return GroupCases::obtain_group(CommonCode::unsafe_create(code)).pattern_id();
}
uint8_t RsLayout::toward_char() const noexcept {
return GroupCases::obtain_group(CommonCode::unsafe_create(code)).toward_char();
}
uint32_t RsLayout::case_id() const noexcept {
return GroupCases::obtain_info(CommonCode::unsafe_create(code)).case_id();
}
rust::Vec<RsLayout> RsLayout::next_cases() const noexcept {
std::vector<CommonCode> result;
auto mover = MaskMover([&result](const RawCode code, uint64_t) {

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

@ -35,6 +35,13 @@ fn layout_demo() {
println!("vertical_mirror: {:?}", code.to_vertical_mirror());
println!("horizontal_mirror: {:?}", code.to_horizontal_mirror());
println!("case_info: {}-{}{}-{}", code.type_id(), code.pattern_id(), code.toward_char() as char, code.case_id());
println!("n_1x1: {}", code.n_1x1());
println!("n_1x2: {}", code.n_1x2());
println!("n_2x1: {}", code.n_2x1());
println!("n_2x2: {}", code.n_2x2());
println!("next_cases: {:?}", code.next_cases());
}

30
src/core_ffi/rust_ffi/src/bridge.rs

@ -2,12 +2,12 @@
mod ffi {
#[derive(Debug)]
struct RsLayout {
code: u64,
code: u64
}
#[derive(Debug)]
struct RsShortCode {
code: u32,
code: u32
}
unsafe extern "C++" {
@ -40,6 +40,32 @@ mod ffi {
/// Calculate the horizontally symmetrical klotski layout.
fn to_horizontal_mirror(self: &RsLayout) -> RsLayout;
/// Obtain number of block 1x1.
fn n_1x1(self: &RsLayout) -> u8;
/// Obtain number of block 1x2.
fn n_1x2(self: &RsLayout) -> u8;
/// Obtain number of block 2x1.
fn n_2x1(self: &RsLayout) -> u8;
/// Obtain number of block 2x2.
fn n_2x2(self: &RsLayout) -> u8;
/// Obtain type_id value of current Layout.
fn type_id(self: &RsLayout) -> u8;
/// Obtain pattern_id value of current Layout.
fn pattern_id(self: &RsLayout) -> u16;
// TODO: add Toward enum for `toward` interface
// Obtain toward char of current Layout.
fn toward_char(self: &RsLayout) -> u8;
/// Obtain case_id value of current Layout.
fn case_id(self: &RsLayout) -> u32;
/// Obtain all next cases in Layout.
fn next_cases(self: &RsLayout) -> Vec<RsLayout>;
}

Loading…
Cancel
Save