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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
|
/****************************************************************************
* Core Library Version 1.7, August 2004
* Copyright (c) 1995-2004 Exact Computation Project
* All rights reserved.
*
* This file is part of CORE (http://cs.nyu.edu/exact/core/); you may
* redistribute it under the terms of the Q Public License version 1.0.
* See the file LICENSE.QPL distributed with CORE.
*
* Licensees holding a valid commercial license may use this file in
* accordance with the commercial license agreement provided with the
* software.
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
*
* File: BigInt.h
* Synopsis:
* a wrapper class for mpz from GMP
*
* Written by
* Chee Yap <yap@cs.nyu.edu>
* Chen Li <chenli@cs.nyu.edu>
* Zilin Du <zilin@cs.nyu.edu>
*
* WWW URL: http://cs.nyu.edu/exact/
* Email: exact@cs.nyu.edu
*
* $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.2-branch/Core/include/CORE/BigInt.h $
* $Id: BigInt.h 29485 2006-03-14 11:52:49Z efif $
***************************************************************************/
#ifndef _CORE_BIGINT_H_
#define _CORE_BIGINT_H_
#include <CORE/Gmp.h>
#include <CORE/RefCount.h>
#include <CORE/MemoryPool.h>
#include <string>
CORE_BEGIN_NAMESPACE
class BigIntRep : public RCRepImpl<BigIntRep> {
public:
BigIntRep() {
mpz_init(mp);
}
BigIntRep(const BigIntRep& z) {
mpz_init_set(mp, z.mp);
}
BigIntRep(signed char c) {
mpz_init_set_si(mp, c);
}
BigIntRep(unsigned char c) {
mpz_init_set_ui(mp, c);
}
BigIntRep(signed int i) {
mpz_init_set_si(mp, i);
}
BigIntRep(unsigned int i) {
mpz_init_set_ui(mp, i);
}
BigIntRep(signed short int s) {
mpz_init_set_si(mp, s);
}
BigIntRep(unsigned short int s) {
mpz_init_set_ui(mp, s);
}
BigIntRep(signed long int l) {
mpz_init_set_si(mp, l);
}
BigIntRep(unsigned long int l) {
mpz_init_set_ui(mp, l);
}
BigIntRep(float f) {
mpz_init_set_d(mp, f);
}
BigIntRep(double d) {
mpz_init_set_d(mp, d);
}
BigIntRep(const char* s, int base=0) {
mpz_init_set_str(mp, s, base);
}
BigIntRep(const std::string& s, int base=0) {
mpz_init_set_str(mp, s.c_str(), base);
}
explicit BigIntRep(mpz_srcptr z) {
mpz_init_set(mp, z);
}
~BigIntRep() {
mpz_clear(mp);
}
CORE_MEMORY(BigIntRep)
mpz_srcptr get_mp() const {
return mp;
}
mpz_ptr get_mp() {
return mp;
}
private:
mpz_t mp;
};
typedef RCImpl<BigIntRep> RCBigInt;
class BigInt : public RCBigInt {
public:
/// \name Constructors
//@{
/// default constructor
BigInt() : RCBigInt(new BigIntRep()) {}
/// constructor for <tt>signed char</tt>
BigInt(signed char x) : RCBigInt(new BigIntRep(x)) {}
/// constructor for <tt>unsigned char</tt>
BigInt(unsigned char x) : RCBigInt(new BigIntRep(x)) {}
/// constructor for <tt>signed short int</tt>
BigInt(signed short int x) : RCBigInt(new BigIntRep(x)) {}
/// constructor for <tt>unsigned short int</tt>
BigInt(unsigned short int x) : RCBigInt(new BigIntRep(x)) {}
/// constructor for <tt>signed int</tt>
BigInt(signed int x) : RCBigInt(new BigIntRep(x)) {}
/// constructor for <tt>unsigned int</tt>
BigInt(unsigned int x) : RCBigInt(new BigIntRep(x)) {}
/// constructor for <tt>signed long int</tt>
BigInt(signed long int x) : RCBigInt(new BigIntRep(x)) {}
/// constructor for <tt>unsigned long int</tt>
BigInt(unsigned long int x) : RCBigInt(new BigIntRep(x)) {}
/// constructor for <tt>float</tt>
BigInt(float x) : RCBigInt(new BigIntRep(x)) {}
/// constructor for <tt>double</tt>
BigInt(double x) : RCBigInt(new BigIntRep(x)) {}
/// constructor for <tt>const char*</tt> with base
BigInt(const char* s, int base=0) : RCBigInt(new BigIntRep(s, base)) {}
/// constructor for <tt>std::string</tt> with base
BigInt(const std::string& s, int base=0) : RCBigInt(new BigIntRep(s, base)) {}
/// constructor for <tt>mpz_srcptr</tt>
explicit BigInt(mpz_srcptr z) : RCBigInt(new BigIntRep(z)) {}
//@}
/// \name Copy-Assignment-Destructor
//@{
/// copy constructor
BigInt(const BigInt& rhs) : RCBigInt(rhs) {
rep->incRef();
}
/// assignment operator
BigInt& operator=(const BigInt& rhs) {
if (this != &rhs) {
rep->decRef();
rep = rhs.rep;
rep->incRef();
}
return *this;
}
/// destructor
~BigInt() {
rep->decRef();
}
//@}
/// \name Overloaded operators
//@{
BigInt& operator +=(const BigInt& rhs) {
makeCopy();
mpz_add(get_mp(), get_mp(), rhs.get_mp());
return *this;
}
BigInt& operator -=(const BigInt& rhs) {
makeCopy();
mpz_sub(get_mp(), get_mp(), rhs.get_mp());
return *this;
}
BigInt& operator *=(const BigInt& rhs) {
makeCopy();
mpz_mul(get_mp(), get_mp(), rhs.get_mp());
return *this;
}
BigInt& operator /=(const BigInt& rhs) {
makeCopy();
mpz_tdiv_q(get_mp(), get_mp(), rhs.get_mp());
return *this;
}
BigInt& operator %=(const BigInt& rhs) {
makeCopy();
mpz_tdiv_r(get_mp(), get_mp(), rhs.get_mp());
return *this;
}
BigInt& operator &=(const BigInt& rhs) {
makeCopy();
mpz_and(get_mp(), get_mp(), rhs.get_mp());
return *this;
}
BigInt& operator |=(const BigInt& rhs) {
makeCopy();
mpz_ior(get_mp(), get_mp(), rhs.get_mp());
return *this;
}
BigInt& operator ^=(const BigInt& rhs) {
makeCopy();
mpz_xor(get_mp(), get_mp(), rhs.get_mp());
return *this;
}
BigInt& operator <<=(unsigned long ul) {
makeCopy();
mpz_mul_2exp(get_mp(), get_mp(), ul);
return *this;
}
BigInt& operator >>=(unsigned long ul) {
makeCopy();
mpz_tdiv_q_2exp(get_mp(), get_mp(), ul);
return *this;
}
//@}
/// \name unary, increment, decrement operators
//@{
BigInt operator+() const {
return BigInt(*this);
}
BigInt operator-() const {
BigInt r;
mpz_neg(r.get_mp(), get_mp());
return r;
}
BigInt& operator++() {
makeCopy();
mpz_add_ui(get_mp(), get_mp(), 1);
return *this;
}
BigInt& operator--() {
makeCopy();
mpz_sub_ui(get_mp(), get_mp(), 1);
return *this;
}
BigInt operator++(int) {
BigInt r(*this);
++(*this);
return r;
}
BigInt operator--(int) {
BigInt r(*this);
--(*this);
return r;
}
//@}
/// \name Helper Functions
//@{
/// Has Exact Division
static bool hasExactDivision() {
return false;
}
/// get mpz pointer (const)
mpz_srcptr get_mp() const {
return rep->get_mp();
}
/// get mpz pointer
mpz_ptr get_mp() {
return rep->get_mp();
}
//@}
/// \name String Conversion Functions
//@{
/// set value from <tt>const char*</tt>
int set_str(const char* s, int base = 0) {
makeCopy();
return mpz_set_str(get_mp(), s, base);
}
/// convert to <tt>std::string</tt>
std::string get_str(int base = 10) const {
int n = mpz_sizeinbase (get_mp(), base) + 2;
char *buffer = new char[n];
mpz_get_str(buffer, base, get_mp());
std::string result(buffer);
delete [] buffer;
return result;
}
//@}
/// \name Conversion Functions
//@{
/// intValue
int intValue() const {
return static_cast<int>(mpz_get_si(get_mp()));
}
/// longValue
long longValue() const {
return mpz_get_si(get_mp());
}
/// ulongValue
unsigned long ulongValue() const {
return mpz_get_ui(get_mp());
}
/// doubleValue
double doubleValue() const {
return mpz_get_d(get_mp());
}
//@}
};
inline BigInt operator+(const BigInt& a, const BigInt& b) {
BigInt r;
mpz_add(r.get_mp(), a.get_mp(), b.get_mp());
return r;
}
inline BigInt operator-(const BigInt& a, const BigInt& b) {
BigInt r;
mpz_sub(r.get_mp(), a.get_mp(), b.get_mp());
return r;
}
inline BigInt operator*(const BigInt& a, const BigInt& b) {
BigInt r;
mpz_mul(r.get_mp(), a.get_mp(), b.get_mp());
return r;
}
inline BigInt operator/(const BigInt& a, const BigInt& b) {
BigInt r;
mpz_tdiv_q(r.get_mp(), a.get_mp(), b.get_mp());
return r;
}
inline BigInt operator%(const BigInt& a, const BigInt& b) {
BigInt r;
mpz_tdiv_r(r.get_mp(), a.get_mp(), b.get_mp());
return r;
}
inline BigInt operator&(const BigInt& a, const BigInt& b) {
BigInt r;
mpz_and(r.get_mp(), a.get_mp(), b.get_mp());
return r;
}
inline BigInt operator|(const BigInt& a, const BigInt& b) {
BigInt r;
mpz_ior(r.get_mp(), a.get_mp(), b.get_mp());
return r;
}
inline BigInt operator^(const BigInt& a, const BigInt& b) {
BigInt r;
mpz_xor(r.get_mp(), a.get_mp(), b.get_mp());
return r;
}
inline BigInt operator<<(const BigInt& a, unsigned long ul) {
BigInt r;
mpz_mul_2exp(r.get_mp(), a.get_mp(), ul);
return r;
}
inline BigInt operator>>(const BigInt& a, unsigned long ul) {
BigInt r;
mpz_tdiv_q_2exp(r.get_mp(), a.get_mp(), ul);
return r;
}
inline int cmp(const BigInt& x, const BigInt& y) {
return mpz_cmp(x.get_mp(), y.get_mp());
}
inline bool operator==(const BigInt& a, const BigInt& b) {
return cmp(a, b) == 0;
}
inline bool operator!=(const BigInt& a, const BigInt& b) {
return cmp(a, b) != 0;
}
inline bool operator>=(const BigInt& a, const BigInt& b) {
return cmp(a, b) >= 0;
}
inline bool operator>(const BigInt& a, const BigInt& b) {
return cmp(a, b) > 0;
}
inline bool operator<=(const BigInt& a, const BigInt& b) {
return cmp(a, b) <= 0;
}
inline bool operator<(const BigInt& a, const BigInt& b) {
return cmp(a, b) < 0;
}
inline std::ostream& operator<<(std::ostream& o, const BigInt& x) {
return CORE::operator<<(o, x.get_mp());
}
inline std::istream& operator>>(std::istream& i, BigInt& x) {
x.makeCopy();
return CORE::operator>>(i, x.get_mp());
}
/// sign
inline int sign(const BigInt& a) {
return mpz_sgn(a.get_mp());
}
/// abs
inline BigInt abs(const BigInt& a) {
BigInt r;
mpz_abs(r.get_mp(), a.get_mp());
return r;
}
/// neg
inline BigInt neg(const BigInt& a) {
BigInt r;
mpz_neg(r.get_mp(), a.get_mp());
return r;
}
/// negate
inline void negate(BigInt& a) {
a.makeCopy();
mpz_neg(a.get_mp(), a.get_mp());
}
/// cmpabs
inline int cmpabs(const BigInt& a, const BigInt& b) {
return mpz_cmpabs(a.get_mp(), b.get_mp());
}
/// \name Conversion Functions
//@{
/// longValue
inline long longValue(const BigInt& a) {
return a.longValue();
}
/// ulongValue
inline unsigned long ulongValue(const BigInt& a) {
return a.ulongValue();
}
/// doubleValue
inline double doubleValue(const BigInt& a) {
return a.doubleValue();
}
//@}
/// \name File I/O Functions
//@{
/// read from file
void readFromFile(BigInt& z, std::istream& in, long maxLength = 0);
/// write to file
void writeToFile(const BigInt& z, std::ostream& in, int base=10, int charsPerLine=80);
//@}
/// \name Misc Functions
//@{
/// isEven
inline bool isEven(const BigInt& z) {
return mpz_even_p(z.get_mp());
}
/// isOdd
inline bool isOdd(const BigInt& z) {
return mpz_odd_p(z.get_mp());
}
/// get exponent of power 2
inline unsigned long getBinExpo(const BigInt& z) {
return mpz_scan1(z.get_mp(), 0);
}
/// get exponent of power k
inline void getKaryExpo(const BigInt& z, BigInt& m, int& e, unsigned long k) {
mpz_t f;
mpz_init_set_ui(f, k);
m.makeCopy();
e = mpz_remove(m.get_mp(), z.get_mp(), f);
mpz_clear(f);
}
/// divisible(x,y) = "x | y"
inline bool isDivisible(const BigInt& x, const BigInt& y) {
return mpz_divisible_p(x.get_mp(), y.get_mp()) != 0;
}
inline bool isDivisible(int x, int y) {
return x % y == 0;
}
inline bool isDivisible(long x, long y) {
return x % y == 0;
}
/// exact div
inline void divexact(BigInt& z, const BigInt& x, const BigInt& y) {
z.makeCopy();
mpz_divexact(z.get_mp(), x.get_mp(), y.get_mp());
}
// Chee (1/12/2004) The definition of div_exact(x,y) next
// ensure that in Polynomials<NT> works with both NT=BigInt and NT=int:
inline BigInt div_exact(const BigInt& x, const BigInt& y) {
BigInt z; // precodition: isDivisible(x,y)
divexact(z, x, y); // z is set to x/y;
return z;
}
inline int div_exact(int x, int y) {
return x/y; // precondition: isDivisible(x,y)
}
inline long div_exact(long x, long y) {
return x/y; // precondition: isDivisible(x,y)
}
/// gcd
inline BigInt gcd(const BigInt& a, const BigInt& b) {
BigInt r;
mpz_gcd(r.get_mp(), a.get_mp(), b.get_mp());
return r;
}
/// div_rem
inline void div_rem(BigInt& q, BigInt& r, const BigInt& a, const BigInt& b) {
q.makeCopy();
r.makeCopy();
mpz_tdiv_qr(q.get_mp(), r.get_mp(), a.get_mp(), b.get_mp());
}
/// power
inline void power(BigInt& c, const BigInt& a, unsigned long ul) {
c.makeCopy();
mpz_pow_ui(c.get_mp(), a.get_mp(), ul);
}
// pow
inline BigInt pow(const BigInt& a, unsigned long ui) {
BigInt r;
power(r, a, ui);
return r;
}
// bit length
inline int bitLength(const BigInt& a) {
return mpz_sizeinbase(a.get_mp(), 2);
}
/// floorLg -- floor of log_2(a)
/** Convention: a=0, floorLg(a) returns -1.
* This makes sense for integer a.
*/
inline long floorLg(const BigInt& a) {
return (sign(a) == 0) ? (-1) : (bitLength(a)-1);
}
/// ceilLg -- ceiling of log_2(a) where a=BigInt, int or long
/** Convention: a=0, ceilLg(a) returns -1.
* This makes sense for integer a.
*/
inline long ceilLg(const BigInt& a) {
if (sign(a) == 0)
return -1;
unsigned long len = bitLength(a);
return (mpz_scan1(a.get_mp(), 0) == len-1) ? (len-1) : len;
}
inline long ceilLg(long a) { // need this for Polynomial<long>
return ceilLg(BigInt(a));
}
inline long ceilLg(int a) { // need this for Polynomial<int>
return ceilLg(BigInt(a));
}
// return a gmp_randstate_t structure
extern gmp_randstate_t* getRandstate();
/// seed function
inline void seed(const BigInt& a) {
gmp_randseed(*getRandstate(), a.get_mp());
}
/// randomize function
inline BigInt randomize(const BigInt& a) {
BigInt r;
mpz_urandomm(r.get_mp(), *getRandstate(), a.get_mp());
return r;
}
//@}
CORE_END_NAMESPACE
#endif // _CORE_BIGINT_H_
|