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