concision_core/ops/
fill.rs1use ndarray::prelude::*;
6use ndarray::{DataMut, RawData};
7
8pub trait MaskFill<A, D>
11where
12    D: Dimension,
13{
14    type Output;
15
16    fn masked_fill(&self, mask: &Array<bool, D>, value: A) -> Self::Output;
17}
18
19pub trait IsSquare {
22    fn is_square(&self) -> bool;
23}
24
25impl<A, S, D> MaskFill<A, D> for ArrayBase<S, D>
30where
31    A: Clone,
32    D: Dimension,
33    S: DataMut<Elem = A>,
34    Self: Clone,
35{
36    type Output = ArrayBase<S, D>;
37
38    fn masked_fill(&self, mask: &Array<bool, D>, value: A) -> Self::Output {
39        let mut arr = self.clone();
40        arr.zip_mut_with(mask, |x, &m| {
41            if m {
42                *x = value.clone();
43            }
44        });
45        arr
46    }
47}
48
49impl<S, D> IsSquare for ArrayBase<S, D>
50where
51    D: Dimension,
52    S: RawData,
53{
54    fn is_square(&self) -> bool {
55        let first = self.shape().first().unwrap();
56        self.shape().iter().all(|x| x == first)
57    }
58}