File indexing completed on 2024-05-19 15:01:40

0001 /***************************************************************************
0002     File                 : Range.h
0003     Project              : LabPlot
0004     --------------------------------------------------------------------
0005     Copyright            : (C) 2020 Stefan Gerlach (stefan.gerlach@uni.kn)
0006     Description          : basic data range class
0007 
0008  ***************************************************************************/
0009 
0010 /***************************************************************************
0011  *                                                                         *
0012  *  This program is free software; you can redistribute it and/or modify   *
0013  *  it under the terms of the GNU General Public License as published by   *
0014  *  the Free Software Foundation; either version 2 of the License, or      *
0015  *  (at your option) any later version.                                    *
0016  *                                                                         *
0017  *  This program is distributed in the hope that it will be useful,        *
0018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0020  *  GNU General Public License for more details.                           *
0021  *                                                                         *
0022  *   You should have received a copy of the GNU General Public License     *
0023  *   along with this program; if not, write to the Free Software           *
0024  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0025  *   Boston, MA  02110-1301  USA                                           *
0026  *                                                                         *
0027  ***************************************************************************/
0028 
0029 #ifndef RANGE_H
0030 #define RANGE_H
0031 
0032 extern "C" {
0033 #include "backend/gsl/parser.h"
0034 }
0035 
0036 #include "backend/lib/macros.h" //SET_NUMBER_LOCALE
0037 
0038 #include <QString>
0039 
0040 //! Auxiliary class for a data range 
0041 /**
0042  *  This class represents a data range [left, right] with right >= left.
0043  *
0044  *  Only types supporting comparison are supported
0045  */
0046 template<class T>
0047 class Range {
0048 public:
0049     Range() : m_min(0), m_max(0) {}
0050     Range(T min, T max) {
0051         this->setRange(min, max);
0052     }
0053     Range(const QString& min, const QString& max) {
0054         SET_NUMBER_LOCALE
0055         //TODO: check for NAN, INF?
0056         this->setRange(parse(qPrintable(min.simplified()), qPrintable(numberLocale.name())), parse(qPrintable(max.simplified()), qPrintable(numberLocale.name())));
0057     }
0058     ~Range() = default;
0059     T min() const { return m_min; }
0060     T max() const { return m_max; }
0061     T& min() { return m_min; }
0062     T& max() { return m_max; }
0063     void setMin(T min) { m_min = min; } // no check (use first)
0064     void setMax(T max) { m_max = std::max(m_min, max); }
0065     void setRange(T min, T max) {
0066         m_min = min;
0067         m_max = std::max(min, max);
0068     }
0069     T size() const { return m_max - m_min; }
0070     // calculate step size from number of steps
0071     T stepSize(const int steps) const { return (steps > 1) ? size()/(T)(steps - 1) : 0; }
0072     bool isZero() const { return (m_max == m_min); }
0073     bool inside(const Range<T>& other) const { return ( m_min <= other.min() && m_max >= other.max() ); }
0074     bool inside(T value) const { return ( m_min <= value && m_max >= value ); }
0075     void translate(T offset) { m_min += offset; m_max += offset; }
0076     bool operator==(const Range<T>& other) const { return ( m_min == other.min() && m_max == other.max() ); }
0077     Range<T>& operator=(const Range<T>& other) = default;
0078 
0079     //! Return a string in the format '[min, max]'
0080     QString toString() const {
0081         return "[" + QLocale().toString(m_min) + ", " + QLocale().toString(m_max) + "]";
0082     }
0083     //TODO: touches(), merge(), subtract(), split(), etc. (see Interval)
0084 
0085 private:
0086     T m_min;    // lower limit
0087     T m_max;    // upper limit
0088 };
0089 
0090 #endif
0091