Skip to content

Commit

Permalink
Make Abelian::negate act on borrowed data (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankmcsherry authored May 28, 2024
1 parent 08fce58 commit 83796a9
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 16 deletions.
4 changes: 3 additions & 1 deletion dogsdogsdogs/src/calculus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ where
self.enter(child)
.inner
.flat_map(|(data, time, diff)| {
let neu = (data.clone(), AltNeu::neu(time.time.clone()), diff.clone().negate());
let mut neg_diff = diff.clone();
neg_diff.negate();
let neu = (data.clone(), AltNeu::neu(time.time.clone()), neg_diff);
let alt = (data, time, diff);
Some(alt).into_iter().chain(Some(neu))
})
Expand Down
8 changes: 6 additions & 2 deletions src/algorithms/identifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,16 @@ where
// keep round-positive records as changes.
let ((round, record), count) = &input[0];
if *round > 0 {
output.push(((0, record.clone()), count.clone().negate()));
let mut neg_count = count.clone();
neg_count.negate();
output.push(((0, record.clone()), neg_count));
output.push(((*round, record.clone()), count.clone()));
}
// if any losers, increment their rounds.
for ((round, record), count) in input[1..].iter() {
output.push(((0, record.clone()), count.clone().negate()));
let mut neg_count = count.clone();
neg_count.negate();
output.push(((0, record.clone()), neg_count));
output.push(((*round+1, record.clone()), count.clone()));
}
})
Expand Down
2 changes: 1 addition & 1 deletion src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ impl<G: Scope, D: Data, R: Abelian+'static> Collection<G, D, R> where G::Timesta
/// ```
pub fn negate(&self) -> Collection<G, D, R> {
self.inner
.map_in_place(|x| x.2 = x.2.clone().negate())
.map_in_place(|x| x.2.negate())
.as_collection()
}

Expand Down
17 changes: 8 additions & 9 deletions src/difference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub trait Monoid : Semigroup {
/// not quite as many as you might imagine.
pub trait Abelian : Monoid {
/// The method of `std::ops::Neg`, for types that do not implement `Neg`.
fn negate(self) -> Self;
fn negate(&mut self);
}

/// A replacement for `std::ops::Mul` for types that do not implement it.
Expand Down Expand Up @@ -97,7 +97,7 @@ macro_rules! builtin_implementation {
macro_rules! builtin_abelian_implementation {
($t:ty) => {
impl Abelian for $t {
#[inline] fn negate(self) -> Self { -self }
#[inline] fn negate(&mut self) { *self = -*self; }
}
};
}
Expand Down Expand Up @@ -137,7 +137,7 @@ macro_rules! wrapping_implementation {
}

impl Abelian for $t {
#[inline] fn negate(self) -> Self { -self }
#[inline] fn negate(&mut self) { *self = -*self; }
}

impl Multiply<Self> for $t {
Expand Down Expand Up @@ -225,9 +225,9 @@ mod tuples {

impl<$($name: Abelian),*> Abelian for ($($name,)*) {
#[allow(non_snake_case)]
#[inline] fn negate(self) -> Self {
let ($($name,)*) = self;
( $($name.negate(), )* )
#[inline] fn negate(&mut self) {
let ($(ref mut $name,)*) = self;
$($name.negate();)*
}
}

Expand Down Expand Up @@ -301,11 +301,10 @@ mod vector {
}

impl<R: Abelian> Abelian for Vec<R> {
fn negate(mut self) -> Self {
fn negate(&mut self) {
for update in self.iter_mut() {
*update = update.clone().negate();
update.negate();
}
self
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/operators/arrange/arrangement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ where
if !input.is_empty() {
logic(key, input, change);
}
change.extend(output.drain(..).map(|(x,d)| (x, d.negate())));
change.extend(output.drain(..).map(|(x,mut d)| { d.negate(); (x, d) }));
crate::consolidation::consolidate(change);
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/operators/reduce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ pub trait ReduceCore<G: Scope, K: ToOwned + ?Sized, V: Data, R: Semigroup> where
if !input.is_empty() {
logic(key, input, change);
}
change.extend(output.drain(..).map(|(x,d)| (x, d.negate())));
change.extend(output.drain(..).map(|(x,mut d)| { d.negate(); (x, d) }));
crate::consolidation::consolidate(change);
})
}
Expand Down
6 changes: 5 additions & 1 deletion src/operators/threshold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ pub trait ThresholdTotal<G: Scope, K: ExchangeData, R: ExchangeData+Semigroup> w
fn threshold_total<R2: Abelian+'static, F: FnMut(&K,&R)->R2+'static>(&self, mut thresh: F) -> Collection<G, K, R2> {
self.threshold_semigroup(move |key, new, old| {
let mut new = thresh(key, new);
if let Some(old) = old { new.plus_equals(&thresh(key, old).negate()); }
if let Some(old) = old {
let mut add = thresh(key, old);
add.negate();
new.plus_equals(&add);
}
if !new.is_zero() { Some(new) } else { None }
})
}
Expand Down

0 comments on commit 83796a9

Please sign in to comment.