Add Fst- and SndNode to node graph

This commit is contained in:
Dennis 2022-04-02 11:00:45 +02:00 committed by Keavon Chambers
parent 758487edf0
commit e999d66108
2 changed files with 49 additions and 38 deletions

View File

@ -39,25 +39,32 @@ impl<T: Node> AnyRef for T {
} }
} }
trait DefaultNode: Default { pub trait DefaultNode: Default {
fn default_node() -> ValueNode<Self> { fn default_node() -> ValueNode<Self> {
ValueNode::new(Self::default()) ValueNode::new(Self::default())
} }
} }
impl<T: std::default::Default> DefaultNode for T {} impl<T: std::default::Default> DefaultNode for T {}
trait After: Sized { pub trait After: Sized {
fn after<'a, First: Node>(&'a self, first: &'a First) -> ComposeNode<'a, First, Self> { fn after<'a, First: Node>(&'a self, first: &'a First) -> ComposeNode<'a, First, Self> {
ComposeNode::new(first, self) ComposeNode::new(first, self)
} }
} }
impl<Second: Node> After for Second {} impl<Second: Node> After for Second {}
pub trait DynamicInput {
fn set_kwarg_by_name(&mut self, name: &str, value: &dyn Any);
fn set_arg_by_index(&mut self, index: usize, value: &dyn Any);
}
fn main() { fn main() {
let int = IntNode::<32>; let int = IntNode::<32>;
let add: u32 = AddNode::<u32>::default().eval((int.eval(&()), int.eval(&()))); let add: u32 = AddNode::<u32>::default().eval((int.eval(&()), int.eval(&())));
let fnode = FnNode::new(|(a, b): &(i32, i32)| a - b); let fnode = FnNode::new(|(a, b): &(i32, i32)| a - b);
let sub = fnode.any(&("a", 2)); //let sub = fnode.any(&("a", 2));
let cache = CacheNode::new(&fnode);
let foo = cache.eval(&(2, 3));
/* /*
let curry: CurryNthArgNode<'_, _, _, u32, u32, 0> = CurryNthArgNode::new(&AddNode, &int); let curry: CurryNthArgNode<'_, _, _, u32, u32, 0> = CurryNthArgNode::new(&AddNode, &int);
@ -65,5 +72,5 @@ fn main() {
let n = ValueNode::new(10_u32); let n = ValueNode::new(10_u32);
let curry: CurryNthArgNode<'_, _, _, u32, _, 0> = CurryNthArgNode::new(&composition, &n); let curry: CurryNthArgNode<'_, _, _, u32, _, 0> = CurryNthArgNode::new(&composition, &n);
*/ */
println!("{}", sub) println!("{}", foo)
} }

View File

@ -9,7 +9,7 @@ use std::{
marker::PhantomData, marker::PhantomData,
}; };
use crate::{insert_after_nth, After, Node}; use crate::{insert_after_nth, After, DynamicInput, Node};
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use parking_lot::RawRwLock; use parking_lot::RawRwLock;
use storage_map::{StorageMap, StorageMapGuard}; use storage_map::{StorageMap, StorageMapGuard};
@ -49,6 +49,30 @@ impl<T: std::ops::Add + 'static + Copy> Node for AddNode<T> {
} }
} }
#[derive(Default)]
/// Destructures a Tuple of two values and returns the first one
pub struct FstNode<T, U>(PhantomData<T>, PhantomData<U>);
impl<T: Copy, U> Node for FstNode<T, U> {
type Output<'a> = &'a T where Self: 'a;
type Input<'a> = &'a (T, U) where Self: 'a;
fn eval<'a, I: Borrow<Self::Input<'a>>>(&'a self, input: I) -> Self::Output<'a> {
let &(ref a, _) = input.borrow();
a
}
}
#[derive(Default)]
/// Destructures a Tuple of two values and returns the first one
pub struct SndNode<T, U>(PhantomData<T>, PhantomData<U>);
impl<T, U: Copy> Node for SndNode<T, U> {
type Output<'a> = &'a U where Self: 'a;
type Input<'a> = &'a (T, U) where Self: 'a;
fn eval<'a, I: Borrow<Self::Input<'a>>>(&'a self, input: I) -> Self::Output<'a> {
let &(_, ref b) = input.borrow();
b
}
}
pub struct ComposeNode<'n, FIRST, SECOND> { pub struct ComposeNode<'n, FIRST, SECOND> {
first: &'n FIRST, first: &'n FIRST,
second: &'n SECOND, second: &'n SECOND,
@ -143,45 +167,25 @@ impl<'n, 'c, CachedNode: Node> CacheNode<'n, 'c, CachedNode> {
} }
} }
/* pub struct ProxyNode<T: DynamicInput>(T);
/// Caches the output of a given Node and acts as a proxy impl<T: DynamicInput> Node for ProxyNode<T> {
/// Automatically resets if it receives different input type Output<'a> = T where Self: 'a;
struct SmartCacheNode<'n, 'c, NODE: Node + 'c>
where
for<'a> NODE::Input<'a>: Hash,
{
cache: InnerSmartCacheNode<'n, 'c, NODE>,
}
impl<'n: 'c, 'c, NODE: Node> Node for SmartCacheNode<'n, 'c, NODE>
where
for<'a> NODE::Input<'a>: Hash,
{
type Input<'a> = NODE::Input<'a> where Self: 'a, 'c : 'a;
type Output<'a> = &'a NODE::Output<'a> where Self: 'a, 'c: 'a;
fn eval<'a, I: Borrow<Self::Input<'a>>>(&'a self, input: I) -> Self::Output<'a> {
let mut hasher = DefaultHasher::new();
input.borrow().hash(&mut hasher);
let hash = hasher.finish();
let node = self.cache.eval(input); type Input<'a> = &'a () where Self: 'a;
node.eval(input);
fn eval<'a, I: Borrow<Self::Input<'a>>>(&'a self, input: I) -> Self::Output<'a> {
todo!() todo!()
} }
} }
impl<T: DynamicInput> DynamicInput for ProxyNode<T> {
fn set_kwarg_by_name(&mut self, name: &str, value: &dyn Any) {
self.0.set_kwarg_by_name(name, value)
}
impl<'n, 'c, NODE: Node> SmartCacheNode<'n, 'c, NODE> fn set_arg_by_index(&mut self, index: usize, value: &dyn Any) {
where self.0.set_arg_by_index(index, value)
for<'a> NODE::Input<'a>: Hash,
{
pub fn clear(&'n mut self) {
self.cache.clear();
} }
pub fn new(node: &'n NODE) -> SmartCacheNode<'n, 'c, NODE> { }
SmartCacheNode {
cache: InnerSmartCacheNode::new(node),
}
}
}*/
/// Caches the output of a given Node and acts as a proxy /// Caches the output of a given Node and acts as a proxy
/// Automatically resets if it receives different input /// Automatically resets if it receives different input