Make the ramen crate a library

This commit is contained in:
mars 2022-09-18 04:13:05 -06:00
parent 7de349e60a
commit 1b3a991144
2 changed files with 15 additions and 24 deletions

View File

@ -5,22 +5,6 @@ pub mod node;
use node::{AtomOp, Edge, Node, NodeKind, SlotIndex};
fn main() {
let mut graph = Graph::new();
let nodes = &mut graph.nodes;
let n0 = nodes.insert(Node::literal(1.0));
let n1 = nodes.insert(Node::literal(2.0));
let n2 = nodes.insert(Node::op(AtomOp::Add, n0, n1));
let n3 = nodes.insert(Node::literal(4.0));
let n4 = nodes.insert(Node::op(AtomOp::Div, n2, n3));
let n5 = nodes.insert(Node::op(AtomOp::Mul, n4, n4));
let _n6 = nodes.insert(Node::op(AtomOp::Add, n5, n0));
let _n7 = nodes.insert(Node::literal(0.0)); // Should be ignored since it's not used
graph.compile().expect("Compilation failure");
}
#[derive(Debug)]
pub enum GraphError {
InvalidReference,
@ -36,21 +20,21 @@ pub struct Graph {
}
impl Graph {
fn new() -> Self {
pub fn new() -> Self {
Self { nodes: Slab::new() }
}
fn get_node(&self, index: usize) -> GraphResult<&Node> {
pub fn get_node(&self, index: usize) -> GraphResult<&Node> {
self.nodes.get(index).ok_or(GraphError::InvalidReference)
}
fn get_node_mut(&mut self, index: usize) -> GraphResult<&mut Node> {
pub fn get_node_mut(&mut self, index: usize) -> GraphResult<&mut Node> {
self.nodes
.get_mut(index)
.ok_or(GraphError::InvalidReference)
}
fn compile(&self) -> GraphResult<()> {
pub fn compile(&self) -> GraphResult<()> {
// TODO: Only compile the nodes that are actually used (set an output node)
// Iterate through the nodes in topological order
@ -80,7 +64,7 @@ impl Graph {
}
// Return the edges that can be derived from the graph
fn edges(&self) -> Result<Vec<Edge>, GraphError> {
pub fn edges(&self) -> Result<Vec<Edge>, GraphError> {
let topo_order = self.topological_order()?;
let mut edges = Vec::new();
@ -103,7 +87,7 @@ impl Graph {
Ok(edges)
}
fn topological_order(&self) -> Result<Vec<usize>, GraphError> {
pub fn topological_order(&self) -> Result<Vec<usize>, GraphError> {
// The number of nodes
let v = self.nodes.len();

View File

@ -42,6 +42,13 @@ impl Node {
inputs: vec![Some(lhs), Some(rhs)],
}
}
pub fn at(self, pos: glam::Vec2) -> Self {
Self {
pos,
..self
}
}
}
#[derive(Copy, Debug, Clone)]
@ -58,13 +65,13 @@ pub enum AtomOp {
Div,
}
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
#[derive(Copy, Debug, Clone, Hash, PartialEq, Eq)]
pub struct SlotIndex {
pub node: usize,
pub slot: usize,
}
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
#[derive(Copy, Debug, Clone, Hash, PartialEq, Eq)]
pub struct Edge {
pub input: SlotIndex,
pub output: SlotIndex,