author | alfadur |
Wed, 28 Aug 2019 18:32:13 +0300 | |
changeset 15375 | 37b632d38f14 |
parent 15373 | 5e2b9740086f |
child 15379 | 27915135f87f |
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}, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
5 |
io::Write, |
15354 | 6 |
mem::{size_of, MaybeUninit}, |
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>); |
15368 | 14 |
unsafe fn iter<F: FnMut(Self)>(slices: &[*mut u8], count: usize, mut f: F); |
15305 | 15 |
} |
16 |
||
15369 | 17 |
macro_rules! type_tuple_impl { |
18 |
($($n: literal: $t: ident),+) => { |
|
19 |
impl<$($t: 'static),+> TypeTuple for ($(&$t),+,) { |
|
15368 | 20 |
fn get_types(types: &mut Vec<TypeId>) { |
15369 | 21 |
$(types.push(TypeId::of::<$t>()));+ |
15368 | 22 |
} |
15305 | 23 |
|
15371 | 24 |
unsafe fn iter<F: FnMut(Self)>(slices: &[*mut u8], count: usize, mut f: F) { |
15368 | 25 |
for i in 0..count { |
26 |
unsafe { |
|
15369 | 27 |
f(($(&*(*slices.get_unchecked($n) as *mut $t).add(i)),+,)); |
15368 | 28 |
} |
29 |
} |
|
30 |
} |
|
31 |
} |
|
15354 | 32 |
|
15369 | 33 |
impl<$($t: 'static),+> TypeTuple for ($(&mut $t),+,) { |
15368 | 34 |
fn get_types(types: &mut Vec<TypeId>) { |
15369 | 35 |
$(types.push(TypeId::of::<$t>()));+ |
15368 | 36 |
} |
37 |
||
15371 | 38 |
unsafe fn iter<F: FnMut(Self)>(slices: &[*mut u8], count: usize, mut f: F) { |
15368 | 39 |
for i in 0..count { |
40 |
unsafe { |
|
15369 | 41 |
f(($(&mut *(*slices.get_unchecked($n) as *mut $t).add(i)),+,)); |
15368 | 42 |
} |
43 |
} |
|
44 |
} |
|
15354 | 45 |
} |
46 |
} |
|
15305 | 47 |
} |
48 |
||
15369 | 49 |
type_tuple_impl!(0: A); |
50 |
type_tuple_impl!(0: A, 1: B); |
|
51 |
type_tuple_impl!(0: A, 1: B, 2: C); |
|
52 |
type_tuple_impl!(0: A, 1: B, 2: C, 3: D); |
|
53 |
type_tuple_impl!(0: A, 1: B, 2: C, 3: D, 4: E); |
|
15368 | 54 |
|
15354 | 55 |
const BLOCK_SIZE: usize = 32768; |
56 |
||
57 |
struct DataBlock { |
|
58 |
max_elements: u16, |
|
59 |
elements_count: u16, |
|
60 |
data: Box<[u8; BLOCK_SIZE]>, |
|
15358 | 61 |
component_blocks: [Option<NonNull<u8>>; 64], |
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
62 |
element_sizes: Box<[u16]>, |
15305 | 63 |
} |
64 |
||
15354 | 65 |
impl Unpin for DataBlock {} |
66 |
||
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
67 |
impl Debug for DataBlock { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
68 |
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
69 |
write!( |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
70 |
f, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
71 |
"Block ({}/{}) {{\n", |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
72 |
self.elements_count, self.max_elements |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
73 |
)?; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
74 |
write!(f, "\tIDs: [")?; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
75 |
let id_slice = unsafe { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
76 |
slice::from_raw_parts( |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
77 |
self.data.as_ptr() as *const GearId, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
78 |
self.elements_count as usize, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
79 |
) |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
80 |
}; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
81 |
for gear_id in id_slice { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
82 |
write!(f, "{}, ", gear_id)?; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
83 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
84 |
write!(f, "]\n")?; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
85 |
for type_index in 0..self.element_sizes.len() { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
86 |
if let Some(ptr) = self.component_blocks[type_index] { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
87 |
write!(f, "\tC{}: [", type_index)?; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
88 |
let slice = unsafe { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
89 |
slice::from_raw_parts( |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
90 |
ptr.as_ptr(), |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
91 |
(self.elements_count * self.element_sizes[type_index]) as usize, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
92 |
) |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
93 |
}; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
94 |
for byte in slice { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
95 |
write!(f, "{}, ", byte)?; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
96 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
97 |
write!(f, "]\n")?; |
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 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
100 |
write!(f, "}}\n") |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
101 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
102 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
103 |
|
15354 | 104 |
impl DataBlock { |
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
105 |
fn new(mask: u64, element_sizes: &[u16]) -> Self { |
15354 | 106 |
let total_size: u16 = element_sizes |
107 |
.iter() |
|
108 |
.enumerate() |
|
15358 | 109 |
.filter(|(i, _)| mask & (1 << *i as u64) != 0) |
15354 | 110 |
.map(|(_, size)| *size) |
111 |
.sum(); |
|
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
112 |
let max_elements = (BLOCK_SIZE / (total_size as usize + size_of::<GearId>())) as u16; |
15354 | 113 |
|
114 |
let mut data: Box<[u8; BLOCK_SIZE]> = |
|
15357 | 115 |
Box::new(unsafe { MaybeUninit::uninit().assume_init() }); |
15354 | 116 |
let mut blocks = [None; 64]; |
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
117 |
let mut offset = size_of::<GearId>() * max_elements as usize; |
15354 | 118 |
|
15372 | 119 |
for i in 0..element_sizes.len() { |
120 |
if mask & (1 << i as u64) != 0 { |
|
15354 | 121 |
blocks[i] = Some(NonNull::new(data[offset..].as_mut_ptr()).unwrap()); |
122 |
offset += element_sizes[i] as usize * max_elements as usize; |
|
123 |
} |
|
124 |
} |
|
15305 | 125 |
Self { |
15354 | 126 |
elements_count: 0, |
127 |
max_elements, |
|
128 |
data, |
|
15358 | 129 |
component_blocks: blocks, |
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
130 |
element_sizes: Box::from(element_sizes), |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
131 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
132 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
133 |
|
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
134 |
fn gear_ids(&self) -> &[GearId] { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
135 |
unsafe { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
136 |
slice::from_raw_parts( |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
137 |
self.data.as_ptr() as *const GearId, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
138 |
self.max_elements as usize, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
139 |
) |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
140 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
141 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
142 |
|
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
143 |
fn gear_ids_mut(&mut self) -> &mut [GearId] { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
144 |
unsafe { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
145 |
slice::from_raw_parts_mut( |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
146 |
self.data.as_mut_ptr() as *mut GearId, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
147 |
self.max_elements as usize, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
148 |
) |
15305 | 149 |
} |
150 |
} |
|
151 |
||
15354 | 152 |
fn is_full(&self) -> bool { |
153 |
self.elements_count == self.max_elements |
|
154 |
} |
|
15305 | 155 |
} |
156 |
||
15354 | 157 |
#[derive(Clone, Copy, Debug, Default)] |
158 |
pub struct LookupEntry { |
|
159 |
index: Option<NonZeroU16>, |
|
160 |
block_index: u16, |
|
15305 | 161 |
} |
162 |
||
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
163 |
impl LookupEntry { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
164 |
fn new(block_index: u16, index: u16) -> Self { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
165 |
Self { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
166 |
index: unsafe { Some(NonZeroU16::new_unchecked(index + 1)) }, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
167 |
block_index, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
168 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
169 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
170 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
171 |
|
15305 | 172 |
pub struct GearDataManager { |
173 |
types: Vec<TypeId>, |
|
15354 | 174 |
blocks: Vec<DataBlock>, |
175 |
block_masks: Vec<u64>, |
|
176 |
element_sizes: Box<[u16; 64]>, |
|
177 |
lookup: Box<[LookupEntry]>, |
|
15305 | 178 |
} |
179 |
||
180 |
impl GearDataManager { |
|
181 |
pub fn new() -> Self { |
|
182 |
Self { |
|
183 |
types: vec![], |
|
15354 | 184 |
blocks: vec![], |
185 |
block_masks: vec![], |
|
186 |
element_sizes: Box::new([0; 64]), |
|
187 |
lookup: vec![LookupEntry::default(); u16::max_value() as usize].into_boxed_slice(), |
|
188 |
} |
|
189 |
} |
|
190 |
||
191 |
#[inline] |
|
192 |
fn get_type_index<T: 'static>(&self) -> Option<usize> { |
|
193 |
let type_id = TypeId::of::<T>(); |
|
194 |
self.types.iter().position(|id| *id == type_id) |
|
195 |
} |
|
196 |
||
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
197 |
fn move_between_blocks(&mut self, src_block_index: u16, src_index: u16, dest_block_index: u16) { |
15373 | 198 |
debug_assert!(src_block_index != dest_block_index); |
199 |
let src_mask = self.block_masks[src_block_index as usize]; |
|
200 |
let dest_mask = self.block_masks[dest_block_index as usize]; |
|
201 |
debug_assert!(src_mask & dest_mask == src_mask); |
|
15354 | 202 |
|
15373 | 203 |
let src_block = &self.blocks[src_block_index as usize]; |
204 |
let dest_block = &self.blocks[dest_block_index as usize]; |
|
205 |
debug_assert!(src_index < src_block.elements_count); |
|
206 |
debug_assert!(!dest_block.is_full()); |
|
15357 | 207 |
|
15373 | 208 |
let dest_index = dest_block.elements_count; |
15372 | 209 |
for i in 0..self.types.len() { |
15373 | 210 |
if src_mask & (1 << i as u64) != 0 { |
211 |
let size = self.element_sizes[i]; |
|
212 |
let src_ptr = src_block.component_blocks[i].unwrap().as_ptr(); |
|
213 |
let dest_ptr = dest_block.component_blocks[i].unwrap().as_ptr(); |
|
15357 | 214 |
unsafe { |
215 |
copy_nonoverlapping( |
|
15373 | 216 |
src_ptr.add((src_index * size) as usize), |
217 |
dest_ptr.add((dest_index * size) as usize), |
|
218 |
size as usize, |
|
15357 | 219 |
); |
15373 | 220 |
if src_index < src_block.elements_count - 1 { |
221 |
copy_nonoverlapping( |
|
222 |
src_ptr.add((size * (src_block.elements_count - 1)) as usize), |
|
223 |
src_ptr.add((size * src_index) as usize), |
|
224 |
size as usize, |
|
225 |
); |
|
226 |
} |
|
15357 | 227 |
} |
228 |
} |
|
15354 | 229 |
} |
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
230 |
|
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
231 |
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
|
232 |
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
|
233 |
|
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
234 |
if src_index < src_block.elements_count - 1 { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
235 |
let relocated_index = src_block.elements_count as usize - 1; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
236 |
let gear_ids = src_block.gear_ids_mut(); |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
237 |
let relocated_id = gear_ids[relocated_index]; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
238 |
|
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
239 |
gear_ids[src_index as usize] = relocated_id; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
240 |
self.lookup[relocated_id.get() as usize - 1] = |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
241 |
LookupEntry::new(src_block_index, src_index); |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
242 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
243 |
src_block.elements_count -= 1; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
244 |
|
15373 | 245 |
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
|
246 |
let dest_index = dest_block.elements_count; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
247 |
|
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
248 |
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
|
249 |
self.lookup[gear_id.get() as usize - 1] = LookupEntry::new(dest_block_index, dest_index); |
15373 | 250 |
dest_block.elements_count += 1; |
15354 | 251 |
} |
252 |
||
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
253 |
fn add_to_block<T: Clone>(&mut self, gear_id: GearId, block_index: u16, value: &T) { |
15356 | 254 |
debug_assert!(self.block_masks[block_index as usize].count_ones() == 1); |
255 |
||
256 |
let block = &mut self.blocks[block_index as usize]; |
|
257 |
debug_assert!(block.elements_count < block.max_elements); |
|
258 |
||
259 |
unsafe { |
|
260 |
let slice = slice::from_raw_parts_mut( |
|
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
261 |
block.component_blocks[0].unwrap().as_ptr() as *mut T, |
15356 | 262 |
block.max_elements as usize, |
263 |
); |
|
264 |
*slice.get_unchecked_mut(block.elements_count as usize) = value.clone(); |
|
265 |
}; |
|
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
266 |
|
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
267 |
let index = block.elements_count; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
268 |
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
|
269 |
block.gear_ids_mut()[index as usize] = gear_id; |
15356 | 270 |
block.elements_count += 1; |
15354 | 271 |
} |
272 |
||
273 |
fn remove_from_block(&mut self, block_index: u16, index: u16) { |
|
15356 | 274 |
let block = &mut self.blocks[block_index as usize]; |
275 |
debug_assert!(index < block.elements_count); |
|
276 |
||
277 |
for (i, size) in self.element_sizes.iter().cloned().enumerate() { |
|
278 |
if index < block.elements_count - 1 { |
|
15358 | 279 |
if let Some(ptr) = block.component_blocks[i] { |
15356 | 280 |
unsafe { |
15357 | 281 |
copy_nonoverlapping( |
15356 | 282 |
ptr.as_ptr() |
283 |
.add((size * (block.elements_count - 1)) as usize), |
|
284 |
ptr.as_ptr().add((size * index) as usize), |
|
285 |
size as usize, |
|
286 |
); |
|
287 |
} |
|
288 |
} |
|
289 |
} |
|
290 |
} |
|
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
291 |
|
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
292 |
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
|
293 |
if index < block.elements_count - 1 { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
294 |
let relocated_index = block.elements_count as usize - 1; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
295 |
let gear_ids = block.gear_ids_mut(); |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
296 |
|
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
297 |
gear_ids[index as usize] = gear_ids[relocated_index]; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
298 |
self.lookup[gear_ids[relocated_index].get() as usize - 1] = |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
299 |
LookupEntry::new(block_index, index); |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
300 |
} |
15356 | 301 |
block.elements_count -= 1; |
15354 | 302 |
} |
303 |
||
304 |
#[inline] |
|
15358 | 305 |
fn ensure_block(&mut self, mask: u64) -> u16 { |
15354 | 306 |
if let Some(index) = self |
307 |
.block_masks |
|
308 |
.iter() |
|
309 |
.enumerate() |
|
310 |
.position(|(i, m)| *m == mask && !self.blocks[i].is_full()) |
|
311 |
{ |
|
312 |
index as u16 |
|
313 |
} else { |
|
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
314 |
self.blocks.push(DataBlock::new( |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
315 |
mask, |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
316 |
&self.element_sizes[0..self.types.len()], |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
317 |
)); |
15358 | 318 |
self.block_masks.push(mask); |
15354 | 319 |
(self.blocks.len() - 1) as u16 |
320 |
} |
|
321 |
} |
|
322 |
||
323 |
pub fn add<T: Clone + 'static>(&mut self, gear_id: GearId, value: &T) { |
|
324 |
if let Some(type_index) = self.get_type_index::<T>() { |
|
15358 | 325 |
let type_bit = 1 << type_index as u64; |
15354 | 326 |
let entry = self.lookup[gear_id.get() as usize - 1]; |
327 |
||
328 |
if let Some(index) = entry.index { |
|
329 |
let mask = self.block_masks[entry.block_index as usize]; |
|
330 |
let new_mask = mask | type_bit; |
|
331 |
||
332 |
if new_mask != mask { |
|
15358 | 333 |
let dest_block_index = self.ensure_block(new_mask); |
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
334 |
self.move_between_blocks(entry.block_index, index.get() - 1, dest_block_index); |
15354 | 335 |
} |
336 |
} else { |
|
15358 | 337 |
let dest_block_index = self.ensure_block(type_bit); |
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
338 |
self.add_to_block(gear_id, dest_block_index, value); |
15354 | 339 |
} |
340 |
} else { |
|
341 |
panic!("Unregistered type") |
|
342 |
} |
|
343 |
} |
|
344 |
||
345 |
pub fn remove<T: 'static>(&mut self, gear_id: GearId) { |
|
346 |
if let Some(type_index) = self.get_type_index::<T>() { |
|
347 |
let entry = self.lookup[gear_id.get() as usize - 1]; |
|
348 |
if let Some(index) = entry.index { |
|
15373 | 349 |
let dest_mask = |
15358 | 350 |
self.block_masks[entry.block_index as usize] & !(1 << type_index as u64); |
15357 | 351 |
|
15373 | 352 |
if dest_mask == 0 { |
15357 | 353 |
self.remove_all(gear_id) |
354 |
} else { |
|
15373 | 355 |
let dest_block_index = self.ensure_block(dest_mask); |
356 |
self.move_between_blocks(entry.block_index, index.get() - 1, dest_block_index); |
|
15357 | 357 |
} |
15354 | 358 |
} |
15357 | 359 |
} else { |
360 |
panic!("Unregistered type") |
|
361 |
} |
|
362 |
} |
|
363 |
||
364 |
pub fn remove_all(&mut self, gear_id: GearId) { |
|
365 |
let entry = self.lookup[gear_id.get() as usize - 1]; |
|
366 |
if let Some(index) = entry.index { |
|
367 |
self.remove_from_block(entry.block_index, index.get() - 1); |
|
15305 | 368 |
} |
369 |
} |
|
370 |
||
371 |
pub fn register<T: 'static>(&mut self) { |
|
15356 | 372 |
debug_assert!(!std::mem::needs_drop::<T>()); |
373 |
debug_assert!(self.types.len() <= 64); |
|
374 |
debug_assert!(size_of::<T>() <= u16::max_value() as usize); |
|
15354 | 375 |
|
15305 | 376 |
let id = TypeId::of::<T>(); |
377 |
if !self.types.contains(&id) { |
|
15354 | 378 |
self.element_sizes[self.types.len()] = size_of::<T>() as u16; |
15305 | 379 |
self.types.push(id); |
380 |
} |
|
381 |
} |
|
382 |
||
15367
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
383 |
pub fn iter<T: TypeTuple + 'static, F: FnMut(T)>(&self, mut f: F) { |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
384 |
let mut arg_types = Vec::with_capacity(64); |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
385 |
T::get_types(&mut arg_types); |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
386 |
|
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
387 |
let mut type_indices = vec![-1i8; arg_types.len()]; |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
388 |
let mut selector = 0u64; |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
389 |
|
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
390 |
for (arg_index, type_id) in arg_types.iter().enumerate() { |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
391 |
match self.types.iter().position(|t| t == type_id) { |
15373 | 392 |
Some(i) if selector & (1 << i as u64) != 0 => panic!("Duplicate type"), |
15367
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
393 |
Some(i) => { |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
394 |
type_indices[arg_index] = i as i8; |
15372 | 395 |
selector |= 1 << i as u64; |
15368 | 396 |
} |
397 |
None => panic!("Unregistered type"), |
|
15305 | 398 |
} |
399 |
} |
|
15367
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
400 |
let mut slices = vec![null_mut(); arg_types.len()]; |
15358 | 401 |
|
15354 | 402 |
for (block_index, mask) in self.block_masks.iter().enumerate() { |
403 |
if mask & selector == selector { |
|
404 |
let block = &self.blocks[block_index]; |
|
15372 | 405 |
block.elements_count; |
15367
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
406 |
for (arg_index, type_index) in type_indices.iter().cloned().enumerate() { |
15368 | 407 |
slices[arg_index as usize] = block.component_blocks[type_index as usize] |
408 |
.unwrap() |
|
409 |
.as_ptr() |
|
15354 | 410 |
} |
15358 | 411 |
|
412 |
unsafe { |
|
413 |
T::iter(&slices[..], block.elements_count as usize, |x| f(x)); |
|
414 |
} |
|
15305 | 415 |
} |
416 |
} |
|
417 |
} |
|
418 |
} |
|
15354 | 419 |
|
420 |
#[cfg(test)] |
|
421 |
mod test { |
|
15358 | 422 |
use super::{super::common::GearId, GearDataManager}; |
15354 | 423 |
|
15358 | 424 |
#[derive(Clone)] |
15354 | 425 |
struct Datum { |
426 |
value: u32, |
|
427 |
} |
|
428 |
||
15371 | 429 |
#[derive(Clone)] |
430 |
struct Tag { |
|
431 |
nothing: u8, |
|
432 |
} |
|
433 |
||
15354 | 434 |
#[test] |
15358 | 435 |
fn single_component_iteration() { |
15354 | 436 |
let mut manager = GearDataManager::new(); |
437 |
manager.register::<Datum>(); |
|
15358 | 438 |
for i in 1..=5 { |
439 |
manager.add(GearId::new(i as u16).unwrap(), &Datum { value: i }); |
|
440 |
} |
|
441 |
||
442 |
let mut sum = 0; |
|
443 |
manager.iter(|(d,): (&Datum,)| sum += d.value); |
|
15368 | 444 |
assert_eq!(sum, 15); |
15358 | 445 |
|
15368 | 446 |
manager.iter(|(d,): (&mut Datum,)| d.value += 1); |
447 |
manager.iter(|(d,): (&Datum,)| sum += d.value); |
|
448 |
assert_eq!(sum, 35); |
|
15354 | 449 |
} |
15371 | 450 |
|
451 |
#[test] |
|
452 |
fn multiple_component_iteration() { |
|
453 |
let mut manager = GearDataManager::new(); |
|
454 |
manager.register::<Datum>(); |
|
455 |
manager.register::<Tag>(); |
|
456 |
for i in 1..=10 { |
|
457 |
let gear_id = GearId::new(i as u16).unwrap(); |
|
458 |
manager.add(gear_id, &Datum { value: i }); |
|
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
459 |
} |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
460 |
|
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
461 |
for i in 1..=10 { |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
462 |
let gear_id = GearId::new(i as u16).unwrap(); |
15371 | 463 |
if i & 1 == 0 { |
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
464 |
manager.add(GearId::new(i as u16).unwrap(), &Tag { nothing: 0 }); |
15371 | 465 |
} |
466 |
} |
|
467 |
||
15375
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
468 |
let mut sum = 0; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
469 |
manager.iter(|(d,): (&Datum,)| sum += d.value); |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
470 |
assert_eq!(sum, 55); |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
471 |
|
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
472 |
let mut tag_sum1 = 0; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
473 |
let mut tag_sum2 = 0; |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
474 |
manager.iter(|(d, _): (&Datum, &Tag)| tag_sum1 += d.value); |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
475 |
manager.iter(|(_, d): (&Tag, &Datum)| tag_sum2 += d.value); |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
476 |
assert_eq!(tag_sum1, 30); |
37b632d38f14
properly update gear id lookup on block modifications
alfadur
parents:
15373
diff
changeset
|
477 |
assert_eq!(tag_sum2, tag_sum1); |
15371 | 478 |
} |
15354 | 479 |
} |