Use `merge` as the `Semilattice` join method
Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
parent
01b7686659
commit
0448441418
|
|
@ -15,9 +15,18 @@ mod test;
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// A join-semilattice.
|
||||
pub trait Semilattice {
|
||||
/// Join or "merge" two semilattices into one.
|
||||
fn join(self, other: Self) -> Self;
|
||||
pub trait Semilattice: Sized {
|
||||
/// Merge an other semilattice into this one.
|
||||
///
|
||||
/// 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.
|
||||
|
|
|
|||
|
|
@ -90,7 +90,7 @@ where
|
|||
V: PartialOrd + Eq,
|
||||
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() {
|
||||
match self.inner.entry(k) {
|
||||
Entry::Occupied(mut e) => {
|
||||
|
|
@ -101,7 +101,6 @@ where
|
|||
}
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -33,10 +33,6 @@ impl<T: PartialOrd, C: PartialOrd> LWWReg<T, C> {
|
|||
let (t, c) = self.inner.into_inner();
|
||||
(c, t)
|
||||
}
|
||||
|
||||
pub fn merge(&mut self, other: Self) {
|
||||
self.inner.merge(other.inner);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, C> Semilattice for LWWReg<T, C>
|
||||
|
|
@ -44,10 +40,8 @@ where
|
|||
T: PartialOrd + Default,
|
||||
C: PartialOrd + Default,
|
||||
{
|
||||
fn join(self, other: Self) -> Self {
|
||||
Self {
|
||||
inner: self.inner.join(other.inner),
|
||||
}
|
||||
fn merge(&mut self, other: Self) {
|
||||
self.inner.merge(other.inner);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -61,10 +61,8 @@ where
|
|||
T: Ord,
|
||||
C: Ord + Copy + Default,
|
||||
{
|
||||
fn join(self, other: Self) -> Self {
|
||||
Self {
|
||||
inner: self.inner.join(other.inner),
|
||||
}
|
||||
fn merge(&mut self, other: Self) {
|
||||
self.inner.merge(other.inner);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -57,11 +57,9 @@ impl<T> From<T> 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 {
|
||||
other
|
||||
} else {
|
||||
self
|
||||
self.0 = other.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,8 +19,8 @@ impl<T> From<Option<T>> for Redactable<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: PartialOrd> Redactable<T> {
|
||||
pub fn merge(&mut self, other: Self) {
|
||||
impl<T: PartialOrd> Semilattice for Redactable<T> {
|
||||
fn merge(&mut self, other: Self) {
|
||||
match (&self, other) {
|
||||
(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)]
|
||||
mod test {
|
||||
use quickcheck_macros::quickcheck;
|
||||
|
|
|
|||
Loading…
Reference in New Issue