rust/hwphysics/src/common.rs
author alfadur
Thu, 25 Jul 2019 23:02:02 +0300
changeset 15296 66c987015f2d
parent 15295 42b710b0f883
child 15302 8095853811a6
permissions -rw-r--r--
replace time with milliseconds
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15296
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
     1
use fpnum::{fp, FPNum};
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
     2
use std::{collections::BinaryHeap, num::NonZeroU16, ops::Add};
15295
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
     3
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
     4
pub type GearId = NonZeroU16;
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14200
diff changeset
     5
pub trait GearData {}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14200
diff changeset
     6
15296
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
     7
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
     8
pub struct Millis(u32);
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
     9
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    10
impl Millis {
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    11
    #[inline]
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    12
    pub fn new(value: u32) -> Self {
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    13
        Self(value)
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    14
    }
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    15
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    16
    #[inline]
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    17
    pub fn get(self) -> u32 {
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    18
        self.0
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    19
    }
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    20
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    21
    #[inline]
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    22
    pub fn to_fixed(self) -> FPNum {
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    23
        FPNum::new(self.0 as i32, 1000)
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    24
    }
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    25
}
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    26
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    27
impl Add for Millis {
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    28
    type Output = Self;
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    29
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    30
    fn add(self, rhs: Self) -> Self::Output {
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    31
        Self(self.0 + rhs.0)
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    32
    }
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    33
}
66c987015f2d replace time with milliseconds
alfadur
parents: 15295
diff changeset
    34
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14200
diff changeset
    35
pub trait GearDataProcessor<T: GearData> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14200
diff changeset
    36
    fn add(&mut self, gear_id: GearId, gear_data: T);
15295
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    37
    fn remove(&mut self, gear_id: GearId);
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14200
diff changeset
    38
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14200
diff changeset
    39
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14200
diff changeset
    40
pub trait GearDataAggregator<T: GearData> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14200
diff changeset
    41
    fn find_processor(&mut self) -> &mut GearDataProcessor<T>;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14200
diff changeset
    42
}
15295
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    43
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    44
pub struct GearAllocator {
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    45
    max_id: u16,
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    46
    free_ids: BinaryHeap<GearId>,
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    47
}
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    48
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    49
impl GearAllocator {
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    50
    pub fn new() -> Self {
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    51
        Self {
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    52
            max_id: 0,
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    53
            free_ids: BinaryHeap::with_capacity(1024),
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    54
        }
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    55
    }
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    56
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    57
    pub fn alloc(&mut self) -> Option<GearId> {
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    58
        self.free_ids.pop().or_else(|| {
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    59
            self.max_id.checked_add(1).and_then(|new_max_id| {
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    60
                self.max_id = new_max_id;
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    61
                NonZeroU16::new(new_max_id)
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    62
            })
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    63
        })
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    64
    }
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    65
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    66
    pub fn free(&mut self, gear_id: GearId) {
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    67
        self.free_ids.push(gear_id)
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    68
    }
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    69
}