-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLarge.h
153 lines (111 loc) · 3.65 KB
/
Large.h
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
#pragma once
#include <string>
#include <vector>
#include <algorithm>
#include <cstdint>
#include <utility>
#include <stdexcept>
class Large {
public:
Large() : digits_({ 0 }), sign_(Sign::Plus) {}
Large(const std::string& value) noexcept;
Large(int64_t value) {
*this = Large(std::to_string(value));
}
Large(const Large& other) : digits_(other.digits_), sign_(other.sign_) {}
Large(Large&& other) noexcept : digits_(std::exchange(other.digits_, std::vector<uint64_t>())),
sign_(std::exchange(other.sign_, Sign::Plus)) {}
~Large() = default;
bool operator==(const Large& other) const noexcept {
if (IsZero() && other.IsZero()) {
return true;
}
return digits_ == other.digits_ && sign_ == other.sign_;
}
bool operator!=(const Large& other) const noexcept {
return !(*this == other);
}
bool operator<(const Large& other) const noexcept;
bool operator<=(const Large& other) const noexcept {
return *this == other || *this < other;
}
bool operator>(const Large& other) const noexcept {
return !(*this <= other);
}
bool operator>=(const Large& other) const noexcept {
return *this == other || *this > other;
}
Large operator-() const noexcept {
Large other = *this;
other.sign_ = other.sign_ == Sign::Plus ? Sign::Minus : Sign::Plus;
return other;
}
Large operator+() const noexcept {
return *this;
}
Large& operator=(const Large& other) noexcept {
return *this = Large(other);
}
Large& operator=(Large&& other) noexcept {
std::swap(sign_, other.sign_);
digits_.swap(other.digits_);
return *this;
}
Large operator++(int32_t) noexcept {
Large tmp = *this;
*this += 1;
return tmp;
}
Large operator--(int32_t) noexcept {
Large tmp = *this;
*this -= 1;
return tmp;
}
Large& operator++() noexcept {
return *this += 1;
}
Large& operator--() noexcept {
return *this -= 1;
}
Large operator+(const Large& other) const noexcept;
Large operator-(const Large& other) const noexcept;
Large operator*(const Large& other) const noexcept;
Large operator/(const Large& other) const;
Large operator%(const Large& other) const;
Large& operator+=(const Large& other) noexcept {
return *this = *this + other;
}
Large& operator-=(const Large& other) noexcept {
return *this = *this - other;
}
Large& operator*=(const Large& other) noexcept {
return *this = *this * other;
}
Large& operator/=(const Large& other) {
return *this = *this / other;
}
Large& operator%=(const Large& other) {
return *this = *this % other;
}
friend Large abs(const Large& num) noexcept;
friend std::string to_string(const Large& num) noexcept;
friend Large pow(const Large& num, const Large& n);
friend std::istream& operator>>(std::istream& is, Large& num) noexcept;
friend std::ostream& operator<<(std::ostream& os, const Large& num) noexcept;
private:
enum Sign { Plus, Minus };
Sign sign_;
std::vector<uint64_t> digits_;
const uint64_t base_ = 1e9;
bool IsZero() const noexcept {
return digits_.size() == 1 && digits_[0] == 0;
}
uint64_t DigitAt(size_t ind) const noexcept {
if (ind < digits_.size()) {
return digits_[ind];
}
return 0;
}
Large SimpleMult(const Large& lhs, const Large& rhs) const noexcept;
Large MultByBase(int64_t power) const noexcept;
};