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
66
67
68
69
70
71
72
73
74
75
//! Contains bitwise operators: [`or`], [`and`], [`xor`] and [`not`].
use std::ops::{BitAnd, BitOr, BitXor, Not};

use crate::array::PrimitiveArray;
use crate::compute::arity::{binary, unary};
use crate::types::NativeType;

/// Performs `OR` operation on two [`PrimitiveArray`]s.
/// # Panic
/// This function errors when the arrays have different lengths.
pub fn or<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
    T: NativeType + BitOr<Output = T>,
{
    binary(lhs, rhs, lhs.data_type().clone(), |a, b| a | b)
}

/// Performs `XOR` operation between two [`PrimitiveArray`]s.
/// # Panic
/// This function errors when the arrays have different lengths.
pub fn xor<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
    T: NativeType + BitXor<Output = T>,
{
    binary(lhs, rhs, lhs.data_type().clone(), |a, b| a ^ b)
}

/// Performs `AND` operation on two [`PrimitiveArray`]s.
/// # Panic
/// This function panics when the arrays have different lengths.
pub fn and<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
    T: NativeType + BitAnd<Output = T>,
{
    binary(lhs, rhs, lhs.data_type().clone(), |a, b| a & b)
}

/// Returns a new [`PrimitiveArray`] with the bitwise `not`.
pub fn not<T>(array: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
    T: NativeType + Not<Output = T>,
{
    let op = move |a: T| !a;
    unary(array, op, array.data_type().clone())
}

/// Performs `OR` operation between a [`PrimitiveArray`] and scalar.
/// # Panic
/// This function errors when the arrays have different lengths.
pub fn or_scalar<T>(lhs: &PrimitiveArray<T>, rhs: &T) -> PrimitiveArray<T>
where
    T: NativeType + BitOr<Output = T>,
{
    unary(lhs, |a| a | *rhs, lhs.data_type().clone())
}

/// Performs `XOR` operation between a [`PrimitiveArray`] and scalar.
/// # Panic
/// This function errors when the arrays have different lengths.
pub fn xor_scalar<T>(lhs: &PrimitiveArray<T>, rhs: &T) -> PrimitiveArray<T>
where
    T: NativeType + BitXor<Output = T>,
{
    unary(lhs, |a| a ^ *rhs, lhs.data_type().clone())
}

/// Performs `AND` operation between a [`PrimitiveArray`] and scalar.
/// # Panic
/// This function panics when the arrays have different lengths.
pub fn and_scalar<T>(lhs: &PrimitiveArray<T>, rhs: &T) -> PrimitiveArray<T>
where
    T: NativeType + BitAnd<Output = T>,
{
    unary(lhs, |a| a & *rhs, lhs.data_type().clone())
}