use crate::simulation::EventKey;
use crate::time::MonotonicTime;
use crate::util::indexed_priority_queue::{IndexedPriorityQueue, InsertKey};
pub(crate) type KeyRegistryId = InsertKey;
#[derive(Default)]
pub(crate) struct KeyRegistry {
keys: IndexedPriorityQueue<MonotonicTime, EventKey>,
}
impl KeyRegistry {
pub(crate) fn insert_key(
&mut self,
event_key: EventKey,
expiration: MonotonicTime,
) -> KeyRegistryId {
self.keys.insert(expiration, event_key)
}
pub(crate) fn insert_eternal_key(&mut self, event_key: EventKey) -> KeyRegistryId {
self.keys.insert(MonotonicTime::MAX, event_key)
}
pub(crate) fn extract_key(&mut self, key_id: KeyRegistryId) -> Option<EventKey> {
self.keys.extract(key_id).map(|(_, key)| key)
}
pub(crate) fn remove_expired_keys(&mut self, now: MonotonicTime) {
while let Some(expiration) = self.keys.peek_key() {
if *expiration >= now {
return;
}
self.keys.pull();
}
}
}