rust/hwphysics/src/data.rs
author alfadur
Tue, 27 Aug 2019 20:09:17 +0300
changeset 15367 d6b4586b271f
parent 15358 b5e0a39856fd
child 15368 445138f388d4
permissions -rw-r--r--
make sure component slice order corresponds to the type args
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
     1
use super::common::GearId;
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
     2
use std::{
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
     3
    any::TypeId,
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
     4
    mem::{size_of, MaybeUninit},
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
     5
    num::NonZeroU16,
15367
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
     6
    ptr::{copy_nonoverlapping, NonNull, null_mut},
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
     7
    slice,
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
     8
};
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
     9
15356
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
    10
pub unsafe trait TypeTuple: Sized {
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    11
    fn len() -> usize;
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    12
    fn get_types(dest: &mut Vec<TypeId>);
15367
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
    13
    unsafe fn iter<F>(slices: &[*mut u8], count: usize, mut f: F)
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    14
    where
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
    15
        F: FnMut(Self);
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    16
}
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    17
15356
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
    18
unsafe impl<T: 'static> TypeTuple for (&T,) {
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    19
    fn len() -> usize {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    20
        1
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    21
    }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    22
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    23
    fn get_types(dest: &mut Vec<TypeId>) {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    24
        dest.push(TypeId::of::<T>());
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    25
    }
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    26
15367
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
    27
    unsafe fn iter<F>(slices: &[*mut u8], count: usize, mut f: F)
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    28
    where
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
    29
        F: FnMut(Self),
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    30
    {
15367
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
    31
        let slice1 = slice::from_raw_parts(slices[0] as *const T, count);
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    32
        for i in 0..count {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    33
            f((slice1.get_unchecked(i),));
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    34
        }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    35
    }
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    36
}
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    37
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    38
const BLOCK_SIZE: usize = 32768;
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    39
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    40
struct DataBlock {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    41
    max_elements: u16,
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    42
    elements_count: u16,
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    43
    data: Box<[u8; BLOCK_SIZE]>,
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
    44
    component_blocks: [Option<NonNull<u8>>; 64],
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    45
}
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    46
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    47
impl Unpin for DataBlock {}
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    48
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    49
impl DataBlock {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    50
    fn new(mask: u64, element_sizes: &[u16; 64]) -> Self {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    51
        let total_size: u16 = element_sizes
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    52
            .iter()
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    53
            .enumerate()
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
    54
            .filter(|(i, _)| mask & (1 << *i as u64) != 0)
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    55
            .map(|(_, size)| *size)
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    56
            .sum();
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    57
        let max_elements = (BLOCK_SIZE / total_size as usize) as u16;
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    58
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    59
        let mut data: Box<[u8; BLOCK_SIZE]> =
15357
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
    60
            Box::new(unsafe { MaybeUninit::uninit().assume_init() });
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    61
        let mut blocks = [None; 64];
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    62
        let mut offset = 0;
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    63
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    64
        for i in 0..64 {
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
    65
            if mask & (1 << i) != 0 {
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    66
                blocks[i] = Some(NonNull::new(data[offset..].as_mut_ptr()).unwrap());
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    67
                offset += element_sizes[i] as usize * max_elements as usize;
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    68
            }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    69
        }
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    70
        Self {
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    71
            elements_count: 0,
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    72
            max_elements,
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    73
            data,
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
    74
            component_blocks: blocks,
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    75
        }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    76
    }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    77
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    78
    fn is_full(&self) -> bool {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    79
        self.elements_count == self.max_elements
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    80
    }
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    81
}
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    82
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    83
#[derive(Clone, Copy, Debug, Default)]
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    84
pub struct LookupEntry {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    85
    index: Option<NonZeroU16>,
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    86
    block_index: u16,
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    87
}
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    88
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    89
pub struct GearDataManager {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    90
    types: Vec<TypeId>,
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    91
    blocks: Vec<DataBlock>,
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    92
    block_masks: Vec<u64>,
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    93
    element_sizes: Box<[u16; 64]>,
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    94
    lookup: Box<[LookupEntry]>,
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    95
}
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    96
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    97
impl GearDataManager {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    98
    pub fn new() -> Self {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    99
        Self {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   100
            types: vec![],
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   101
            blocks: vec![],
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   102
            block_masks: vec![],
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   103
            element_sizes: Box::new([0; 64]),
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   104
            lookup: vec![LookupEntry::default(); u16::max_value() as usize].into_boxed_slice(),
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   105
        }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   106
    }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   107
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   108
    #[inline]
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   109
    fn get_type_index<T: 'static>(&self) -> Option<usize> {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   110
        let type_id = TypeId::of::<T>();
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   111
        self.types.iter().position(|id| *id == type_id)
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   112
    }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   113
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   114
    fn move_between_blocks(&mut self, from_block_index: u16, from_index: u16, to_block_index: u16) {
15357
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   115
        debug_assert!(from_block_index != to_block_index);
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   116
        let source_mask = self.block_masks[from_block_index as usize];
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   117
        let destination_mask = self.block_masks[to_block_index as usize];
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   118
        debug_assert!(source_mask & destination_mask == source_mask);
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   119
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   120
        let source = &self.blocks[from_block_index as usize];
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   121
        let destination = &self.blocks[to_block_index as usize];
15357
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   122
        debug_assert!(from_index < source.elements_count);
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   123
        debug_assert!(!destination.is_full());
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   124
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   125
        for i in 0..64 {
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   126
            if source_mask & 1 << i != 0 {
15357
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   127
                unsafe {
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   128
                    copy_nonoverlapping(
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   129
                        source.component_blocks[i].unwrap().as_ptr(),
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   130
                        destination.component_blocks[i].unwrap().as_ptr(),
15357
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   131
                        self.element_sizes[i] as usize,
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   132
                    );
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   133
                }
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   134
            }
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   135
        }
15357
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   136
        self.blocks[from_block_index as usize].elements_count -= 1;
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   137
        self.blocks[to_block_index as usize].elements_count += 1;
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   138
    }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   139
15356
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   140
    fn add_to_block<T: Clone>(&mut self, block_index: u16, value: &T) {
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   141
        debug_assert!(self.block_masks[block_index as usize].count_ones() == 1);
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   142
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   143
        let block = &mut self.blocks[block_index as usize];
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   144
        debug_assert!(block.elements_count < block.max_elements);
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   145
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   146
        unsafe {
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   147
            let slice = slice::from_raw_parts_mut(
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   148
                block.data.as_mut_ptr() as *mut T,
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   149
                block.max_elements as usize,
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   150
            );
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   151
            *slice.get_unchecked_mut(block.elements_count as usize) = value.clone();
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   152
        };
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   153
        block.elements_count += 1;
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   154
    }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   155
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   156
    fn remove_from_block(&mut self, block_index: u16, index: u16) {
15356
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   157
        let block = &mut self.blocks[block_index as usize];
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   158
        debug_assert!(index < block.elements_count);
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   159
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   160
        for (i, size) in self.element_sizes.iter().cloned().enumerate() {
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   161
            if index < block.elements_count - 1 {
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   162
                if let Some(ptr) = block.component_blocks[i] {
15356
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   163
                    unsafe {
15357
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   164
                        copy_nonoverlapping(
15356
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   165
                            ptr.as_ptr()
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   166
                                .add((size * (block.elements_count - 1)) as usize),
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   167
                            ptr.as_ptr().add((size * index) as usize),
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   168
                            size as usize,
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   169
                        );
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   170
                    }
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   171
                }
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   172
            }
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   173
        }
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   174
        block.elements_count -= 1;
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   175
    }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   176
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   177
    #[inline]
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   178
    fn ensure_block(&mut self, mask: u64) -> u16 {
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   179
        if let Some(index) = self
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   180
            .block_masks
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   181
            .iter()
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   182
            .enumerate()
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   183
            .position(|(i, m)| *m == mask && !self.blocks[i].is_full())
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   184
        {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   185
            index as u16
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   186
        } else {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   187
            self.blocks.push(DataBlock::new(mask, &self.element_sizes));
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   188
            self.block_masks.push(mask);
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   189
            (self.blocks.len() - 1) as u16
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   190
        }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   191
    }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   192
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   193
    pub fn add<T: Clone + 'static>(&mut self, gear_id: GearId, value: &T) {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   194
        if let Some(type_index) = self.get_type_index::<T>() {
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   195
            let type_bit = 1 << type_index as u64;
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   196
            let entry = self.lookup[gear_id.get() as usize - 1];
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   197
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   198
            if let Some(index) = entry.index {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   199
                let mask = self.block_masks[entry.block_index as usize];
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   200
                let new_mask = mask | type_bit;
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   201
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   202
                if new_mask != mask {
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   203
                    let dest_block_index = self.ensure_block(new_mask);
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   204
                    self.move_between_blocks(entry.block_index, index.get() - 1, dest_block_index);
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   205
                }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   206
            } else {
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   207
                let dest_block_index = self.ensure_block(type_bit);
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   208
                self.add_to_block(dest_block_index, value);
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   209
            }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   210
        } else {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   211
            panic!("Unregistered type")
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   212
        }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   213
    }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   214
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   215
    pub fn remove<T: 'static>(&mut self, gear_id: GearId) {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   216
        if let Some(type_index) = self.get_type_index::<T>() {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   217
            let entry = self.lookup[gear_id.get() as usize - 1];
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   218
            if let Some(index) = entry.index {
15357
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   219
                let destination_mask =
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   220
                    self.block_masks[entry.block_index as usize] & !(1 << type_index as u64);
15357
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   221
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   222
                if destination_mask == 0 {
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   223
                    self.remove_all(gear_id)
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   224
                } else {
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   225
                    let destination_block_index = self.ensure_block(destination_mask);
15357
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   226
                    self.move_between_blocks(
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   227
                        entry.block_index,
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   228
                        index.get() - 1,
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   229
                        destination_block_index,
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   230
                    );
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   231
                }
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   232
            }
15357
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   233
        } else {
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   234
            panic!("Unregistered type")
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   235
        }
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   236
    }
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   237
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   238
    pub fn remove_all(&mut self, gear_id: GearId) {
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   239
        let entry = self.lookup[gear_id.get() as usize - 1];
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   240
        if let Some(index) = entry.index {
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   241
            self.remove_from_block(entry.block_index, index.get() - 1);
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   242
        }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   243
    }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   244
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   245
    pub fn register<T: 'static>(&mut self) {
15356
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   246
        debug_assert!(!std::mem::needs_drop::<T>());
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   247
        debug_assert!(self.types.len() <= 64);
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   248
        debug_assert!(size_of::<T>() <= u16::max_value() as usize);
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   249
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   250
        let id = TypeId::of::<T>();
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   251
        if !self.types.contains(&id) {
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   252
            self.element_sizes[self.types.len()] = size_of::<T>() as u16;
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   253
            self.types.push(id);
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   254
        }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   255
    }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   256
15367
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
   257
    pub fn iter<T: TypeTuple + 'static, F: FnMut(T)>(&self, mut f: F) {
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
   258
        let mut arg_types = Vec::with_capacity(64);
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
   259
        T::get_types(&mut arg_types);
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
   260
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
   261
        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
   262
        let mut selector = 0u64;
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
   263
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
   264
        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
   265
            match self.types.iter().position(|t| t == type_id) {
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
   266
                Some(i) if selector & 1 << i as u64 != 0 => panic!("Duplicate type"),
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
   267
                Some(i) => {
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
   268
                    type_indices[arg_index] = i as i8;
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
   269
                    selector &= 1 << i as u64;
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
   270
                },
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
   271
                None => panic!("Unregistered type")
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   272
            }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   273
        }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   274
15367
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
   275
        let mut slices = vec![null_mut(); arg_types.len()];
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   276
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   277
        for (block_index, mask) in self.block_masks.iter().enumerate() {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   278
            if mask & selector == selector {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   279
                let block = &self.blocks[block_index];
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   280
15367
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
   281
                for (arg_index, type_index) in type_indices.iter().cloned().enumerate() {
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
   282
                    slices[arg_index as usize] =
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
   283
                        block.component_blocks[type_index as usize].unwrap().as_ptr()
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   284
                }
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   285
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   286
                unsafe {
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   287
                    T::iter(&slices[..], block.elements_count as usize, |x| f(x));
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   288
                }
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   289
            }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   290
        }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   291
    }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   292
}
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   293
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   294
#[cfg(test)]
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   295
mod test {
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   296
    use super::{super::common::GearId, GearDataManager};
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   297
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   298
    #[derive(Clone)]
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   299
    struct Datum {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   300
        value: u32,
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   301
    }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   302
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   303
    #[test]
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   304
    fn single_component_iteration() {
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   305
        assert!(std::mem::size_of::<Datum>() > 0);
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   306
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   307
        let mut manager = GearDataManager::new();
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   308
        manager.register::<Datum>();
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   309
        for i in 1..=5 {
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   310
            manager.add(GearId::new(i as u16).unwrap(), &Datum { value: i });
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   311
        }
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   312
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   313
        let mut sum = 0;
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   314
        manager.iter(|(d,): (&Datum,)| sum += d.value);
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   315
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   316
        assert_eq!(sum, 15);
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   317
    }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   318
}