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