Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datastructure #1

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,5 @@ Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

/.idea
1 change: 1 addition & 0 deletions native/logikSimulation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ crate-type = ["cdylib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
libc="0.2"
227 changes: 227 additions & 0 deletions native/logikSimulation/src/components.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
use std::fmt::Debug;
use std::ops::{Deref, DerefMut};
use crate::ffi::{FFIComponent, Point, FFIProperty};
use crate::wire::WireState;
use std::convert::TryFrom;

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
#[repr(u8)]
pub enum Direction {
North,
East,
South,
West
}

impl TryFrom<u8> for Direction {
type Error = ();

fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(Direction::North),
1 => Ok(Direction::East),
2 => Ok(Direction::South),
3 => Ok(Direction::West),
_ => Err(()),
}
}
}

#[derive(Debug)]
pub struct GraphicalComponent {
component: Box<dyn Component>,
position: Point,
direction: Direction
}

impl GraphicalComponent {
pub fn new(component: Box<dyn Component>, position: Point, direction: Direction) -> Self {
GraphicalComponent{ component, position, direction }
}

pub fn export(&self) -> FFIComponent {
FFIComponent::new(self.component.sprite(),
self.position.clone(),
self.direction.clone(),
self.component.inputs_pos().into_iter()
.map(|e| e + self.position)
.collect(),
self.component.outputs_pos().into_iter()
.map(|e| e + self.position)
.collect(),
self.component.properties()
.into_iter()
.map(|e| e.export())
.collect::<Vec<_>>())
}

pub fn pos(&self) -> Point {
self.position
}

pub fn direction(&self) -> Direction {
self.direction
}

/// Returns a vector containing the points in 2D space where the input pins are located
pub fn inputs_pos(&self) -> Vec<Point> {
self.component.inputs_pos().into_iter()
.map(|e| e + self.position)
.map(|e| e.rotate(Direction::North, self.direction))
.collect()
}

/// Returns a vector containing the points in 2D space where the output pins are located
pub fn outputs_pos(&self) -> Vec<Point> {
self.component.outputs_pos().into_iter()
.map(|e| e + self.position)
.map(|e| e.rotate(Direction::North, self.direction))
.collect()
}
}

impl Deref for GraphicalComponent {
type Target = Box<dyn Component>;

fn deref(&self) -> &Self::Target {
&self.component
}
}

impl DerefMut for GraphicalComponent {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.component
}
}

#[repr(i64)]
#[derive(Debug)]
pub enum Sprite {
Constant = 0,
AND,
}

#[repr(i64)]
#[derive(Debug)]
pub enum PropertyType {
HighTime, // Placeholder values until actual properties are implemented
LowTime,
}

pub trait Component: Debug {
fn sprite(&self) -> Sprite;
fn inputs(&self) -> Vec<&Option<usize>>;
fn inputs_mut(&mut self) -> Vec<&mut Option<usize>>;
fn inputs_pos(&self) -> Vec<Point>;
fn outputs(&self) -> Vec<&Option<usize>>;
fn outputs_mut(&mut self) -> Vec<&mut Option<usize>>;
fn outputs_pos(&self) -> Vec<Point>;
fn properties(&self) -> Vec<Box<dyn Property>>;
}

pub trait Property {
fn export(&self) -> FFIProperty;
}

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum PortType {
Input,
Output,
}

#[derive(Debug)]
pub struct Constant {
emitting: WireState,
output: Option<usize>,
}

impl Constant {
pub fn new(emitting: WireState) -> Self {
Self { emitting, output: None }
}
}

impl Component for Constant {
fn sprite(&self) -> Sprite {
Sprite::Constant
}

fn inputs(&self) -> Vec<&Option<usize>> {
vec![]
}

fn inputs_mut(&mut self) -> Vec<&mut Option<usize>> {
vec![]
}

fn inputs_pos(&self) -> Vec<Point> {
vec![]
}

fn outputs(&self) -> Vec<&Option<usize>> {
vec![&self.output]
}

fn outputs_mut(&mut self) -> Vec<&mut Option<usize>> {
vec![&mut self.output]
}

fn outputs_pos(&self) -> Vec<Point> {
vec![Point::new(0, 0)]
}

fn properties(&self) -> Vec<Box<dyn Property>> {
Vec::new()
}
}

#[derive(Debug)]
pub struct AND {
input1: Option<usize>,
input2: Option<usize>,
output: Option<usize>,
}

impl AND {
pub fn new() -> Self {
Self {
input1: None,
input2: None,
output: None,
}
}
}

impl Component for AND {
fn sprite(&self) -> Sprite {
Sprite::AND
}

fn inputs(&self) -> Vec<&Option<usize>> {
vec![&self.input1, &self.input2]
}

fn inputs_mut(&mut self) -> Vec<&mut Option<usize>> {
vec![&mut self.input1, &mut self.input2]
}

fn inputs_pos(&self) -> Vec<Point> {
vec![(-1, -1).into(), (1, -1).into()]
}

fn outputs(&self) -> Vec<&Option<usize>> {
vec![&self.output]
}

fn outputs_mut(&mut self) -> Vec<&mut Option<usize>> {
vec![&mut self.output]
}

fn outputs_pos(&self) -> Vec<Point> {
vec![(0, 1).into()]
}

fn properties(&self) -> Vec<Box<dyn Property>> {
Vec::new()
}
}
Loading