use std::fmt;
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct ExprId(pub(crate) u32);
impl ExprId {
pub const ZERO: Self = Self(0);
pub const ONE: Self = Self(1);
pub const TWO: Self = Self(2);
#[inline]
pub fn from_index(index: u32) -> Self {
Self(index)
}
#[inline]
pub fn index(&self) -> u32 {
self.0
}
}
impl fmt::Debug for ExprId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "e{}", self.0)
}
}
impl fmt::Display for ExprId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "e{}", self.0)
}
}
impl Default for ExprId {
fn default() -> Self {
Self::ZERO
}
}
impl PartialOrd for ExprId {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.0.cmp(&other.0))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Node {
Var(u16),
Lit(u64),
Add(ExprId, ExprId),
Mul(ExprId, ExprId),
Neg(ExprId),
Recip(ExprId),
Sqrt(ExprId),
Sin(ExprId),
Atan2(ExprId, ExprId),
Exp2(ExprId),
Log2(ExprId),
Select(ExprId, ExprId, ExprId),
}
impl Node {
#[inline]
pub fn lit(v: f64) -> Self {
Self::Lit(v.to_bits())
}
#[inline]
pub fn as_f64(&self) -> Option<f64> {
match self {
Self::Lit(bits) => Some(f64::from_bits(*bits)),
_ => None,
}
}
}