Skip to content

Commit

Permalink
expose numeric bitwise on Series
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 22, 2021
1 parent f1c03ee commit 0621721
Show file tree
Hide file tree
Showing 18 changed files with 342 additions and 274 deletions.
1 change: 0 additions & 1 deletion polars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ diff = ["polars-core/diff", "polars-lazy/diff"]
moment = ["polars-core/moment", "polars-lazy/moment"]
arange = ["polars-lazy/arange"]
true_div = ["polars-lazy/true_div"]
series_bitwise = ["polars-core/series_bitwise"]

# don't use this
private = []
Expand Down
1 change: 0 additions & 1 deletion polars/polars-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ list = []
rank = []
diff = []
moment = []
series_bitwise = []


# opt-in datatypes for Series
Expand Down
138 changes: 0 additions & 138 deletions polars/polars-core/src/chunked_array/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,144 +543,6 @@ impl Pow for Utf8Chunked {}
impl Pow for ListChunked {}
impl Pow for CategoricalChunked {}

#[cfg(feature = "series_bitwise")]
mod bitwise {
use super::*;
use crate::utils::{combine_validities, CustomIterTools};
use std::ops::{BitAnd, BitOr, BitXor};

impl<T> BitAnd for &ChunkedArray<T>
where
T: PolarsIntegerType,
T::Native: BitAnd<Output = T::Native>,
{
type Output = ChunkedArray<T>;

fn bitand(self, rhs: Self) -> Self::Output {
let (l, r) = align_chunks_binary(self, rhs);
let chunks = l
.downcast_iter()
.zip(r.downcast_iter())
.map(|(l_arr, r_arr)| {
let l_vals = l_arr.values().as_slice();
let r_vals = l_arr.values().as_slice();
let valididity =
combine_validities(l_arr.validity().as_ref(), r_arr.validity().as_ref());

let av = l_vals
.iter()
.zip(r_vals)
.map(|(l, r)| *l & *r)
.collect_trusted::<AlignedVec<_>>();

let arr =
PrimitiveArray::from_data(T::get_dtype().to_arrow(), av.into(), valididity);
Arc::new(arr) as ArrayRef
})
.collect::<Vec<_>>();

ChunkedArray::new_from_chunks(self.name(), chunks)
}
}

impl<T> BitOr for &ChunkedArray<T>
where
T: PolarsIntegerType,
T::Native: BitOr<Output = T::Native>,
{
type Output = ChunkedArray<T>;

fn bitor(self, rhs: Self) -> Self::Output {
let (l, r) = align_chunks_binary(self, rhs);
let chunks = l
.downcast_iter()
.zip(r.downcast_iter())
.map(|(l_arr, r_arr)| {
let l_vals = l_arr.values().as_slice();
let r_vals = l_arr.values().as_slice();
let valididity =
combine_validities(l_arr.validity().as_ref(), r_arr.validity().as_ref());

let av = l_vals
.iter()
.zip(r_vals)
.map(|(l, r)| *l | *r)
.collect_trusted::<AlignedVec<_>>();

let arr =
PrimitiveArray::from_data(T::get_dtype().to_arrow(), av.into(), valididity);
Arc::new(arr) as ArrayRef
})
.collect::<Vec<_>>();

ChunkedArray::new_from_chunks(self.name(), chunks)
}
}

impl<T> BitXor for &ChunkedArray<T>
where
T: PolarsIntegerType,
T::Native: BitXor<Output = T::Native>,
{
type Output = ChunkedArray<T>;

fn bitxor(self, rhs: Self) -> Self::Output {
let (l, r) = align_chunks_binary(self, rhs);
let chunks = l
.downcast_iter()
.zip(r.downcast_iter())
.map(|(l_arr, r_arr)| {
let l_vals = l_arr.values().as_slice();
let r_vals = l_arr.values().as_slice();
let valididity =
combine_validities(l_arr.validity().as_ref(), r_arr.validity().as_ref());

let av = l_vals
.iter()
.zip(r_vals)
.map(|(l, r)| l.bitxor(*r))
.collect_trusted::<AlignedVec<_>>();

let arr =
PrimitiveArray::from_data(T::get_dtype().to_arrow(), av.into(), valididity);
Arc::new(arr) as ArrayRef
})
.collect::<Vec<_>>();

ChunkedArray::new_from_chunks(self.name(), chunks)
}
}

macro_rules! impl_floats {
($_type:ty) => {
impl BitXor for &$_type {
type Output = $_type;

fn bitxor(self, _rhs: Self) -> Self::Output {
unimplemented!()
}
}
impl BitAnd for &$_type {
type Output = $_type;

fn bitand(self, _rhs: Self) -> Self::Output {
unimplemented!()
}
}
impl BitOr for &$_type {
type Output = $_type;

fn bitor(self, _rhs: Self) -> Self::Output {
unimplemented!()
}
}
};
}

impl_floats!(Float64Chunked);
impl_floats!(Float32Chunked);
}

#[cfg(test)]
pub(crate) mod test {
use crate::prelude::*;
Expand Down

0 comments on commit 0621721

Please sign in to comment.