arr_rs/core/operations/
ops.rs

1use std::cmp::Ordering;
2use std::ops::{Index, IndexMut};
3
4use crate::core::prelude::*;
5
6// ==== Indexing
7
8impl <T: ArrayElement> Index<usize> for Array<T> {
9    type Output = T;
10
11    fn index(&self, index: usize) -> &Self::Output {
12        &self.elements[index]
13    }
14}
15
16impl <T: ArrayElement> Index<&[usize]> for Array<T> {
17    type Output = T;
18
19    fn index(&self, coords: &[usize]) -> &Self::Output {
20        let index = self.index_at(coords).unwrap_or_else(|err| panic!("{err}"));
21        &self.elements[index]
22    }
23}
24
25impl <T: ArrayElement> IndexMut<usize> for Array<T> {
26
27    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
28        &mut self.elements[index]
29    }
30}
31
32// ==== Compare
33
34impl <T: ArrayElement> PartialEq for Array<T> {
35
36    fn eq(&self, other: &Self) -> bool {
37        assert_eq!(self.get_shape(), other.get_shape());
38        self.elements.iter()
39            .zip(&other.elements)
40            .all(|(a, b)| a == b)
41    }
42}
43
44impl <T: ArrayElement> PartialOrd for Array<T> {
45
46    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
47        assert_eq!(self.get_shape(), other.get_shape());
48        self.elements.partial_cmp(&other.elements)
49    }
50
51    fn lt(&self, other: &Self) -> bool {
52        assert_eq!(self.get_shape(), other.get_shape());
53        self.elements.lt(&other.elements)
54    }
55
56    fn le(&self, other: &Self) -> bool {
57        assert_eq!(self.get_shape(), other.get_shape());
58        self.elements.le(&other.elements)
59    }
60
61    fn gt(&self, other: &Self) -> bool {
62        assert_eq!(self.get_shape(), other.get_shape());
63        self.elements.gt(&other.elements)
64    }
65
66    fn ge(&self, other: &Self) -> bool {
67        assert_eq!(self.get_shape(), other.get_shape());
68        self.elements.ge(&other.elements)
69    }
70}