Use `merge` as the `Semilattice` join method

Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
Alexis Sellier 2022-11-24 19:10:32 +01:00
parent 01b7686659
commit 0448441418
No known key found for this signature in database
6 changed files with 21 additions and 30 deletions

View File

@ -15,9 +15,18 @@ mod test;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// A join-semilattice. /// A join-semilattice.
pub trait Semilattice { pub trait Semilattice: Sized {
/// Join or "merge" two semilattices into one. /// Merge an other semilattice into this one.
fn join(self, other: Self) -> Self; ///
/// This operation should obbey the semilattice laws and should thus be idempotent,
/// associative and commutative.
fn merge(&mut self, other: Self);
/// Like [`Semilattice::merge`] but takes and returns a new semilattice.
fn join(mut self, other: Self) -> Self {
self.merge(other);
self
}
} }
/// Reduce an iterator of semilattice values to its least upper bound. /// Reduce an iterator of semilattice values to its least upper bound.

View File

@ -90,7 +90,7 @@ where
V: PartialOrd + Eq, V: PartialOrd + Eq,
C: Ord + Copy + Default, C: Ord + Copy + Default,
{ {
fn join(mut self, other: Self) -> Self { fn merge(&mut self, other: Self) {
for (k, v) in other.inner.into_iter() { for (k, v) in other.inner.into_iter() {
match self.inner.entry(k) { match self.inner.entry(k) {
Entry::Occupied(mut e) => { Entry::Occupied(mut e) => {
@ -101,7 +101,6 @@ where
} }
} }
} }
self
} }
} }

View File

@ -33,10 +33,6 @@ impl<T: PartialOrd, C: PartialOrd> LWWReg<T, C> {
let (t, c) = self.inner.into_inner(); let (t, c) = self.inner.into_inner();
(c, t) (c, t)
} }
pub fn merge(&mut self, other: Self) {
self.inner.merge(other.inner);
}
} }
impl<T, C> Semilattice for LWWReg<T, C> impl<T, C> Semilattice for LWWReg<T, C>
@ -44,10 +40,8 @@ where
T: PartialOrd + Default, T: PartialOrd + Default,
C: PartialOrd + Default, C: PartialOrd + Default,
{ {
fn join(self, other: Self) -> Self { fn merge(&mut self, other: Self) {
Self { self.inner.merge(other.inner);
inner: self.inner.join(other.inner),
}
} }
} }

View File

@ -61,10 +61,8 @@ where
T: Ord, T: Ord,
C: Ord + Copy + Default, C: Ord + Copy + Default,
{ {
fn join(self, other: Self) -> Self { fn merge(&mut self, other: Self) {
Self { self.inner.merge(other.inner);
inner: self.inner.join(other.inner),
}
} }
} }

View File

@ -57,11 +57,9 @@ impl<T> From<T> for Max<T> {
} }
impl<T: PartialOrd> Semilattice for Max<T> { impl<T: PartialOrd> Semilattice for Max<T> {
fn join(self, other: Self) -> Self { fn merge(&mut self, other: Self) {
if other.0 > self.0 { if other.0 > self.0 {
other self.0 = other.0;
} else {
self
} }
} }
} }

View File

@ -19,8 +19,8 @@ impl<T> From<Option<T>> for Redactable<T> {
} }
} }
impl<T: PartialOrd> Redactable<T> { impl<T: PartialOrd> Semilattice for Redactable<T> {
pub fn merge(&mut self, other: Self) { fn merge(&mut self, other: Self) {
match (&self, other) { match (&self, other) {
(Self::Redacted, _) => {} (Self::Redacted, _) => {}
(Self::Present(_), Self::Redacted) => { (Self::Present(_), Self::Redacted) => {
@ -35,13 +35,6 @@ impl<T: PartialOrd> Redactable<T> {
} }
} }
impl<T: PartialOrd> Semilattice for Redactable<T> {
fn join(mut self, other: Self) -> Self {
self.merge(other);
self
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use quickcheck_macros::quickcheck; use quickcheck_macros::quickcheck;