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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
   Appellation: shape <mod>
   Contrib: FL03 <jo3mccain@icloud.com>
*/
use super::{Axis, Rank, ShapeError, Stride};
use crate::prelude::{SwapAxes, TensorResult};
#[cfg(not(feature = "std"))]
use alloc::vec;
use core::ops::{self, Deref};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "std")]
use std::vec;
/// A shape is a description of the number of elements in each dimension.
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize,))]
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Shape(Vec<usize>);

impl Shape {
    pub fn new(shape: Vec<usize>) -> Self {
        Self(shape)
    }
    /// Creates a new shape of rank 0.
    pub fn scalar() -> Self {
        Self(Vec::new())
    }

    pub fn with_capacity(capacity: usize) -> Self {
        Self(Vec::with_capacity(capacity))
    }
    /// Creates a new shape of the given rank with all dimensions set to 0.
    pub fn zeros(rank: usize) -> Self {
        Self(vec![0; rank])
    }
    /// Get a reference to the shape as a slice.
    pub fn as_slice(&self) -> &[usize] {
        &self.0
    }
    /// Get a mutable reference to the shape as a slice.
    pub fn as_slice_mut(&mut self) -> &mut [usize] {
        &mut self.0
    }

    pub fn check_size(&self) -> Result<usize, ShapeError> {
        let size_nonzero = self
            .as_slice()
            .iter()
            .filter(|&&d| d != 0)
            .try_fold(1usize, |acc, &d| acc.checked_mul(d))
            .ok_or_else(|| ShapeError::Overflow)?;
        if size_nonzero > ::std::isize::MAX as usize {
            Err(ShapeError::Overflow)
        } else {
            Ok(self.size())
        }
    }
    /// Attempts to create a one-dimensional shape that describes the
    /// diagonal of the current shape.
    pub fn diag(&self) -> Shape {
        Self::new(i![self.nrows()])
    }
    pub fn get_final_position(&self) -> Vec<usize> {
        self.iter().map(|&dim| dim - 1).collect()
    }
    /// Inserts a new dimension along the given [Axis].
    pub fn insert(&mut self, index: Axis, dim: usize) {
        self.0.insert(*index, dim)
    }
    /// Inserts a new dimension along the given [Axis].
    pub fn insert_axis(&self, index: Axis) -> Self {
        let mut shape = self.clone();
        shape.insert(index, 1);
        shape
    }
    /// Returns true if the shape is empty.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
    /// Returns true if the shape is a scalar.
    pub fn is_scalar(&self) -> bool {
        self.is_empty()
    }
    /// Checks to see if the shape is square
    pub fn is_square(&self) -> bool {
        self.iter().all(|&dim| dim == self[0])
    }
    /// Returns true if the strides are C contiguous (aka row major).
    pub fn is_contiguous(&self, stride: &Stride) -> bool {
        if self.0.len() != stride.len() {
            return false;
        }
        let mut acc = 1;
        for (&stride, &dim) in stride.iter().zip(self.iter()).rev() {
            if stride != acc {
                return false;
            }
            acc *= dim;
        }
        true
    }
    /// The number of columns in the shape.
    pub fn ncols(&self) -> usize {
        if self.len() >= 2 {
            self[1]
        } else if self.len() == 1 {
            1
        } else {
            0
        }
    }
    /// The number of rows in the shape.
    pub fn nrows(&self) -> usize {
        if self.len() >= 1 {
            self[0]
        } else {
            0
        }
    }
    /// Removes and returns the last dimension of the shape.
    pub fn pop(&mut self) -> Option<usize> {
        self.0.pop()
    }
    /// Add a new dimension to the shape.
    pub fn push(&mut self, dim: usize) {
        self.0.push(dim)
    }
    /// Get the number of dimensions, or [Rank], of the shape
    pub fn rank(&self) -> Rank {
        self.0.len().into()
    }
    /// Remove the dimension at the given [Axis],
    pub fn remove(&mut self, index: Axis) -> usize {
        self.0.remove(*index)
    }
    /// Remove the dimension at the given [Axis].
    pub fn remove_axis(&self, index: Axis) -> Shape {
        let mut shape = self.clone();
        shape.remove(index);
        shape
    }
    /// Reverse the dimensions of the shape.
    pub fn reverse(&mut self) {
        self.0.reverse()
    }
    /// Set the dimension at the given [Axis].
    pub fn set(&mut self, index: Axis, dim: usize) {
        self[index] = dim
    }
    /// The number of elements in the shape.
    pub fn size(&self) -> usize {
        self.0.iter().product()
    }

    /// Swap the dimensions of the current [Shape] at the given [Axis].
    pub fn swap(&mut self, a: Axis, b: Axis) {
        self.0.swap(a.axis(), b.axis())
    }
    /// Swap the dimensions at the given [Axis], creating a new [Shape]
    pub fn swap_axes(&self, swap: Axis, with: Axis) -> Self {
        let mut shape = self.clone();
        shape.swap(swap, with);
        shape
    }
}

// Internal methods
#[allow(dead_code)]
#[doc(hidden)]
impl Shape {
    pub(crate) fn default_strides(&self) -> Stride {
        // Compute default array strides
        // Shape (a, b, c) => Give strides (b * c, c, 1)
        let mut strides = Stride::zeros(self.rank());
        // For empty arrays, use all zero strides.
        if self.iter().all(|&d| d != 0) {
            let mut it = strides.as_slice_mut().iter_mut().rev();
            // Set first element to 1
            if let Some(rs) = it.next() {
                *rs = 1;
            }
            let mut cum_prod = 1;
            for (rs, dim) in it.zip(self.iter().rev()) {
                cum_prod *= *dim;
                *rs = cum_prod;
            }
        }
        strides
    }

    pub(crate) fn matmul_shape(&self, other: &Self) -> TensorResult<Self> {
        if *self.rank() != 2 || *other.rank() != 2 || self[1] != other[0] {
            return Err(ShapeError::IncompatibleShapes.into());
        }
        Ok(Self::from((self[0], other[1])))
    }

    pub(crate) fn stride_contiguous(&self) -> Stride {
        let mut stride: Vec<_> = self
            .0
            .iter()
            .rev()
            .scan(1, |prod, u| {
                let prod_pre_mult = *prod;
                *prod *= u;
                Some(prod_pre_mult)
            })
            .collect();
        stride.reverse();
        stride.into()
    }

    pub(crate) fn upcast(&self, to: &Shape, stride: &Stride) -> Option<Stride> {
        let mut new_stride = to.as_slice().to_vec();
        // begin at the back (the least significant dimension)
        // size of the axis has to either agree or `from` has to be 1
        if to.rank() < self.rank() {
            return None;
        }

        let mut iter = new_stride.as_mut_slice().iter_mut().rev();
        for ((er, es), dr) in self
            .as_slice()
            .iter()
            .rev()
            .zip(stride.as_slice().iter().rev())
            .zip(iter.by_ref())
        {
            /* update strides */
            if *dr == *er {
                /* keep stride */
                *dr = *es;
            } else if *er == 1 {
                /* dead dimension, zero stride */
                *dr = 0
            } else {
                return None;
            }
        }

        /* set remaining strides to zero */
        for dr in iter {
            *dr = 0;
        }

        Some(new_stride.into())
    }
}

impl AsRef<[usize]> for Shape {
    fn as_ref(&self) -> &[usize] {
        &self.0
    }
}

impl AsMut<[usize]> for Shape {
    fn as_mut(&mut self) -> &mut [usize] {
        &mut self.0
    }
}

impl Deref for Shape {
    type Target = [usize];

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Extend<usize> for Shape {
    fn extend<I: IntoIterator<Item = usize>>(&mut self, iter: I) {
        self.0.extend(iter)
    }
}

impl SwapAxes for Shape {
    fn swap_axes(&self, a: Axis, b: Axis) -> Self {
        self.swap_axes(a, b)
    }
}

impl FromIterator<usize> for Shape {
    fn from_iter<I: IntoIterator<Item = usize>>(iter: I) -> Self {
        Self(Vec::from_iter(iter))
    }
}

impl IntoIterator for Shape {
    type Item = usize;
    type IntoIter = vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl<'a> IntoIterator for &'a mut Shape {
    type Item = &'a mut usize;
    type IntoIter = core::slice::IterMut<'a, usize>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.iter_mut()
    }
}

impl ops::Index<usize> for Shape {
    type Output = usize;

    fn index(&self, index: usize) -> &Self::Output {
        &self.0[index]
    }
}

impl ops::Index<Axis> for Shape {
    type Output = usize;

    fn index(&self, index: Axis) -> &Self::Output {
        &self.0[*index]
    }
}

impl ops::IndexMut<usize> for Shape {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.0[index]
    }
}

impl ops::IndexMut<Axis> for Shape {
    fn index_mut(&mut self, index: Axis) -> &mut Self::Output {
        &mut self.0[*index]
    }
}

impl ops::Index<ops::Range<usize>> for Shape {
    type Output = [usize];

    fn index(&self, index: ops::Range<usize>) -> &Self::Output {
        &self.0[index]
    }
}

impl ops::Index<ops::RangeTo<usize>> for Shape {
    type Output = [usize];

    fn index(&self, index: ops::RangeTo<usize>) -> &Self::Output {
        &self.0[index]
    }
}

impl ops::Index<ops::RangeFrom<usize>> for Shape {
    type Output = [usize];

    fn index(&self, index: ops::RangeFrom<usize>) -> &Self::Output {
        &self.0[index]
    }
}

impl ops::Index<ops::RangeFull> for Shape {
    type Output = [usize];

    fn index(&self, index: ops::RangeFull) -> &Self::Output {
        &self.0[index]
    }
}

impl ops::Index<ops::RangeInclusive<usize>> for Shape {
    type Output = [usize];

    fn index(&self, index: ops::RangeInclusive<usize>) -> &Self::Output {
        &self.0[index]
    }
}

impl ops::Index<ops::RangeToInclusive<usize>> for Shape {
    type Output = [usize];

    fn index(&self, index: ops::RangeToInclusive<usize>) -> &Self::Output {
        &self.0[index]
    }
}

unsafe impl Send for Shape {}

unsafe impl Sync for Shape {}

impl From<()> for Shape {
    fn from(_: ()) -> Self {
        Self::default()
    }
}

impl From<usize> for Shape {
    fn from(dim: usize) -> Self {
        Self(vec![dim])
    }
}

impl From<Vec<usize>> for Shape {
    fn from(shape: Vec<usize>) -> Self {
        Self(shape)
    }
}

impl From<&[usize]> for Shape {
    fn from(shape: &[usize]) -> Self {
        Self(shape.to_vec())
    }
}

impl From<(usize,)> for Shape {
    fn from(shape: (usize,)) -> Self {
        Self(vec![shape.0])
    }
}

impl From<(usize, usize)> for Shape {
    fn from(shape: (usize, usize)) -> Self {
        Self(vec![shape.0, shape.1])
    }
}

impl From<(usize, usize, usize)> for Shape {
    fn from(shape: (usize, usize, usize)) -> Self {
        Self(vec![shape.0, shape.1, shape.2])
    }
}

impl From<(usize, usize, usize, usize)> for Shape {
    fn from(shape: (usize, usize, usize, usize)) -> Self {
        Self(vec![shape.0, shape.1, shape.2, shape.3])
    }
}

impl From<(usize, usize, usize, usize, usize)> for Shape {
    fn from(shape: (usize, usize, usize, usize, usize)) -> Self {
        Self(vec![shape.0, shape.1, shape.2, shape.3, shape.4])
    }
}

impl From<(usize, usize, usize, usize, usize, usize)> for Shape {
    fn from(shape: (usize, usize, usize, usize, usize, usize)) -> Self {
        Self(vec![shape.0, shape.1, shape.2, shape.3, shape.4, shape.5])
    }
}

// macro_rules! tuple_vec {
//     ($($n:tt),*) => {
//         vec![$($n,)*]
//     };

// }

// macro_rules! impl_from_tuple {
//     ($($n:tt: $name:ident),+) => {
//         impl<$($name),+> From<($($name,)+)> for Shape
//         where
//             $($name: Into<usize>,)+
//         {
//             fn from(shape: ($($name,)+)) -> Self {
//                 Self(vec![$($name.into(),)+])
//             }
//         }
//     };
// }

// impl_from_tuple!(A: A);