-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigint.hpp
150 lines (113 loc) · 3.01 KB
/
bigint.hpp
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
#ifndef __BIGINT_HPP_
#define __BIGINT_HPP_
#include <vector>
#include <string>
/** Unsigned long long int */
typedef unsigned long long int ull;
/**
* Big integer class
*/
class bigint {
private:
// Private static members
/** Numeric base */
static const ull _BASE = 1000000000ULL;
// Private static methods
/**
* Naive multiplication
*
* @param a Multiplier
* @param a Multiplicand
* @return Multiplication product
*/
static std::vector<ull> _NAIVE_MULT(const std::vector<ull> &a, const std::vector<ull> &b);
/**
* Karatsuba multiplication
*
* @param a Multiplier
* @param a Multiplicand
* @return Multiplication product
*/
static std::vector<ull> _KARATSUBA_MULT(const std::vector<ull> &a, const std::vector<ull> &b);
// Private members
/** Number data */
std::vector<ull> _data;
// Private constructors
/**
* Big integer constructor from std::vector
*
* @param n Initializator
*/
inline bigint(const std::vector<ull> n) : _data(n) {};
public:
// Public constructors
/**
* Default constructor
*/
inline bigint() : _data(static_cast<std::size_t>(1), 0ULL) {};
/**
* Big integer copy constructor
*
* @param n Number to copy
*/
inline bigint(const bigint &n) : _data(n._data) {};
/**
* Big integer constructor from unsigned long long less than the
* `bigint::_BASE`
*
* @param n Initializator
*/
inline bigint(const ull &n) : _data(static_cast<std::size_t>(1), n) {};
// Constant getters
/**
* Get the number of chunks
*
* @return Number of memory chunks
*/
inline std::size_t chunks() const {
return this->_data.size();
}
// Public type conversion operators overloading
/** Cast to std::string */
operator std::string() const;
// Public assingment operators overloading
/** Direct assignment */
bigint &operator = (const bigint &n);
// Public arithmetic operators overloading
/**
* Addition operator
*
* @param a Augend
* @param b Addend
* @return Addition sum
*/
friend const bigint operator + (const bigint &a, const bigint &b);
/**
* Subtraction operator without extra zeroes removing
*
* @param a Minuend
* @param b Subtrahend
* @return Subtraction difference
*/
friend const bigint operator - (const bigint &a, const bigint &b);
/**
* Multiplication operator
*
* @param a Multiplier
* @param b Multiplicand
* @return Multiplication product
*/
friend const bigint operator * (const bigint &a, const bigint &b);
/**
* Standard output
*
* @param stream Output stream
* @param n Number to output
* @return The same output stream
*/
friend inline std::ostream &operator << (std::ostream &stream, const bigint &n) {
stream << std::string(n);
return stream;
}
};
#endif // __BIGINT_HPP_