summaryrefslogtreecommitdiff
path: root/number.cpp
blob: f6f445c9799a8ab121cc04d544c59861666606f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "number.h"

/* 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 == UINT64_MAX)) {
            carry = true;
        } else {
            carry = ((UINT64_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;
}

/* }}} */