rust/hwphysics/src/time.rs
author alfadur
Thu, 25 Jul 2019 19:58:19 +0300
changeset 15275 7446258fab98
child 15279 42b710b0f883
permissions -rw-r--r--
add time events
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15275
7446258fab98 add time events
alfadur
parents:
diff changeset
     1
use crate::common::{GearDataProcessor, GearId};
7446258fab98 add time events
alfadur
parents:
diff changeset
     2
use fpnum::{fp, FPNum};
7446258fab98 add time events
alfadur
parents:
diff changeset
     3
use std::{
7446258fab98 add time events
alfadur
parents:
diff changeset
     4
    cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd},
7446258fab98 add time events
alfadur
parents:
diff changeset
     5
    collections::BinaryHeap,
7446258fab98 add time events
alfadur
parents:
diff changeset
     6
};
7446258fab98 add time events
alfadur
parents:
diff changeset
     7
7446258fab98 add time events
alfadur
parents:
diff changeset
     8
pub type EventId = u16;
7446258fab98 add time events
alfadur
parents:
diff changeset
     9
7446258fab98 add time events
alfadur
parents:
diff changeset
    10
struct TimeEvent {
7446258fab98 add time events
alfadur
parents:
diff changeset
    11
    time: FPNum,
7446258fab98 add time events
alfadur
parents:
diff changeset
    12
    gear_id: GearId,
7446258fab98 add time events
alfadur
parents:
diff changeset
    13
    event_id: EventId,
7446258fab98 add time events
alfadur
parents:
diff changeset
    14
}
7446258fab98 add time events
alfadur
parents:
diff changeset
    15
7446258fab98 add time events
alfadur
parents:
diff changeset
    16
impl PartialOrd for TimeEvent {
7446258fab98 add time events
alfadur
parents:
diff changeset
    17
    #[inline]
7446258fab98 add time events
alfadur
parents:
diff changeset
    18
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
7446258fab98 add time events
alfadur
parents:
diff changeset
    19
        self.time.partial_cmp(&other.time)
7446258fab98 add time events
alfadur
parents:
diff changeset
    20
    }
7446258fab98 add time events
alfadur
parents:
diff changeset
    21
}
7446258fab98 add time events
alfadur
parents:
diff changeset
    22
7446258fab98 add time events
alfadur
parents:
diff changeset
    23
impl PartialEq for TimeEvent {
7446258fab98 add time events
alfadur
parents:
diff changeset
    24
    #[inline]
7446258fab98 add time events
alfadur
parents:
diff changeset
    25
    fn eq(&self, other: &Self) -> bool {
7446258fab98 add time events
alfadur
parents:
diff changeset
    26
        self.time.eq(&other.time)
7446258fab98 add time events
alfadur
parents:
diff changeset
    27
    }
7446258fab98 add time events
alfadur
parents:
diff changeset
    28
}
7446258fab98 add time events
alfadur
parents:
diff changeset
    29
7446258fab98 add time events
alfadur
parents:
diff changeset
    30
impl Ord for TimeEvent {
7446258fab98 add time events
alfadur
parents:
diff changeset
    31
    #[inline]
7446258fab98 add time events
alfadur
parents:
diff changeset
    32
    fn cmp(&self, other: &Self) -> Ordering {
7446258fab98 add time events
alfadur
parents:
diff changeset
    33
        self.time.cmp(&other.time)
7446258fab98 add time events
alfadur
parents:
diff changeset
    34
    }
7446258fab98 add time events
alfadur
parents:
diff changeset
    35
}
7446258fab98 add time events
alfadur
parents:
diff changeset
    36
7446258fab98 add time events
alfadur
parents:
diff changeset
    37
impl Eq for TimeEvent {}
7446258fab98 add time events
alfadur
parents:
diff changeset
    38
7446258fab98 add time events
alfadur
parents:
diff changeset
    39
pub struct OccurredEvents {
7446258fab98 add time events
alfadur
parents:
diff changeset
    40
    events: Vec<(GearId, EventId)>,
7446258fab98 add time events
alfadur
parents:
diff changeset
    41
}
7446258fab98 add time events
alfadur
parents:
diff changeset
    42
7446258fab98 add time events
alfadur
parents:
diff changeset
    43
impl OccurredEvents {
7446258fab98 add time events
alfadur
parents:
diff changeset
    44
    fn new() -> Self {
7446258fab98 add time events
alfadur
parents:
diff changeset
    45
        Self { events: vec![] }
7446258fab98 add time events
alfadur
parents:
diff changeset
    46
    }
7446258fab98 add time events
alfadur
parents:
diff changeset
    47
7446258fab98 add time events
alfadur
parents:
diff changeset
    48
    fn clear(&mut self) {
7446258fab98 add time events
alfadur
parents:
diff changeset
    49
        self.events.clear()
7446258fab98 add time events
alfadur
parents:
diff changeset
    50
    }
7446258fab98 add time events
alfadur
parents:
diff changeset
    51
}
7446258fab98 add time events
alfadur
parents:
diff changeset
    52
7446258fab98 add time events
alfadur
parents:
diff changeset
    53
pub struct TimeProcessor {
7446258fab98 add time events
alfadur
parents:
diff changeset
    54
    current_event_id: EventId,
7446258fab98 add time events
alfadur
parents:
diff changeset
    55
    current_time: FPNum,
7446258fab98 add time events
alfadur
parents:
diff changeset
    56
    events: BinaryHeap<TimeEvent>,
7446258fab98 add time events
alfadur
parents:
diff changeset
    57
    timeouts: OccurredEvents,
7446258fab98 add time events
alfadur
parents:
diff changeset
    58
}
7446258fab98 add time events
alfadur
parents:
diff changeset
    59
7446258fab98 add time events
alfadur
parents:
diff changeset
    60
impl TimeProcessor {
7446258fab98 add time events
alfadur
parents:
diff changeset
    61
    pub fn new() -> Self {
7446258fab98 add time events
alfadur
parents:
diff changeset
    62
        Self {
7446258fab98 add time events
alfadur
parents:
diff changeset
    63
            current_event_id: 0,
7446258fab98 add time events
alfadur
parents:
diff changeset
    64
            current_time: fp!(0),
7446258fab98 add time events
alfadur
parents:
diff changeset
    65
            events: BinaryHeap::with_capacity(1024),
7446258fab98 add time events
alfadur
parents:
diff changeset
    66
            timeouts: OccurredEvents::new(),
7446258fab98 add time events
alfadur
parents:
diff changeset
    67
        }
7446258fab98 add time events
alfadur
parents:
diff changeset
    68
    }
7446258fab98 add time events
alfadur
parents:
diff changeset
    69
7446258fab98 add time events
alfadur
parents:
diff changeset
    70
    pub fn register(&mut self, gear_id: GearId, timeout: FPNum) -> EventId {
7446258fab98 add time events
alfadur
parents:
diff changeset
    71
        let event_id = self.current_event_id;
7446258fab98 add time events
alfadur
parents:
diff changeset
    72
        self.current_event_id.wrapping_add(1);
7446258fab98 add time events
alfadur
parents:
diff changeset
    73
        let event = TimeEvent {
7446258fab98 add time events
alfadur
parents:
diff changeset
    74
            time: self.current_time + timeout,
7446258fab98 add time events
alfadur
parents:
diff changeset
    75
            gear_id,
7446258fab98 add time events
alfadur
parents:
diff changeset
    76
            event_id,
7446258fab98 add time events
alfadur
parents:
diff changeset
    77
        };
7446258fab98 add time events
alfadur
parents:
diff changeset
    78
        self.events.push(event);
7446258fab98 add time events
alfadur
parents:
diff changeset
    79
        event_id
7446258fab98 add time events
alfadur
parents:
diff changeset
    80
    }
7446258fab98 add time events
alfadur
parents:
diff changeset
    81
7446258fab98 add time events
alfadur
parents:
diff changeset
    82
    pub fn process(&mut self, time_step: FPNum) -> &OccurredEvents {
7446258fab98 add time events
alfadur
parents:
diff changeset
    83
        self.timeouts.clear();
7446258fab98 add time events
alfadur
parents:
diff changeset
    84
        self.current_time += time_step;
7446258fab98 add time events
alfadur
parents:
diff changeset
    85
        while self
7446258fab98 add time events
alfadur
parents:
diff changeset
    86
            .events
7446258fab98 add time events
alfadur
parents:
diff changeset
    87
            .peek()
7446258fab98 add time events
alfadur
parents:
diff changeset
    88
            .filter(|e| e.time <= self.current_time)
7446258fab98 add time events
alfadur
parents:
diff changeset
    89
            .is_some()
7446258fab98 add time events
alfadur
parents:
diff changeset
    90
        {
7446258fab98 add time events
alfadur
parents:
diff changeset
    91
            let event = self.events.pop().unwrap();
7446258fab98 add time events
alfadur
parents:
diff changeset
    92
            self.timeouts.events.push((event.gear_id, event.event_id))
7446258fab98 add time events
alfadur
parents:
diff changeset
    93
        }
7446258fab98 add time events
alfadur
parents:
diff changeset
    94
        &self.timeouts
7446258fab98 add time events
alfadur
parents:
diff changeset
    95
    }
7446258fab98 add time events
alfadur
parents:
diff changeset
    96
}