From ea05c9c5fa30b8822f618e861d12a09df1f8f017 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Mon, 28 May 2012 13:00:50 +1000 Subject: Fix memory error and x = max(-inf, expr) stuff. --- impl/Complete.hpp | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 impl/Complete.hpp (limited to 'impl/Complete.hpp') diff --git a/impl/Complete.hpp b/impl/Complete.hpp new file mode 100644 index 0000000..1cf40af --- /dev/null +++ b/impl/Complete.hpp @@ -0,0 +1,119 @@ +#ifndef COMPLETE_HPP +#define COMPLETE_HPP + +#include +#include + +template +struct Complete { + Complete() + : _value(0), _infinity(false) { } + Complete(const T& value, const bool& infinity) + : _value(value), _infinity(infinity) { + if (value == 0 && infinity == true) { + throw "Zero infinity? Die die die!"; + } + } + Complete(const Complete& other) + : _value(other._value), _infinity(other._infinity) { } + + Complete& operator=(const Complete& other) { + _value = other._value; + _infinity = other._infinity; + return *this; + } + Complete& operator+=(const Complete& other) { + return (*this) = (*this) + other; + } + Complete& operator-=(const Complete& other) { + return (*this) = (*this) - other; + } + + Complete operator-() const { + return Complete(- _value, _infinity); + } + Complete operator+(const Complete& other) const { + if (_infinity) { + return *this; + } else if (other._infinity) { + return other; + } else { + return Complete(_value + other._value, false); + } + } + Complete operator-(const Complete& other) const { + return *this + (- other); + } + Complete operator*(const Complete& other) const { + if (_infinity) { + return *this; + } else if (other._infinity) { + return other; + } else { + return Complete(_value + other._value, false); + } + } + + bool operator!() const { + return _value == 0; + } + bool operator<(const Complete& other) const { + if (*this == other) + return false; + if (_infinity) { + return _value < 0; + } else if (other._infinity) { + return other._value > 0; + } else { + return _value < other._value; + } + } + bool operator>(const Complete& other) const { + return !(*this < other || *this == other); + } + bool operator==(const Complete& other) const { + if (_infinity) { + return other._infinity && ((_value < 0 && other._value < 0) || + (_value > 0 && other._value > 0)); + } else { + return _value == other._value; + } + } + bool operator!=(const Complete& other) const { + return !(*this == other); + } + + template + friend std::istream& operator<<(std::istream&, Complete&); + template + friend std::ostream& operator<<(std::ostream&, const Complete&); + + private: + T _value; + bool _infinity; +}; + +template +std::istream& operator>>(std::istream& cin, Complete& num) { + Z value; + cin >> value; + num = Complete(value, false); + return cin; +} + +template +std::ostream& operator<<(std::ostream& cout, const Complete& num) { + if (num._infinity) { + cout << (num._value > 0 ? "inf" : "-inf"); + } else { + cout << num._value; + } + return cout; +} + +template<> +Complete infinity() { + return Complete(1, true); +} + +#endif -- cgit v1.2.3