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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use crate::idtree;
use crate::storage::{Storage, HierarchicalOneToNStorage, IntoIter, IntoIterMut};
use densevec::DenseVec;
use crate::sync::{ReadGuardRef, WriteGuardRef};
use crate::Component;

use std::slice;
use std::mem;
use std::fmt::{self, Debug};
use crate::utils::*;

pub struct OneToNForest<T>{
    arena: idtree::Arena<T>,
    entities_roots: DenseVec<Vec<idtree::NodeId>>,
    reverse_index: DenseVec<idtree::NodeId>,
}

impl<'a, T: 'a + Component> Storage<'a, T> for OneToNForest<T>{
    type Get = RootsIter<'a, T>;
    type GetMut = RootsIterMut<'a, T>;
    type DerefTarget = RootsIter<'a, T>;
    type IdedIter = RawIdedIter<'a, T>;
    type Iter = RawIter<'a, T>;
    type IterMut = RawIterMut<'a, T>;

    fn new() -> OneToNForest<T>{
        OneToNForest{
            arena: idtree::Arena::new(),
            entities_roots: DenseVec::new(),
            reverse_index: DenseVec::new(),
        }
    }

    fn with_capacity(capacity: usize) -> OneToNForest<T>{
        OneToNForest{
            arena: idtree::Arena::with_capacity(capacity),
            entities_roots: DenseVec::with_capacity(capacity),
            reverse_index: DenseVec::with_capacity(capacity),
        }
    }

    fn insert(&mut self, guid: usize, t: T){
        unsafe{ self.insert_root(guid, t) };
    }

    fn remove(&mut self, guid: usize){
        for id in unsafe{ self.entities_roots.get_unchecked(guid).iter() }{
            self.arena.remove_tree(*id);
        }
        self.entities_roots.remove(guid);
    }

    unsafe fn get(&self, guid: usize) -> RootsIter<'a,T>{
        RootsIter{
            iter: delete_lifetime(self.entities_roots.get_unchecked(guid)).iter(),
            arena: delete_lifetime(&self.arena)
        }
    }

    unsafe fn get_mut(&mut self, guid: usize) -> RootsIterMut<'a,T>{
        RootsIterMut{
            iter: delete_lifetime(self.entities_roots.get_unchecked(guid)).iter(),
            arena: delete_lifetime_mut(&mut self.arena)
        }
    }

    unsafe fn fast_get(&self, guid: usize) -> RootsIter<'a,T>{
        RootsIter{
            iter: delete_lifetime(self.entities_roots.get_fast_unchecked(guid)).iter(),
            arena: delete_lifetime(&self.arena)
        }
    }

    unsafe fn fast_get_mut(&mut self, guid: usize) -> RootsIterMut<'a,T>{
        RootsIterMut{
            iter: delete_lifetime(self.entities_roots.get_fast_unchecked(guid)).iter(),
            arena: delete_lifetime_mut(&mut self.arena)
        }
    }

    fn fast_index(&self, guid: usize) -> usize{
        self.entities_roots.fast_index(guid)
    }

    fn guid_from_fast_index(&self, idx: usize) -> usize{
        unsafe{ self.entities_roots.unchecked_guid_from_fast_index(idx) }
    }

    fn contains(&self, guid: usize) -> bool{
        self.reverse_index.contains(guid)
    }

    fn ided_iter(&self) -> Self::IdedIter {
        unsafe{
            RawIdedIter{
                arena: delete_lifetime(&self.arena),
                iter: mem::transmute(self.entities_roots.iter())
            }
        }
    }

    fn iter(&self) -> Self::Iter {
        unsafe{
            RawIter{
                arena: delete_lifetime(&self.arena),
                iter: mem::transmute(self.entities_roots.values())
            }
        }
    }

    fn iter_mut(&mut self) -> Self::IterMut {
        unsafe{
            RawIterMut{
                arena: delete_lifetime_mut(&mut self.arena),
                iter: mem::transmute(self.entities_roots.values())
            }
        }
    }
}

pub struct RawIdedIter<'a, T: 'a>{
    arena: &'a idtree::Arena<T>,
    iter: densevec::Iter<'a, usize, Vec<idtree::NodeId>>,
}

impl<'a, T: 'a> Iterator for RawIdedIter<'a, T>{
    type Item = (usize, RootsIter<'a, T>);
    fn next(&mut self) -> Option<(usize, RootsIter<'a, T>)>{
        unsafe{
            self.iter.next().map(|(id, roots)| (id, RootsIter{
                iter: roots.iter(),
                arena: delete_lifetime(self.arena),
            }))
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}

impl<'a, T: 'a> ExactSizeIterator for RawIdedIter<'a, T>{
    #[inline]
    fn len(&self) -> usize {
        self.iter.len()
    }
}

pub struct RawIter<'a, T: 'a>{
    arena: &'a idtree::Arena<T>,
    iter: slice::Iter<'a, Vec<idtree::NodeId>>,
}

impl<'a, T: 'a> Iterator for RawIter<'a, T>{
    type Item = RootsIter<'a, T>;
    fn next(&mut self) -> Option<RootsIter<'a, T>>{
        unsafe{
            self.iter.next().map(|roots| RootsIter{
                iter: roots.iter(),
                arena: delete_lifetime(self.arena),
            })
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}

impl<'a, T: 'a> ExactSizeIterator for RawIter<'a, T>{
    #[inline]
    fn len(&self) -> usize {
        self.iter.len()
    }
}

pub struct RawIterMut<'a, T: 'a>{
    arena: &'a mut idtree::Arena<T>,
    iter: slice::Iter<'a, Vec<idtree::NodeId>>,
}

impl<'a, T: 'a> Iterator for RawIterMut<'a, T>{
    type Item = RootsIterMut<'a, T>;
    fn next(&mut self) -> Option<RootsIterMut<'a, T>>{
        unsafe{
            self.iter.next().map(|roots| RootsIterMut{
                iter: roots.iter(),
                arena: delete_lifetime_mut(self.arena),
            })
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}

impl<'a, T: 'a> ExactSizeIterator for RawIterMut<'a, T>{
    #[inline]
    fn len(&self) -> usize {
        self.iter.len()
    }
}

impl<'a, T: 'a + Component> HierarchicalOneToNStorage<'a,T> for OneToNForest<T>{
    unsafe fn insert_root(&mut self, guid: usize, t: T) -> idtree::NodeRefMut<T>{
        let root = self.arena.new_node(t);
        self.entities_roots.entry(guid)
            .or_insert_with(|| vec![])
            .push(root.id());

        //TODO: is it enough to insert in the reverse index here? or also with childs
        self.reverse_index.insert(guid, root.id());
        root
    }

    unsafe fn insert_child(&mut self, parent: idtree::NodeId, t: T) -> idtree::NodeRefMut<T>{
        parent.append_new(t, &mut self.arena)
    }
}


pub struct RootsIter<'a, T: 'a>{
    iter: slice::Iter<'a, idtree::NodeId>,
    arena: &'a idtree::Arena<T>,
}

impl<'a, T: 'a + Component> Debug for RootsIter<'a,T>{
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result{
        fmt.write_str("Roots iter for Unknown component")
    }
}

impl<'a, T: 'a> Iterator for RootsIter<'a, T>{
    type Item = idtree::NodeRef<'a,T>;

    fn next(&mut self) -> Option<idtree::NodeRef<'a, T>>{
        self.iter.next().map(|id| self.arena.get(*id))
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}

impl<'a, T: 'a> ExactSizeIterator for RootsIter<'a, T>{
    #[inline]
    fn len(&self) -> usize {
        self.iter.len()
    }
}

pub struct RootsIterMut<'a, T: 'a>{
    iter: slice::Iter<'a, idtree::NodeId>,
    arena: &'a mut idtree::Arena<T>,
}

impl<'a, T: 'a> Iterator for RootsIterMut<'a, T>{
    type Item = idtree::NodeRefMut<'a,T>;

    fn next(&mut self) -> Option<idtree::NodeRefMut<'a, T>>{
        self.iter.next().map(|id| unsafe{ mem::transmute::<idtree::NodeRefMut<T>, idtree::NodeRefMut<T>>(self.arena.get_mut(*id)) } )
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}

impl<'a, T: 'a> ExactSizeIterator for RootsIterMut<'a, T>{
    #[inline]
    fn len(&self) -> usize {
        self.iter.len()
    }
}

pub struct Iter<'a, T: 'a>{
    storage: ReadGuardRef<'a, OneToNForest<T>>,
    iter: densevec::Values<'a, Vec<idtree::NodeId>>,
}

impl<'a, T: 'a> Iterator for Iter<'a, T>{
    type Item = RootsIter<'a, T>;
    fn next(&mut self) -> Option<RootsIter<'a, T>>{
        self.iter.next().map(|roots| RootsIter{
            iter: roots.iter(),
            arena: unsafe{ mem::transmute::<&idtree::Arena<T>, &idtree::Arena<T>>(&self.storage.arena) },
        })
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}

impl<'a, T: 'a> ExactSizeIterator for Iter<'a, T>{
    #[inline]
    fn len(&self) -> usize {
        self.iter.len()
    }
}

impl<'a, T> IntoIter for ReadGuardRef<'a, OneToNForest<T>>{
    type Iter = Iter<'a, T>;
    fn into_iter(self) -> Iter<'a, T>{
        Iter{
            iter: unsafe{ mem::transmute::<densevec::Values<Vec<idtree::NodeId>>, densevec::Values<Vec<idtree::NodeId>>>(self.entities_roots.values()) },
            storage: self,
        }
    }
}

pub struct IterMut<'a, T: 'a>{
    storage: WriteGuardRef<'a, OneToNForest<T>>,
    iter: densevec::Values<'a, Vec<idtree::NodeId>>,
}

impl<'a, T: 'a> Iterator for IterMut<'a, T>{
    type Item = RootsIterMut<'a, T>;
    fn next(&mut self) -> Option<RootsIterMut<'a, T>>{
        self.iter.next().map(|roots| RootsIterMut{
            iter: roots.iter(),
            arena: unsafe{ mem::transmute::<&mut idtree::Arena<T>, &mut idtree::Arena<T>>(&mut self.storage.arena) },
        })
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}

impl<'a, T: 'a> ExactSizeIterator for IterMut<'a, T>{
    #[inline]
    fn len(&self) -> usize {
        self.iter.len()
    }
}

impl<'a, T> IntoIterMut for WriteGuardRef<'a, OneToNForest<T>>{
    type IterMut = IterMut<'a, T>;
    fn into_iter_mut(self) -> IterMut<'a, T>{
        IterMut{
            iter: unsafe{ mem::transmute::<densevec::Values<Vec<idtree::NodeId>>, densevec::Values<Vec<idtree::NodeId>>>(self.entities_roots.values()) },
            storage: self,
        }
    }
}