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