rsdiff_graphs/dcg/
node.rs

1/*
2    Appellation: node <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use crate::NodeIndex;
6use rsdiff::id::AtomicId;
7use rsdiff::ops::{BinaryOp, Op, UnaryOp};
8
9#[derive(Clone, Debug)]
10pub enum Node<T> {
11    Binary {
12        lhs: NodeIndex,
13        rhs: NodeIndex,
14        op: BinaryOp,
15    },
16    Unary {
17        input: NodeIndex,
18        op: UnaryOp,
19    },
20    Op {
21        inputs: Vec<NodeIndex>,
22        op: Op,
23    },
24    Input {
25        id: AtomicId,
26        param: bool,
27        value: T,
28    },
29}
30
31impl<T> Node<T> {
32    pub fn binary(lhs: NodeIndex, rhs: NodeIndex, op: impl Into<BinaryOp>) -> Self {
33        Node::Binary {
34            lhs,
35            rhs,
36            op: op.into(),
37        }
38    }
39
40    pub fn unary(input: NodeIndex, op: impl Into<UnaryOp>) -> Self {
41        Node::Unary {
42            input,
43            op: op.into(),
44        }
45    }
46
47    pub fn op(inputs: impl IntoIterator<Item = NodeIndex>, op: impl Into<Op>) -> Self {
48        Node::Op {
49            inputs: Vec::from_iter(inputs),
50            op: op.into(),
51        }
52    }
53
54    pub fn input(param: bool, value: T) -> Self {
55        Node::Input {
56            id: AtomicId::new(),
57            param,
58            value,
59        }
60    }
61
62    pub fn value(&self) -> T
63    where
64        T: Copy + Default,
65    {
66        match self {
67            Node::Input { value, .. } => *value,
68            _ => T::default(),
69        }
70    }
71}