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