differential_dataflow/
difference.rs1use crate::Data;
10
11#[deprecated]
12pub use self::Abelian as Diff;
13
14pub trait Semigroup : ::std::marker::Sized + Data + Clone {
26 fn plus_equals(&mut self, rhs: &Self);
28 fn is_zero(&self) -> bool;
37}
38
39pub trait Monoid : Semigroup {
41 fn zero() -> Self;
43}
44
45pub trait Abelian : Monoid {
51 fn negate(self) -> Self;
53}
54
55pub trait Multiply<Rhs = Self> {
57 type Output;
59 fn multiply(self, rhs: &Rhs) -> Self::Output;
61}
62
63macro_rules! builtin_implementation {
65 ($t:ty) => {
66 impl Semigroup for $t {
67 #[inline] fn plus_equals(&mut self, rhs: &Self) { *self += rhs; }
68 #[inline] fn is_zero(&self) -> bool { self == &0 }
69 }
70
71 impl Monoid for $t {
72 #[inline] fn zero() -> Self { 0 }
73 }
74
75 impl Multiply<Self> for $t {
76 type Output = Self;
77 fn multiply(self, rhs: &Self) -> Self { self * rhs}
78 }
79 };
80}
81
82macro_rules! builtin_abelian_implementation {
83 ($t:ty) => {
84 impl Abelian for $t {
85 #[inline] fn negate(self) -> Self { -self }
86 }
87 };
88}
89
90builtin_implementation!(i8);
91builtin_implementation!(i16);
92builtin_implementation!(i32);
93builtin_implementation!(i64);
94builtin_implementation!(i128);
95builtin_implementation!(isize);
96builtin_implementation!(u8);
97builtin_implementation!(u16);
98builtin_implementation!(u32);
99builtin_implementation!(u64);
100builtin_implementation!(u128);
101builtin_implementation!(usize);
102
103builtin_abelian_implementation!(i8);
104builtin_abelian_implementation!(i16);
105builtin_abelian_implementation!(i32);
106builtin_abelian_implementation!(i64);
107builtin_abelian_implementation!(i128);
108builtin_abelian_implementation!(isize);
109
110macro_rules! wrapping_implementation {
112 ($t:ty) => {
113 impl Semigroup for $t {
114 #[inline] fn plus_equals(&mut self, rhs: &Self) { *self += rhs; }
115 #[inline] fn is_zero(&self) -> bool { self == &std::num::Wrapping(0) }
116 }
117
118 impl Monoid for $t {
119 #[inline] fn zero() -> Self { std::num::Wrapping(0) }
120 }
121
122 impl Abelian for $t {
123 #[inline] fn negate(self) -> Self { -self }
124 }
125
126 impl Multiply<Self> for $t {
127 type Output = Self;
128 fn multiply(self, rhs: &Self) -> Self { self * rhs}
129 }
130 };
131}
132
133wrapping_implementation!(std::num::Wrapping<i8>);
134wrapping_implementation!(std::num::Wrapping<i16>);
135wrapping_implementation!(std::num::Wrapping<i32>);
136wrapping_implementation!(std::num::Wrapping<i64>);
137wrapping_implementation!(std::num::Wrapping<i128>);
138wrapping_implementation!(std::num::Wrapping<isize>);
139
140
141pub use self::present::Present;
142mod present {
143 use abomonation_derive::Abomonation;
144 use serde::{Deserialize, Serialize};
145
146 #[derive(Abomonation, Copy, Ord, PartialOrd, Eq, PartialEq, Debug, Clone, Serialize, Deserialize, Hash)]
155 pub struct Present;
156
157 impl<T: Clone> super::Multiply<T> for Present {
158 type Output = T;
159 fn multiply(self, rhs: &T) -> T {
160 rhs.clone()
161 }
162 }
163
164 impl super::Semigroup for Present {
165 fn plus_equals(&mut self, _rhs: &Self) { }
166 fn is_zero(&self) -> bool { false }
167 }
168}
169
170mod tuples {
172
173 use super::{Semigroup, Monoid, Abelian, Multiply};
174
175 macro_rules! tuple_implementation {
177 ( ($($name:ident)*), ($($name2:ident)*) ) => (
178 impl<$($name: Semigroup),*> Semigroup for ($($name,)*) {
179 #[allow(non_snake_case)]
180 #[inline] fn plus_equals(&mut self, rhs: &Self) {
181 let ($(ref mut $name,)*) = *self;
182 let ($(ref $name2,)*) = *rhs;
183 $($name.plus_equals($name2);)*
184 }
185 #[allow(unused_mut)]
186 #[allow(non_snake_case)]
187 #[inline] fn is_zero(&self) -> bool {
188 let mut zero = true;
189 let ($(ref $name,)*) = *self;
190 $( zero &= $name.is_zero(); )*
191 zero
192 }
193 }
194
195 impl<$($name: Monoid),*> Monoid for ($($name,)*) {
196 #[allow(non_snake_case)]
197 #[inline] fn zero() -> Self {
198 ( $($name::zero(), )* )
199 }
200 }
201
202 impl<$($name: Abelian),*> Abelian for ($($name,)*) {
203 #[allow(non_snake_case)]
204 #[inline] fn negate(self) -> Self {
205 let ($($name,)*) = self;
206 ( $($name.negate(), )* )
207 }
208 }
209
210 impl<T, $($name: Multiply<T>),*> Multiply<T> for ($($name,)*) {
211 type Output = ($(<$name as Multiply<T>>::Output,)*);
212 #[allow(unused_variables)]
213 #[allow(non_snake_case)]
214 #[inline] fn multiply(self, rhs: &T) -> Self::Output {
215 let ($($name,)*) = self;
216 ( $($name.multiply(rhs), )* )
217 }
218 }
219 )
220 }
221
222 tuple_implementation!((), ());
223 tuple_implementation!((A1), (A2));
224 tuple_implementation!((A1 B1), (A2 B2));
225 tuple_implementation!((A1 B1 C1), (A2 B2 C2));
226 tuple_implementation!((A1 B1 C1 D1), (A2 B2 C2 D2));
227}
228
229mod vector {
231
232 use super::{Semigroup, Monoid, Abelian, Multiply};
233
234 impl<R: Semigroup> Semigroup for Vec<R> {
235 fn plus_equals(&mut self, rhs: &Self) {
236 while self.len() < rhs.len() {
238 let element = &rhs[self.len()];
239 self.push(element.clone());
240 }
241
242 for (index, update) in rhs.iter().enumerate() {
244 self[index].plus_equals(update);
245 }
246 }
247 fn is_zero(&self) -> bool {
248 self.iter().all(|x| x.is_zero())
249 }
250 }
251
252 impl<R: Monoid> Monoid for Vec<R> {
253 fn zero() -> Self {
254 Self::new()
255 }
256 }
257
258 impl<R: Abelian> Abelian for Vec<R> {
259 fn negate(mut self) -> Self {
260 for update in self.iter_mut() {
261 *update = update.clone().negate();
262 }
263 self
264 }
265 }
266
267 impl<T, R: Multiply<T>> Multiply<T> for Vec<R> {
268 type Output = Vec<<R as Multiply<T>>::Output>;
269 fn multiply(self, rhs: &T) -> Self::Output {
270 self.into_iter()
271 .map(|x| x.multiply(rhs))
272 .collect()
273 }
274 }
275}