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