]> Dogcows Code - chaz/yoink/blob - src/stlplus/portability/inf.hpp
cleanup stlplus files
[chaz/yoink] / src / stlplus / portability / inf.hpp
1 #ifndef STLPLUS_INF
2 #define STLPLUS_INF
3 ////////////////////////////////////////////////////////////////////////////////
4
5 // Author: Andy Rushton
6 // Copyright: (c) Southampton University 1999-2004
7 // (c) Andy Rushton 2004-2009
8 // License: BSD License, see ../docs/license.html
9
10 // An infinite-precision integer class. This allows calculations on large
11 // integers to be performed without overflow.
12
13 // this class can throw the following exceptions:
14 // std::out_of_range
15 // std::overflow_error
16 // std::invalid_argument
17 // stlplus::divide_by_zero // why doesn't std have this?
18 // all of these are derivations of the baseclass:
19 // std::logic_error
20 // So you can catch all of them by catching the baseclass
21
22 // Warning: inf was never intended to be fast, it is just for programs which
23 // need a bit of infinite-precision integer arithmetic. For high-performance
24 // processing, use the Gnu Multi-Precision (GMP) library. The inf type is just
25 // easier to integrate and is already ported to all platforms and compilers
26 // that STLplus is ported to.
27
28 ////////////////////////////////////////////////////////////////////////////////
29 #include "portability_fixes.hpp"
30 #include "portability_exceptions.hpp"
31 #include <string>
32 #include <iostream>
33
34 ////////////////////////////////////////////////////////////////////////////////
35
36 namespace stlplus
37 {
38
39 ////////////////////////////////////////////////////////////////////////////////
40
41 class inf
42 {
43 public:
44
45 //////////////////////////////////////////////////////////////////////////////
46 // constructors and assignments initialise the inf
47
48 // the void constructor initialises to zero, the others initialise to the
49 // value of the C integer type or the text value contained in the string
50
51 inf(void);
52 explicit inf(short);
53 explicit inf(unsigned short);
54 explicit inf(int);
55 explicit inf(unsigned);
56 explicit inf(long);
57 explicit inf(unsigned long);
58 explicit inf(const std::string&) throw(std::invalid_argument);
59 inf(const inf&);
60
61 ~inf(void);
62
63 // assignments with equivalent behaviour to the constructors
64
65 inf& operator = (short);
66 inf& operator = (unsigned short);
67 inf& operator = (int);
68 inf& operator = (unsigned);
69 inf& operator = (long);
70 inf& operator = (unsigned long);
71 inf& operator = (const std::string&) throw(std::invalid_argument);
72 inf& operator = (const inf&);
73
74 //////////////////////////////////////////////////////////////////////////////
75 // conversions back to the C types
76 // truncate: controls the behaviour when the value is too long for the result
77 // true: truncate the value
78 // false: throw an exception
79
80 short to_short(bool truncate = true) const throw(std::overflow_error);
81 unsigned short to_unsigned_short(bool truncate = true) const throw(std::overflow_error);
82
83 int to_int(bool truncate = true) const throw(std::overflow_error);
84 unsigned to_unsigned(bool truncate = true) const throw(std::overflow_error);
85
86 long to_long(bool truncate = true) const throw(std::overflow_error);
87 unsigned long to_unsigned_long(bool truncate = true) const throw(std::overflow_error);
88
89 //////////////////////////////////////////////////////////////////////////////
90 // bitwise manipulation
91
92 void resize(unsigned bits);
93 void reduce(void);
94
95 // the number of significant bits in the value
96 unsigned bits (void) const;
97 unsigned size (void) const;
98
99 // the number of bits that can be accessed by the bit() method (=bits() rounded up to the next byte)
100 unsigned indexable_bits(void) const;
101
102 bool bit (unsigned index) const throw(std::out_of_range);
103 bool operator [] (unsigned index) const throw(std::out_of_range);
104
105 void set (unsigned index) throw(std::out_of_range);
106 void clear (unsigned index) throw(std::out_of_range);
107 void preset (unsigned index, bool value) throw(std::out_of_range);
108
109 inf slice(unsigned low, unsigned high) const throw(std::out_of_range);
110
111 //////////////////////////////////////////////////////////////////////////////
112 // tests for common values or ranges
113
114 bool negative (void) const;
115 bool natural (void) const;
116 bool positive (void) const;
117 bool zero (void) const;
118 bool non_zero (void) const;
119
120 // tests used in if(i) and if(!i)
121 // operator bool (void) const;
122 bool operator ! (void) const;
123
124 //////////////////////////////////////////////////////////////////////////////
125 // comparisons
126
127 bool operator == (const inf&) const;
128 bool operator != (const inf&) const;
129 bool operator < (const inf&) const;
130 bool operator <= (const inf&) const;
131 bool operator > (const inf&) const;
132 bool operator >= (const inf&) const;
133
134 //////////////////////////////////////////////////////////////////////////////
135 // bitwise logic operations
136
137 inf& invert (void);
138 inf operator ~ (void) const;
139
140 inf& operator &= (const inf&);
141 inf operator & (const inf&) const;
142
143 inf& operator |= (const inf&);
144 inf operator | (const inf&) const;
145
146 inf& operator ^= (const inf&);
147 inf operator ^ (const inf&) const;
148
149 inf& operator <<= (unsigned shift);
150 inf operator << (unsigned shift) const;
151
152 inf& operator >>= (unsigned shift);
153 inf operator >> (unsigned shift) const;
154
155 //////////////////////////////////////////////////////////////////////////////
156 // arithmetic operations
157
158 inf& negate (void);
159 inf operator - (void) const;
160
161 inf& abs(void);
162 friend inf abs(const inf&);
163
164 inf& operator += (const inf&);
165 inf operator + (const inf&) const;
166
167 inf& operator -= (const inf&);
168 inf operator - (const inf&) const;
169
170 inf& operator *= (const inf&);
171 inf operator * (const inf&) const;
172
173 inf& operator /= (const inf&) throw(divide_by_zero);
174 inf operator / (const inf&) const throw(divide_by_zero);
175
176 inf& operator %= (const inf&) throw(divide_by_zero);
177 inf operator % (const inf&) const throw(divide_by_zero);
178
179 // combined division operator - returns the result pair(quotient,remainder) in one go
180 std::pair<inf,inf> divide(const inf&) const throw(divide_by_zero);
181
182 //////////////////////////////////////////////////////////////////////////////
183 // pre- and post- increment and decrement
184
185 inf& operator ++ (void);
186 inf operator ++ (int);
187 inf& operator -- (void);
188 inf operator -- (int);
189
190 //////////////////////////////////////////////////////////////////////////////
191 // string representation and I/O
192
193 std::string image_debug(void) const;
194
195 // conversion to a string representation
196 // radix must be 10, 2, 8 or 16
197 std::string to_string(unsigned radix = 10) const
198 throw(std::invalid_argument);
199
200 // conversion from a string
201 // radix == 0 - radix is deduced from the input - assumed 10 unless number is prefixed by 0b, 0 or 0x
202 // however, you can specify the radix to be 10, 2, 8 or 16 to force that interpretation
203 inf& from_string(const std::string&, unsigned radix = 0)
204 throw(std::invalid_argument);
205
206 //////////////////////////////////////////////////////////////////////////////
207 private:
208 std::string m_data;
209 public:
210 const std::string& get_bytes(void) const;
211 void set_bytes(const std::string&);
212 };
213
214 ////////////////////////////////////////////////////////////////////////////////
215 // redefine friends for gcc v4.1
216
217 inf abs(const inf&);
218
219 ////////////////////////////////////////////////////////////////////////////////
220
221 std::ostream& operator << (std::ostream&, const inf&);
222 std::istream& operator >> (std::istream&, inf&);
223
224 ////////////////////////////////////////////////////////////////////////////////
225
226 } // end namespace stlplus
227
228 #endif
This page took 0.039949 seconds and 4 git commands to generate.