76 lines
1.9 KiB
Rust
76 lines
1.9 KiB
Rust
#![feature(array_zip)]
|
|
|
|
use system::Storage;
|
|
use crate::system::grid::{Position, VectorStorage};
|
|
|
|
mod system;
|
|
|
|
#[derive(Eq, PartialEq, Debug)]
|
|
#[repr(C)]
|
|
pub struct CPosition(i32, i32);
|
|
|
|
pub struct CStorage(VectorStorage);
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn storage_new(grid_size: u32) -> &'static mut CStorage {
|
|
let mut pntr = Box::new(CStorage(VectorStorage::new(grid_size)));
|
|
Box::leak(pntr)
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn storage_at(storage: &CStorage, i: i32, j: i32) -> bool {
|
|
storage.0.at(&Position { x: i, y: j })
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn storage_deposit(storage: &mut CStorage, i: i32, j: i32, val: u8) {
|
|
storage.0.write(&Position { x: i, y: j }, val == 1);
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn walk(d: u32, i: i32, j: i32) -> CPosition {
|
|
return test::b(d, i, j);
|
|
}
|
|
|
|
mod test {
|
|
use num_integer::Integer;
|
|
use crate::CPosition;
|
|
use crate::system::grid::Position;
|
|
|
|
pub(crate) fn a(d: u32, i: i32, j: i32) -> CPosition {
|
|
match d {
|
|
0 => CPosition(i + 1, j),
|
|
1 => CPosition(i - 1, j),
|
|
2 => CPosition(i, j + 1),
|
|
3 => CPosition(i, j - 1),
|
|
_ => panic!("Ahh"),
|
|
}
|
|
}
|
|
|
|
pub(crate) fn b(d: u32, i: i32, j: i32) -> CPosition {
|
|
let (dim, sign) = d.div_rem(&2);
|
|
let sign = if sign == 0 { 1 } else { -1 };
|
|
// HACK: Our conventin and the MVA are different, since we are trying to strangle fig this, quick hack.
|
|
let offset = Position::in_direction(dim, sign);
|
|
let next = Position { x: i, y: j } + offset;
|
|
|
|
CPosition(next.x, next.y)
|
|
}
|
|
|
|
#[test]
|
|
fn test() {
|
|
let d = [0, 1, 2, 3];
|
|
d.iter()
|
|
.map(|d| d.div_rem(&2))
|
|
.for_each(|p| println!("{p:?}"));
|
|
}
|
|
|
|
#[test]
|
|
fn alignment() {
|
|
let d = [0, 1, 2, 3];
|
|
d.iter()
|
|
.map(|d| (a(*d, 0, 0), b(*d, 0, 0)))
|
|
.for_each(|p| assert_eq!(p.0, p.1));
|
|
}
|
|
}
|