rust/hwphysics/src/common.rs
author alfadur
Thu, 25 Jul 2019 22:31:24 +0300
changeset 15295 42b710b0f883
parent 15284 24828281c9c5
child 15296 66c987015f2d
permissions -rw-r--r--
add gear allocator
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15295
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
     1
use std::{collections::BinaryHeap, num::NonZeroU16};
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
     2
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
     3
pub type GearId = NonZeroU16;
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14200
diff changeset
     4
pub trait GearData {}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14200
diff changeset
     5
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14200
diff changeset
     6
pub trait GearDataProcessor<T: GearData> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14200
diff changeset
     7
    fn add(&mut self, gear_id: GearId, gear_data: T);
15295
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
     8
    fn remove(&mut self, gear_id: GearId);
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14200
diff changeset
     9
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14200
diff changeset
    10
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14200
diff changeset
    11
pub trait GearDataAggregator<T: GearData> {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14200
diff changeset
    12
    fn find_processor(&mut self) -> &mut GearDataProcessor<T>;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14200
diff changeset
    13
}
15295
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    14
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    15
pub struct GearAllocator {
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    16
    max_id: u16,
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    17
    free_ids: BinaryHeap<GearId>,
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    18
}
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    19
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    20
impl GearAllocator {
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    21
    pub fn new() -> Self {
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    22
        Self {
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    23
            max_id: 0,
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    24
            free_ids: BinaryHeap::with_capacity(1024),
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    25
        }
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    26
    }
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    27
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    28
    pub fn alloc(&mut self) -> Option<GearId> {
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    29
        self.free_ids.pop().or_else(|| {
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    30
            self.max_id.checked_add(1).and_then(|new_max_id| {
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    31
                self.max_id = new_max_id;
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    32
                NonZeroU16::new(new_max_id)
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    33
            })
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    34
        })
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    35
    }
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    36
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    37
    pub fn free(&mut self, gear_id: GearId) {
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    38
        self.free_ids.push(gear_id)
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    39
    }
42b710b0f883 add gear allocator
alfadur
parents: 15284
diff changeset
    40
}