]>
Dogcows Code - chaz/yoink/blob - src/stlplus/portability/inf.hpp
f28541af6bf9ce7083ffb4f7b26aeb9c8f807321
3 ////////////////////////////////////////////////////////////////////////////////
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
10 // An infinite-precision integer class. This allows calculations on large
11 // integers to be performed without overflow.
13 // this class can throw the following exceptions:
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:
20 // So you can catch all of them by catching the baseclass
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.
28 ////////////////////////////////////////////////////////////////////////////////
29 #include "portability_fixes.hpp"
30 #include "portability_exceptions.hpp"
34 ////////////////////////////////////////////////////////////////////////////////
39 ////////////////////////////////////////////////////////////////////////////////
45 //////////////////////////////////////////////////////////////////////////////
46 // constructors and assignments initialise the inf
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
53 explicit inf(unsigned short);
55 explicit inf(unsigned);
57 explicit inf(unsigned long);
58 explicit inf(const std::string
&) throw(std::invalid_argument
);
63 // assignments with equivalent behaviour to the constructors
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
&);
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
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
);
83 int to_int(bool truncate
= true) const throw(std::overflow_error
);
84 unsigned to_unsigned(bool truncate
= true) const throw(std::overflow_error
);
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
);
89 //////////////////////////////////////////////////////////////////////////////
90 // bitwise manipulation
92 void resize(unsigned bits
);
95 // the number of significant bits in the value
96 unsigned bits (void) const;
97 unsigned size (void) const;
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;
102 bool bit (unsigned index
) const throw(std::out_of_range
);
103 bool operator [] (unsigned index
) const throw(std::out_of_range
);
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
);
109 inf
slice(unsigned low
, unsigned high
) const throw(std::out_of_range
);
111 //////////////////////////////////////////////////////////////////////////////
112 // tests for common values or ranges
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;
120 // tests used in if(i) and if(!i)
121 // operator bool (void) const;
122 bool operator ! (void) const;
124 //////////////////////////////////////////////////////////////////////////////
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;
134 //////////////////////////////////////////////////////////////////////////////
135 // bitwise logic operations
138 inf
operator ~ (void) const;
140 inf
& operator &= (const inf
&);
141 inf
operator & (const inf
&) const;
143 inf
& operator |= (const inf
&);
144 inf
operator | (const inf
&) const;
146 inf
& operator ^= (const inf
&);
147 inf
operator ^ (const inf
&) const;
149 inf
& operator <<= (unsigned shift
);
150 inf
operator << (unsigned shift
) const;
152 inf
& operator >>= (unsigned shift
);
153 inf
operator >> (unsigned shift
) const;
155 //////////////////////////////////////////////////////////////////////////////
156 // arithmetic operations
159 inf
operator - (void) const;
162 friend inf
abs(const inf
&);
164 inf
& operator += (const inf
&);
165 inf
operator + (const inf
&) const;
167 inf
& operator -= (const inf
&);
168 inf
operator - (const inf
&) const;
170 inf
& operator *= (const inf
&);
171 inf
operator * (const inf
&) const;
173 inf
& operator /= (const inf
&) throw(divide_by_zero
);
174 inf
operator / (const inf
&) const throw(divide_by_zero
);
176 inf
& operator %= (const inf
&) throw(divide_by_zero
);
177 inf
operator % (const inf
&) const throw(divide_by_zero
);
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
);
182 //////////////////////////////////////////////////////////////////////////////
183 // pre- and post- increment and decrement
185 inf
& operator ++ (void);
186 inf
operator ++ (int);
187 inf
& operator -- (void);
188 inf
operator -- (int);
190 //////////////////////////////////////////////////////////////////////////////
191 // string representation and I/O
193 std::string
image_debug(void) const;
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
);
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
);
206 //////////////////////////////////////////////////////////////////////////////
210 const std::string
& get_bytes(void) const;
211 void set_bytes(const std::string
&);
214 ////////////////////////////////////////////////////////////////////////////////
215 // redefine friends for gcc v4.1
219 ////////////////////////////////////////////////////////////////////////////////
221 std::ostream
& operator << (std::ostream
&, const inf
&);
222 std::istream
& operator >> (std::istream
&, inf
&);
224 ////////////////////////////////////////////////////////////////////////////////
226 } // end namespace stlplus
This page took 0.042921 seconds and 4 git commands to generate.