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