Trait prefix_sum::summable::Summable
source · 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
- Any piece of code using
add_assign
should be equivalent to the code usingadd_assign_ref
instead, excluding any performance differences. The same must be true forsub_assign
andsub_assign_ref
. - Addition should be associative, that is if
a
,b
andc
are values of a type implementingSummable
, thenmust leavea.add_assign(b); a.add_assign(c);
a
in the same state asb.add_assign(c); a.add_assign(b);
- Addition should be commutative, that is if
a
andb
are values of a type implementingSummable
, thenshould leavea.add_assign(b);
a
in the same state as what the following code leavesb
in:b.add_assign(a);
- The additive identity of
add_assign
should be the value returned byzero
, that is, ifa
is a value of a type implementingSummable
, thenmust not change the value ofa.add_assign(Summable::zero());
a
. - Any value substracted from itself must be zero, that is, if
a
andb
are two values of a type implementingSummable
, then ifa
is equal tob
, thenmust leavea.sub_assign(b);
a
in the same state asa = Summable::zero();
- Subtraction should be equivalent to addition of inverses, that is if
a
andb
are values of any type implementingSummable
, thenmust leavea.sub_assign(b);
a
in the same state aslet 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§
sourcefn zero() -> Self
fn zero() -> Self
Returns the identity for the given type. This should always return the same value.
sourcefn add_assign_ref(&mut self, rhs: &Self)
fn add_assign_ref(&mut self, rhs: &Self)
Add the given value to self.
sourcefn sub_assign_ref(&mut self, rhs: &Self)
fn sub_assign_ref(&mut self, rhs: &Self)
Subtract the given value from self.
sourcefn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Add the given value to self while reusing resources in rhs
.
sourcefn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Subtract the given value from self while reusing resources in rhs
.