rust/hwphysics/src/data.rs
author alfadur
Wed, 14 Aug 2019 23:06:11 +0300
changeset 15320 90f9a0eac3ae
parent 15310 0076bf602969
child 15359 dff37ac61dcf
permissions -rw-r--r--
remove nopng from vcpkg build
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15310
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
     1
use std::any::{Any, TypeId};
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
     2
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
     3
const BLOCK_SIZE: usize = 8192;
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
     4
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
     5
pub trait TypeTuple {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
     6
    type Storage: Default;
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
     7
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
     8
    fn len() -> usize;
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
     9
    fn get_types(dest: &mut Vec<TypeId>);
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    10
}
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    11
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    12
//TODO macroise this template for tuples up to sufficient size
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    13
impl<T: 'static> TypeTuple for (T,) {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    14
    type Storage = (Vec<T>,);
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    15
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    16
    #[inline]
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    17
    fn len() -> usize {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    18
        1
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    19
    }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    20
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    21
    #[inline]
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    22
    fn get_types(dest: &mut Vec<TypeId>) {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    23
        dest.push(TypeId::of::<T>());
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    24
    }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    25
}
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    26
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    27
pub struct GearDataCollection<T: TypeTuple> {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    28
    len: usize,
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    29
    blocks: T::Storage,
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    30
}
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    31
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    32
impl<T: TypeTuple> GearDataCollection<T> {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    33
    fn new() -> Self {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    34
        Self {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    35
            len: 0,
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    36
            blocks: T::Storage::default(),
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    37
        }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    38
    }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    39
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    40
    fn iter<F: Fn(T)>(&self, f: F) {}
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    41
}
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    42
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    43
pub struct GearDataGroup {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    44
    group_selector: u64,
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    45
    data: Box<dyn Any + 'static>,
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    46
}
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    47
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    48
impl GearDataGroup {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    49
    fn iter() {}
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    50
}
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    51
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    52
pub struct GearDataManager {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    53
    types: Vec<TypeId>,
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    54
    groups: Vec<GearDataGroup>,
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    55
}
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    56
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    57
impl GearDataManager {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    58
    pub fn new() -> Self {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    59
        Self {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    60
            types: vec![],
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    61
            groups: vec![],
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    62
        }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    63
    }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    64
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    65
    pub fn register<T: 'static>(&mut self) {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    66
        assert!(self.types.len() <= 64);
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    67
        let id = TypeId::of::<T>();
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    68
        if !self.types.contains(&id) {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    69
            self.types.push(id);
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    70
        }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    71
    }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    72
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    73
    fn create_selector(&self, types: &[TypeId]) -> u64 {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    74
        let mut selector = 0u64;
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    75
        for (i, typ) in self.types.iter().enumerate() {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    76
            if types.contains(&typ) {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    77
                selector |= 1 << (i as u64)
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    78
            }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    79
        }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    80
        selector
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    81
    }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    82
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    83
    pub fn iter<T: TypeTuple + 'static, F: Fn(T) + Copy>(&self, f: F) {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    84
        let mut types = vec![];
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    85
        T::get_types(&mut types);
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    86
        let selector = self.create_selector(&types);
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    87
        for group in &self.groups {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    88
            if group.group_selector & selector == selector {
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    89
                group
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    90
                    .data
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    91
                    .downcast_ref::<GearDataCollection<T>>()
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    92
                    .unwrap()
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    93
                    .iter(f);
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    94
            }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    95
        }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    96
    }
0076bf602969 start gear data group implementation
alfadur
parents:
diff changeset
    97
}