Skip to main content

partiql/value/
pql_vector.rs

1use std::ops::{Add, Div, Mul, Neg, Rem, Sub};
2
3use rayon::prelude::*;
4
5use crate::value::PqlValue;
6
7#[derive(Debug, Default, Clone, PartialEq)]
8pub struct PqlVector(pub Vec<PqlValue>);
9
10impl From<PqlVector> for PqlValue {
11    fn from(v: PqlVector) -> Self {
12        Self::Array(v.0)
13    }
14}
15
16impl Neg for PqlVector {
17    type Output = Self;
18    fn neg(self) -> Self::Output {
19        let v = self.0.into_iter().map(|value| -value).collect::<Vec<_>>();
20        Self(v)
21    }
22}
23
24impl Add for PqlVector {
25    type Output = Self;
26    fn add(self, other: Self) -> Self::Output {
27        let v = self
28            .0
29            .into_iter()
30            .zip(other.0.into_iter())
31            .map(|(a, b)| a + b)
32            .collect::<Vec<PqlValue>>();
33        Self(v)
34    }
35}
36
37impl Sub for PqlVector {
38    type Output = Self;
39    fn sub(self, other: Self) -> Self::Output {
40        let v = self
41            .0
42            .into_iter()
43            .zip(other.0.into_iter())
44            .map(|(a, b)| a - b)
45            .collect::<Vec<PqlValue>>();
46        Self(v)
47    }
48}
49
50impl Mul for PqlVector {
51    type Output = Self;
52    fn mul(self, other: Self) -> Self::Output {
53        let v = self
54            .0
55            .into_iter()
56            .zip(other.0.into_iter())
57            .map(|(a, b)| a * b)
58            .collect::<Vec<PqlValue>>();
59        Self(v)
60    }
61}
62
63impl Div for PqlVector {
64    type Output = Self;
65    fn div(self, other: Self) -> Self::Output {
66        let v = self
67            .0
68            .into_iter()
69            .zip(other.0.into_iter())
70            .map(|(a, b)| a / b)
71            .collect::<Vec<PqlValue>>();
72        Self(v)
73    }
74}
75
76impl Rem for PqlVector {
77    type Output = Self;
78    fn rem(self, other: Self) -> Self::Output {
79        let v = self
80            .0
81            .into_iter()
82            .zip(other.0.into_iter())
83            .map(|(a, b)| a % b)
84            .collect::<Vec<PqlValue>>();
85        Self(v)
86    }
87}