Skip to content

Commit

Permalink
manual implement series trait and impl bitwise operations
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 21, 2021
1 parent 2f0ddef commit 27f01d7
Show file tree
Hide file tree
Showing 13 changed files with 1,664 additions and 81 deletions.
1 change: 1 addition & 0 deletions polars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ 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: 1 addition & 0 deletions polars/polars-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ list = []
rank = []
diff = []
moment = []
series_bitwise = []


# opt-in datatypes for Series
Expand Down
138 changes: 138 additions & 0 deletions polars/polars-core/src/chunked_array/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,144 @@ 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
8 changes: 0 additions & 8 deletions polars/polars-core/src/chunked_array/ops/rolling_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,14 +401,6 @@ where
}
}

impl ChunkWindow for ListChunked {}
impl ChunkWindow for Utf8Chunked {}
impl ChunkWindow for BooleanChunked {}
#[cfg(feature = "dtype-categorical")]
impl ChunkWindow for CategoricalChunked {}
#[cfg(feature = "object")]
impl<T> ChunkWindow for ObjectChunked<T> {}

impl<T> ChunkRollApply for ChunkedArray<T>
where
T: PolarsNumericType,
Expand Down
14 changes: 0 additions & 14 deletions polars/polars-core/src/chunked_array/ops/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,20 +397,6 @@ impl ChunkSort<CategoricalType> for CategoricalChunked {
}
}

impl ChunkSort<ListType> for ListChunked {
fn sort(&self, _reverse: bool) -> Self {
unimplemented!()
}

fn sort_in_place(&mut self, _reverse: bool) {
unimplemented!()
}

fn argsort(&self, _reverse: bool) -> UInt32Chunked {
unimplemented!()
}
}

#[cfg(feature = "object")]
impl<T> ChunkSort<ObjectType<T>> for ObjectChunked<T> {
fn sort(&self, _reverse: bool) -> Self {
Expand Down
14 changes: 0 additions & 14 deletions polars/polars-core/src/chunked_array/ops/unique/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,6 @@ macro_rules! is_unique_duplicated {
}};
}

impl ChunkUnique<ListType> for ListChunked {
fn unique(&self) -> Result<ChunkedArray<ListType>> {
Err(PolarsError::InvalidOperation(
"unique not supported for list".into(),
))
}

fn arg_unique(&self) -> Result<UInt32Chunked> {
Err(PolarsError::InvalidOperation(
"unique not supported for list".into(),
))
}
}
#[cfg(feature = "object")]
impl<T> ChunkUnique<ObjectType<T>> for ObjectChunked<T> {
fn unique(&self) -> Result<ChunkedArray<ObjectType<T>>> {
Expand Down Expand Up @@ -397,7 +384,6 @@ where
}
}

impl ToDummies<ListType> for ListChunked {}
#[cfg(feature = "object")]
impl<T> ToDummies<ObjectType<T>> for ObjectChunked<T> {}
impl ToDummies<Float32Type> for Float32Chunked {}
Expand Down
3 changes: 0 additions & 3 deletions polars/polars-core/src/series/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ impl NumOpsDispatch for Utf8Chunked {
Ok(out.into_series())
}
}
impl NumOpsDispatch for BooleanChunked {}
impl NumOpsDispatch for ListChunked {}
impl NumOpsDispatch for CategoricalChunked {}

#[cfg(feature = "checked_arithmetic")]
pub mod checked {
Expand Down

0 comments on commit 27f01d7

Please sign in to comment.