orx_concurrent_iter/implementations/jagged_arrays/
index.rs

1use super::AsRawSlice;
2use core::cmp::Ordering;
3
4/// Index of an element in a jagged array.
5#[derive(Default, PartialEq, Debug, Clone)]
6pub struct JaggedIndex {
7    /// Index of the array containing the element.
8    pub f: usize,
9    /// Index of the element within the array containing it.
10    pub i: usize,
11}
12
13impl From<(usize, usize)> for JaggedIndex {
14    fn from((f, i): (usize, usize)) -> Self {
15        Self::new(f, i)
16    }
17}
18
19impl From<[usize; 2]> for JaggedIndex {
20    fn from([f, i]: [usize; 2]) -> Self {
21        Self::new(f, i)
22    }
23}
24
25impl JaggedIndex {
26    /// Creates a new jagged index:
27    ///
28    /// * `f`: index of the array containing the element.
29    /// * `i`: index of the element within the array containing it.
30    pub fn new(f: usize, i: usize) -> Self {
31        Self { f, i }
32    }
33
34    pub(super) fn is_in_exc_bounds_of<T>(&self, slices: &[impl AsRawSlice<T>]) -> bool {
35        match slices.is_empty() {
36            true => self.f == 0 && self.i == 0,
37            false => self.f < slices.len() && self.i <= slices[self.f].length(),
38        }
39    }
40}
41
42impl PartialOrd for JaggedIndex {
43    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
44        match self.f.partial_cmp(&other.f) {
45            Some(Ordering::Equal) => self.i.partial_cmp(&other.i),
46            ord => ord,
47        }
48    }
49}