mech_compare/
max.rs

1use crate::*;
2use mech_core::*;
3#[cfg(feature = "matrix")]
4use mech_core::matrix::Matrix;
5
6// Max ------------------------------------------------------------------------
7
8macro_rules! max_scalar_lhs_op {
9  ($lhs:expr, $rhs:expr, $out:expr) => {
10    unsafe {
11      for i in 0..(&*$lhs).len() {
12        let a = (&*$lhs)[i].clone();
13        let b = (*$rhs).clone();
14        (&mut *$out)[i] =
15          if a.partial_cmp(&b) != Some(std::cmp::Ordering::Less) {
16            a
17          } else {
18            b
19          };
20      }
21    }
22  };
23}
24
25macro_rules! max_scalar_rhs_op {
26  ($lhs:expr, $rhs:expr, $out:expr) => {
27    unsafe {
28      for i in 0..(&*$rhs).len() {
29        let a = (*$lhs).clone();
30        let b = (&*$rhs)[i].clone();
31        (&mut *$out)[i] = if a.partial_cmp(&b) != Some(std::cmp::Ordering::Less) { a } else { b };
32      }
33    }
34  };
35}
36
37macro_rules! max_vec_op {
38  ($lhs:expr, $rhs:expr, $out:expr) => {
39    unsafe {
40      for i in 0..(&*$lhs).len() {
41        let a = (&*$lhs)[i].clone();
42        let b = (&*$rhs)[i].clone();
43        (&mut *$out)[i] = if a.partial_cmp(&b) != Some(std::cmp::Ordering::Less) { a } else { b };
44      }
45    }
46  };
47}
48
49macro_rules! max_op {
50  ($lhs:expr, $rhs:expr, $out:expr) => {
51    unsafe {
52      let a = (*$lhs).clone();
53      let b = (*$rhs).clone();
54      (*$out) = if a.partial_cmp(&b) != Some(std::cmp::Ordering::Less) { a } else { b };
55    }
56  };
57}
58
59macro_rules! max_mat_vec_op {
60  ($lhs:expr, $rhs:expr, $out:expr) => {
61    unsafe {
62      let mut out_deref = &mut (*$out);
63      let lhs_deref = &(*$lhs);
64      let rhs_deref = &(*$rhs);
65      for (mut col, lhs_col) in out_deref.column_iter_mut().zip(lhs_deref.column_iter()) {
66        for i in 0..col.len() {
67          let a = lhs_col[i].clone();
68          let b = rhs_deref[i].clone();
69          col[i] = if a.partial_cmp(&b) != Some(std::cmp::Ordering::Less) { a } else { b };
70        }
71      }
72    }
73  };
74}
75
76macro_rules! max_vec_mat_op {
77  ($lhs:expr, $rhs:expr, $out:expr) => {
78    unsafe {
79      let mut out_deref = &mut (*$out);
80      let lhs_deref = &(*$lhs);
81      let rhs_deref = &(*$rhs);
82      for (mut col, rhs_col) in out_deref.column_iter_mut().zip(rhs_deref.column_iter()) {
83        for i in 0..col.len() {
84          let a = lhs_deref[i].clone();
85          let b = rhs_col[i].clone();
86          col[i] = if a.partial_cmp(&b) != Some(std::cmp::Ordering::Less) { a } else { b };
87        }
88      }
89    }
90  };
91}
92
93macro_rules! max_mat_row_op {
94  ($lhs:expr, $rhs:expr, $out:expr) => {
95    unsafe {
96      let mut out_deref = &mut (*$out);
97      let lhs_deref = &(*$lhs);
98      let rhs_deref = &(*$rhs);
99      for (mut row, lhs_row) in out_deref.row_iter_mut().zip(lhs_deref.row_iter()) {
100        for i in 0..row.len() {
101          let a = lhs_row[i].clone();
102          let b = rhs_deref[i].clone();
103          row[i] = if a.partial_cmp(&b) != Some(std::cmp::Ordering::Less) { a } else { b };
104        }
105      }
106    }
107  };
108}
109
110macro_rules! max_row_mat_op {
111  ($lhs:expr, $rhs:expr, $out:expr) => {
112    unsafe {
113      let mut out_deref = &mut (*$out);
114      let lhs_deref = &(*$lhs);
115      let rhs_deref = &(*$rhs);
116      for (mut row, rhs_row) in out_deref.row_iter_mut().zip(rhs_deref.row_iter()) {
117        for i in 0..row.len() {
118          let a = lhs_deref[i].clone();
119          let b = rhs_row[i].clone();
120          row[i] = if a.partial_cmp(&b) != Some(std::cmp::Ordering::Less) { a } else { b };
121        }
122      }
123    }
124  };
125}
126
127impl_compare_fxns2!(Max);
128
129fn impl_max_fxn(lhs_value: Value, rhs_value: Value) -> MResult<Box<dyn MechFunction>> {
130  impl_binop_match_arms!(
131    Max,
132    register_fxn_descriptor_inner,
133    (lhs_value, rhs_value),
134    I8,   i8, "i8";
135    I16,  i16, "i16";
136    I32,  i32, "i32";
137    I64,  i64, "i64";
138    I128, i128, "i128";
139    U8,   u8, "u8";
140    U16,  u16, "u16";
141    U32,  u32, "u32";
142    U64,  u64, "u64";
143    U128, u128, "u128";
144    F32,  f32, "f32";
145    F64,  f64, "f64";
146    R64, R64, "rational";
147    C64, C64, "complex";
148  )
149}
150
151impl_mech_binop_fxn!(CompareMax,impl_max_fxn,"compare/max");