1#![feature(step_trait)]
2use crate::*;
3use mech_core::*;
4use std::iter::Step;
5use nalgebra::{
6 base::{Matrix as naMatrix, Storage, StorageMut},
7 Dim, Scalar,
8};
9use mech_core::matrix::Matrix;
10use std::marker::PhantomData;
11
12#[derive(Debug)]
15pub struct RangeExclusiveScalar<T, MatA> {
16 pub from: Ref<T>,
17 pub to: Ref<T>,
18 pub out: Ref<MatA>,
19 phantom: PhantomData<T>,
20}
21impl<T, R1, C1, S1> MechFunctionFactory for RangeExclusiveScalar<T, naMatrix<T, R1, C1, S1>>
22where
23 T: Copy + Debug + Clone + Sync + Send +
24 CompileConst + ConstElem + AsValueKind +
25 PartialOrd + 'static + One + Add<Output = T>,
26 Ref<naMatrix<T, R1, C1, S1>>: ToValue,
27 naMatrix<T, R1, C1, S1>: CompileConst + ConstElem + AsNaKind,
28 R1: Dim + 'static, C1: Dim, S1: StorageMut<T, R1, C1> + Clone + Debug + 'static,
29{
30 fn new(args: FunctionArgs) -> MResult<Box<dyn MechFunction>> {
31 match args {
32 FunctionArgs::Binary(out, from, to) => {
33 let from: Ref<T> = unsafe { from.as_unchecked() }.clone();
34 let to: Ref<T> = unsafe { to.as_unchecked() }.clone();
35 let out: Ref<naMatrix<T, R1, C1, S1>> = unsafe { out.as_unchecked() }.clone();
36 Ok(Box::new(Self { from, to, out, phantom: PhantomData::default() }))
37 },
38 _ => Err(MechError2::new(
39 IncorrectNumberOfArguments { expected: 3, found: args.len() },
40 None
41 ).with_compiler_loc()
42 ),
43 }
44 }
45}
46impl<T, R1, C1, S1> MechFunctionImpl for RangeExclusiveScalar<T, naMatrix<T, R1, C1, S1>>
47where
48 Ref<naMatrix<T, R1, C1, S1>>: ToValue,
49 T: Copy + Scalar + Clone + Debug + Sync + Send + 'static + PartialOrd + One + Add<Output = T> + 'static,
50 R1: Dim, C1: Dim, S1: StorageMut<T, R1, C1> + Clone + Debug,
51{
52 fn solve(&self) {
53 unsafe {
54 let out_ptr = self.out.as_ptr() as *mut naMatrix<T, R1, C1, S1>;
55 let mut current = *self.from.as_ptr();
56 for i in 0..(*out_ptr).len() {
57 (&mut (*out_ptr))[i] = current;
58 current = current + T::one();
59 }
60 }
61 }
62 fn out(&self) -> Value { self.out.to_value() }
63 fn to_string(&self) -> String { format!("{:#?}", self) }
64}
65#[cfg(feature = "compiler")]
66impl<T, R1, C1, S1> MechFunctionCompiler for RangeExclusiveScalar<T, naMatrix<T, R1, C1, S1>>
67where
68 T: CompileConst + ConstElem + AsValueKind,
69 naMatrix<T, R1, C1, S1>: CompileConst + ConstElem + AsNaKind,
70{
71 fn compile(&self, ctx: &mut CompileCtx) -> MResult<Register> {
72 let name = format!("RangeExclusiveScalar<{}{}>", T::as_value_kind(), naMatrix::<T, R1, C1, S1>::as_na_kind());
73 compile_binop!(name, self.out, self.from, self.to, ctx, FeatureFlag::Builtin(FeatureKind::RangeExclusive) );
74 }
75}
76
77#[macro_export]
78macro_rules! impl_range_exclusive_match_arms {
79 ($fxn:ident, $arg1:expr, $arg2:expr, $($ty:tt, $feat:tt);+ $(;)?) => {
80 paste! {
81 match ($arg1, $arg2) {
82 $(
83 #[cfg(feature = $feat)]
84 (Value::[<$ty:camel>](from), Value::[<$ty:camel>](to)) => {
85 let from_val = *from.borrow();
86 let to_val = *to.borrow();
87 let diff = to_val - from_val;
88 if diff < $ty::zero() {
89 return Err(MechError2::new(
90 EmptyRangeError{},
91 None
92 ).with_compiler_loc());
93 }
94 let size = range_size_to_usize!(diff, $ty);
95 let mut vec = vec![from_val; size];
96 match size {
97 0 => Err(MechError2::new(
98 EmptyRangeError{},
99 None
100 ).with_compiler_loc()),
101 #[cfg(feature = "matrix1")]
102 1 => {
103 register_range!($fxn, $ty, $feat, Matrix1);
104 Ok(Box::new($fxn::<$ty,Matrix1<$ty>>{from: from.clone(), to: to.clone(), out: Ref::new(Matrix1::from_element(vec[0])), phantom: PhantomData::default()}))
105 }
106 #[cfg(all(not(feature = "matrix1"), feature = "matrixd") )]
107 1 => {
108 register_range!($fxn, $ty, $feat, DMatrix);
109 Ok(Box::new($fxn::<$ty,DMatrix<$ty>>{from: from.clone(), to: to.clone(), out: Ref::new(DMatrix::from_element(1,1,vec[0])), phantom: PhantomData::default()}))
110 }
111 #[cfg(feature = "row_vector2")]
112 2 => {
113 register_range!($fxn, $ty, $feat, RowVector2);
114 Ok(Box::new($fxn::<$ty,RowVector2<$ty>>{from: from.clone(), to: to.clone(), out: Ref::new(RowVector2::from_vec(vec)), phantom: PhantomData::default()}))
115 }
116 #[cfg(feature = "row_vector3")]
117 3 => {
118 register_range!($fxn, $ty, $feat, RowVector3);
119 Ok(Box::new($fxn::<$ty,RowVector3<$ty>>{from: from.clone(), to: to.clone(), out: Ref::new(RowVector3::from_vec(vec)), phantom: PhantomData::default()}))
120 }
121 #[cfg(feature = "row_vector4")]
122 4 => {
123 register_range!($fxn, $ty, $feat, RowVector4);
124 Ok(Box::new($fxn::<$ty,RowVector4<$ty>>{from: from.clone(), to: to.clone(), out: Ref::new(RowVector4::from_vec(vec)), phantom: PhantomData::default()}))
125 }
126 #[cfg(feature = "row_vectord")]
127 n => {
128 register_range!($fxn, $ty, $feat, RowDVector);
129 Ok(Box::new($fxn::<$ty,RowDVector<$ty>>{from: from.clone(), to: to.clone(), out: Ref::new(RowDVector::from_vec(vec)), phantom: PhantomData::default()}))
130 }
131 }
132 }
133 )+
134 (arg1,arg2) => Err(MechError2::new(
135 UnhandledFunctionArgumentKind2 {arg: (arg1.kind(),arg2.kind()), fxn_name: stringify!($fxn).to_string() },
136 None
137 ).with_compiler_loc()),
138 }
139 }
140 }
141}
142
143fn impl_range_exclusive_fxn(arg1_value: Value, arg2_value: Value) -> MResult<Box<dyn MechFunction>> {
144 impl_range_exclusive_match_arms!(RangeExclusiveScalar, arg1_value, arg2_value,
145 f32, "f32";
146 f64, "f64";
147 i8, "i8";
148 i16, "i16";
149 i32, "i32";
150 i64, "i64";
151 i128,"i128";
152 u8, "u8";
153 u16, "u16";
154 u32, "u32";
155 u64, "u64";
156 u128,"u128";
157 )
158}
159
160pub struct RangeExclusive {}
161
162impl NativeFunctionCompiler for RangeExclusive {
163 fn compile(&self, arguments: &Vec<Value>) -> MResult<Box<dyn MechFunction>> {
164 if arguments.len() != 2 {
165 return Err(MechError2::new(IncorrectNumberOfArguments { expected: 2, found: arguments.len() },None).with_compiler_loc());
166 }
167 let arg1 = arguments[0].clone();
168 let arg2 = arguments[1].clone();
169 match impl_range_exclusive_fxn(arg1.clone(), arg2.clone()) {
170 Ok(fxn) => Ok(fxn),
171 Err(_) => {
172 match (arg1,arg2) {
173 (Value::MutableReference(arg1),Value::MutableReference(arg2)) => {impl_range_exclusive_fxn(arg1.borrow().clone(),arg2.borrow().clone())}
174 (Value::MutableReference(arg1),arg2) => {impl_range_exclusive_fxn(arg1.borrow().clone(),arg2.clone())}
175 (arg1,Value::MutableReference(arg2)) => {impl_range_exclusive_fxn(arg1.clone(),arg2.borrow().clone())}
176 (arg1,arg2) => Err(MechError2::new(
177 UnhandledFunctionArgumentKind2 { arg: (arg1.kind(),arg2.kind()), fxn_name: "range/exclusive".to_string() },
178 None
179 ).with_compiler_loc()
180 ),
181 }
182 }
183 }
184 }
185}
186
187register_descriptor! {
188 FunctionCompilerDescriptor {
189 name: "range/exclusive",
190 ptr: &RangeExclusive{},
191 }
192}