orx_v/impl_nvec/
impl_nvec_from_v1.rs

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
/// Implements `NVec<D1, T>` for a struct which is sort of like a 1-dimensional
/// vector, such as the standard `Vec<T>`.
#[macro_export]
macro_rules! impl_v1 {
    ([$($impl_generics:tt)*], $V:ty, [$($where:tt)*]) => {
        #[allow(unused_imports)]
        use $crate::*;

        impl<$($impl_generics)*> NVec<D1, T> for $V where $($where)* {
            #[inline(always)]
            fn at(&self, idx: impl IntoIdx<D1>) -> T {
                self[idx.into_idx()[0]]
            }

            fn child(&self, _: IdxNever) -> impl NVec<<D1 as Dim>::PrevDim, T> {
                self
            }

            fn all(&self) -> impl Iterator<Item = T> {
                self.iter().copied()
            }
        }
    };
    ($const_arg:tt, [$($impl_generics:tt)*], $V:ty, [$($where:tt)*]) => {
        #[allow(unused_imports)]
        use $crate::*;

        impl<const $const_arg: usize, $($impl_generics)*> NVec<D1, T> for $V where $($where)* {
            #[inline(always)]
            fn at(&self, idx: impl IntoIdx<D1>) -> T {
                self[idx.into_idx()[0]]
            }

            fn child(&self, _: IdxNever) -> impl NVec<<D1 as Dim>::PrevDim, T> {
                self
            }

            fn all(&self) -> impl Iterator<Item = T> {
                self.iter().copied()
            }
        }
    };
}

/// Implements `NVec<D, T>` for a struct which is sort of like a 1-dimensional
/// vector, such as the standard `Vec<T>`, and a dimension `D` which is at least
/// 2-dimensional.
#[macro_export]
macro_rules! impl_vn {
    ($dim:ty, [$($impl_generics:tt)*], $V:ty, [$($where:tt)*]) => {
        #[allow(unused_imports)]
        use $crate::*;

        impl<$($impl_generics)*> NVec<$dim, T> for $V where $($where)* {
            #[inline(always)]
            fn at(&self, idx: impl IntoIdx<$dim>) -> T {
                let (i, c_idx) = idx.into_idx().split_idx();
                self.child(i).at(c_idx)
            }

            fn child(&self, i: usize) -> impl NVec<<$dim as Dim>::PrevDim, T> {
                &self[i]
            }

            fn all(&self) -> impl Iterator<Item = T> {
                self.iter().flat_map(|x| x.all())
            }
        }
    };
    ($dim:ty, $const_arg:tt, [$($impl_generics:tt)*], $V:ty, [$($where:tt)*]) => {
        #[allow(unused_imports)]
        use $crate::*;

        impl<const $const_arg: usize, $($impl_generics)*> NVec<$dim, T> for $V where $($where)* {
            #[inline(always)]
            fn at(&self, idx: impl IntoIdx<$dim>) -> T {
                let (i, c_idx) = idx.into_idx().split_idx();
                self.child(i).at(c_idx)
            }

            fn child(&self, i: usize) -> impl NVec<<$dim as Dim>::PrevDim, T> {
                &self[i]
            }

            fn all(&self) -> impl Iterator<Item = T> {
                self.iter().flat_map(|x| x.all())
            }
        }
    };
}