polars_ops/series/ops/
bitwise.rs
1use polars_core::chunked_array::ChunkedArray;
2use polars_core::chunked_array::ops::arity::unary_mut_values;
3use polars_core::prelude::DataType;
4use polars_core::series::Series;
5use polars_core::{with_match_physical_float_polars_type, with_match_physical_integer_polars_type};
6use polars_error::{PolarsResult, polars_bail};
7
8use super::*;
9
10macro_rules! apply_bitwise_op {
11 ($($op:ident),+ $(,)?) => {
12 $(
13 pub fn $op(s: &Series) -> PolarsResult<Series> {
14 match s.dtype() {
15 DataType::Boolean => {
16 let ca: &ChunkedArray<BooleanType> = s.as_any().downcast_ref().unwrap();
17 Ok(unary_mut_values::<BooleanType, UInt32Type, _, _>(
18 ca,
19 |a| polars_compute::bitwise::BitwiseKernel::$op(a),
20 ).into_series())
21 },
22 dt if dt.is_integer() => {
23 with_match_physical_integer_polars_type!(dt, |$T| {
24 let ca: &ChunkedArray<$T> = s.as_any().downcast_ref().unwrap();
25 Ok(unary_mut_values::<$T, UInt32Type, _, _>(
26 ca,
27 |a| polars_compute::bitwise::BitwiseKernel::$op(a),
28 ).into_series())
29 })
30 },
31 dt if dt.is_float() => {
32 with_match_physical_float_polars_type!(dt, |$T| {
33 let ca: &ChunkedArray<$T> = s.as_any().downcast_ref().unwrap();
34 Ok(unary_mut_values::<$T, UInt32Type, _, _>(
35 ca,
36 |a| polars_compute::bitwise::BitwiseKernel::$op(a),
37 ).into_series())
38 })
39 },
40 dt => {
41 polars_bail!(InvalidOperation: "dtype {:?} not supported in '{}' operation", dt, stringify!($op))
42 },
43 }
44 }
45 )+
46
47 };
48}
49
50apply_bitwise_op! {
51 count_ones,
52 count_zeros,
53 leading_ones,
54 leading_zeros,
55 trailing_ones,
56 trailing_zeros,
57}