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
use std::cmp::Ordering;
use std::ops::{Index, IndexMut};

use crate::core::prelude::*;

// ==== Indexing

impl <T: ArrayElement> Index<usize> for Array<T> {
    type Output = T;

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

impl <T: ArrayElement> Index<&[usize]> for Array<T> {
    type Output = T;

    fn index(&self, coords: &[usize]) -> &Self::Output {
        let index = self.index_at(coords).unwrap_or_else(|err| panic!("{err}"));
        &self.elements[index]
    }
}

impl <T: ArrayElement> IndexMut<usize> for Array<T> {

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

// ==== Compare

impl <T: ArrayElement> PartialEq for Array<T> {

    fn eq(&self, other: &Self) -> bool {
        assert_eq!(self.get_shape(), other.get_shape());
        self.elements.iter()
            .zip(&other.elements)
            .all(|(a, b)| a == b)
    }
}

impl <T: ArrayElement> PartialOrd for Array<T> {

    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        assert_eq!(self.get_shape(), other.get_shape());
        self.elements.partial_cmp(&other.elements)
    }

    fn lt(&self, other: &Self) -> bool {
        assert_eq!(self.get_shape(), other.get_shape());
        self.elements.lt(&other.elements)
    }

    fn le(&self, other: &Self) -> bool {
        assert_eq!(self.get_shape(), other.get_shape());
        self.elements.le(&other.elements)
    }

    fn gt(&self, other: &Self) -> bool {
        assert_eq!(self.get_shape(), other.get_shape());
        self.elements.gt(&other.elements)
    }

    fn ge(&self, other: &Self) -> bool {
        assert_eq!(self.get_shape(), other.get_shape());
        self.elements.ge(&other.elements)
    }
}