vector_expression/
vector_expression.rs1use xpr::{ops::Term, Expression, Fold, Xpr};
2
3#[derive(Debug)]
6struct Vec<const N: usize>(Box<[f64; N]>);
7
8impl<const N: usize> Vec<{ N }> {
9 #[inline]
10 fn new(array: [f64; N]) -> Self {
11 Self(Box::new(array))
12 }
13}
14
15impl<const N: usize> Expression for Vec<N> {}
17
18struct IthElement<const N: usize>(usize);
21
22impl<const N: usize> Fold<Term<Vec<{ N }>>> for IthElement<{ N }> {
24 type Output = f64;
26
27 #[inline]
29 fn fold(&mut self, Term(v): &Term<Vec<{ N }>>) -> f64 {
30 v.0[self.0]
31 }
32}
33
34impl<T, const N: usize> From<Xpr<T>> for Vec<{ N }>
35where
36 IthElement<N>: Fold<Xpr<T>, Output = f64>,
37{
38 #[inline]
40 fn from(expr: Xpr<T>) -> Self {
41 let mut ret = Vec::new(unsafe { std::mem::MaybeUninit::uninit().assume_init() });
43
44 for (i, e) in ret.0.iter_mut().enumerate() {
46 *e = IthElement(i).fold(&expr);
47 }
48 ret
49 }
50}
51
52pub fn main() {
53 let x1 = Vec::new([0.6; 5000]).into_xpr();
55 let x2 = Vec::new([1.0; 5000]).into_xpr();
56 let x3 = Vec::new([40.0; 5000]).into_xpr();
57 let x4 = Vec::new([100.0; 5000]).into_xpr();
58 let x5 = Vec::new([3000.0; 5000]).into_xpr();
59
60 let v = Vec::from(x1 + x2 + x3 + x4 + x5);
62 println!("v[0..5] = {:?}", &v.0[0..5]);
63}