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