use crate::EventSet;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct MaxSet {
max: u64,
}
impl EventSet for MaxSet {
type EventIter = EventIter;
fn new() -> Self {
MaxSet { max: 0 }
}
fn next_event(&mut self) -> u64 {
self.max += 1;
self.max
}
fn add_event(&mut self, event: u64) -> bool {
if event <= self.max {
false
} else {
self.max = event;
true
}
}
fn add_event_range(&mut self, start: u64, end: u64) -> bool {
debug_assert!(start <= end);
self.add_event(end)
}
fn is_event(&self, event: u64) -> bool {
event <= self.max
}
fn events(&self) -> (u64, Vec<u64>) {
(self.max, vec![])
}
fn frontier(&self) -> u64 {
self.max
}
fn join(&mut self, other: &Self) {
self.max = std::cmp::max(self.max, other.max);
}
fn meet(&mut self, other: &Self) {
self.max = std::cmp::min(self.max, other.max);
}
fn subtracted(&self, other: &Self) -> Vec<u64> {
if self.max > other.max {
((other.max + 1)..=self.max).collect()
} else {
Vec::new()
}
}
fn event_iter(self) -> Self::EventIter {
EventIter {
current: 0,
max: self.max,
}
}
}
impl MaxSet {
pub fn from(max: u64) -> Self {
Self { max }
}
}
pub struct EventIter {
current: u64,
max: u64,
}
impl Iterator for EventIter {
type Item = u64;
fn next(&mut self) -> Option<Self::Item> {
if self.current == self.max {
None
} else {
self.current += 1;
Some(self.current)
}
}
}
impl fmt::Debug for MaxSet {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.max)
}
}