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