1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
//! Partially ordered elements with a least upper bound. //! //! Lattices form the basis of differential dataflow's efficient execution in the presence of //! iterative sub-computations. All logical times in differential dataflow must implement the //! `Lattice` trait, and all reasoning in operators are done it terms of `Lattice` methods. use timely::order::PartialOrder; /// A bounded partially ordered type supporting joins and meets. pub trait Lattice : PartialOrder { /// The smallest element of the type. /// /// #Examples /// /// ``` /// use differential_dataflow::lattice::Lattice; /// /// let min = <usize as Lattice>::minimum(); /// assert_eq!(min, usize::min_value()); /// ``` fn minimum() -> Self; /// The smallest element greater than or equal to both arguments. /// /// # Examples /// /// ``` /// # extern crate timely; /// # extern crate differential_dataflow; /// # use timely::PartialOrder; /// # use timely::order::Product; /// # use differential_dataflow::lattice::Lattice; /// # fn main() { /// /// let time1 = Product::new(3, 7); /// let time2 = Product::new(4, 6); /// let join = time1.join(&time2); /// /// assert_eq!(join, Product::new(4, 7)); /// # } /// ``` fn join(&self, &Self) -> Self; /// Updates `self` to the smallest element greater than or equal to both arguments. /// /// # Examples /// /// ``` /// # extern crate timely; /// # extern crate differential_dataflow; /// # use timely::PartialOrder; /// # use timely::order::Product; /// # use differential_dataflow::lattice::Lattice; /// # fn main() { /// /// let mut time1 = Product::new(3, 7); /// let time2 = Product::new(4, 6); /// time1.join_assign(&time2); /// /// assert_eq!(time1, Product::new(4, 7)); /// # } /// ``` fn join_assign(&mut self, other: &Self) where Self: Sized { *self = self.join(other); } /// The largest element less than or equal to both arguments. /// /// # Examples /// /// ``` /// # extern crate timely; /// # extern crate differential_dataflow; /// # use timely::PartialOrder; /// # use timely::order::Product; /// # use differential_dataflow::lattice::Lattice; /// # fn main() { /// /// let time1 = Product::new(3, 7); /// let time2 = Product::new(4, 6); /// let meet = time1.meet(&time2); /// /// assert_eq!(meet, Product::new(3, 6)); /// # } /// ``` fn meet(&self, &Self) -> Self; /// Updates `self` to the largest element less than or equal to both arguments. /// /// # Examples /// /// ``` /// # extern crate timely; /// # extern crate differential_dataflow; /// # use timely::PartialOrder; /// # use timely::order::Product; /// # use differential_dataflow::lattice::Lattice; /// # fn main() { /// /// let mut time1 = Product::new(3, 7); /// let time2 = Product::new(4, 6); /// time1.meet_assign(&time2); /// /// assert_eq!(time1, Product::new(3, 6)); /// # } /// ``` fn meet_assign(&mut self, other: &Self) where Self: Sized { *self = self.meet(other); } /// Advances self to the largest time indistinguishable under `frontier`. /// /// This method produces the "largest" lattice element with the property that for every /// lattice element greater than some element of `frontier`, both the result and `self` /// compare identically to the lattice element. The result is the "largest" element in /// the sense that any other element with the same property (compares identically to times /// greater or equal to `frontier`) must be less or equal to the result. /// /// When provided an empty frontier `self` is not modified. /// /// # Examples /// /// ``` /// # extern crate timely; /// # extern crate differential_dataflow; /// # use timely::PartialOrder; /// # use timely::order::Product; /// # use differential_dataflow::lattice::Lattice; /// # fn main() { /// /// let time = Product::new(3, 7); /// let mut advanced = Product::new(3, 7); /// let frontier = vec![Product::new(4, 8), Product::new(5, 3)]; /// advanced.advance_by(&frontier[..]); /// /// // `time` and `advanced` are indistinguishable to elements >= an element of `frontier` /// for i in 0 .. 10 { /// for j in 0 .. 10 { /// let test = Product::new(i, j); /// // for `test` in the future of `frontier` .. /// if frontier.iter().any(|t| t.less_equal(&test)) { /// assert_eq!(time.less_equal(&test), advanced.less_equal(&test)); /// } /// } /// } /// /// assert_eq!(advanced, Product::new(4, 7)); /// # } /// ``` #[inline] fn advance_by(&mut self, frontier: &[Self]) where Self: Sized { if let Some(first) = frontier.get(0) { let mut result = self.join(first); for f in &frontier[1..] { result.meet_assign(&self.join(f)); } *self = result; } } } use timely::order::Product; impl<T1: Lattice, T2: Lattice> Lattice for Product<T1, T2> { #[inline] fn minimum() -> Self { Product::new(T1::minimum(), T2::minimum()) } #[inline] fn join(&self, other: &Product<T1, T2>) -> Product<T1, T2> { Product { outer: self.outer.join(&other.outer), inner: self.inner.join(&other.inner), } } #[inline] fn meet(&self, other: &Product<T1, T2>) -> Product<T1, T2> { Product { outer: self.outer.meet(&other.outer), inner: self.inner.meet(&other.inner), } } } macro_rules! implement_lattice { ($index_type:ty, $minimum:expr) => ( impl Lattice for $index_type { #[inline] fn minimum() -> Self { $minimum } #[inline] fn join(&self, other: &Self) -> Self { ::std::cmp::max(*self, *other) } #[inline] fn meet(&self, other: &Self) -> Self { ::std::cmp::min(*self, *other) } } ) } use std::time::Duration; implement_lattice!(Duration, Duration::new(0, 0)); implement_lattice!(usize, 0); implement_lattice!(u128, 0); implement_lattice!(u64, 0); implement_lattice!(u32, 0); implement_lattice!(u16, 0); implement_lattice!(u8, 0); implement_lattice!(i32, 0); implement_lattice!((), ());