-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpIntClass.cpp
More file actions
263 lines (222 loc) · 6.07 KB
/
Copy pathpIntClass.cpp
File metadata and controls
263 lines (222 loc) · 6.07 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
Copyright © 2024 Claus Vind - Andreasen
This program is free software; you can redistribute it and /or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 - 1307 USA
This General Public License does not permit incorporating your program into proprietary programs.If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library.
If this is what you want to do, use the GNU Library General Public License instead of this License.
*/
#include <iostream>
#include "pIntClass.h"
/******************************************************************************
Below is (some of) the implementation of pIntClass
****************************************************************************/
pIntClass::pIntClass()
{
value.clear();
value.reserve(ReservationSize);
}
pIntClass::pIntClass(const pIntClass& x)
{
value.clear();
value.reserve(ReservationSize);
for (int i = 0; i < x.value.size(); i++) value.push_back(x.value[i]);
}
pIntClass::pIntClass(const int x)
{
int _x = x;
value.clear();
value.reserve(ReservationSize);
if (x < 0) _x = -_x;
for (; _x; _x = _x / MODULUS) {
value.push_back(_x % MODULUS);
}
if (x < 0)
for (size_t ix = 0; ix < value.size();ix++)
value[ix] = -value[ix];
normalize(value);
}
/*
* A pIntClass number is normalized if
* value.size() == 0 -> meaning the number has the value 0
* all non-zero entries in value[] have the same sign and is in the open
* interval ] -modulus,...., modulus[
* (it means that the sign of a non-zero pIntClass2 number is the sign of
* value.back(), the most significant 'digit' non-zero entry).
*
* Addition and Subtraktion may temporarily make a value not normalized,
* normalize will bring it back in order.
*/
void pIntClass::normalize(std::vector<int>& val) {
// drop leading zeroes
while (val.size() && (val.back() == 0)) val.pop_back();
if (val.size()) {
/* low to high negative carry */
int carry = 0;
for (size_t i = 0; i < val.size(); i++)
{
val[i] += carry;
if (val[i] >= MODULUS) {
carry = 1;
val[i] -= MODULUS;
}
else
carry = 0;
}
if (carry) val.push_back(carry);
carry = 0;
/* low to high positive carry */
for (size_t i = 0; i < val.size(); i++)
{
val[i] += carry;
if (val[i] <= -MODULUS) {
carry = -1;
val[i] += MODULUS;
}
else
carry = 0;
}
if (carry) val.push_back(carry);
/* all entries in value now is in ]-modulus...modulus[ */
/* now we need to make sure they have the same sign */
carry = 0;
if (val.back() < 0) { /* negative numberss*/
for (size_t i = 0; i < val.size(); i++) {
val[i] += carry;
carry = 0;
if (val[i] > 0) {
val[i] -= MODULUS;
carry = 1;
}
}
if (carry) val.push_back(carry);
}
else if (val.back() > 0) { /* positive numbers */
for (size_t i = 0; i < val.size(); i++) {
val[i] += carry;
carry = 0;
if (val[i] < 0) {
val[i] += MODULUS;
carry = -1;
}
}
if (carry) val.push_back(carry);
}
while (val.back() == 0) {
val.pop_back();
}
}
}
pIntClass::~pIntClass()
{
value.clear();
}
pIntClass& pIntClass::operator=(const pIntClass& rhs)
{
if ( this != &rhs)
{
value.clear();
for (int i = 0; i < rhs.value.size(); i++) value.push_back(rhs.value[i]);
}
return *this;
}
pIntClass pIntClass::operator=(const int rhs)
{
value.clear();
s64 tmp = 0;
if (rhs < 0) {
tmp = -((s64) rhs);
}
else
tmp = rhs;
do {
if(rhs < 0)
value.push_back(-(tmp % MODULUS));
else
value.push_back(tmp % MODULUS);
tmp = tmp / MODULUS;
} while (tmp > 0);
normalize(value);
return *this;
}
bool pIntClass::operator!=( const pIntClass& b) { return !( *this == b); }
bool pIntClass::operator!=(const int b) { return !(*this == b); }
bool pIntClass::IsBiggerNummerically(const pIntClass& b)
{
if (IsZero() && b.IsZero()) return false;
if (IsZero() && !b.IsZero()) return false;
if (!IsZero() && b.IsZero()) return true;
if (value.size() > b.value.size()) return true;
if (value.size() < b.value.size()) return false;
/* this and b are the same size, but may differ in signs */
for (size_t i = value.size(); i > 0; i--)
{
int a = ( value.back() >= 0) ? value[i-1] : -value[i-1];
int _b = (b.value.back() >= 0) ? b.value[i-1] : -b.value[i-1];
if (a > _b) return true ;
else if (a < _b) return false;
}
return false;
}
void pIntClass::DivModulus() {
switch (value.size())
{
case 0: break;
default:
for (int ix = 0; value.size() && (ix < (value.size() - 1)); ix++)
{
value[ix] = value[ix + 1];
}
value.pop_back();
break;
}
}
int pIntClass::operator[](int index)
{
if (index < value.size())
return value[index]; // we strip the sign !
else
return 0; // elements of value[] are all positive !
}
int pIntClass::Sign()
{
if (value.size() == 0) return 0;// 0 is positive by definition
else return value.back() > 0 ? 1 : -1;
}
int pIntClass::ChSignBit()
{
if (value.size() == 0) return 0;
else
for (size_t i = 0; i < value.size(); i++) value[i] = -value[i];
return (value.back() >= 0) ? 1 : -1;
}
pIntClass& pIntClass::operator++()
{
if (value.size() == 0) value.push_back(1);
else {
value[0]++;
normalize(value);
}
return *this;
}
pIntClass pIntClass::operator++(int)
{
pIntClass res = *this;
++*this;
return res;
}
pIntClass& pIntClass::operator--()
{
if (value.size() == 0) value.push_back(-1);
else {
value[0]--;
normalize(value);
}
return *this;
}
pIntClass pIntClass::operator--(int)
{
pIntClass res = *this;
--*this;
return res;
}