orx_v/
empty_vec.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
91
92
93
94
95
96
97
98
99
use crate::{CardIdx, Dim, EmptyCard, IntoIdx, NVec, NVecCoreSealed, NVecMut};
use core::fmt::Debug;
use core::marker::PhantomData;

/// An empty vector of dimension `D` with no elements.
#[derive(Clone, Copy)]
pub struct EmptyVec<D: Dim, T> {
    phantom: PhantomData<(D, T)>,
}

impl<D: Dim, T> Default for EmptyVec<D, T> {
    fn default() -> Self {
        Self {
            phantom: Default::default(),
        }
    }
}

impl<D: Dim, T> Debug for EmptyVec<D, T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(
            f,
            "{{ kind: EmptyVec, dim: D{}, values: [] }}",
            D::dimension()
        )
    }
}

impl<D: Dim, T> NVecCoreSealed<D, T> for EmptyVec<D, T> {
    fn core_num_children(&self) -> usize {
        0
    }

    fn core_card(&self, idx: impl Into<<D as Dim>::CardIdx>) -> usize {
        let idx = idx.into();
        match CardIdx::<D>::is_d0(&idx) {
            true => 0,
            false => EmptyCard::<D>::panic_oob(idx),
        }
    }

    #[allow(unreachable_code)]
    fn core_child(&self, i: <D as Dim>::ChildIdx) -> impl NVecCoreSealed<<D as Dim>::PrevDim, T> {
        EmptyCard::<D>::panic_oob(i);
        EmptyVec::<_, T>::default()
    }

    fn core_map<F: FnMut(&T) -> O, O>(&self, idx: impl IntoIdx<D>, _: &mut F) -> O {
        EmptyCard::<D>::panic_oob(idx)
    }

    fn core_is_rectangular(&self) -> bool {
        true
    }
}

impl<D: Dim, T> NVec<D, T> for EmptyVec<D, T> {
    fn at(&self, idx: impl IntoIdx<D>) -> T {
        EmptyCard::<D>::panic_oob(idx)
    }

    #[allow(unreachable_code)]
    fn child(&self, i: <D as Dim>::ChildIdx) -> impl NVec<<D as Dim>::PrevDim, T> {
        EmptyCard::<D>::panic_oob(i);
        EmptyVec::default()
    }

    fn all(&self) -> impl Iterator<Item = T> {
        core::iter::empty()
    }
}

impl<D: Dim, T> NVecMut<D, T> for EmptyVec<D, T> {
    fn at_mut<Idx: IntoIdx<D>>(&mut self, idx: Idx) -> &mut T {
        EmptyCard::<D>::panic_oob(idx)
    }

    fn set<Idx: IntoIdx<D>>(&mut self, idx: Idx, _: T) {
        EmptyCard::<D>::panic_oob(idx)
    }

    #[allow(unreachable_code)]
    fn child_mut(&mut self, i: <D as Dim>::ChildIdx) -> impl NVecMut<<D as Dim>::PrevDim, T> {
        EmptyCard::<D>::panic_oob(i);
        EmptyVec::default()
    }

    fn mut_all<F>(&mut self, _: F)
    where
        F: FnMut(&mut T),
    {
    }

    fn reset_all(&mut self, _: T)
    where
        T: PartialEq + Copy,
    {
    }
}