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