1use std::fmt;
4
5#[derive(Clone, Copy, PartialEq, Eq, Hash)]
7pub struct ExprId(pub(crate) u32);
8
9impl ExprId {
11 pub const ZERO: Self = Self(0);
13 pub const ONE: Self = Self(1);
15 pub const TWO: Self = Self(2);
17
18 #[inline]
20 pub fn from_index(index: u32) -> Self {
21 Self(index)
22 }
23
24 #[inline]
26 pub fn index(&self) -> u32 {
27 self.0
28 }
29}
30
31impl fmt::Debug for ExprId {
32 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33 write!(f, "e{}", self.0)
34 }
35}
36
37impl fmt::Display for ExprId {
38 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39 write!(f, "e{}", self.0)
40 }
41}
42
43impl Default for ExprId {
44 fn default() -> Self {
45 Self::ZERO
46 }
47}
48
49impl PartialOrd for ExprId {
50 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
51 Some(self.0.cmp(&other.0))
52 }
53}
54
55#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
60pub enum Node {
61 Var(u16),
64 Lit(u64),
66
67 Add(ExprId, ExprId),
70 Mul(ExprId, ExprId),
72 Neg(ExprId),
74 Recip(ExprId),
76 Sqrt(ExprId),
78 Sin(ExprId),
80 Atan2(ExprId, ExprId),
82 Exp2(ExprId),
84 Log2(ExprId),
86 Select(ExprId, ExprId, ExprId),
88}
89
90impl Node {
91 #[inline]
93 pub fn lit(v: f64) -> Self {
94 Self::Lit(v.to_bits())
95 }
96
97 #[inline]
99 pub fn as_f64(&self) -> Option<f64> {
100 match self {
101 Self::Lit(bits) => Some(f64::from_bits(*bits)),
102 _ => None,
103 }
104 }
105}