Chris@1186: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@1186: Chris@1186: /* Chris@1186: Sonic Visualiser Chris@1186: An audio file viewer and annotation editor. Chris@1186: Centre for Digital Music, Queen Mary, University of London. Chris@1186: This file copyright 2006 Chris Cannam and QMUL. Chris@1186: Chris@1186: This program is free software; you can redistribute it and/or Chris@1186: modify it under the terms of the GNU General Public License as Chris@1186: published by the Free Software Foundation; either version 2 of the Chris@1186: License, or (at your option) any later version. See the file Chris@1186: COPYING included with this distribution for more information. Chris@1186: */ Chris@1186: Chris@1186: #ifndef MAGNITUDE_RANGE_H Chris@1186: #define MAGNITUDE_RANGE_H Chris@1186: Chris@1194: #include Chris@1194: Chris@1186: /** Chris@1186: * Maintain a min and max value, and update them when supplied a new Chris@1186: * data point. Chris@1186: */ Chris@1186: class MagnitudeRange Chris@1186: { Chris@1186: public: Chris@1186: MagnitudeRange() : m_min(0), m_max(0) { } Chris@1186: bool operator==(const MagnitudeRange &r) { Chris@1186: return r.m_min == m_min && r.m_max == m_max; Chris@1186: } Chris@1186: bool isSet() const { return (m_min != 0.f || m_max != 0.f); } Chris@1186: void set(float min, float max) { Chris@1186: m_min = min; Chris@1186: m_max = max; Chris@1186: if (m_max < m_min) m_max = m_min; Chris@1186: } Chris@1186: bool sample(float f) { Chris@1186: bool changed = false; Chris@1186: if (isSet()) { Chris@1186: if (f < m_min) { m_min = f; changed = true; } Chris@1186: if (f > m_max) { m_max = f; changed = true; } Chris@1186: } else { Chris@1186: m_max = m_min = f; Chris@1186: changed = true; Chris@1186: } Chris@1186: return changed; Chris@1194: } Chris@1194: bool sample(const std::vector &ff) { Chris@1194: bool changed = false; Chris@1194: for (auto f: ff) { Chris@1194: if (sample(f)) { Chris@1194: changed = true; Chris@1194: } Chris@1194: } Chris@1194: return changed; Chris@1194: } Chris@1186: bool sample(const MagnitudeRange &r) { Chris@1186: bool changed = false; Chris@1186: if (isSet()) { Chris@1186: if (r.m_min < m_min) { m_min = r.m_min; changed = true; } Chris@1186: if (r.m_max > m_max) { m_max = r.m_max; changed = true; } Chris@1186: } else { Chris@1186: m_min = r.m_min; Chris@1186: m_max = r.m_max; Chris@1186: changed = true; Chris@1186: } Chris@1186: return changed; Chris@1186: } Chris@1186: float getMin() const { return m_min; } Chris@1186: float getMax() const { return m_max; } Chris@1186: private: Chris@1186: float m_min; Chris@1186: float m_max; Chris@1186: }; Chris@1186: Chris@1186: #endif