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
use std::usize;
use std::mem;
use std::hash::Hash;
use std::ops::Deref;

use crate::storage::{Storage, IntoIter, IntoIterMut};
use crate::sync::{ReadGuardRef, WriteGuardRef};
use densevec::{self, DenseVec};
use crate::utils::*;
use crossbeam_skiplist::SkipSet;

pub struct UniqueDenseVec<T> {
    storage: DenseVec<T>,
    unique: SkipSet<u32>,
}

pub struct RefMut<'a, T>{
    value: &'a mut T,
    unique: &'a SkipSet<u32>,
}

impl<'a, T> Deref for RefMut<'a, T>{
    type Target = T;
    fn deref(&self) -> &T {
        self.value
    }
}

impl<'a, T: Hash + Eq> RefMut<'a, T> {
    pub fn set(&mut self, t: T) {
        if *self.value != t {
            self.unique.remove(&fxhash::hash32(self.value));
            let new_hash = fxhash::hash32(&t);
            if self.unique.contains(&new_hash) {
                panic!("Trying to store non unique value on a unique storage")
            }
            self.unique.insert(new_hash);
        }
        *self.value = t;
    }
}

impl<'a, T: 'a> Storage<'a, T> for UniqueDenseVec<T>
where T: Hash
{
    type Get = &'a T;
    type GetMut = RefMut<'a, T>;
    type DerefTarget = T;
    type IdedIter = densevec::Iter<'a, usize, T>;
    type Iter = densevec::Values<'a, T>;
    type IterMut = UniqueIterMut<'a, T>;

    #[inline]
    fn new() -> UniqueDenseVec<T>{
        UniqueDenseVec{
            storage: DenseVec::default(),
            unique: SkipSet::default()
        }
    }

    #[inline]
    fn with_capacity(capacity: usize) -> Self{
        UniqueDenseVec{
            storage: DenseVec::with_capacity(capacity),
            unique: SkipSet::new()
        }
    }

    #[inline]
    fn insert(&mut self, guid: usize, t: T){
        let hash = fxhash::hash32(&t);
        if self.unique.contains(&hash) {
            panic!("Trying to store non unique value on a unique storage")
        }
        self.storage.insert(guid, t);
        self.unique.insert(hash);
    }

    #[inline]
    fn remove(&mut self, guid: usize){
        let t = self.storage.remove(guid).unwrap();
        self.unique.remove(&fxhash::hash32(&t));
    }

    #[inline]
    unsafe fn get(&self, guid: usize) -> &'a T{
        delete_lifetime(self.storage.get_unchecked(guid))
    }

    #[inline]
    unsafe fn get_mut(&mut self, guid: usize) -> Self::GetMut {
        let value = delete_lifetime_mut(self.storage.get_unchecked_mut(guid));
        let unique = delete_lifetime_mut(&mut self.unique);
        RefMut{ value, unique }
    }

    #[inline]
    unsafe fn fast_get(&self, idx: usize) -> Self::Get{
        delete_lifetime(self.storage.get_fast_unchecked(idx))
    }

    #[inline]
    unsafe fn fast_get_mut(&mut self, idx: usize) -> Self::GetMut{
        let value = delete_lifetime_mut(self.storage.get_fast_unchecked_mut(idx));
        let unique = delete_lifetime_mut(&mut self.unique);
        RefMut{ value, unique }
    }

    #[inline]
    fn fast_index(&self, guid: usize) -> usize{
        unsafe{ self.storage.fast_index_unchecked_for(guid) }
    }

    #[inline]
    fn guid_from_fast_index(&self, idx: usize) -> usize{
        unsafe{ self.storage.unchecked_guid_from_fast_index(idx) }
    }

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

    #[inline]
    fn ided_iter(&self) -> densevec::Iter<'a, usize, T>{
        unsafe{ mem::transmute(self.storage.iter()) }
    }

    #[inline]
    fn iter(&self) -> densevec::Values<'a, T>{
        unsafe{ mem::transmute::<densevec::Values<T>,densevec::Values<'a, T>>(self.storage.values()) }
    }

    #[inline]
    fn iter_mut(&mut self) -> UniqueIterMut<'a, T>{
        unsafe{
            UniqueIterMut {
                unique: delete_lifetime(&self.unique),
                iter: mem::transmute::<densevec::ValuesMut<T>, densevec::ValuesMut<'a, T>>(self.storage.values_mut()),
            }
        }
    }
}

pub struct UniqueIterMut<'a, T: 'a>{
    unique: &'a SkipSet<u32>,
    iter: densevec::ValuesMut<'a, T>
}

impl<'a, T: 'a> Iterator for UniqueIterMut<'a, T>{
    type Item = RefMut<'a, T>;
    #[inline]
    fn next(&mut self) -> Option<RefMut<'a, T>>{
        self.iter.next().map(|value| RefMut{ value, unique: self.unique})
    }

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

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

pub struct UniqueIntoIter<'a, T: 'a>{
    _guard: ReadGuardRef<'a, UniqueDenseVec<T>>,
    iter: densevec::Values<'a, T>
}

impl<'a, T: 'a> Iterator for UniqueIntoIter<'a, T>{
    type Item = &'a T;
    #[inline]
    fn next(&mut self) -> Option<&'a T>{
        self.iter.next()
    }

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

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

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

pub struct UniqueIntoIterMut<'a, T: 'a>{
    _guard: WriteGuardRef<'a, UniqueDenseVec<T>>,
    iter: densevec::ValuesMut<'a, T>
}

impl<'a, T: 'a> Iterator for UniqueIntoIterMut<'a, T>{
    type Item = RefMut<'a, T>;
    #[inline]
    fn next(&mut self) -> Option<RefMut<'a, T>>{
        self.iter.next().map(|value| RefMut{
            value,
            unique: unsafe{ delete_lifetime(&self._guard.unique) }
        })
    }

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

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

impl<'a, T> IntoIterMut for WriteGuardRef<'a, UniqueDenseVec<T>>{
    type IterMut = UniqueIntoIterMut<'a, T>;
    fn into_iter_mut(mut self) -> UniqueIntoIterMut<'a, T>{
        UniqueIntoIterMut{
           iter: unsafe{ mem::transmute::<densevec::ValuesMut<T>, densevec::ValuesMut<'a,T>>(self.storage.values_mut()) },
            _guard: self,
        }
    }
}


// #[cfg(feature = "parallel_iter")]
// impl<'a, T: 'a> ParStorage<'a, T> for UniqueDenseVec<T>
// where
//     T: Hash + Eq + Sync + Send,
//     Self::Get: Send,
//     Self::GetMut: Send
// {
//     type ParIter = densevec::ParValues<'a, T>;
//     type ParIterMut = UniqueParIterMut<'a, T>;

//     fn par_iter(&self) -> Self::ParIter {
//         unsafe{ mem::transmute(self.storage.par_values()) }
//     }

//     fn par_iter_mut(&mut self) -> Self::ParIterMut {
//         todo!()
//         // unsafe{ mem::transmute(self.storage.par_values_mut()) }
//     }
// }


// #[cfg(feature = "parallel_iter")]
// pub struct UniqueParIterMut{}

// pub struct DenseIdedIterMut<'a, T: 'a>{
//     _guard: WriteGuardRef<'a, DenseVec<T>>,
//     iter: densevec::IterMut<'a, usize, T>
// }

// impl<'a, T: 'a> Iterator for DenseIdedIterMut<'a, T>{
//     type Item = (usize, &'a mut T);
//     #[inline]
//     fn next(&mut self) -> Option<(usize, &'a mut T)>{
//         self.iter.next()
//     }

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

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

// impl<'a, T> IntoIdedIterMut for WriteGuardRef<'a, DenseVec<T>>{
//     type IdedIterMut = DenseIdedIterMut<'a, T>;
//     fn into_ided_iter_mut(mut self) -> DenseIdedIterMut<'a, T>{
//         DenseIdedIterMut{
//             iter:  unsafe{ mem::transmute(self.iter_mut()) },
//             _guard: self,
//         }
//     }
// }