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
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2016 bluss and ndarray developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use itertools::zip;
use std::fmt;

use super::Dimension;
use super::IntoDimension;
use crate::Ix;

/// Dimension description.
///
/// `Dim` describes the number of axes and the length of each axis
/// in an array. It is also used as an index type.
///
/// See also the [`Dimension` trait](Dimension.t.html) for its methods and
/// operations.
///
/// # Examples
///
/// To create an array with a particular dimension, you'd just pass
/// a tuple (in this example (3, 2) is used), which is converted to
/// `Dim` by the array constructor.
///
/// ```
/// use ndarray::Array2;
/// use ndarray::Dim;
///
/// let mut array = Array2::zeros((3, 2));
/// array[[0, 0]] = 1.;
/// assert_eq!(array.raw_dim(), Dim([3, 2]));
/// ```
#[derive(Copy, Clone, PartialEq, Eq, Hash, Default)]
pub struct Dim<I: ?Sized> {
    index: I,
}

impl<I> Dim<I> {
    /// Private constructor and accessors for Dim
    pub(crate) fn new(index: I) -> Dim<I> {
        Dim { index }
    }
    #[inline(always)]
    pub(crate) fn ix(&self) -> &I {
        &self.index
    }
    #[inline(always)]
    pub(crate) fn ixm(&mut self) -> &mut I {
        &mut self.index
    }
}

/// Create a new dimension value.
#[allow(non_snake_case)]
pub fn Dim<T>(index: T) -> T::Dim
where
    T: IntoDimension,
{
    index.into_dimension()
}

impl<I: ?Sized> PartialEq<I> for Dim<I>
where
    I: PartialEq,
{
    fn eq(&self, rhs: &I) -> bool {
        self.index == *rhs
    }
}

impl<I> fmt::Debug for Dim<I>
where
    I: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self.index)
    }
}

use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};

macro_rules! impl_op {
    ($op:ident, $op_m:ident, $opassign:ident, $opassign_m:ident, $expr:ident) => {
        impl<I> $op for Dim<I>
        where
            Dim<I>: Dimension,
        {
            type Output = Self;
            fn $op_m(mut self, rhs: Self) -> Self {
                $expr!(self, &rhs);
                self
            }
        }

        impl<I> $opassign for Dim<I>
        where
            Dim<I>: Dimension,
        {
            fn $opassign_m(&mut self, rhs: Self) {
                $expr!(*self, &rhs);
            }
        }

        impl<'a, I> $opassign<&'a Dim<I>> for Dim<I>
        where
            Dim<I>: Dimension,
        {
            fn $opassign_m(&mut self, rhs: &Self) {
                for (x, &y) in zip(self.slice_mut(), rhs.slice()) {
                    $expr!(*x, y);
                }
            }
        }
    };
}

macro_rules! impl_single_op {
    ($op:ident, $op_m:ident, $opassign:ident, $opassign_m:ident, $expr:ident) => {
        impl $op<Ix> for Dim<[Ix; 1]> {
            type Output = Self;
            #[inline]
            fn $op_m(mut self, rhs: Ix) -> Self {
                $expr!(self, rhs);
                self
            }
        }

        impl $opassign<Ix> for Dim<[Ix; 1]> {
            #[inline]
            fn $opassign_m(&mut self, rhs: Ix) {
                $expr!((*self)[0], rhs);
            }
        }
    };
}

macro_rules! impl_scalar_op {
    ($op:ident, $op_m:ident, $opassign:ident, $opassign_m:ident, $expr:ident) => {
        impl<I> $op<Ix> for Dim<I>
        where
            Dim<I>: Dimension,
        {
            type Output = Self;
            fn $op_m(mut self, rhs: Ix) -> Self {
                $expr!(self, rhs);
                self
            }
        }

        impl<I> $opassign<Ix> for Dim<I>
        where
            Dim<I>: Dimension,
        {
            fn $opassign_m(&mut self, rhs: Ix) {
                for x in self.slice_mut() {
                    $expr!(*x, rhs);
                }
            }
        }
    };
}

macro_rules! add {
    ($x:expr, $y:expr) => {
        $x += $y;
    };
}
macro_rules! sub {
    ($x:expr, $y:expr) => {
        $x -= $y;
    };
}
macro_rules! mul {
    ($x:expr, $y:expr) => {
        $x *= $y;
    };
}
impl_op!(Add, add, AddAssign, add_assign, add);
impl_single_op!(Add, add, AddAssign, add_assign, add);
impl_op!(Sub, sub, SubAssign, sub_assign, sub);
impl_single_op!(Sub, sub, SubAssign, sub_assign, sub);
impl_op!(Mul, mul, MulAssign, mul_assign, mul);
impl_scalar_op!(Mul, mul, MulAssign, mul_assign, mul);