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
use std::usize; use std::mem; use crate::storage::{Storage, IntoIter, IntoIterMut, IntoIdedIterMut}; #[cfg(feature = "parallel_iter")] use crate::storage::ParStorage; use crate::sync::{ReadGuardRef, WriteGuardRef}; use densevec::{self, DenseVec}; use crate::utils::*; impl<'a, T: 'a> Storage<'a, T> for DenseVec<T>{ type Get = &'a T; type GetMut = &'a mut T; type DerefTarget = T; type IdedIter = densevec::Iter<'a, usize, T>; type Iter = densevec::Values<'a, T>; type IterMut = densevec::ValuesMut<'a, T>; #[inline] fn new() -> DenseVec<T>{ DenseVec::new() } #[inline] fn with_capacity(capacity: usize) -> Self{ DenseVec::with_capacity(capacity) } #[inline] fn insert(&mut self, guid: usize, t: T){ self.insert(guid, t); } #[inline] fn remove(&mut self, guid: usize){ self.remove(guid).unwrap(); } #[inline] unsafe fn get(&self, guid: usize) -> &'a T{ delete_lifetime(self.get_unchecked(guid)) } #[inline] unsafe fn get_mut(&mut self, guid: usize) -> &'a mut T{ delete_lifetime_mut(self.get_unchecked_mut(guid)) } #[inline] unsafe fn fast_get(&self, idx: usize) -> Self::Get{ delete_lifetime(self.get_fast_unchecked(idx)) } #[inline] unsafe fn fast_get_mut(&mut self, idx: usize) -> Self::GetMut{ delete_lifetime_mut(self.get_fast_unchecked_mut(idx)) } #[inline] fn fast_index(&self, guid: usize) -> usize{ unsafe{ self.fast_index_unchecked_for(guid) } } #[inline] fn guid_from_fast_index(&self, idx: usize) -> usize{ unsafe{ self.unchecked_guid_from_fast_index(idx) } } #[inline] fn contains(&self, guid: usize) -> bool{ self.contains_key(guid) } #[inline] fn ided_iter(&self) -> densevec::Iter<'a, usize, T>{ unsafe{ mem::transmute(self.iter()) } } #[inline] fn iter(&self) -> densevec::Values<'a, T>{ unsafe{ mem::transmute::<densevec::Values<T>,densevec::Values<'a, T>>(self.values()) } } #[inline] fn iter_mut(&mut self) -> densevec::ValuesMut<'a, T>{ unsafe{ mem::transmute::<densevec::ValuesMut<T>,densevec::ValuesMut<'a, T>>(self.values_mut()) } } } #[cfg(feature = "parallel_iter")] impl<'a, T: 'a> ParStorage<'a, T> for DenseVec<T> where T: Sync + Send, <Self as Storage<'a, T>>::Get: Send, <Self as Storage<'a, T>>::GetMut: Send { type ParIter = densevec::ParValues<'a, T>; type ParIterMut = densevec::ParValuesMut<'a, T>; fn par_iter(&self) -> Self::ParIter { unsafe{ mem::transmute(self.par_values()) } } fn par_iter_mut(&mut self) -> Self::ParIterMut { unsafe{ mem::transmute(self.par_values_mut()) } } } pub struct DenseIter<'a, T: 'a>{ _guard: ReadGuardRef<'a, DenseVec<T>>, iter: densevec::Values<'a, T> } impl<'a, T: 'a> Iterator for DenseIter<'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 DenseIter<'a, T>{ #[inline] fn len(&self) -> usize { self.iter.len() } } pub struct DenseIterMut<'a, T: 'a>{ _guard: WriteGuardRef<'a, DenseVec<T>>, iter: densevec::ValuesMut<'a, T> } impl<'a, T: 'a> Iterator for DenseIterMut<'a, T>{ type Item = &'a mut T; #[inline] fn next(&mut self) -> Option<&'a mut T>{ self.iter.next() } #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() } } impl<'a, T: 'a> ExactSizeIterator for DenseIterMut<'a, T>{ #[inline] fn len(&self) -> usize { self.iter.len() } } impl<'a, T> IntoIter for ReadGuardRef<'a, DenseVec<T>>{ type Iter = DenseIter<'a, T>; fn into_iter(self) -> DenseIter<'a, T>{ DenseIter{ iter: unsafe{ mem::transmute::<densevec::Values<T>, densevec::Values<'a,T>>(self.values()) }, _guard: self, } } } impl<'a, T> IntoIterMut for WriteGuardRef<'a, DenseVec<T>>{ type IterMut = DenseIterMut<'a, T>; fn into_iter_mut(mut self) -> DenseIterMut<'a, T>{ DenseIterMut{ iter: unsafe{ mem::transmute::<densevec::ValuesMut<T>, densevec::ValuesMut<'a,T>>(self.values_mut()) }, _guard: self, } } } 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, } } }