Skip to content

Commit 27492f2

Browse files
Lattice implementation for tuples (#359)
1 parent f862b65 commit 27492f2

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

src/lattice.rs

+52-1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,57 @@ impl<T1: Lattice, T2: Lattice> Lattice for Product<T1, T2> {
171171
}
172172
}
173173

174+
/// A type that has a unique maximum element.
175+
pub trait Maximum {
176+
/// The unique maximal element of the set.
177+
fn maximum() -> Self;
178+
}
179+
180+
/// Implements `Maximum` for elements with a `MAX` associated constant.
181+
macro_rules! implement_maximum {
182+
($($index_type:ty,)*) => (
183+
$(
184+
impl Maximum for $index_type {
185+
fn maximum() -> Self { Self::MAX }
186+
}
187+
)*
188+
)
189+
}
190+
191+
implement_maximum!(usize, u128, u64, u32, u16, u8, isize, i128, i64, i32, i16, i8, Duration,);
192+
impl Maximum for () { fn maximum() -> () { () }}
193+
194+
use timely::progress::Timestamp;
195+
196+
// Tuples have the annoyance that they are only a lattice for `T2` with maximal elements,
197+
// as the `meet` operator on `(x, _)` and `(y, _)` would be `(x meet y, maximum())`.
198+
impl<T1: Lattice+Clone, T2: Lattice+Clone+Maximum+Timestamp> Lattice for (T1, T2) {
199+
#[inline]
200+
fn join(&self, other: &(T1, T2)) -> (T1, T2) {
201+
if self.0.eq(&other.0) {
202+
(self.0.clone(), self.1.join(&other.1))
203+
} else if self.0.less_than(&other.0) {
204+
other.clone()
205+
} else if other.0.less_than(&self.0) {
206+
self.clone()
207+
} else {
208+
(self.0.join(&other.0), T2::minimum())
209+
}
210+
}
211+
#[inline]
212+
fn meet(&self, other: &(T1, T2)) -> (T1, T2) {
213+
if self.0.eq(&other.0) {
214+
(self.0.clone(), self.1.meet(&other.1))
215+
} else if self.0.less_than(&other.0) {
216+
self.clone()
217+
} else if other.0.less_than(&self.0) {
218+
other.clone()
219+
} else {
220+
(self.0.meet(&other.0), T2::maximum())
221+
}
222+
}
223+
}
224+
174225
macro_rules! implement_lattice {
175226
($index_type:ty, $minimum:expr) => (
176227
impl Lattice for $index_type {
@@ -317,4 +368,4 @@ impl<T: Lattice+Clone> Lattice for Antichain<T> {
317368
}
318369
upper
319370
}
320-
}
371+
}

0 commit comments

Comments
 (0)