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