mech_range/
inclusive.rs

1#![feature(step_trait)]
2use crate::*;
3use mech_core::*;
4use std::iter::Step;
5
6// Inclusive ------------------------------------------------------------------
7
8#[cfg(feature = "row_vectord")]
9#[derive(Debug)]
10struct RangeInclusiveScalar<T> {
11  max: Ref<T>,
12  min: Ref<T>,
13  out: Ref<RowDVector<T>>,
14}
15
16#[cfg(feature = "row_vectord")]
17impl<T> MechFunctionImpl for RangeInclusiveScalar<T>
18where
19    T: Copy + Debug + Clone + Sync + Send + Step + PartialEq + 'static,
20    Ref<RowDVector<T>>: ToValue
21{
22  fn solve(&self) {
23    let max_ptr = self.max.as_ptr();
24    let min_ptr = self.min.as_ptr();
25    let out_ptr = self.out.as_mut_ptr();
26    
27    unsafe {
28      let rng = (*min_ptr..=*max_ptr).collect::<Vec<T>>();
29      *out_ptr = RowDVector::from_vec(rng);
30    }
31  }
32  fn out(&self) -> Value { self.out.to_value() }
33  fn to_string(&self) -> String { format!("{:#?}", self)}
34}
35#[cfg(feature = "compiler")]
36impl<T> MechFunctionCompiler for RangeInclusiveScalar<T> {
37  fn compile(&self, ctx: &mut CompileCtx) -> MResult<Register> {
38    todo!();
39  }
40}
41
42pub struct RangeInclusive {}
43
44impl NativeFunctionCompiler for RangeInclusive {
45  fn compile(&self, arguments: &Vec<Value>) -> MResult<Box<dyn MechFunction>> {
46    if arguments.len() != 2 {
47      return Err(MechError{file: file!().to_string(), tokens: vec![], msg: "".to_string(), id: line!(), kind: MechErrorKind::IncorrectNumberOfArguments});
48    }
49    match (arguments[0].clone(), arguments[1].clone()) {
50      #[cfg(all(feature = "i8", feature = "row_vectord"))]
51      (Value::I8(min),   Value::I8(max))   => Ok(Box::new(RangeInclusiveScalar{max,min, out: Ref::new(RowDVector::from_element(1,0))})),
52      #[cfg(all(feature = "i16", feature = "row_vectord"))]
53      (Value::I16(min),  Value::I16(max))  => Ok(Box::new(RangeInclusiveScalar{max,min, out: Ref::new(RowDVector::from_element(1,0))})),
54      #[cfg(all(feature = "i32", feature = "row_vectord"))]
55      (Value::I32(min),  Value::I32(max))  => Ok(Box::new(RangeInclusiveScalar{max,min, out: Ref::new(RowDVector::from_element(1,0))})),
56      #[cfg(all(feature = "i64", feature = "row_vectord"))]
57      (Value::I64(min),  Value::I64(max))  => Ok(Box::new(RangeInclusiveScalar{max,min, out: Ref::new(RowDVector::from_element(1,0))})),
58      #[cfg(all(feature = "i128", feature = "row_vectord"))]
59      (Value::I128(min), Value::I128(max)) => Ok(Box::new(RangeInclusiveScalar{max,min, out: Ref::new(RowDVector::from_element(1,0))})),
60      #[cfg(all(feature = "u8", feature = "row_vectord"))]
61      (Value::U8(min),   Value::U8(max))   => Ok(Box::new(RangeInclusiveScalar{max,min, out: Ref::new(RowDVector::from_element(1,0))})),
62      #[cfg(all(feature = "u16", feature = "row_vectord"))]
63      (Value::U16(min),  Value::U16(max))  => Ok(Box::new(RangeInclusiveScalar{max,min, out: Ref::new(RowDVector::from_element(1,0))})),
64      #[cfg(all(feature = "u32", feature = "row_vectord"))]
65      (Value::U32(min),  Value::U32(max))  => Ok(Box::new(RangeInclusiveScalar{max,min, out: Ref::new(RowDVector::from_element(1,0))})),
66      #[cfg(all(feature = "u64", feature = "row_vectord"))]
67      (Value::U64(min),  Value::U64(max))  => Ok(Box::new(RangeInclusiveScalar{max,min, out: Ref::new(RowDVector::from_element(1,0))})),
68      #[cfg(all(feature = "u128", feature = "row_vectord"))]
69      (Value::U128(min), Value::U128(max)) => Ok(Box::new(RangeInclusiveScalar{max,min, out: Ref::new(RowDVector::from_element(1,0))})),
70      #[cfg(all(feature = "f32", feature = "row_vectord"))]
71      (Value::F32(min),  Value::F32(max))  => Ok(Box::new(RangeInclusiveScalar{max,min, out: Ref::new(RowDVector::from_element(1,F32::new(0.0)))})),
72      #[cfg(all(feature = "f64", feature = "row_vectord"))]
73      (Value::F64(min),  Value::F64(max))  => Ok(Box::new(RangeInclusiveScalar{max,min, out: Ref::new(RowDVector::from_element(1,F64::new(0.0)))})),
74      x => Err(MechError{file: file!().to_string(), tokens: vec![], msg: "".to_string(), id: line!(), kind: MechErrorKind::UnhandledFunctionArgumentKind})
75    }
76    }
77}