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