| 15 | |
| 16 | impl<T> TimeBasedVec<T> { |
| 17 | pub fn push(&mut self, time: Time, item: T) { |
| 18 | // Fast path: we're appending an event that should be at the end of the vec |
| 19 | if self |
| 20 | .items |
| 21 | .last() |
| 22 | .map(|item| item.time < time) |
| 23 | .unwrap_or(true) |
| 24 | { |
| 25 | self.items.push(ItemWithTime { time, item }); |
| 26 | } else { |
| 27 | let index = self.items.partition_point(|item| item.time < time); |
| 28 | self.items.insert(index, ItemWithTime { time, item }); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /// Returns the most recent item that has happened before or at `time`. |
| 33 | pub fn get_most_recent_at(&self, time: Time) -> Option<&ItemWithTime<T>> { |