pub trait Summable {
    fn zero() -> Self;
    fn add_assign_ref(&mut self, rhs: &Self);
    fn sub_assign_ref(&mut self, rhs: &Self);
    fn add_assign(&mut self, rhs: Self);
    fn sub_assign(&mut self, rhs: Self);
}
Expand description

Trait for types that can be used in a PrefixSum.

The distinction between add_assign and add_assign_ref exists to allow types such as BigInt to make proper use of memory allocations as appropriate.

See also the SummableSigned marker trait.

Laws

  1. Any piece of code using add_assign should be equivalent to the code using add_assign_ref instead, excluding any performance differences. The same must be true for sub_assign and sub_assign_ref.
  2. Addition should be associative, that is if a, b and c are values of a type implementing Summable, then
    a.add_assign(b);
    a.add_assign(c);
    must leave a in the same state as
    b.add_assign(c);
    a.add_assign(b);
  3. Addition should be commutative, that is if a and b are values of a type implementing Summable, then
    a.add_assign(b);
    should leave a in the same state as what the following code leaves b in:
    b.add_assign(a);
  4. The additive identity of add_assign should be the value returned by zero, that is, if a is a value of a type implementing Summable, then
    a.add_assign(Summable::zero());
    must not change the value of a.
  5. Any value substracted from itself must be zero, that is, if a and b are two values of a type implementing Summable, then if a is equal to b, then
    a.sub_assign(b);
    must leave a in the same state as
    a = Summable::zero();
  6. Subtraction should be equivalent to addition of inverses, that is if a and b are values of any type implementing Summable, then
    a.sub_assign(b);
    must leave a in the same state as
    let b_tmp = b;
    b = Summable::zero();
    b.sub_assign(b_tmp);
    a.add_assign(b);

Note that usage of a PrefixSum will always result in negative values somewhere, so unsigned integers must implement addition and subtraction in a wrapping manner to be usable.

Required Methods§

Returns the identity for the given type. This should always return the same value.

Add the given value to self.

Subtract the given value from self.

Add the given value to self while reusing resources in rhs.

Subtract the given value from self while reusing resources in rhs.

Implementations on Foreign Types§

Implementors§