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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use super::*;



///This is a `Vec<BBox<N,&'a mut T>>` under the hood
///with the added guarentee that all the `&'a mut T`
///point to the same slice.
///
///From this struct a user can create a [`TreeInd`].
pub struct TreeIndBase<'a,N:Num,T>{
    aabbs:Box<[BBox<N,&'a mut T>]>,
    orig:Ptr<[T]>
}
impl<'a,N:Num,T> TreeIndBase<'a,N,T>{

    /// Create a [`TreeIndBase`].
    ///
    /// # Examples
    ///
    ///```
    /// let mut aabbs = [
    ///    broccoli::bbox(broccoli::rect(0isize, 10, 0, 10), 0),
    /// ];
    ///
    /// let mut base=broccoli::container::TreeIndBase::new(&mut aabbs,|a|a.rect); 
    /// let mut tree = base.build();
    /// ```
    pub fn new(bots:&'a mut [T],mut func:impl FnMut(&mut T)->Rect<N>)->TreeIndBase<'a,N,T>{
        let orig = Ptr(bots as *mut _);

        TreeIndBase{
            orig,
            aabbs:bots.iter_mut().map(|a|crate::bbox(func(a),a)).collect::<Vec<_>>().into_boxed_slice()
        }
    }

    /// Extra the internals of a [`TreeIndBase`].
    ///
    /// # Examples
    ///
    ///```
    /// let mut aabbs = [
    ///    broccoli::bbox(broccoli::rect(0isize, 10, 0, 10), 0),
    /// ];
    ///
    /// let mut base=broccoli::container::TreeIndBase::new(&mut aabbs,|a|a.rect); 
    /// let mut inner=base.into_inner();
    /// let mut tree = broccoli::new(&mut inner);
    /// //We can make a tree using the internals, but we lost the guarentee
    /// //that all the `&'a mut T` belong to the same slice.
    /// ```
    pub fn into_inner(self)->Box<[BBox<N,&'a mut T>]>{
        self.aabbs
    }

    /// Build a [`TreeInd`].
    ///
    /// # Examples
    ///
    ///```
    /// let mut aabbs = [
    ///    broccoli::bbox(broccoli::rect(0isize, 10, 0, 10), 0),
    /// ];
    ///
    /// let mut base=broccoli::container::TreeIndBase::new(&mut aabbs,|a|a.rect); 
    /// let mut tree = base.build();
    /// ```
    pub fn build<'b>(&'b mut self)->TreeInd<'a,'b,N,T>{
        let tree=crate::new(&mut self.aabbs);

        TreeInd{
            tree,
            orig:self.orig
        }
    }

    /// Build a [`TreeInd`].
    ///
    /// # Examples
    ///
    ///```
    /// let mut aabbs = [
    ///    broccoli::bbox(broccoli::rect(0isize, 10, 0, 10), 0),
    /// ];
    ///
    /// let mut base=broccoli::container::TreeIndBase::new(&mut aabbs,|a|a.rect); 
    /// let mut tree = base.build_par(broccoli::RayonJoin);
    /// ```
    pub fn build_par<'b>(&'b mut self,joiner:impl crate::Joinable)->TreeInd<'a,'b,N,T> where N:Send+Sync,T:Send+Sync{
        let tree=crate::new_par(joiner,&mut self.aabbs);

        TreeInd{
            tree,
            orig:self.orig
        }
    }

}



/// A less general tree that provides `collect` functions
/// and also derefs to a [`Tree`].
///
/// [`TreeInd`] assumes there is a layer of indirection where
/// all the pointers point to the same slice.
/// It uses this assumption to provide `collect` functions that allow
/// storing query results that can then be iterated through multiple times
/// quickly.
///
#[repr(C)]
pub struct TreeInd<'a,'b,N:Num,T>{
    tree:Tree<'b,BBox<N,&'a mut T>>,
    orig:Ptr<[T]>
}

#[repr(C)]
pub(super) struct TreeIndPtr<N:Num,T>{
    pub(super) tree:TreePtr<BBox<N,Ptr<T>>>,
    pub(super) orig:Ptr<[T]>
}


impl<'a,'b, N:Num,T> From<TreeInd<'a,'b,N,T>> for Tree<'b, BBox<N,&'a mut T>> {
    #[inline(always)]
    fn from(a: TreeInd<'a,'b,N,T>) -> Self {
        a.tree
    }
}


impl<'a,'b, N: Num , T> core::ops::Deref for TreeInd<'a,'b, N, T> {
    type Target = Tree<'b, BBox<N, &'a mut T>>;
    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        &self.tree
    }
}
impl<'a, 'b,N: Num, T> core::ops::DerefMut for TreeInd<'a, 'b,N, T> {
    #[inline(always)]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.tree
    }
}

unsafe impl<'a,'b,N:Num,T> FromSlice<'a,'b> for TreeInd<'a,'b,N,T>{
    type T=BBox<N,&'a mut T>;
    type Inner=T;
    type Num=N;
    fn get_inner_elements(&self)->&[Self::Inner]{
        unsafe{&*self.orig.0}
    }

    fn get_inner_elements_mut(&mut self)->&mut [Self::Inner]{
        unsafe{&mut *self.orig.0}
    }

    fn get_tree_mut(&mut self)->&mut Tree<'b,BBox<N,&'a mut T>>{
        self
    }
}




impl<'a,'b,N:Num,T> TreeInd<'a,'b,N,T>{


    pub(super) fn into_ptr(self)->TreeIndPtr<N,T>{
        
        TreeIndPtr{
            tree:TreePtr{
                _inner:unsafe{self.tree.inner.convert()},
                _num_aabbs:self.tree.num_aabbs
            },
            orig:self.orig
        }
    }
}