author | alfadur |
Sun, 26 Mar 2023 06:06:14 +0300 | |
changeset 15942 | bcd43b90401a |
parent 15941 | 8035f7452b48 |
child 15945 | 343b8819b051 |
permissions | -rw-r--r-- |
15354 | 1 |
use super::common::GearId; |
2 |
use std::{ |
|
3 |
any::TypeId, |
|
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
4 |
fmt::{Debug, Error, Formatter}, |
15392 | 5 |
marker::PhantomData, |
15426 | 6 |
mem::{align_of, size_of, MaybeUninit}, |
15354 | 7 |
num::NonZeroU16, |
15368 | 8 |
ptr::{copy_nonoverlapping, null_mut, NonNull}, |
15354 | 9 |
slice, |
10 |
}; |
|
15305 | 11 |
|
15368 | 12 |
pub trait TypeTuple: Sized { |
15373 | 13 |
fn get_types(types: &mut Vec<TypeId>); |
15392 | 14 |
} |
15 |
||
16 |
impl TypeTuple for () { |
|
15393 | 17 |
fn get_types(_types: &mut Vec<TypeId>) {} |
15392 | 18 |
} |
19 |
||
20 |
impl<T: 'static> TypeTuple for &T { |
|
21 |
fn get_types(types: &mut Vec<TypeId>) { |
|
22 |
types.push(TypeId::of::<T>()); |
|
23 |
} |
|
24 |
} |
|
25 |
||
26 |
pub trait TypeIter: TypeTuple { |
|
15387 | 27 |
unsafe fn iter<F: FnMut(GearId, Self)>(slices: &[*mut u8], count: usize, f: F); |
15305 | 28 |
} |
29 |
||
15369 | 30 |
macro_rules! type_tuple_impl { |
31 |
($($n: literal: $t: ident),+) => { |
|
32 |
impl<$($t: 'static),+> TypeTuple for ($(&$t),+,) { |
|
15368 | 33 |
fn get_types(types: &mut Vec<TypeId>) { |
15369 | 34 |
$(types.push(TypeId::of::<$t>()));+ |
15368 | 35 |
} |
15392 | 36 |
} |
15305 | 37 |
|
15392 | 38 |
impl<$($t: 'static),+> TypeIter for ($(&$t),+,) { |
15379 | 39 |
unsafe fn iter<F: FnMut(GearId, Self)>(slices: &[*mut u8], count: usize, mut f: F) { |
15368 | 40 |
for i in 0..count { |
15380 | 41 |
f(*(*slices.get_unchecked(0) as *const GearId).add(i), |
42 |
($(&*(*slices.get_unchecked($n + 1) as *mut $t).add(i)),+,)); |
|
15368 | 43 |
} |
44 |
} |
|
45 |
} |
|
15354 | 46 |
|
15369 | 47 |
impl<$($t: 'static),+> TypeTuple for ($(&mut $t),+,) { |
15368 | 48 |
fn get_types(types: &mut Vec<TypeId>) { |
15369 | 49 |
$(types.push(TypeId::of::<$t>()));+ |
15368 | 50 |
} |
15392 | 51 |
} |
15368 | 52 |
|
15392 | 53 |
impl<$($t: 'static),+> TypeIter for ($(&mut $t),+,) { |
15379 | 54 |
unsafe fn iter<F: FnMut(GearId, Self)>(slices: &[*mut u8], count: usize, mut f: F) { |
15368 | 55 |
for i in 0..count { |
15380 | 56 |
f(*(*slices.get_unchecked(0) as *const GearId).add(i), |
57 |
($(&mut *(*slices.get_unchecked($n + 1) as *mut $t).add(i)),+,)); |
|
15368 | 58 |
} |
59 |
} |
|
15354 | 60 |
} |
61 |
} |
|
15305 | 62 |
} |
63 |
||
15369 | 64 |
type_tuple_impl!(0: A); |
65 |
type_tuple_impl!(0: A, 1: B); |
|
66 |
type_tuple_impl!(0: A, 1: B, 2: C); |
|
67 |
type_tuple_impl!(0: A, 1: B, 2: C, 3: D); |
|
68 |
type_tuple_impl!(0: A, 1: B, 2: C, 3: D, 4: E); |
|
15368 | 69 |
|
15354 | 70 |
const BLOCK_SIZE: usize = 32768; |
71 |
||
72 |
struct DataBlock { |
|
73 |
max_elements: u16, |
|
74 |
elements_count: u16, |
|
75 |
data: Box<[u8; BLOCK_SIZE]>, |
|
15358 | 76 |
component_blocks: [Option<NonNull<u8>>; 64], |
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
77 |
element_sizes: Box<[u16]>, |
15305 | 78 |
} |
79 |
||
15354 | 80 |
impl Unpin for DataBlock {} |
81 |
||
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
82 |
impl Debug for DataBlock { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
83 |
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
84 |
write!( |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
85 |
f, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
86 |
"Block ({}/{}) {{\n", |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
87 |
self.elements_count, self.max_elements |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
88 |
)?; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
89 |
write!(f, "\tIDs: [")?; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
90 |
let id_slice = unsafe { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
91 |
slice::from_raw_parts( |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
92 |
self.data.as_ptr() as *const GearId, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
93 |
self.elements_count as usize, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
94 |
) |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
95 |
}; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
96 |
for gear_id in id_slice { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
97 |
write!(f, "{}, ", gear_id)?; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
98 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
99 |
write!(f, "]\n")?; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
100 |
for type_index in 0..self.element_sizes.len() { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
101 |
if let Some(ptr) = self.component_blocks[type_index] { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
102 |
write!(f, "\tC{}: [", type_index)?; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
103 |
let slice = unsafe { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
104 |
slice::from_raw_parts( |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
105 |
ptr.as_ptr(), |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
106 |
(self.elements_count * self.element_sizes[type_index]) as usize, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
107 |
) |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
108 |
}; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
109 |
for byte in slice { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
110 |
write!(f, "{}, ", byte)?; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
111 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
112 |
write!(f, "]\n")?; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
113 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
114 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
115 |
write!(f, "}}\n") |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
116 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
117 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
118 |
|
15354 | 119 |
impl DataBlock { |
15426 | 120 |
fn new(mask: u64, element_sizes: &[u16], element_alignments: &[u8]) -> Self { |
121 |
let total_padding: usize = element_alignments.iter().map(|x| *x as usize).sum(); |
|
15354 | 122 |
let total_size: u16 = element_sizes |
123 |
.iter() |
|
124 |
.enumerate() |
|
15358 | 125 |
.filter(|(i, _)| mask & (1 << *i as u64) != 0) |
15354 | 126 |
.map(|(_, size)| *size) |
127 |
.sum(); |
|
15426 | 128 |
let max_elements = |
129 |
((BLOCK_SIZE - total_padding) / (total_size as usize + size_of::<GearId>())) as u16; |
|
15354 | 130 |
|
15426 | 131 |
//ensure the block memory is aligned to GearId |
132 |
let tmp_data: Box<[GearId; BLOCK_SIZE / size_of::<GearId>()]> = |
|
133 |
Box::new(unsafe { MaybeUninit::uninit().assume_init() }); |
|
15354 | 134 |
let mut data: Box<[u8; BLOCK_SIZE]> = |
15426 | 135 |
unsafe { Box::from_raw(Box::into_raw(tmp_data) as *mut [u8; BLOCK_SIZE]) }; |
136 |
||
15354 | 137 |
let mut blocks = [None; 64]; |
15426 | 138 |
let mut address = unsafe { |
139 |
data.as_mut_ptr() |
|
140 |
.add(size_of::<GearId>() * max_elements as usize) |
|
141 |
}; |
|
15354 | 142 |
|
15941 | 143 |
let mut mask_bits = mask; |
144 |
while mask_bits != 0 { |
|
145 |
let i = mask_bits.trailing_zeros() as usize; |
|
146 |
||
147 |
unsafe { |
|
148 |
address = address.add(address.align_offset(element_alignments[i] as usize)); |
|
149 |
blocks[i] = Some(NonNull::new_unchecked(address)); |
|
150 |
address = address.add(element_sizes[i] as usize * max_elements as usize) |
|
151 |
}; |
|
152 |
||
153 |
mask_bits &= mask_bits - 1; |
|
15354 | 154 |
} |
15426 | 155 |
|
15305 | 156 |
Self { |
15354 | 157 |
elements_count: 0, |
158 |
max_elements, |
|
159 |
data, |
|
15358 | 160 |
component_blocks: blocks, |
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
161 |
element_sizes: Box::from(element_sizes), |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
162 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
163 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
164 |
|
15941 | 165 |
#[inline] |
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
166 |
fn gear_ids(&self) -> &[GearId] { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
167 |
unsafe { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
168 |
slice::from_raw_parts( |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
169 |
self.data.as_ptr() as *const GearId, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
170 |
self.max_elements as usize, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
171 |
) |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
172 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
173 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
174 |
|
15941 | 175 |
#[inline] |
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
176 |
fn gear_ids_mut(&mut self) -> &mut [GearId] { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
177 |
unsafe { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
178 |
slice::from_raw_parts_mut( |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
179 |
self.data.as_mut_ptr() as *mut GearId, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
180 |
self.max_elements as usize, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
181 |
) |
15305 | 182 |
} |
183 |
} |
|
184 |
||
15941 | 185 |
#[inline] |
15354 | 186 |
fn is_full(&self) -> bool { |
187 |
self.elements_count == self.max_elements |
|
188 |
} |
|
15305 | 189 |
} |
190 |
||
15354 | 191 |
#[derive(Clone, Copy, Debug, Default)] |
15379 | 192 |
struct LookupEntry { |
15354 | 193 |
index: Option<NonZeroU16>, |
194 |
block_index: u16, |
|
15305 | 195 |
} |
196 |
||
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
197 |
impl LookupEntry { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
198 |
fn new(block_index: u16, index: u16) -> Self { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
199 |
Self { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
200 |
index: unsafe { Some(NonZeroU16::new_unchecked(index + 1)) }, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
201 |
block_index, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
202 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
203 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
204 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
205 |
|
15387 | 206 |
#[derive(Copy, Clone, Eq, PartialEq, Debug)] |
207 |
struct BlockMask { |
|
208 |
type_mask: u64, |
|
209 |
tag_mask: u64, |
|
210 |
} |
|
211 |
||
212 |
impl BlockMask { |
|
213 |
#[inline] |
|
214 |
fn new(type_mask: u64, tag_mask: u64) -> Self { |
|
215 |
Self { |
|
216 |
type_mask, |
|
217 |
tag_mask, |
|
218 |
} |
|
219 |
} |
|
220 |
||
221 |
#[inline] |
|
222 |
fn with_type(&self, type_bit: u64) -> Self { |
|
223 |
Self::new(self.type_mask | type_bit, self.tag_mask) |
|
224 |
} |
|
225 |
||
226 |
#[inline] |
|
15942 | 227 |
fn without_type(&self, type_bit: u64) -> Self { |
228 |
Self::new(self.type_mask & !type_bit, self.tag_mask) |
|
229 |
} |
|
230 |
||
231 |
#[inline] |
|
15387 | 232 |
fn with_tag(&self, tag_bit: u64) -> Self { |
233 |
Self::new(self.type_mask, self.tag_mask | tag_bit) |
|
234 |
} |
|
235 |
} |
|
236 |
||
15305 | 237 |
pub struct GearDataManager { |
238 |
types: Vec<TypeId>, |
|
15387 | 239 |
tags: Vec<TypeId>, |
15354 | 240 |
blocks: Vec<DataBlock>, |
15387 | 241 |
block_masks: Vec<BlockMask>, |
15354 | 242 |
element_sizes: Box<[u16; 64]>, |
15426 | 243 |
element_alignments: Box<[u8; 64]>, |
15354 | 244 |
lookup: Box<[LookupEntry]>, |
15305 | 245 |
} |
246 |
||
247 |
impl GearDataManager { |
|
248 |
pub fn new() -> Self { |
|
249 |
Self { |
|
15387 | 250 |
types: Vec::with_capacity(64), |
251 |
tags: Vec::with_capacity(64), |
|
15354 | 252 |
blocks: vec![], |
253 |
block_masks: vec![], |
|
254 |
element_sizes: Box::new([0; 64]), |
|
15426 | 255 |
element_alignments: Box::new([0; 64]), |
15829 | 256 |
lookup: vec![LookupEntry::default(); u16::MAX as usize].into_boxed_slice(), |
15354 | 257 |
} |
258 |
} |
|
259 |
||
260 |
#[inline] |
|
261 |
fn get_type_index<T: 'static>(&self) -> Option<usize> { |
|
262 |
let type_id = TypeId::of::<T>(); |
|
263 |
self.types.iter().position(|id| *id == type_id) |
|
264 |
} |
|
265 |
||
15387 | 266 |
#[inline] |
267 |
fn get_tag_index<T: 'static>(&self) -> Option<usize> { |
|
268 |
let type_id = TypeId::of::<T>(); |
|
269 |
self.tags.iter().position(|id| *id == type_id) |
|
270 |
} |
|
271 |
||
15395
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
272 |
fn move_between_blocks( |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
273 |
&mut self, |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
274 |
src_block_index: u16, |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
275 |
src_index: u16, |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
276 |
dest_block_index: u16, |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
277 |
) -> u16 { |
15373 | 278 |
debug_assert!(src_block_index != dest_block_index); |
279 |
let src_mask = self.block_masks[src_block_index as usize]; |
|
280 |
let dest_mask = self.block_masks[dest_block_index as usize]; |
|
15942 | 281 |
debug_assert!(src_mask.type_mask & dest_mask.type_mask != 0); |
15354 | 282 |
|
15373 | 283 |
let src_block = &self.blocks[src_block_index as usize]; |
284 |
let dest_block = &self.blocks[dest_block_index as usize]; |
|
285 |
debug_assert!(src_index < src_block.elements_count); |
|
286 |
debug_assert!(!dest_block.is_full()); |
|
15357 | 287 |
|
15373 | 288 |
let dest_index = dest_block.elements_count; |
15941 | 289 |
|
290 |
let mut type_mask = src_mask.type_mask; |
|
291 |
while type_mask != 0 { |
|
292 |
let i = type_mask.trailing_zeros() as usize; |
|
293 |
||
294 |
let size = self.element_sizes[i]; |
|
295 |
let src_ptr = src_block.component_blocks[i].unwrap().as_ptr(); |
|
15942 | 296 |
if let Some(dest_ptr) = dest_block.component_blocks[i] { |
297 |
let dest_ptr = dest_ptr.as_ptr(); |
|
298 |
unsafe { |
|
299 |
copy_nonoverlapping( |
|
300 |
src_ptr.add((src_index * size) as usize), |
|
301 |
dest_ptr.add((dest_index * size) as usize), |
|
302 |
size as usize, |
|
303 |
); |
|
304 |
} |
|
305 |
} |
|
15941 | 306 |
unsafe { |
307 |
if src_index < src_block.elements_count - 1 { |
|
15357 | 308 |
copy_nonoverlapping( |
15941 | 309 |
src_ptr.add((size * (src_block.elements_count - 1)) as usize), |
310 |
src_ptr.add((size * src_index) as usize), |
|
15373 | 311 |
size as usize, |
15357 | 312 |
); |
313 |
} |
|
314 |
} |
|
15941 | 315 |
|
316 |
type_mask &= type_mask - 1; |
|
15354 | 317 |
} |
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
318 |
|
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
319 |
let src_block = &mut self.blocks[src_block_index as usize]; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
320 |
let gear_id = src_block.gear_ids()[src_index as usize]; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
321 |
|
15942 | 322 |
if src_index + 1 < src_block.elements_count { |
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
323 |
let relocated_index = src_block.elements_count as usize - 1; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
324 |
let gear_ids = src_block.gear_ids_mut(); |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
325 |
let relocated_id = gear_ids[relocated_index]; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
326 |
|
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
327 |
gear_ids[src_index as usize] = relocated_id; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
328 |
self.lookup[relocated_id.get() as usize - 1] = |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
329 |
LookupEntry::new(src_block_index, src_index); |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
330 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
331 |
src_block.elements_count -= 1; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
332 |
|
15373 | 333 |
let dest_block = &mut self.blocks[dest_block_index as usize]; |
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
334 |
let dest_index = dest_block.elements_count; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
335 |
|
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
336 |
dest_block.gear_ids_mut()[dest_index as usize] = gear_id; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
337 |
self.lookup[gear_id.get() as usize - 1] = LookupEntry::new(dest_block_index, dest_index); |
15373 | 338 |
dest_block.elements_count += 1; |
15395
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
339 |
dest_block.elements_count - 1 |
15354 | 340 |
} |
341 |
||
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
342 |
fn add_to_block<T: Clone>(&mut self, gear_id: GearId, block_index: u16, value: &T) { |
15387 | 343 |
debug_assert!( |
344 |
self.block_masks[block_index as usize] |
|
345 |
.type_mask |
|
346 |
.count_ones() |
|
347 |
== 1 |
|
348 |
); |
|
15356 | 349 |
|
350 |
let block = &mut self.blocks[block_index as usize]; |
|
351 |
debug_assert!(block.elements_count < block.max_elements); |
|
352 |
||
353 |
unsafe { |
|
15395
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
354 |
*(block.component_blocks[0].unwrap().as_ptr() as *mut T) |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
355 |
.add(block.elements_count as usize) = value.clone(); |
15356 | 356 |
}; |
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
357 |
|
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
358 |
let index = block.elements_count; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
359 |
self.lookup[gear_id.get() as usize - 1] = LookupEntry::new(block_index, index); |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
360 |
block.gear_ids_mut()[index as usize] = gear_id; |
15356 | 361 |
block.elements_count += 1; |
15354 | 362 |
} |
363 |
||
364 |
fn remove_from_block(&mut self, block_index: u16, index: u16) { |
|
15356 | 365 |
let block = &mut self.blocks[block_index as usize]; |
366 |
debug_assert!(index < block.elements_count); |
|
367 |
||
368 |
for (i, size) in self.element_sizes.iter().cloned().enumerate() { |
|
369 |
if index < block.elements_count - 1 { |
|
15358 | 370 |
if let Some(ptr) = block.component_blocks[i] { |
15356 | 371 |
unsafe { |
15357 | 372 |
copy_nonoverlapping( |
15356 | 373 |
ptr.as_ptr() |
374 |
.add((size * (block.elements_count - 1)) as usize), |
|
375 |
ptr.as_ptr().add((size * index) as usize), |
|
376 |
size as usize, |
|
377 |
); |
|
378 |
} |
|
379 |
} |
|
380 |
} |
|
381 |
} |
|
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
382 |
|
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
383 |
self.lookup[block.gear_ids()[index as usize].get() as usize - 1] = LookupEntry::default(); |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
384 |
if index < block.elements_count - 1 { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
385 |
let relocated_index = block.elements_count as usize - 1; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
386 |
let gear_ids = block.gear_ids_mut(); |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
387 |
|
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
388 |
gear_ids[index as usize] = gear_ids[relocated_index]; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
389 |
self.lookup[gear_ids[relocated_index].get() as usize - 1] = |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
390 |
LookupEntry::new(block_index, index); |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
391 |
} |
15356 | 392 |
block.elements_count -= 1; |
15354 | 393 |
} |
394 |
||
15395
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
395 |
fn write_component<T: Clone>( |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
396 |
&mut self, |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
397 |
block_index: u16, |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
398 |
index: u16, |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
399 |
type_index: usize, |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
400 |
value: &T, |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
401 |
) { |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
402 |
debug_assert!(type_index < self.types.len()); |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
403 |
let block = &mut self.blocks[block_index as usize]; |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
404 |
debug_assert!(index < block.elements_count); |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
405 |
|
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
406 |
unsafe { |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
407 |
*(block.component_blocks[type_index].unwrap().as_ptr() as *mut T).add(index as usize) = |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
408 |
value.clone(); |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
409 |
}; |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
410 |
} |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
411 |
|
15354 | 412 |
#[inline] |
15387 | 413 |
fn ensure_block(&mut self, mask: BlockMask) -> u16 { |
15354 | 414 |
if let Some(index) = self |
415 |
.block_masks |
|
416 |
.iter() |
|
417 |
.enumerate() |
|
418 |
.position(|(i, m)| *m == mask && !self.blocks[i].is_full()) |
|
419 |
{ |
|
420 |
index as u16 |
|
421 |
} else { |
|
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
422 |
self.blocks.push(DataBlock::new( |
15387 | 423 |
mask.type_mask, |
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
424 |
&self.element_sizes[0..self.types.len()], |
15426 | 425 |
&self.element_alignments[0..self.types.len()], |
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
426 |
)); |
15358 | 427 |
self.block_masks.push(mask); |
15354 | 428 |
(self.blocks.len() - 1) as u16 |
429 |
} |
|
430 |
} |
|
431 |
||
432 |
pub fn add<T: Clone + 'static>(&mut self, gear_id: GearId, value: &T) { |
|
433 |
if let Some(type_index) = self.get_type_index::<T>() { |
|
15358 | 434 |
let type_bit = 1 << type_index as u64; |
15354 | 435 |
let entry = self.lookup[gear_id.get() as usize - 1]; |
436 |
||
437 |
if let Some(index) = entry.index { |
|
438 |
let mask = self.block_masks[entry.block_index as usize]; |
|
15387 | 439 |
let new_mask = mask.with_type(type_bit); |
15354 | 440 |
|
441 |
if new_mask != mask { |
|
15358 | 442 |
let dest_block_index = self.ensure_block(new_mask); |
15395
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
443 |
let dest_index = self.move_between_blocks( |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
444 |
entry.block_index, |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
445 |
index.get() - 1, |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
446 |
dest_block_index, |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
447 |
); |
a87b5e16b863
save data for blocks with more than a single component
alfadur
parents:
15393
diff
changeset
|
448 |
self.write_component(dest_block_index, dest_index, type_index, value); |
15354 | 449 |
} |
450 |
} else { |
|
15387 | 451 |
let dest_block_index = self.ensure_block(BlockMask::new(type_bit, 0)); |
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
452 |
self.add_to_block(gear_id, dest_block_index, value); |
15354 | 453 |
} |
454 |
} else { |
|
455 |
panic!("Unregistered type") |
|
456 |
} |
|
457 |
} |
|
458 |
||
15387 | 459 |
pub fn add_tag<T: 'static>(&mut self, gear_id: GearId) { |
460 |
if let Some(tag_index) = self.get_tag_index::<T>() { |
|
461 |
let tag_bit = 1 << tag_index as u64; |
|
462 |
let entry = self.lookup[gear_id.get() as usize - 1]; |
|
463 |
||
464 |
if let Some(index) = entry.index { |
|
465 |
let mask = self.block_masks[entry.block_index as usize]; |
|
466 |
let new_mask = mask.with_tag(tag_bit); |
|
467 |
||
468 |
if new_mask != mask { |
|
469 |
let dest_block_index = self.ensure_block(new_mask); |
|
470 |
self.move_between_blocks(entry.block_index, index.get() - 1, dest_block_index); |
|
471 |
} |
|
472 |
} else { |
|
473 |
panic!("Cannot tag a gear with no data") |
|
474 |
} |
|
475 |
} else { |
|
476 |
panic!("Unregistered tag") |
|
477 |
} |
|
478 |
} |
|
479 |
||
15354 | 480 |
pub fn remove<T: 'static>(&mut self, gear_id: GearId) { |
481 |
if let Some(type_index) = self.get_type_index::<T>() { |
|
15942 | 482 |
let type_bit = 1 << type_index as u64; |
15354 | 483 |
let entry = self.lookup[gear_id.get() as usize - 1]; |
15942 | 484 |
|
15354 | 485 |
if let Some(index) = entry.index { |
15942 | 486 |
let mask = self.block_masks[entry.block_index as usize]; |
487 |
let new_mask = mask.without_type(type_bit); |
|
15357 | 488 |
|
15942 | 489 |
if new_mask != mask { |
490 |
if new_mask.type_mask == 0 { |
|
491 |
self.remove_from_block(entry.block_index, index.get() - 1); |
|
492 |
} else { |
|
493 |
let dest_block_index = self.ensure_block(new_mask); |
|
494 |
self.move_between_blocks( |
|
495 |
entry.block_index, |
|
496 |
index.get() - 1, |
|
497 |
dest_block_index, |
|
498 |
); |
|
499 |
} |
|
15357 | 500 |
} |
15354 | 501 |
} |
15357 | 502 |
} else { |
503 |
panic!("Unregistered type") |
|
504 |
} |
|
505 |
} |
|
506 |
||
507 |
pub fn remove_all(&mut self, gear_id: GearId) { |
|
508 |
let entry = self.lookup[gear_id.get() as usize - 1]; |
|
509 |
if let Some(index) = entry.index { |
|
510 |
self.remove_from_block(entry.block_index, index.get() - 1); |
|
15305 | 511 |
} |
512 |
} |
|
513 |
||
514 |
pub fn register<T: 'static>(&mut self) { |
|
15356 | 515 |
debug_assert!(!std::mem::needs_drop::<T>()); |
15829 | 516 |
debug_assert!(size_of::<T>() <= u16::MAX as usize); |
15354 | 517 |
|
15305 | 518 |
let id = TypeId::of::<T>(); |
15387 | 519 |
if size_of::<T>() == 0 { |
520 |
if !self.tags.contains(&id) { |
|
521 |
debug_assert!(self.tags.len() <= 64); |
|
522 |
self.tags.push(id) |
|
523 |
} |
|
524 |
} else { |
|
525 |
if !self.types.contains(&id) { |
|
526 |
debug_assert!(self.types.len() <= 64); |
|
527 |
self.element_sizes[self.types.len()] = size_of::<T>() as u16; |
|
15426 | 528 |
self.element_alignments[self.types.len()] = align_of::<T>() as u8; |
15387 | 529 |
self.types.push(id); |
530 |
} |
|
15305 | 531 |
} |
532 |
} |
|
533 |
||
15392 | 534 |
fn run_impl<T: TypeIter + 'static, F: FnMut(GearId, T)>( |
535 |
&mut self, |
|
536 |
type_selector: u64, |
|
537 |
included_tags: u64, |
|
538 |
type_indices: &[i8], |
|
539 |
mut f: F, |
|
540 |
) { |
|
541 |
let mut slices = vec![null_mut(); type_indices.len() + 1]; |
|
15358 | 542 |
|
15354 | 543 |
for (block_index, mask) in self.block_masks.iter().enumerate() { |
15392 | 544 |
if mask.type_mask & type_selector == type_selector |
545 |
&& mask.tag_mask & included_tags == included_tags |
|
546 |
{ |
|
15379 | 547 |
let block = &mut self.blocks[block_index]; |
548 |
slices[0] = block.data.as_mut_ptr(); |
|
549 |
||
15367
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
550 |
for (arg_index, type_index) in type_indices.iter().cloned().enumerate() { |
15379 | 551 |
slices[arg_index as usize + 1] = block.component_blocks[type_index as usize] |
15368 | 552 |
.unwrap() |
553 |
.as_ptr() |
|
15354 | 554 |
} |
15358 | 555 |
|
556 |
unsafe { |
|
15379 | 557 |
T::iter(&slices[..], block.elements_count as usize, |id, x| f(id, x)); |
15358 | 558 |
} |
15305 | 559 |
} |
560 |
} |
|
561 |
} |
|
15392 | 562 |
|
15829 | 563 |
pub fn get<T: 'static>(&self, gear_id: GearId) -> Option<&T> { |
564 |
let entry = self.lookup[gear_id.get() as usize - 1]; |
|
565 |
match (entry.index, self.get_type_index::<T>()) { |
|
566 |
(Some(index), Some(type_index)) => { |
|
567 |
let block = &self.blocks[entry.block_index as usize]; |
|
568 |
block.component_blocks[type_index].map(|ptr| unsafe { |
|
569 |
&*(ptr.as_ptr() as *const T).add(index.get() as usize - 1) |
|
570 |
}) |
|
571 |
} |
|
572 |
_ => None, |
|
573 |
} |
|
574 |
} |
|
575 |
||
15392 | 576 |
pub fn iter<T: TypeIter + 'static>(&mut self) -> DataIterator<T> { |
577 |
let mut arg_types = Vec::with_capacity(64); |
|
578 |
T::get_types(&mut arg_types); |
|
579 |
let mut type_indices = vec![-1i8; arg_types.len()]; |
|
580 |
let mut selector = 0u64; |
|
581 |
||
582 |
for (arg_index, type_id) in arg_types.iter().enumerate() { |
|
583 |
match self.types.iter().position(|t| t == type_id) { |
|
584 |
Some(i) if selector & (1 << i as u64) != 0 => panic!("Duplicate type"), |
|
585 |
Some(i) => { |
|
586 |
type_indices[arg_index] = i as i8; |
|
587 |
selector |= 1 << i as u64; |
|
588 |
} |
|
589 |
None => panic!("Unregistered type"), |
|
590 |
} |
|
591 |
} |
|
592 |
DataIterator::new(self, selector, type_indices) |
|
593 |
} |
|
594 |
} |
|
595 |
||
596 |
pub struct DataIterator<'a, T> { |
|
597 |
data: &'a mut GearDataManager, |
|
598 |
types: u64, |
|
599 |
type_indices: Vec<i8>, |
|
600 |
tags: u64, |
|
601 |
phantom_types: PhantomData<T>, |
|
602 |
} |
|
603 |
||
604 |
impl<'a, T: TypeIter + 'static> DataIterator<'a, T> { |
|
605 |
fn new( |
|
606 |
data: &'a mut GearDataManager, |
|
607 |
types: u64, |
|
608 |
type_indices: Vec<i8>, |
|
609 |
) -> DataIterator<'a, T> { |
|
610 |
Self { |
|
611 |
data, |
|
612 |
types, |
|
613 |
type_indices, |
|
614 |
tags: 0, |
|
615 |
phantom_types: PhantomData, |
|
616 |
} |
|
617 |
} |
|
618 |
||
619 |
pub fn with_tags<U: TypeTuple + 'static>(self) -> Self { |
|
620 |
let mut tag_types = Vec::with_capacity(64); |
|
621 |
U::get_types(&mut tag_types); |
|
622 |
let mut tags = 0; |
|
623 |
||
624 |
for (i, tag) in self.data.tags.iter().enumerate() { |
|
625 |
if tag_types.contains(tag) { |
|
626 |
tags |= 1 << i as u64; |
|
627 |
} |
|
628 |
} |
|
629 |
Self { tags, ..self } |
|
630 |
} |
|
631 |
||
632 |
#[inline] |
|
633 |
pub fn run<F: FnMut(T)>(&mut self, mut f: F) { |
|
634 |
self.run_id(|_, x| f(x)) |
|
635 |
} |
|
636 |
||
637 |
#[inline] |
|
638 |
pub fn run_id<F: FnMut(GearId, T)>(&mut self, f: F) { |
|
639 |
self.data |
|
640 |
.run_impl(self.types, self.tags, &self.type_indices, f); |
|
641 |
} |
|
15305 | 642 |
} |
15354 | 643 |
|
644 |
#[cfg(test)] |
|
645 |
mod test { |
|
15358 | 646 |
use super::{super::common::GearId, GearDataManager}; |
15354 | 647 |
|
15358 | 648 |
#[derive(Clone)] |
15942 | 649 |
struct DatumA { |
650 |
value: u32, |
|
651 |
} |
|
652 |
||
653 |
#[derive(Clone)] |
|
654 |
struct DatumB { |
|
15354 | 655 |
value: u32, |
656 |
} |
|
657 |
||
15371 | 658 |
#[derive(Clone)] |
15392 | 659 |
struct Tag; |
15371 | 660 |
|
15354 | 661 |
#[test] |
15829 | 662 |
fn direct_access() { |
663 |
let mut manager = GearDataManager::new(); |
|
15942 | 664 |
manager.register::<DatumA>(); |
15829 | 665 |
for i in 1..=5 { |
15942 | 666 |
manager.add(GearId::new(i as u16).unwrap(), &DatumA { value: i * i }); |
15829 | 667 |
} |
668 |
||
669 |
for i in 1..=5 { |
|
670 |
assert_eq!( |
|
671 |
manager |
|
15942 | 672 |
.get::<DatumA>(GearId::new(i as u16).unwrap()) |
15829 | 673 |
.unwrap() |
674 |
.value, |
|
675 |
i * i |
|
676 |
); |
|
677 |
} |
|
678 |
} |
|
679 |
||
680 |
#[test] |
|
15358 | 681 |
fn single_component_iteration() { |
15354 | 682 |
let mut manager = GearDataManager::new(); |
15942 | 683 |
manager.register::<DatumA>(); |
684 |
||
15358 | 685 |
for i in 1..=5 { |
15942 | 686 |
manager.add(GearId::new(i as u16).unwrap(), &DatumA { value: i }); |
15358 | 687 |
} |
688 |
||
689 |
let mut sum = 0; |
|
15942 | 690 |
manager.iter().run(|(d,): (&DatumA,)| sum += d.value); |
15368 | 691 |
assert_eq!(sum, 15); |
15358 | 692 |
|
15942 | 693 |
manager.iter().run(|(d,): (&mut DatumA,)| d.value += 1); |
694 |
manager.iter().run(|(d,): (&DatumA,)| sum += d.value); |
|
15368 | 695 |
assert_eq!(sum, 35); |
15354 | 696 |
} |
15371 | 697 |
|
698 |
#[test] |
|
15392 | 699 |
fn tagged_component_iteration() { |
15371 | 700 |
let mut manager = GearDataManager::new(); |
15942 | 701 |
manager.register::<DatumA>(); |
15371 | 702 |
manager.register::<Tag>(); |
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
703 |
|
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
704 |
for i in 1..=10 { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
705 |
let gear_id = GearId::new(i as u16).unwrap(); |
15942 | 706 |
manager.add(gear_id, &DatumA { value: i }); |
707 |
} |
|
708 |
||
709 |
for i in (2..=10).step_by(2) { |
|
710 |
let gear_id = GearId::new(i as u16).unwrap(); |
|
711 |
manager.add_tag::<Tag>(gear_id); |
|
15371 | 712 |
} |
713 |
||
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
714 |
let mut sum = 0; |
15942 | 715 |
manager.iter().run(|(d,): (&DatumA,)| sum += d.value); |
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
716 |
assert_eq!(sum, 55); |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
717 |
|
15392 | 718 |
let mut tag_sum = 0; |
719 |
manager |
|
720 |
.iter() |
|
721 |
.with_tags::<&Tag>() |
|
15942 | 722 |
.run(|(d,): (&DatumA,)| tag_sum += d.value); |
15392 | 723 |
assert_eq!(tag_sum, 30); |
15371 | 724 |
} |
15942 | 725 |
|
726 |
#[test] |
|
727 |
fn removal() { |
|
728 |
let mut manager = GearDataManager::new(); |
|
729 |
manager.register::<DatumA>(); |
|
730 |
manager.register::<DatumB>(); |
|
731 |
||
732 |
for i in 1..=10 { |
|
733 |
let gear_id = GearId::new(i as u16).unwrap(); |
|
734 |
manager.add(gear_id, &DatumA { value: i }); |
|
735 |
manager.add(gear_id, &DatumB { value: i }); |
|
736 |
} |
|
737 |
||
738 |
for i in (1..=10).step_by(2) { |
|
739 |
let gear_id = GearId::new(i as u16).unwrap(); |
|
740 |
manager.remove::<DatumA>(gear_id); |
|
741 |
} |
|
742 |
||
743 |
let mut sum_a = 0; |
|
744 |
manager.iter().run(|(d,): (&DatumA,)| sum_a += d.value); |
|
745 |
assert_eq!(sum_a, 30); |
|
746 |
||
747 |
let mut sum_b = 0; |
|
748 |
manager.iter().run(|(d,): (&DatumB,)| sum_b += d.value); |
|
749 |
assert_eq!(sum_b, 55); |
|
750 |
} |
|
15354 | 751 |
} |