diff options
Diffstat (limited to 'balance.cc')
-rw-r--r-- | balance.cc | 657 |
1 files changed, 200 insertions, 457 deletions
@@ -1,529 +1,272 @@ -#include "balance.h" -#include "util.h" +/* + * Copyright (c) 2003-2007, John Wiegley. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of New Artisans LLC nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ -#include <deque> -#include <algorithm> +#include "balance.h" namespace ledger { -amount_t balance_t::amount(const commodity_t& commodity) const -{ - if (! commodity) { - if (amounts.size() == 1) { - amounts_map::const_iterator i = amounts.begin(); - return (*i).second; - } - else if (amounts.size() > 1) { - // Try stripping annotations before giving an error. - balance_t temp(strip_annotations()); - if (temp.amounts.size() == 1) - return temp.amount(commodity); - - std::ostringstream errmsg; - errmsg << "Requested amount of a balance with multiple commodities: " - << temp; - throw new amount_error(errmsg.str()); - } - } - else if (amounts.size() > 0) { - amounts_map::const_iterator i = amounts.find(&commodity); - if (i != amounts.end()) - return (*i).second; - } - return amount_t(); -} - -balance_t balance_t::value(const datetime_t& moment) const +balance_t& balance_t::operator+=(const balance_t& bal) { - balance_t temp; - - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); + for (amounts_map::const_iterator i = bal.amounts.begin(); + i != bal.amounts.end(); i++) - temp += (*i).second.value(moment); - - return temp; + *this += i->second; + return *this; } -balance_t balance_t::price() const +balance_t& balance_t::operator+=(const amount_t& amt) { - balance_t temp; + if (amt.is_null()) + throw_(balance_error, + "Cannot add an uninitialized amount to a balance"); - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); - i++) - temp += (*i).second.price(); + if (amt.is_realzero()) + return *this; - return temp; -} + amounts_map::iterator i = amounts.find(&amt.commodity()); + if (i != amounts.end()) + i->second += amt; + else + amounts.insert(amounts_map::value_type(&amt.commodity(), amt)); -datetime_t balance_t::date() const -{ - datetime_t temp; - - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); - i++) { - datetime_t date = (*i).second.date(); - if (! temp && date) - temp = date; - else if (temp != date) - return datetime_t(); - } - - return temp; + return *this; } -balance_t balance_t::strip_annotations(const bool keep_price, - const bool keep_date, - const bool keep_tag) const +balance_t& balance_t::operator-=(const balance_t& bal) { - balance_t temp; - - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); + for (amounts_map::const_iterator i = bal.amounts.begin(); + i != bal.amounts.end(); i++) - temp += (*i).second.strip_annotations(keep_price, keep_date, keep_tag); - - return temp; + *this -= i->second; + return *this; } -void balance_t::write(std::ostream& out, - const int first_width, - const int latter_width) const +balance_t& balance_t::operator-=(const amount_t& amt) { - bool first = true; - int lwidth = latter_width; - - if (lwidth == -1) - lwidth = first_width; - - if (commodity_t::commodities_sorted) { - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); - i++) { - int width; - if (! first) { - out << std::endl; - width = lwidth; - } else { - first = false; - width = first_width; - } - - out.width(width); - out.fill(' '); - out << std::right << (*i).second; - } + if (amt.is_null()) + throw_(balance_error, + "Cannot subtract an uninitialized amount from a balance"); + + if (amt.is_realzero()) + return *this; + + amounts_map::iterator i = amounts.find(&amt.commodity()); + if (i != amounts.end()) { + i->second -= amt; + if (i->second.is_realzero()) + amounts.erase(i); } else { - typedef std::deque<const amount_t *> amounts_deque; - amounts_deque sorted; - - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); - i++) - if ((*i).second) - sorted.push_back(&(*i).second); - - std::stable_sort(sorted.begin(), sorted.end(), compare_amount_commodities()); - - for (amounts_deque::const_iterator i = sorted.begin(); - i != sorted.end(); - i++) { - int width; - if (! first) { - out << std::endl; - width = lwidth; - } else { - first = false; - width = first_width; - } - - out.width(width); - out.fill(' '); - out << std::right << **i; - } - } - - if (first) { - out.width(first_width); - out.fill(' '); - out << std::right << "0"; - } -} - -balance_t& balance_t::operator*=(const balance_t& bal) -{ - if (realzero() || bal.realzero()) { - return *this = 0L; - } - else if (bal.amounts.size() == 1) { - return *this *= (*bal.amounts.begin()).second; - } - else if (amounts.size() == 1) { - return *this = bal * *this; - } - else { - // Since we would fail with an error at this point otherwise, try - // stripping annotations to see if we can come up with a - // reasonable result. The user will not notice any annotations - // missing (since they are viewing a stripped report anyway), only - // that some of their value expression may not see any pricing or - // date data because of this operation. - - balance_t temp(bal.strip_annotations()); - if (temp.amounts.size() == 1) - return *this *= temp; - temp = strip_annotations(); - if (temp.amounts.size() == 1) - return *this = bal * temp; - - std::ostringstream errmsg; - errmsg << "Cannot multiply two balances: " << temp << " * " << bal; - throw new amount_error(errmsg.str()); + amounts.insert(amounts_map::value_type(&amt.commodity(), amt.negate())); } + return *this; } balance_t& balance_t::operator*=(const amount_t& amt) { - if (realzero() || amt.realzero()) { - return *this = 0L; + if (amt.is_null()) + throw_(balance_error, + "Cannot multiply a balance by an uninitialized amount"); + + if (is_realzero()) { + ; + } + else if (amt.is_realzero()) { + *this = amt; } else if (! amt.commodity()) { - // Multiplying by the null commodity causes all amounts to be - // increased by the same factor. + // Multiplying by an amount with no commodity causes all the + // component amounts to be increased by the same factor. for (amounts_map::iterator i = amounts.begin(); i != amounts.end(); i++) - (*i).second *= amt; + i->second *= amt; } else if (amounts.size() == 1) { - *this = (*amounts.begin()).second * amt; + // Multiplying by a commoditized amount is only valid if the sole + // commodity in the balance is of the same kind as the amount's + // commodity. + if (*amounts.begin()->first == amt.commodity()) + amounts.begin()->second *= amt; + else + throw_(balance_error, + "Cannot multiply a balance with annotated commodities by a commoditized amount"); } else { - amounts_map::iterator i = amounts.find(&amt.commodity()); - if (i != amounts.end()) { - (*i).second *= amt; - } else { - // Try stripping annotations before giving an error. - balance_t temp(strip_annotations()); - if (temp.amounts.size() == 1) { - return *this = (*temp.amounts.begin()).second * amt; - } else { - i = temp.amounts.find(&amt.commodity()); - if (i != temp.amounts.end()) - return *this = temp * amt; - } - - std::ostringstream errmsg; - errmsg << "Attempt to multiply balance by a commodity" - << " not found in that balance: " - << temp << " * " << amt; - throw new amount_error(errmsg.str()); - } + assert(amounts.size() > 1); + throw_(balance_error, + "Cannot multiply a multi-commodity balance by a commoditized amount"); } return *this; } -balance_t& balance_t::operator/=(const balance_t& bal) -{ - if (bal.realzero()) { - std::ostringstream errmsg; - errmsg << "Attempt to divide by zero: " << *this << " / " << bal; - throw new amount_error(errmsg.str()); - } - else if (realzero()) { - return *this = 0L; - } - else if (bal.amounts.size() == 1) { - return *this /= (*bal.amounts.begin()).second; - } - else if (*this == bal) { - return *this = 1L; - } - else { - // Try stripping annotations before giving an error. - balance_t temp(bal.strip_annotations()); - if (temp.amounts.size() == 1) - return *this /= temp; - - std::ostringstream errmsg; - errmsg << "Cannot divide between two balances: " << temp << " / " << bal; - throw new amount_error(errmsg.str()); - } -} - balance_t& balance_t::operator/=(const amount_t& amt) { - if (amt.realzero()) { - std::ostringstream errmsg; - errmsg << "Attempt to divide by zero: " << *this << " / " << amt; - throw new amount_error(errmsg.str()); + if (amt.is_null()) + throw_(balance_error, + "Cannot divide a balance by an uninitialized amount"); + + if (is_realzero()) { + ; } - else if (realzero()) { - return *this = 0L; + else if (amt.is_realzero()) { + throw_(balance_error, "Divide by zero"); } else if (! amt.commodity()) { - // Dividing by the null commodity causes all amounts to be - // decreased by the same factor. + // Dividing by an amount with no commodity causes all the + // component amounts to be divided by the same factor. for (amounts_map::iterator i = amounts.begin(); i != amounts.end(); i++) - (*i).second /= amt; + i->second /= amt; } - else if (amounts.size() == 1 && - (*amounts.begin()).first == &amt.commodity()) { - (*amounts.begin()).second /= amt; + else if (amounts.size() == 1) { + // Dividing by a commoditized amount is only valid if the sole + // commodity in the balance is of the same kind as the amount's + // commodity. + if (*amounts.begin()->first == amt.commodity()) + amounts.begin()->second /= amt; + else + throw_(balance_error, + "Cannot divide a balance with annotated commodities by a commoditized amount"); } else { - amounts_map::iterator i = amounts.find(&amt.commodity()); - if (i != amounts.end()) { - (*i).second /= amt; - } else { - // Try stripping annotations before giving an error. - balance_t temp(strip_annotations()); - if (temp.amounts.size() == 1 && - (*temp.amounts.begin()).first == &amt.commodity()) - return *this = temp / amt; - - std::ostringstream errmsg; - errmsg << "Attempt to divide balance by a commodity" - << " not found in that balance: " - << temp << " * " << amt; - throw new amount_error(errmsg.str()); - } + assert(amounts.size() > 1); + throw_(balance_error, + "Cannot divide a multi-commodity balance by a commoditized amount"); } return *this; } -balance_t::operator amount_t() const +optional<balance_t> +balance_t::value(const optional<moment_t>& moment) const { - if (amounts.size() == 1) { - return (*amounts.begin()).second; - } - else if (amounts.size() == 0) { - return amount_t(); - } - else { - // Try stripping annotations before giving an error. - balance_t temp(strip_annotations()); - if (temp.amounts.size() == 1) - return (*temp.amounts.begin()).second; - - std::ostringstream errmsg; - errmsg << "Cannot convert a balance with " - << "multiple commodities to an amount: " << temp; - throw new amount_error(errmsg.str()); - } -} + optional<balance_t> temp; -} // namespace ledger + for (amounts_map::const_iterator i = amounts.begin(); + i != amounts.end(); + i++) + if (optional<amount_t> val = i->second.value(moment)) { + if (! temp) + temp = balance_t(); + *temp += *val; + } -#ifdef USE_BOOST_PYTHON + return temp; +} -#include <boost/python.hpp> +optional<amount_t> +balance_t::commodity_amount(const optional<const commodity_t&>& commodity) const +{ + // jww (2007-05-20): Needs work + if (! commodity) { + if (amounts.size() == 1) { + amounts_map::const_iterator i = amounts.begin(); + return i->second; + } + else if (amounts.size() > 1) { + // Try stripping annotations before giving an error. + balance_t temp(strip_annotations()); + if (temp.amounts.size() == 1) + return temp.commodity_amount(commodity); -using namespace boost::python; -using namespace ledger; + throw_(amount_error, + "Requested amount of a balance with multiple commodities: " << temp); + } + } + else if (amounts.size() > 0) { + amounts_map::const_iterator i = amounts.find(&*commodity); + if (i != amounts.end()) + return i->second; + } + return none; +} -unsigned int balance_len(balance_t& bal) +balance_t balance_t::strip_annotations(const bool keep_price, + const bool keep_date, + const bool keep_tag) const { - return bal.amounts.size(); + balance_t temp; + + for (amounts_map::const_iterator i = amounts.begin(); + i != amounts.end(); + i++) + temp += i->second.strip_annotations(keep_price, keep_date, keep_tag); + + return temp; } -amount_t balance_getitem(balance_t& bal, int i) +void balance_t::print(std::ostream& out, + const int first_width, + const int latter_width) const { - std::size_t len = bal.amounts.size(); + bool first = true; + int lwidth = latter_width; - if (abs(i) >= len) { - PyErr_SetString(PyExc_IndexError, "Index out of range"); - throw_error_already_set(); - } + if (lwidth == -1) + lwidth = first_width; - int x = i < 0 ? len + i : i; - amounts_map::iterator elem = bal.amounts.begin(); - while (--x >= 0) - elem++; + typedef std::vector<const amount_t *> amounts_array; + amounts_array sorted; - return (*elem).second; -} + for (amounts_map::const_iterator i = amounts.begin(); + i != amounts.end(); + i++) + if (i->second) + sorted.push_back(&i->second); -unsigned int balance_pair_len(balance_pair_t& bal_pair) -{ - return balance_len(bal_pair.quantity); -} + std::stable_sort(sorted.begin(), sorted.end(), + compare_amount_commodities()); -amount_t balance_pair_getitem(balance_pair_t& bal_pair, int i) -{ - return balance_getitem(bal_pair.quantity, i); -} + for (amounts_array::const_iterator i = sorted.begin(); + i != sorted.end(); + i++) { + int width; + if (! first) { + out << std::endl; + width = lwidth; + } else { + first = false; + width = first_width; + } -void export_balance() -{ - class_< balance_t > ("Balance") - .def(init<balance_t>()) - .def(init<amount_t>()) - .def(init<long>()) - .def(init<unsigned long>()) - .def(init<double>()) - - .def(self += self) - .def(self += other<amount_t>()) - .def(self += long()) - .def(self + self) - .def(self + other<amount_t>()) - .def(self + long()) - .def(self -= self) - .def(self -= other<amount_t>()) - .def(self -= long()) - .def(self - self) - .def(self - other<amount_t>()) - .def(self - long()) - .def(self *= self) - .def(self *= other<amount_t>()) - .def(self *= long()) - .def(self * self) - .def(self * other<amount_t>()) - .def(self * long()) - .def(self /= self) - .def(self /= other<amount_t>()) - .def(self /= long()) - .def(self / self) - .def(self / other<amount_t>()) - .def(self / long()) - .def(- self) - - .def(self < self) - .def(self < other<amount_t>()) - .def(self < long()) - .def(self <= self) - .def(self <= other<amount_t>()) - .def(self <= long()) - .def(self > self) - .def(self > other<amount_t>()) - .def(self > long()) - .def(self >= self) - .def(self >= other<amount_t>()) - .def(self >= long()) - .def(self == self) - .def(self == other<amount_t>()) - .def(self == long()) - .def(self != self) - .def(self != other<amount_t>()) - .def(self != long()) - .def(! self) - - .def(abs(self)) - .def(self_ns::str(self)) - - .def("__len__", balance_len) - .def("__getitem__", balance_getitem) - - .def("valid", &balance_t::valid) - - .def("realzero", &balance_t::realzero) - .def("amount", &balance_t::amount) - .def("value", &balance_t::value) - .def("price", &balance_t::price) - .def("date", &balance_t::date) - .def("strip_annotations", &balance_t::strip_annotations) - .def("write", &balance_t::write) - .def("round", &balance_t::round) - .def("negate", &balance_t::negate) - .def("negated", &balance_t::negated) - ; + out.width(width); + out.fill(' '); + out << std::right << **i; + } - class_< balance_pair_t > ("BalancePair") - .def(init<balance_pair_t>()) - .def(init<balance_t>()) - .def(init<amount_t>()) - .def(init<long>()) - .def(init<unsigned long>()) - .def(init<double>()) - - .def(self += self) - .def(self += other<balance_t>()) - .def(self += other<amount_t>()) - .def(self += long()) - .def(self + self) - .def(self + other<balance_t>()) - .def(self + other<amount_t>()) - .def(self + long()) - .def(self -= self) - .def(self -= other<balance_t>()) - .def(self -= other<amount_t>()) - .def(self -= long()) - .def(self - self) - .def(self - other<balance_t>()) - .def(self - other<amount_t>()) - .def(self - long()) - .def(self *= self) - .def(self *= other<balance_t>()) - .def(self *= other<amount_t>()) - .def(self *= long()) - .def(self * self) - .def(self * other<balance_t>()) - .def(self * other<amount_t>()) - .def(self * long()) - .def(self /= self) - .def(self /= other<balance_t>()) - .def(self /= other<amount_t>()) - .def(self /= long()) - .def(self / self) - .def(self / other<balance_t>()) - .def(self / other<amount_t>()) - .def(self / long()) - .def(- self) - - .def(self < self) - .def(self < other<balance_t>()) - .def(self < other<amount_t>()) - .def(self < long()) - .def(self <= self) - .def(self <= other<balance_t>()) - .def(self <= other<amount_t>()) - .def(self <= long()) - .def(self > self) - .def(self > other<balance_t>()) - .def(self > other<amount_t>()) - .def(self > long()) - .def(self >= self) - .def(self >= other<balance_t>()) - .def(self >= other<amount_t>()) - .def(self >= long()) - .def(self == self) - .def(self == other<balance_t>()) - .def(self == other<amount_t>()) - .def(self == long()) - .def(self != self) - .def(self != other<balance_t>()) - .def(self != other<amount_t>()) - .def(self != long()) - .def(! self) - - .def(abs(self)) - .def(self_ns::str(self)) - - .def("__len__", balance_pair_len) - .def("__getitem__", balance_pair_getitem) - - .def("valid", &balance_pair_t::valid) - - .def("realzero", &balance_pair_t::realzero) - .def("amount", &balance_pair_t::amount) - .def("value", &balance_pair_t::value) - .def("price", &balance_pair_t::price) - .def("date", &balance_pair_t::date) - .def("strip_annotations", &balance_pair_t::strip_annotations) - .def("write", &balance_pair_t::write) - .def("round", &balance_pair_t::round) - .def("negate", &balance_pair_t::negate) - .def("negated", &balance_pair_t::negated) - - .add_property("cost", - make_getter(&balance_pair_t::cost, - return_value_policy<reference_existing_object>())) - ; + if (first) { + out.width(first_width); + out.fill(' '); + out << std::right << "0"; + } } -#endif // USE_BOOST_PYTHON +} // namespace ledger |