]> Dogcows Code - chaz/yoink/blobdiff - src/stlplus/portability/inf.hpp
testing new non-autotools build system
[chaz/yoink] / src / stlplus / portability / inf.hpp
diff --git a/src/stlplus/portability/inf.hpp b/src/stlplus/portability/inf.hpp
new file mode 100644 (file)
index 0000000..f28541a
--- /dev/null
@@ -0,0 +1,228 @@
+#ifndef STLPLUS_INF\r
+#define STLPLUS_INF\r
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+//   Author:    Andy Rushton\r
+//   Copyright: (c) Southampton University 1999-2004\r
+//              (c) Andy Rushton           2004-2009\r
+//   License:   BSD License, see ../docs/license.html\r
+\r
+//   An infinite-precision integer class. This allows calculations on large\r
+//   integers to be performed without overflow.\r
+\r
+//   this class can throw the following exceptions:\r
+//     std::out_of_range\r
+//     std::overflow_error\r
+//     std::invalid_argument\r
+//     stlplus::divide_by_zero    // why doesn't std have this?\r
+//   all of these are derivations of the baseclass:\r
+//     std::logic_error\r
+//   So you can catch all of them by catching the baseclass\r
+\r
+//   Warning: inf was never intended to be fast, it is just for programs which\r
+//   need a bit of infinite-precision integer arithmetic. For high-performance\r
+//   processing, use the Gnu Multi-Precision (GMP) library. The inf type is just\r
+//   easier to integrate and is already ported to all platforms and compilers\r
+//   that STLplus is ported to.\r
+\r
+////////////////////////////////////////////////////////////////////////////////\r
+#include "portability_fixes.hpp"\r
+#include "portability_exceptions.hpp"\r
+#include <string>\r
+#include <iostream>\r
+\r
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+namespace stlplus\r
+{\r
+\r
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+  class inf\r
+  {\r
+  public:\r
+\r
+    //////////////////////////////////////////////////////////////////////////////\r
+    // constructors and assignments initialise the inf\r
+\r
+    // the void constructor initialises to zero, the others initialise to the\r
+    // value of the C integer type or the text value contained in the string\r
+\r
+    inf(void);\r
+    explicit inf(short);\r
+    explicit inf(unsigned short);\r
+    explicit inf(int);\r
+    explicit inf(unsigned);\r
+    explicit inf(long);\r
+    explicit inf(unsigned long);\r
+    explicit inf(const std::string&) throw(std::invalid_argument);\r
+    inf(const inf&);\r
+\r
+    ~inf(void);\r
+\r
+    // assignments with equivalent behaviour to the constructors\r
+\r
+    inf& operator = (short);\r
+    inf& operator = (unsigned short);\r
+    inf& operator = (int);\r
+    inf& operator = (unsigned);\r
+    inf& operator = (long);\r
+    inf& operator = (unsigned long);\r
+    inf& operator = (const std::string&) throw(std::invalid_argument);\r
+    inf& operator = (const inf&);\r
+\r
+    //////////////////////////////////////////////////////////////////////////////\r
+    // conversions back to the C types\r
+    // truncate: controls the behaviour when the value is too long for the result\r
+    //           true: truncate the value\r
+    //           false: throw an exception\r
+\r
+    short to_short(bool truncate = true) const throw(std::overflow_error);\r
+    unsigned short to_unsigned_short(bool truncate = true) const throw(std::overflow_error);\r
+\r
+    int to_int(bool truncate = true) const throw(std::overflow_error);\r
+    unsigned to_unsigned(bool truncate = true) const throw(std::overflow_error);\r
+\r
+    long to_long(bool truncate = true) const throw(std::overflow_error);\r
+    unsigned long to_unsigned_long(bool truncate = true) const throw(std::overflow_error);\r
+\r
+    //////////////////////////////////////////////////////////////////////////////\r
+    // bitwise manipulation\r
+\r
+    void resize(unsigned bits);\r
+    void reduce(void);\r
+\r
+    // the number of significant bits in the value\r
+    unsigned bits (void) const;\r
+    unsigned size (void) const;\r
+\r
+    // the number of bits that can be accessed by the bit() method (=bits() rounded up to the next byte)\r
+    unsigned indexable_bits(void) const;\r
+\r
+    bool bit (unsigned index) const throw(std::out_of_range);\r
+    bool operator [] (unsigned index) const throw(std::out_of_range);\r
+\r
+    void set (unsigned index) throw(std::out_of_range);\r
+    void clear (unsigned index) throw(std::out_of_range);\r
+    void preset (unsigned index, bool value) throw(std::out_of_range);\r
+\r
+    inf slice(unsigned low, unsigned high) const throw(std::out_of_range);\r
+\r
+    //////////////////////////////////////////////////////////////////////////////\r
+    // tests for common values or ranges\r
+\r
+    bool negative (void) const;\r
+    bool natural (void) const;\r
+    bool positive (void) const;\r
+    bool zero (void) const;\r
+    bool non_zero (void) const;\r
+\r
+    // tests used in if(i) and if(!i)\r
+//  operator bool (void) const;\r
+    bool operator ! (void) const;\r
+\r
+    //////////////////////////////////////////////////////////////////////////////\r
+    // comparisons\r
+\r
+    bool operator == (const inf&) const;\r
+    bool operator != (const inf&) const;\r
+    bool operator < (const inf&) const;\r
+    bool operator <= (const inf&) const;\r
+    bool operator > (const inf&) const;\r
+    bool operator >= (const inf&) const;\r
+\r
+    //////////////////////////////////////////////////////////////////////////////\r
+    // bitwise logic operations\r
+\r
+    inf& invert (void);\r
+    inf operator ~ (void) const;\r
+\r
+    inf& operator &= (const inf&);\r
+    inf operator & (const inf&) const;\r
+\r
+    inf& operator |= (const inf&);\r
+    inf operator | (const inf&) const;\r
+\r
+    inf& operator ^= (const inf&);\r
+    inf operator ^ (const inf&) const;\r
+\r
+    inf& operator <<= (unsigned shift);\r
+    inf operator << (unsigned shift) const;\r
+\r
+    inf& operator >>= (unsigned shift);\r
+    inf operator >> (unsigned shift) const;\r
+\r
+    //////////////////////////////////////////////////////////////////////////////\r
+    // arithmetic operations\r
+\r
+    inf& negate (void);\r
+    inf operator - (void) const;\r
+\r
+    inf& abs(void);\r
+    friend inf abs(const inf&);\r
+\r
+    inf& operator += (const inf&);\r
+    inf operator + (const inf&) const;\r
+\r
+    inf& operator -= (const inf&);\r
+    inf operator - (const inf&) const;\r
+\r
+    inf& operator *= (const inf&);\r
+    inf operator * (const inf&) const;\r
+\r
+    inf& operator /= (const inf&) throw(divide_by_zero);\r
+    inf operator / (const inf&) const throw(divide_by_zero);\r
+\r
+    inf& operator %= (const inf&) throw(divide_by_zero);\r
+    inf operator % (const inf&) const throw(divide_by_zero);\r
+\r
+    // combined division operator - returns the result pair(quotient,remainder) in one go\r
+    std::pair<inf,inf> divide(const inf&) const throw(divide_by_zero);\r
+\r
+    //////////////////////////////////////////////////////////////////////////////\r
+    // pre- and post- increment and decrement\r
+\r
+    inf& operator ++ (void);\r
+    inf operator ++ (int);\r
+    inf& operator -- (void);\r
+    inf operator -- (int);\r
+\r
+    //////////////////////////////////////////////////////////////////////////////\r
+    // string representation and I/O\r
+\r
+    std::string image_debug(void) const;\r
+\r
+    // conversion to a string representation\r
+    // radix must be 10, 2, 8 or 16\r
+    std::string to_string(unsigned radix = 10) const\r
+      throw(std::invalid_argument);\r
+\r
+    // conversion from a string\r
+    // radix == 0 - radix is deduced from the input - assumed 10 unless number is prefixed by 0b, 0 or 0x\r
+    // however, you can specify the radix to be 10, 2, 8 or 16 to force that interpretation\r
+    inf& from_string(const std::string&, unsigned radix = 0)\r
+      throw(std::invalid_argument);\r
+\r
+    //////////////////////////////////////////////////////////////////////////////\r
+  private:\r
+    std::string m_data;\r
+  public:\r
+    const std::string& get_bytes(void) const;\r
+    void set_bytes(const std::string&);\r
+  };\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+  // redefine friends for gcc v4.1\r
+\r
+  inf abs(const inf&);\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+\r
+  std::ostream& operator << (std::ostream&, const inf&);\r
+  std::istream& operator >> (std::istream&, inf&);\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+\r
+} // end namespace stlplus\r
+\r
+#endif\r
This page took 0.028373 seconds and 4 git commands to generate.