Commit 1d898e81 authored by Jason Rhinelander's avatar Jason Rhinelander

Bundle::transferTo() - transfer everything

`a.transferTo(b)` is almost equivalent to `a.transfer(a, b)` except
that `a` is also cleared (with just `transfer()`, `a` could end up with
neglible amounts for negative quantities when `b` has nearly-identical
positive quantities of the good).
parent 46cff556
......@@ -484,6 +484,17 @@ BundleSigned BundleSigned::transfer(const BundleSigned &amount, double epsilon)
return actual;
}
BundleSigned BundleSigned::transferTo(BundleSigned &to, double epsilon) {
beginTransaction(true);
to.beginTransaction(true);
BundleSigned ret = transfer(*this, to, epsilon);
clear();
commitTransaction();
to.commitTransaction();
return ret;
}
bool Bundle::hasApprox(const BundleSigned &amount, const Bundle &to, double epsilon) const {
for (auto &g : amount) {
double abs_transfer = std::abs(g.second);
......
......@@ -307,6 +307,13 @@ class BundleSigned {
return transfer(std::forward<Args>(args)...);
}
/** Transfers all quantities held by the caller into the target bundle. This uses
* transfer() internally. It is almost equivalent to `bundle.transfer(bundle, to)`, except
* that `bundle` will always be cleared, while it is possible for `transfer()` to result
* in some neglible quantities remaining in `bundle`.
*/
BundleSigned transferTo(BundleSigned &to, double epsilon = default_transfer_epsilon);
/// Adds two BundleSigned objects together and returns the result.
BundleSigned operator + (const BundleSigned &b) const;
/// Subtracts one BundleSigned from another and returns the result.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment