arr_rs/boolean/operations/
ops.rs1use std::ops::{
2 BitAnd, BitAndAssign,
3 BitOr, BitOrAssign,
4 BitXor, BitXorAssign,
5 Not,
6};
7
8use crate::{
9 boolean::prelude::*,
10 core::prelude::*,
11 numeric::prelude::*,
12};
13
14impl <N: BoolNumeric + From<<N as Not>::Output>> Not for Array<N> {
15 type Output = Self;
16
17 fn not(self) -> Self::Output {
18 let elements: Vec<N> = self.elements.into_iter()
19 .map(|x| (!x).into())
20 .collect();
21
22 Self::new(elements, self.shape).unwrap()
23 }
24}
25
26macro_rules! impl_bitwise_ops {
27 ($op_trait: ident, $op_func: ident, $op_assign_trait: ident, $op_assign_func: ident) => {
28 impl<N: Numeric + $op_trait<Output = N>> $op_trait<Array<N>> for Array<N> {
29 type Output = Array<N>;
30
31 fn $op_func(self, other: Array<N>) -> Self::Output {
32 assert_eq!(self.get_shape(), other.get_shape());
33
34 let elements = self.elements.into_iter()
35 .zip(other.elements.into_iter())
36 .map(|(a, b)| a.$op_func(b))
37 .collect();
38 Array { elements,shape: self.shape, }
39 }
40 }
41
42 impl<N: Numeric + $op_trait<Output = N>> $op_trait<N> for Array<N> {
43 type Output = Array<N>;
44
45 fn $op_func(self, other: N) -> Self::Output {
46 let elements = self.elements.into_iter()
47 .map(|a| a.$op_func(other))
48 .collect();
49 Array { elements,shape: self.shape, }
50 }
51 }
52
53 impl<N: Numeric + $op_trait<Output = N>> $op_assign_trait<Array<N>> for Array<N> {
54 fn $op_assign_func(&mut self, other: Array<N>) {
55 assert_eq!(self.get_shape(), other.get_shape());
56
57 self.elements.iter_mut()
58 .zip(other.elements.into_iter())
59 .for_each(|(a, b)| *a = a.$op_func(b));
60 }
61 }
62
63 impl<N: Numeric + $op_trait<Output = N>> $op_assign_trait<N> for Array<N> {
64 fn $op_assign_func(&mut self, other: N) {
65 self.elements.iter_mut()
66 .for_each(|a| *a = a.$op_func(other));
67 }
68 }
69 };
70}
71
72impl_bitwise_ops!(BitAnd, bitand, BitAndAssign, bitand_assign);
73impl_bitwise_ops!(BitOr, bitor, BitOrAssign, bitor_assign);
74impl_bitwise_ops!(BitXor, bitxor, BitXorAssign, bitxor_assign);