home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / integer.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  23.4 KB  |  1,023 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2.  
  3. /* 
  4. Copyright (C) 1988 Free Software Foundation
  5.     written by Doug Lea (dl@rocky.oswego.edu)
  6.  
  7. This file is part of GNU CC.
  8.  
  9. GNU CC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the GNU CC General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. GNU CC, but only under the conditions described in the
  18. GNU CC General Public License.   A copy of this license is
  19. supposed to have been given to you along with GNU CC so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies.  
  23. */
  24.  
  25. #ifndef _Integer_h
  26. #pragma once
  27. #define _Integer_h 1
  28.  
  29. #include <stream.h>
  30.  
  31. struct IntRep                    // internal Integer representations
  32. {
  33.   unsigned short  len;          // current length
  34.   unsigned short  sz;           // allocated space
  35.   short           sgn;          // 1 means >= 0; 0 means < 0 
  36.   unsigned short  s[1];         // represented as ushort array starting here
  37.  
  38.   friend IntRep*  Ialloc(IntRep*, const unsigned short *, int, int, int);
  39.   friend IntRep*  Icopy_long(IntRep*, long);
  40.   friend IntRep*  Icopy(IntRep*, IntRep*);
  41.   friend IntRep*  Iresize(IntRep*, int);
  42.   friend IntRep*  add(IntRep*, int, IntRep*, int, IntRep*);
  43.   friend IntRep*  add(IntRep*, int, long, IntRep*);
  44.   friend IntRep*  multiply(IntRep*, IntRep*, IntRep*);
  45.   friend IntRep*  multiply(IntRep*, long, IntRep*);
  46.   friend IntRep*  lshift(IntRep*, long, IntRep*);
  47.   friend IntRep*  lshift(IntRep*, IntRep*, int, IntRep*);
  48.   friend IntRep*  bitop(IntRep*, IntRep*, IntRep*, char);
  49.   friend IntRep*  bitop(IntRep*, long, IntRep*, char);
  50.   friend IntRep*  power(IntRep*, long, IntRep*);
  51.   friend IntRep*  div(IntRep*, IntRep*, IntRep*);
  52.   friend IntRep*  mod(IntRep*, IntRep*, IntRep*);
  53.   friend IntRep*  div(IntRep*, long, IntRep*);
  54.   friend IntRep*  mod(IntRep*, long, IntRep*);
  55.   friend IntRep*  compl(IntRep*, IntRep*);
  56.   friend IntRep*  sqr(IntRep*);
  57.   friend IntRep*  pow(IntRep*, long);
  58.   friend IntRep*  gcd(IntRep*, IntRep* y);
  59.   friend int      compare(IntRep*, IntRep*);
  60.   friend int      compare(IntRep*, long);
  61.   friend int      ucompare(IntRep*, IntRep*);
  62.   friend int      ucompare(IntRep*, long);
  63.   friend char*    Itoa(IntRep* x, int base = 10, int width = 0);
  64.   friend IntRep*  atoIntRep(const char* s, int base = 10);
  65.   friend long     Itolong(IntRep*);
  66.   friend double   Itodouble(IntRep*);
  67.   friend int      Iislong(IntRep*);
  68.   friend int      Iisdouble(IntRep*);
  69.   friend long     lg(IntRep*);
  70. };
  71.  
  72.  
  73. class IntTmp;
  74.  
  75. class Integer
  76. {
  77.   friend class    IntTmp;
  78. protected:
  79.   IntRep*         rep;
  80. public:
  81.                   Integer();
  82.                   Integer(long);
  83.                   Integer(Integer&);
  84.  
  85.                   ~Integer();
  86.  
  87.   void            operator =  (Integer&);
  88.   void            operator =  (IntTmp&);
  89.   void            operator =  (long);
  90.  
  91.   friend int      operator == (Integer&, Integer&);
  92.   friend int      operator == (Integer&, long);
  93.  
  94.   friend int      operator != (Integer&, Integer&);
  95.   friend int      operator != (Integer&, long);
  96.  
  97.   friend int      operator <  (Integer&, Integer&);
  98.   friend int      operator <  (Integer&, long);
  99.  
  100.   friend int      operator <= (Integer&, Integer&);
  101.   friend int      operator <= (Integer&, long);
  102.  
  103.   friend int      operator >  (Integer&, Integer&);
  104.   friend int      operator >  (Integer&, long);
  105.  
  106.   friend int      operator >= (Integer&, Integer&);
  107.   friend int      operator >= (Integer&, long);
  108.  
  109.   IntTmp          operator -  ();
  110.   IntTmp          operator ~  ();
  111.   void            operator ++ ();
  112.   void            operator -- ();
  113.  
  114.   IntTmp          operator +  (Integer&);
  115.   IntTmp          operator +  (IntTmp&);
  116.   IntTmp          operator +  (long);
  117.   friend IntTmp   operator +  (long, Integer&);
  118.   void            operator += (Integer&);
  119.   void            operator += (long);
  120.   IntTmp          operator -  (Integer&);
  121.   IntTmp          operator -  (IntTmp&);
  122.   IntTmp          operator -  (long);
  123.   friend IntTmp   operator -  (long, Integer&);
  124.   void            operator -= (Integer&);
  125.   void            operator -= (long);
  126.   IntTmp          operator *  (Integer&);
  127.   IntTmp          operator *  (IntTmp&);
  128.   IntTmp          operator *  (long);
  129.   friend IntTmp   operator *  (long, Integer&);
  130.   void            operator *= (Integer&);
  131.   void            operator *= (long);
  132.   IntTmp          operator /  (Integer&);
  133.   IntTmp          operator /  (IntTmp&);
  134.   IntTmp          operator /  (long);
  135.   void            operator /= (Integer&);
  136.   void            operator /= (long);
  137.   IntTmp          operator %  (Integer&);
  138.   IntTmp          operator %  (IntTmp&);
  139.   IntTmp          operator %  (long);
  140.   void            operator %= (Integer&);
  141.   void            operator %= (long);
  142.   IntTmp          operator << (Integer&);
  143.   IntTmp          operator << (IntTmp&);
  144.   IntTmp          operator << (long);
  145.   void            operator <<=(Integer&);
  146.   void            operator <<=(long);
  147.   IntTmp          operator >> (Integer&);
  148.   IntTmp          operator >> (IntTmp&);
  149.   IntTmp          operator >> (long);
  150.   void            operator >>=(Integer&);
  151.   void            operator >>=(long);
  152.   IntTmp          operator &  (Integer&);
  153.   IntTmp          operator &  (IntTmp&);
  154.   IntTmp          operator &  (long);
  155.   friend IntTmp   operator &  (long, Integer&);
  156.   void            operator &= (Integer&);
  157.   void            operator &= (long);
  158.   IntTmp          operator |  (Integer&);
  159.   IntTmp          operator |  (IntTmp&);
  160.   IntTmp          operator |  (long);
  161.   friend IntTmp   operator |  (long, Integer&);
  162.   void            operator |= (Integer&);
  163.   void            operator |= (long);
  164.   IntTmp          operator ^  (Integer&);
  165.   IntTmp          operator ^  (IntTmp&);
  166.   IntTmp          operator ^  (long);
  167.   friend IntTmp   operator ^  (long, Integer&);
  168.   void            operator ^= (Integer&);
  169.   void            operator ^= (long);
  170.  
  171.   friend Integer& operator <? (Integer& x, Integer& y); // min
  172.   friend Integer& operator >? (Integer& x, Integer& y); // max
  173.  
  174. // builtin Integer functions
  175.  
  176.   friend int      compare(Integer&, Integer&);  // signed comparison
  177.   friend int      ucompare(Integer&, Integer&); // unsigned comparison
  178.  
  179.   void            negate();                     // negate in-place
  180.   void            abs();                        // absolute-value in-place
  181.   friend IntTmp   abs(Integer&);                // absolute value
  182.   friend long     lg (Integer&);                // floor log base 2 of abs(x)
  183.   friend IntTmp   sqr(Integer&);                // square
  184.   friend Integer  sqrt(Integer&);               // floor of square root
  185.   friend IntTmp   gcd(Integer& x, Integer& y);  // greatest common divisor
  186.   friend IntTmp   lcm(Integer& x, Integer& y);  // least common multiple
  187.   friend int      even(Integer&);               // true if even
  188.   friend int      odd(Integer&);                // true if odd
  189.   friend int      sign(Integer&);               // returns -1, 0, +1
  190.   friend IntTmp   pow(Integer& x, Integer& y);  // x to the y power
  191.   friend IntTmp   pow(Integer& x, long y);
  192.   friend IntTmp   Ipow(long x, long y);         // x to the y as Integer 
  193.   friend void     setbit(Integer& x, long b);   // set b'th bit of x
  194.   friend void     clearbit(Integer& x, long b); // clear b'th bit
  195.   friend int      testbit(Integer& x, long b);  // return b'th bit
  196.   friend double   ratio(Integer& x, Integer& y);// return x/y as a double
  197.   friend void     divide(Integer& x, Integer& y, Integer& q, Integer& r);
  198.   friend void     divide(Integer& x, long y, Integer& q, long& r);
  199.  
  200. // coercion & conversion
  201.  
  202.   int             fits_in_long();
  203.   int             fits_in_double();
  204.  
  205.   long            operator long();
  206.   double          operator double();
  207.  
  208.   friend char*    Itoa(Integer& x, int base = 10, int width = 0);
  209.   friend IntTmp   atoI(const char* s, int base = 10);
  210.   
  211.   friend istream& operator >> (istream& s, Integer& y);
  212.   friend ostream& operator << (ostream& s, Integer& y);
  213.  
  214. // miscellany
  215.  
  216.   void            error(char* msg);
  217.   int             OK();  
  218. };
  219.  
  220. class IntTmp: public Integer
  221. {
  222.   friend class    Integer;
  223.  
  224. public:
  225.                   IntTmp(IntRep*);
  226.                   IntTmp(Integer&);
  227.                   IntTmp(IntTmp&);
  228.                   ~IntTmp();
  229.  
  230.   IntTmp          operator +  (IntTmp&);
  231.   IntTmp          operator +  (Integer&);
  232.   IntTmp          operator +  (long);
  233.   friend IntTmp   operator +  (long, IntTmp&);
  234.   IntTmp          operator -  (Integer&);
  235.   IntTmp          operator -  (IntTmp&);
  236.   IntTmp          operator -  (long);
  237.   friend IntTmp   operator -  (long, IntTmp&);
  238.   IntTmp          operator *  (Integer&);
  239.   IntTmp          operator *  (IntTmp&);
  240.   IntTmp          operator *  (long);
  241.   friend IntTmp   operator *  (long, IntTmp&);
  242.   IntTmp          operator /  (Integer&);
  243.   IntTmp          operator /  (IntTmp&);
  244.   IntTmp          operator /  (long);
  245.   IntTmp          operator %  (Integer&);
  246.   IntTmp          operator %  (IntTmp&);
  247.   IntTmp          operator %  (long);
  248.   IntTmp          operator << (Integer&);
  249.   IntTmp          operator << (IntTmp&);
  250.   IntTmp          operator << (long);
  251.   IntTmp          operator >> (Integer&);
  252.   IntTmp          operator >> (IntTmp&);
  253.   IntTmp          operator >> (long);
  254.   IntTmp          operator &  (Integer&);
  255.   IntTmp          operator &  (IntTmp&);
  256.   IntTmp          operator &  (long);
  257.   friend IntTmp   operator &  (long, IntTmp&);
  258.   IntTmp          operator |  (Integer&);
  259.   IntTmp          operator |  (IntTmp&);
  260.   IntTmp          operator |  (long);
  261.   friend IntTmp   operator |  (long, IntTmp&);
  262.   IntTmp          operator ^  (Integer&);
  263.   IntTmp          operator ^  (IntTmp&);
  264.   IntTmp          operator ^  (long);
  265.   friend IntTmp   operator ^  (long, IntTmp&);
  266.  
  267.  
  268.   IntTmp          operator -  ();
  269.   IntTmp          operator ~  ();
  270.   friend IntTmp   abs(IntTmp&);
  271.   friend IntTmp   sqr(IntTmp&);
  272.   friend IntTmp   pow(IntTmp&  x, long y);
  273. };
  274.  
  275.  
  276. char*             dec(Integer& x, int width = 0);
  277. char*             oct(Integer& x, int width = 0);
  278. char*             hex(Integer& x, int width = 0);
  279.  
  280.  
  281. //#ifdef __OPTIMIZE__
  282.  
  283.  
  284. inline Integer::Integer()
  285. {
  286.   rep = 0;
  287. }
  288.  
  289. inline Integer::Integer(long y)
  290. {
  291.   rep = Icopy_long(0, y);
  292. }
  293.  
  294. inline Integer::Integer(Integer&  y)
  295. {
  296.   rep = Icopy(0, y.rep);
  297. }
  298.  
  299.  
  300. inline Integer::~Integer()
  301. {
  302.   delete rep;
  303. }
  304.  
  305. inline void  Integer::operator = (Integer&  y)
  306. {
  307.   rep = Icopy(rep, y.rep);
  308. }
  309.  
  310. inline void  Integer::operator = (IntTmp&  y)
  311. {
  312.   delete rep;  rep = y.rep; y.rep = 0;
  313. }
  314.  
  315. inline void Integer::operator = (long y)
  316. {
  317.   rep = Icopy_long(rep, y); 
  318. }
  319.  
  320. inline int compare(Integer& x, Integer& y)
  321. {
  322.   return compare(x.rep, y.rep);
  323. }
  324.  
  325. inline int ucompare(Integer& x, Integer& y)
  326. {
  327.   return ucompare(x.rep, y.rep);
  328. }
  329.  
  330. inline int operator == (Integer&  x, Integer&  y)
  331. {
  332.   return compare(x.rep, y.rep) == 0; 
  333. }
  334.  
  335. inline int operator == (Integer&  x, long y)
  336. {
  337.   return compare(x.rep, y) == 0; 
  338. }
  339.  
  340. inline int operator != (Integer&  x, Integer&  y)
  341. {
  342.   return compare(x.rep, y.rep) != 0; 
  343. }
  344.  
  345. inline int operator != (Integer&  x, long y)
  346. {
  347.   return compare(x.rep, y) != 0; 
  348. }
  349.  
  350. inline int operator <  (Integer&  x, Integer&  y)
  351. {
  352.   return compare(x.rep, y.rep) <  0; 
  353. }
  354.  
  355. inline int operator <  (Integer&  x, long y)
  356. {
  357.   return compare(x.rep, y) <  0; 
  358. }
  359.  
  360. inline int operator <= (Integer&  x, Integer&  y)
  361. {
  362.   return compare(x.rep, y.rep) <= 0; 
  363. }
  364.  
  365. inline int operator <= (Integer&  x, long y)
  366. {
  367.   return compare(x.rep, y) <= 0; 
  368. }
  369.  
  370. inline int operator >  (Integer&  x, Integer&  y)
  371. {
  372.   return compare(x.rep, y.rep) >  0; 
  373. }
  374.  
  375. inline int operator >  (Integer&  x, long y)
  376. {
  377.   return compare(x.rep, y) >  0; 
  378. }
  379.  
  380. inline int operator >= (Integer&  x, Integer&  y)
  381. {
  382.   return compare(x.rep, y.rep) >= 0; 
  383. }
  384.  
  385. inline int operator >= (Integer&  x, long y)
  386. {
  387.   return compare(x.rep, y) >= 0; 
  388. }
  389.  
  390. inline IntTmp::IntTmp(IntRep* y)
  391. {
  392.   rep = y;
  393. }
  394.  
  395. inline IntTmp::IntTmp(Integer& x)
  396. {
  397.   rep = x.rep; x.rep = 0;
  398. }
  399.  
  400. inline IntTmp::IntTmp(IntTmp& x)
  401. {
  402.   rep = x.rep; x.rep = 0;
  403. }
  404.  
  405. inline IntTmp::~IntTmp() {}
  406.  
  407. inline IntTmp  Integer::operator +  (Integer& y)
  408. {
  409.   return (add(rep, 0, y.rep, 0, 0));
  410. }
  411.  
  412. inline IntTmp  Integer::operator +  (IntTmp& y)
  413. {
  414.   IntRep* r = add(rep, 0, y.rep, 0, y.rep); y.rep = 0; return r;
  415. }
  416.  
  417. inline IntTmp  Integer::operator +  (long y)
  418. {
  419.   return(add(rep, 0, y, 0));
  420. }
  421.  
  422. inline IntTmp  operator +  (long  x, Integer& y)
  423. {
  424.   return(add(y.rep, 0, x, 0));
  425. }
  426.  
  427. inline IntTmp  IntTmp::operator +  (Integer& y)
  428. {
  429.   IntRep* r = add(rep, 0, y.rep, 0, rep); rep = 0; return r;
  430. }
  431.  
  432. inline IntTmp  IntTmp::operator +  (IntTmp& y)
  433. {
  434.   IntRep* r = add(rep, 0, y.rep, 0, rep); rep = 0; return r;
  435. }
  436.  
  437. inline IntTmp  IntTmp::operator +  (long y)
  438. {
  439.   IntRep* r = add(rep, 0, y, rep); rep = 0; return r;
  440. }
  441.  
  442. inline IntTmp  operator +  (long y, IntTmp& x)
  443. {
  444.   IntRep* r = add(x.rep, 0, y, x.rep); x.rep = 0; return r;
  445. }
  446.  
  447. inline void  Integer::operator += (Integer& y)
  448. {
  449.   rep = add(rep, 0, y.rep, 0, rep);
  450. }
  451.  
  452. inline void  Integer::operator += (long y)
  453. {
  454.   rep = add(rep, 0, y, rep);
  455. }
  456.  
  457.  
  458. inline void Integer::operator ++ ()
  459. {
  460.   rep = add(rep, 0, 1, rep);
  461. }
  462.  
  463.  
  464. inline IntTmp  Integer::operator -  (Integer& y)
  465. {
  466.   return (add(rep, 0, y.rep, 1, 0));
  467. }
  468.  
  469. inline IntTmp  Integer::operator -  (IntTmp& y)
  470. {
  471.   IntRep* r = add(rep, 0, y.rep, 1, y.rep); y.rep = 0; return r;
  472. }
  473.  
  474. inline IntTmp  Integer::operator -  (long y)
  475. {
  476.   return(add(rep, 0, -y, 0));
  477. }
  478.  
  479. inline IntTmp  operator -  (long  x, Integer& y)
  480. {
  481.   return(add(y.rep, 1, x, 0));
  482. }
  483.  
  484. inline IntTmp  IntTmp::operator -  (Integer& y)
  485. {
  486.   IntRep* r = add(rep, 0, y.rep, 1, rep); rep = 0; return r;
  487. }
  488.  
  489. inline IntTmp  IntTmp::operator -  (IntTmp& y)
  490. {
  491.   IntRep* r = add(rep, 0, y.rep, 1, rep); rep = 0; return r;
  492. }
  493.  
  494. inline IntTmp  IntTmp::operator -  (long y)
  495. {
  496.   IntRep* r = add(rep, 0, -y, rep); rep = 0; return r;
  497. }
  498.  
  499. inline IntTmp  operator -  (long y, IntTmp& x)
  500. {
  501.   IntRep* r = add(x.rep, 1, y, x.rep);  x.rep = 0; return r;
  502. }
  503.  
  504. inline void  Integer::operator -= (Integer& y)
  505. {
  506.   rep = add(rep, 0, y.rep, 1, rep); 
  507. }
  508.  
  509. inline void  Integer::operator -= (long y)
  510. {
  511.   rep = add(rep, 0, -y, rep);
  512. }
  513.  
  514. inline void Integer::operator -- ()
  515. {
  516.   rep = add(rep, 0, -1, rep);
  517. }
  518.  
  519. inline IntTmp  Integer::operator *  (Integer& y)
  520. {
  521.   return (multiply(rep, y.rep, 0));
  522. }
  523.  
  524. inline IntTmp  Integer::operator *  (IntTmp& y)
  525. {
  526.   IntRep* r =  multiply(rep, y.rep, y.rep); y.rep = 0; return r;
  527. }
  528.  
  529. inline IntTmp  Integer::operator *  (long y)
  530. {
  531.   return(multiply(rep, y, 0));
  532. }
  533.  
  534. inline IntTmp  operator *  (long  x, Integer& y)
  535. {
  536.   return(multiply(y.rep, x, 0));
  537. }
  538.  
  539. inline IntTmp  IntTmp::operator *  (Integer& y)
  540. {
  541.   IntRep* r =  multiply(rep, y.rep, rep); rep = 0; return r;
  542. }
  543.  
  544. inline IntTmp  IntTmp::operator *  (IntTmp& y)
  545. {
  546.   IntRep* r =  multiply(rep, y.rep, rep); rep = 0; return r;
  547. }
  548.  
  549. inline IntTmp  IntTmp::operator *  (long y)
  550. {
  551.   IntRep* r = multiply(rep, y, rep); rep = 0; return r;
  552. }
  553.  
  554. inline IntTmp  operator *  (long y, IntTmp& x)
  555. {
  556.   IntRep* r = multiply(x.rep, y, x.rep); x.rep = 0; return r;
  557. }
  558.  
  559. inline void Integer::operator *= (Integer& y)
  560. {
  561.   rep = multiply(rep, y.rep, rep);
  562. }
  563.  
  564. inline void Integer::operator *= (long y)
  565. {
  566.   rep = multiply(rep, y, rep); 
  567. }
  568.  
  569. inline IntTmp sqr(Integer& x)
  570. {
  571.   return(multiply(x.rep, x.rep, 0));
  572. }
  573.  
  574. inline IntTmp sqr(IntTmp& x)
  575. {
  576.   IntRep* r = multiply(x.rep, x.rep, x.rep); x.rep = 0; return r;
  577. }
  578.  
  579. inline IntTmp  Integer::operator &  (Integer& y)
  580. {
  581.   return (bitop(rep, y.rep, 0, '&'));
  582. }
  583.  
  584. inline IntTmp  Integer::operator &  (IntTmp& y)
  585. {
  586.   IntRep* r =  bitop(rep, y.rep, y.rep, '&'); y.rep = 0; return r;
  587. }
  588.  
  589. inline IntTmp  Integer::operator &  (long y)
  590. {
  591.   return(bitop(rep, y, 0, '&'));
  592. }
  593.  
  594. inline IntTmp  operator &  (long  x, Integer& y)
  595. {
  596.   return(bitop(y.rep, x, 0, '&'));
  597. }
  598.  
  599. inline IntTmp  IntTmp::operator &  (Integer& y)
  600. {
  601.   IntRep* r =  bitop(rep, y.rep, rep, '&'); rep = 0; return r;
  602. }
  603.  
  604. inline IntTmp  IntTmp::operator &  (IntTmp& y)
  605. {
  606.   IntRep* r =  bitop(rep, y.rep, rep, '&'); rep = 0; return r;
  607. }
  608.  
  609. inline IntTmp  IntTmp::operator &  (long y)
  610. {
  611.   IntRep* r = bitop(rep, y, rep, '&'); rep = 0; return r;
  612. }
  613.  
  614. inline IntTmp  operator &  (long y, IntTmp& x)
  615. {
  616.   IntRep* r = bitop(x.rep, y, x.rep, '&'); x.rep = 0; return r;
  617. }
  618.  
  619. inline void  Integer::operator &= (Integer& y)
  620. {
  621.   rep = bitop(rep, y.rep, rep, '&'); 
  622. }
  623.  
  624. inline void  Integer::operator &= (long y)
  625. {
  626.   rep = bitop(rep, y, rep, '&'); 
  627. }
  628.  
  629. inline IntTmp  Integer::operator |  (Integer& y)
  630. {
  631.   return (bitop(rep, y.rep, 0, '|'));
  632. }
  633.  
  634. inline IntTmp  Integer::operator |  (IntTmp& y)
  635. {
  636.   IntRep* r =  bitop(rep, y.rep, y.rep, '|'); y.rep = 0; return r;
  637. }
  638.  
  639. inline IntTmp  Integer::operator |  (long y)
  640. {
  641.   return(bitop(rep, y, 0, '|'));
  642. }
  643.  
  644. inline IntTmp  operator |  (long  x, Integer& y)
  645. {
  646.   return(bitop(y.rep, x, 0, '|'));
  647. }
  648.  
  649. inline IntTmp  IntTmp::operator |  (Integer& y)
  650. {
  651.   IntRep* r =  bitop(rep, y.rep, rep, '|'); rep = 0; return r;
  652. }
  653.  
  654. inline IntTmp  IntTmp::operator |  (IntTmp& y)
  655. {
  656.   IntRep* r =  bitop(rep, y.rep, rep, '|'); rep = 0; return r;
  657. }
  658.  
  659. inline IntTmp  IntTmp::operator |  (long y)
  660. {
  661.   IntRep* r = bitop(rep, y, rep, '|'); rep = 0; return r;
  662. }
  663.  
  664. inline IntTmp  operator |  (long y, IntTmp& x)
  665. {
  666.   IntRep* r = bitop(x.rep, y, x.rep, '|'); x.rep = 0; return r;
  667. }
  668.  
  669. inline void Integer::operator |= (Integer& y)
  670. {
  671.   rep = bitop(rep, y.rep, rep, '|'); 
  672. }
  673.  
  674. inline void  Integer::operator |= (long y)
  675. {
  676.   rep = bitop(rep, y, rep, '|'); 
  677. }
  678.  
  679. inline IntTmp  Integer::operator ^  (Integer& y)
  680. {
  681.   return (bitop(rep, y.rep, 0, '^'));
  682. }
  683.  
  684. inline IntTmp  Integer::operator ^  (IntTmp& y)
  685. {
  686.   IntRep* r =  bitop(rep, y.rep, y.rep, '^'); y.rep = 0; return r;
  687. }
  688.  
  689. inline IntTmp  Integer::operator ^  (long y)
  690. {
  691.   return(bitop(rep, y, 0, '^'));
  692. }
  693.  
  694. inline IntTmp  operator ^  (long  x, Integer& y)
  695. {
  696.   return(bitop(y.rep, x, 0, '^'));
  697. }
  698.  
  699. inline IntTmp  IntTmp::operator ^  (Integer& y)
  700. {
  701.   IntRep* r =  bitop(rep, y.rep, rep, '^'); rep = 0; return r;
  702. }
  703.  
  704. inline IntTmp  IntTmp::operator ^  (IntTmp& y)
  705. {
  706.   IntRep* r =  bitop(rep, y.rep, rep, '^'); rep = 0; return r;
  707. }
  708.  
  709. inline IntTmp  IntTmp::operator ^  (long y)
  710. {
  711.   IntRep* r = bitop(rep, y, rep, '^'); rep = 0; return r;
  712. }
  713.  
  714. inline IntTmp  operator ^  (long y, IntTmp& x)
  715. {
  716.   IntRep* r = bitop(x.rep, y, x.rep, '^'); x.rep = 0; return r;
  717. }
  718.  
  719. inline void  Integer::operator ^= (Integer& y)
  720. {
  721.   rep = bitop(rep, y.rep, rep, '^'); 
  722. }
  723.  
  724. inline void  Integer::operator ^= (long y)
  725. {
  726.   rep = bitop(rep, y, rep, '^'); 
  727. }
  728.  
  729. inline IntTmp  Integer::operator /  (Integer& y)
  730. {
  731.   return (div(rep, y.rep, 0));
  732. }
  733.  
  734. inline IntTmp  Integer::operator /  (IntTmp& y)
  735. {
  736.   IntRep* r =  div(rep, y.rep, y.rep); y.rep = 0; return r;
  737. }
  738.  
  739. inline IntTmp  Integer::operator /  (long y)
  740. {
  741.   return(div(rep, y, 0));
  742. }
  743.  
  744. inline IntTmp  IntTmp::operator /  (Integer& y)
  745. {
  746.   IntRep* r =  div(rep, y.rep, rep); rep = 0; return r;
  747. }
  748.  
  749. inline IntTmp  IntTmp::operator /  (IntTmp& y)
  750. {
  751.   IntRep* r =  div(rep, y.rep, rep); rep = 0; return r;
  752. }
  753.  
  754. inline IntTmp  IntTmp::operator /  (long y)
  755. {
  756.   IntRep* r = div(rep, y, rep); rep = 0; return r;
  757. }
  758.  
  759. inline void Integer::operator /= (Integer& y)
  760. {
  761.   rep = div(rep, y.rep, rep);  
  762. }
  763.  
  764. inline void Integer::operator /= (long y)
  765. {
  766.   rep = div(rep, y, rep);  
  767. }
  768.  
  769. inline IntTmp  Integer::operator %  (Integer& y)
  770. {
  771.   return (mod(rep, y.rep, 0));
  772. }
  773.  
  774. inline IntTmp  Integer::operator %  (IntTmp& y)
  775. {
  776.   IntRep* r =  mod(rep, y.rep, y.rep); y.rep = 0; return r;
  777. }
  778.  
  779. inline IntTmp  Integer::operator %  (long y)
  780. {
  781.   return(mod(rep, y, 0));
  782. }
  783.  
  784. inline IntTmp  IntTmp::operator %  (Integer& y)
  785. {
  786.   IntRep* r =  mod(rep, y.rep, rep); rep = 0; return r;
  787. }
  788.  
  789. inline IntTmp  IntTmp::operator %  (IntTmp& y)
  790. {
  791.   IntRep* r =  mod(rep, y.rep, rep); rep = 0; return r;
  792. }
  793.  
  794. inline IntTmp  IntTmp::operator %  (long y)
  795. {
  796.   IntRep* r = mod(rep, y, rep); rep = 0; return r;
  797. }
  798.  
  799. inline void Integer::operator %= (Integer& y)
  800. {
  801.   rep = mod(rep, y.rep, rep);
  802. }
  803.  
  804. inline void Integer::operator %= (long y)
  805. {
  806.   rep = mod(rep, y, rep);
  807. }
  808.  
  809. inline IntTmp  Integer::operator <<  (Integer& y)
  810. {
  811.   return (lshift(rep, y.rep, 0, 0));
  812. }
  813.  
  814. inline IntTmp  Integer::operator << (IntTmp& y)
  815. {
  816.   IntRep* r =  lshift(rep, y.rep, 0, y.rep); y.rep = 0; return r;
  817. }
  818.  
  819. inline IntTmp  Integer::operator <<  (long y)
  820. {
  821.   return(lshift(rep, y, 0));
  822. }
  823.  
  824. inline IntTmp  IntTmp::operator <<  (Integer& y)
  825. {
  826.   IntRep* r =  lshift(rep, y.rep, 0, rep); rep = 0; return r;
  827. }
  828.  
  829. inline IntTmp  IntTmp::operator <<  (IntTmp& y)
  830. {
  831.   IntRep* r =  lshift(rep, y.rep, 0, rep); rep = 0; return r;
  832. }
  833.  
  834. inline IntTmp  IntTmp::operator <<  (long y)
  835. {
  836.   IntRep* r = lshift(rep, y, rep); rep = 0; return r;
  837. }
  838.  
  839. inline void Integer::operator <<= (Integer&  y)
  840. {
  841.   rep = lshift(rep, y.rep, 0, rep); 
  842. }
  843.  
  844. inline void Integer::operator <<= (long  y)
  845. {
  846.   rep = lshift(rep, y, rep); 
  847. }
  848.  
  849. inline IntTmp  Integer::operator >>  (Integer& y)
  850. {
  851.   return (lshift(rep, y.rep, 1, 0));
  852. }
  853.  
  854. inline IntTmp  Integer::operator >> (IntTmp& y)
  855. {
  856.   IntRep* r =  lshift(rep, y.rep, 1, y.rep); y.rep = 0; return r;
  857. }
  858.  
  859. inline IntTmp  Integer::operator >>  (long y)
  860. {
  861.   return(lshift(rep, -y, 0));
  862. }
  863.  
  864. inline IntTmp  IntTmp::operator >>  (Integer& y)
  865. {
  866.   IntRep* r =  lshift(rep, y.rep, 1, rep); rep = 0; return r;
  867. }
  868.  
  869. inline IntTmp  IntTmp::operator >>  (IntTmp& y)
  870. {
  871.   IntRep* r =  lshift(rep, y.rep, 1, rep); rep = 0; return r;
  872. }
  873.  
  874. inline IntTmp  IntTmp::operator >>  (long y)
  875. {
  876.   IntRep* r = lshift(rep, -y, rep); rep = 0; return r;
  877. }
  878.  
  879. inline void Integer::operator >>= (Integer&  y)
  880. {
  881.   rep = lshift(rep, y.rep, 1, rep);
  882. }
  883.  
  884. inline void  Integer::operator >>= (long y)
  885. {
  886.   rep = lshift(rep, -y, rep); 
  887. }
  888.  
  889. inline Integer& operator <? (Integer& x, Integer& y)
  890. {
  891.   return (compare(x.rep, y.rep) <= 0) ? x : y;
  892. }
  893.  
  894. inline Integer& operator >? (Integer& x, Integer& y)
  895. {
  896.   return (compare(x.rep, y.rep) >= 0)?  x : y;
  897. }
  898.  
  899. inline int sign(Integer& x)
  900. {
  901.   return (x.rep->len == 0) ? 0 : ( (x.rep->sgn == 1) ? 1 : -1 );
  902. }
  903.  
  904. inline IntTmp abs(Integer& x)
  905. {
  906.   return(Ialloc(0, x.rep->s, x.rep->len, 1, x.rep->len));
  907. }
  908.  
  909. inline IntTmp abs(IntTmp& x)
  910. {
  911.   IntRep* r = x.rep; r->sgn = 1; x.rep = 0; return r;
  912. }
  913.  
  914. inline void Integer::abs()
  915. {
  916.   rep->sgn = 1; 
  917. }
  918.  
  919. inline void Integer::negate()
  920. {
  921.   if (rep->len != 0) rep->sgn = !rep->sgn;
  922. }
  923.  
  924. inline IntTmp Integer::operator - ()
  925. {
  926.   int sgn = (rep->len == 0)? rep->sgn : !rep->sgn;
  927.   return(Ialloc(0, rep->s, rep->len, sgn, rep->len));
  928. }
  929.  
  930. inline IntTmp IntTmp::operator - ()
  931. {
  932.   IntRep* r = rep; if (r->len != 0) r->sgn = !r->sgn; rep = 0; return r;
  933. }
  934.  
  935. inline IntTmp pow(Integer& x, long y)
  936. {
  937.   return(power(x.rep, y, 0));
  938. }
  939.  
  940. inline IntTmp Ipow(long x, long y)
  941. {
  942.   IntRep* r = Icopy_long(0, x);  return(power(r, y, r));
  943. }
  944.  
  945. inline IntTmp pow(Integer& x, Integer& y)
  946. {
  947.   long yy = long(y);   return power(x.rep, yy, 0); // not incorrect!
  948. }
  949.  
  950. inline IntTmp pow(IntTmp& x, long y)
  951. {
  952.   IntRep* r = power(x.rep, y, x.rep); x.rep = 0; return r;
  953. }
  954.  
  955. inline int even(Integer& y)
  956. {
  957.   return y.rep->len == 0 || !(y.rep->s[0] & 1);
  958. }
  959.  
  960. inline int odd(Integer& y)
  961. {
  962.   return y.rep->len > 0 && (y.rep->s[0] & 1);
  963. }
  964.  
  965. inline char* Itoa(Integer& y, int base = 10, int width = 0)
  966. {
  967.   return Itoa(y.rep, base, width);
  968. }
  969.  
  970. inline IntTmp  atoI(const char* s, int base = 10)
  971. {
  972.   return atoIntRep(s, base);
  973. }
  974.  
  975. inline ostream& operator << (ostream& s, Integer& y)
  976. {
  977.   return s << Itoa(y.rep);
  978. }
  979.  
  980. inline long Integer::operator long()
  981.   return Itolong(rep);
  982. }
  983.  
  984. inline int Integer::fits_in_long()
  985. {
  986.   return Iislong(rep);
  987. }
  988.  
  989. inline double Integer::operator double()
  990.   return Itodouble(rep);
  991. }
  992.  
  993. inline int Integer::fits_in_double()   
  994. {
  995.   return Iisdouble(rep);
  996. }
  997.  
  998. inline IntTmp  Integer::operator ~ ()
  999. {
  1000.   return compl(rep, 0);
  1001. }
  1002.  
  1003. inline IntTmp  IntTmp::operator ~ ()
  1004. {
  1005.   IntRep* r = compl(rep, rep); rep = 0; return r;
  1006. }
  1007.  
  1008. inline IntTmp  gcd(Integer& x, Integer& y)
  1009. {
  1010.   return gcd(x.rep, y.rep);
  1011. }
  1012.  
  1013. inline long lg(Integer& x)
  1014. {
  1015.   return lg(x.rep);
  1016. }
  1017.  
  1018. //#endif
  1019.  
  1020. #endif
  1021.