Thanks to visit codestin.com
Credit goes to docs.rs

ndarray/dimension/
remove_axis.rs

1// Copyright 2014-2016 bluss and ndarray developers.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use crate::{Axis, Dim, Dimension, Ix, Ix0, Ix1};
10
11/// Array shape with a next smaller dimension.
12///
13/// `RemoveAxis` defines a larger-than relation for array shapes:
14/// removing one axis from *Self* gives smaller dimension *Smaller*.
15pub trait RemoveAxis: Dimension
16{
17    /// Remove the specified axis from a dimension.
18    fn remove_axis(&self, axis: Axis) -> Self::Smaller;
19}
20
21impl RemoveAxis for Dim<[Ix; 1]>
22{
23    #[inline]
24    fn remove_axis(&self, axis: Axis) -> Ix0
25    {
26        debug_assert!(axis.index() < self.ndim());
27        Ix0()
28    }
29}
30
31impl RemoveAxis for Dim<[Ix; 2]>
32{
33    #[inline]
34    fn remove_axis(&self, axis: Axis) -> Ix1
35    {
36        let axis = axis.index();
37        debug_assert!(axis < self.ndim());
38        if axis == 0 {
39            Ix1(get!(self, 1))
40        } else {
41            Ix1(get!(self, 0))
42        }
43    }
44}
45
46macro_rules! impl_remove_axis_array(
47    ($($n:expr),*) => (
48    $(
49        impl RemoveAxis for Dim<[Ix; $n]>
50        {
51            #[inline]
52            fn remove_axis(&self, axis: Axis) -> Self::Smaller {
53                debug_assert!(axis.index() < self.ndim());
54                let mut result = Dim([0; $n - 1]);
55                {
56                    let src = self.slice();
57                    let dst = result.slice_mut();
58                    dst[..axis.index()].copy_from_slice(&src[..axis.index()]);
59                    dst[axis.index()..].copy_from_slice(&src[axis.index() + 1..]);
60                }
61                result
62            }
63        }
64    )*
65    );
66);
67
68impl_remove_axis_array!(3, 4, 5, 6);