Arbint class implemented
[ipdf/code.git] / src / arbint.h
1 /**
2  * @file arbint.h
3  * @brief Arbitrary sized integer declarations
4  * @see arbint.cpp
5  */
6  
7 #ifndef _ARBINT_H
8 #define _ARBINT_H
9
10 #include "common.h"
11
12 namespace IPDF
13 {
14         class Arbint
15         {
16                 public:
17                         typedef int64_t digit_t;
18                 
19                         Arbint(digit_t i);
20                         Arbint(const std::vector<digit_t> & digits);
21                         Arbint(unsigned n, digit_t d0, ...);
22                         Arbint(const std::string & str, const std::string & base="0123456789");
23                         ~Arbint() {}
24                         Arbint(const Arbint & cpy);
25                         
26                         digit_t AsDigit() const 
27                         {
28                                 return (m_sign) ? -m_digits[0] : m_digits[0];
29                         }
30                         
31                         inline bool Sign() const {return m_sign;}
32                         inline char SignChar() const {return (m_sign) ? '-' : '+';}
33                         std::string DigitStr() const;
34
35                         std::string Str(const std::string & base="0123456789") const;
36                         inline std::string Str(const char * base) const
37                         {
38                                 return Str(std::string(base));
39                         }
40                         
41                         Arbint & operator=(const Arbint & equ);
42                         Arbint & operator+=(const Arbint & add);
43                         Arbint & operator-=(const Arbint & sub);
44                         Arbint & operator*=(const Arbint & mul);
45                         void Division(const Arbint & div, Arbint & result, Arbint & modulo) const;
46                         
47                         inline Arbint operator+(const Arbint & add) const 
48                         {
49                                 Arbint a(*this);
50                                 a += add;
51                                 return a;
52                         }
53                         inline Arbint operator-(const Arbint & add) const 
54                         {
55                                 Arbint a(*this);
56                                 a -= add;
57                                 return a;
58                         }
59                         
60                         inline Arbint operator*(const Arbint & mul) const
61                         {
62                                 Arbint a(*this);
63                                 a *= mul;
64                                 return a;
65                         }
66                         
67                         inline Arbint & operator/=(const Arbint & div)
68                         {
69                                 Arbint result(0L);
70                                 Arbint remainder(0L);
71                                 this->Division(div, result, remainder);
72                                 this->operator=(result);
73                                 return *this;
74                         }
75                         inline Arbint operator/(const Arbint & div)
76                         {
77                                 Arbint cpy(*this);
78                                 cpy /= div;
79                                 return cpy;
80                         }
81                         inline Arbint operator%(const Arbint & div)
82                         {
83                                 Arbint result(0L);
84                                 Arbint remainder(0L);
85                                 this->Division(div, result, remainder);
86                                 return remainder;
87                         }
88                         
89                         bool operator==(const Arbint & equ) const;
90                         bool operator<(const Arbint & less) const;
91
92                         inline bool operator!=(const Arbint & equ) const 
93                         {
94                                 return !(this->operator==(equ));
95                         }
96                         inline bool operator<=(const Arbint & leq) const 
97                         {
98                                 return (this->operator==(leq) || this->operator<(leq));
99                         }
100                         
101                         inline bool operator>=(const Arbint & leq) const 
102                         {
103                                 return (this->operator==(leq) || this->operator<(leq));
104                         }
105                         inline bool operator>(const Arbint & grea) const
106                         {
107                                 return !(this->operator<=(grea));
108                         }
109                         
110                         bool IsZero() const;
111                         
112                         unsigned Shrink();
113                 private:                
114                                 
115                         Arbint & AddBasic(const Arbint & add);
116                         Arbint & SubBasic(const Arbint & sub);
117         
118                         std::vector<digit_t> m_digits;
119                         bool m_sign;
120                         void Zero();
121                         
122         };      
123
124 extern "C"
125 {
126         typedef int64_t digit_t;
127         digit_t add_digits(digit_t * dst, digit_t * add, digit_t size);
128         digit_t sub_digits(digit_t * dst, digit_t * add, digit_t size);
129         digit_t mul_digits(digit_t * dst, digit_t mul, digit_t size);
130 }
131
132 }
133 #endif //_ARBINT_H

UCC git Repository :: git.ucc.asn.au