#include #include #include "number.h" /* Type Conversion {{{ */ std::string number::to_hex_string() const { if (_operands.empty()) { return std::string("0x0"); } std::ostringstream ss; ss << "0x" << std::hex; for (auto it = _operands.crbegin(); it != _operands.crend(); ++it) { ss << *it; /* If more operands are present, successive ones must be padded. */ ss << std::setfill('0'); ss << std::setw(8); } return ss.str(); } std::string number::to_dec_string() const { if (_operands.empty()) { return std::string("0"); } std::ostringstream ss; ss << std::dec; if (_operands.size() == 1) { ss << _operands.front(); } else { /* TODO */ ss << "[Not implemented]"; } return ss.str(); } /* }}} */ /* Operations {{{ */ number number::operator+(const number &n) const { /* We make sure our object is greater than or equal to n. */ if (*this < n) { return n.operator+(*this); } number result; auto it = _operands.cbegin(); auto it_n = n._operands.cbegin(); bool carry = false; while (it != _operands.cend()) { const auto n1 = *it; const auto n2 = *it_n; result._operands.push_back(n1 + n2 + (carry ? 1 : 0)); if (carry && (n1 == UINT32_MAX)) { carry = true; } else { carry = ((UINT32_MAX - n1 - (carry ? 1 : 0)) < n2); } ++it; ++it_n; } if (carry) { result._operands.push_back(1); } return result; } /* }}} */ /* Comparison operators {{{ */ bool number::operator<(const number &n) const { const auto size = this->_operands.size(); if (size < n._operands.size()) { return true; } if (size > n._operands.size()) { return false; } /* Because first item is the least significant. */ auto l1(this->_operands); auto l2(n._operands); l1.reverse(); l2.reverse(); return l1 < l2; } bool number::operator>(const number &n) const { return n.operator<(*this); } bool number::operator<=(const number &n) const { return operator<(n) || operator==(n); } bool number::operator>=(const number &n) const { return operator>(n) || operator==(n); } bool number::operator==(const number &n) const { return _operands == n._operands; } bool number::operator!=(const number &n) const { return _operands != n._operands; } /* }}} */ std::ostream & operator<<(std::ostream &os, const number &n) { return os << n.to_hex_string(); }