rust/hwphysics/src/data.rs
author alfadur
Sun, 26 Mar 2023 03:53:49 +0300
changeset 15941 8035f7452b48
parent 15829 d5e6c8c92d87
child 15942 bcd43b90401a
permissions -rw-r--r--
optimize type mask iteration
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
     1
use super::common::GearId;
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
     2
use std::{
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
     3
    any::TypeId,
15375
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
     4
    fmt::{Debug, Error, Formatter},
15392
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
     5
    marker::PhantomData,
15426
a027e60d7820 fix alignments of the component slices
alfadur
parents: 15395
diff changeset
     6
    mem::{align_of, size_of, MaybeUninit},
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
     7
    num::NonZeroU16,
15368
445138f388d4 expand iteration implementation to larger tuples
alfadur
parents: 15367
diff changeset
     8
    ptr::{copy_nonoverlapping, null_mut, NonNull},
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
     9
    slice,
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    10
};
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    11
15368
445138f388d4 expand iteration implementation to larger tuples
alfadur
parents: 15367
diff changeset
    12
pub trait TypeTuple: Sized {
15373
5e2b9740086f refill memory when moving out of block
alfadur
parents: 15372
diff changeset
    13
    fn get_types(types: &mut Vec<TypeId>);
15392
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
    14
}
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
    15
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
    16
impl TypeTuple for () {
15393
0ef770a40e75 add wind to physics processor
alfadur
parents: 15392
diff changeset
    17
    fn get_types(_types: &mut Vec<TypeId>) {}
15392
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
    18
}
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
    19
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
    20
impl<T: 'static> TypeTuple for &T {
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
    21
    fn get_types(types: &mut Vec<TypeId>) {
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
    22
        types.push(TypeId::of::<T>());
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
    23
    }
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
    24
}
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
    25
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
    26
pub trait TypeIter: TypeTuple {
15387
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
    27
    unsafe fn iter<F: FnMut(GearId, Self)>(slices: &[*mut u8], count: usize, f: F);
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    28
}
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    29
15369
0f2fd8d12734 fix tupo
alfadur
parents: 15368
diff changeset
    30
macro_rules! type_tuple_impl {
0f2fd8d12734 fix tupo
alfadur
parents: 15368
diff changeset
    31
    ($($n: literal: $t: ident),+) => {
0f2fd8d12734 fix tupo
alfadur
parents: 15368
diff changeset
    32
        impl<$($t: 'static),+> TypeTuple for ($(&$t),+,) {
15368
445138f388d4 expand iteration implementation to larger tuples
alfadur
parents: 15367
diff changeset
    33
            fn get_types(types: &mut Vec<TypeId>) {
15369
0f2fd8d12734 fix tupo
alfadur
parents: 15368
diff changeset
    34
                $(types.push(TypeId::of::<$t>()));+
15368
445138f388d4 expand iteration implementation to larger tuples
alfadur
parents: 15367
diff changeset
    35
            }
15392
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
    36
        }
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    37
15392
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
    38
        impl<$($t: 'static),+> TypeIter for ($(&$t),+,) {
15379
27915135f87f allow iterating with gear id
alfadur
parents: 15375
diff changeset
    39
            unsafe fn iter<F: FnMut(GearId, Self)>(slices: &[*mut u8], count: usize, mut f: F) {
15368
445138f388d4 expand iteration implementation to larger tuples
alfadur
parents: 15367
diff changeset
    40
                for i in 0..count {
15380
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15379
diff changeset
    41
                    f(*(*slices.get_unchecked(0) as *const GearId).add(i),
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15379
diff changeset
    42
                      ($(&*(*slices.get_unchecked($n + 1) as *mut $t).add(i)),+,));
15368
445138f388d4 expand iteration implementation to larger tuples
alfadur
parents: 15367
diff changeset
    43
                }
445138f388d4 expand iteration implementation to larger tuples
alfadur
parents: 15367
diff changeset
    44
            }
445138f388d4 expand iteration implementation to larger tuples
alfadur
parents: 15367
diff changeset
    45
        }
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    46
15369
0f2fd8d12734 fix tupo
alfadur
parents: 15368
diff changeset
    47
        impl<$($t: 'static),+> TypeTuple for ($(&mut $t),+,) {
15368
445138f388d4 expand iteration implementation to larger tuples
alfadur
parents: 15367
diff changeset
    48
            fn get_types(types: &mut Vec<TypeId>) {
15369
0f2fd8d12734 fix tupo
alfadur
parents: 15368
diff changeset
    49
                $(types.push(TypeId::of::<$t>()));+
15368
445138f388d4 expand iteration implementation to larger tuples
alfadur
parents: 15367
diff changeset
    50
            }
15392
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
    51
        }
15368
445138f388d4 expand iteration implementation to larger tuples
alfadur
parents: 15367
diff changeset
    52
15392
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
    53
        impl<$($t: 'static),+> TypeIter for ($(&mut $t),+,) {
15379
27915135f87f allow iterating with gear id
alfadur
parents: 15375
diff changeset
    54
            unsafe fn iter<F: FnMut(GearId, Self)>(slices: &[*mut u8], count: usize, mut f: F) {
15368
445138f388d4 expand iteration implementation to larger tuples
alfadur
parents: 15367
diff changeset
    55
                for i in 0..count {
15380
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15379
diff changeset
    56
                    f(*(*slices.get_unchecked(0) as *const GearId).add(i),
6e3e5be8b2e2 update hwphysics motion to use the new system
alfadur
parents: 15379
diff changeset
    57
                      ($(&mut *(*slices.get_unchecked($n + 1) as *mut $t).add(i)),+,));
15368
445138f388d4 expand iteration implementation to larger tuples
alfadur
parents: 15367
diff changeset
    58
                }
445138f388d4 expand iteration implementation to larger tuples
alfadur
parents: 15367
diff changeset
    59
            }
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    60
        }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    61
    }
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    62
}
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    63
15369
0f2fd8d12734 fix tupo
alfadur
parents: 15368
diff changeset
    64
type_tuple_impl!(0: A);
0f2fd8d12734 fix tupo
alfadur
parents: 15368
diff changeset
    65
type_tuple_impl!(0: A, 1: B);
0f2fd8d12734 fix tupo
alfadur
parents: 15368
diff changeset
    66
type_tuple_impl!(0: A, 1: B, 2: C);
0f2fd8d12734 fix tupo
alfadur
parents: 15368
diff changeset
    67
type_tuple_impl!(0: A, 1: B, 2: C, 3: D);
0f2fd8d12734 fix tupo
alfadur
parents: 15368
diff changeset
    68
type_tuple_impl!(0: A, 1: B, 2: C, 3: D, 4: E);
15368
445138f388d4 expand iteration implementation to larger tuples
alfadur
parents: 15367
diff changeset
    69
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    70
const BLOCK_SIZE: usize = 32768;
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    71
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    72
struct DataBlock {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    73
    max_elements: u16,
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    74
    elements_count: u16,
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    75
    data: Box<[u8; BLOCK_SIZE]>,
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
    76
    component_blocks: [Option<NonNull<u8>>; 64],
15375
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
    77
    element_sizes: Box<[u16]>,
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    78
}
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    79
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    80
impl Unpin for DataBlock {}
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
    81
15375
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
    82
impl Debug for DataBlock {
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
    83
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
    84
        write!(
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
    85
            f,
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
    86
            "Block ({}/{}) {{\n",
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
    87
            self.elements_count, self.max_elements
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
    88
        )?;
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
    89
        write!(f, "\tIDs: [")?;
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
    90
        let id_slice = unsafe {
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
    91
            slice::from_raw_parts(
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
    92
                self.data.as_ptr() as *const GearId,
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
    93
                self.elements_count as usize,
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
    94
            )
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
    95
        };
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
    96
        for gear_id in id_slice {
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
    97
            write!(f, "{}, ", gear_id)?;
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
    98
        }
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
    99
        write!(f, "]\n")?;
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   100
        for type_index in 0..self.element_sizes.len() {
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   101
            if let Some(ptr) = self.component_blocks[type_index] {
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   102
                write!(f, "\tC{}: [", type_index)?;
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   103
                let slice = unsafe {
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   104
                    slice::from_raw_parts(
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   105
                        ptr.as_ptr(),
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   106
                        (self.elements_count * self.element_sizes[type_index]) as usize,
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   107
                    )
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   108
                };
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   109
                for byte in slice {
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   110
                    write!(f, "{}, ", byte)?;
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   111
                }
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   112
                write!(f, "]\n")?;
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   113
            }
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   114
        }
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   115
        write!(f, "}}\n")
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   116
    }
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   117
}
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   118
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   119
impl DataBlock {
15426
a027e60d7820 fix alignments of the component slices
alfadur
parents: 15395
diff changeset
   120
    fn new(mask: u64, element_sizes: &[u16], element_alignments: &[u8]) -> Self {
a027e60d7820 fix alignments of the component slices
alfadur
parents: 15395
diff changeset
   121
        let total_padding: usize = element_alignments.iter().map(|x| *x as usize).sum();
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   122
        let total_size: u16 = element_sizes
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   123
            .iter()
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   124
            .enumerate()
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   125
            .filter(|(i, _)| mask & (1 << *i as u64) != 0)
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   126
            .map(|(_, size)| *size)
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   127
            .sum();
15426
a027e60d7820 fix alignments of the component slices
alfadur
parents: 15395
diff changeset
   128
        let max_elements =
a027e60d7820 fix alignments of the component slices
alfadur
parents: 15395
diff changeset
   129
            ((BLOCK_SIZE - total_padding) / (total_size as usize + size_of::<GearId>())) as u16;
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   130
15426
a027e60d7820 fix alignments of the component slices
alfadur
parents: 15395
diff changeset
   131
        //ensure the block memory is aligned to GearId
a027e60d7820 fix alignments of the component slices
alfadur
parents: 15395
diff changeset
   132
        let tmp_data: Box<[GearId; BLOCK_SIZE / size_of::<GearId>()]> =
a027e60d7820 fix alignments of the component slices
alfadur
parents: 15395
diff changeset
   133
            Box::new(unsafe { MaybeUninit::uninit().assume_init() });
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   134
        let mut data: Box<[u8; BLOCK_SIZE]> =
15426
a027e60d7820 fix alignments of the component slices
alfadur
parents: 15395
diff changeset
   135
            unsafe { Box::from_raw(Box::into_raw(tmp_data) as *mut [u8; BLOCK_SIZE]) };
a027e60d7820 fix alignments of the component slices
alfadur
parents: 15395
diff changeset
   136
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   137
        let mut blocks = [None; 64];
15426
a027e60d7820 fix alignments of the component slices
alfadur
parents: 15395
diff changeset
   138
        let mut address = unsafe {
a027e60d7820 fix alignments of the component slices
alfadur
parents: 15395
diff changeset
   139
            data.as_mut_ptr()
a027e60d7820 fix alignments of the component slices
alfadur
parents: 15395
diff changeset
   140
                .add(size_of::<GearId>() * max_elements as usize)
a027e60d7820 fix alignments of the component slices
alfadur
parents: 15395
diff changeset
   141
        };
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   142
15941
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   143
        let mut mask_bits = mask;
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   144
        while mask_bits != 0 {
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   145
            let i = mask_bits.trailing_zeros() as usize;
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   146
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   147
            unsafe {
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   148
                address = address.add(address.align_offset(element_alignments[i] as usize));
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   149
                blocks[i] = Some(NonNull::new_unchecked(address));
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   150
                address = address.add(element_sizes[i] as usize * max_elements as usize)
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   151
            };
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   152
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   153
            mask_bits &= mask_bits - 1;
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   154
        }
15426
a027e60d7820 fix alignments of the component slices
alfadur
parents: 15395
diff changeset
   155
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   156
        Self {
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   157
            elements_count: 0,
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   158
            max_elements,
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   159
            data,
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   160
            component_blocks: blocks,
15375
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   161
            element_sizes: Box::from(element_sizes),
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   162
        }
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   163
    }
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   164
15941
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   165
    #[inline]
15375
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   166
    fn gear_ids(&self) -> &[GearId] {
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   167
        unsafe {
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   168
            slice::from_raw_parts(
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   169
                self.data.as_ptr() as *const GearId,
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   170
                self.max_elements as usize,
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   171
            )
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   172
        }
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   173
    }
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   174
15941
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   175
    #[inline]
15375
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   176
    fn gear_ids_mut(&mut self) -> &mut [GearId] {
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   177
        unsafe {
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   178
            slice::from_raw_parts_mut(
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   179
                self.data.as_mut_ptr() as *mut GearId,
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   180
                self.max_elements as usize,
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   181
            )
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   182
        }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   183
    }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   184
15941
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   185
    #[inline]
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   186
    fn is_full(&self) -> bool {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   187
        self.elements_count == self.max_elements
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   188
    }
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   189
}
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   190
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   191
#[derive(Clone, Copy, Debug, Default)]
15379
27915135f87f allow iterating with gear id
alfadur
parents: 15375
diff changeset
   192
struct LookupEntry {
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   193
    index: Option<NonZeroU16>,
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   194
    block_index: u16,
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   195
}
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   196
15375
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   197
impl LookupEntry {
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   198
    fn new(block_index: u16, index: u16) -> Self {
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   199
        Self {
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   200
            index: unsafe { Some(NonZeroU16::new_unchecked(index + 1)) },
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   201
            block_index,
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   202
        }
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   203
    }
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   204
}
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   205
15387
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   206
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   207
struct BlockMask {
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   208
    type_mask: u64,
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   209
    tag_mask: u64,
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   210
}
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   211
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   212
impl BlockMask {
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   213
    #[inline]
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   214
    fn new(type_mask: u64, tag_mask: u64) -> Self {
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   215
        Self {
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   216
            type_mask,
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   217
            tag_mask,
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   218
        }
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   219
    }
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   220
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   221
    #[inline]
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   222
    fn with_type(&self, type_bit: u64) -> Self {
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   223
        Self::new(self.type_mask | type_bit, self.tag_mask)
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   224
    }
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   225
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   226
    #[inline]
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   227
    fn with_tag(&self, tag_bit: u64) -> Self {
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   228
        Self::new(self.type_mask, self.tag_mask | tag_bit)
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   229
    }
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   230
}
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   231
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   232
pub struct GearDataManager {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   233
    types: Vec<TypeId>,
15387
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   234
    tags: Vec<TypeId>,
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   235
    blocks: Vec<DataBlock>,
15387
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   236
    block_masks: Vec<BlockMask>,
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   237
    element_sizes: Box<[u16; 64]>,
15426
a027e60d7820 fix alignments of the component slices
alfadur
parents: 15395
diff changeset
   238
    element_alignments: Box<[u8; 64]>,
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   239
    lookup: Box<[LookupEntry]>,
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   240
}
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   241
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   242
impl GearDataManager {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   243
    pub fn new() -> Self {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   244
        Self {
15387
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   245
            types: Vec::with_capacity(64),
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   246
            tags: Vec::with_capacity(64),
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   247
            blocks: vec![],
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   248
            block_masks: vec![],
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   249
            element_sizes: Box::new([0; 64]),
15426
a027e60d7820 fix alignments of the component slices
alfadur
parents: 15395
diff changeset
   250
            element_alignments: Box::new([0; 64]),
15829
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   251
            lookup: vec![LookupEntry::default(); u16::MAX as usize].into_boxed_slice(),
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   252
        }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   253
    }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   254
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   255
    #[inline]
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   256
    fn get_type_index<T: 'static>(&self) -> Option<usize> {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   257
        let type_id = TypeId::of::<T>();
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   258
        self.types.iter().position(|id| *id == type_id)
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   259
    }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   260
15387
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   261
    #[inline]
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   262
    fn get_tag_index<T: 'static>(&self) -> Option<usize> {
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   263
        let type_id = TypeId::of::<T>();
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   264
        self.tags.iter().position(|id| *id == type_id)
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   265
    }
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   266
15395
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   267
    fn move_between_blocks(
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   268
        &mut self,
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   269
        src_block_index: u16,
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   270
        src_index: u16,
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   271
        dest_block_index: u16,
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   272
    ) -> u16 {
15373
5e2b9740086f refill memory when moving out of block
alfadur
parents: 15372
diff changeset
   273
        debug_assert!(src_block_index != dest_block_index);
5e2b9740086f refill memory when moving out of block
alfadur
parents: 15372
diff changeset
   274
        let src_mask = self.block_masks[src_block_index as usize];
5e2b9740086f refill memory when moving out of block
alfadur
parents: 15372
diff changeset
   275
        let dest_mask = self.block_masks[dest_block_index as usize];
15387
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   276
        debug_assert!(src_mask.type_mask & dest_mask.type_mask == src_mask.type_mask);
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   277
15373
5e2b9740086f refill memory when moving out of block
alfadur
parents: 15372
diff changeset
   278
        let src_block = &self.blocks[src_block_index as usize];
5e2b9740086f refill memory when moving out of block
alfadur
parents: 15372
diff changeset
   279
        let dest_block = &self.blocks[dest_block_index as usize];
5e2b9740086f refill memory when moving out of block
alfadur
parents: 15372
diff changeset
   280
        debug_assert!(src_index < src_block.elements_count);
5e2b9740086f refill memory when moving out of block
alfadur
parents: 15372
diff changeset
   281
        debug_assert!(!dest_block.is_full());
15357
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   282
15373
5e2b9740086f refill memory when moving out of block
alfadur
parents: 15372
diff changeset
   283
        let dest_index = dest_block.elements_count;
15941
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   284
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   285
        let mut type_mask = src_mask.type_mask;
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   286
        while type_mask != 0 {
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   287
            let i = type_mask.trailing_zeros() as usize;
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   288
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   289
            let size = self.element_sizes[i];
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   290
            let src_ptr = src_block.component_blocks[i].unwrap().as_ptr();
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   291
            let dest_ptr = dest_block.component_blocks[i].unwrap().as_ptr();
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   292
            unsafe {
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   293
                copy_nonoverlapping(
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   294
                    src_ptr.add((src_index * size) as usize),
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   295
                    dest_ptr.add((dest_index * size) as usize),
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   296
                    size as usize,
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   297
                );
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   298
                if src_index < src_block.elements_count - 1 {
15357
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   299
                    copy_nonoverlapping(
15941
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   300
                        src_ptr.add((size * (src_block.elements_count - 1)) as usize),
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   301
                        src_ptr.add((size * src_index) as usize),
15373
5e2b9740086f refill memory when moving out of block
alfadur
parents: 15372
diff changeset
   302
                        size as usize,
15357
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   303
                    );
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   304
                }
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   305
            }
15941
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   306
8035f7452b48 optimize type mask iteration
alfadur
parents: 15829
diff changeset
   307
            type_mask &= type_mask - 1;
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   308
        }
15375
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   309
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   310
        let src_block = &mut self.blocks[src_block_index as usize];
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   311
        let gear_id = src_block.gear_ids()[src_index as usize];
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   312
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   313
        if src_index < src_block.elements_count - 1 {
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   314
            let relocated_index = src_block.elements_count as usize - 1;
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   315
            let gear_ids = src_block.gear_ids_mut();
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   316
            let relocated_id = gear_ids[relocated_index];
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   317
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   318
            gear_ids[src_index as usize] = relocated_id;
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   319
            self.lookup[relocated_id.get() as usize - 1] =
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   320
                LookupEntry::new(src_block_index, src_index);
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   321
        }
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   322
        src_block.elements_count -= 1;
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   323
15373
5e2b9740086f refill memory when moving out of block
alfadur
parents: 15372
diff changeset
   324
        let dest_block = &mut self.blocks[dest_block_index as usize];
15375
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   325
        let dest_index = dest_block.elements_count;
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   326
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   327
        dest_block.gear_ids_mut()[dest_index as usize] = gear_id;
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   328
        self.lookup[gear_id.get() as usize - 1] = LookupEntry::new(dest_block_index, dest_index);
15373
5e2b9740086f refill memory when moving out of block
alfadur
parents: 15372
diff changeset
   329
        dest_block.elements_count += 1;
15395
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   330
        dest_block.elements_count - 1
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   331
    }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   332
15375
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   333
    fn add_to_block<T: Clone>(&mut self, gear_id: GearId, block_index: u16, value: &T) {
15387
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   334
        debug_assert!(
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   335
            self.block_masks[block_index as usize]
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   336
                .type_mask
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   337
                .count_ones()
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   338
                == 1
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   339
        );
15356
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   340
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   341
        let block = &mut self.blocks[block_index as usize];
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   342
        debug_assert!(block.elements_count < block.max_elements);
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   343
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   344
        unsafe {
15395
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   345
            *(block.component_blocks[0].unwrap().as_ptr() as *mut T)
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   346
                .add(block.elements_count as usize) = value.clone();
15356
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   347
        };
15375
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   348
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   349
        let index = block.elements_count;
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   350
        self.lookup[gear_id.get() as usize - 1] = LookupEntry::new(block_index, index);
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   351
        block.gear_ids_mut()[index as usize] = gear_id;
15356
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   352
        block.elements_count += 1;
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   353
    }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   355
    fn remove_from_block(&mut self, block_index: u16, index: u16) {
15356
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   356
        let block = &mut self.blocks[block_index as usize];
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   357
        debug_assert!(index < block.elements_count);
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   358
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   359
        for (i, size) in self.element_sizes.iter().cloned().enumerate() {
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   360
            if index < block.elements_count - 1 {
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   361
                if let Some(ptr) = block.component_blocks[i] {
15356
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   362
                    unsafe {
15357
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   363
                        copy_nonoverlapping(
15356
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   364
                            ptr.as_ptr()
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   365
                                .add((size * (block.elements_count - 1)) as usize),
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   366
                            ptr.as_ptr().add((size * index) as usize),
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   367
                            size as usize,
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   368
                        );
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   369
                    }
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   370
                }
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   371
            }
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   372
        }
15375
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   373
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   374
        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: 15373
diff changeset
   375
        if index < block.elements_count - 1 {
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   376
            let relocated_index = block.elements_count as usize - 1;
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   377
            let gear_ids = block.gear_ids_mut();
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   378
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   379
            gear_ids[index as usize] = gear_ids[relocated_index];
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   380
            self.lookup[gear_ids[relocated_index].get() as usize - 1] =
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   381
                LookupEntry::new(block_index, index);
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   382
        }
15356
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   383
        block.elements_count -= 1;
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   384
    }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   385
15395
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   386
    fn write_component<T: Clone>(
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   387
        &mut self,
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   388
        block_index: u16,
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   389
        index: u16,
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   390
        type_index: usize,
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   391
        value: &T,
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   392
    ) {
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   393
        debug_assert!(type_index < self.types.len());
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   394
        let block = &mut self.blocks[block_index as usize];
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   395
        debug_assert!(index < block.elements_count);
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   396
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   397
        unsafe {
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   398
            *(block.component_blocks[type_index].unwrap().as_ptr() as *mut T).add(index as usize) =
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   399
                value.clone();
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   400
        };
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   401
    }
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   402
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   403
    #[inline]
15387
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   404
    fn ensure_block(&mut self, mask: BlockMask) -> u16 {
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   405
        if let Some(index) = self
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   406
            .block_masks
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   407
            .iter()
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   408
            .enumerate()
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   409
            .position(|(i, m)| *m == mask && !self.blocks[i].is_full())
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   410
        {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   411
            index as u16
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   412
        } else {
15375
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   413
            self.blocks.push(DataBlock::new(
15387
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   414
                mask.type_mask,
15375
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   415
                &self.element_sizes[0..self.types.len()],
15426
a027e60d7820 fix alignments of the component slices
alfadur
parents: 15395
diff changeset
   416
                &self.element_alignments[0..self.types.len()],
15375
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   417
            ));
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   418
            self.block_masks.push(mask);
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   419
            (self.blocks.len() - 1) as u16
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   420
        }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   421
    }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   422
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   423
    pub fn add<T: Clone + 'static>(&mut self, gear_id: GearId, value: &T) {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   424
        if let Some(type_index) = self.get_type_index::<T>() {
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   425
            let type_bit = 1 << type_index as u64;
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   426
            let entry = self.lookup[gear_id.get() as usize - 1];
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   427
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   428
            if let Some(index) = entry.index {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   429
                let mask = self.block_masks[entry.block_index as usize];
15387
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   430
                let new_mask = mask.with_type(type_bit);
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   431
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   432
                if new_mask != mask {
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   433
                    let dest_block_index = self.ensure_block(new_mask);
15395
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   434
                    let dest_index = self.move_between_blocks(
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   435
                        entry.block_index,
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   436
                        index.get() - 1,
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   437
                        dest_block_index,
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   438
                    );
a87b5e16b863 save data for blocks with more than a single component
alfadur
parents: 15393
diff changeset
   439
                    self.write_component(dest_block_index, dest_index, type_index, value);
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   440
                }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   441
            } else {
15387
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   442
                let dest_block_index = self.ensure_block(BlockMask::new(type_bit, 0));
15375
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   443
                self.add_to_block(gear_id, dest_block_index, value);
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   444
            }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   445
        } else {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   446
            panic!("Unregistered type")
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   447
        }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   448
    }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   449
15387
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   450
    pub fn add_tag<T: 'static>(&mut self, gear_id: GearId) {
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   451
        if let Some(tag_index) = self.get_tag_index::<T>() {
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   452
            let tag_bit = 1 << tag_index as u64;
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   453
            let entry = self.lookup[gear_id.get() as usize - 1];
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   454
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   455
            if let Some(index) = entry.index {
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   456
                let mask = self.block_masks[entry.block_index as usize];
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   457
                let new_mask = mask.with_tag(tag_bit);
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   458
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   459
                if new_mask != mask {
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   460
                    let dest_block_index = self.ensure_block(new_mask);
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   461
                    self.move_between_blocks(entry.block_index, index.get() - 1, dest_block_index);
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   462
                }
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   463
            } else {
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   464
                panic!("Cannot tag a gear with no data")
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   465
            }
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   466
        } else {
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   467
            panic!("Unregistered tag")
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   468
        }
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   469
    }
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   470
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   471
    pub fn remove<T: 'static>(&mut self, gear_id: GearId) {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   472
        if let Some(type_index) = self.get_type_index::<T>() {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   473
            let entry = self.lookup[gear_id.get() as usize - 1];
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   474
            if let Some(index) = entry.index {
15387
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   475
                let mut dest_mask = self.block_masks[entry.block_index as usize];
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   476
                dest_mask.type_mask &= !(1 << type_index as u64);
15357
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   477
15387
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   478
                if dest_mask.type_mask == 0 {
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   479
                    self.remove_from_block(entry.block_index, index.get() - 1);
15357
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   480
                } else {
15373
5e2b9740086f refill memory when moving out of block
alfadur
parents: 15372
diff changeset
   481
                    let dest_block_index = self.ensure_block(dest_mask);
5e2b9740086f refill memory when moving out of block
alfadur
parents: 15372
diff changeset
   482
                    self.move_between_blocks(entry.block_index, index.get() - 1, dest_block_index);
15357
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   483
                }
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   484
            }
15357
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   485
        } else {
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   486
            panic!("Unregistered type")
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   487
        }
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   488
    }
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   489
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   490
    pub fn remove_all(&mut self, gear_id: GearId) {
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   491
        let entry = self.lookup[gear_id.get() as usize - 1];
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   492
        if let Some(index) = entry.index {
135c29237a56 implement moving between blocks
alfadur
parents: 15356
diff changeset
   493
            self.remove_from_block(entry.block_index, index.get() - 1);
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   494
        }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   495
    }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   496
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   497
    pub fn register<T: 'static>(&mut self) {
15356
277acc9f9fcf implement addition to/removal from blocks
alfadur
parents: 15354
diff changeset
   498
        debug_assert!(!std::mem::needs_drop::<T>());
15829
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   499
        debug_assert!(size_of::<T>() <= u16::MAX as usize);
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   500
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   501
        let id = TypeId::of::<T>();
15387
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   502
        if size_of::<T>() == 0 {
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   503
            if !self.tags.contains(&id) {
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   504
                debug_assert!(self.tags.len() <= 64);
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   505
                self.tags.push(id)
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   506
            }
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   507
        } else {
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   508
            if !self.types.contains(&id) {
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   509
                debug_assert!(self.types.len() <= 64);
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   510
                self.element_sizes[self.types.len()] = size_of::<T>() as u16;
15426
a027e60d7820 fix alignments of the component slices
alfadur
parents: 15395
diff changeset
   511
                self.element_alignments[self.types.len()] = align_of::<T>() as u8;
15387
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   512
                self.types.push(id);
90a79670de52 allow registering zero-sized types
alfadur
parents: 15381
diff changeset
   513
            }
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   514
        }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   515
    }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   516
15392
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   517
    fn run_impl<T: TypeIter + 'static, F: FnMut(GearId, T)>(
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   518
        &mut self,
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   519
        type_selector: u64,
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   520
        included_tags: u64,
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   521
        type_indices: &[i8],
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   522
        mut f: F,
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   523
    ) {
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   524
        let mut slices = vec![null_mut(); type_indices.len() + 1];
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   525
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   526
        for (block_index, mask) in self.block_masks.iter().enumerate() {
15392
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   527
            if mask.type_mask & type_selector == type_selector
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   528
                && mask.tag_mask & included_tags == included_tags
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   529
            {
15379
27915135f87f allow iterating with gear id
alfadur
parents: 15375
diff changeset
   530
                let block = &mut self.blocks[block_index];
27915135f87f allow iterating with gear id
alfadur
parents: 15375
diff changeset
   531
                slices[0] = block.data.as_mut_ptr();
27915135f87f allow iterating with gear id
alfadur
parents: 15375
diff changeset
   532
15367
d6b4586b271f make sure component slice order corresponds to the type args
alfadur
parents: 15358
diff changeset
   533
                for (arg_index, type_index) in type_indices.iter().cloned().enumerate() {
15379
27915135f87f allow iterating with gear id
alfadur
parents: 15375
diff changeset
   534
                    slices[arg_index as usize + 1] = block.component_blocks[type_index as usize]
15368
445138f388d4 expand iteration implementation to larger tuples
alfadur
parents: 15367
diff changeset
   535
                        .unwrap()
445138f388d4 expand iteration implementation to larger tuples
alfadur
parents: 15367
diff changeset
   536
                        .as_ptr()
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   537
                }
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   538
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   539
                unsafe {
15379
27915135f87f allow iterating with gear id
alfadur
parents: 15375
diff changeset
   540
                    T::iter(&slices[..], block.elements_count as usize, |id, x| f(id, x));
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   541
                }
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   542
            }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   543
        }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   544
    }
15392
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   545
15829
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   546
    pub fn get<T: 'static>(&self, gear_id: GearId) -> Option<&T> {
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   547
        let entry = self.lookup[gear_id.get() as usize - 1];
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   548
        match (entry.index, self.get_type_index::<T>()) {
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   549
            (Some(index), Some(type_index)) => {
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   550
                let block = &self.blocks[entry.block_index as usize];
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   551
                block.component_blocks[type_index].map(|ptr| unsafe {
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   552
                    &*(ptr.as_ptr() as *const T).add(index.get() as usize - 1)
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   553
                })
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   554
            }
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   555
            _ => None,
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   556
        }
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   557
    }
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   558
15392
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   559
    pub fn iter<T: TypeIter + 'static>(&mut self) -> DataIterator<T> {
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   560
        let mut arg_types = Vec::with_capacity(64);
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   561
        T::get_types(&mut arg_types);
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   562
        let mut type_indices = vec![-1i8; arg_types.len()];
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   563
        let mut selector = 0u64;
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   564
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   565
        for (arg_index, type_id) in arg_types.iter().enumerate() {
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   566
            match self.types.iter().position(|t| t == type_id) {
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   567
                Some(i) if selector & (1 << i as u64) != 0 => panic!("Duplicate type"),
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   568
                Some(i) => {
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   569
                    type_indices[arg_index] = i as i8;
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   570
                    selector |= 1 << i as u64;
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   571
                }
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   572
                None => panic!("Unregistered type"),
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   573
            }
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   574
        }
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   575
        DataIterator::new(self, selector, type_indices)
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   576
    }
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   577
}
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   578
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   579
pub struct DataIterator<'a, T> {
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   580
    data: &'a mut GearDataManager,
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   581
    types: u64,
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   582
    type_indices: Vec<i8>,
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   583
    tags: u64,
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   584
    phantom_types: PhantomData<T>,
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   585
}
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   586
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   587
impl<'a, T: TypeIter + 'static> DataIterator<'a, T> {
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   588
    fn new(
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   589
        data: &'a mut GearDataManager,
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   590
        types: u64,
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   591
        type_indices: Vec<i8>,
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   592
    ) -> DataIterator<'a, T> {
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   593
        Self {
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   594
            data,
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   595
            types,
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   596
            type_indices,
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   597
            tags: 0,
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   598
            phantom_types: PhantomData,
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   599
        }
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   600
    }
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   601
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   602
    pub fn with_tags<U: TypeTuple + 'static>(self) -> Self {
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   603
        let mut tag_types = Vec::with_capacity(64);
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   604
        U::get_types(&mut tag_types);
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   605
        let mut tags = 0;
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   606
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   607
        for (i, tag) in self.data.tags.iter().enumerate() {
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   608
            if tag_types.contains(tag) {
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   609
                tags |= 1 << i as u64;
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   610
            }
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   611
        }
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   612
        Self { tags, ..self }
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   613
    }
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   614
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   615
    #[inline]
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   616
    pub fn run<F: FnMut(T)>(&mut self, mut f: F) {
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   617
        self.run_id(|_, x| f(x))
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   618
    }
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   619
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   620
    #[inline]
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   621
    pub fn run_id<F: FnMut(GearId, T)>(&mut self, f: F) {
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   622
        self.data
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   623
            .run_impl(self.types, self.tags, &self.type_indices, f);
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   624
    }
15305
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
   625
}
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   626
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   627
#[cfg(test)]
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   628
mod test {
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   629
    use super::{super::common::GearId, GearDataManager};
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   630
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   631
    #[derive(Clone)]
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   632
    struct Datum {
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   633
        value: u32,
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   634
    }
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   635
15371
24a9afbf33c6 add multicomponent iteration test
alfadur
parents: 15369
diff changeset
   636
    #[derive(Clone)]
15392
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   637
    struct Tag;
15371
24a9afbf33c6 add multicomponent iteration test
alfadur
parents: 15369
diff changeset
   638
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   639
    #[test]
15829
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   640
    fn direct_access() {
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   641
        let mut manager = GearDataManager::new();
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   642
        manager.register::<Datum>();
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   643
        for i in 1..=5 {
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   644
            manager.add(GearId::new(i as u16).unwrap(), &Datum { value: i * i });
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   645
        }
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   646
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   647
        for i in 1..=5 {
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   648
            assert_eq!(
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   649
                manager
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   650
                    .get::<Datum>(GearId::new(i as u16).unwrap())
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   651
                    .unwrap()
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   652
                    .value,
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   653
                i * i
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   654
            );
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   655
        }
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   656
    }
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   657
d5e6c8c92d87 add direct access to gear data
alfadur
parents: 15426
diff changeset
   658
    #[test]
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   659
    fn single_component_iteration() {
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   660
        let mut manager = GearDataManager::new();
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   661
        manager.register::<Datum>();
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   662
        for i in 1..=5 {
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   663
            manager.add(GearId::new(i as u16).unwrap(), &Datum { value: i });
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   664
        }
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   665
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   666
        let mut sum = 0;
15392
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   667
        manager.iter().run(|(d,): (&Datum,)| sum += d.value);
15368
445138f388d4 expand iteration implementation to larger tuples
alfadur
parents: 15367
diff changeset
   668
        assert_eq!(sum, 15);
15358
b5e0a39856fd complete basic ecs
alfadur
parents: 15357
diff changeset
   669
15392
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   670
        manager.iter().run(|(d,): (&mut Datum,)| d.value += 1);
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   671
        manager.iter().run(|(d,): (&Datum,)| sum += d.value);
15368
445138f388d4 expand iteration implementation to larger tuples
alfadur
parents: 15367
diff changeset
   672
        assert_eq!(sum, 35);
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   673
    }
15371
24a9afbf33c6 add multicomponent iteration test
alfadur
parents: 15369
diff changeset
   674
24a9afbf33c6 add multicomponent iteration test
alfadur
parents: 15369
diff changeset
   675
    #[test]
15392
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   676
    fn tagged_component_iteration() {
15371
24a9afbf33c6 add multicomponent iteration test
alfadur
parents: 15369
diff changeset
   677
        let mut manager = GearDataManager::new();
24a9afbf33c6 add multicomponent iteration test
alfadur
parents: 15369
diff changeset
   678
        manager.register::<Datum>();
24a9afbf33c6 add multicomponent iteration test
alfadur
parents: 15369
diff changeset
   679
        manager.register::<Tag>();
24a9afbf33c6 add multicomponent iteration test
alfadur
parents: 15369
diff changeset
   680
        for i in 1..=10 {
24a9afbf33c6 add multicomponent iteration test
alfadur
parents: 15369
diff changeset
   681
            let gear_id = GearId::new(i as u16).unwrap();
24a9afbf33c6 add multicomponent iteration test
alfadur
parents: 15369
diff changeset
   682
            manager.add(gear_id, &Datum { value: i });
15375
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   683
        }
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   684
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   685
        for i in 1..=10 {
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   686
            let gear_id = GearId::new(i as u16).unwrap();
15371
24a9afbf33c6 add multicomponent iteration test
alfadur
parents: 15369
diff changeset
   687
            if i & 1 == 0 {
15392
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   688
                manager.add_tag::<Tag>(gear_id);
15371
24a9afbf33c6 add multicomponent iteration test
alfadur
parents: 15369
diff changeset
   689
            }
24a9afbf33c6 add multicomponent iteration test
alfadur
parents: 15369
diff changeset
   690
        }
24a9afbf33c6 add multicomponent iteration test
alfadur
parents: 15369
diff changeset
   691
15375
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   692
        let mut sum = 0;
15392
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   693
        manager.iter().run(|(d,): (&Datum,)| sum += d.value);
15375
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   694
        assert_eq!(sum, 55);
37b632d38f14 properly update gear id lookup on block modifications
alfadur
parents: 15373
diff changeset
   695
15392
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   696
        let mut tag_sum = 0;
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   697
        manager
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   698
            .iter()
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   699
            .with_tags::<&Tag>()
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   700
            .run(|(d,): (&Datum,)| tag_sum += d.value);
b387a51705ac implement iteration with tags
alfadur
parents: 15387
diff changeset
   701
        assert_eq!(tag_sum, 30);
15371
24a9afbf33c6 add multicomponent iteration test
alfadur
parents: 15369
diff changeset
   702
    }
15354
dff37ac61dcf convert ecs storage to untyped
alfadur
parents: 15305
diff changeset
   703
}