5
5
#ifndef BIGINT_HH
6
6
#define BIGINT_HH
7
7
8
+ #include < utility>
9
+
8
10
// This one is pretty simple but has a fair divide implementation.
9
11
// Though I'm not ambitious enough to do that FFT-like stuff.
10
12
//
@@ -166,12 +168,11 @@ public:
166
168
BigInt (llong_t ) _fast;
167
169
BigInt (ullong_t ) _fast;
168
170
BigInt (BigInt const &) _fast;
171
+ BigInt (BigInt &&) _fast;
169
172
BigInt (char const *, onedig_t = 10 ) _fast;
170
173
171
- BigInt &operator = (llong_t ) _fast;
172
- BigInt &operator = (ullong_t ) _fast;
173
174
BigInt &operator = (BigInt const &) _fast;
174
- BigInt &operator = (char const * ) _fast;
175
+ BigInt &operator = (BigInt && ) _fast;
175
176
176
177
// Input conversion from text.
177
178
@@ -236,25 +237,6 @@ public:
236
237
BigInt &negate () { if (!is_zero ()) positive = !positive; return *this ; }
237
238
BigInt operator -() const { return BigInt (*this ).negate (); }
238
239
239
- BigInt &operator += (llong_t ) _fast;
240
- BigInt &operator -= (llong_t ) _fast;
241
- BigInt &operator *= (llong_t ) _fast;
242
- BigInt &operator /= (llong_t ) _fast;
243
- BigInt &operator %= (llong_t ) _fast;
244
-
245
- BigInt &operator = (unsigned long x) { return (*this )=(ullong_t )x; }
246
- BigInt &operator += (unsigned long x) { return (*this )+=(ullong_t )x; }
247
- BigInt &operator -= (unsigned long x) { return (*this )-=(ullong_t )x; }
248
- BigInt &operator *= (unsigned long x) { return (*this )*=(ullong_t )x; }
249
- BigInt &operator /= (unsigned long x) { return (*this )/=(ullong_t )x; }
250
- BigInt &operator %= (unsigned long x) { return (*this )%=(ullong_t )x; }
251
-
252
- BigInt &operator += (ullong_t ) _fast;
253
- BigInt &operator -= (ullong_t ) _fast;
254
- BigInt &operator *= (ullong_t ) _fast;
255
- BigInt &operator /= (ullong_t ) _fast;
256
- BigInt &operator %= (ullong_t ) _fast;
257
-
258
240
BigInt &operator += (BigInt const &) _fast;
259
241
BigInt &operator -= (BigInt const &) _fast;
260
242
BigInt &operator *= (BigInt const &) _fast;
@@ -267,78 +249,21 @@ public:
267
249
static void div (BigInt const &, BigInt const &,
268
250
BigInt ", BigInt &rem) _fasta;
269
251
270
- // Avoid the need for explicit casts to [u]llong_t.
271
-
272
- // disabled by DK
273
- // operator int() const { return int (operator llong_t()); }
274
- // operator unsigned() const { return unsigned (operator ullong_t()); }
275
-
276
- BigInt &operator = (int n) { return operator = (llong_t (n)); }
277
- BigInt &operator += (int n) { return operator += (llong_t (n)); }
278
- BigInt &operator -= (int n) { return operator -= (llong_t (n)); }
279
- BigInt &operator *= (int n) { return operator *= (llong_t (n)); }
280
- BigInt &operator /= (int n) { return operator /= (llong_t (n)); }
281
- BigInt &operator %= (int n) { return operator %= (llong_t (n)); }
282
-
283
- BigInt &operator = (unsigned n) { return operator = (ullong_t (n)); }
284
- BigInt &operator += (unsigned n) { return operator += (ullong_t (n)); }
285
- BigInt &operator -= (unsigned n) { return operator -= (ullong_t (n)); }
286
- BigInt &operator *= (unsigned n) { return operator *= (ullong_t (n)); }
287
- BigInt &operator /= (unsigned n) { return operator /= (ullong_t (n)); }
288
- BigInt &operator %= (unsigned n) { return operator %= (ullong_t (n)); }
289
-
290
- // Binary arithmetic operators. These are entirely syntactic sugar.
291
- // Though there's joy in repetition -- let the preprocessor enjoy.
292
-
293
- #define decl_binary (T ) \
294
- BigInt operator + (T b) const { return BigInt (*this ) += b; } \
295
- BigInt operator - (T b) const { return BigInt (*this ) -= b; } \
296
- BigInt operator * (T b) const { return BigInt (*this ) *= b; } \
297
- BigInt operator / (T b) const { return BigInt (*this ) /= b; } \
298
- BigInt operator % (T b) const { return BigInt (*this ) %= b; }
299
- decl_binary (int );
300
- decl_binary (unsigned );
301
- decl_binary (llong_t );
302
- decl_binary (ullong_t );
303
- decl_binary (BigInt const &);
304
- #undef decl_binary
305
-
306
- BigInt operator + (unsigned long b) const { return BigInt (*this ) += (ullong_t )b; } \
307
- BigInt operator - (unsigned long b) const { return BigInt (*this ) -= (ullong_t )b; } \
308
- BigInt operator * (unsigned long b) const { return BigInt (*this ) *= (ullong_t )b; } \
309
- BigInt operator / (unsigned long b) const { return BigInt (*this ) /= (ullong_t )b; } \
310
- BigInt operator % (unsigned long b) const { return BigInt (*this ) %= (ullong_t )b; }
311
-
312
- // Binary comparision operators.
313
-
314
- #define decl_binary (T ) \
315
- bool operator < (T b) const { return compare (b) < 0 ; } \
316
- bool operator > (T b) const { return compare (b) > 0 ; } \
317
- bool operator <= (T b) const { return compare (b) <= 0 ; } \
318
- bool operator >= (T b) const { return compare (b) >= 0 ; } \
319
- bool operator == (T b) const { return compare (b) == 0 ; } \
320
- bool operator != (T b) const { return compare (b) != 0 ; }
321
- decl_binary (int );
322
- decl_binary (unsigned );
323
- decl_binary (llong_t );
324
- decl_binary (ullong_t );
325
- decl_binary (BigInt const &);
326
- #undef decl_binary
327
-
328
- bool operator < (unsigned long b) const { return compare ((ullong_t )b) < 0 ; } \
329
- bool operator > (unsigned long b) const { return compare ((ullong_t )b) > 0 ; } \
330
- bool operator <= (unsigned long b) const { return compare ((ullong_t )b) <= 0 ; } \
331
- bool operator >= (unsigned long b) const { return compare ((ullong_t )b) >= 0 ; } \
332
- bool operator == (unsigned long b) const { return compare ((ullong_t )b) == 0 ; } \
333
- bool operator != (unsigned long b) const { return compare ((ullong_t )b) != 0 ; }
334
-
335
252
// Returns the largest x such that 2^x <= abs() or 0 if input is 0
336
253
// Not part of original BigInt.
337
254
unsigned floorPow2 () const _fast;
338
255
339
256
// Sets the number to the power of two given by the exponent
340
257
// Not part of original BigInt.
341
258
void setPower2 (unsigned exponent) _fast;
259
+
260
+ void swap (BigInt &other)
261
+ {
262
+ std::swap (other.size , size);
263
+ std::swap (other.length , length);
264
+ std::swap (other.digit , digit);
265
+ std::swap (other.positive , positive);
266
+ }
342
267
};
343
268
344
269
@@ -350,5 +275,21 @@ BigInt sqrt (BigInt const &) _fast;
350
275
BigInt gcd (const BigInt &, const BigInt &) _fast;
351
276
BigInt modinv (const BigInt &, const BigInt &) _fast;
352
277
278
+ // Binary arithmetic operators
279
+
280
+ inline BigInt operator + (const BigInt &lhs, const BigInt &rhs) { return BigInt (lhs) += rhs; }
281
+ inline BigInt operator - (const BigInt &lhs, const BigInt &rhs) { return BigInt (lhs) -= rhs; }
282
+ inline BigInt operator * (const BigInt &lhs, const BigInt &rhs) { return BigInt (lhs) *= rhs; }
283
+ inline BigInt operator / (const BigInt &lhs, const BigInt &rhs) { return BigInt (lhs) /= rhs; }
284
+ inline BigInt operator % (const BigInt &lhs, const BigInt &rhs) { return BigInt (lhs) %= rhs; }
285
+
286
+ // Binary comparison operators
287
+
288
+ inline bool operator < (const BigInt &lhs, const BigInt &rhs) { return lhs.compare (rhs) < 0 ; }
289
+ inline bool operator > (const BigInt &lhs, const BigInt &rhs) { return lhs.compare (rhs) > 0 ; }
290
+ inline bool operator <= (const BigInt &lhs, const BigInt &rhs) { return lhs.compare (rhs) <= 0 ; }
291
+ inline bool operator >= (const BigInt &lhs, const BigInt &rhs) { return lhs.compare (rhs) >= 0 ; }
292
+ inline bool operator == (const BigInt &lhs, const BigInt &rhs) { return lhs.compare (rhs) == 0 ; }
293
+ inline bool operator != (const BigInt &lhs, const BigInt &rhs) { return lhs.compare (rhs) != 0 ; }
353
294
354
295
#endif // ndef BIGINT_HH
0 commit comments