Skip to main content

mech_compare/
eq.rs

1use crate::*;
2use mech_core::*;
3#[cfg(feature = "matrix")]
4use mech_core::matrix::Matrix;
5
6// Equal ---------------------------------------------------------------
7
8macro_rules! eq_scalar_lhs_op {
9  ($lhs:expr, $rhs:expr, $out:expr) => {
10    unsafe {
11      for i in 0..(*$lhs).len() {
12        (&mut (*$out))[i] = (&(*$lhs))[i] == (*$rhs);
13      }}};}
14
15macro_rules! eq_scalar_rhs_op {
16  ($lhs:expr, $rhs:expr, $out:expr) => {
17    unsafe {
18      for i in 0..(*$rhs).len() {
19        (&mut (*$out))[i] = (*$lhs) == (&(*$rhs))[i];
20      }}};}
21
22macro_rules! eq_vec_op {
23  ($lhs:expr, $rhs:expr, $out:expr) => {
24    unsafe {
25      for i in 0..(*$lhs).len() {
26        (&mut (*$out))[i] = (&(*$lhs))[i] == (&(*$rhs))[i];
27      }}};}
28
29macro_rules! eq_op {
30  ($lhs:expr, $rhs:expr, $out:expr) => {
31    unsafe {
32      (*$out) = (*$lhs) == (*$rhs);
33    }};}
34
35macro_rules! eq_mat_vec_op {
36  ($lhs:expr, $rhs:expr, $out:expr) => {
37    unsafe {
38      let mut out_deref = &mut (*$out);
39      let lhs_deref = &(*$lhs);
40      let rhs_deref = &(*$rhs);
41      for (mut col, lhs_col) in out_deref.column_iter_mut().zip(lhs_deref.column_iter()) {
42        for i in 0..col.len() {
43          col[i] = lhs_col[i] == rhs_deref[i];
44        }
45      }
46    }
47  };}   
48      
49macro_rules! eq_vec_mat_op {
50  ($lhs:expr, $rhs:expr, $out:expr) => {
51      unsafe {
52        let mut out_deref = &mut (*$out);
53        let lhs_deref = &(*$lhs);
54        let rhs_deref = &(*$rhs);
55        for (mut col, rhs_col) in out_deref.column_iter_mut().zip(rhs_deref.column_iter()) {
56          for i in 0..col.len() {
57            col[i] = lhs_deref[i] == rhs_col[i];
58          }
59        }
60      }
61  };}
62  
63macro_rules! eq_mat_row_op {
64  ($lhs:expr, $rhs:expr, $out:expr) => {
65      unsafe {
66      let mut out_deref = &mut (*$out);
67      let lhs_deref = &(*$lhs);
68      let rhs_deref = &(*$rhs);
69      for (mut row, lhs_row) in out_deref.row_iter_mut().zip(lhs_deref.row_iter()) {
70          for i in 0..row.len() {
71          row[i] = lhs_row[i] == rhs_deref[i];
72          }
73      }
74      }
75  };}
76
77macro_rules! eq_row_mat_op {
78  ($lhs:expr, $rhs:expr, $out:expr) => {
79      unsafe {
80      let mut out_deref = &mut (*$out);
81      let lhs_deref = &(*$lhs);
82      let rhs_deref = &(*$rhs);
83      for (mut row, rhs_row) in out_deref.row_iter_mut().zip(rhs_deref.row_iter()) {
84          for i in 0..row.len() {
85          row[i] = lhs_deref[i] == rhs_row[i];
86          }
87      }
88      }
89  };}    
90
91impl_compare_fxns!(EQ);
92
93#[cfg(feature = "atom")]
94#[derive(Debug)]
95pub struct AtomEq {
96  pub lhs: Ref<MechAtom>,
97  pub rhs: Ref<MechAtom>,
98  pub out: Ref<bool>,
99}
100impl MechFunctionFactory for AtomEq {
101  fn new(args: FunctionArgs) -> MResult<Box<dyn MechFunction>> {
102    match args {
103      FunctionArgs::Binary(out, arg1, arg2) => {
104        let lhs: Ref<MechAtom> = unsafe { arg1.as_unchecked() }.clone();
105        let rhs: Ref<MechAtom> = unsafe { arg2.as_unchecked() }.clone();
106        let out: Ref<bool> = unsafe { out.as_unchecked() }.clone();
107        Ok(Box::new(AtomEq { lhs, rhs, out }))
108      }
109      _ => Err(MechError2::new(
110          IncorrectNumberOfArguments { expected: 2, found: args.len() }, 
111          None
112        ).with_compiler_loc()
113      ),
114    }
115  }
116}
117#[cfg(feature = "atom")]
118impl MechFunctionImpl for AtomEq {
119  fn solve(&self) {
120    let lhs_ptr = self.lhs.as_ptr();
121    let rhs_ptr = self.rhs.as_ptr();
122    let mut out_ptr = self.out.as_mut_ptr();
123    unsafe {
124      *out_ptr = (*lhs_ptr) == (*rhs_ptr);
125    }
126  }
127  fn out(&self) -> Value { self.out.to_value() }
128  fn to_string(&self) -> String { format!("{:#?}", self) }
129}
130#[cfg(feature = "atom")]
131#[cfg(feature = "compiler")]
132impl MechFunctionCompiler for AtomEq {
133  fn compile(&self, ctx: &mut CompileCtx) -> MResult<Register> {
134    let name = format!("AtomEq");
135    compile_binop!(name, self.out, self.lhs, self.rhs, ctx, FeatureFlag::Builtin(FeatureKind::Atom));
136  }
137}
138
139#[cfg(feature = "table")]
140#[derive(Debug)]
141pub struct TableEq {
142  pub lhs: Ref<MechTable>,
143  pub rhs: Ref<MechTable>,
144  pub out: Ref<bool>,
145}
146impl MechFunctionFactory for TableEq {
147  fn new(args: FunctionArgs) -> MResult<Box<dyn MechFunction>> {
148    match args {
149      FunctionArgs::Binary(out, arg1, arg2) => {
150        let lhs: Ref<MechTable> = unsafe { arg1.as_unchecked() }.clone();
151        let rhs: Ref<MechTable> = unsafe { arg2.as_unchecked() }.clone();
152        let out: Ref<bool> = unsafe { out.as_unchecked() }.clone();
153        Ok(Box::new(TableEq { lhs, rhs, out }))
154      }
155      _ => Err(MechError2::new(
156          IncorrectNumberOfArguments { expected: 2, found: args.len() }, 
157          None
158        ).with_compiler_loc()
159      ),
160    }
161  }
162}
163#[cfg(feature = "table")]
164impl MechFunctionImpl for TableEq {
165  fn solve(&self) {
166    let lhs_ptr = self.lhs.as_ptr();
167    let rhs_ptr = self.rhs.as_ptr();
168    let mut out_ptr = self.out.as_mut_ptr();
169    unsafe {
170      *out_ptr = (*lhs_ptr) == (*rhs_ptr);
171    }
172  }
173  fn out(&self) -> Value { self.out.to_value() }
174  fn to_string(&self) -> String { format!("{:#?}", self) }
175}
176#[cfg(feature = "table")]
177#[cfg(feature = "compiler")]
178impl MechFunctionCompiler for TableEq {
179  fn compile(&self, ctx: &mut CompileCtx) -> MResult<Register> {
180    let name = format!("TableEq");
181    compile_binop!(name, self.out, self.lhs, self.rhs, ctx, FeatureFlag::Builtin(FeatureKind::Table));
182  }
183}
184
185fn impl_eq_fxn(lhs_value: Value, rhs_value: Value) -> MResult<Box<dyn MechFunction>> {
186  match (&lhs_value, &rhs_value) {
187    #[cfg(all(feature = "table"))]
188    (Value::Table(lhs), Value::Table(rhs)) => {
189      register_descriptor! {
190        FunctionDescriptor {
191          name: "TableEq",
192          ptr: TableEq::new,
193        }
194      }
195      return Ok(Box::new(TableEq{lhs: lhs.clone(), rhs: rhs.clone(), out: Ref::new(false) }));
196    }
197    #[cfg(feature = "atom")]
198    (Value::Atom(lhs), Value::Atom(rhs)) => {
199      register_descriptor! {
200        FunctionDescriptor {
201          name: "AtomEq",
202          ptr: AtomEq::new,
203        }
204      }
205      return Ok(Box::new(AtomEq{lhs: lhs.clone(), rhs: rhs.clone(), out: Ref::new(false) }));
206    }
207    _ => (),
208  }
209  impl_binop_match_arms!(
210    EQ,
211    register_fxn_descriptor_inner,
212    (lhs_value, rhs_value),
213    Bool, bool, "bool";
214    I8,   bool, "i8";
215    I16,  bool, "i16";
216    I32,  bool, "i32";
217    I64,  bool, "i64";
218    I128, bool, "i128";
219    U8,   bool, "u8";
220    U16,  bool, "u16";
221    U32,  bool, "u32";
222    U64,  bool, "u64";
223    U128, bool, "u128";
224    F32,  bool, "f32";
225    F64,  bool, "f64";
226    String, bool, "string";
227    R64, bool, "rational";
228    C64, bool, "complex";
229  )
230}
231
232impl_mech_binop_fxn!(CompareEqual,impl_eq_fxn,"compare/eq");