Skip to content

Commit 0b18367

Browse files
committed
Update Big-Int with copy, move, swap
Fixes: #1340
1 parent c8f69b5 commit 0b18367

File tree

2 files changed

+38
-221
lines changed

2 files changed

+38
-221
lines changed

src/big-int/bigint.cc

Lines changed: 10 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,12 @@ BigInt::BigInt (BigInt const &y)
464464
memcpy (digit, y.digit, length * sizeof (onedig_t));
465465
}
466466

467+
BigInt::BigInt (BigInt &&y)
468+
: BigInt()
469+
{
470+
swap(y);
471+
}
472+
467473
BigInt::BigInt (char const *s, onedig_t b)
468474
: size (adjust_size (small)),
469475
length (0),
@@ -473,40 +479,18 @@ BigInt::BigInt (char const *s, onedig_t b)
473479
scan (s, b);
474480
}
475481

476-
477-
BigInt &
478-
BigInt::operator= (llong_t l)
479-
{
480-
reallocate (small);
481-
assign (l);
482-
return *this;
483-
}
484-
485-
BigInt &
486-
BigInt::operator= (ullong_t ul)
487-
{
488-
reallocate (small);
489-
assign (ul);
490-
return *this;
491-
}
492-
493482
BigInt &
494483
BigInt::operator= (BigInt const &y)
495484
{
496-
if (&y != this)
497-
{
498-
reallocate (y.length);
499-
length = y.length;
500-
positive = y.positive;
501-
memcpy (digit, y.digit, length * sizeof (onedig_t));
502-
}
485+
BigInt copy(y);
486+
swap(copy);
503487
return *this;
504488
}
505489

506490
BigInt &
507-
BigInt::operator= (char const *s)
491+
BigInt::operator= (BigInt &&y)
508492
{
509-
scan (s);
493+
swap(y);
510494
return *this;
511495
}
512496

@@ -916,114 +900,6 @@ BigInt::mul (onedig_t const *dig, unsigned len, bool pos)
916900
}
917901

918902

919-
BigInt &
920-
BigInt::operator+= (llong_t y)
921-
{
922-
bool pos = y > 0;
923-
ullong_t uy = pos ? y : -y;
924-
onedig_t yb[small];
925-
unsigned yl;
926-
digit_set (uy, yb, yl);
927-
add (yb, yl, pos);
928-
return *this;
929-
}
930-
931-
BigInt &
932-
BigInt::operator-= (llong_t y)
933-
{
934-
bool pos = y > 0;
935-
ullong_t uy = pos ? y : -y;
936-
onedig_t yb[small];
937-
unsigned yl;
938-
digit_set (uy, yb, yl);
939-
add (yb, yl, !pos);
940-
return *this;
941-
}
942-
943-
BigInt &
944-
BigInt::operator*= (llong_t y)
945-
{
946-
bool pos = y > 0;
947-
ullong_t uy = pos ? y : -y;
948-
onedig_t yb[small];
949-
unsigned yl;
950-
digit_set (uy, yb, yl);
951-
mul (yb, yl, pos);
952-
return *this;
953-
}
954-
955-
BigInt &
956-
BigInt::operator/= (llong_t y)
957-
{
958-
bool pos = y > 0;
959-
ullong_t uy = pos ? y : -y;
960-
onedig_t yb[small];
961-
unsigned yl;
962-
digit_set (uy, yb, yl);
963-
return *this /= BigInt (yb, yl, pos);
964-
}
965-
966-
BigInt &
967-
BigInt::operator%= (llong_t y)
968-
{
969-
bool pos = y > 0;
970-
ullong_t uy = pos ? y : -y;
971-
onedig_t yb[small];
972-
unsigned yl;
973-
digit_set (uy, yb, yl);
974-
return *this %= BigInt (yb, yl, pos);
975-
}
976-
977-
978-
BigInt &
979-
BigInt::operator+= (ullong_t uy)
980-
{
981-
onedig_t yb[small];
982-
unsigned yl;
983-
digit_set (uy, yb, yl);
984-
add (yb, yl, true);
985-
return *this;
986-
}
987-
988-
BigInt &
989-
BigInt::operator-= (ullong_t uy)
990-
{
991-
onedig_t yb[small];
992-
unsigned yl;
993-
digit_set (uy, yb, yl);
994-
add (yb, yl, false);
995-
return *this;
996-
}
997-
998-
BigInt &
999-
BigInt::operator*= (ullong_t uy)
1000-
{
1001-
onedig_t yb[small];
1002-
unsigned yl;
1003-
digit_set (uy, yb, yl);
1004-
mul (yb, yl, true);
1005-
return *this;
1006-
}
1007-
1008-
BigInt &
1009-
BigInt::operator/= (ullong_t uy)
1010-
{
1011-
onedig_t yb[small];
1012-
unsigned yl;
1013-
digit_set (uy, yb, yl);
1014-
return *this /= BigInt (yb, yl, true);
1015-
}
1016-
1017-
BigInt &
1018-
BigInt::operator%= (ullong_t uy)
1019-
{
1020-
onedig_t yb[small];
1021-
unsigned yl;
1022-
digit_set (uy, yb, yl);
1023-
return *this %= BigInt (yb, yl, true);
1024-
}
1025-
1026-
1027903
BigInt &
1028904
BigInt::operator+= (BigInt const &y)
1029905
{

src/big-int/bigint.hh

Lines changed: 28 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef BIGINT_HH
66
#define BIGINT_HH
77

8+
#include <utility>
9+
810
// This one is pretty simple but has a fair divide implementation.
911
// Though I'm not ambitious enough to do that FFT-like stuff.
1012
//
@@ -166,12 +168,11 @@ public:
166168
BigInt (llong_t) _fast;
167169
BigInt (ullong_t) _fast;
168170
BigInt (BigInt const &) _fast;
171+
BigInt (BigInt &&) _fast;
169172
BigInt (char const *, onedig_t = 10) _fast;
170173

171-
BigInt &operator= (llong_t) _fast;
172-
BigInt &operator= (ullong_t) _fast;
173174
BigInt &operator= (BigInt const &) _fast;
174-
BigInt &operator= (char const *) _fast;
175+
BigInt &operator= (BigInt &&) _fast;
175176

176177
// Input conversion from text.
177178

@@ -236,25 +237,6 @@ public:
236237
BigInt &negate() { if(!is_zero()) positive = !positive; return *this; }
237238
BigInt operator-() const { return BigInt (*this).negate(); }
238239

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-
258240
BigInt &operator+= (BigInt const &) _fast;
259241
BigInt &operator-= (BigInt const &) _fast;
260242
BigInt &operator*= (BigInt const &) _fast;
@@ -267,78 +249,21 @@ public:
267249
static void div (BigInt const &, BigInt const &,
268250
BigInt &quot, BigInt &rem) _fasta;
269251

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-
335252
// Returns the largest x such that 2^x <= abs() or 0 if input is 0
336253
// Not part of original BigInt.
337254
unsigned floorPow2 () const _fast;
338255

339256
// Sets the number to the power of two given by the exponent
340257
// Not part of original BigInt.
341258
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+
}
342267
};
343268

344269

@@ -350,5 +275,21 @@ BigInt sqrt (BigInt const &) _fast;
350275
BigInt gcd (const BigInt &, const BigInt &) _fast;
351276
BigInt modinv (const BigInt &, const BigInt &) _fast;
352277

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; }
353294

354295
#endif//ndef BIGINT_HH

0 commit comments

Comments
 (0)