extern crate alloc;
use alloc::collections::BTreeMap;
mod iter;
mod lattice;
#[cfg(kani)]
mod proofs;
mod relative;
mod wire;
pub use iter::Iter;
pub use relative::Disagreement;
pub use wire::DecodeError;
#[derive(Clone, Default, PartialEq, Eq, Hash, Debug)]
pub struct VersionVector {
counters: BTreeMap<u32, u64>,
}
impl VersionVector {
#[must_use]
pub const fn new() -> Self {
Self {
counters: BTreeMap::new(),
}
}
#[must_use]
pub fn get(&self, station_id: u32) -> u64 {
self.counters.get(&station_id).copied().unwrap_or(0)
}
pub fn increment(&mut self, station_id: u32) -> u64 {
let next = self.get(station_id).saturating_add(1);
let _ = self.counters.insert(station_id, next);
next
}
pub fn observe(&mut self, station_id: u32, counter: u64) {
if counter > self.get(station_id) {
let _ = self.counters.insert(station_id, counter);
}
}
#[must_use]
pub fn iter(&self) -> Iter<'_> {
Iter::new(self.counters.iter())
}
}
impl<'a> IntoIterator for &'a VersionVector {
type Item = (u32, u64);
type IntoIter = Iter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}