use std::collections::{btree_map, BTreeMap};
mod time_int;
mod timeline;
use crate::{time::Time, SizeBytes, TimeRange};
pub use time_int::TimeInt;
pub use timeline::{Timeline, TimelineName};
#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct TimePoint(BTreeMap<Timeline, TimeInt>);
impl From<BTreeMap<Timeline, TimeInt>> for TimePoint {
fn from(timelines: BTreeMap<Timeline, TimeInt>) -> Self {
Self(timelines)
}
}
impl TimePoint {
pub fn timeless() -> Self {
Self::default()
}
pub fn get(&self, timeline: &Timeline) -> Option<&TimeInt> {
self.0.get(timeline)
}
pub fn insert(&mut self, timeline: Timeline, time: TimeInt) -> Option<TimeInt> {
self.0.insert(timeline, time)
}
pub fn remove(&mut self, timeline: &Timeline) -> Option<TimeInt> {
self.0.remove(timeline)
}
#[inline]
pub fn is_timeless(&self) -> bool {
self.0.is_empty()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[inline]
pub fn timelines(&self) -> impl ExactSizeIterator<Item = &Timeline> {
self.0.keys()
}
#[inline]
pub fn times(&self) -> impl ExactSizeIterator<Item = &TimeInt> {
self.0.values()
}
#[inline]
pub fn iter(&self) -> impl ExactSizeIterator<Item = (&Timeline, &TimeInt)> {
self.0.iter()
}
#[inline]
pub fn union_max(mut self, rhs: &Self) -> Self {
for (&timeline, &time) in rhs {
match self.0.entry(timeline) {
btree_map::Entry::Vacant(entry) => {
entry.insert(time);
}
btree_map::Entry::Occupied(mut entry) => {
let entry = entry.get_mut();
*entry = TimeInt::max(*entry, time);
}
}
}
self
}
}
impl SizeBytes for TimePoint {
#[inline]
fn heap_size_bytes(&self) -> u64 {
type K = Timeline;
type V = TimeInt;
let inner: &BTreeMap<K, V> = &self.0;
let keys_size_bytes = std::mem::size_of::<K>() * inner.len();
let values_size_bytes = std::mem::size_of::<V>() * inner.len();
(keys_size_bytes + values_size_bytes) as u64
}
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, num_derive::FromPrimitive)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum TimeType {
Time,
Sequence,
}
impl TimeType {
fn hash(&self) -> u64 {
match self {
Self::Time => 0,
Self::Sequence => 1,
}
}
pub fn format(&self, time_int: TimeInt) -> String {
if time_int <= TimeInt::BEGINNING {
"-∞".into()
} else if time_int >= TimeInt::MAX {
"+∞".into()
} else {
match self {
Self::Time => Time::from(time_int).format(),
Self::Sequence => format!("#{}", time_int.0),
}
}
}
pub fn format_range(&self, time_range: TimeRange) -> String {
format!(
"{}..={}",
self.format(time_range.min),
self.format(time_range.max)
)
}
}
impl IntoIterator for TimePoint {
type Item = (Timeline, TimeInt);
type IntoIter = btree_map::IntoIter<Timeline, TimeInt>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<'a> IntoIterator for &'a TimePoint {
type Item = (&'a Timeline, &'a TimeInt);
type IntoIter = btree_map::Iter<'a, Timeline, TimeInt>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
impl FromIterator<(Timeline, TimeInt)> for TimePoint {
#[inline]
fn from_iter<T: IntoIterator<Item = (Timeline, TimeInt)>>(iter: T) -> Self {
Self(iter.into_iter().collect())
}
}
impl<const N: usize> From<[(Timeline, TimeInt); N]> for TimePoint {
#[inline]
fn from(timelines: [(Timeline, TimeInt); N]) -> Self {
Self(timelines.into_iter().collect())
}
}