differential_dataflow/dynamic/
pointstamp.rs1use columnar::Columnar;
15use serde::{Deserialize, Serialize};
16use smallvec::SmallVec;
17
18#[derive(Hash, Default, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Serialize, Columnar)]
24#[columnar(derive(Eq, PartialEq, Ord, PartialOrd))]
25pub struct PointStamp<T, const N: usize = 1> {
26 vector: SmallVec<[T; N]>,
28}
29
30impl<T: Timestamp, const N: usize> PartialEq<[T]> for PointStamp<T, N> {
31 fn eq(&self, other: &[T]) -> bool {
32 self.vector.iter()
33 .zip(other.iter().chain(std::iter::repeat(&T::minimum())))
34 .all(|(t1, t2)| t1.eq(t2))
35 }
36}
37
38impl<T: Timestamp, const N: usize> PartialEq<PointStamp<T, N>> for [T] {
39 fn eq(&self, other: &PointStamp<T, N>) -> bool {
40 self.iter()
41 .zip(other.vector.iter().chain(std::iter::repeat(&T::minimum())))
42 .all(|(t1, t2)| t1.eq(t2))
43 }
44}
45
46impl<T: Timestamp, const N: usize> PartialOrder<[T]> for PointStamp<T, N> {
47 fn less_equal(&self, other: &[T]) -> bool {
48 self.vector.iter()
49 .zip(other.iter().chain(std::iter::repeat(&T::minimum())))
50 .all(|(t1, t2)| t1.less_equal(t2))
51 }
52}
53
54impl<T: Timestamp, const N: usize> PartialOrder<PointStamp<T, N>> for [T] {
55 fn less_equal(&self, other: &PointStamp<T, N>) -> bool {
56 self.iter()
57 .zip(other.vector.iter().chain(std::iter::repeat(&T::minimum())))
58 .all(|(t1, t2)| t1.less_equal(t2))
59 }
60}
61
62impl<T: Timestamp, const N: usize> PointStamp<T, N> {
63 pub fn new(mut vector: SmallVec<[T; N]>) -> Self {
67 while vector.last() == Some(&T::minimum()) {
68 vector.pop();
69 }
70 PointStamp { vector }
71 }
72 pub fn into_inner(self) -> SmallVec<[T; N]> {
78 self.vector
79 }
80}
81
82impl<'de, T: Timestamp, const N: usize> Deserialize<'de> for PointStamp<T, N> {
85 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
86 #[derive(Deserialize)]
88 struct PointStamp<T, const N: usize> { vector: SmallVec<[T; N]> }
89 let PointStamp { vector } = PointStamp::<T, N>::deserialize(deserializer)?;
90 Ok(Self::new(vector))
91 }
92}
93
94impl<T, const N: usize> std::ops::Deref for PointStamp<T, N> {
95 type Target = [T];
96 fn deref(&self) -> &Self::Target {
97 &self.vector
98 }
99}
100
101use timely::order::PartialOrder;
103impl<T: Timestamp, const N: usize> PartialOrder for PointStamp<T, N> {
104 #[inline]
105 fn less_equal(&self, other: &Self) -> bool {
106 let (this, that) = (&self.vector[..], &other.vector[..]);
113 this.len() <= that.len() && this.iter().zip(that.iter()).all(|(t1, t2)| t1.less_equal(t2))
114 }
115}
116
117use timely::progress::timestamp::Refines;
118impl<T: Timestamp, const N: usize> Refines<()> for PointStamp<T, N> {
119 fn to_inner(_outer: ()) -> Self {
120 Self { vector: Default::default() }
121 }
122 fn to_outer(self) -> () {
123 ()
124 }
125 fn summarize(_summary: <Self>::Summary) -> () {
126 ()
127 }
128}
129
130use timely::progress::PathSummary;
133
134#[derive(Hash, Default, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Serialize, Deserialize)]
136pub struct PointStampSummary<TS> {
137 pub retain: Option<usize>,
141 pub actions: Vec<TS>,
146}
147
148impl<T: Timestamp, const N: usize> PathSummary<PointStamp<T, N>> for PointStampSummary<T::Summary> {
149 fn results_in(&self, timestamp: &PointStamp<T, N>) -> Option<PointStamp<T, N>> {
150 let timestamps = if let Some(retain) = self.retain {
152 if retain < timestamp.vector.len() {
153 ×tamp.vector[..retain]
154 } else {
155 ×tamp.vector[..]
156 }
157 } else {
158 ×tamp.vector[..]
159 };
160
161 let mut vector = Vec::with_capacity(std::cmp::max(timestamps.len(), self.actions.len()));
162 let min_len = std::cmp::min(timestamps.len(), self.actions.len());
164 for (action, timestamp) in self.actions.iter().zip(timestamps.iter()) {
165 vector.push(action.results_in(timestamp)?);
166 }
167 for timestamp in timestamps.iter().skip(min_len) {
169 vector.push(timestamp.clone());
170 }
171 for action in self.actions.iter().skip(min_len) {
173 vector.push(action.results_in(&T::minimum())?);
174 }
175
176 Some(PointStamp::new(vector.into()))
177 }
178 fn followed_by(&self, other: &Self) -> Option<Self> {
179 let retain = match (self.retain, other.retain) {
181 (Some(x), Some(y)) => Some(std::cmp::min(x, y)),
182 (Some(x), None) => Some(x),
183 (None, Some(y)) => Some(y),
184 (None, None) => None,
185 };
186
187 let self_actions = if let Some(retain) = other.retain {
189 if retain < self.actions.len() {
190 &self.actions[..retain]
191 } else {
192 &self.actions[..]
193 }
194 } else {
195 &self.actions[..]
196 };
197
198 let mut actions = Vec::with_capacity(std::cmp::max(self_actions.len(), other.actions.len()));
199 let min_len = std::cmp::min(self_actions.len(), other.actions.len());
201 for (action1, action2) in self_actions.iter().zip(other.actions.iter()) {
202 actions.push(action1.followed_by(action2)?);
203 }
204 actions.extend(self_actions.iter().skip(min_len).cloned());
206 actions.extend(other.actions.iter().skip(min_len).cloned());
208
209 Some(Self { retain, actions })
210 }
211}
212
213impl<TS: PartialOrder> PartialOrder for PointStampSummary<TS> {
214 fn less_equal(&self, other: &Self) -> bool {
215 self.retain == other.retain
220 && self.actions.len() <= other.actions.len()
221 && self
222 .actions
223 .iter()
224 .zip(other.actions.iter())
225 .all(|(t1, t2)| t1.less_equal(t2))
226 }
227}
228
229use timely::progress::Timestamp;
231impl<T: Timestamp, const N: usize> Timestamp for PointStamp<T, N> {
232 fn minimum() -> Self {
233 Self::new(Default::default())
234 }
235 type Summary = PointStampSummary<T::Summary>;
236}
237
238use crate::lattice::Lattice;
241impl<T: Lattice + Timestamp, const N: usize> Lattice for PointStamp<T, N> {
242 #[inline(always)]
243 fn join(&self, other: &Self) -> Self {
244 let (this, that) = (&self.vector[..], &other.vector[..]);
248 if this.is_empty() { return other.clone(); }
250 if that.is_empty() { return self.clone(); }
251 let (long, short) = if this.len() >= that.len() { (this, that) } else { (that, this) };
252 let vector = long
253 .iter()
254 .enumerate()
255 .map(|(index, time)| if index < short.len() { time.join(&short[index]) } else { time.clone() })
256 .collect();
257 Self { vector }
258 }
259 #[inline]
260 fn join_assign(&mut self, other: &Self) {
261 let that = &other.vector[..];
262 let my_len = self.vector.len();
263 for (this, that) in self.vector.iter_mut().zip(that.iter()) {
264 this.join_assign(that);
265 }
266 if that.len() > my_len {
267 self.vector.extend(that[my_len..].iter().cloned());
268 }
269 }
270 #[inline(always)]
271 fn meet(&self, other: &Self) -> Self {
272 let vector = self.vector.iter().zip(other.vector.iter()).map(|(this, that)| this.meet(that)).collect();
275 Self::new(vector)
276 }
277 #[inline]
278 fn meet_assign(&mut self, other: &Self) {
279 let that = &other.vector[..];
280 self.vector.truncate(that.len());
281 for (this, that) in self.vector.iter_mut().zip(that.iter()) {
282 this.meet_assign(that);
283 }
284 while self.vector.last() == Some(&T::minimum()) { self.vector.pop(); }
285 }
286}
287
288mod columnation {
289 use columnation::{Columnation, Region};
290 use smallvec::SmallVec;
291 use crate::dynamic::pointstamp::PointStamp;
292
293 impl<T: Columnation+Clone, const N: usize> Columnation for PointStamp<T, N> {
294 type InnerRegion = PointStampStack<T::InnerRegion, N>;
295 }
296
297 pub struct PointStampStack<R: Region<Item: Columnation+Clone>, const N: usize = 1>(<SmallVec<[R::Item; N]> as Columnation>::InnerRegion);
299
300 impl<R: Region<Item: Columnation+Clone>, const N: usize> Default for PointStampStack<R, N> {
301 #[inline]
302 fn default() -> Self {
303 Self(Default::default())
304 }
305 }
306
307 impl<R: Region<Item: Columnation+Clone>, const N: usize> Region for PointStampStack<R, N> {
308 type Item = PointStamp<R::Item, N>;
309
310 #[inline]
311 unsafe fn copy(&mut self, item: &Self::Item) -> Self::Item {
312 Self::Item { vector: self.0.copy(&item.vector) }
313 }
314
315 fn clear(&mut self) {
316 self.0.clear();
317 }
318
319 fn reserve_items<'a, I>(&mut self, items: I) where Self: 'a, I: Iterator<Item=&'a Self::Item> + Clone {
320 self.0.reserve_items(items.map(|x| &x.vector));
321 }
322
323 fn reserve_regions<'a, I>(&mut self, regions: I) where Self: 'a, I: Iterator<Item=&'a Self> + Clone {
324 self.0.reserve_regions(regions.map(|r| &r.0));
325 }
326
327 fn heap_size(&self, callback: impl FnMut(usize, usize)) {
328 self.0.heap_size(callback);
329 }
330 }
331}