summaryrefslogtreecommitdiff
path: root/number.h
blob: d0d2a10d5571a8a647f3bf2d765f87b454f235c3 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#pragma once

#include <list>
#include <ostream>

#include <cstdint>

class number {
public:
    number() = default;
    number(const number &) = default;
    number(number &&) = default;

    number &operator=(const number &) = default;
    number &operator=(number &&) = default;

    number(std::uint64_t);

    /**
     * \brief Return a hexadecimal string representation of this number.
     */
    std::string to_hex_string() const;

    /**
     * \brief Return a decimal string representation of this number.
     */
    std::string to_dec_string() const;

    /**
     * \brief Return a 32-bits-wide integer that corresponds to this number.
     *
     * \throws std::out_of_range The number cannot be represented by a single
     * 32-bits-wide integer.
     */
    std::uint32_t to_uint32() const;

    /**
     * \brief Return a 64-bits-wide integer that corresponds to this number.
     *
     * \throws std::out_of_range The number cannot be represented by a single
     * 64-bits-wide integer.
     */
    std::uint64_t to_uint64() const;

    /**
     * \brief Return true if this number is not equal to 0. Otherwise, return
     * false.
     */
    explicit operator bool() const;

    /**
     * \brief Return the result of the addition of a number and this number.
     *
     * \param n The number to add to this number.
     */
    number operator+(const number &n) const;

    /**
     * \brief Return the result of the multiplication of a number and this
     * number.
     *
     * \param n The number to multiply with this number.
     */
    number operator*(const number &) const;

    /**
     * \brief Return a number equals to this number where n bits have been
     * shifted to the left.
     *
     * \param n Number of bits to shift.
     */
    number operator<<(std::uint32_t n) const;

    /**
     * \brief Return a number equals to this number where n bits have been
     * shifted to the right.
     *
     * \param n Number of bits to shift.
     */
    number operator>>(std::uint32_t) const;

    /**
     * \brief Tell whether the number passed as parameter is strictly less than
     * this number.
     *
     * \param n The number to compare with this number.
     */
    bool operator<(const number &n) const;

    /**
     * \brief Tell whether the number passed as parameter is strictly greater
     * than this number.
     *
     * \param n The number to compare with this number.
     */
    bool operator>(const number &n) const;

    /**
     * \brief Tell whether the number passed as parameter is less than or equal
     * to this number.
     *
     * \param n The number to compare with this number.
     */
    bool operator<=(const number &n) const;

    /**
     * \brief Tell whether the number passed as parameter is greater than or
     * equal to this number.
     *
     * \param n The number to compare with this number.
     */
    bool operator>=(const number &n) const;

    /**
     * \brief Tell whether the number passed as parameter is equal to this
     * number.
     *
     * \param n The number to compare with this number.
     */
    bool operator==(const number &n) const;

    /**
     * \brief Tell whether the number passed as parameter is not equal to this
     * number.
     *
     * \param n The number to compare with this number.
     */
    bool operator!=(const number &n) const;

    /**
     * \brief Tell whether this number evaluates to false.
     */
    bool operator!() const;

    /**
     * \brief Add a number to this number.
     *
     * \param n Number to add to this number.
     */
    number &operator+=(const number &n);

    /**
     * \brief Shift the number n bits to the left.
     *
     * \param n Number of bits to shift.
     */
    number &operator<<=(std::uint32_t);

    /**
     * \brief Shift the number n bits to the right.
     *
     * \param n Number of bits to shift.
     */
    number &operator>>=(std::uint32_t);

    /**
     * \brief Increment this number.
     */
    number &operator++();

private:
    /**
     * \brief Remove unused leading 0 operands.
     *
     * Call this method after shrinking the number so that leading 0 operands
     * are removed.
     */
    void shrink_to_fit();

     /* First item is the least significant. */
    std::list<std::uint32_t> _operands;
};

std::ostream &operator<<(std::ostream &, const number &);