polars_core/series/arithmetic/
bitops.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use std::borrow::Cow;

use polars_error::PolarsResult;

use super::{polars_bail, BooleanChunked, ChunkedArray, DataType, IntoSeries, Series};

macro_rules! impl_bitop {
    ($(($trait:ident, $f:ident))+) => {
        $(
        impl std::ops::$trait for &Series {
            type Output = PolarsResult<Series>;
            fn $f(self, rhs: Self) -> Self::Output {
                use DataType as DT;
                match self.dtype() {
                    DT::Boolean => {
                        let lhs: &BooleanChunked = self.as_ref().as_ref().as_ref();
                        let rhs = lhs.unpack_series_matching_type(rhs)?;
                        Ok(lhs.$f(rhs).into_series())
                    },
                    dt if dt.is_integer() => with_match_physical_integer_polars_type!(dt, |$T| {
                        let lhs: &ChunkedArray<$T> = self.as_ref().as_ref().as_ref();

                        let rhs = if rhs.len() == 1 {
                            Cow::Owned(rhs.cast(self.dtype())?)
                        } else {
                            Cow::Borrowed(rhs)
                        };

                        let rhs = lhs.unpack_series_matching_type(&rhs)?;
                        Ok(lhs.$f(&rhs).into_series())
                    }),
                    _ => polars_bail!(opq = $f, self.dtype()),
                }
            }
        }
        impl std::ops::$trait for Series {
            type Output = PolarsResult<Series>;
            #[inline(always)]
            fn $f(self, rhs: Self) -> Self::Output {
                <&Series as std::ops::$trait>::$f(&self, &rhs)
            }
        }
        impl std::ops::$trait<&Series> for Series {
            type Output = PolarsResult<Series>;
            #[inline(always)]
            fn $f(self, rhs: &Series) -> Self::Output {
                <&Series as std::ops::$trait>::$f(&self, rhs)
            }
        }
        impl std::ops::$trait<Series> for &Series {
            type Output = PolarsResult<Series>;
            #[inline(always)]
            fn $f(self, rhs: Series) -> Self::Output {
                <&Series as std::ops::$trait>::$f(self, &rhs)
            }
        }
        )+
    };
}

impl_bitop! {
    (BitAnd, bitand)
    (BitOr, bitor)
    (BitXor, bitxor)
}