diff options
author | Ben Boeckel <MathStuf@gmail.com> | 2008-12-23 14:07:52 -0500 |
---|---|---|
committer | Ben Boeckel <MathStuf@gmail.com> | 2008-12-23 14:07:52 -0500 |
commit | b0630958114b8e2057fb656c7dd7c7b98b76ce63 (patch) | |
tree | b357db7459974090d3816fa48c346372ab80e6c7 /sigcore | |
parent | a123a4793cba7a6609ab917172fd3273fc2d0307 (diff) | |
download | sigen-b0630958114b8e2057fb656c7dd7c7b98b76ce63.tar.gz sigen-b0630958114b8e2057fb656c7dd7c7b98b76ce63.tar.xz sigen-b0630958114b8e2057fb656c7dd7c7b98b76ce63.zip |
Added Range class
Diffstat (limited to 'sigcore')
-rw-r--r-- | sigcore/CMakeLists.txt | 2 | ||||
-rw-r--r-- | sigcore/Range.cpp | 170 | ||||
-rw-r--r-- | sigcore/Range.h | 134 |
3 files changed, 306 insertions, 0 deletions
diff --git a/sigcore/CMakeLists.txt b/sigcore/CMakeLists.txt index d0ef426d..2bafe851 100644 --- a/sigcore/CMakeLists.txt +++ b/sigcore/CMakeLists.txt @@ -9,10 +9,12 @@ SET(sigcore_HEADERS Global.h Hat.h Matrix.h + Range.h Script.h ) SET(sigcore_SRCS Fraction.cpp + Range.cpp Script.cpp ) diff --git a/sigcore/Range.cpp b/sigcore/Range.cpp new file mode 100644 index 00000000..4b57b986 --- /dev/null +++ b/sigcore/Range.cpp @@ -0,0 +1,170 @@ +/* + * Copyright 2008 Ben Boeckel <MathStuf@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +// Header include +#include "Range.h" + +Sigcore::Range::Range() +{ + set(1, -1); +} + +Sigcore::Range::Range(const double minimum, const double maximum) +{ + set(minimum, maximum); +} + +void Sigcore::Range::set(const double minimum, const double maximum) +{ + m_minimum = minimum; + m_maximum = maximum; +} + +void Sigcore::Range::setMinimum(const double minimum) +{ + if (minimum <= m_maximum) + m_minimum = minimum; + else + m_minimum = m_maximum = minimum; +} + +void Sigcore::Range::setMaximum(const double maximum) +{ + if (m_minimum <= maximum) + m_maximum = maximum; + else + m_minimum = m_maximum = maximum; +} + +double Sigcore::Range::minimum() const +{ + return m_minimum; +} + +double Sigcore::Range::maximum() const +{ + return m_maximum; +} + +bool Sigcore::Range::valid() const +{ + return (m_minimum <= m_maximum); +} + +bool Sigcore::Range::in(const double value) const +{ + return ((m_minimum <= value) && (value <= m_maximum)); +} + +bool Sigcore::Range::in(const Range& range) const +{ + return (range.valid() || ((m_minimum <= range.m_minimum) && (range.m_maximum <= m_maximum))); +} + +Sigcore::Range Sigcore::Range::operator&(const Range& rhs) const +{ + return Range(qMax(m_minimum, rhs.m_minimum), qMin(m_maximum, rhs.m_maximum)); +} + +Sigcore::Range& Sigcore::Range::operator&=(const Range& rhs) +{ + return *this = *this & rhs; +} + +Sigcore::Range Sigcore::Range::operator|(const Range& rhs) const +{ + return Range(qMin(m_minimum, rhs.m_minimum), qMax(m_maximum, rhs.m_maximum)); +} + +Sigcore::Range& Sigcore::Range::operator|=(const Range& rhs) +{ + return *this = *this | rhs; +} + +Sigcore::Range& Sigcore::Range::operator=(const Range& rhs) +{ + if (this == &rhs) + return *this; + m_minimum = rhs.m_minimum; + m_maximum = rhs.m_maximum; + return *this; +} + +Sigcore::Range Sigcore::Range::operator-() const +{ + return Range(-m_maximum, -m_minimum); +} + +Sigcore::Range Sigcore::Range::operator+(const Range& rhs) const +{ + return Range(m_minimum + rhs.m_minimum, m_maximum + rhs.m_maximum); +} + +Sigcore::Range Sigcore::Range::operator-(const Range& rhs) const +{ + return Range(m_minimum - rhs.m_maximum, m_maximum - rhs.m_minimum); +} + +Sigcore::Range Sigcore::Range::operator*(const Range& rhs) const +{ + const double minMin = m_minimum * rhs.m_minimum; + const double minMax = m_minimum * rhs.m_maximum; + const double maxMin = m_maximum * rhs.m_minimum; + const double maxMax = m_maximum * rhs.m_maximum; + return Range(qMin(qMin(minMin, minMax), qMin(maxMin, maxMax)), qMax(qMax(minMin, minMax), qMax(maxMin, maxMax))); +} + +Sigcore::Range Sigcore::Range::operator/(const Range& rhs) const +{ + if (rhs.in(0)) + return Range(1, -1); + const double minMin = m_minimum / rhs.m_minimum; + const double minMax = m_minimum / rhs.m_maximum; + const double maxMin = m_maximum / rhs.m_minimum; + const double maxMax = m_maximum / rhs.m_maximum; + return Range(qMin(qMin(minMin, minMax), qMin(maxMin, maxMax)), qMax(qMax(minMin, minMax), qMax(maxMin, maxMax))); +} + +Sigcore::Range& Sigcore::Range::operator+=(const Range& rhs) +{ + return *this = *this + rhs; +} + +Sigcore::Range& Sigcore::Range::operator-=(const Range& rhs) +{ + return *this = *this - rhs; +} + +Sigcore::Range& Sigcore::Range::operator*=(const Range& rhs) +{ + return *this = *this * rhs; +} + +Sigcore::Range& Sigcore::Range::operator/=(const Range& rhs) +{ + return *this = *this / rhs; +} + +bool Sigcore::Range::operator==(const Range& rhs) const +{ + return ((m_minimum == rhs.m_minimum) && (m_maximum == rhs.m_maximum)); +} + +bool Sigcore::Range::operator!=(const Range& rhs) const +{ + return !(*this == rhs); +} diff --git a/sigcore/Range.h b/sigcore/Range.h new file mode 100644 index 00000000..a2515845 --- /dev/null +++ b/sigcore/Range.h @@ -0,0 +1,134 @@ +/* + * Copyright 2008 Ben Boeckel <MathStuf@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +/** + * \file sigcore/Range.h + */ + +#ifndef SIGCORE_RANGE +#define SIGCORE_RANGE + +// Sigcore includes +#include "Global.h" + +namespace Sigcore +{ +/** + * \class Sigcore::Range Range.h sigcore/Range.h + * \brief Class that represents a range of values. + */ +class SIGCORE_EXPORT Range +{ + public: + /** + * Default constructor. + */ + Range(); + + /** + * Constructor. + * + * \param minimum The minimum value of the range. + * \param maximum The maximum value of the range. + */ + Range(const double minimum, const double maximum); + + /** + * Sets the values for the range. + * + * \param minimum The minimum value of the range. + * \param maximum The maximum value of the range. + */ + void set(const double minimum, const double maximum); + /** + * Set the minimum value for the range. + * + * \param minimum The minimum value of the range. + */ + void setMinimum(const double minimum); + /** + * Set the maximum value for the range. + * + * \param maximum The maximum value of the range. + */ + void setMaximum(const double maximum); + + /** + * \sa setMinimum + * + * \return The minimum value of the range. + */ + double minimum() const; + /** + * \sa setMaximum + * + * \return The maximum value of the range. + */ + double maximum() const; + + /** + * \return \p true if the range is valid, \p false otherwise. + */ + bool valid() const; + + /** + * \return \p true if the range contains \p value, \p false otherwise. + */ + bool in(const double value) const; + /** + * \return \p true if \p range is completely within the range, \p false otherwise. + */ + bool in(const Range& range) const; + + /** + * Intersects two ranges. + * + * \param rhs The range to intersect with. + * \return The common range. + */ + Range operator&(const Range& rhs) const; + Range& operator&=(const Range& rhs); + + /** + * Merges two ranges. + * + * \param rhs The range to merge with. + * \return The merged range. + */ + Range operator|(const Range& rhs) const; + Range& operator|=(const Range& rhs); + + Range& operator=(const Range& rhs); + Range operator-() const; + Range operator+(const Range& rhs) const; + Range operator-(const Range& rhs) const; + Range operator*(const Range& rhs) const; + Range operator/(const Range& rhs) const; + Range& operator+=(const Range& rhs); + Range& operator-=(const Range& rhs); + Range& operator*=(const Range& rhs); + Range& operator/=(const Range& rhs); + bool operator==(const Range& rhs) const; + bool operator!=(const Range& rhs) const; + private: + double m_minimum; + double m_maximum; +}; +} +Q_DECLARE_METATYPE(Sigcore::Range) + +#endif |