differential_dataflow/
difference.rs1pub trait IsZero {
15 fn is_zero(&self) -> bool;
24}
25
26pub trait Semigroup<Rhs: ?Sized = Self> : Clone + IsZero {
38 fn plus_equals(&mut self, rhs: &Rhs);
40}
41
42impl<'a, S, T: Semigroup<S>> Semigroup<&'a S> for T {
44 fn plus_equals(&mut self, rhs: &&'a S) {
45 self.plus_equals(&**rhs);
46 }
47}
48
49pub trait Monoid : Semigroup {
51 fn zero() -> Self;
53}
54
55pub trait Abelian : Monoid {
61 fn negate(&mut self);
63}
64
65pub trait Multiply<Rhs = Self> {
67 type Output;
69 fn multiply(self, rhs: &Rhs) -> Self::Output;
71}
72
73macro_rules! builtin_implementation {
75 ($t:ty) => {
76 impl IsZero for $t {
77 #[inline] fn is_zero(&self) -> bool { self == &0 }
78 }
79 impl Semigroup for $t {
80 #[inline] fn plus_equals(&mut self, rhs: &Self) { *self += rhs; }
81 }
82
83 impl Monoid for $t {
84 #[inline] fn zero() -> Self { 0 }
85 }
86
87 impl Multiply<Self> for $t {
88 type Output = Self;
89 fn multiply(self, rhs: &Self) -> Self { self * rhs}
90 }
91 };
92}
93
94macro_rules! builtin_abelian_implementation {
95 ($t:ty) => {
96 impl Abelian for $t {
97 #[inline] fn negate(&mut self) { *self = -*self; }
98 }
99 };
100}
101
102builtin_implementation!(i8);
103builtin_implementation!(i16);
104builtin_implementation!(i32);
105builtin_implementation!(i64);
106builtin_implementation!(i128);
107builtin_implementation!(isize);
108builtin_implementation!(u8);
109builtin_implementation!(u16);
110builtin_implementation!(u32);
111builtin_implementation!(u64);
112builtin_implementation!(u128);
113builtin_implementation!(usize);
114
115builtin_abelian_implementation!(i8);
116builtin_abelian_implementation!(i16);
117builtin_abelian_implementation!(i32);
118builtin_abelian_implementation!(i64);
119builtin_abelian_implementation!(i128);
120builtin_abelian_implementation!(isize);
121
122macro_rules! wrapping_implementation {
124 ($t:ty) => {
125 impl IsZero for $t {
126 #[inline] fn is_zero(&self) -> bool { self == &std::num::Wrapping(0) }
127 }
128 impl Semigroup for $t {
129 #[inline] fn plus_equals(&mut self, rhs: &Self) { *self += rhs; }
130 }
131
132 impl Monoid for $t {
133 #[inline] fn zero() -> Self { std::num::Wrapping(0) }
134 }
135
136 impl Abelian for $t {
137 #[inline] fn negate(&mut self) { *self = -*self; }
138 }
139
140 impl Multiply<Self> for $t {
141 type Output = Self;
142 fn multiply(self, rhs: &Self) -> Self { self * rhs}
143 }
144 };
145}
146
147wrapping_implementation!(std::num::Wrapping<i8>);
148wrapping_implementation!(std::num::Wrapping<i16>);
149wrapping_implementation!(std::num::Wrapping<i32>);
150wrapping_implementation!(std::num::Wrapping<i64>);
151wrapping_implementation!(std::num::Wrapping<i128>);
152wrapping_implementation!(std::num::Wrapping<isize>);
153
154
155pub use self::present::Present;
156mod present {
157 use serde::{Deserialize, Serialize};
158
159 #[derive(Copy, Ord, PartialOrd, Eq, PartialEq, Debug, Clone, Serialize, Deserialize, Hash, columnar::Columnar)]
168 pub struct Present;
169
170 impl<T: Clone> super::Multiply<T> for Present {
171 type Output = T;
172 fn multiply(self, rhs: &T) -> T {
173 rhs.clone()
174 }
175 }
176
177 impl super::IsZero for Present {
178 fn is_zero(&self) -> bool { false }
179 }
180
181 impl super::Semigroup for Present {
182 fn plus_equals(&mut self, _rhs: &Self) { }
183 }
184
185 impl columnation::Columnation for Present {
186 type InnerRegion = columnation::CopyRegion<Present>;
187 }
188}
189
190mod tuples {
192
193 use super::{IsZero, Semigroup, Monoid, Abelian, Multiply};
194
195 macro_rules! tuple_implementation {
197 ( ($($name:ident)*), ($($name2:ident)*) ) => (
198
199 impl<$($name: IsZero),*> IsZero for ($($name,)*) {
200 #[allow(unused_mut)]
201 #[allow(non_snake_case)]
202 #[inline] fn is_zero(&self) -> bool {
203 let mut zero = true;
204 let ($(ref $name,)*) = *self;
205 $( zero &= $name.is_zero(); )*
206 zero
207 }
208 }
209
210 impl<$($name: Semigroup),*> Semigroup for ($($name,)*) {
211 #[allow(non_snake_case)]
212 #[inline] fn plus_equals(&mut self, rhs: &Self) {
213 let ($(ref mut $name,)*) = *self;
214 let ($(ref $name2,)*) = *rhs;
215 $($name.plus_equals($name2);)*
216 }
217 }
218
219 impl<$($name: Monoid),*> Monoid for ($($name,)*) {
220 #[allow(non_snake_case)]
221 #[inline] fn zero() -> Self {
222 ( $($name::zero(), )* )
223 }
224 }
225
226 impl<$($name: Abelian),*> Abelian for ($($name,)*) {
227 #[allow(non_snake_case)]
228 #[inline] fn negate(&mut self) {
229 let ($(ref mut $name,)*) = self;
230 $($name.negate();)*
231 }
232 }
233
234 impl<T, $($name: Multiply<T>),*> Multiply<T> for ($($name,)*) {
235 type Output = ($(<$name as Multiply<T>>::Output,)*);
236 #[allow(unused_variables)]
237 #[allow(non_snake_case)]
238 #[inline] fn multiply(self, rhs: &T) -> Self::Output {
239 let ($($name,)*) = self;
240 ( $($name.multiply(rhs), )* )
241 }
242 }
243 )
244 }
245
246 tuple_implementation!((), ());
247 tuple_implementation!((A1), (A2));
248 tuple_implementation!((A1 B1), (A2 B2));
249 tuple_implementation!((A1 B1 C1), (A2 B2 C2));
250 tuple_implementation!((A1 B1 C1 D1), (A2 B2 C2 D2));
251}
252
253mod vector {
255
256 use super::{IsZero, Semigroup, Monoid, Abelian, Multiply};
257
258 impl<R: IsZero> IsZero for Vec<R> {
259 fn is_zero(&self) -> bool {
260 self.iter().all(|x| x.is_zero())
261 }
262 }
263
264 impl<R: Semigroup> Semigroup for Vec<R> {
265 fn plus_equals(&mut self, rhs: &Self) {
266 self.plus_equals(&rhs[..])
267 }
268 }
269
270 impl<R: Semigroup> Semigroup<[R]> for Vec<R> {
271 fn plus_equals(&mut self, rhs: &[R]) {
272 for (index, update) in rhs.iter().enumerate().take(self.len()) {
274 self[index].plus_equals(update);
275 }
276
277 while self.len() < rhs.len() {
279 let element = &rhs[self.len()];
280 self.push(element.clone());
281 }
282 }
283 }
284
285 #[cfg(test)]
286 mod tests {
287 use crate::difference::Semigroup;
288
289 #[test]
290 fn test_semigroup_vec() {
291 let mut a = vec![1,2,3];
292 a.plus_equals([1,1,1,1].as_slice());
293 assert_eq!(vec![2,3,4,1], a);
294 }
295 }
296
297 impl<R: Monoid> Monoid for Vec<R> {
298 fn zero() -> Self {
299 Self::new()
300 }
301 }
302
303 impl<R: Abelian> Abelian for Vec<R> {
304 fn negate(&mut self) {
305 for update in self.iter_mut() {
306 update.negate();
307 }
308 }
309 }
310
311 impl<T, R: Multiply<T>> Multiply<T> for Vec<R> {
312 type Output = Vec<<R as Multiply<T>>::Output>;
313 fn multiply(self, rhs: &T) -> Self::Output {
314 self.into_iter()
315 .map(|x| x.multiply(rhs))
316 .collect()
317 }
318 }
319}