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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use super::*;

///When we traverse the tree in read-only mode, we can simply return a reference to each node.
///We don't need to protect the user from only mutating parts of the BBox's since they can't
///change anything.
pub type Vistr<'a, N> = compt::dfs_order::Vistr<'a, N, compt::dfs_order::PreOrder>;

mod vistr_mut {
    use crate::inner_prelude::*;

    /// Tree Iterator that returns a protected mutable reference to each node.
    #[repr(transparent)]
    pub struct VistrMut<'a, N> {
        pub(crate) inner: compt::dfs_order::VistrMut<'a, N, compt::dfs_order::PreOrder>,
    }

    impl<'a, N> VistrMut<'a, N> {
        ///It is safe to borrow the iterator and then produce mutable references from that
        ///as long as by the time the borrow ends, all the produced references also go away.
        #[inline(always)]
        pub fn create_wrap_mut(&mut self) -> VistrMut<N> {
            VistrMut {
                inner: self.inner.create_wrap_mut(),
            }
        }

        #[inline(always)]
        pub fn as_slice_mut(&mut self) -> PMut<[N]> {
            PMut::new(self.inner.as_slice_mut())
        }

        #[inline(always)]
        pub fn into_slice(self) -> PMut<'a, [N]> {
            PMut::new(self.inner.into_slice())
        }
    }

    impl<'a, N> core::ops::Deref for VistrMut<'a, N> {
        type Target = Vistr<'a, N>;

        #[inline(always)]
        fn deref(&self) -> &Vistr<'a, N> {
            unsafe { &*(self as *const VistrMut<_> as *const Vistr<_>) }
        }
    }

    unsafe impl<'a, N> compt::FixedDepthVisitor for VistrMut<'a, N> {}

    impl<'a, N> Visitor for VistrMut<'a, N> {
        type Item = PMut<'a, N>;

        #[inline(always)]
        fn next(self) -> (Self::Item, Option<[Self; 2]>) {
            let (nn, rest) = self.inner.next();

            let k = match rest {
                Some([left, right]) => Some([VistrMut { inner: left }, VistrMut { inner: right }]),
                None => None,
            };
            (PMut::new(nn), k)
        }

        #[inline(always)]
        fn level_remaining_hint(&self) -> (usize, Option<usize>) {
            self.inner.level_remaining_hint()
        }

        #[inline(always)]
        fn dfs_preorder(self, mut func: impl FnMut(Self::Item)) {
            self.inner.dfs_preorder(move |a| func(PMut::new(a)));
        }
    }
}
pub use vistr_mut::VistrMut;

///A trait api to hide the lifetime of [`NodeMut`].
///This way query algorithms do not need to worry about this lifetime.
pub trait Node {
    type T: Aabb<Num = Self::Num>;
    type Num: Num;
    fn get(&self) -> NodeRef<Self::T>;
    fn get_mut(&mut self) -> NodeRefMut<Self::T>;
}

impl<'a, T: Aabb> Node for NodeMut<'a, T> {
    type T = T;
    type Num = T::Num;
    fn get(&self) -> NodeRef<Self::T> {
        NodeRef {
            bots: self.range.as_ref(),
            cont: &self.cont,
            div: &self.div,
        }
    }
    fn get_mut(&mut self) -> NodeRefMut<Self::T> {
        NodeRefMut {
            bots: self.range.as_mut(),
            cont: &self.cont,
            div: &self.div,
        }
    }
}

use crate::collections::NodePtr;
impl<'a, T: Aabb> AsRef<NodePtr<T>> for NodeMut<'a, T> {
    fn as_ref(&self) -> &NodePtr<T> {
        unsafe { &*(self as *const _ as *const _) }
    }
}

///A node in [`Tree`].
pub struct NodeMut<'a, T: Aabb> {
    pub(crate) range: PMut<'a, [T]>,
    //range is empty iff cont is none.
    pub(crate) cont: Option<axgeom::Range<T::Num>>,
    //for non leafs:
    //  div is some if either this node as bots or a child does
    //for leafs:
    //  div is none
    pub(crate) div: Option<T::Num>,
}

impl<'a, T: Aabb> NodeMut<'a, T> {
    pub fn get(&self) -> NodeRef<T> {
        NodeRef {
            bots: self.range.as_ref(),
            cont: &self.cont,
            div: &self.div,
        }
    }
    pub fn get_mut(&mut self) -> NodeRefMut<T> {
        NodeRefMut {
            bots: self.range.as_mut(),
            cont: &self.cont,
            div: &self.div,
        }
    }
}

///Mutable reference to a [`Node`].
pub struct NodeRefMut<'a, T: Aabb> {
    ///The bots that belong to this node.
    pub bots: PMut<'a, [T]>,

    ///Is None iff bots is empty.
    pub cont: &'a Option<axgeom::Range<T::Num>>,

    ///Is None if node is a leaf, or there are no bots in this node or in any decendants.
    pub div: &'a Option<T::Num>,
}

///Reference to a [`Node`].
pub struct NodeRef<'a, T: Aabb> {
    ///The bots that belong to this node.
    pub bots: &'a [T],

    ///Is None iff bots is empty.
    pub cont: &'a Option<axgeom::Range<T::Num>>,

    ///Is None if node is a leaf, or there are no bots in this node or in any decendants.
    pub div: &'a Option<T::Num>,
}