Use correct font metrics from truetype files.
[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 uint64_t digit_t;
18                 
19                         Arbint(int64_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                         virtual ~Arbint() {}
24                         Arbint(const Arbint & cpy);
25                         
26                         int64_t AsDigit() const
27                         {
28                                 int64_t digit = (m_digits.size() == 1) ? m_digits[0] : 0xBADF00D;
29                                 return (m_sign) ? -digit : digit;
30                         }
31                         
32                         inline bool Sign() const {return m_sign;}
33                         inline char SignChar() const {return (m_sign) ? '-' : '+';}
34                         std::string DigitStr() const;
35
36                         std::string Str(const std::string & base="0123456789") const;
37                         inline std::string Str(const char * base) const
38                         {
39                                 return Str(std::string(base));
40                         }
41                         
42                         Arbint & operator=(const Arbint & equ);
43                         Arbint & operator+=(const Arbint & add);
44                         Arbint & operator-=(const Arbint & sub);
45                         Arbint & operator*=(const Arbint & mul);
46                         void Division(const Arbint & div, Arbint & result, Arbint & modulo) const;
47                         
48                         Arbint & operator<<=(unsigned amount);
49                         Arbint & operator>>=(unsigned amount);
50                         
51                         inline Arbint operator+(const Arbint & add) const 
52                         {
53                                 Arbint a(*this);
54                                 a += add;
55                                 return a;
56                         }
57                         inline Arbint operator-(const Arbint & add) const 
58                         {
59                                 Arbint a(*this);
60                                 a -= add;
61                                 return a;
62                         }
63
64                         inline Arbint operator-()
65                         {
66                                 Arbint a(*this);
67                                 a.m_sign = !a.m_sign;
68                                 return a;
69                         }
70                         
71                         inline Arbint operator-() const
72                         {
73                                 Arbint a(*this);
74                                 a.m_sign = !a.m_sign;
75                                 return a;
76                         }
77                         
78                         inline Arbint operator*(const Arbint & mul) const
79                         {
80                                 Arbint a(*this);
81                                 a *= mul;
82                                 return a;
83                         }
84                         
85                         inline Arbint & operator/=(const Arbint & div)
86                         {
87                                 Arbint result(0L);
88                                 Arbint remainder(0L);
89                                 this->Division(div, result, remainder);
90                                 this->operator=(result);
91                                 return *this;
92                         }
93                         inline Arbint operator/(const Arbint & div)
94                         {
95                                 Arbint cpy(*this);
96                                 cpy /= div;
97                                 return cpy;
98                         }
99                         inline Arbint operator%(const Arbint & div)
100                         {
101                                 Arbint result(0L);
102                                 Arbint remainder(0L);
103                                 this->Division(div, result, remainder);
104                                 return remainder;
105                         }
106                         
107                         bool operator==(const Arbint & equ) const;
108                         bool operator<(const Arbint & less) const;
109                         
110
111                         inline bool operator!=(const Arbint & equ) const 
112                         {
113                                 return !(this->operator==(equ));
114                         }
115                         inline bool operator<=(const Arbint & leq) const 
116                         {
117                                 return (this->operator==(leq) || this->operator<(leq));
118                         }
119                         
120                         inline bool operator>=(const Arbint & leq) const 
121                         {
122                                 return (this->operator==(leq) || this->operator>(leq));
123                         }
124                         inline bool operator>(const Arbint & grea) const
125                         {
126                                 return !(this->operator<=(grea));
127                         }
128                         inline Arbint operator>>(unsigned amount) const
129                         {
130                                 Arbint result(*this);
131                                 result >>= amount;
132                                 return result;
133                         }
134                         inline Arbint operator<<(unsigned amount) const
135                         {
136                                 Arbint result(*this);
137                                 result <<= amount;
138                                 return result;
139                         }
140                         bool IsZero() const;
141
142                         inline operator double() const
143                         {
144                                 double acc = 0;
145                                 for (int i = m_digits.size()-1; i >= 0; --i)
146                                 {
147                                         acc += (double)m_digits[i];
148                                         acc *= (double)UINT64_MAX + 1.0;
149                                 }
150                                 if (m_sign) acc *= -1;
151                                 return acc;
152                         }
153                         inline operator int64_t() const {return AsDigit();}
154                         //inline operator int() const {return int(AsDigit());}
155                         
156                         unsigned Shrink();
157                         
158                         inline Arbint Abs() const {Arbint a(*this); a.m_sign = false; return a;}
159                 private:                
160                                 
161                         Arbint & AddBasic(const Arbint & add);
162                         Arbint & SubBasic(const Arbint & sub);
163                         
164                         void GrowDigit(digit_t new_msd); // add a new most significant digit
165                         
166                         bool GetBit(unsigned i) const;
167                         void BitClear(unsigned i);
168                         void BitSet(unsigned i);
169                         
170         
171                         std::vector<digit_t> m_digits;
172                         bool m_sign;
173                         void Zero();
174                         
175         };      
176
177
178
179 extern "C"
180 {
181         typedef uint64_t digit_t;
182         digit_t add_digits(digit_t * dst, digit_t * add, digit_t size);
183         digit_t sub_digits(digit_t * dst, digit_t * add, digit_t size);
184         digit_t mul_digits(digit_t * dst, digit_t mul, digit_t size);
185         digit_t div_digits(digit_t * dst, digit_t div, digit_t size, digit_t * rem);
186 }
187
188
189
190 }
191 #endif //_ARBINT_H

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