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
use super::Tensor;
use crate::tensor::backend::Backend;
use crate::tensor::{Data, Shape};

#[derive(Debug, Clone)]
pub struct BoolTensor<B: Backend, const D: usize> {
    pub(crate) value: B::BoolTensorPrimitive<D>,
}

impl<B, const D: usize> BoolTensor<B, D>
where
    B: Backend,
{
    pub fn new(tensor: B::BoolTensorPrimitive<D>) -> Self {
        Self { value: tensor }
    }

    pub fn shape(&self) -> &Shape<D> {
        B::bool_shape(&self.value)
    }

    pub fn to_device(&self, device: B::Device) -> Self {
        Self::new(B::bool_to_device(&self.value, device))
    }

    /// Returns the dimensions of the current tensor.
    ///
    /// Equivalent to `tensor.shape().dims`.
    pub fn dims(&self) -> [usize; D] {
        self.shape().dims
    }

    pub fn into_data(self) -> Data<bool, D> {
        B::bool_into_data(self.value)
    }

    pub fn to_data(&self) -> Data<bool, D> {
        B::bool_to_data(&self.value)
    }

    pub fn from_data(data: Data<bool, D>) -> Self {
        let value = B::from_data_bool(data, B::Device::default());
        Self::new(value)
    }

    pub fn to_int(&self) -> Tensor<B::IntegerBackend, D> {
        let data = B::bool_to_data(&self.value);
        Tensor::from_data(data.convert())
    }

    pub fn from_int_backend(tensor: BoolTensor<B::IntegerBackend, D>) -> Self {
        Self::new(tensor.value.into())
    }

    /// Reshape the tensor to have the given shape.
    ///
    /// # Panics
    ///
    /// If the tensor can not be reshape to the given shape.
    pub fn reshape<const D2: usize, S: Into<Shape<D2>>>(&self, shape: S) -> BoolTensor<B, D2> {
        BoolTensor::new(B::bool_reshape(&self.value, shape.into()))
    }

    pub fn index<const D2: usize>(&self, indexes: [std::ops::Range<usize>; D2]) -> Self {
        Self::new(B::bool_index(&self.value, indexes))
    }
}