home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 4
/
FreshFish_May-June1994.bin
/
gnu
/
lib
/
g++-include
/
integer.h
< prev
next >
Wrap
C/C++ Source or Header
|
1994-02-20
|
27KB
|
1,097 lines
// This may look like C code, but it is really -*- C++ -*-
/*
Copyright (C) 1988 Free Software Foundation
written by Doug Lea (dl@rocky.oswego.edu)
This file is part of the GNU C++ Library. This library is free
software; you can redistribute it and/or modify it under the terms of
the GNU Library General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version. This library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef _Integer_h
#ifdef __GNUG__
#pragma interface
#endif
#define _Integer_h 1
#include <iostream.h>
struct IntRep // internal Integer representations
{
unsigned short len; // current length
unsigned short sz; // allocated space (0 means static).
short sgn; // 1 means >= 0; 0 means < 0
unsigned short s[1]; // represented as ushort array starting here
};
// True if REP is staticly (or manually) allocated,
// and should not be deleted by an Integer destructor.
#define STATIC_IntRep(rep) ((rep)->sz==0)
extern IntRep* Ialloc(IntRep*, const unsigned short *, int, int, int);
extern IntRep* Icalloc(IntRep*, int);
extern IntRep* Icopy_ulong(IntRep*, unsigned long);
extern IntRep* Icopy_long(IntRep*, long);
extern IntRep* Icopy(IntRep*, const IntRep*);
extern IntRep* Iresize(IntRep*, int);
extern IntRep* add(const IntRep*, int, const IntRep*, int, IntRep*);
extern IntRep* add(const IntRep*, int, long, IntRep*);
extern IntRep* multiply(const IntRep*, const IntRep*, IntRep*);
extern IntRep* multiply(const IntRep*, long, IntRep*);
extern IntRep* lshift(const IntRep*, long, IntRep*);
extern IntRep* lshift(const IntRep*, const IntRep*, int, IntRep*);
extern IntRep* bitop(const IntRep*, const IntRep*, IntRep*, char);
extern IntRep* bitop(const IntRep*, long, IntRep*, char);
extern IntRep* power(const IntRep*, long, IntRep*);
extern IntRep* div(const IntRep*, const IntRep*, IntRep*);
extern IntRep* mod(const IntRep*, const IntRep*, IntRep*);
extern IntRep* div(const IntRep*, long, IntRep*);
extern IntRep* mod(const IntRep*, long, IntRep*);
extern IntRep* compl(const IntRep*, IntRep*);
extern IntRep* abs(const IntRep*, IntRep*);
extern IntRep* negate(const IntRep*, IntRep*);
extern IntRep* pow(const IntRep*, long);
extern IntRep* gcd(const IntRep*, const IntRep* y);
extern int compare(const IntRep*, const IntRep*);
extern int compare(const IntRep*, long);
extern int ucompare(const IntRep*, const IntRep*);
extern int ucompare(const IntRep*, long);
extern char* Itoa(const IntRep* x, int base = 10, int width = 0);
extern char* cvtItoa(const IntRep* x, char* fmt, int& fmtlen, int base,
int showbase, int width, int align_right,
char fillchar, char Xcase, int showpos);
extern IntRep* atoIntRep(const char* s, int base = 10);
extern long Itolong(const IntRep*);
extern int Iislong(const IntRep*);
extern long lg(const IntRep*);
extern IntRep _ZeroRep, _OneRep, _MinusOneRep;
class Integer
{
protected:
IntRep* rep;
public:
Integer();
Integer(int);
Integer(long);
Integer(unsigned long);
Integer(IntRep*);
Integer(const Integer&);
~Integer();
void operator = (const Integer&);
void operator = (long);
// unary operations to self
void operator ++ ();
void operator -- ();
void negate(); // negate in-place
void abs(); // absolute-value in-place
void complement(); // bitwise complement in-place
// assignment-based operations
void operator += (const Integer&);
void operator -= (const Integer&);
void operator *= (const Integer&);
void operator /= (const Integer&);
void operator %= (const Integer&);
void operator <<=(const Integer&);
void operator >>=(const Integer&);
void operator &= (const Integer&);
void operator |= (const Integer&);
void operator ^= (const Integer&);
void operator += (long);
void operator -= (long);
void operator *= (long);
void operator /= (long);
void operator %= (long);
void operator <<=(long);
void operator >>=(long);
void operator &= (long);
void operator |= (long);
void operator ^= (long);
// (constructive binary operations are inlined below)
#ifdef __GNUG__
friend Integer operator <? (const Integer& x, const Integer& y); // min
friend Integer operator >? (const Integer& x, const Integer& y); // max
#endif
// builtin Integer functions that must be friends
friend long lg (const Integer&); // floor log base 2 of abs(x)
friend double ratio(const Integer& x, const Integer& y);
// return x/y as a double
friend Integer gcd(const Integer&, const Integer&);
friend int even(const Integer&); // true if even
friend int odd(const Integer&); // true if odd
friend int sign(const Integer&); // returns -1, 0, +1
friend void (setbit)(Integer& x, long b); // set b'th bit of x
friend void clearbit(Integer& x, long b); // clear b'th bit
friend int testbit(const Integer& x, long b); // return b'th bit
// procedural versions of operators
friend void abs(const Integer& x, Integer& dest);
friend void negate(const Integer& x, Integer& dest);
friend void complement(const Integer& x, Integer& dest);
friend int compare(const Integer&, const Integer&);
friend int ucompare(const Integer&, const Integer&);
friend void add(const Integer& x, const Integer& y, Integer& dest);
friend void sub(const Integer& x, const Integer& y, Integer& dest);
friend void mul(const Integer& x, const Integer& y, Integer& dest);
friend void div(const Integer& x, const Integer& y, Integer& dest);
friend void mod(const Integer& x, const Integer& y, Integer& dest);
friend void divide(const Integer& x, const Integer& y,
Integer& q, Integer& r);
friend void and(const Integer& x, const Integer& y, Integer& dest);
friend void or(const Integer& x, const Integer& y, Integer& dest);
friend void xor(const Integer& x, const Integer& y, Integer& dest);
friend void lshift(const Integer& x, const Integer& y, Integer& dest);
friend void rshift(const Integer& x, const Integer& y, Integer& dest);
friend void pow(const Integer& x, const Integer& y, Integer& dest);
friend int compare(const Integer&, long);
friend int ucompare(const Integer&, long);
friend void add(const Integer& x, long y, Integer& dest);
friend void sub(const Integer& x, long y, Integer& dest);
friend void mul(const Integer& x, long y, Integer& dest);
friend void div(const Integer& x, long y, Integer& dest);
friend void mod(const Integer& x, long y, Integer& dest);
friend void divide(const Integer& x, long y, Integer& q, long& r);
friend void and(const Integer& x, long y, Integer& dest);
friend void or(const Integer& x, long y, Integer& dest);
friend void xor(const Integer& x, long y, Integer& dest);
friend void lshift(const Integer& x, long y, Integer& dest);
friend void rshift(const Integer& x, long y, Integer& dest);
friend void pow(const Integer& x, long y, Integer& dest);
friend int compare(long, const Integer&);
friend int ucompare(long, const Integer&);
fri