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
use crate::storage::{
    Storage, HierarchicalStorage, OneToNStorage, SliceView, SliceViewMut,
    IntoIter, IntoIterMut,
    IntoIdedIterMut,
    IntoOrderedIter, IntoOrderedIterMut,
    IntoOrderedIdedIterMut,
    ChangedStorageExt,
    IntoChangesIter, IntoOrderedChangesIter, OrderedId
};
#[cfg(feature = "parallel_iter")]
use crate::storage::ParStorage;
use crate::sync::{ReadGuardRef, WriteGuardRef};
use std::marker;
use crate::utils::*;
use crate::idtree;
use hashbrown::HashSet;

pub struct Changed<S,T>{
    storage: S,
    changed: HashSet<usize>,
    storage_changed: bool,
    _marker: marker::PhantomData<T>,
}

impl<'a, S, T> ChangedStorageExt<'a> for Changed<S,T>
    where S: Storage<'a,T>
{

    fn changed_entities(&self) -> hashbrown::hash_set::Iter<usize>{
        self.changed.iter()
    }
}

impl<'a, T: 'a, S: Storage<'a,T>> Storage<'a, T> for Changed<S,T> {
    type Get = <S as Storage<'a,T>>::Get;
    type GetMut = <S as Storage<'a,T>>::GetMut;
    type DerefTarget = <S as Storage<'a,T>>::DerefTarget;
    type IdedIter = <S as Storage<'a,T>>::IdedIter;
    type Iter = <S as Storage<'a,T>>::Iter;
    type IterMut = <S as Storage<'a,T>>::IterMut;

    #[inline]
    fn new() -> Changed<S,T>{
        Changed{
            storage: S::new(),
            changed: HashSet::new(),
            storage_changed: false,
            _marker: marker::PhantomData
        }
    }

    #[inline]
    fn with_capacity(capacity: usize) -> Self{
        Changed{
            storage: S::with_capacity(capacity),
            changed: HashSet::with_capacity(capacity),
            storage_changed: false,
            _marker: marker::PhantomData
        }
    }

    #[inline]
    fn insert(&mut self, guid: usize, t: T){
        self.storage_changed = true;
        self.storage.insert(guid, t);
    }

    #[inline]
    fn remove(&mut self, guid: usize){
        self.storage_changed = true;
        self.changed.remove(&guid);
        self.storage.remove(guid);
    }

    #[inline]
    unsafe fn get(&self, guid: usize) -> <S as Storage<'a,T>>::Get {
        self.storage.get(guid)
    }

    #[inline]
    unsafe fn get_mut(&mut self, guid: usize) -> <S as Storage<'a,T>>::GetMut {
        self.storage.get_mut(guid)
    }

    #[inline]
    unsafe fn fast_get(&self, guid: usize) -> <S as Storage<'a,T>>::Get {
        self.storage.fast_get(guid)
    }

    #[inline]
    unsafe fn fast_get_mut(&mut self, guid: usize) -> <S as Storage<'a,T>>::GetMut {
        self.storage.fast_get_mut(guid)
    }

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

    fn guid_from_fast_index(&self, idx: usize) -> usize{
        self.storage.guid_from_fast_index(idx)
    }

    #[inline]
    fn contains(&self, guid: usize) -> bool{
       self.storage.contains(guid)
    }

    fn update(&mut self){
        self.storage_changed = false;
        self.changed.clear();
    }

    fn storage_changed(&self) -> bool{
        self.storage_changed || !self.changed.is_empty()
    }

    fn tracks_changes() -> bool { true }

    fn ided_iter(&self) -> Self::IdedIter{
        self.storage.ided_iter()
    }

    fn iter(&self) -> Self::Iter{
        self.storage.iter()
    }

    fn iter_mut(&mut self) -> Self::IterMut{
        self.storage.iter_mut()
    }
}

#[cfg(feature = "parallel_iter")]
impl<'a, T: 'a, S: ParStorage<'a,T>> ParStorage<'a, T> for Changed<S,T>
where
    S: Storage<'a, T>,
    <S as Storage<'a,T>>::Get: Send,
    <S as Storage<'a,T>>::GetMut: Send,
{
    type ParIter = <S as ParStorage<'a,T>>::ParIter;
    type ParIterMut = <S as ParStorage<'a,T>>::ParIterMut;

    fn par_iter(&self) -> Self::ParIter{
        self.storage.par_iter()
    }

    fn par_iter_mut(&mut self) -> Self::ParIterMut{
        self.storage.par_iter_mut()
    }
}

impl<'a, S, T> HierarchicalStorage<'a,T> for Changed<S, T>
where S: HierarchicalStorage<'a,T>,
    T: 'a,
{
    unsafe fn insert_child(&mut self, parent_guid: usize, guid: usize, value: T){
        self.storage_changed = true;
        self.storage.insert_child(parent_guid, guid, value)
    }

    unsafe fn get_node(&self, guid: usize) -> idtree::NodeRef<T>{
        self.storage.get_node(guid)
    }

    unsafe fn get_node_mut(&mut self, guid: usize) -> idtree::NodeRefMut<T>{
        self.storage.get_node_mut(guid)
    }

    unsafe fn fast_get_node(&self, idx: idtree::NodeId) -> idtree::NodeRef<T>{
        self.storage.fast_get_node(idx)
    }

    unsafe fn fast_get_node_mut(&mut self, idx: idtree::NodeId) -> idtree::NodeRefMut<T>{
        self.storage.fast_get_node_mut(idx)
    }

    unsafe fn guid_for(&self, nodeid: idtree::NodeId) -> usize{
        self.storage.guid_for(nodeid)
    }

    unsafe fn ordered_fast_index(&self, guid: usize) -> idtree::NodeId{
        self.storage.ordered_fast_index(guid)
    }

    fn ordered_ids(&self) -> ReadGuardRef<Vec<OrderedId<idtree::NodeId>>> {
        self.storage.ordered_ids()
    }
}

impl<'a, S, T> OneToNStorage<'a,T> for Changed<S, T>
where S: OneToNStorage<'a,T>,
    T: 'a,
{
    fn insert_slice<I: IntoIterator<Item = T>>(&mut self, guid: usize, t: I){
        self.storage_changed = true;
        self.storage.insert_slice(guid, t)
    }

    unsafe fn get_slice(&self, guid: usize) -> SliceView<T>{
        self.storage.get_slice(guid)
    }

    unsafe fn get_slice_mut(&mut self, guid: usize) -> SliceViewMut<T>{
        self.storage.get_slice_mut(guid)
    }
}

// TODO: this implementations trigger a recursive error sometimes cause
// ReadGuard<Changed> is also IntoIter
impl<'a, S, T> IntoIter for ReadGuardRef<'a, Changed<S,T>>
where ReadGuardRef<'a,S>: IntoIter
{
    type Iter = <ReadGuardRef<'a,S> as IntoIter>::Iter;
    fn into_iter(self) -> <ReadGuardRef<'a,S> as IntoIter>::Iter{
        ReadGuardRef::map(self, |changed| &changed.storage).into_iter()
    }
}

impl<'a, S, T> IntoIterMut for WriteGuardRef<'a, Changed<S,T>>
where WriteGuardRef<'a,S>: IntoIterMut
{
    type IterMut = <WriteGuardRef<'a,S> as IntoIterMut>::IterMut;
    fn into_iter_mut(self) -> <WriteGuardRef<'a,S> as IntoIterMut>::IterMut {
        WriteGuardRef::map(self, |changed| &mut changed.storage).into_iter_mut()
    }
}

impl<'a, S, T> IntoOrderedIter for ReadGuardRef<'a, Changed<S, T>>
where ReadGuardRef<'a, S>: IntoOrderedIter
{
    type OrderedIter = <ReadGuardRef<'a, S> as IntoOrderedIter>::OrderedIter;
    fn into_ordered_iter(self) -> Self::OrderedIter {
        ReadGuardRef::map(self, |changed| &changed.storage).into_ordered_iter()
    }
}

impl<'a, S, T> IntoOrderedIterMut for WriteGuardRef<'a, Changed<S, T>>
where WriteGuardRef<'a, S>: IntoOrderedIterMut
{
    type OrderedIterMut = <WriteGuardRef<'a, S> as IntoOrderedIterMut>::OrderedIterMut;
    fn into_ordered_iter_mut(self) -> Self::OrderedIterMut {
        WriteGuardRef::map(self, |changed| &mut changed.storage).into_ordered_iter_mut()
    }
}

impl<'a,S,T> IntoChangesIter<'a,T> for WriteGuardRef<'a, Changed<S, T>>
where S: Storage<'a,T>,
    WriteGuardRef<'a, S>: IntoIdedIterMut,
    <WriteGuardRef<'a, S> as IntoIdedIterMut>::IdedIterMut: Iterator<Item = (usize, <S as Storage<'a,T>>::GetMut)>,
{
    type ChangesItem = <S as Storage<'a,T>>::GetMut;
    type RawSubStorage = S;
    type SubStorage = WriteGuardRef<'a, S>;

    fn extend_changes<F>(mut self, mut has_chagend: F)
    where F: FnMut(Self::ChangesItem) -> bool
    {
        let changed = unsafe{ delete_lifetime_mut(&mut self.changed) };
        let iter = WriteGuardRef::map(
            self,
            |changed| &mut changed.storage
        ).into_ided_iter_mut();
        changed.extend(iter.filter_map(|(id, t)| if has_chagend(t){
            Some(id)
        }else{
            None
        }));
    }

    unsafe fn into_storage_and_changes(mut self) -> (Self::SubStorage, &'a mut HashSet<usize>){
        let changed = delete_lifetime_mut(&mut self.changed);
        let substorage = WriteGuardRef::map(
            self,
            |changed| &mut changed.storage
        );
        (substorage, changed)
    }
}

impl<'a,S,T> IntoOrderedChangesIter<'a,T> for WriteGuardRef<'a, Changed<S, T>>
where S: Storage<'a,T>,
    S: HierarchicalStorage<'a,T> + 'a,
    WriteGuardRef<'a, S>: IntoOrderedIdedIterMut,
    <WriteGuardRef<'a, S> as IntoOrderedIdedIterMut>::OrderedIdedIterMut: Iterator<Item = (usize, idtree::NodeRefMut<'a,T>)>
{
    type OrderedChangesItem = idtree::NodeRefMut<'a,T>;
    type RawSubStorage = S;
    type SubStorage = WriteGuardRef<'a, S>;

    fn ordered_extend_changes<F>(self, mut has_changed: F)
    where F: FnMut(Self::OrderedChangesItem) -> bool
    {
        let (guard, changed) = unsafe{ self.into_storage_and_changes() };
        let iter = guard.into_ordered_ided_iter_mut();
        changed.extend(iter.filter_map(|(id, t)| if has_changed(t){
            Some(id)
        }else{
            None
        }));
    }

    unsafe fn into_storage_and_changes(mut self) -> (Self::SubStorage, &'a mut HashSet<usize>){
        let changed = delete_lifetime_mut(&mut self.changed);
        let substorage = WriteGuardRef::map(self, |changed| &mut changed.storage);
        (substorage, changed)
    }
}