palimpsest_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]
78 fn is_zero(&self) -> bool {
79 self == &0
80 }
81 }
82 impl Semigroup for $t {
83 #[inline]
84 fn plus_equals(&mut self, rhs: &Self) {
85 *self += rhs;
86 }
87 }
88
89 impl Monoid for $t {
90 #[inline]
91 fn zero() -> Self {
92 0
93 }
94 }
95
96 impl Multiply<Self> for $t {
97 type Output = Self;
98 fn multiply(self, rhs: &Self) -> Self {
99 self * rhs
100 }
101 }
102 };
103}
104
105macro_rules! builtin_abelian_implementation {
106 ($t:ty) => {
107 impl Abelian for $t {
108 #[inline]
109 fn negate(&mut self) {
110 *self = -*self;
111 }
112 }
113 };
114}
115
116builtin_implementation!(i8);
117builtin_implementation!(i16);
118builtin_implementation!(i32);
119builtin_implementation!(i64);
120builtin_implementation!(i128);
121builtin_implementation!(isize);
122builtin_implementation!(u8);
123builtin_implementation!(u16);
124builtin_implementation!(u32);
125builtin_implementation!(u64);
126builtin_implementation!(u128);
127builtin_implementation!(usize);
128
129builtin_abelian_implementation!(i8);
130builtin_abelian_implementation!(i16);
131builtin_abelian_implementation!(i32);
132builtin_abelian_implementation!(i64);
133builtin_abelian_implementation!(i128);
134builtin_abelian_implementation!(isize);
135
136macro_rules! wrapping_implementation {
138 ($t:ty) => {
139 impl IsZero for $t {
140 #[inline]
141 fn is_zero(&self) -> bool {
142 self == &std::num::Wrapping(0)
143 }
144 }
145 impl Semigroup for $t {
146 #[inline]
147 fn plus_equals(&mut self, rhs: &Self) {
148 *self += rhs;
149 }
150 }
151
152 impl Monoid for $t {
153 #[inline]
154 fn zero() -> Self {
155 std::num::Wrapping(0)
156 }
157 }
158
159 impl Abelian for $t {
160 #[inline]
161 fn negate(&mut self) {
162 *self = -*self;
163 }
164 }
165
166 impl Multiply<Self> for $t {
167 type Output = Self;
168 fn multiply(self, rhs: &Self) -> Self {
169 self * rhs
170 }
171 }
172 };
173}
174
175wrapping_implementation!(std::num::Wrapping<i8>);
176wrapping_implementation!(std::num::Wrapping<i16>);
177wrapping_implementation!(std::num::Wrapping<i32>);
178wrapping_implementation!(std::num::Wrapping<i64>);
179wrapping_implementation!(std::num::Wrapping<i128>);
180wrapping_implementation!(std::num::Wrapping<isize>);
181
182pub use self::present::Present;
183mod present {
184 use serde::{Deserialize, Serialize};
185
186 #[derive(Copy, Ord, PartialOrd, Eq, PartialEq, Debug, Clone, Serialize, Deserialize, Hash)]
195 pub struct Present;
196
197 impl<T: Clone> super::Multiply<T> for Present {
198 type Output = T;
199 fn multiply(self, rhs: &T) -> T {
200 rhs.clone()
201 }
202 }
203
204 impl super::IsZero for Present {
205 fn is_zero(&self) -> bool {
206 false
207 }
208 }
209
210 impl super::Semigroup for Present {
211 fn plus_equals(&mut self, _rhs: &Self) {}
212 }
213}
214
215mod tuples {
217
218 use super::{Abelian, IsZero, Monoid, Multiply, Semigroup};
219
220 macro_rules! tuple_implementation {
222 ( ($($name:ident)*), ($($name2:ident)*) ) => (
223
224 impl<$($name: IsZero),*> IsZero for ($($name,)*) {
225 #[allow(unused_mut)]
226 #[allow(non_snake_case)]
227 #[inline] fn is_zero(&self) -> bool {
228 let mut zero = true;
229 let ($(ref $name,)*) = *self;
230 $( zero &= $name.is_zero(); )*
231 zero
232 }
233 }
234
235 impl<$($name: Semigroup),*> Semigroup for ($($name,)*) {
236 #[allow(non_snake_case)]
237 #[inline] fn plus_equals(&mut self, rhs: &Self) {
238 let ($(ref mut $name,)*) = *self;
239 let ($(ref $name2,)*) = *rhs;
240 $($name.plus_equals($name2);)*
241 }
242 }
243
244 impl<$($name: Monoid),*> Monoid for ($($name,)*) {
245 #[allow(non_snake_case)]
246 #[inline] fn zero() -> Self {
247 ( $($name::zero(), )* )
248 }
249 }
250
251 impl<$($name: Abelian),*> Abelian for ($($name,)*) {
252 #[allow(non_snake_case)]
253 #[inline] fn negate(&mut self) {
254 let ($(ref mut $name,)*) = self;
255 $($name.negate();)*
256 }
257 }
258
259 impl<T, $($name: Multiply<T>),*> Multiply<T> for ($($name,)*) {
260 type Output = ($(<$name as Multiply<T>>::Output,)*);
261 #[allow(unused_variables)]
262 #[allow(non_snake_case)]
263 #[inline] fn multiply(self, rhs: &T) -> Self::Output {
264 let ($($name,)*) = self;
265 ( $($name.multiply(rhs), )* )
266 }
267 }
268 )
269 }
270
271 tuple_implementation!((), ());
272 tuple_implementation!((A1), (A2));
273 tuple_implementation!((A1 B1), (A2 B2));
274 tuple_implementation!((A1 B1 C1), (A2 B2 C2));
275 tuple_implementation!((A1 B1 C1 D1), (A2 B2 C2 D2));
276}
277
278mod vector {
280
281 use super::{Abelian, IsZero, Monoid, Multiply, Semigroup};
282
283 impl<R: IsZero> IsZero for Vec<R> {
284 fn is_zero(&self) -> bool {
285 self.iter().all(|x| x.is_zero())
286 }
287 }
288
289 impl<R: Semigroup> Semigroup for Vec<R> {
290 fn plus_equals(&mut self, rhs: &Self) {
291 self.plus_equals(&rhs[..])
292 }
293 }
294
295 impl<R: Semigroup> Semigroup<[R]> for Vec<R> {
296 fn plus_equals(&mut self, rhs: &[R]) {
297 for (index, update) in rhs.iter().enumerate().take(self.len()) {
299 self[index].plus_equals(update);
300 }
301
302 while self.len() < rhs.len() {
304 let element = &rhs[self.len()];
305 self.push(element.clone());
306 }
307 }
308 }
309
310 #[cfg(test)]
311 mod tests {
312 use crate::difference::Semigroup;
313
314 #[test]
315 fn test_semigroup_vec() {
316 let mut a = vec![1, 2, 3];
317 a.plus_equals([1, 1, 1, 1].as_slice());
318 assert_eq!(vec![2, 3, 4, 1], a);
319 }
320 }
321
322 impl<R: Monoid> Monoid for Vec<R> {
323 fn zero() -> Self {
324 Self::new()
325 }
326 }
327
328 impl<R: Abelian> Abelian for Vec<R> {
329 fn negate(&mut self) {
330 for update in self.iter_mut() {
331 update.negate();
332 }
333 }
334 }
335
336 impl<T, R: Multiply<T>> Multiply<T> for Vec<R> {
337 type Output = Vec<<R as Multiply<T>>::Output>;
338 fn multiply(self, rhs: &T) -> Self::Output {
339 self.into_iter().map(|x| x.multiply(rhs)).collect()
340 }
341 }
342}