# int64.h

Go to the documentation of this file.

    00001 #ifndef PTLIB_INT64_H
    00002 #define PTLIB_INT64_H
    00003
    00005 // Really big integer class for architectures without
    00006
    00007 #ifdef P_NEEDS_INT64
    00008
    00009 class PInt64__ {
    00010   public:
    00011     operator long()  const { return (long)low; }
    00012     operator int()   const { return (int)low; }
    00013     operator short() const { return (short)low; }
    00014     operator char()  const { return (char)low; }
    00015
    00016     operator unsigned long()  const { return (unsigned long)low; }
    00017     operator unsigned int()   const { return (unsigned int)low; }
    00018     operator unsigned short() const { return (unsigned short)low; }
    00019     operator unsigned char()  const { return (unsigned char)low; }
    00020
    00021   protected:
    00022     PInt64__() { }
    00023     PInt64__(unsigned long l) : low(l), high(0) { }
    00024     PInt64__(unsigned long l, unsigned long h) : low(l), high(h) { }
    00025
    00026     void operator=(const PInt64__ & v) { low = v.low; high = v.high; }
    00027
    00028     void Inc() { if (++low == 0) ++high; }
    00029     void Dec() { if (--low == 0) --high; }
    00030
    00031     void Or (long v) { low |= v; }
    00032     void And(long v) { low &= v; }
    00033     void Xor(long v) { low ^= v; }
    00034
    00035     void Add(const PInt64__ & v);
    00036     void Sub(const PInt64__ & v);
    00037     void Mul(const PInt64__ & v);
    00038     void Div(const PInt64__ & v);
    00039     void Mod(const PInt64__ & v);
    00040     void Or (const PInt64__ & v) { low |= v.low; high |= v.high; }
    00041     void And(const PInt64__ & v) { low &= v.low; high &= v.high; }
    00042     void Xor(const PInt64__ & v) { low ^= v.low; high ^= v.high; }
    00043     void ShiftLeft(int bits);
    00044     void ShiftRight(int bits);
    00045
    00046     PBoolean Eq(unsigned long v) const { return low == v && high == 0; }
    00047     PBoolean Ne(unsigned long v) const { return low != v || high != 0; }
    00048
    00049     PBoolean Eq(const PInt64__ & v) const { return low == v.low && high == v.high; }
    00050     PBoolean Ne(const PInt64__ & v) const { return low != v.low || high != v.high; }
    00051
    00052     unsigned long low, high;
    00053 };
    00054
    00055
    00056 #define DECL_OPS(cls, type) \
    00057     const cls & operator=(type v) { PInt64__::operator=(cls(v)); return *this; } \
    00058     cls operator+(type v) const { cls t = *this; t.Add(v); return t; } \
    00059     cls operator-(type v) const { cls t = *this; t.Sub(v); return t; } \
    00060     cls operator*(type v) const { cls t = *this; t.Mul(v); return t; } \
    00061     cls operator/(type v) const { cls t = *this; t.Div(v); return t; } \
    00062     cls operator%(type v) const { cls t = *this; t.Mod(v); return t; } \
    00063     cls operator|(type v) const { cls t = *this; t.Or (v); return t; } \
    00064     cls operator&(type v) const { cls t = *this; t.And(v); return t; } \
    00065     cls operator^(type v) const { cls t = *this; t.Xor(v); return t; } \
    00066     cls operator<<(type v) const { cls t = *this; t.ShiftLeft((int)v); return t; } \
    00067     cls operator>>(type v) const { cls t = *this; t.ShiftRight((int)v); return t; } \
    00068     const cls & operator+=(type v) { Add(v); return *this; } \
    00069     const cls & operator-=(type v) { Sub(v); return *this; } \
    00070     const cls & operator*=(type v) { Mul(v); return *this; } \
    00071     const cls & operator/=(type v) { Div(v); return *this; } \
    00072     const cls & operator|=(type v) { Or (v); return *this; } \
    00073     const cls & operator&=(type v) { And(v); return *this; } \
    00074     const cls & operator^=(type v) { Xor(v); return *this; } \
    00075     const cls & operator<<=(type v) { ShiftLeft((int)v); return *this; } \
    00076     const cls & operator>>=(type v) { ShiftRight((int)v); return *this; } \
    00077     PBoolean operator==(type v) const { return Eq(v); } \
    00078     PBoolean operator!=(type v) const { return Ne(v); } \
    00079     PBoolean operator< (type v) const { return Lt(v); } \
    00080     PBoolean operator> (type v) const { return Gt(v); } \
    00081     PBoolean operator>=(type v) const { return !Gt(v); } \
    00082     PBoolean operator<=(type v) const { return !Lt(v); } \
    00083
    00084
    00085 class PInt64 : public PInt64__ {
    00086   public:
    00087     PInt64() { }
    00088     PInt64(long l) : PInt64__(l, l < 0 ? -1 : 0) { }
    00089     PInt64(unsigned long l, long h) : PInt64__(l, h) { }
    00090     PInt64(const PInt64__ & v) : PInt64__(v) { }
    00091
    00092     PInt64 operator~() const { return PInt64(~low, ~high); }
    00093     PInt64 operator-() const { return operator~()+1; }
    00094
    00095     PInt64 operator++() { Inc(); return *this; }
    00096     PInt64 operator--() { Dec(); return *this; }
    00097
    00098     PInt64 operator++(int) { PInt64 t = *this; Inc(); return t; }
    00099     PInt64 operator--(int) { PInt64 t = *this; Dec(); return t; }
    00100
    00101     DECL_OPS(PInt64, char)
    00102     DECL_OPS(PInt64, unsigned char)
    00103     DECL_OPS(PInt64, short)
    00104     DECL_OPS(PInt64, unsigned short)
    00105     DECL_OPS(PInt64, int)
    00106     DECL_OPS(PInt64, unsigned int)
    00107     DECL_OPS(PInt64, long)
    00108     DECL_OPS(PInt64, unsigned long)
    00109     DECL_OPS(PInt64, const PInt64 &)
    00110
    00111     friend ostream & operator<<(ostream &, const PInt64 &);
    00112     friend istream & operator>>(istream &, PInt64 &);
    00113
    00114   protected:
    00115     void Add(long v) { Add(PInt64(v)); }
    00116     void Sub(long v) { Sub(PInt64(v)); }
    00117     void Mul(long v) { Mul(PInt64(v)); }
    00118     void Div(long v) { Div(PInt64(v)); }
    00119     void Mod(long v) { Mod(PInt64(v)); }
    00120     PBoolean Lt(long v) const { return Lt(PInt64(v)); }
    00121     PBoolean Gt(long v) const { return Gt(PInt64(v)); }
    00122     PBoolean Lt(const PInt64 &) const;
    00123     PBoolean Gt(const PInt64 &) const;
    00124 };
    00125
    00126
    00127 class PUInt64 : public PInt64__ {
    00128   public:
    00129     PUInt64() { }
    00130     PUInt64(unsigned long l) : PInt64__(l, 0) { }
    00131     PUInt64(unsigned long l, unsigned long h) : PInt64__(l, h) { }
    00132     PUInt64(const PInt64__ & v) : PInt64__(v) { }
    00133
    00134     PUInt64 operator~() const { return PUInt64(~low, ~high); }
    00135
    00136     const PUInt64 & operator++() { Inc(); return *this; }
    00137     const PUInt64 & operator--() { Dec(); return *this; }
    00138
    00139     PUInt64 operator++(int) { PUInt64 t = *this; Inc(); return t; }
    00140     PUInt64 operator--(int) { PUInt64 t = *this; Dec(); return t; }
    00141
    00142     DECL_OPS(PUInt64, char)
    00143     DECL_OPS(PUInt64, unsigned char)
    00144     DECL_OPS(PUInt64, short)
    00145     DECL_OPS(PUInt64, unsigned short)
    00146     DECL_OPS(PUInt64, int)
    00147     DECL_OPS(PUInt64, unsigned int)
    00148     DECL_OPS(PUInt64, long)
    00149     DECL_OPS(PUInt64, unsigned long)
    00150     DECL_OPS(PUInt64, const PUInt64 &)
    00151
    00152     friend ostream & operator<<(ostream &, const PUInt64 &);
    00153     friend istream & operator>>(istream &, PUInt64 &);
    00154
    00155   protected:
    00156     void Add(long v) { Add(PUInt64(v)); }
    00157     void Sub(long v) { Sub(PUInt64(v)); }
    00158     void Mul(long v) { Mul(PUInt64(v)); }
    00159     void Div(long v) { Div(PUInt64(v)); }
    00160     void Mod(long v) { Mod(PUInt64(v)); }
    00161     PBoolean Lt(long v) const { return Lt(PUInt64(v)); }
    00162     PBoolean Gt(long v) const { return Gt(PUInt64(v)); }
    00163     PBoolean Lt(const PUInt64 &) const;
    00164     PBoolean Gt(const PUInt64 &) const;
    00165 };
    00166
    00167 #undef DECL_OPS
    00168
    00169 #endif  // P_NEEDS_INT64
    00170
    00171 #endif  // PTLIB_INT64_H
    00172
    00173
    00174 // End Of File ///////////////////////////////////////////////////////////////